]> code.delx.au - gnu-emacs-elpa/blob - company-files.el
Bumped version to 0.4.
[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.
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 (defun company-files-grab-existing-name ()
30 ;; Grab file names with spaces, only when they include quotes.
31 (let ((file (or (company-grab-line "\"\\(~?/[^\"\n]*\\)" 1)
32 (company-grab-line "\'\\(~?/[^\'\n]*\\)" 1)
33 (company-grab-line "[ \t\n]\\(~?/[^ \t\n]*\\)" 1)))
34 dir)
35 (and file
36 (setq dir (file-name-directory file))
37 (file-exists-p dir)
38 (file-name-all-completions (file-name-nondirectory file) dir)
39 file)))
40
41 (defvar company-files-completion-cache nil)
42
43 (defun company-files-complete (prefix)
44 (let* ((dir (file-name-directory prefix))
45 (file (file-name-nondirectory prefix))
46 candidates)
47 (setq company-files-completion-cache nil)
48 (unless (equal dir (car company-files-completion-cache))
49 (dolist (file (company-files-directory-files dir file))
50 (setq file (concat dir file))
51 (push file candidates)
52 (when (file-directory-p file)
53 ;; Add one level of children.
54 (dolist (child (company-files-directory-files file ""))
55 (push (concat file child) candidates))))
56 (setq company-files-completion-cache (cons dir (nreverse candidates))))
57 (cdr company-files-completion-cache)))
58
59 ;;;###autoload
60 (defun company-files (command &optional arg &rest ignored)
61 "a `company-mode' completion back-end existing file names."
62 (interactive (list 'interactive))
63 (case command
64 ('interactive (company-begin-backend 'company-files))
65 ('prefix (company-files-grab-existing-name))
66 ('candidates (company-files-complete arg))
67 ('location (cons (dired-noselect
68 (file-name-directory (directory-file-name arg))) 1))
69 ('sorted t)
70 ('no-cache t)))
71
72 (provide 'company-files)
73 ;;; company-files.el ends here