28710
|
1 ;;; crm.el --- read multiple strings with completion
|
|
2
|
|
3 ;; Copyright (C) 1985, 1986, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000
|
|
4 ;; Free Software Foundation, Inc.
|
|
5
|
|
6 ;; Author: Sen Nagata <sen@eccosys.com>
|
|
7 ;; Keywords: completion, minibuffer, multiple elements
|
|
8
|
|
9 ;; This file is part of GNU Emacs.
|
|
10
|
|
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
12 ;; it under the terms of the GNU General Public License as published by
|
|
13 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
14 ;; any later version.
|
|
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
|
|
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
24 ;; Boston, MA 02111-1307, USA.
|
|
25
|
|
26 ;;; Commentary:
|
|
27
|
|
28 ;; This code defines a function, `completing-read-multiple', which
|
|
29 ;; provides the ability to read multiple strings in the minibuffer,
|
|
30 ;; with completion.
|
|
31
|
|
32 ;; By using this functionality, a user may specify multiple strings at
|
|
33 ;; a single prompt, optionally using completion.
|
|
34
|
|
35 ;; Multiple strings are specified by separating each of the strings
|
|
36 ;; with a prespecified separator character. For example, if the
|
|
37 ;; separator character is a comma, the strings 'alice', 'bob', and
|
|
38 ;; 'eve' would be specified as 'alice,bob,eve'.
|
|
39
|
|
40 ;; The default value for the separator character is the value of
|
|
41 ;; `crm-default-separator' (comma). The separator character may be
|
|
42 ;; changed by modifying the value of `crm-separator'.
|
|
43
|
|
44 ;; Continguous strings of non-separator-characters are referred to as
|
|
45 ;; 'elements'. In the aforementioned example, the elements are:
|
|
46 ;; 'alice', 'bob', and 'eve'.
|
|
47
|
|
48 ;; Completion is available on a per-element basis. For example, if
|
|
49 ;; the contents of the minibuffer are 'alice,bob,eve' and point is
|
|
50 ;; between 'l' and 'i', pressing TAB operates on the element 'alice'.
|
|
51
|
|
52 ;; For the moment, I have decided to not bind any special behavior to
|
|
53 ;; the separator key. In the future, the separator key might be used
|
|
54 ;; to provide completion in certain circumstances. One of the reasons
|
|
55 ;; why this functionality is not yet provided is that it is unclear to
|
|
56 ;; the author what the precise circumstances are, under which
|
|
57 ;; separator-invoked completion should be provided.
|
|
58
|
|
59 ;; Design note: `completing-read-multiple' is modeled after
|
|
60 ;; `completing-read'. They should be similar -- it was intentional.
|
|
61
|
|
62 ;; Some of this code started out as translation from C code in
|
|
63 ;; src/minibuf.c to Emacs Lisp code.
|
|
64
|
|
65 ;; Thanks to Richard Stallman for all of his help (many of the good
|
|
66 ;; ideas in here are from him), Gerd Moellmann for his attention,
|
|
67 ;; Stefan Monnier for responding with a code sample and comments very
|
|
68 ;; early on, and Kai Grossjohann & Soren Dayton for valuable feedback.
|
|
69
|
|
70 ;;; Questions and Thoughts:
|
|
71
|
|
72 ;; -the author has gone through a number of test-and-fix cycles w/
|
|
73 ;; this code, so it should be usable. please let me know if you find
|
|
74 ;; any problems.
|
|
75
|
|
76 ;; -should `completing-read-multiple' allow a trailing separator in
|
|
77 ;; a return value when REQUIRE-MATCH is t? if not, should beep when a user
|
|
78 ;; tries to exit the minibuffer via RET?
|
|
79
|
|
80 ;; -TODO: possibly make return values from `crm-do-completion' into constants
|
|
81
|
|
82 ;; -TODO: find out whether there is an appropriate way to distinguish between
|
|
83 ;; functions intended for internal use and those that aren't.
|
|
84
|
|
85 ;; -tip: use M-f and M-b for ease of navigation among elements.
|
|
86
|
|
87 ;;; History:
|
|
88 ;;
|
|
89 ;; 2000-04-10:
|
|
90 ;;
|
|
91 ;; first revamped version
|
|
92
|
|
93 ;;; Code:
|
|
94 (defconst crm-default-separator ","
|
|
95 "Default separator for `completing-read-multiple'.")
|
|
96
|
|
97 (defvar crm-separator crm-default-separator
|
|
98 "Separator used for separating strings in `completing-read-multiple'.
|
|
99 It should be a single character string that doesn't appear in the list of
|
|
100 completion candidates. Modify this value to make `completing-read-multiple'
|
|
101 use a separator other than `crm-default-separator'.")
|
|
102
|
|
103 ;; actual filling in of these maps occurs below via `crm-init-keymaps'
|
|
104 (defvar crm-local-completion-map nil
|
|
105 "Local keymap for minibuffer multiple input with completion.
|
|
106 Analog of `minibuffer-local-completion-map'.")
|
|
107
|
|
108 (defvar crm-local-must-match-map nil
|
|
109 "Local keymap for minibuffer multiple input with exact match completion.
|
|
110 Analog of `minibuffer-local-must-match-map' for crm.")
|
|
111
|
|
112 ;; this is supposed to be analogous to last_exact_completion in src/minibuf.c
|
|
113 (defvar crm-last-exact-completion nil
|
|
114 "Completion string if last attempt reported \"Complete, but not unique\".")
|
|
115
|
|
116 (defvar crm-left-of-element nil
|
|
117 "String to the left of the current element.")
|
|
118
|
|
119 (defvar crm-current-element nil
|
|
120 "The current element.")
|
|
121
|
|
122 (defvar crm-right-of-element nil
|
|
123 "String to the right of the current element.")
|
|
124
|
|
125 (defvar crm-beginning-of-element nil
|
|
126 "Buffer position representing the beginning of the current element.")
|
|
127
|
|
128 (defvar crm-end-of-element nil
|
|
129 "Buffer position representing the end of the current element.")
|
|
130
|
|
131 ;; emulates temp_echo_area_glyphs from src/minibuf.c
|
|
132 (defun crm-temp-echo-area-glyphs (message-string)
|
|
133 "Temporarily display MESSAGE-STRING in echo area.
|
|
134 After user-input or 2 seconds, erase the displayed string."
|
|
135 (save-excursion
|
|
136 (goto-char (point-max))
|
|
137 (insert message-string)
|
|
138 (sit-for 2)
|
|
139 (backward-char (length message-string))
|
|
140 (delete-char (length message-string))))
|
|
141
|
|
142 ;; this function evolved from a posting by Stefan Monnier
|
|
143 (defun crm-collection-fn (string predicate flag)
|
|
144 "Function used by `completing-read-multiple' to compute completion values.
|
|
145 The value of STRING is the string to be completed.
|
|
146
|
|
147 The value of PREDICATE is a function to filter possible matches, or
|
|
148 nil if none.
|
|
149
|
|
150 The value of FLAG is used to specify the type of completion operation.
|
|
151 A value of nil specifies `try-completion'. A value of t specifies
|
|
152 `all-completions'. A value of lambda specifes a test for an exact match.
|
|
153
|
|
154 For more information on STRING, PREDICATE, and FLAG, see the Elisp
|
|
155 Reference sections on 'Programmed Completion' and 'Basic Completion
|
|
156 Functions'."
|
|
157 (let ((lead ""))
|
|
158 (when (string-match (concat ".*" crm-separator) string)
|
|
159 (setq lead (substring string 0 (match-end 0)))
|
|
160 (setq string (substring string (match-end 0))))
|
|
161 (if (eq flag 'lambda)
|
|
162 ;; return t for exact match, nil otherwise
|
|
163 (let ((result (try-completion string table predicate)))
|
|
164 (if (stringp result)
|
|
165 nil
|
|
166 (if result
|
|
167 t
|
|
168 nil))))
|
|
169 (if flag
|
|
170 ;; called via (all-completions string 'crm-completion-fn predicate)?
|
|
171 (all-completions string table predicate)
|
|
172 ;; called via (try-completion string 'crm-completion-fn predicate)?
|
|
173 (let ((result (try-completion string table predicate)))
|
|
174 (if (stringp result)
|
|
175 (concat lead result)
|
|
176 result)))))
|
|
177
|
|
178 (defun crm-find-current-element ()
|
|
179 "Parse the minibuffer to find the current element.
|
|
180 If no element can be found, return nil.
|
|
181
|
|
182 If an element is found, bind:
|
|
183
|
|
184 -the variable `crm-current-element' to the current element,
|
|
185
|
|
186 -the variables `crm-left-of-element' and `crm-right-of-element' to
|
|
187 the strings to the left and right of the current element,
|
|
188 respectively, and
|
|
189
|
|
190 -the variables `crm-beginning-of-element' and `crm-end-of-element' to
|
|
191 the buffer positions of the beginning and end of the current element
|
|
192 respectively,
|
|
193
|
|
194 and return t."
|
|
195 (let* ((minibuffer-string (buffer-string))
|
|
196 (end-index (or (string-match "," minibuffer-string (1- (point)))
|
|
197 (1- (point-max))))
|
|
198 (target-string (substring minibuffer-string 0 end-index))
|
|
199 (index (or (string-match
|
|
200 (concat crm-separator "\\([^" crm-separator "]*\\)$")
|
|
201 target-string)
|
|
202 (string-match
|
|
203 (concat "^\\([^" crm-separator "]*\\)$")
|
|
204 target-string))))
|
|
205 (if (not (numberp index))
|
|
206 ;; no candidate found
|
|
207 nil
|
|
208 (progn
|
|
209 ;;
|
|
210 (setq crm-beginning-of-element (match-beginning 1))
|
|
211 (setq crm-end-of-element end-index)
|
|
212 ;; string to the left of the current element
|
|
213 (setq crm-left-of-element (substring target-string 0 (match-beginning 1)))
|
|
214 ;; the current element
|
|
215 (setq crm-current-element (match-string 1 target-string))
|
|
216 ;; string to the right of the current element
|
|
217 (setq crm-right-of-element (substring minibuffer-string end-index))
|
|
218 t))))
|
|
219
|
|
220 (defun crm-test-completion (candidate)
|
|
221 "Return t if CANDIDATE is an exact match for a valid completion."
|
|
222 (let ((completions
|
|
223 ;; TODO: verify whether the arguments are appropriate
|
|
224 (all-completions candidate table minibuffer-completion-predicate)))
|
|
225 (if (member candidate completions)
|
|
226 t
|
|
227 nil)))
|
|
228
|
|
229 (defun crm-minibuffer-completion-help ()
|
|
230 "Display a list of possible completions of the current minibuffer element."
|
|
231 (interactive)
|
|
232 (message "Making completion list...")
|
|
233 (if (not (crm-find-current-element))
|
|
234 nil
|
|
235 (let ((completions (all-completions crm-current-element
|
|
236 minibuffer-completion-table
|
|
237 minibuffer-completion-predicate)))
|
|
238 (message nil)
|
|
239 (if (null completions)
|
|
240 (crm-temp-echo-area-glyphs " [No completions]")
|
|
241 (with-output-to-temp-buffer "*Completions*"
|
|
242 (display-completion-list (sort completions 'string-lessp))))))
|
|
243 nil)
|
|
244
|
|
245 (defun crm-do-completion ()
|
|
246 "This is the internal completion engine.
|
|
247 This function updates the text in the minibuffer
|
|
248 to complete the current string, and returns a number between 0 and 6.
|
|
249 The meanings of the return values are:
|
|
250
|
|
251 0 - the string has no possible completion
|
|
252 1 - the string is already a valid and unique match
|
|
253 2 - not used
|
|
254 3 - the string is already a valid match (but longer matches exist too)
|
|
255 4 - the string was completed to a valid match
|
|
256 5 - some completion has been done, but the result is not a match
|
|
257 6 - no completion was done, and the string is not an exact match"
|
|
258
|
|
259 (if (not (crm-find-current-element))
|
|
260 nil
|
|
261 (let (last completion completedp)
|
|
262 (setq completion
|
|
263 (try-completion crm-current-element
|
|
264 minibuffer-completion-table
|
|
265 minibuffer-completion-predicate))
|
|
266 (setq last crm-last-exact-completion)
|
|
267 (setq crm-last-exact-completion nil)
|
|
268
|
|
269 (catch 'crm-exit
|
|
270
|
|
271 (if (null completion) ; no possible completion
|
|
272 (progn
|
|
273 (crm-temp-echo-area-glyphs " [No match]")
|
|
274 (throw 'crm-exit 0)))
|
|
275
|
|
276 (if (eq completion t) ; was already an exact and unique completion
|
|
277 (throw 'crm-exit 1))
|
|
278
|
|
279 (setq completedp
|
|
280 (null (string-equal completion crm-current-element)))
|
|
281
|
|
282 (if completedp
|
|
283 (progn
|
|
284 (erase-buffer)
|
|
285 (insert crm-left-of-element completion)
|
|
286 ;; (if crm-complete-up-to-point
|
|
287 ;; (insert crm-separator))
|
|
288 (insert crm-right-of-element)
|
|
289 (backward-char (length crm-right-of-element))
|
|
290 ;; TODO: is this correct?
|
|
291 (setq crm-current-element completion)))
|
|
292
|
|
293 (if (null (crm-test-completion crm-current-element))
|
|
294 (progn
|
|
295 (if completedp ; some completion happened
|
|
296 (throw 'crm-exit 5)
|
|
297 (if completion-auto-help
|
|
298 (crm-minibuffer-completion-help)
|
|
299 (crm-temp-echo-area-glyphs " [Next char not unique]")))
|
|
300 (throw 'crm-exit 6))
|
|
301 (if completedp
|
|
302 (throw 'crm-exit 4)))
|
|
303
|
|
304 (setq crm-last-exact-completion completion)
|
|
305 (if (not (null last))
|
|
306 (progn
|
|
307 (if (not (null (equal crm-current-element last)))
|
|
308 (crm-minibuffer-completion-help))))
|
|
309
|
|
310 ;; returning -- was already an exact completion
|
|
311 (throw 'crm-exit 3)))))
|
|
312
|
|
313 (defun crm-minibuffer-complete ()
|
|
314 "Complete the current element.
|
|
315 If no characters can be completed, display a list of possible completions.
|
|
316
|
|
317 Return t if the current element is now a valid match; otherwise return nil."
|
|
318 (interactive)
|
|
319 ;; take care of scrolling if necessary -- completely cribbed from minibuf.c
|
|
320 (if (not (eq last-command this-command))
|
|
321 ;; ok?
|
|
322 (setq minibuffer-scroll-window nil))
|
|
323 (let ((window minibuffer-scroll-window))
|
|
324 (if (and (not (null window))
|
|
325 ;; ok?
|
|
326 (not (null (window-buffer window))))
|
|
327 (let (tem)
|
|
328 (set-buffer (window-buffer window))
|
|
329 ;; ok?
|
|
330 (setq tem (pos-visible-in-window-p (point-max) window))
|
|
331 (if (not (null tem))
|
|
332 ;; ok?
|
|
333 (set-window-start window (point-min) nil)
|
|
334 (scroll-other-window nil))
|
|
335 ;; reaching here means exiting the function w/ return value of nil
|
|
336 nil)
|
|
337
|
|
338 (let* (
|
|
339 ;(crm-end-of-element nil)
|
|
340 (result (crm-do-completion)))
|
|
341 (cond
|
|
342 ((eq 0 result)
|
|
343 nil)
|
|
344 ((eq 1 result)
|
|
345 ;; adapted from Emacs 21
|
|
346 (if (not (eq (point) crm-end-of-element))
|
|
347 (goto-char (+ 1 crm-end-of-element)))
|
|
348 (crm-temp-echo-area-glyphs " [Sole completion]")
|
|
349 t)
|
|
350 ((eq 3 result)
|
|
351 ;; adapted from Emacs 21
|
|
352 (if (not (eq (point) crm-end-of-element))
|
|
353 (goto-char (+ 1 crm-end-of-element)))
|
|
354 (crm-temp-echo-area-glyphs " [Complete, but not unique]")
|
|
355 t))))))
|
|
356
|
|
357 ;; i love traffic lights...but only when they're green
|
|
358 (defun crm-find-longest-completable-substring (string)
|
|
359 "Determine the longest completable (left-anchored) substring of STRING.
|
|
360 The description \"left-anchored\" means the positions of the characters
|
|
361 in the substring must be the same as those of the corresponding characters
|
|
362 in STRING. Anchoring is what `^' does in a regular expression.
|
|
363
|
|
364 The table and predicate used for completion are
|
|
365 `minibuffer-completion-table' and `minibuffer-completion-predicate',
|
|
366 respectively.
|
|
367
|
|
368 A non-nil return value means that there is some substring which is
|
|
369 completable. A return value of t means that STRING itself is
|
|
370 completable. If a string value is returned it is the longest
|
|
371 completable proper substring of STRING. If nil is returned, STRING
|
|
372 does not have any non-empty completable substrings.
|
|
373
|
|
374 Remember: \"left-anchored\" substring"
|
|
375 (let* ((length-of-string (length string))
|
|
376 (index length-of-string)
|
|
377 (done (if (> length-of-string 0)
|
|
378 nil
|
|
379 t))
|
|
380 (first t) ; ugh, special handling for first time through...
|
|
381 goal-string
|
|
382 result)
|
|
383 ;; loop through left-anchored substrings in order of descending length,
|
|
384 ;; find the first substring that is completable
|
|
385 (while (not done)
|
|
386 (setq result (try-completion (substring string 0 index)
|
|
387 minibuffer-completion-table
|
|
388 minibuffer-completion-predicate))
|
|
389 (if result
|
|
390 ;; found completable substring
|
|
391 (progn
|
|
392 (setq done t)
|
|
393 (if (and (eq result t) first)
|
|
394 ;; exactly matching string first time through
|
|
395 (setq goal-string t)
|
|
396 ;; fully-completed proper substring
|
|
397 (setq goal-string (substring string 0 index)))))
|
|
398 (setq index (1- index))
|
|
399 (if first
|
|
400 (setq first nil))
|
|
401 (if (<= index 0)
|
|
402 (setq done t)))
|
|
403 ;; possible values include: t, nil, some string
|
|
404 goal-string))
|
|
405
|
|
406 ;; TODO: decide whether trailing separator is allowed. current
|
|
407 ;; implementation appears to allow it
|
|
408 (defun crm-strings-completed-p (separated-string)
|
|
409 "Verify that strings in SEPARATED-STRING are completed strings.
|
|
410 A return value of t means that all strings were verified. A number is
|
|
411 returned if verification was unsuccessful. This number represents the
|
|
412 position in SEPARATED-STRING up to where completion was successful."
|
|
413 (let ((strings (split-string separated-string crm-separator))
|
|
414 ;; buffers start at 1, not 0
|
|
415 (current-position 1)
|
|
416 current-string
|
|
417 result
|
|
418 done)
|
|
419 (while (and strings (not done))
|
|
420 (setq current-string (car strings)
|
|
421 result (try-completion current-string
|
|
422 minibuffer-completion-table
|
|
423 minibuffer-completion-predicate))
|
|
424 (if (eq result t)
|
|
425 (setq strings (cdr strings)
|
|
426 current-position (+ current-position
|
|
427 (length current-string)
|
|
428 ;; automatically adding 1 for separator
|
|
429 ;; character
|
|
430 1))
|
|
431 ;; still one more case of a match
|
|
432 (if (stringp result)
|
|
433 (let ((string-list
|
|
434 (all-completions result
|
|
435 minibuffer-completion-table
|
|
436 minibuffer-completion-predicate)))
|
|
437 (if (member result string-list)
|
|
438 ;; ho ho, code duplication...
|
|
439 (setq strings (cdr strings)
|
|
440 current-position (+ current-position
|
|
441 (length current-string)
|
|
442 1))
|
|
443 (progn
|
|
444 (setq done t)
|
|
445 ;; current-string is a partially-completed string
|
|
446 (setq current-position (+ current-position
|
|
447 (length current-string))))))
|
|
448 ;; current-string cannot be completed
|
|
449 (let ((completable-substring
|
|
450 (crm-find-longest-completable-substring current-string)))
|
|
451 (setq done t)
|
|
452 (setq current-position (+ current-position
|
|
453 (length completable-substring)))))))
|
|
454 ;; return our result
|
|
455 (if (null strings)
|
|
456 t
|
|
457 current-position)))
|
|
458
|
|
459 ;; try to complete candidate, then check all separated strings. move
|
|
460 ;; point to problem position if checking fails for some string. if
|
|
461 ;; checking succeeds for all strings, exit.
|
|
462 (defun crm-minibuffer-complete-and-exit ()
|
|
463 "If all of the minibuffer elements are valid completions then exit.
|
|
464 All elements in the minibuffer must match. If there is a mismatch, move point
|
|
465 to the location of mismatch and do not exit.
|
|
466
|
|
467 This function is modeled after `minibuffer_complete_and_exit' in src/minibuf.c"
|
|
468 (interactive)
|
|
469
|
|
470 (if (not (crm-find-current-element))
|
|
471 nil
|
|
472 (let (result)
|
|
473
|
|
474 (setq result
|
|
475 (catch 'crm-exit
|
|
476
|
|
477 (if (eq (point-min) (point-max))
|
|
478 (throw 'crm-exit t))
|
|
479
|
|
480 ;; TODO: this test is suspect?
|
|
481 (if (not (null (crm-test-completion crm-current-element)))
|
|
482 (throw 'crm-exit "check"))
|
|
483
|
|
484 ;; TODO: determine how to detect errors
|
|
485 (let ((result (crm-do-completion)))
|
|
486
|
|
487 (cond
|
|
488 ((or (eq 1 result)
|
|
489 (eq 3 result))
|
|
490 (throw 'crm-exit "check"))
|
|
491 ((eq 4 result)
|
|
492 (if (not (null minibuffer-completion-confirm))
|
|
493 (progn
|
|
494 (crm-temp-echo-area-glyphs " [Confirm]")
|
|
495 nil)
|
|
496 (throw 'crm-exit "check")))
|
|
497 (nil)))))
|
|
498
|
|
499 (if (null result)
|
|
500 nil
|
|
501 (if (equal result "check")
|
|
502 (let ((check-strings
|
|
503 (crm-strings-completed-p (buffer-string))))
|
|
504 ;; check all of minibuffer
|
|
505 (if (eq check-strings t)
|
|
506 (throw 'exit nil)
|
|
507 (if (numberp check-strings)
|
|
508 (progn
|
|
509 (goto-char check-strings)
|
|
510 (crm-temp-echo-area-glyphs " [An element did not match]"))
|
|
511 (message "Unexpected error"))))
|
|
512 (if (eq result t)
|
|
513 (throw 'exit nil)
|
|
514 (message "Unexpected error")))))))
|
|
515
|
|
516 (defun crm-init-keymaps ()
|
|
517 "Initialize the keymaps used by `completing-read-multiple'.
|
|
518 Two keymaps are used depending on the value of the REQUIRE-MATCH
|
|
519 argument of the function `completing-read-multiple'.
|
|
520
|
|
521 If REQUIRE-MATCH is nil, the keymap `crm-local-completion-map' is used.
|
|
522 This keymap inherits from the keymap named `minibuffer-local-completion-map'.
|
|
523 The only difference is that TAB is bound to `crm-minibuffer-complete' in
|
|
524 the inheriting keymap.
|
|
525
|
|
526 If REQUIRE-MACTH is non-nil, the keymap `crm-local-must-match-map' is used.
|
|
527 This keymap inherits from the keymap named `minibuffer-local-must-match-map'.
|
|
528 The inheriting keymap binds RET to `crm-minibuffer-complete-and-exit'
|
|
529 and TAB to `crm-minibuffer-complete'."
|
|
530 (unless crm-local-completion-map
|
|
531 (setq crm-local-completion-map (make-sparse-keymap))
|
|
532 (set-keymap-parent crm-local-completion-map
|
|
533 minibuffer-local-completion-map)
|
|
534 ;; key definitions
|
|
535 (define-key crm-local-completion-map
|
|
536 (kbd "TAB")
|
|
537 (function crm-minibuffer-complete)))
|
|
538
|
|
539 (unless crm-local-must-match-map
|
|
540 (setq crm-local-must-match-map (make-sparse-keymap))
|
|
541 (set-keymap-parent crm-local-must-match-map
|
|
542 minibuffer-local-must-match-map)
|
|
543 ;; key definitions
|
|
544 (define-key crm-local-must-match-map
|
|
545 (kbd "RET")
|
|
546 (function crm-minibuffer-complete-and-exit))
|
|
547 (define-key crm-local-must-match-map
|
|
548 (kbd "TAB")
|
|
549 (function crm-minibuffer-complete))))
|
|
550
|
|
551 (crm-init-keymaps)
|
|
552
|
|
553 ;; superemulates behavior of completing_read in src/minibuf.c
|
|
554 ;;;###autoload
|
|
555 (defun completing-read-multiple
|
|
556 (prompt table &optional predicate require-match initial-input
|
|
557 hist def inherit-input-method)
|
|
558 "Read multiple strings in the minibuffer, with completion.
|
|
559 By using this functionality, a user may specify multiple strings at a
|
|
560 single prompt, optionally using completion.
|
|
561
|
|
562 Multiple strings are specified by separating each of the strings with
|
|
563 a prespecified separator character. For example, if the separator
|
|
564 character is a comma, the strings 'alice', 'bob', and 'eve' would be
|
|
565 specified as 'alice,bob,eve'.
|
|
566
|
|
567 The default value for the separator character is the value of
|
|
568 `crm-default-separator' (comma). The separator character may be
|
|
569 changed by modifying the value of `crm-separator'.
|
|
570
|
|
571 Continguous strings of non-separator-characters are referred to as
|
|
572 'elements'. In the aforementioned example, the elements are: 'alice',
|
|
573 'bob', and 'eve'.
|
|
574
|
|
575 Completion is available on a per-element basis. For example, if the
|
|
576 contents of the minibuffer are 'alice,bob,eve' and point is between
|
|
577 'l' and 'i', pressing TAB operates on the element 'alice'.
|
|
578
|
|
579 The return value of this function is a list of the read strings.
|
|
580
|
|
581 See the documentation for `completing-read' for details on the arguments:
|
|
582 PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HIST, DEF, and
|
|
583 INHERIT-INPUT-METHOD."
|
|
584 (let ((minibuffer-completion-table (function crm-collection-fn))
|
|
585 (minibuffer-completion-predicate predicate)
|
|
586 ;; see completing_read in src/minibuf.c
|
|
587 (minibuffer-completion-confirm (if (eq require-match t)
|
|
588 nil
|
|
589 t))
|
|
590 crm-last-exact-completion
|
|
591 crm-current-element
|
|
592 crm-left-of-element
|
|
593 crm-right-of-element
|
|
594 crm-beginning-of-element
|
|
595 crm-end-of-element
|
|
596 map)
|
|
597 (if require-match
|
|
598 ;; use `crm-local-must-match-map'
|
|
599 (setq map crm-local-must-match-map)
|
|
600 ;; use `minibuffer-local-completion-map'
|
|
601 (setq map minibuffer-local-completion-map))
|
|
602 (split-string (read-from-minibuffer
|
|
603 prompt initial-input map
|
|
604 nil hist def inherit-input-method)
|
|
605 crm-separator)))
|
|
606
|
|
607 ;; testing and debugging
|
|
608 ;;; (defun crm-init-test-environ ()
|
|
609 ;;; "Set up some variables for testing."
|
|
610 ;;; (interactive)
|
|
611 ;;; (setq my-prompt "Prompt: ")
|
|
612 ;;; (setq my-table
|
|
613 ;;; '(("hi") ("there") ("man") ("may") ("mouth") ("ma")
|
|
614 ;;; ("a") ("ab") ("abc") ("abd") ("abf") ("zab") ("acb")
|
|
615 ;;; ("da") ("dab") ("dabc") ("dabd") ("dabf") ("dzab") ("dacb")
|
|
616 ;;; ("fda") ("fdab") ("fdabc") ("fdabd") ("fdabf") ("fdzab") ("fdacb")
|
|
617 ;;; ("gda") ("gdab") ("gdabc") ("gdabd") ("gdabf") ("gdzab") ("gdacb")
|
|
618 ;;; ))
|
|
619 ;;; (setq my-separator ","))
|
|
620
|
|
621 ;(completing-read-multiple my-prompt my-table)
|
|
622 ;(completing-read-multiple my-prompt my-table nil t)
|
|
623 ;(completing-read-multiple my-prompt my-table nil "match")
|
|
624 ;(completing-read my-prompt my-table nil t)
|
|
625 ;(completing-read my-prompt my-table nil "match")
|
|
626
|
|
627 (provide 'crm)
|
|
628
|
|
629 ;;; crm.el ends here
|