]> code.delx.au - gnu-emacs-elpa/blob - company-files.el
Add support for company-face
[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 (if (equal prefix "")
34 (directory-files dir nil "\\`[^.]\\|\\`.[^.]")
35 (file-name-all-completions prefix dir))))
36
37 (defvar company-files--regexps
38 (let* ((root (if (eq system-type 'windows-nt)
39 "[a-zA-Z]:/"
40 "/"))
41 (begin (concat "\\(?:\\.\\{1,2\\}/\\|~/\\|" root "\\)")))
42 (list (concat "\"\\(" begin "[^\"\n]*\\)")
43 (concat "\'\\(" begin "[^\'\n]*\\)")
44 (concat "\\(?:[ \t]\\|^\\)\\(" begin "[^ \t\n]*\\)"))))
45
46 (defun company-files--grab-existing-name ()
47 ;; Grab the file name.
48 ;; When surrounded with quotes, it can include spaces.
49 (let (file dir)
50 (and (cl-dolist (regexp company-files--regexps)
51 (when (setq file (company-grab-line regexp 1))
52 (cl-return file)))
53 (company-files--connected-p file)
54 (setq dir (file-name-directory file))
55 (not (string-match "//" dir))
56 (file-exists-p dir)
57 (file-name-all-completions (file-name-nondirectory file) dir)
58 file)))
59
60 (defun company-files--connected-p (file)
61 (or (not (file-remote-p file))
62 (file-remote-p file nil t)))
63
64 (defvar company-files--completion-cache nil)
65
66 (defun company-files--complete (prefix)
67 (let* ((dir (file-name-directory prefix))
68 (key (list (file-name-nondirectory prefix)
69 (expand-file-name dir)
70 (nth 5 (file-attributes dir))))
71 (file (file-name-nondirectory prefix))
72 (completion-ignore-case read-file-name-completion-ignore-case)
73 candidates directories)
74 (unless (company-file--keys-match-p key (car company-files--completion-cache))
75 (dolist (file (company-files--directory-files dir file))
76 (setq file (concat dir file))
77 (when (company-files--connected-p file)
78 (push file candidates)
79 (when (file-directory-p file)
80 (push file directories))))
81 (dolist (directory (reverse directories))
82 ;; Add one level of children.
83 (dolist (child (company-files--directory-files directory ""))
84 (push (concat directory
85 (unless (eq (aref directory (1- (length directory))) ?/) "/")
86 child) candidates)))
87 (setq company-files--completion-cache (cons key (nreverse candidates))))
88 (all-completions prefix
89 (cdr company-files--completion-cache))))
90
91 (defun company-file--keys-match-p (new old)
92 (and (equal (cdr old) (cdr new))
93 (string-prefix-p (car old) (car new))))
94
95 ;;;###autoload
96 (defun company-files (command &optional arg &rest ignored)
97 "`company-mode' completion backend existing file names.
98 Completions works for proper absolute and relative files paths.
99 File paths with spaces are only supported inside strings."
100 (interactive (list 'interactive))
101 (cl-case command
102 (interactive (company-begin-backend 'company-files))
103 (prefix (company-files--grab-existing-name))
104 (candidates (company-files--complete arg))
105 (location (cons (dired-noselect
106 (file-name-directory (directory-file-name arg))) 1))
107 (sorted t)
108 (no-cache t)))
109
110 (provide 'company-files)
111 ;;; company-files.el ends here