首选具有Emacs文件名完成的某些文件扩展名

首选具有Emacs文件名完成的某些文件扩展名,emacs,Emacs,我有很多目录,里面装满了一堆TeX文档。因此,有许多文件具有相同的基本文件名和不同的扩展名。但是,其中只有一个是可编辑的。我想用一种方法说服Emacs,如果我在一个目录中 document.tex document.log document.pdf document.bbl document.aux ... 我在迷你缓冲区里,做什么 ~/Documents/.../doc<TAB> ~/Documents/../doc 它填充“document.tex”,因为这是该目录中唯一真

我有很多目录,里面装满了一堆TeX文档。因此,有许多文件具有相同的基本文件名和不同的扩展名。但是,其中只有一个是可编辑的。我想用一种方法说服Emacs,如果我在一个目录中

document.tex
document.log
document.pdf
document.bbl
document.aux
...
我在迷你缓冲区里,做什么

~/Documents/.../doc<TAB>
~/Documents/../doc

它填充“document.tex”,因为这是该目录中唯一真正可正确编辑的文档。有人知道这样做的好方法吗?

在您的情况下,最简单的方法可能就是自定义变量“completion-ignored extensions”


然而,这将意味着emacs总是忽略诸如“.log”和“.pdf”之类的内容,这可能不是您想要的。如果您希望它更具选择性,您可能必须有效地重新实现函数文件名完成。

如果您愿意安装大型ish库并阅读一些文档,您可以查看并定义满足您需要的。另一种选择是,其wiki页面上有一个示例,可以很容易地更改为按文件扩展名的函数排序。

我已经编写了一些代码,可以实现您想要的功能。基本思想是设置变量
'completion-ignored-extensions
以匹配要跳过的扩展名,但仅当存在
.tex
文件时。这个代码就是这样做的

(defadvice find-file-read-args (around find-file-read-args-limit-choices activate)
  "set some stuff up for controlling extensions when tab completing"
  (let ((completion-ignored-extensions completion-ignored-extensions)
        (find-file-limit-choices t))
    ad-do-it))

(defadvice minibuffer-complete (around minibuffer-complete-limit-choices nil activate)
  "When in find-file, check for files of extension .tex, and if they're found, ignore .log .pdf .bbl .aux"
  (let ((add-or-remove
     (if (and (boundp 'find-file-limit-choices) find-file-limit-choices
          (save-excursion
        (let ((b (progn (beginning-of-line) (point)))
              (e (progn (end-of-line) (point))))
          (directory-files (file-name-directory (buffer-substring-no-properties b e)) nil "\\.tex$"))))
     'add-to-list
       'remove)))
(mapc (lambda (e) (setq completion-ignored-extensions
            (funcall add-or-remove 'completion-ignored-extensions e)))
      '(".log" ".pdf" ".bbl" ".aux")))
  ad-do-it)

享受。

我得到了一个“mapcq”的未定义函数错误——这是由我应该加载的elisp包提供的吗?我应该提到我尝试了“mapc”和“mapchar”,但两个都不起作用。不过这看起来很棒,谢谢!mapcq是一个输入错误,我修正了它。mapc为我工作,因为代码符合我的期望。一个问题可能是代码没有被激活。尝试在mapc行之前添加一个(y或n-p“Running?”),当您添加选项卡时,系统会提示您。如果未激活,则建议不会被激活(出于某些原因),您可以尝试添加(ad activate“minibuffer complete limit choices”)。当我在香草emacs中测试这个时,我不需要它。明白了!使用mapc和稍长一点的被忽略扩展列表(当然这是我的错),效果非常好。非常感谢!