2009-02-17 35 views
12

編輯:我知道有鍵盤退出(這通常綁定到C - G);但我更感興趣的是瞭解如何處理Emacs編輯功能(如本例中的情況)。我偶爾遇到這種情況,當時我只想改變一些內置函數。,keyboard-escape-quit的最佳方式是什麼,不會破壞其他窗口?

在emacs中,當你打M-ESC ESC(或者ESC三次)時,你可以擺脫很多情況,比如transient-mark等等。但是我習慣性地按下了轉義鍵(我實際上將它重映射到一個命中的逃生鍵)超過我的意圖,並最終殺死我的Windows配置,這是很煩人的。功能鍵盤逃逸遷出在simple.el定義:

(defun keyboard-escape-quit() 
    "Exit the current \"mode\" (in a generalized sense of the word). 
This command can exit an interactive command such as `query-replace', 
can clear out a prefix argument or a region, 
can get out of the minibuffer or other recursive edit, 
cancel the use of the current buffer (for special-purpose buffers), 
or go back to just one window (by deleting all but the selected window)." 
    (interactive) 
    (cond ((eq last-command 'mode-exited) nil) 
    ((> (minibuffer-depth) 0) 
    (abort-recursive-edit)) 
    (current-prefix-arg 
    nil) 
    ((and transient-mark-mode mark-active) 
    (deactivate-mark)) 
    ((> (recursion-depth) 0) 
    (exit-recursive-edit)) 
    (buffer-quit-function 
    (funcall buffer-quit-function)) 
    ((not (one-window-p t)) 
    (delete-other-windows)) 
    ((string-match "^ \\*" (buffer-name (current-buffer))) 
    (bury-buffer)))) 

而且我可以看到,我不想讓行:

((not (one-window-p t)) 
    (delete-other-windows)) 

但是,什麼是修改這一目標的最佳方式功能?我只能看到兩種方法:1)修改simple.el 2)將此函數複製到我的.emacs文件並在那裏進行修改。兩種方式都不是真的;理想情況下,我希望看到defadvice上的一些內容,但我不明白在這種情況下我該怎麼做。

回答

14

你可以使用環繞通知,並重新定義問題的功能做你想要的(即一個窗口-p應該總是返回T)什麼:

(defadvice keyboard-escape-quit (around my-keyboard-escape-quit activate) 
    (let (orig-one-window-p) 
    (fset 'orig-one-window-p (symbol-function 'one-window-p)) 
    (fset 'one-window-p (lambda (&optional nomini all-frames) t)) 
    (unwind-protect 
     ad-do-it 
     (fset 'one-window-p (symbol-function 'orig-one-window-p))))) 

這種行爲像(讓...)但必須更復雜,因爲你需要覆蓋一個有限範圍的函數而不是一個變量。

+4

使用flet的更好的單線程版本) (flet((one-window-p(&optional nomini all-frames)t))ad-do-it)) – polyglot 2009-06-20 16:22:05

7

我通常會發現'keyboard-quit(C-g)可以解決所有這些情況。但是,如果你真的想要這個函數的變種,我認爲複製到你的.emacs文件(和重命名,我通常用一個前綴bp),並在那裏進行編輯可能是最好的選擇。編輯,迴應編輯:一般來說,每當我想要一個emacs函數的編輯版本,我要麼自己寫,要麼將其複製到我的.emacs,重新命名爲bp-whotever,然後做適當的編輯。

這樣做的缺點是我的.emacs是巨大的,並且可能還沒有被使用過的古代函數的額外關係......好處在於,無論何時我需要寫新的東西,我都有大量的樣本代碼來看看......

0

單擊Escape鍵時默認爲Meta前綴鍵;也就是涉及Meta鍵的鍵綁定。

三次按Esc鍵將運行keyboard-escape-quit,這就像鍵盤退出,但更多的是「做我的意思」的行爲。

此代碼可能有助於您的用例。您可以在您的Emacs初始化文件中使用此:

;;; esc always quits 
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit) 
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit) 
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit) 
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit) 
(define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit) 
(global-set-key [escape] 'keyboard-quit) 
1

下面是另一個更簡單的忠告,它利用的事實,即keyboard-escape-quit電話buffer-quit-function關閉窗口前:

(defadvice keyboard-escape-quit 
    (around keyboard-escape-quit-dont-close-windows activate) 
    (let ((buffer-quit-function (lambda()()))) 
    ad-do-it)) 

作品使用Emacs 25.1。 (我最初使用了@ scottfrazer的建議,但是它在25.1中並不開心。)

1

我對如何處理Emacs的編輯功能更感興趣(就像這樣案件)。我偶爾遇到這種情況,當時我只想改變一些內置函數。

這正是我創建庫el-patch的目的。你可以把它放在init文件中:

相關問題