1 ;;; company-files.el --- company-mode completion backend for file names
3 ;; Copyright (C) 2009-2011, 2013-2021 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
31 (defgroup company-files nil
32 "Completion backend for file names."
35 (defcustom company-files-exclusions nil
36 "A list of file name extensions and directory names to ignore.
37 The values should use the same format as `completion-ignored-extensions'."
38 :type '(repeat (string :tag "File extension or directory name"))
39 :package-version '(company . "0.9.1"))
41 (defcustom company-files-chop-trailing-slash t
42 "Non-nil to remove the trailing slash after inserting directory name.
44 This way it's easy to continue completion by typing `/' again.
46 Set this to nil to disable that behavior."
49 (defun company-files--directory-files (dir prefix)
50 ;; Don't use directory-files. It produces directories without trailing /.
52 (let ((comp (sort (file-name-all-completions prefix dir)
53 (lambda (s1 s2) (string-lessp (downcase s1) (downcase s2))))))
54 (when company-files-exclusions
55 (setq comp (company-files--exclusions-filtered comp)))
57 (delete "../" (delete "./" comp))
61 (defun company-files--exclusions-filtered (completions)
62 (let* ((dir-exclusions (cl-remove-if-not #'company-files--trailing-slash-p
63 company-files-exclusions))
64 (file-exclusions (cl-set-difference company-files-exclusions
66 (cl-loop for c in completions
67 unless (if (company-files--trailing-slash-p c)
68 (member c dir-exclusions)
69 (cl-find-if (lambda (exclusion)
70 (string-suffix-p exclusion c))
74 (defvar company-files--regexps
75 (let* ((root (if (eq system-type 'windows-nt)
78 (begin (concat "\\(?:\\.\\{1,2\\}/\\|~/\\|" root "\\)")))
79 (list (concat "\"\\(" begin "[^\"\n]*\\)")
80 (concat "\'\\(" begin "[^\'\n]*\\)")
81 (concat "\\(?:[ \t=\[]\\|^\\)\\(" begin "[^ \t\n]*\\)"))))
83 (defun company-files--grab-existing-name ()
84 ;; Grab the file name.
85 ;; When surrounded with quotes, it can include spaces.
87 (and (cl-dolist (regexp company-files--regexps)
88 (when (setq file (company-grab-line regexp 1))
90 (company-files--connected-p file)
91 (setq dir (file-name-directory file))
92 (not (string-match "//" dir))
96 (defun company-files--connected-p (file)
97 (or (not (file-remote-p file))
98 (file-remote-p file nil t)))
100 (defun company-files--trailing-slash-p (file)
101 ;; `file-directory-p' is very expensive on remotes. We are relying on
102 ;; `file-name-all-completions' returning directories with trailing / instead.
103 (let ((len (length file)))
104 (and (> len 0) (eq (aref file (1- len)) ?/))))
106 (defvar company-files--completion-cache nil)
108 (defun company-files--complete (prefix)
109 (let* ((dir (file-name-directory prefix))
110 (file (file-name-nondirectory prefix))
112 (expand-file-name dir)
113 (nth 5 (file-attributes dir))))
114 (completion-ignore-case read-file-name-completion-ignore-case))
115 (unless (company-file--keys-match-p key (car company-files--completion-cache))
116 (let* ((candidates (mapcar (lambda (f) (concat dir f))
117 (company-files--directory-files dir file)))
118 (directories (unless (file-remote-p dir)
119 (cl-remove-if-not (lambda (f)
120 (and (company-files--trailing-slash-p f)
121 (not (file-remote-p f))
122 (company-files--connected-p f)))
124 (children (and directories
125 (cl-mapcan (lambda (d)
126 (mapcar (lambda (c) (concat d c))
127 (company-files--directory-files d "")))
129 (setq company-files--completion-cache
130 (cons key (append candidates children)))))
131 (all-completions prefix
132 (cdr company-files--completion-cache))))
134 (defun company-file--keys-match-p (new old)
135 (and (equal (cdr old) (cdr new))
136 (string-prefix-p (car old) (car new))))
138 (defun company-files--post-completion (arg)
139 (when (and company-files-chop-trailing-slash
140 (company-files--trailing-slash-p arg))
144 (defun company-files (command &optional arg &rest ignored)
145 "`company-mode' completion backend existing file names.
146 Completions works for proper absolute and relative files paths.
147 File paths with spaces are only supported inside strings."
148 (interactive (list 'interactive))
150 (interactive (company-begin-backend 'company-files))
151 (prefix (company-files--grab-existing-name))
152 (candidates (company-files--complete arg))
153 (location (cons (dired-noselect
154 (file-name-directory (directory-file-name arg))) 1))
155 (post-completion (company-files--post-completion arg))
156 (kind (if (string-suffix-p "/" arg) 'folder 'file))
160 (provide 'company-files)
161 ;;; company-files.el ends here