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