]> code.delx.au - gnu-emacs-elpa/blob - company-files.el
Tag company-dabbrev-ignore-buffers with package-version
[gnu-emacs-elpa] / company-files.el
1 ;;; company-files.el --- company-mode completion backend for file paths
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 (defun company-files--directory-files (dir prefix)
32 (ignore-errors
33 ;; Don't use directory-files. It produces directories without trailing /.
34 (let ((comp (sort (file-name-all-completions prefix dir)
35 (lambda (s1 s2) (string-lessp (downcase s1) (downcase s2))))))
36 (if (equal prefix "")
37 (delete "../" (delete "./" comp))
38 comp))))
39
40 (defvar company-files--regexps
41 (let* ((root (if (eq system-type 'windows-nt)
42 "[a-zA-Z]:/"
43 "/"))
44 (begin (concat "\\(?:\\.\\{1,2\\}/\\|~/\\|" root "\\)")))
45 (list (concat "\"\\(" begin "[^\"\n]*\\)")
46 (concat "\'\\(" begin "[^\'\n]*\\)")
47 (concat "\\(?:[ \t]\\|^\\)\\(" begin "[^ \t\n]*\\)"))))
48
49 (defun company-files--grab-existing-name ()
50 ;; Grab the file name.
51 ;; When surrounded with quotes, it can include spaces.
52 (let (file dir)
53 (and (cl-dolist (regexp company-files--regexps)
54 (when (setq file (company-grab-line regexp 1))
55 (cl-return file)))
56 (company-files--connected-p file)
57 (setq dir (file-name-directory file))
58 (not (string-match "//" dir))
59 (file-exists-p dir)
60 file)))
61
62 (defun company-files--connected-p (file)
63 (or (not (file-remote-p file))
64 (file-remote-p file nil t)))
65
66 (defun company-files--trailing-slash-p (file)
67 ;; `file-directory-p' is very expensive on remotes. We are relying on
68 ;; `file-name-all-completions' returning directories with trailing / instead.
69 (let ((len (length file)))
70 (and (> len 0) (eq (aref file (1- len)) ?/))))
71
72 (defvar company-files--completion-cache nil)
73
74 (defun company-files--complete (prefix)
75 (let* ((dir (file-name-directory prefix))
76 (file (file-name-nondirectory prefix))
77 (key (list file
78 (expand-file-name dir)
79 (nth 5 (file-attributes dir))))
80 (completion-ignore-case read-file-name-completion-ignore-case))
81 (unless (company-file--keys-match-p key (car company-files--completion-cache))
82 (let* ((candidates (mapcar (lambda (f) (concat dir f))
83 (company-files--directory-files dir file)))
84 (directories (unless (file-remote-p dir)
85 (cl-remove-if-not (lambda (f)
86 (and (company-files--trailing-slash-p f)
87 (not (file-remote-p f))
88 (company-files--connected-p f)))
89 candidates)))
90 (children (and directories
91 (cl-mapcan (lambda (d)
92 (mapcar (lambda (c) (concat d c))
93 (company-files--directory-files d "")))
94 directories))))
95 (setq company-files--completion-cache
96 (cons key (append candidates children)))))
97 (all-completions prefix
98 (cdr company-files--completion-cache))))
99
100 (defun company-file--keys-match-p (new old)
101 (and (equal (cdr old) (cdr new))
102 (string-prefix-p (car old) (car new))))
103
104 ;;;###autoload
105 (defun company-files (command &optional arg &rest ignored)
106 "`company-mode' completion backend existing file names.
107 Completions works for proper absolute and relative files paths.
108 File paths with spaces are only supported inside strings."
109 (interactive (list 'interactive))
110 (cl-case command
111 (interactive (company-begin-backend 'company-files))
112 (prefix (company-files--grab-existing-name))
113 (candidates (company-files--complete arg))
114 (location (cons (dired-noselect
115 (file-name-directory (directory-file-name arg))) 1))
116 (post-completion (when (company-files--trailing-slash-p arg)
117 (delete-char -1)))
118 (sorted t)
119 (no-cache t)))
120
121 (provide 'company-files)
122 ;;; company-files.el ends here