我可以在任何地方使用 ido-completing-read 而不是 Complete-read 吗?

发布于 2024-07-22 01:29:43 字数 755 浏览 8 评论 0原文

我是 ido-mode 的忠实粉丝,以至于我想将它用于 describe-functionfind-tag 等等,而不必编写类似“我可以在 Emacs 中搜索标签时获得 ido-mode-style 补全吗?”之类的内容。 为每一个。

(defalias completing-read ido-completing-read)

(setf 'completing-read 'ido-completing-read)

不起作用,至少部分是因为 ido-completing-read 在其主体中调用了 completing-read,因此任何简单的重新定义都会导致无限递归。

从理论上讲,这应该是可能的,因为 ido-completing-read 的文档字符串的第一行是“Ido replacement for thebuilt-in completing-read”。 我环顾四周,似乎找不到其他人尝试过或成功做到这一点。

我意识到 Icicles 可能提供了类似的东西,无论如何我最终可能会这样做,但它比我现在愿意承担的风险更大一些。

谢谢你的帮助。

I'm a big fan of ido-mode, so much so that I would like to use it for things like describe-function or find-tag and so on, without having to write something like in "Can I get ido-mode-style completion for searching tags in Emacs?" for each one.

Both

(defalias completing-read ido-completing-read)

and

(setf 'completing-read 'ido-completing-read)

don't work, at least partly because ido-completing-read calls completing-read in its body, so any simple redefinition would result in infinite recursion.

In theory, it should be possible, since the first line of the docstring for ido-completing-read is "Ido replacement for the built-in completing-read." I've looked around a bit and can't seem to find anyone else who has attempted or succeeded at it.

I realize that Icicles probably provides something like this, and I may end up going with that anyway, but it is a bit more of a plunge than I care to take right now.

Thanks for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

时常饿 2024-07-29 01:29:43

编辑:现在这是一个 Emacs MELPA 提供的软件包。 它已扩展为成熟的次要模式。 开发在 GitHub 上进行。

原帖:

这是我对雅各布答案的改进。 归功于他最初的魔法。 我添加了一个覆盖变量,您可以使用它来防止在特定函数中使用 ido-completing-read 。 我还添加了一个检查,如果没有完成,则使用原始的完成读取(这种情况偶尔会发生,例如在 org-mode 的 org-remember-apply-template 中,这与 Jacobo 的原始建议)。

