unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Keith David Bershatsky <esq@lawlist.com>
To: "John Wiegley" <johnw@gnu.org>
Cc: Marcin Borkowski <mbork@mbork.pl>,
	22873@debbugs.gnu.org, Richard Stallman <rms@gnu.org>
Subject: bug#22873: #22873 (multiple fake cursors)
Date: Wed, 27 Dec 2017 17:20:46 -0800	[thread overview]
Message-ID: <m2o9mjbqc1.wl%esq@lawlist.com> (raw)
In-Reply-To: <m2oaayavh1.wl%esq@lawlist.com>

John:  Here is an example modification to the most recent version Magnar's multiple-cursors package to make use of #22873 with the patch.diff from earlier today for Emacs 26.  I like yellow for even numbered columns and red for odd numbered columns, and I like the vertical bar fake cursor, so that is what is used in this example.

(require 'multiple-cursors-core)
(require 'cl) ;; for oddp and evenp

(add-hook 'multiple-cursors-mode-disabled-hook
          (lambda () (kill-local-variable 'mc-glyph-list)))

(defvar mc/use-built-in-cursors t
"Whether to use the built-in fake cursors.")

(defun mc/create-fake-cursor-at-point (&optional id)
  "Add a fake cursor and possibly a fake active region overlay based on point and mark.
Saves the current state in the overlay to be restored later."
  (unless mc--max-cursors-original
    (setq mc--max-cursors-original mc/max-cursors))
  (when mc/max-cursors
    (unless (< (mc/num-cursors) mc/max-cursors)
      (if (yes-or-no-p (format "%d active cursors. Continue? " (mc/num-cursors)))
          (setq mc/max-cursors (read-number "Enter a new, temporary maximum: "))
        (mc/remove-fake-cursors)
        (error "Aborted: too many cursors"))))
  (let ((overlay
          (if mc/use-built-in-cursors
            (make-overlay (point) (point))
            (mc/make-cursor-overlay-at-point))))
    (overlay-put overlay 'mc-id (or id (mc/create-cursor-id)))
    (overlay-put overlay 'type 'fake-cursor)
    (overlay-put overlay 'priority 100)
    (mc/store-current-state-in-overlay overlay)
    (when (use-region-p)
      (overlay-put overlay 'region-overlay
                   (mc/make-region-overlay-between-point-and-mark)))
    overlay))

(defun mc/execute-this-command-for-all-cursors-1 ()
  "Used with post-command-hook to execute supported commands for all cursors.
  It uses two lists of commands to know what to do: the run-once
list and the run-for-all list. If a command is in neither of these lists,
it will prompt for the proper action and then save that preference.
  Some commands are so unsupported that they are even prevented for
the original cursor, to inform about the lack of support."
  (unless mc--executing-command-for-fake-cursor
    (if (eq 1 (mc/num-cursors)) ;; no fake cursors? disable mc/mode
        (mc/mode 0)
      (when this-original-command
        (let ((original-command (or mc--this-command
                                    (command-remapping this-original-command)
                                    this-original-command)))
          ;; skip keyboard macros, since they will generate actual commands that are
          ;; also run in the command loop - we'll handle those later instead.
          (when (functionp original-command)
            ;; if it's a lambda, we can't know if it's supported or not
            ;; - so go ahead and assume it's ok, because we're just optimistic like that
            (if (or (not (symbolp original-command))
                    ;; lambda registered by smartrep
                    (string-prefix-p "(" (symbol-name original-command)))
                (mc/execute-command-for-all-fake-cursors original-command)
              ;; smartrep `intern's commands into own obarray to help
              ;; `describe-bindings'.  So, let's re-`intern' here to
              ;; make the command comparable by `eq'.
              (setq original-command (intern (symbol-name original-command)))
              ;; otherwise it's a symbol, and we can be more thorough
              (if (get original-command 'mc--unsupported)
                  (message "%S is not supported with multiple cursors%s"
                           original-command
                           (get original-command 'mc--unsupported))
                (when (and original-command
                           (not (memq original-command mc--default-cmds-to-run-once))
                           (not (memq original-command mc/cmds-to-run-once))
                           (or (memq original-command mc--default-cmds-to-run-for-all)
                               (memq original-command mc/cmds-to-run-for-all)
                               (mc/prompt-for-inclusion-in-whitelist original-command)))
                  (mc/execute-command-for-all-fake-cursors original-command))))
            (when mc/use-built-in-cursors
              (let* ((win (selected-window))
                     (window-start (window-start win))
                     (window-end (window-end win))
                     (overlays (mc/all-fake-cursors))
                     (lst
                       (mapcar
                         (lambda (x)
                           (let* ((pos (overlay-start x))
                                  (current-column
                                    (if (and (>= pos window-start)
                                             (<= pos window-end))
                                      (save-excursion
                                        (goto-char pos)
                                        (current-column))
                                      0)))
                             (cond
                               ((oddp current-column)
                                 (list pos "bar" "#FF0000")) ;; red or [1.0 0.0 0.0]
                               ((evenp current-column)
                                 (list pos "bar" "yellow"))))) ;; [1.0 1.0 0.0]
                         overlays)))
                (setq mc-glyph-list lst)))))))))


(defun mc/execute-this-command-for-all-cursors-1 ()
  "Used with post-command-hook to execute supported commands for all cursors.

It uses two lists of commands to know what to do: the run-once
list and the run-for-all list. If a command is in neither of these lists,
it will prompt for the proper action and then save that preference.

Some commands are so unsupported that they are even prevented for
the original cursor, to inform about the lack of support."
  (unless mc--executing-command-for-fake-cursor

    (if (eq 1 (mc/num-cursors)) ;; no fake cursors? disable mc-mode
        (multiple-cursors-mode 0)
      (when this-original-command
        (let ((original-command (or mc--this-command
                                    (command-remapping this-original-command)
                                    this-original-command)))

          ;; skip keyboard macros, since they will generate actual commands that are
          ;; also run in the command loop - we'll handle those later instead.
          (when (functionp original-command)

            ;; if it's a lambda, we can't know if it's supported or not
            ;; - so go ahead and assume it's ok, because we're just optimistic like that
            (if (or (not (symbolp original-command))
                    ;; lambda registered by smartrep
                    (string-prefix-p "(" (symbol-name original-command)))
                (mc/execute-command-for-all-fake-cursors original-command)

              ;; smartrep `intern's commands into own obarray to help
              ;; `describe-bindings'.  So, let's re-`intern' here to
              ;; make the command comparable by `eq'.
              (setq original-command (intern (symbol-name original-command)))

              ;; otherwise it's a symbol, and we can be more thorough
              (if (get original-command 'mc--unsupported)
                  (message "%S is not supported with multiple cursors%s"
                           original-command
                           (get original-command 'mc--unsupported))
                (when (and original-command
                           (not (memq original-command mc--default-cmds-to-run-once))
                           (not (memq original-command mc/cmds-to-run-once))
                           (or mc/always-run-for-all
                               (memq original-command mc--default-cmds-to-run-for-all)
                               (memq original-command mc/cmds-to-run-for-all)
                               (mc/prompt-for-inclusion-in-whitelist original-command)))
                  (mc/execute-command-for-all-fake-cursors original-command))))))

            (when mc/use-built-in-cursors
              (let* ((win (selected-window))
                     (window-start (window-start win))
                     (window-end (window-end win))
                     (overlays (mc/all-fake-cursors))
                     (lst
                       (mapcar
                         (lambda (x)
                           (let* ((pos (overlay-start x))
                                  (current-column
                                    (if (and (>= pos window-start)
                                             (<= pos window-end))
                                      (save-excursion
                                        (goto-char pos)
                                        (current-column))
                                      0)))
                             (cond
                               ((oddp current-column)
                                 (list pos "bar" "#FF0000")) ;; red or [1.0 0.0 0.0]
                               ((evenp current-column)
                                 (list pos "bar" "yellow"))))) ;; [1.0 1.0 0.0]
                         overlays)))
                (setq mc-glyph-list lst))) ))))





  parent reply	other threads:[~2017-12-28  1:20 UTC|newest]

Thread overview: 66+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-01 18:44 bug#22873: 25.1.50; Feature Request -- Multiple Cursors (built-in support) Keith David Bershatsky
2016-03-03  6:30 ` bug#22873: Can we support multiple Cursors? John Wiegley
     [not found] ` <m2h9gocbu9.fsf@newartisans.com>
2016-03-03  6:54   ` Marcin Borkowski
     [not found]   ` <8760x49hly.fsf@mbork.pl>
2016-03-03 11:20     ` Richard Stallman
     [not found]     ` <E1abRJS-0002hO-PV@fencepost.gnu.org>
2016-03-03 15:05       ` Marcin Borkowski
     [not found]       ` <87ziuf8uun.fsf@mbork.pl>
2016-03-04  9:19         ` Richard Stallman
2016-03-04 23:16 ` Keith David Bershatsky
2016-03-05  6:59   ` Marcin Borkowski
2016-03-09  6:27 ` Keith David Bershatsky
2016-03-09 16:03   ` Eli Zaretskii
2016-03-09 18:30 ` Keith David Bershatsky
2016-03-11  7:18 ` Keith David Bershatsky
2016-03-14 18:35 ` Keith David Bershatsky
2016-03-14 18:49   ` Eli Zaretskii
2016-03-14 22:38 ` Keith David Bershatsky
2016-03-16  8:00 ` Keith David Bershatsky
2016-03-18  4:00 ` Keith David Bershatsky
2016-03-26 23:58   ` John Wiegley
2016-03-29  3:45 ` Keith David Bershatsky
2016-03-29 14:58   ` Eli Zaretskii
2016-03-29 17:26 ` Keith David Bershatsky
2017-06-25 22:09 ` Keith David Bershatsky
2017-07-30 17:39 ` Keith David Bershatsky
2017-08-11  0:00 ` bug#22873: Can we support multiple cursors? Keith David Bershatsky
2017-08-13 18:19 ` Keith David Bershatsky
2017-08-13 18:36   ` Eli Zaretskii
2017-08-14  3:20 ` Keith David Bershatsky
2017-08-14 15:01   ` Eli Zaretskii
2017-08-14 20:35 ` Keith David Bershatsky
2017-12-27 17:13 ` bug#22873: #22873 (multiple fake cursors); and, #17684 (crosshairs) Keith David Bershatsky
2017-12-27 23:55   ` John Wiegley
2017-12-28  1:20 ` Keith David Bershatsky [this message]
2017-12-28  1:26 ` bug#22873: #22873 (multiple fake cursors) Keith David Bershatsky
2018-07-17 19:09 ` bug#22873: #22873 (multiple fake cursors); and, #17684 (crosshairs) Keith David Bershatsky
2018-08-29  6:39 ` Keith David Bershatsky
2019-05-03  0:48 ` Keith David Bershatsky
2019-05-06 18:39 ` Keith David Bershatsky
2019-05-28  8:31 ` Keith David Bershatsky
2019-06-02  7:29 ` Keith David Bershatsky
2019-07-16 19:28 ` Keith David Bershatsky
2019-07-23  6:01 ` Keith David Bershatsky
2019-08-23  5:19 ` Keith David Bershatsky
2019-10-17 21:08 ` bug#22873: #22873 (multiple fake cursors); and, #17684 (crosshairs / fill-column) Keith David Bershatsky
2020-03-04  9:03 ` Keith David Bershatsky
  -- strict thread matches above, loose matches on Subject: below --
2014-06-03 20:36 bug#17684: 24.4.50; Feature Request -- Vertical Lines to the Left of and Through Characters Keith David Bershatsky
2018-07-09  5:28 ` bug#17684: #22873 (multiple fake cursors); and, #17684 (crosshairs) Keith David Bershatsky
2018-11-11  3:36 ` Keith David Bershatsky
2018-11-11 16:51 ` Keith David Bershatsky
2018-11-21  4:53 ` bug#17684: #22873 (multiple fake cursors); and, #17684 (crosshairs / fill-column) Keith David Bershatsky
2018-12-14  7:11 ` Keith David Bershatsky
2019-04-09  4:03 ` bug#17684: #22873 (multiple fake cursors); and, #17684 (crosshairs) Keith David Bershatsky
2019-04-21  5:15 ` Keith David Bershatsky
2019-04-29  1:21 ` Keith David Bershatsky
2019-06-08 23:44 ` Keith David Bershatsky
2019-06-16  8:07 ` Keith David Bershatsky
2019-06-24  2:25 ` Keith David Bershatsky
2019-06-30  5:42 ` Keith David Bershatsky
2019-07-31 19:39 ` Keith David Bershatsky
2019-10-18  1:12 ` bug#17684: #22873 (multiple fake cursors); and, #17684 (crosshairs / fill-column) Keith David Bershatsky
2019-11-18  6:58 ` Keith David Bershatsky
2020-01-27  7:39 ` Keith David Bershatsky
2020-05-02 20:50 ` Keith David Bershatsky
2020-10-01  3:00   ` bug#17684: bug#22873: " Lars Ingebrigtsen
2020-10-01  3:54     ` Keith David Bershatsky
2020-10-01 16:21       ` Lars Ingebrigtsen
2020-10-01 17:00         ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2o9mjbqc1.wl%esq@lawlist.com \
    --to=esq@lawlist.com \
    --cc=22873@debbugs.gnu.org \
    --cc=johnw@gnu.org \
    --cc=mbork@mbork.pl \
    --cc=rms@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).