Disable multiple-cursors-mode if there's only one cursor (the real one).

This commit is contained in:
Magnar Sveen 2012-07-20 15:59:01 +02:00
parent 0bfd611151
commit 608ceb4b81
2 changed files with 128 additions and 100 deletions

View File

@ -2,13 +2,9 @@
(lambda () (lambda ()
(delete-selection-mode 1))) (delete-selection-mode 1)))
(defun num-cursors ()
(1+ (count-if 'mc/fake-cursor-p
(overlays-in (point-min) (point-max)))))
(Then "^I should have \\([0-9]+\\) cursors$" (Then "^I should have \\([0-9]+\\) cursors$"
(lambda (num) (lambda (num)
(let ((actual (num-cursors))) (let ((actual (mc/num-cursors)))
(assert (eq (string-to-number num) actual) nil (assert (eq (string-to-number num) actual) nil
"Expected to have %s cursors, but was %d." num actual)))) "Expected to have %s cursors, but was %d." num actual))))
@ -16,7 +12,7 @@
(lambda () (lambda ()
(assert (not multiple-cursors-mode) nil (assert (not multiple-cursors-mode) nil
"Expected to have one cursor, but multiple-cursors-mode is still active.") "Expected to have one cursor, but multiple-cursors-mode is still active.")
(assert (eq 1 (num-cursors)) nil (assert (eq 1 (mc/num-cursors)) nil
"Expected to have one cursor, but there are still fake cursor overlays."))) "Expected to have one cursor, but there are still fake cursor overlays.")))
(When "^I press \"\\(.+\\)\"$" (When "^I press \"\\(.+\\)\"$"

View File

@ -178,10 +178,15 @@ cursor with updated info."
(defun mc/prompt-for-inclusion-in-whitelist (original-command) (defun mc/prompt-for-inclusion-in-whitelist (original-command)
"Asks the user, then adds the command either to the once-list or the all-list." "Asks the user, then adds the command either to the once-list or the all-list."
(if (y-or-n-p (format "Do %S for all cursors?" original-command)) (if (y-or-n-p (format "Do %S for all cursors?" original-command))
(add-to-list 'mc--cmds-to-run-for-all original-command) (add-to-list 'mc--default-cmds-to-run-for-all original-command)
(add-to-list 'mc--cmds-to-run-once original-command) (add-to-list 'mc--default-cmds-to-run-once original-command)
nil)) nil))
(defun mc/num-cursors ()
"The number of cursors (real and fake) in the buffer."
(1+ (count-if 'mc/fake-cursor-p
(overlays-in (point-min) (point-max)))))
(defun mc/execute-this-command-for-all-cursors () (defun mc/execute-this-command-for-all-cursors ()
"Used with post-command-hook to execute supported commands for "Used with post-command-hook to execute supported commands for
all cursors. It also checks a list of explicitly unsupported all cursors. It also checks a list of explicitly unsupported
@ -191,6 +196,8 @@ inform about the lack of support.
Commands that are neither supported nor explicitly unsupported Commands that are neither supported nor explicitly unsupported
is executed normally for point, but skipped for the fake is executed normally for point, but skipped for the fake
cursors." cursors."
(if (eq 1 (mc/num-cursors)) ;; no fake cursors? disable mc-mode
(multiple-cursors-mode 0)
(let ((original-command (or (command-remapping this-original-command) (let ((original-command (or (command-remapping this-original-command)
this-original-command))) this-original-command)))
@ -205,10 +212,10 @@ cursors."
original-command original-command
(get original-command 'mc--unsupported)) (get original-command 'mc--unsupported))
(when (and original-command (when (and original-command
(not (memq original-command mc--cmds-to-run-once)) (not (memq original-command mc--default-cmds-to-run-once))
(or (memq original-command mc--cmds-to-run-for-all) (or (memq original-command mc--default-cmds-to-run-for-all)
(mc/prompt-for-inclusion-in-whitelist original-command))) (mc/prompt-for-inclusion-in-whitelist original-command)))
(mc/execute-command-for-all-fake-cursors original-command)))))) (mc/execute-command-for-all-fake-cursors original-command)))))))
(defun mc/remove-fake-cursors () (defun mc/remove-fake-cursors ()
"Remove all fake cursors. "Remove all fake cursors.
@ -284,47 +291,68 @@ from being executed if in multiple-cursors-mode."
(overlay-put cursor 'kill-ring-yank-pointer kill-ring-yank-pointer))))))) (overlay-put cursor 'kill-ring-yank-pointer kill-ring-yank-pointer)))))))
;;---------------------------------------------------------------------------------------- ;;----------------------------------------------------------------------------------------
;; Commands to run only once (don't need to message about skipping it) (defvar mc--default-cmds-to-run-once '(save-buffer
(setq mc--cmds-to-run-once '(mark-next-like-this ido-exit-minibuffer
save-buffer undo undo-tree-undo
undo redo undo-tree-redo
undo-tree-undo
undo-tree-redo
universal-argument universal-argument
universal-argument-other-key universal-argument-other-key
top-level
mc/switch-from-mark-multiple-to-cursors mc/switch-from-mark-multiple-to-cursors
mc/edit-lines mc/edit-lines
mc/edit-ends-of-lines mc/edit-ends-of-lines
mc/edit-beginnings-of-lines)) mc/edit-beginnings-of-lines
mc/mark-next-like-this)
"Default set of commands to run only once in multiple-cursors-mode.")
;; Commands that should be mirrored by all cursors (defvar mc/cmds-to-run-once nil
(setq mc--cmds-to-run-for-all '(mc/keyboard-quit "Commands to run only once in multiple-cursors-mode.")
(defvar mc--default-cmds-to-run-for-all '(mc/keyboard-quit
self-insert-command self-insert-command
js2-insert-and-indent
wrap-region-trigger
sgml-slash
slime-space
previous-line previous-line
next-line next-line
newline newline
yas/expand
newline-and-indent newline-and-indent
join-line join-line
right-char forward-char right-char
right-word forward-word right-word
left-char backward-char forward-char
left-word backward-word forward-word
left-char
left-word
backward-char
backward-word
upcase-word upcase-word
downcase-word downcase-word
capitalize-word capitalize-word
forward-list forward-list
backward-list backward-list
hippie-expand hippie-expand-lines hippie-expand
yank yank-indented yank-pop hippie-expand-lines
yank
yank-pop
kill-word kill-word
kill-region-or-backward-word
kill-line kill-line
kill-whole-line kill-whole-line
backward-kill-word
backward-delete-char-untabify
delete-char delete-forward-char
delete-backward-char
just-one-space
zap-to-char
end-of-line
set-mark-command
move-end-of-line
beginning-of-line
move-beginning-of-line
kill-ring-save
back-to-indentation
;; mode specific commands
org-shiftright
sgml-slash
slime-space
subword-forward subword-forward
subword-backward subword-backward
subword-mark subword-mark
@ -334,32 +362,36 @@ from being executed if in multiple-cursors-mode."
subword-capitalize subword-capitalize
subword-upcase subword-upcase
subword-downcase subword-downcase
backward-kill-word
backward-delete-char-untabify
delete-char delete-forward-char c-electric-delete-forward
delete-backward-char c-electric-backspace
c-electric-paren
c-electric-semi&comma
org-shiftright
just-one-space
zap-to-char
end-of-line
set-mark-command
js2-beginning-of-line js2-beginning-of-line
js2-end-of-line js2-end-of-line
js2-insert-and-indent
js2r-inline-var js2r-inline-var
change-number-at-point c-electric-delete-forward
move-end-of-line c-electric-backspace
beginning-of-line c-electric-paren
c-electric-semi&comma
;; common extension commands
wrap-region-trigger
yas/expand
;; magnars extension commands
er/expand-region er/expand-region
er/mark-word er/contract-region
smart-forward smart-forward
smart-backward smart-backward
smart-up smart-up
smart-down smart-down)
move-beginning-of-line "Default set of commands that should be mirrored by all cursors")
(defvar mc/cmds-to-run-for-all nil
"Commands to run for all cursors in multiple-cursors-mode")
(setq mc/cmds-to-run-for-all '(save-region-or-current-line
kill-region-or-backward-word
change-number-at-point
dired-back-to-start-of-files dired-back-to-start-of-files
kill-ring-save save-region-or-current-line yank-indented))
back-to-indentation))
(provide 'multiple-cursors-core) (provide 'multiple-cursors-core)