]> code.delx.au - gnu-emacs-elpa/blob - company-files.el
Don't complete // as file name.
[gnu-emacs-elpa] / company-files.el
1 ;;; company-files.el --- a company-mode completion back-end for file names
2 ;;
3 ;; Copyright (C) 2009 Nikolaj Schumacher
4 ;;
5 ;; This file is part of company 0.4.2.
6 ;;
7 ;; This program is free software; you can redistribute it and/or
8 ;; modify it under the terms of the GNU General Public License
9 ;; as published by the Free Software Foundation; either version 2
10 ;; of the License, or (at your option) any later version.
11 ;;
12 ;; This program is distributed in the hope that it will be useful,
13 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 ;; GNU General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
19
20 (require 'company)
21 (eval-when-compile (require 'cl))
22
23 (defun company-files-directory-files (dir prefix)
24 (ignore-errors
25 (if (equal prefix "")
26 (directory-files dir nil "\\`[^.]\\|\\`.[^.]")
27 (file-name-all-completions prefix dir))))
28
29 (defvar company-files-regexps
30 (let ((begin (if (eq system-type 'windows-nt)
31 "[a-z][A-Z]\\"
32 "~?/")))
33 (list (concat "\"\\(" begin "[^\"\n]*\\)")
34 (concat "\'\\(" begin "[^\'\n]*\\)")
35 (concat "\\(?:[ \t]\\|^\\)\\(" begin "[^ \t\n]*\\)"))))
36
37 (defun company-files-grab-existing-name ()
38 ;; Grab file names with spaces, only when they include quotes.
39 (let (file dir)
40 (and (dolist (regexp company-files-regexps)
41 (when (setq file (company-grab-line regexp 1))
42 (return file)))
43 (setq dir (file-name-directory file))
44 (not (string-match "//" dir))
45 (file-exists-p dir)
46 (file-name-all-completions (file-name-nondirectory file) dir)
47 file)))
48
49 (defvar company-files-completion-cache nil)
50
51 (defun company-files-complete (prefix)
52 (let* ((dir (file-name-directory prefix))
53 (file (file-name-nondirectory prefix))
54 candidates)
55 (unless (equal dir (car company-files-completion-cache))
56 (dolist (file (company-files-directory-files dir file))
57 (setq file (concat dir file))
58 (push file candidates)
59 (when (file-directory-p file)
60 ;; Add one level of children.
61 (dolist (child (company-files-directory-files file ""))
62 (push (concat file child) candidates))))
63 (setq company-files-completion-cache (cons dir (nreverse candidates))))
64 (cdr company-files-completion-cache)))
65
66 ;;;###autoload
67 (defun company-files (command &optional arg &rest ignored)
68 "a `company-mode' completion back-end existing file names."
69 (interactive (list 'interactive))
70 (case command
71 ('interactive (company-begin-backend 'company-files))
72 ('prefix (company-files-grab-existing-name))
73 ('candidates (company-files-complete arg))
74 ('location (cons (dired-noselect
75 (file-name-directory (directory-file-name arg))) 1))
76 ('sorted t)
77 ('no-cache t)))
78
79 (provide 'company-files)
80 ;;; company-files.el ends here