]>
Commit | Line | Data |
---|---|---|
1 | ;;; company-files.el --- company-mode completion backend for file names | |
2 | ||
3 | ;; Copyright (C) 2009-2011, 2013-2021 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Author: Nikolaj Schumacher | |
6 | ||
7 | ;; This file is part of GNU Emacs. | |
8 | ||
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. | |
13 | ||
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. | |
18 | ||
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/>. | |
21 | ||
22 | ||
23 | ;;; Commentary: | |
24 | ;; | |
25 | ||
26 | ;;; Code: | |
27 | ||
28 | (require 'company) | |
29 | (require 'cl-lib) | |
30 | ||
31 | (defgroup company-files nil | |
32 | "Completion backend for file names." | |
33 | :group 'company) | |
34 | ||
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")) | |
40 | ||
41 | (defcustom company-files-chop-trailing-slash t | |
42 | "Non-nil to remove the trailing slash after inserting directory name. | |
43 | ||
44 | This way it's easy to continue completion by typing `/' again. | |
45 | ||
46 | Set this to nil to disable that behavior." | |
47 | :type 'boolean) | |
48 | ||
49 | (defun company-files--directory-files (dir prefix) | |
50 | ;; Don't use directory-files. It produces directories without trailing /. | |
51 | (condition-case err | |
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))) | |
56 | (if (equal prefix "") | |
57 | (delete "../" (delete "./" comp)) | |
58 | comp)) | |
59 | (file-error nil))) | |
60 | ||
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 | |
65 | dir-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)) | |
71 | file-exclusions)) | |
72 | collect c))) | |
73 | ||
74 | (defvar company-files--regexps | |
75 | (let* ((root (if (eq system-type 'windows-nt) | |
76 | "[a-zA-Z]:/" | |
77 | "/")) | |
78 | (begin (concat "\\(?:\\.\\{1,2\\}/\\|~/\\|" root "\\)"))) | |
79 | (list (concat "\"\\(" begin "[^\"\n]*\\)") | |
80 | (concat "\'\\(" begin "[^\'\n]*\\)") | |
81 | (concat "\\(?:[ \t=\[]\\|^\\)\\(" begin "[^ \t\n]*\\)")))) | |
82 | ||
83 | (defun company-files--grab-existing-name () | |
84 | ;; Grab the file name. | |
85 | ;; When surrounded with quotes, it can include spaces. | |
86 | (let (file dir) | |
87 | (and (cl-dolist (regexp company-files--regexps) | |
88 | (when (setq file (company-grab-line regexp 1)) | |
89 | (cl-return file))) | |
90 | (company-files--connected-p file) | |
91 | (setq dir (file-name-directory file)) | |
92 | (not (string-match "//" dir)) | |
93 | (file-exists-p dir) | |
94 | file))) | |
95 | ||
96 | (defun company-files--connected-p (file) | |
97 | (or (not (file-remote-p file)) | |
98 | (file-remote-p file nil t))) | |
99 | ||
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)) ?/)))) | |
105 | ||
106 | (defvar company-files--completion-cache nil) | |
107 | ||
108 | (defun company-files--complete (prefix) | |
109 | (let* ((dir (file-name-directory prefix)) | |
110 | (file (file-name-nondirectory prefix)) | |
111 | (key (list file | |
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))) | |
123 | candidates))) | |
124 | (children (and directories | |
125 | (cl-mapcan (lambda (d) | |
126 | (mapcar (lambda (c) (concat d c)) | |
127 | (company-files--directory-files d ""))) | |
128 | directories)))) | |
129 | (setq company-files--completion-cache | |
130 | (cons key (append candidates children))))) | |
131 | (all-completions prefix | |
132 | (cdr company-files--completion-cache)))) | |
133 | ||
134 | (defun company-file--keys-match-p (new old) | |
135 | (and (equal (cdr old) (cdr new)) | |
136 | (string-prefix-p (car old) (car new)))) | |
137 | ||
138 | (defun company-files--post-completion (arg) | |
139 | (when (and company-files-chop-trailing-slash | |
140 | (company-files--trailing-slash-p arg)) | |
141 | (delete-char -1))) | |
142 | ||
143 | ;;;###autoload | |
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)) | |
149 | (cl-case command | |
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)) | |
157 | (sorted t) | |
158 | (no-cache t))) | |
159 | ||
160 | (provide 'company-files) | |
161 | ;;; company-files.el ends here |