2013-06-04 20 views
1

我如何能实现在Emacs的功能,杀死一个字,一个字杀或线路功能的实现,那么如果再次呼吁立即它杀死的整条生产线,也许叫kill-word-or-line。我有点像elisp n00b,但是如果有人能够指出我的行为方式类似,即在连续调用两次时有不同的行为,我可能会自己做。在Emacs

如果剪切环载满行,如果杀行版本被称为即我猜杀字将需要重新插入的行被杀害之前这将是很好。下面是一个例子:(在 '|' 表示点位置)

This is an exam|ple line.

;调用kill-word-or-line第一次得到这样的......

This is an | line.

;再拨kill-word-or-line即可得到...

|

压井环应该包含exampleThis is an example line.

回答

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))))))) 

两者都适于从所述命令中this blog post

+0

谢谢,这个作品完美!我只是让消息更加一致,即添加了“杀死的字”和“杀死的消息” – mkm

4

last-command变量包含交互最后执行的命令,你可以用它来测试相同的命令是否被称为先后两次:

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

这机构用于例如在undo