Prefers some file extensions with Emacs filename completion

I have many directories filled with a bunch of TeX documents. So, there are many files with the same base name and different extensions. However, only one of them is editable. I would like to convince Emacs that if I'm in the directory where I have

document.tex document.log document.pdf document.bbl document.aux ... 

and I'm in the minibuffer and doing

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

it fills in 'document.tex' because it is the only truly editable document in this directory. Does anyone know a good way to do this?

+4
source share
3 answers

I wrote code that should do what you want. The main idea is to set the variable 'completion-ignored-extensions to match the extensions you want to skip, but only with .tex files. This code does this.

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

Enjoy.

+6
source

Probably the easiest way to do this in your case is to simply set the completion-ignored extension variable.

However, this will mean that emacs always ignores things like ".log" and ".pdf", which may not be what you want. If you want it to be more selective, you might have to efficiently re-implement the function name-completion.

+1
source

If you are open to installing a large library and reading some documentation, you can look at Icicles and define a sort function to suit your needs. An alternative is ido , whose wiki page has an example of sorting by mtime , which should be easily changed to sort by the file name extension function.

+1
source

All Articles