【问题标题】:How to display autocomplete choices in emacs?如何在 emacs 中显示自动完成选项?
【发布时间】:2010-11-11 18:28:49
【问题描述】:

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

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

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

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

当用户选择某些东西时,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))

【问题讨论】:

    标签: emacs autocomplete elisp haxe


    【解决方案1】:

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

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

    【讨论】:

    • 我看到了,但不知道如何用它显示工具提示和文档。它提供的只是 vim 之类的自动完成功能,这不是我想要的。
    • 看起来在自动完成模式下,ac-select-candidate 函数用于显示当前选择的候选者,您可以为该函数定义一个建议以在 minibuffer 中显示 desc
    【解决方案2】:

    为什么要不断地重新发明轮子? company-mode 支持几种语言

    【讨论】:

    • 这是要走的路。我不知道为什么我错过了公司模式并在明显劣质的自动完成模式上浪费时间。也许是丑陋的名字;)
    • 唯一的问题是公司模式的文档。远不清楚如何使用它,我收到了很多人的电子邮件,询问我的公司设置。可以在链接到这里的配置文件中看到:richardriley.net/projects/emacs/dotemacs
    【解决方案3】:

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

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

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

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

        【讨论】:

        • 太棒了,要是能去掉右侧的 selid 就好了:)
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-03
        • 2013-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-19
        • 2021-12-05
        相关资源
        最近更新 更多