1

我怎样才能在 emacs 中实现一个函数来杀死一个单词,然后如果立即再次调用它会杀死整个行,也许称为kill-word-or-line. 我有点像 elisp n00b,但如果有人可以将我指向一个行为类似的函数,即在连续调用两次时具有不同的操作,我自己可能能够做到这一点。

如果调用 kill line 版本,如果 kill ring 包含完整的行会很好,即我猜在行被杀死之前需要再次插入被杀死的单词。下面是一个例子:('|'表示点的位置)

This is an exam|ple line.

; 第一次打电话kill-word-or-line得到类似的东西......

This is an | line.

; 再次致电kill-word-or-line以获取...

|

杀戮环应包含exampleThis is an example line.

4

2 回答 2

4

last-command变量包含最后一个交互式执行的命令,您可以使用它来测试同一命令是否被连续调用两次:

(defun kill-word-or-line ()
  (interactive)
  (if (eq last-command 'kill-word-or-line)
      (message "kill line")
    (message "kill word")))

该机制用于例如实现undo

于 2013-06-04T12:22:00.483 回答
2

您可以使用以下建议kill-region来杀死选定的区域,或者先杀死点上的单词,然后再杀死整行。

杀死单词或行
(defadvice kill-region (before slick-cut-line first activate compile)
  "When called interactively kill the current word or line.

Calling it once without a region will kill the current word.
Calling it a second time will kill the current line."
  (interactive
   (if mark-active (list (region-beginning) (region-end))
    (if (eq last-command 'kill-region)
        (progn
          ;; Return the previous kill to rebuild the line
          (yank)
          ;; Add a blank kill, otherwise the word gets appended.
          ;; Change to (kill-new "" t) to remove the word and only
          ;; keep the whole line.
          (kill-new "")
          (message "Killed Line")
          (list (line-beginning-position)
                (line-beginning-position 2)))
      (save-excursion
        (forward-char)
        (backward-word)
        (mark-word)
        (message "Killed Word")
        (list (mark) (point)))))))

这也是一样的,但是是复制而不是杀死。

复制单词或行
(defadvice kill-ring-save (before slick-copy-line activate compile)
  "When called interactively with no region, copy the word or line

Calling it once without a region will copy the current word.
Calling it a second time will copy the current line."
    (interactive
     (if mark-active (list (region-beginning) (region-end))
       (if (eq last-command 'kill-ring-save)
           (progn
             ;; Uncomment to only keep the line in the kill ring
             ;; (kill-new "" t)
             (message "Copied line")
             (list (line-beginning-position)
                   (line-beginning-position 2)))
         (save-excursion
           (forward-char)
           (backward-word)
           (mark-word)
           (message "Copied word")
           (list (mark) (point)))))))

两者都改编自这篇博文中的命令。

于 2013-06-05T14:10:52.440 回答