1 ;;; company-files.el --- company-mode completion back-end for file names
3 ;; Copyright (C) 2009-2011, 2013 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)
34 (directory-files dir nil "\\`[^.]\\|\\`.[^.]")
35 (file-name-all-completions prefix dir))))
37 (defvar company-files-regexps
38 (let* ((begin (if (eq system-type 'windows-nt)
41 (begin (concat "\\(?:\\.\\{1,2\\}/\\|~/\\|" begin "\\)")))
42 (list (concat "\"\\(" begin "[^\"\n]*\\)")
43 (concat "\'\\(" begin "[^\'\n]*\\)")
44 (concat "\\(?:[ \t]\\|^\\)\\(" begin "[^ \t\n]*\\)"))))
46 (defun company-files-grab-existing-name ()
47 ;; Grab file names with spaces, only when they include quotes.
49 (and (cl-dolist (regexp company-files-regexps)
50 (when (setq file (company-grab-line regexp 1))
52 (setq dir (file-name-directory file))
53 (not (string-match "//" dir))
55 (file-name-all-completions (file-name-nondirectory file) dir)
58 (defvar company-files-completion-cache nil)
60 (defun company-files-complete (prefix)
61 (let* ((dir (file-name-directory prefix))
62 (dir-exp (expand-file-name dir))
63 (file (file-name-nondirectory prefix))
64 candidates directories)
65 (unless (equal dir-exp (car company-files-completion-cache))
66 (dolist (file (company-files-directory-files dir file))
67 (setq file (concat dir file))
68 (push file candidates)
69 (when (file-directory-p file)
70 (push file directories)))
71 (dolist (directory (reverse directories))
72 ;; Add one level of children.
73 (dolist (child (company-files-directory-files directory ""))
74 (push (concat directory
75 (unless (eq (aref directory (1- (length directory))) ?/) "/")
77 (setq company-files-completion-cache (cons dir-exp (nreverse candidates))))
78 (all-completions prefix
79 (cdr company-files-completion-cache))))
82 (defun company-files (command &optional arg &rest ignored)
83 "`company-mode' completion back-end existing file names.
84 Completions are triggered when the text inside quotes starts with any of
85 the following: ./, ../, ~/, or root drive"
86 (interactive (list 'interactive))
88 (interactive (company-begin-backend 'company-files))
89 (prefix (company-files-grab-existing-name))
90 (candidates (company-files-complete arg))
91 (location (cons (dired-noselect
92 (file-name-directory (directory-file-name arg))) 1))
96 (provide 'company-files)
97 ;;; company-files.el ends here