开发者

The `last-command' variable in Emacs Lisp

I wrote a snippet to copy previous line to point,if I repeat the command,it copy more previous line. It's here:

    (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-regio开发者_开发技巧n (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))

I intended to yank previous line to override current line.If I repeat my-copy-line-here,I'll yank the 2th previous line,this is accomplished by the test (if (eq this-command last-command).But it failed ,Because every time after executing my-copy-line-here,it sets the last-command to yank,instead my-copy-line-here.I just can't figure out what's going on.I need your help.


`M-y (yank-pop) works similarly, pasting previous copied lines on repeated calls. Checking its sources, I see two differences with yours:

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

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

Perhaps one or both of these together can be useful. Set this-command after calling yank maybe?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