2009-07-17 69 views
7

我正在使用emacs爲haxe編程語言實現自動完成。如何在emacs中顯示自動完成選項?

我已經想出瞭如何獲取我想呈現的自動填充列表。該列表是在格式:

'((name1 type1 desc1) 
    (name2 type2 desc2) ... 

我想顯示到包含在格式文本「NAME1 TYPE1」後的光標位置(如在自動填充模式)用戶列表和desc在小緩衝區當前選擇的項目。用戶應該能夠選擇完成或放棄,就像在自動完成模式下一樣。

當用戶選擇某些內容時,應在光標位置插入name1。

什麼是最好/最簡單的方法來做到這一點?有沒有一些標準的emacs方式來做到這一點,或者我應該自己編碼?

編輯:我有函數來獲取基於緩衝區的自動完成候選列表。現在我正在努力如何將它集成到自動完成模式。由於完成操作很繁重,我只想在光標打開的情況下觸發它。「字符。

這是我的代碼。

(defun get-completes-from-haxe (hxml-file file pos) 
    (let* ((completion-buffer (get-buffer-create "*haxe-completions*")) 
     (cmd (concat "cd " (file-name-directory hxml-file) "; haxe " hxml-file " --display " file "@" (number-to-string pos)))) 
    (ignore-errors 
     (shell-command cmd completion-buffer) 
     (let ((clist (xml-parse-region 1 (buffer-size completion-buffer) completion-buffer)) 
      (completes nil)) 
     (dolist (s (cddar clist)) 
      (when (listp s) 
      (let* ((item (cdr s)) 
        (name (cdaar item)) 
        (type (car (cddadr item))) 
        (desc (cdddr item))) 
       (setf completes (cons name completes))))) 
     completes)))) 

(defun file-find-upwards (buffer file-name) 
    ;; Chase links in the source file and search in the dir where it points. 
    (setq dir-name (or (and (buffer-file-name buffer) 
          (file-name-directory (file-chase-links 
               (buffer-file-name buffer)))) 
        default-directory)) 
    ;; Chase links before visiting the file. This makes it easier to 
    ;; use a single file for several related directories. 
    (setq dir-name (file-chase-links dir-name)) 
    (setq dir-name (expand-file-name dir-name)) 
    ;; Move up in the dir hierarchy till we find a change log file. 
    (let ((file1 (concat dir-name file-name)) 
     parent-dir) 
    (while (and (not (file-exists-p file1)) 
       (progn (setq parent-dir 
          (file-name-directory 
           (directory-file-name 
           (file-name-directory file1)))) 
         ;; Give up if we are already at the root dir. 
         (not (string= (file-name-directory file1) 
            parent-dir)))) 
     ;; Move up to the parent dir and try again. 
     (setq file1 (expand-file-name file-name parent-dir))) 
    ;; If we found the file in a parent dir, use that. Otherwise, 
    ;; return nil 
    (if (or (get-file-buffer file1) (file-exists-p file1)) 
     file1 
     nil))) 


(defun get-candidate-list (buffer pos) 
    (get-completes-from-haxe 
    (file-find-upwards buffer "compile.hxml") 
    (buffer-file-name buffer) 
    pos)) 

回答

7

我建議建立的優秀軟件包自動完成:http://www.emacswiki.org/emacs/AutoComplete

您可以自定義源自動完成,它似乎相當直截了當。

+0

我看到了,但無法弄清楚如何顯示工具提示和文檔。它提供的只是vim自動完成,這不是我想要的。 – Marko 2009-07-17 22:32:04

+1

看起來像在自動完成模式下,ac-select-candidate函數用於顯示當前候選人的選擇,您可以爲該函數定義一個建議,以在小型緩衝區中顯示desc – user49117 2009-07-22 11:47:37

1

ido.el的ido-completion-read是另一種方法。

它的一個有用的功能是,您可以使用正則表達式篩選候選項,並且可以實時地打開和關閉此功能。有關更多詳細信息,請參閱ido.el。

1

ido包具有稱爲'ido-completion-read'的方便完成功能。例如,這裏有一個簡單的調整,以使其顯示列出一行接一行的基礎:

(defun x-ido-completing-read 
    (prompt choices &optional predicate require-match initial-input hist def) 
    (let* ((indent (concat "\n" (make-string (length prompt) ?))) 
     (ido-decorations 
     `("" "" ,indent ,(concat indent "...") 
      "[" "]" " [No match]" " [Matched]" " [Not readable]" " [Too big]"))) 
    (ido-completing-read prompt choices predicate require-match initial-input hist def))) 
1

裏面的yasnippet代碼,可從谷歌代碼,他們使用「下拉-list.el」的全在娟鍾以顯示可能在點使用的可能的片段。這樣可以在光標位置提供一個彈出式(類似工具提示)菜單,您可以使用箭頭鍵選擇並進入,也可以按數字進行選擇。

http://www.emacswiki.org/emacs/dropdown-list.el

+0

好極了,如果只有這樣纔有可能擺脫selid在右側:) – Marko 2009-07-23 10:55:44

3

爲什麼要保持車輪重塑?公司模式支持幾種語言

相關問題