crm.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. ;;; crm.el --- read multiple strings with completion
  2. ;; Copyright (C) 1985-1986, 1993-2012 Free Software Foundation, Inc.
  3. ;; Author: Sen Nagata <sen@eccosys.com>
  4. ;; Keywords: completion, minibuffer, multiple elements
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;; This code defines a function, `completing-read-multiple', which
  18. ;; provides the ability to read multiple strings in the minibuffer,
  19. ;; with completion.
  20. ;; By using this functionality, a user may specify multiple strings at
  21. ;; a single prompt, optionally using completion.
  22. ;; Multiple strings are specified by separating each of the strings
  23. ;; with a prespecified separator character. For example, if the
  24. ;; separator character is a comma, the strings 'alice', 'bob', and
  25. ;; 'eve' would be specified as 'alice,bob,eve'.
  26. ;; The default value for the separator character is the value of
  27. ;; `crm-default-separator' (comma). The separator character may be
  28. ;; changed by modifying the value of `crm-separator'.
  29. ;; Contiguous strings of non-separator-characters are referred to as
  30. ;; 'elements'. In the aforementioned example, the elements are:
  31. ;; 'alice', 'bob', and 'eve'.
  32. ;; Completion is available on a per-element basis. For example, if
  33. ;; the contents of the minibuffer are 'alice,bob,eve' and point is
  34. ;; between 'l' and 'i', pressing TAB operates on the element 'alice'.
  35. ;; For the moment, I have decided to not bind any special behavior to
  36. ;; the separator key. In the future, the separator key might be used
  37. ;; to provide completion in certain circumstances. One of the reasons
  38. ;; why this functionality is not yet provided is that it is unclear to
  39. ;; the author what the precise circumstances are, under which
  40. ;; separator-invoked completion should be provided.
  41. ;; Design note: `completing-read-multiple' is modeled after
  42. ;; `completing-read'. They should be similar -- it was intentional.
  43. ;; Some of this code started out as translation from C code in
  44. ;; src/minibuf.c to Emacs Lisp code. After this code was rewritten in Elisp
  45. ;; and made to operate on any field, this file was completely rewritten to
  46. ;; just reuse that code.
  47. ;; Thanks to Sen Nagata <sen@eccosys.com> for the original version of the
  48. ;; code, and sorry for throwing it all out. --Stef
  49. ;; Thanks to Richard Stallman for all of his help (many of the good
  50. ;; ideas in here are from him), Gerd Moellmann for his attention,
  51. ;; Stefan Monnier for responding with a code sample and comments very
  52. ;; early on, and Kai Grossjohann & Soren Dayton for valuable feedback.
  53. ;;; Questions and Thoughts:
  54. ;; -should `completing-read-multiple' allow a trailing separator in
  55. ;; a return value when REQUIRE-MATCH is t? if not, should beep when a user
  56. ;; tries to exit the minibuffer via RET?
  57. ;; -tip: use M-f and M-b for ease of navigation among elements.
  58. ;; - the difference between minibuffer-completion-table and
  59. ;; crm-completion-table is just crm--collection-fn. In most cases it
  60. ;; shouldn't make any difference. But if a non-CRM completion function
  61. ;; happens to be used, it will use minibuffer-completion-table and
  62. ;; crm--collection-fn will try to make it do "more or less the right
  63. ;; thing" by making it complete on the last element, which is about as
  64. ;; good as we can hope for right now.
  65. ;; I'm not sure if it's important or not. Maybe we could just throw away
  66. ;; crm-completion-table and crm--collection-fn, but there doesn't seem to
  67. ;; be a pressing need for it, and since Sen did bother to write it, we may
  68. ;; as well keep it, in case it helps.
  69. ;;; History:
  70. ;;
  71. ;; 2000-04-10:
  72. ;;
  73. ;; first revamped version
  74. ;;; Code:
  75. (defconst crm-default-separator ","
  76. "Default separator for `completing-read-multiple'.")
  77. (defvar crm-separator crm-default-separator
  78. "Separator used for separating strings in `completing-read-multiple'.
  79. It should be a single character string that doesn't appear in the list of
  80. completion candidates. Modify this value to make `completing-read-multiple'
  81. use a separator other than `crm-default-separator'.")
  82. (defvar crm-local-completion-map
  83. (let ((map (make-sparse-keymap)))
  84. (set-keymap-parent map minibuffer-local-completion-map)
  85. (define-key map [remap minibuffer-complete] #'crm-complete)
  86. (define-key map [remap minibuffer-complete-word] #'crm-complete-word)
  87. (define-key map [remap minibuffer-completion-help] #'crm-completion-help)
  88. map)
  89. "Local keymap for minibuffer multiple input with completion.
  90. Analog of `minibuffer-local-completion-map'.")
  91. (defvar crm-local-must-match-map
  92. (let ((map (make-sparse-keymap)))
  93. ;; We'd want to have multiple inheritance here.
  94. (set-keymap-parent map minibuffer-local-must-match-map)
  95. (define-key map [remap minibuffer-complete] #'crm-complete)
  96. (define-key map [remap minibuffer-complete-word] #'crm-complete-word)
  97. (define-key map [remap minibuffer-completion-help] #'crm-completion-help)
  98. (define-key map [remap minibuffer-complete-and-exit]
  99. #'crm-complete-and-exit)
  100. map)
  101. "Local keymap for minibuffer multiple input with exact match completion.
  102. Analog of `minibuffer-local-must-match-map' for crm.")
  103. (defvar crm-completion-table nil
  104. "An alist whose elements' cars are strings, or an obarray.
  105. This is a table used for completion by `completing-read-multiple' and its
  106. supporting functions.")
  107. ;; this function evolved from a posting by Stefan Monnier
  108. (defun crm--collection-fn (string predicate flag)
  109. "Function used by `completing-read-multiple' to compute completion values.
  110. The value of STRING is the string to be completed.
  111. The value of PREDICATE is a function to filter possible matches, or
  112. nil if none.
  113. The value of FLAG is used to specify the type of completion operation.
  114. A value of nil specifies `try-completion'. A value of t specifies
  115. `all-completions'. A value of lambda specifies a test for an exact match.
  116. For more information on STRING, PREDICATE, and FLAG, see the Elisp
  117. Reference sections on 'Programmed Completion' and 'Basic Completion
  118. Functions'."
  119. (let ((beg 0))
  120. (while (string-match crm-separator string beg)
  121. (setq beg (match-end 0)))
  122. (completion-table-with-context (substring string 0 beg)
  123. crm-completion-table
  124. (substring string beg)
  125. predicate
  126. flag)))
  127. (defun crm--select-current-element ()
  128. "Parse the minibuffer to find the current element.
  129. Place an overlay on the element, with a `field' property, and return it."
  130. (let* ((bob (minibuffer-prompt-end))
  131. (start (save-excursion
  132. (if (re-search-backward crm-separator bob t)
  133. (match-end 0)
  134. bob)))
  135. (end (save-excursion
  136. (if (re-search-forward crm-separator nil t)
  137. (match-beginning 0)
  138. (point-max))))
  139. (ol (make-overlay start end nil nil t)))
  140. (overlay-put ol 'field (make-symbol "crm"))
  141. ol))
  142. (defun crm-completion-help ()
  143. "Display a list of possible completions of the current minibuffer element."
  144. (interactive)
  145. (let ((ol (crm--select-current-element)))
  146. (unwind-protect
  147. (minibuffer-completion-help)
  148. (delete-overlay ol)))
  149. nil)
  150. (defun crm-complete ()
  151. "Complete the current element.
  152. If no characters can be completed, display a list of possible completions.
  153. Return t if the current element is now a valid match; otherwise return nil."
  154. (interactive)
  155. (let ((ol (crm--select-current-element)))
  156. (unwind-protect
  157. (minibuffer-complete)
  158. (delete-overlay ol))))
  159. (defun crm-complete-word ()
  160. "Complete the current element at most a single word.
  161. Like `minibuffer-complete-word' but for `completing-read-multiple'."
  162. (interactive)
  163. (let ((ol (crm--select-current-element)))
  164. (unwind-protect
  165. (minibuffer-complete-word)
  166. (delete-overlay ol))))
  167. (defun crm-complete-and-exit ()
  168. "If all of the minibuffer elements are valid completions then exit.
  169. All elements in the minibuffer must match. If there is a mismatch, move point
  170. to the location of mismatch and do not exit.
  171. This function is modeled after `minibuffer-complete-and-exit'."
  172. (interactive)
  173. (let ((doexit t))
  174. (goto-char (minibuffer-prompt-end))
  175. (while
  176. (and doexit
  177. (let ((ol (crm--select-current-element)))
  178. (goto-char (overlay-end ol))
  179. (unwind-protect
  180. (catch 'exit
  181. (minibuffer-complete-and-exit)
  182. ;; This did not throw `exit', so there was a problem.
  183. (setq doexit nil))
  184. (goto-char (overlay-end ol))
  185. (delete-overlay ol))
  186. (not (eobp))))
  187. ;; Skip to the next element.
  188. (forward-char 1))
  189. (if doexit (exit-minibuffer))))
  190. (defun crm--choose-completion-string (choice buffer base-position
  191. &rest ignored)
  192. "Completion string chooser for `completing-read-multiple'.
  193. This is called from `choose-completion-string-functions'.
  194. It replaces the string that is currently being completed, without
  195. exiting the minibuffer."
  196. (let ((completion-no-auto-exit t)
  197. (choose-completion-string-functions nil))
  198. (choose-completion-string choice buffer base-position)
  199. t))
  200. ;; superemulates behavior of completing_read in src/minibuf.c
  201. ;;;###autoload
  202. (defun completing-read-multiple
  203. (prompt table &optional predicate require-match initial-input
  204. hist def inherit-input-method)
  205. "Read multiple strings in the minibuffer, with completion.
  206. By using this functionality, a user may specify multiple strings at a
  207. single prompt, optionally using completion.
  208. Multiple strings are specified by separating each of the strings with
  209. a prespecified separator character. For example, if the separator
  210. character is a comma, the strings 'alice', 'bob', and 'eve' would be
  211. specified as 'alice,bob,eve'.
  212. The default value for the separator character is the value of
  213. `crm-default-separator' (comma). The separator character may be
  214. changed by modifying the value of `crm-separator'.
  215. Contiguous strings of non-separator-characters are referred to as
  216. 'elements'. In the aforementioned example, the elements are: 'alice',
  217. 'bob', and 'eve'.
  218. Completion is available on a per-element basis. For example, if the
  219. contents of the minibuffer are 'alice,bob,eve' and point is between
  220. 'l' and 'i', pressing TAB operates on the element 'alice'.
  221. The return value of this function is a list of the read strings.
  222. See the documentation for `completing-read' for details on the arguments:
  223. PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HIST, DEF, and
  224. INHERIT-INPUT-METHOD."
  225. (unwind-protect
  226. (progn
  227. (add-hook 'choose-completion-string-functions
  228. 'crm--choose-completion-string)
  229. (let* ((minibuffer-completion-table #'crm--collection-fn)
  230. (minibuffer-completion-predicate predicate)
  231. ;; see completing_read in src/minibuf.c
  232. (minibuffer-completion-confirm
  233. (unless (eq require-match t) require-match))
  234. (crm-completion-table table)
  235. (map (if require-match
  236. crm-local-must-match-map
  237. crm-local-completion-map))
  238. ;; If the user enters empty input, read-from-minibuffer returns
  239. ;; the empty string, not DEF.
  240. (input (read-from-minibuffer
  241. prompt initial-input map
  242. nil hist def inherit-input-method)))
  243. (and def (string-equal input "") (setq input def))
  244. (split-string input crm-separator)))
  245. (remove-hook 'choose-completion-string-functions
  246. 'crm--choose-completion-string)))
  247. (define-obsolete-function-alias 'crm-minibuffer-complete 'crm-complete "23.1")
  248. (define-obsolete-function-alias
  249. 'crm-minibuffer-completion-help 'crm-completion-help "23.1")
  250. (define-obsolete-function-alias
  251. 'crm-minibuffer-complete-and-exit 'crm-complete-and-exit "23.1")
  252. ;; testing and debugging
  253. ;; (defun crm-init-test-environ ()
  254. ;; "Set up some variables for testing."
  255. ;; (interactive)
  256. ;; (setq my-prompt "Prompt: ")
  257. ;; (setq my-table
  258. ;; '(("hi") ("there") ("man") ("may") ("mouth") ("ma")
  259. ;; ("a") ("ab") ("abc") ("abd") ("abf") ("zab") ("acb")
  260. ;; ("da") ("dab") ("dabc") ("dabd") ("dabf") ("dzab") ("dacb")
  261. ;; ("fda") ("fdab") ("fdabc") ("fdabd") ("fdabf") ("fdzab") ("fdacb")
  262. ;; ("gda") ("gdab") ("gdabc") ("gdabd") ("gdabf") ("gdzab") ("gdacb")
  263. ;; ))
  264. ;; (setq my-separator ","))
  265. ;(completing-read-multiple my-prompt my-table)
  266. ;(completing-read-multiple my-prompt my-table nil t)
  267. ;(completing-read-multiple my-prompt my-table nil "match")
  268. ;(completing-read my-prompt my-table nil t)
  269. ;(completing-read my-prompt my-table nil "match")
  270. (provide 'crm)
  271. ;;; crm.el ends here