7

我正在为 haxe 编程语言在 emacs 中实现自动完成。

我已经想出了如何获取我想要呈现的自动完成列表。该列表的格式为:

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

我想在光标位置(如自动完成模式)之后向用户显示包含格式为“name1 type1”的文本的用户列表,并在 minibuffer 中显示当前选定项目的 desc。用户应该能够在自动完成模式下选择完成或放弃。

当用户选择某些东西时,name1 应该插入到光标位置。

最好/最简单的方法是什么?是否有一些标准的 emacs 方法可以做到这一点,或者我应该自己编写一些代码?

编辑:我有根据缓冲区获取自动完成候选者列表的功能。现在我正在努力如何将其集成到自动完成模式。由于 get-completes 是繁重的操作,我只想在光标位于“。”时触发它。特点。

这是我的代码。

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

5 回答 5

7

我建议使用优秀的包 AutoComplete:http ://www.emacswiki.org/emacs/AutoComplete

您可以为自动完成定义自定义源,这看起来相当简单。

于 2009-07-17T17:10:36.573 回答
3

为什么要不断地重新发明轮子?公司模式支持几种语言

于 2009-08-14T14:19:16.740 回答
1

在 yasnippet 代码(可从 google 代码获得)中,他们使用 Jaeyoun Chung 的“dropdown-list.el”来显示可以在某一点使用的可能片段。这会在光标位置提供一个弹出(类似工具提示)菜单,您可以使用箭头键选择并输入,也可以按数字选择。

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

于 2009-07-23T03:33:24.883 回答
1

ido 包具有方便的完成功能,称为“ido-completing-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)))
于 2009-07-21T04:41:56.903 回答
1

来自 ido.el 的 ido-completing-read 是另一种方法。

它的一个有用功能是您可以使用正则表达式来过滤掉候选者,并且您可以即时打开和关闭此功能。有关详细信息,请参阅 ido.el。

于 2009-07-19T20:31:26.677 回答