1 ;;; company-files.el --- company-mode completion backend for file paths
3 ;; Copyright (C) 2009-2011, 2014-2015 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 <http://www.gnu.org/licenses/>.
31 (defun company-files--directory-files (dir prefix)
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))))))
37 (delete "../" (delete "./" comp))
40 (defvar company-files--regexps
41 (let* ((root (if (eq system-type 'windows-nt)
44 (begin (concat "\\(?:\\.\\{1,2\\}/\\|~/\\|" root "\\)")))
45 (list (concat "\"\\(" begin "[^\"\n]*\\)")
46 (concat "\'\\(" begin "[^\'\n]*\\)")
47 (concat "\\(?:[ \t]\\|^\\)\\(" begin "[^ \t\n]*\\)"))))
49 (defun company-files--grab-existing-name ()
50 ;; Grab the file name.
51 ;; When surrounded with quotes, it can include spaces.
53 (and (cl-dolist (regexp company-files--regexps)
54 (when (setq file (company-grab-line regexp 1))
56 (company-files--connected-p file)
57 (setq dir (file-name-directory file))
58 (not (string-match "//" dir))
62 (defun company-files--connected-p (file)
63 (or (not (file-remote-p file))
64 (file-remote-p file nil t)))
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)) ?/))))
72 (defvar company-files--completion-cache nil)
74 (defun company-files--complete (prefix)
75 (let* ((dir (file-name-directory prefix))
76 (file (file-name-nondirectory prefix))
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)))
90 (children (and directories
91 (cl-mapcan (lambda (d)
92 (mapcar (lambda (c) (concat d c))
93 (company-files--directory-files d "")))
95 (setq company-files--completion-cache
96 (cons key (append candidates children)))))
97 (all-completions prefix
98 (cdr company-files--completion-cache))))
100 (defun company-file--keys-match-p (new old)
101 (and (equal (cdr old) (cdr new))
102 (string-prefix-p (car old) (car new))))
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))
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)
121 (provide 'company-files)
122 ;;; company-files.el ends here