Emacs Lisp中的`last-command'变量

时间:2011-10-08 12:40:28

标签: emacs elisp

我写了一个片段来复制前一行到点,如果我重复命令,它会复制更多的前一行。就在这里:

    (defun my-copy-line (num)
      "copy lines"
      (interactive "p")
      (save-excursion
        (move-end-of-line 1)
        (push-mark)
        (move-beginning-of-line num)
        (kill-ring-save (point) (mark))))


(defvar copy-line-num 1)
(defun my-copy-line-here (num)
  "copy line ahead here"
  (interactive "p")
  (if (eq this-command last-command)
      (setq copy-line-num (+ copy-line-num num)) ;count num lines up
    (setq copy-line-default 1))
  (save-excursion
    (save-excursion                     ;make current line blank
      (move-beginning-of-line 1)
      (push-mark)
      (move-end-of-line 1)
      (kill-region (point) (mark)))     ;不用kill-line,以免删除空白行
    (push-mark)
    (previous-line copy-line-num)
    (my-copy-line 1)
    (exchange-point-and-mark)
    (yank))
  (setq this-command 'my-copy-line-here))

我打算拉上前一行来覆盖当前行。如果我重复my-copy-line-here,我会抓住第2行,这是由测试(if (eq this-command last-command)完成的。但它失败了,因为每一行执行my-copy-line-here后的时间,它将last-command设置为yank,而不是my-copy-line-here。我只是无法弄清楚发生了什么。我需要你的帮助。

1 个答案:

答案 0 :(得分:2)

`M-y(yank-pop)的工作方式类似,在重复调用时粘贴以前复制的行。检查其来源,我发现与你的两个不同之处:

; explicit check for yank 
(if (not (eq last-command 'yank))

; setting this command
(setq this-command 'yank)

也许这些中的一个或两个可能有用。可能在调用this-command之后设置yank吗?