annotate lisp/emacs-lisp/crm.el @ 42308:c931d7a1b0df

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