1

I’m trying to implement this when I press C-return:

  • If there’s an active region, it activates multiple cursors by calling mc/edit-lines
  • If there’s no active region, it initiates rectangular selection by calling cua-rectangle-mark-mode.

Here’s the code I wrote:

(defun cua-or-multicursor ()
  (interactive)
  (if (use-region-p)
      (progn
        (message "Using multi cursor~~") ;; For debug
        (mc/edit-lines))
    (progn
      (message "Using cua...") ;; For debug
      (cua-rectangle-mark-mode))))

(global-set-key (kbd "C-<return>") 'cua-or-multicursor)

The cua part of it seems to work appropriately. The problem is the multiple cursors part. I selected for example 2 lines (by setting the mark and moving down once), and pressed C-return, it prompted me whether I wanted to call cua-rectangle-mark-mode for all lines. I said yes. Then I started moving my cursors (it did give me 2 cursors), and I saw that the 2 cursors left a region behind them (which doesn’t happen if I just bind mc/edit-lines to C-return), just like if I set the mark and then move my cursor. Here’s what I mean:

Region behind multi cursor?

In the message buffer it said

Mark set
Using multicursor~~
Using cua...
Mark set
C-?:help M-p:pad M-o:open M-c:close M-b:blank M-s:string M-f:fill M-i:incr M-n:seq

It seems cua-rectangle-mark-mode is called right after multiple cursors… Any idea on why this would happen?

BTW I have GNU Emacs 24.4.1 on a Mac.

  • Is your goal to draw a vertical line of multiple cursors exactly where the cursor is located, or send all the cursors to the left (each line begin) or right (each line end) of the region selected? Have you also tried using (region-active-p) instead? – lawlist Mar 10 '15 at 4:26
  • @lawlist My usual way of using multiple cursors is to select some text first and then activate mc/edit-lines. So yes, I want multiple cursors to draw a vertical line. Overall what I want to achieve is to bind both multiple cursors and rectangle functionality of cua to C-return, and choose one of them depending on the existence of active region. – MetroWind Mar 10 '15 at 4:30
  • @lawlist, region-active-p seems to behave the same here. – MetroWind Mar 10 '15 at 4:45
1

You have likely chosen to activate your function cua-or-multicursor for all cursors -- probably by answering yes the first time you used it -- that initial choice was stored in the mc/list-file. The default location for the mc/list-file is "~/.emacs.d/.mc-lists.el". [Be sure to back it up before editing or deleting it, just in case it has a lot of choices you have previously stored.] You need to manually edit or delete the mc/list-file, and add your new function cua-or-multicursor to the run only once list of multiple-cursors-core.el -- i.e., mc--default-cmds-to-run-once (this is a list of many such functions).

Your function as written is just fine. What is happening is that your function works correctly for the first cursor, but because you repeat the function for each subsequent cursor, the region is no longer active and that is why the else statement of your function is activating. Your function only needs to run once (not for each cursor).

  • Wow this is indeed what happened. Thanks a lot. – MetroWind Mar 10 '15 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.