(defvar ido-enable-replace-completing-read t
  "If t, use ido-completing-read instead of completing-read if possible.

Set it to nil using let in around-advice for functions where the
original completing-read is required.  For example, if a function
foo absolutely must use the original completing-read, define some
advice like this:

(defadvice foo (around original-completing-read-only activate)
  (let (ido-enable-replace-completing-read) ad-do-it))")

;; Replace completing-read wherever possible, unless directed otherwise
(defadvice completing-read
  (around use-ido-when-possible activate)
  (if (or (not ido-enable-replace-completing-read) ; Manual override disable ido
          (boundp 'ido-cur-list)) ; Avoid infinite loop from ido calling this
      ad-do-it
    (let ((allcomp (all-completions "" collection predicate)))
      (if allcomp
          (setq ad-return-value
                (ido-completing-read prompt
                               allcomp
                               nil require-match initial-input hist def))
        ad-do-it))))

哦,要在 Mx 中使用 ido,请使用 amx

Edit: This is now an Emacs package available from MELPA. It has been expanded into a full-fledged minor mode. Development happens on GitHub.

Original post:

Here is my refinement of Jacobo's answer. Credit to him for the original magic. I've added an override variable, which you can use to prevent the use of ido-completing-read in specific functions. I have also added a check that uses the original completing-read if there are no completions (This happens occasionally, for example in org-remember-apply-template from org-mode, which breaks with Jacobo's original advice).

(defvar ido-enable-replace-completing-read t
  "If t, use ido-completing-read instead of completing-read if possible.

Set it to nil using let in around-advice for functions where the
original completing-read is required.  For example, if a function
foo absolutely must use the original completing-read, define some
advice like this:

(defadvice foo (around original-completing-read-only activate)
  (let (ido-enable-replace-completing-read) ad-do-it))")

;; Replace completing-read wherever possible, unless directed otherwise
(defadvice completing-read
  (around use-ido-when-possible activate)
  (if (or (not ido-enable-replace-completing-read) ; Manual override disable ido
          (boundp 'ido-cur-list)) ; Avoid infinite loop from ido calling this
      ad-do-it
    (let ((allcomp (all-completions "" collection predicate)))
      (if allcomp
          (setq ad-return-value
                (ido-completing-read prompt
                               allcomp
                               nil require-match initial-input hist def))
        ad-do-it))))

Oh, and for using ido in M-x, use amx.

不爱素颜 2024-07-29 01:29:43

戏法,胡言乱语,急!

(defadvice completing-read
  (around foo activate)
  (if (boundp 'ido-cur-list)
      ad-do-it
    (setq ad-return-value
      (ido-completing-read
       prompt
       (all-completions "" collection predicate)
       nil require-match initial-input hist def))))

这适用于除 subr 之外的所有内容,其中最重要的是执行扩展命令(与 Mx 绑定的内容)。 但我们可以从 Mx 得到我们想要的东西

(global-set-key
 "\M-x"
 (lambda ()
   (interactive)
   (call-interactively
    (intern
     (ido-completing-read
      "M-x "
      (all-completions "" obarray 'commandp))))))

Hocus pocus, abracadabra, presto!

(defadvice completing-read
  (around foo activate)
  (if (boundp 'ido-cur-list)
      ad-do-it
    (setq ad-return-value
      (ido-completing-read
       prompt
       (all-completions "" collection predicate)
       nil require-match initial-input hist def))))

That works with everything but subr's, from which execute-extended-command is the one that matters (what is binded to M-x). But we can get what we want from M-x

(global-set-key
 "\M-x"
 (lambda ()
   (interactive)
   (call-interactively
    (intern
     (ido-completing-read
      "M-x "
      (all-completions "" obarray 'commandp))))))
嗼ふ静 2024-07-29 01:29:43

我认为 ido-mode 还没有为此做好准备。 特别是,ido-completing-read 目前仅适用于字符串,而 completing-read 也支持列表。 当您想要对要完成的项目有不同的用户级别描述时,这一点非常重要。

因此,我对它还不能开箱即用并不感到惊讶。 如果不亲自修改代码,您最好的选择可能就是提交错误报告/功能请求。

I don't think ido-mode is ready for this quite yet. In particular, ido-completing-read currently only works with strings, while completing-read supports alists as well. This is very important once you want to have a different user-level description of the items you want to complete on.

Therefore I am not surprised that it doesn't work out of the box, yet. Short of modifying the code yourself your best bet is probably to just file a bug report/feature request.

我是有多爱你 2024-07-29 01:29:43

Ido 附带了一个可以执行此操作的函数,因此只需在 .emacs 文件中调用它即可:

(ido-无处不在)

Ido comes with a function that should do this, so just call it in your .emacs file:

(ido-everywhere t)

身边 2024-07-29 01:29:43

使用 Emacs 24.3,ido-ubiquitous 对我不起作用。 所以尝试了这个,到目前为止效果很好:

(defun my-completing-read (prompt collection &optional predicate
                  require-match initial-input
                  hist def inherit-input-method)
  (if (listp collection)
      (ido-completing-read prompt collection predicate require-match
               initial-input hist def inherit-input-method)
    (completing-read-default prompt collection predicate require-match
                 initial-input hist def inherit-input-method)))

(setq completing-read-function 'my-completing-read)

Using Emacs 24.3, ido-ubiquitous didn't work for me. So tried this and it is working fine so far:

(defun my-completing-read (prompt collection &optional predicate
                  require-match initial-input
                  hist def inherit-input-method)
  (if (listp collection)
      (ido-completing-read prompt collection predicate require-match
               initial-input hist def inherit-input-method)
    (completing-read-default prompt collection predicate require-match
                 initial-input hist def inherit-input-method)))

(setq completing-read-function 'my-completing-read)
我一向站在原地 2024-07-29 01:29:43

只是一个想法:您是否尝试过编辑 ido-completing-read 来调用 original-completing-read 而不是 completing-read,定义 original-completing-read 成为当前的 completing-read 然后做你的 defalias 或 setf 事情?

Just a thought: have you tried editing ido-completing-read to call original-completing-read instead of completing-read, defining original-completing-read to be the current completing-read and then doing your defalias or setf thing?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文