popup-kill-ring和multiple-cursors

时间:2014-02-05 14:28:17

标签: emacs elisp

我是popup-kill-ringmultiple-cursors的狂热用户,但是,当使用前者而后者处于活动状态时,默认行为不符合我的喜好。

popup-kill-ring提供了一个弹出菜单,用于从kill-ring中选择要插入的项目。存在多个(虚拟)游标时的默认行为是单独为每个游标选择项目。通常我想在所有光标位置插入所选项目。

任何人都知道一个允许所需行为的解决方案吗?

1 个答案:

答案 0 :(得分:2)

理想情况下,我们可以添加从弹出窗口(popup-kill-ring-select)插入项目到mc/cmds-to-run-for-all的命令,这会导致命令为所有游标运行,但这不起作用。我们的其他选项是编写一个自定义函数,如果多个游标是活动的,则在弹出菜单中为每个游标插入当前选定的项目,否则回退到默认命令。这就是我想出来的

(defun my-default-popup-kill-ring-behviour ()
  "Switch to default `popup-kill-ring' behavior"
  (interactive)
  (define-key popup-kill-ring-keymap "\r" 'popup-kill-ring-select)
  (let ((m (with-no-warnings menu)))
    (when m
      (popup-delete m)))
  (mc/execute-command-for-all-cursors 'popup-kill-ring)
  (define-key popup-kill-ring-keymap "\r" 'mc-popup-kill-ring-insert))

(defun mc-popup-kill-ring-insert()
  "Insert the selected text at all cursors"
  (interactive)
  ;; If multiple cursor mode is active
  (when multiple-cursors-mode
    ;; Extract the currently selected text from the popup menu
    ;; copied from `popup-kill-ring-select'
    (let* ((m (with-no-warnings menu))
           (item (popup-item-value-or-self (nth (popup-cursor m) (popup-list m))))
           ;; Item is just the text shown in popup not the text actually killed
           ;; following code retrieves the killed text
           (num (popup-kill-ring-get-index item))
           (text (when num
                   (nth num kill-ring)))
           ;; Fake command to insert the kill text
           (command (lambda ()
                      (interactive)
                      (when text
                        (insert text)))))
      ;; Execute the command for each fake cursor
      (save-excursion
        (mc/execute-command-for-all-fake-cursors command))))
  ;; Finally execute the command for current cursor
  (call-interactively 'popup-kill-ring-select))

(define-key popup-kill-ring-keymap "\r" 'mc-popup-kill-ring-insert)
(define-key popup-kill-ring-keymap (kbd "C-<return>") 'my-default-popup-kill-ring-behviour)


(global-set-key (kbd "M-y") 'popup-kill-ring)

现在您可以将此命令绑定到所需的键绑定,下面我将其绑定到enter

(define-key popup-kill-ring-keymap "\r" 'mc-popup-kill-ring-insert)

要在启动popup-kill-ring后切换到默认行为,您可以使用命令my-default-popup-kill-ring-behviour命令将其绑定到C-RET

(define-key popup-kill-ring-keymap (kbd "C-<return>") 'my-default-popup-kill-ring-behviour)