2010-08-04 102 views
8

我經常定義一個紅寶石符號(例如:some_value),然後我想創建一個名稱相同的方法def some_valueEmacs紅寶石符號字完成

不幸的是,第二次出現some_value字符串的自動完成(M + /)不起作用,略有不同(:some_valuesome_value)。

如何設置emacs來處理這些事件?

+1

究竟是M- /映射爲你?如果是dabbrev-expand,那麼您對dabbrev-abbrev-char-regexp有什麼價值? – 2010-08-05 22:05:24

+1

就像0x4b所說的,我們需要更多的信息。你使用的是什麼紅寶石模式? RubyMode? MuMaMo模式? FWIW,只有當字符串的唯一出現是':some_value'時,dabbrev才能完成。 – 2010-12-01 23:28:02

+0

我正在使用基本的ruby模式。我沒有改變M - /做什麼。但是,我用什麼模式是無關緊要的。它在沒有模式的'scratch'緩衝區中不起作用。 – vise 2010-12-02 07:15:11

回答

7

假設M- /綁定到dabbrev-expand,則可以配置dabbrev-mode以在擴展字符串時忽略某些前綴。爲了使一個冒號被忽略的前綴,類型

M-x customize-group 

然後

dabbrev 

這將您帶到dabbrev模式自定義頁面。去點Dabbrev Abbrev跳過主導正則表達式並點擊Value menu。從菜單中選擇「Regexp」。

現在,您在輸入單個冒號的值菜單旁邊會看到一個標有「Regexp:」的文本框。

: 

然後在下一行按一下按鈕State和選擇價值「保存爲將來的會話」。

+0

謝謝,這正是我正在尋找的! – vise 2010-12-07 17:59:34

+0

偉大的提示!我將其擴展到只在Ruby和Clojure模式下啓用它:https://gist.github.com/shepmaster/10877484 – Shepmaster 2014-04-16 13:44:35

1

不是您的問題的直接答案,但您應該通過使用auto complete modersense配對獲得更智能的Ruby自動補全。

+0

謝謝,我沒有安裝,但rsense看起來很漂亮。 – vise 2010-12-07 18:02:53

2

首先,我的結果!我在我的模型中輸入了:some_crazy_symbol。在一個換行符,我輸入def so,打M-/兩次,結束了

def some_crazy_symbol 
end 

(Rinari提供的end。)

我得到這個用hippie-expand工作得很好。如果你想測試一下,綁定hippie-expandM-/像這樣:

(global-set-key (kbd "M-/") 'hippie-expand) 

繼承人的documentation。嬉皮通過在當前點上嘗試一些不同的擴展來擴展工作。這些擴展存儲在hippie-expand-try-functions-list變量中。在我的系統(並且默認),這個變量被設置爲:

(try-complete-file-name-partially try-complete-file-name try-expand-all-abbrevs try-expand-list try-expand-line try-expand-dabbrev try-expand-dabbrev-all-buffers try-expand-dabbrev-from-kill try-complete-lisp-symbol-partially try-complete-lisp-symbol)

迷你緩衝區讀取顯示,這種特殊的擴展是使用try-expand-dabbrev函數來完成。

1

如果Dabbrev縮略跳過前導正則表達式hippie-expand不會做你想要什麼,你已經有了一些elisp的技能,你可以爲hippie-expand創建一個自定義的功能。

請參閱emacs-wiki上的hippie-expand頁面上有關「substring expansion」的部分。還有,你可以使用那裏你可以定製自定義功能..

Substring Expansion

在做Lisp程序標準dabbrev是用處不大,因爲Emacs有沒有命名空間,所以在包符號開頭相同的前綴,因此如果需要從包中完成符號,則必須一次又一次地輸入相同的前綴。與IswitchBuffers一樣,如果可以鍵入唯一的子字符串以獲得所需的符號,則更爲有效。 Dabbrev在這方面沒有提供任何內容,所以我轉向了以前從未使用過的Hippie Expand。

下面是基於嬉皮的功能擴展dabbrev擴展執行串擴展:

(defun try-my-dabbrev-substring (old) 
    (let ((old-fun (symbol-function 'he-dabbrev-search))) 
    (fset 'he-dabbrev-search (symbol-function 'my-dabbrev-substring-search)) 
    (unwind-protect 
     (try-expand-dabbrev old) 
     (fset 'he-dabbrev-search old-fun)))) 


(defun my-dabbrev-substring-search (pattern &optional reverse limit) 
    (let ((result()) 
    (regpat (cond ((not hippie-expand-dabbrev-as-symbol) 
       (concat (regexp-quote pattern) "\\sw+")) 
       ((eq (char-syntax (aref pattern 0)) ?_) 
       (concat (regexp-quote pattern) "\\(\\sw\\|\\s_\\)+")) 
       (t 
       (concat (regexp-quote pattern) 
        "\\(\\sw\\|\\s_\\)+"))))) 
    (while (and (not result) 
     (if reverse 
      (re-search-backward regpat limit t) 
      (re-search-forward regpat limit t))) 
     (setq result (buffer-substring-no-properties (save-excursion 
                (goto-char (match-beginning 0)) 
                (skip-syntax-backward "w_") 
                (point)) 
          (match-end 0))) 
     (if (he-string-member result he-tried-table t) 
     (setq result nil)))  ; ignore if bad prefix or already in table 
    result)) 
1

我想我會分享solution I came up with that works for hippie-expand

總結:當:被認爲是一個標點符號

(defun hippie-expand-ruby-symbols (orig-fun &rest args) 
    (if (eq major-mode 'ruby-mode) 
     (let ((table (make-syntax-table ruby-mode-syntax-table))) 
     (modify-syntax-entry ?: "." table) 
     (with-syntax-table table (apply orig-fun args))) 
    (apply orig-fun args))) 

(advice-add 'hippie-expand :around #'hippie-expand-ruby-symbols) 

hippie-expand將擴大在ruby-mode符號,所以這個建議創建一個臨時的語法表,其中:是標點字符,並調用hippie-expand