]> code.delx.au - gnu-emacs-elpa/blob - packages/company-0.5/company-files.el
* COPYING, ChangeLog, README, admin/org-synch.el,
[gnu-emacs-elpa] / packages / company-0.5 / company-files.el
1 ;;; company-files.el --- a company-mode completion back-end for file names
2
3 ;; Copyright (C) 2009, 2010 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 ;;; Code:
23
24 (require 'company)
25 (eval-when-compile (require 'cl))
26
27 (defun company-files-directory-files (dir prefix)
28 (ignore-errors
29 (if (equal prefix "")
30 (directory-files dir nil "\\`[^.]\\|\\`.[^.]")
31 (file-name-all-completions prefix dir))))
32
33 (defvar company-files-regexps
34 (let ((begin (if (eq system-type 'windows-nt)
35 "[a-z][A-Z]\\"
36 "~?/")))
37 (list (concat "\"\\(" begin "[^\"\n]*\\)")
38 (concat "\'\\(" begin "[^\'\n]*\\)")
39 (concat "\\(?:[ \t]\\|^\\)\\(" begin "[^ \t\n]*\\)"))))
40
41 (defun company-files-grab-existing-name ()
42 ;; Grab file names with spaces, only when they include quotes.
43 (let (file dir)
44 (and (dolist (regexp company-files-regexps)
45 (when (setq file (company-grab-line regexp 1))
46 (return file)))
47 (setq dir (file-name-directory file))
48 (not (string-match "//" dir))
49 (file-exists-p dir)
50 (file-name-all-completions (file-name-nondirectory file) dir)
51 file)))
52
53 (defvar company-files-completion-cache nil)
54
55 (defun company-files-complete (prefix)
56 (let* ((dir (file-name-directory prefix))
57 (file (file-name-nondirectory prefix))
58 candidates)
59 (unless (equal dir (car company-files-completion-cache))
60 (dolist (file (company-files-directory-files dir file))
61 (setq file (concat dir file))
62 (push file candidates)
63 (when (file-directory-p file)
64 ;; Add one level of children.
65 (dolist (child (company-files-directory-files file ""))
66 (push (concat file child) candidates))))
67 (setq company-files-completion-cache (cons dir (nreverse candidates))))
68 (cdr company-files-completion-cache)))
69
70 ;;;###autoload
71 (defun company-files (command &optional arg &rest ignored)
72 "a `company-mode' completion back-end existing file names."
73 (interactive (list 'interactive))
74 (case command
75 ('interactive (company-begin-backend 'company-files))
76 ('prefix (company-files-grab-existing-name))
77 ('candidates (company-files-complete arg))
78 ('location (cons (dired-noselect
79 (file-name-directory (directory-file-name arg))) 1))
80 ('sorted t)
81 ('no-cache t)))
82
83 (provide 'company-files)
84 ;;; company-files.el ends here