简体   繁体   English

在Emacs迷你缓冲区中启用自动完成功能

[英]Enable auto-complete in Emacs minibuffer

I'm trying to turn auto-complete in the minibuffer: 我正试图在迷你缓冲区中自动完成

(add-hook 'minibuffer-setup-hook 'auto-complete-mode)

What I get is auto-complete working in the first instance of minibuffer, but no longer. 我得到的是第一个迷你缓冲器auto-complete工作,但不再。 That is the full minibuffer-setup-hook after loading: 这是加载后的完整minibuffer-setup-hook

(auto-complete-mode turn-on-visual-line-mode ido-minibuffer-setup rfn-eshadow-setup-minibuffer minibuffer-history-isearch-setup minibuffer-history-initialize)

How to turn auto-complete on persistently? 如何持续打开auto-complete

You rarely ever want to add a function symbol to a hook variable if that function acts as a toggle (which will be the case for most minor modes). 如果该函数充当切换(对于大多数次要模式将是这种情况),您很少想要将函数符号添加到钩子变量。

minibuffer-setup-hook runs "just after entry to minibuffer", which means that you would be enabling auto complete mode the first time you enter the minibuffer; minibuffer-setup-hook在“进入迷你缓冲区后”运行,这意味着您第一次进入迷你缓冲区时将启用自动完成模式; disabling it the second time; 第二次禁用它; enabling it the third time; 第三次启用它; etc... 等等...

Typically you would either look to see if there's a pre-defined turn-on-autocomplete-mode type of function, or define your own: 通常,您要么查看是否存在预定义turn-on-autocomplete-mode类型的功能,或者定义您自己的:

(defun my-turn-on-autocomplete-mode ()
  (autocomplete-mode 1)) ;; an argument of 1 will enable most modes
(add-hook 'minibuffer-setup-hook 'my-turn-on-auto-complete-mode)

I can't test that, because you haven't linked to the autocomplete-mode you are using. 我无法测试,因为您没有链接到您正在使用的自动完成模式。

The creator of "auto-complete-mode" explicitly excludes the minibuffer for use with auto completion. “自动完成模式”的创建者明确排除了用于自动完成的迷你缓冲区。 The definition for the minor mode is: 次要模式的定义是:

(define-global-minor-mode global-auto-complete-mode
  auto-complete-mode auto-complete-mode-maybe
  :group 'auto-complete)

so the "turn mode on" function is "auto-complete-mode-maybe" - the definition of that function is: 所以“开启模式”功能是“自动完成模式 - 可能” - 该功能的定义是:

(defun auto-complete-mode-maybe ()
  "What buffer `auto-complete-mode' prefers."
  (if (and (not (minibufferp (current-buffer)))
           (memq major-mode ac-modes))
      (auto-complete-mode 1)))

This function explicitly tests in the if statement if the current-buffer is the minibuffer and doesn't turn on the auto-complete-mode if it is. 如果当前缓冲区是迷你缓冲区,则此函数在if语句中显式测试,如果是,则不打开自动完成模式。

If you want to use auto-complete-mode in the minibuffer, you should probably contact the maintainer of the mode and ask him why he excluded the minibuffer and what programming changes he feels are necessary to enable the mode in the minibuffer. 如果你想在迷你缓冲区中使用自动完成模式,你可能应该联系该模式的维护者并询问他为什么要排除迷你缓冲区以及他认为在迷你缓冲区中启用模式所需的编程更改。

Zev called to my attention auto-complete-mode-maybe , and that is the required modifications (file auto-complete.el , all changes have comments): Zev打电话给我注意auto-complete-mode-maybe ,这就是所需的修改(文件auto-complete.el ,所有更改都有注释):

;; Add this variable
(defcustom ac-in-minibuffer t
  "Non-nil means expand in minibuffer."
  :type 'boolean
  :group 'auto-complete)

...

(defun ac-handle-post-command ()
  (condition-case var
      (when (and ac-triggered
                 (not (ido-active)) ;; Disable auto pop-up in ido mode
                 (or ac-auto-start
                     ac-completing)
                 (not isearch-mode))
        (setq ac-last-point (point))
        (ac-start :requires (unless ac-completing ac-auto-start))
        (ac-inline-update))
    (error (ac-error var))))

...

(defun auto-complete-mode-maybe ()
  "What buffer `auto-complete-mode' prefers."
  (if (or (and (minibufferp (current-buffer)) ac-in-minibuffer) ;; Changed
          (memq major-mode ac-modes))
      (auto-complete-mode 1)))

And .emacs : .emacs

(add-hook 'minibuffer-setup-hook 'auto-complete-mode)

Certainly, there are binding conflicts but it is possible to solve them. 当然,存在约束性冲突,但有可能解决它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM