]> rtime.felk.cvut.cz Git - sojka/company-mode.git/blob - company-files.el
Bumped version to 0.4.2.
[sojka/company-mode.git] / 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          (file-exists-p dir)
45          (file-name-all-completions (file-name-nondirectory file) dir)
46          file)))
47
48 (defvar company-files-completion-cache nil)
49
50 (defun company-files-complete (prefix)
51   (let* ((dir (file-name-directory prefix))
52          (file (file-name-nondirectory prefix))
53          candidates)
54     (unless (equal dir (car company-files-completion-cache))
55       (dolist (file (company-files-directory-files dir file))
56         (setq file (concat dir file))
57         (push file candidates)
58         (when (file-directory-p file)
59           ;; Add one level of children.
60           (dolist (child (company-files-directory-files file ""))
61             (push (concat file child) candidates))))
62       (setq company-files-completion-cache (cons dir (nreverse candidates))))
63     (cdr company-files-completion-cache)))
64
65 ;;;###autoload
66 (defun company-files (command &optional arg &rest ignored)
67   "a `company-mode' completion back-end existing file names."
68   (interactive (list 'interactive))
69   (case command
70     ('interactive (company-begin-backend 'company-files))
71     ('prefix (company-files-grab-existing-name))
72     ('candidates (company-files-complete arg))
73     ('location (cons (dired-noselect
74                       (file-name-directory (directory-file-name arg))) 1))
75     ('sorted t)
76     ('no-cache t)))
77
78 (provide 'company-files)
79 ;;; company-files.el ends here