Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
5.9 KiB

4 years ago
  1. ;;; company-files.el --- company-mode completion backend for file names
  2. ;; Copyright (C) 2009-2011, 2014-2015 Free Software Foundation, Inc.
  3. ;; Author: Nikolaj Schumacher
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;;
  17. ;;; Code:
  18. (require 'company)
  19. (require 'cl-lib)
  20. (defgroup company-files nil
  21. "Completion backend for file names."
  22. :group 'company)
  23. (defcustom company-files-exclusions nil
  24. "File name extensions and directory names to ignore.
  25. The values should use the same format as `completion-ignored-extensions'."
  26. :type '(const string)
  27. :package-version '(company . "0.9.1"))
  28. (defun company-files--directory-files (dir prefix)
  29. ;; Don't use directory-files. It produces directories without trailing /.
  30. (condition-case err
  31. (let ((comp (sort (file-name-all-completions prefix dir)
  32. (lambda (s1 s2) (string-lessp (downcase s1) (downcase s2))))))
  33. (when company-files-exclusions
  34. (setq comp (company-files--exclusions-filtered comp)))
  35. (if (equal prefix "")
  36. (delete "../" (delete "./" comp))
  37. comp))
  38. (file-error nil)))
  39. (defun company-files--exclusions-filtered (completions)
  40. (let* ((dir-exclusions (cl-delete-if-not #'company-files--trailing-slash-p
  41. company-files-exclusions))
  42. (file-exclusions (cl-set-difference company-files-exclusions
  43. dir-exclusions)))
  44. (cl-loop for c in completions
  45. unless (if (company-files--trailing-slash-p c)
  46. (member c dir-exclusions)
  47. (cl-find-if (lambda (exclusion)
  48. (string-suffix-p exclusion c))
  49. file-exclusions))
  50. collect c)))
  51. (defvar company-files--regexps
  52. (let* ((root (if (eq system-type 'windows-nt)
  53. "[a-zA-Z]:/"
  54. "/"))
  55. (begin (concat "\\(?:\\.\\{1,2\\}/\\|~/\\|" root "\\)")))
  56. (list (concat "\"\\(" begin "[^\"\n]*\\)")
  57. (concat "\'\\(" begin "[^\'\n]*\\)")
  58. (concat "\\(?:[ \t=]\\|^\\)\\(" begin "[^ \t\n]*\\)"))))
  59. (defun company-files--grab-existing-name ()
  60. ;; Grab the file name.
  61. ;; When surrounded with quotes, it can include spaces.
  62. (let (file dir)
  63. (and (cl-dolist (regexp company-files--regexps)
  64. (when (setq file (company-grab-line regexp 1))
  65. (cl-return file)))
  66. (company-files--connected-p file)
  67. (setq dir (file-name-directory file))
  68. (not (string-match "//" dir))
  69. (file-exists-p dir)
  70. file)))
  71. (defun company-files--connected-p (file)
  72. (or (not (file-remote-p file))
  73. (file-remote-p file nil t)))
  74. (defun company-files--trailing-slash-p (file)
  75. ;; `file-directory-p' is very expensive on remotes. We are relying on
  76. ;; `file-name-all-completions' returning directories with trailing / instead.
  77. (let ((len (length file)))
  78. (and (> len 0) (eq (aref file (1- len)) ?/))))
  79. (defvar company-files--completion-cache nil)
  80. (defun company-files--complete (prefix)
  81. (let* ((dir (file-name-directory prefix))
  82. (file (file-name-nondirectory prefix))
  83. (key (list file
  84. (expand-file-name dir)
  85. (nth 5 (file-attributes dir))))
  86. (completion-ignore-case read-file-name-completion-ignore-case))
  87. (unless (company-file--keys-match-p key (car company-files--completion-cache))
  88. (let* ((candidates (mapcar (lambda (f) (concat dir f))
  89. (company-files--directory-files dir file)))
  90. (directories (unless (file-remote-p dir)
  91. (cl-remove-if-not (lambda (f)
  92. (and (company-files--trailing-slash-p f)
  93. (not (file-remote-p f))
  94. (company-files--connected-p f)))
  95. candidates)))
  96. (children (and directories
  97. (cl-mapcan (lambda (d)
  98. (mapcar (lambda (c) (concat d c))
  99. (company-files--directory-files d "")))
  100. directories))))
  101. (setq company-files--completion-cache
  102. (cons key (append candidates children)))))
  103. (all-completions prefix
  104. (cdr company-files--completion-cache))))
  105. (defun company-file--keys-match-p (new old)
  106. (and (equal (cdr old) (cdr new))
  107. (string-prefix-p (car old) (car new))))
  108. ;;;###autoload
  109. (defun company-files (command &optional arg &rest ignored)
  110. "`company-mode' completion backend existing file names.
  111. Completions works for proper absolute and relative files paths.
  112. File paths with spaces are only supported inside strings."
  113. (interactive (list 'interactive))
  114. (cl-case command
  115. (interactive (company-begin-backend 'company-files))
  116. (prefix (company-files--grab-existing-name))
  117. (candidates (company-files--complete arg))
  118. (location (cons (dired-noselect
  119. (file-name-directory (directory-file-name arg))) 1))
  120. (post-completion (when (company-files--trailing-slash-p arg)
  121. (delete-char -1)))
  122. (sorted t)
  123. (no-cache t)))
  124. (provide 'company-files)
  125. ;;; company-files.el ends here