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