commit | author | age
|
5cb5f7
|
1 |
;;; company-files.el --- company-mode completion backend for file names |
C |
2 |
|
|
3 |
;; Copyright (C) 2009-2011, 2014-2015 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 <http://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 |
"File name extensions and directory names to ignore. |
|
37 |
The values should use the same format as `completion-ignored-extensions'." |
|
38 |
:type '(const string) |
|
39 |
:package-version '(company . "0.9.1")) |
|
40 |
|
|
41 |
(defun company-files--directory-files (dir prefix) |
|
42 |
;; Don't use directory-files. It produces directories without trailing /. |
|
43 |
(condition-case err |
|
44 |
(let ((comp (sort (file-name-all-completions prefix dir) |
|
45 |
(lambda (s1 s2) (string-lessp (downcase s1) (downcase s2)))))) |
|
46 |
(when company-files-exclusions |
|
47 |
(setq comp (company-files--exclusions-filtered comp))) |
|
48 |
(if (equal prefix "") |
|
49 |
(delete "../" (delete "./" comp)) |
|
50 |
comp)) |
|
51 |
(file-error nil))) |
|
52 |
|
|
53 |
(defun company-files--exclusions-filtered (completions) |
|
54 |
(let* ((dir-exclusions (cl-delete-if-not #'company-files--trailing-slash-p |
|
55 |
company-files-exclusions)) |
|
56 |
(file-exclusions (cl-set-difference company-files-exclusions |
|
57 |
dir-exclusions))) |
|
58 |
(cl-loop for c in completions |
|
59 |
unless (if (company-files--trailing-slash-p c) |
|
60 |
(member c dir-exclusions) |
|
61 |
(cl-find-if (lambda (exclusion) |
|
62 |
(string-suffix-p exclusion c)) |
|
63 |
file-exclusions)) |
|
64 |
collect c))) |
|
65 |
|
|
66 |
(defvar company-files--regexps |
|
67 |
(let* ((root (if (eq system-type 'windows-nt) |
|
68 |
"[a-zA-Z]:/" |
|
69 |
"/")) |
|
70 |
(begin (concat "\\(?:\\.\\{1,2\\}/\\|~/\\|" root "\\)"))) |
|
71 |
(list (concat "\"\\(" begin "[^\"\n]*\\)") |
|
72 |
(concat "\'\\(" begin "[^\'\n]*\\)") |
|
73 |
(concat "\\(?:[ \t=]\\|^\\)\\(" begin "[^ \t\n]*\\)")))) |
|
74 |
|
|
75 |
(defun company-files--grab-existing-name () |
|
76 |
;; Grab the file name. |
|
77 |
;; When surrounded with quotes, it can include spaces. |
|
78 |
(let (file dir) |
|
79 |
(and (cl-dolist (regexp company-files--regexps) |
|
80 |
(when (setq file (company-grab-line regexp 1)) |
|
81 |
(cl-return file))) |
|
82 |
(company-files--connected-p file) |
|
83 |
(setq dir (file-name-directory file)) |
|
84 |
(not (string-match "//" dir)) |
|
85 |
(file-exists-p dir) |
|
86 |
file))) |
|
87 |
|
|
88 |
(defun company-files--connected-p (file) |
|
89 |
(or (not (file-remote-p file)) |
|
90 |
(file-remote-p file nil t))) |
|
91 |
|
|
92 |
(defun company-files--trailing-slash-p (file) |
|
93 |
;; `file-directory-p' is very expensive on remotes. We are relying on |
|
94 |
;; `file-name-all-completions' returning directories with trailing / instead. |
|
95 |
(let ((len (length file))) |
|
96 |
(and (> len 0) (eq (aref file (1- len)) ?/)))) |
|
97 |
|
|
98 |
(defvar company-files--completion-cache nil) |
|
99 |
|
|
100 |
(defun company-files--complete (prefix) |
|
101 |
(let* ((dir (file-name-directory prefix)) |
|
102 |
(file (file-name-nondirectory prefix)) |
|
103 |
(key (list file |
|
104 |
(expand-file-name dir) |
|
105 |
(nth 5 (file-attributes dir)))) |
|
106 |
(completion-ignore-case read-file-name-completion-ignore-case)) |
|
107 |
(unless (company-file--keys-match-p key (car company-files--completion-cache)) |
|
108 |
(let* ((candidates (mapcar (lambda (f) (concat dir f)) |
|
109 |
(company-files--directory-files dir file))) |
|
110 |
(directories (unless (file-remote-p dir) |
|
111 |
(cl-remove-if-not (lambda (f) |
|
112 |
(and (company-files--trailing-slash-p f) |
|
113 |
(not (file-remote-p f)) |
|
114 |
(company-files--connected-p f))) |
|
115 |
candidates))) |
|
116 |
(children (and directories |
|
117 |
(cl-mapcan (lambda (d) |
|
118 |
(mapcar (lambda (c) (concat d c)) |
|
119 |
(company-files--directory-files d ""))) |
|
120 |
directories)))) |
|
121 |
(setq company-files--completion-cache |
|
122 |
(cons key (append candidates children))))) |
|
123 |
(all-completions prefix |
|
124 |
(cdr company-files--completion-cache)))) |
|
125 |
|
|
126 |
(defun company-file--keys-match-p (new old) |
|
127 |
(and (equal (cdr old) (cdr new)) |
|
128 |
(string-prefix-p (car old) (car new)))) |
|
129 |
|
|
130 |
;;;###autoload |
|
131 |
(defun company-files (command &optional arg &rest ignored) |
|
132 |
"`company-mode' completion backend existing file names. |
|
133 |
Completions works for proper absolute and relative files paths. |
|
134 |
File paths with spaces are only supported inside strings." |
|
135 |
(interactive (list 'interactive)) |
|
136 |
(cl-case command |
|
137 |
(interactive (company-begin-backend 'company-files)) |
|
138 |
(prefix (company-files--grab-existing-name)) |
|
139 |
(candidates (company-files--complete arg)) |
|
140 |
(location (cons (dired-noselect |
|
141 |
(file-name-directory (directory-file-name arg))) 1)) |
|
142 |
(post-completion (when (company-files--trailing-slash-p arg) |
|
143 |
(delete-char -1))) |
|
144 |
(sorted t) |
|
145 |
(no-cache t))) |
|
146 |
|
|
147 |
(provide 'company-files) |
|
148 |
;;; company-files.el ends here |