Subject: Re: bug#22873: Can we support multiple Cursors?

From: Keith David Bershatsky <[email-masked]>
Date: Sun, 25 Jun 2017 15:09:16 -0700

WHAT'S NEW:

-  Fake cursors now only appear in the selected-window if the `mc-list' is defined -- i.e., the fake cursors are removed when the window loses focus, and they are added again when the window acquires focus.

-  `mc_color_vector_calculate` will take the familiar Emacs way of specifying colors (e.g., "red" and "#FF0000") and convert them internally to the LSL color vector.  The conversion process is transparent to the user.

-  `mc_remove_when_scrolled` will remove multiple cursors when scrolling.  This is important because `update_window_end` will not remove the cursors when scrolling.  [There was an emacs-devel thread (about 1.5 years ago) where I had sought help tracking the multiple cursors when scrolling.  It turned out that the method of tracking cursors was not the problem (since they don't move) -- it was simply that `update_window_end` was not being called in that circumstance.]

-  `mc_x_y_hpos_vpos' contains some extra `stuff` that is irrelevant to the functionality of this patch -- the extra `stuff` is used by me as a workaround to deal with the overlay after-string property that gets in the way of properly calculating coordinates.  [See the TODO section below.]

-  The patch applies to commit a30e7e12ed8465e2565dd318d921bc87f52ce18e from 03/28/2016.  [See apology for the inconvenience further down below.]

TODO:

-  Optimize drawing and removal of multiple fake cursors.

-  Fix any bugs (there will surely be many).

-  Implement a way to properly calculate x, y, hpos, vpos when overlays are present -- e.g., the overlay after-string property wreaks havoc on the current method of calculating coordinates.

-  Try and convince one or more real programmers to take over from here, since we now have a working proof concept.  [I am just a weekend hobbyist without any formal programming study.]

SUGGESTION:

I would respectfully suggest to the Emacs development team that multiple cursors be implemented in two stages:

-  The first stage would be the creation and removal of fake cursors to be used kind of like overlays.  I.e., specify the `point`, cursor-style, and color.  The suggestion by Eli Z. to support specifying colors with strings like "red" and "#FF0000" has now been implemented.

-  The second stage would be to implement built-in functionality similar to the multiple cursors library by Magnar Sveen.

[In an earlier section of this thread, I mentioned a substantial slow-down that I experienced when using the library written by Mr. Sveen.  I have since discovered that it was due to `line-number-at-pos', and I have already reached out to the author with an alternative suggestion to use (format-mode-line "%l").]

INSTALLATION (for anyone a little less familiar):

STEP #1 (clone master branch):  git clone -b master git://git.sv.gnu.org/emacs.git

STEP #2:  cd over to the root of the `emacs` source directory cloned in the first step above.

STEP #3 (hard reset):  git reset --hard a30e7e12ed8465e2565dd318d921bc87f52ce18e

You will see a message:  "HEAD is now at a30e7e1 Mention the `M-q' changes"

[We need to go back in time to 03/28/2016 as that is the master branch version that I am still using -- sorry for the inconvenience.  I will update to the latest version of Emacs at some point in the future as time permits.  Creating patches that apply to a "moving target" is challenging.]

STEP #4 (put the patch in place):  Copy the latest patch (multiple_cursors_008.diff) to the root of the emacs source directory cloned in the first step above.

STEP #5 (apply the patch):  git apply multiple_cursors_008.diff

STEP #6:  ./autogen.sh

STEP #7:  Configure the build for X, MS Windows, or OSX; e.g.,:  ./configure --with-ns

STEP #8:  make

STEP #9:  make install

STEP #10:  Launch the newly built Emacs, and copy the following function to the `*scratch*` buffer, and type `M-x mc-test`.

(defun mc-test ()
"Draw fake cursors at all POS defined in the `mc-list'.  Multiple fake cursors
are supported by GUI versions of Emacs built for X, Windows and OSX.
Popular forms of specifying colors such as \"red\" and \"#FF0000\" are now
supported, as well as LSL color vectors such as [1.0 0.0 0.0].  For those users
who choose the former familiar methods of specifying colors with strings,
`mc_color_vector_calculate' will convert those strings to LSL color vectors.
The color vectors are commonly referred to as LSL (The Linden Scripting Language).
`nsterm.m' uses `NSColor', which works well with LSL.  `w32term.c' uses
`PALETTERGB' or `RGB', and the conversion from LSL is done internally by
multiplying each element of the LSL color vector by 255.  `xterm.c' uses
`x_make_truecolor_pixel', which uses 16-bit RGB -- the conversion from LSL
happens internally by multiplying each element of the LSL color vector by 65535."
(interactive)
  (let ((buffer (get-buffer-create "*MC-TEST*")))
    (with-current-buffer buffer
      (erase-buffer)
      (insert "This is a test!")
      (setq mc-list '((1 "hbar" "red")
                      (2 "bar" "yellow")
                      (3 "box" "#00FF00")
                      (4 "hollow" "#0000FF")
                      (5 ("hbar" 3) [1.0 0.0 1.0])
                      (6 ("bar" 3) [0.0 1.0 1.0]))))
    (select-window (display-buffer buffer))
    ;;; The trigger in `keyboard.c` is not activated in this example, so we
    ;;; Force the multiple cursors to be drawn.
    (mc-draw-erase (selected-window))))