2013-06-04 69 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