diff options
| author | Stefan Monnier | 2008-04-14 19:54:30 +0000 |
|---|---|---|
| committer | Stefan Monnier | 2008-04-14 19:54:30 +0000 |
| commit | 66787d5102721eb738237d9a44e37eb0fe55c50f (patch) | |
| tree | cf2fe1a2e9add4037c12e2b03fc589ec6dc77726 | |
| parent | 9ec3aaef2b79e905f97a57fdb17a401e62b2d0fe (diff) | |
| download | emacs-66787d5102721eb738237d9a44e37eb0fe55c50f.tar.gz emacs-66787d5102721eb738237d9a44e37eb0fe55c50f.zip | |
Complete rewrite.
| -rw-r--r-- | lisp/ChangeLog | 2 | ||||
| -rw-r--r-- | lisp/emacs-lisp/crm.el | 543 |
2 files changed, 108 insertions, 437 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index a43325c7b5b..c6102575cf6 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,5 +1,7 @@ | |||
| 1 | 2008-04-14 Stefan Monnier <monnier@iro.umontreal.ca> | 1 | 2008-04-14 Stefan Monnier <monnier@iro.umontreal.ca> |
| 2 | 2 | ||
| 3 | * emacs-lisp/crm.el: Complete rewrite. | ||
| 4 | |||
| 3 | * tmm.el (tmm-completion-delete-prompt): Don't hardcode point-min==1. | 5 | * tmm.el (tmm-completion-delete-prompt): Don't hardcode point-min==1. |
| 4 | (tmm-add-prompt): Make sure completion-setup-hook is preserved even in | 6 | (tmm-add-prompt): Make sure completion-setup-hook is preserved even in |
| 5 | case of an error in display-completion-list. | 7 | case of an error in display-completion-list. |
diff --git a/lisp/emacs-lisp/crm.el b/lisp/emacs-lisp/crm.el index 4b0dcb32d58..85372771853 100644 --- a/lisp/emacs-lisp/crm.el +++ b/lisp/emacs-lisp/crm.el | |||
| @@ -60,7 +60,12 @@ | |||
| 60 | ;; `completing-read'. They should be similar -- it was intentional. | 60 | ;; `completing-read'. They should be similar -- it was intentional. |
| 61 | 61 | ||
| 62 | ;; Some of this code started out as translation from C code in | 62 | ;; Some of this code started out as translation from C code in |
| 63 | ;; src/minibuf.c to Emacs Lisp code. | 63 | ;; src/minibuf.c to Emacs Lisp code. After this code was rewritten in Elisp |
| 64 | ;; and made to operate on any field, this file was completely rewritten to | ||
| 65 | ;; just reuse that code. | ||
| 66 | |||
| 67 | ;; Thanks to Sen Nagata <sen@eccosys.com> for the original version of the | ||
| 68 | ;; code, and sorry for throwing it all out. --Stef | ||
| 64 | 69 | ||
| 65 | ;; Thanks to Richard Stallman for all of his help (many of the good | 70 | ;; 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, | 71 | ;; ideas in here are from him), Gerd Moellmann for his attention, |
| @@ -69,21 +74,24 @@ | |||
| 69 | 74 | ||
| 70 | ;;; Questions and Thoughts: | 75 | ;;; Questions and Thoughts: |
| 71 | 76 | ||
| 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 | ;; -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 | ;; a return value when REQUIRE-MATCH is t? if not, should beep when a user |
| 78 | ;; tries to exit the minibuffer via RET? | 79 | ;; tries to exit the minibuffer via RET? |
| 79 | 80 | ||
| 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. | 81 | ;; -tip: use M-f and M-b for ease of navigation among elements. |
| 86 | 82 | ||
| 83 | ;; - the difference between minibuffer-completion-table and | ||
| 84 | ;; crm-completion-table is just crm--collection-fn. In most cases it | ||
| 85 | ;; shouldn't make any difference. But if a non-CRM completion function | ||
| 86 | ;; happens to be used, it will use minibuffer-completion-table and | ||
| 87 | ;; crm--collection-fn will try to make it do "more or less the right | ||
| 88 | ;; thing" by making it complete on the last element, which is about as | ||
| 89 | ;; good as we can hope for right now. | ||
| 90 | ;; I'm not sure if it's important or not. Maybe we could just throw away | ||
| 91 | ;; crm-completion-table and crm--collection-fn, but there doesn't seem to | ||
| 92 | ;; be a pressing need for it, and since Sen did bother to write it, we may | ||
| 93 | ;; as well keep it, in case it helps. | ||
| 94 | |||
| 87 | ;;; History: | 95 | ;;; History: |
| 88 | ;; | 96 | ;; |
| 89 | ;; 2000-04-10: | 97 | ;; 2000-04-10: |
| @@ -100,12 +108,26 @@ 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' | 108 | completion candidates. Modify this value to make `completing-read-multiple' |
| 101 | use a separator other than `crm-default-separator'.") | 109 | use a separator other than `crm-default-separator'.") |
| 102 | 110 | ||
| 103 | ;; actual filling in of these maps occurs below via `crm-init-keymaps' | 111 | (defvar crm-local-completion-map |
| 104 | (defvar crm-local-completion-map nil | 112 | (let ((map (make-sparse-keymap))) |
| 113 | (set-keymap-parent map minibuffer-local-completion-map) | ||
| 114 | (define-key map [remap minibuffer-complete] #'crm-complete) | ||
| 115 | (define-key map [remap minibuffer-complete-word] #'crm-complete-word) | ||
| 116 | (define-key map [remap minibuffer-completion-help] #'crm-completion-help) | ||
| 117 | map) | ||
| 105 | "Local keymap for minibuffer multiple input with completion. | 118 | "Local keymap for minibuffer multiple input with completion. |
| 106 | Analog of `minibuffer-local-completion-map'.") | 119 | Analog of `minibuffer-local-completion-map'.") |
| 107 | 120 | ||
| 108 | (defvar crm-local-must-match-map nil | 121 | (defvar crm-local-must-match-map |
| 122 | (let ((map (make-sparse-keymap))) | ||
| 123 | ;; We'd want to have multiple inheritance here. | ||
| 124 | (set-keymap-parent map minibuffer-local-must-match-map) | ||
| 125 | (define-key map [remap minibuffer-complete] #'crm-complete) | ||
| 126 | (define-key map [remap minibuffer-complete-word] #'crm-complete-word) | ||
| 127 | (define-key map [remap minibuffer-completion-help] #'crm-completion-help) | ||
| 128 | (define-key map [remap minibuffer-complete-and-exit] | ||
| 129 | #'crm-complete-and-exit) | ||
| 130 | map) | ||
| 109 | "Local keymap for minibuffer multiple input with exact match completion. | 131 | "Local keymap for minibuffer multiple input with exact match completion. |
| 110 | Analog of `minibuffer-local-must-match-map' for crm.") | 132 | Analog of `minibuffer-local-must-match-map' for crm.") |
| 111 | 133 | ||
| @@ -114,38 +136,8 @@ Analog of `minibuffer-local-must-match-map' for crm.") | |||
| 114 | This is a table used for completion by `completing-read-multiple' and its | 136 | This is a table used for completion by `completing-read-multiple' and its |
| 115 | supporting functions.") | 137 | supporting functions.") |
| 116 | 138 | ||
| 117 | ;; this is supposed to be analogous to last_exact_completion in src/minibuf.c | ||
| 118 | (defvar crm-last-exact-completion nil | ||
| 119 | "Completion string if last attempt reported \"Complete, but not unique\".") | ||
| 120 | |||
| 121 | (defvar crm-left-of-element nil | ||
| 122 | "String to the left of the current element.") | ||
| 123 | |||
| 124 | (defvar crm-current-element nil | ||
| 125 | "The current element.") | ||
| 126 | |||
| 127 | (defvar crm-right-of-element nil | ||
| 128 | "String to the right of the current element.") | ||
| 129 | |||
| 130 | (defvar crm-beginning-of-element nil | ||
| 131 | "Buffer position representing the beginning of the current element.") | ||
| 132 | |||
| 133 | (defvar crm-end-of-element nil | ||
| 134 | "Buffer position representing the end of the current element.") | ||
| 135 | |||
| 136 | ;; emulates temp_echo_area_glyphs from src/minibuf.c | ||
| 137 | (defun crm-temp-echo-area-glyphs (message-string) | ||
| 138 | "Temporarily display MESSAGE-STRING in echo area. | ||
| 139 | After user-input or 2 seconds, erase the displayed string." | ||
| 140 | (save-excursion | ||
| 141 | (goto-char (point-max)) | ||
| 142 | (insert message-string) | ||
| 143 | (sit-for 2) | ||
| 144 | (backward-char (length message-string)) | ||
| 145 | (delete-char (length message-string)))) | ||
| 146 | |||
| 147 | ;; this function evolved from a posting by Stefan Monnier | 139 | ;; this function evolved from a posting by Stefan Monnier |
| 148 | (defun crm-collection-fn (string predicate flag) | 140 | (defun crm--collection-fn (string predicate flag) |
| 149 | "Function used by `completing-read-multiple' to compute completion values. | 141 | "Function used by `completing-read-multiple' to compute completion values. |
| 150 | The value of STRING is the string to be completed. | 142 | The value of STRING is the string to be completed. |
| 151 | 143 | ||
| @@ -159,407 +151,84 @@ A value of nil specifies `try-completion'. A value of t specifies | |||
| 159 | For more information on STRING, PREDICATE, and FLAG, see the Elisp | 151 | For more information on STRING, PREDICATE, and FLAG, see the Elisp |
| 160 | Reference sections on 'Programmed Completion' and 'Basic Completion | 152 | Reference sections on 'Programmed Completion' and 'Basic Completion |
| 161 | Functions'." | 153 | Functions'." |
| 162 | (let ((lead "")) | 154 | (let ((beg 0)) |
| 163 | (when (string-match (concat ".*" crm-separator) string) | 155 | (while (string-match crm-separator string beg) |
| 164 | (setq lead (substring string 0 (match-end 0))) | 156 | (setq beg (match-end 0))) |
| 165 | (setq string (substring string (match-end 0)))) | 157 | (completion-table-with-context (substring string 0 beg) |
| 166 | (if (eq flag 'lambda) | 158 | crm-completion-table |
| 167 | ;; return t for exact match, nil otherwise | 159 | (substring string beg) |
| 168 | (let ((result (try-completion string crm-completion-table predicate))) | 160 | predicate |
| 169 | (if (stringp result) | 161 | flag))) |
| 170 | nil | 162 | |
| 171 | (if result | 163 | (defun crm--select-current-element () |
| 172 | t | ||
| 173 | nil)))) | ||
| 174 | (if flag | ||
| 175 | ;; called via (all-completions string 'crm-completion-fn predicate)? | ||
| 176 | (all-completions string crm-completion-table predicate) | ||
| 177 | ;; called via (try-completion string 'crm-completion-fn predicate)? | ||
| 178 | (let ((result (try-completion string crm-completion-table predicate))) | ||
| 179 | (if (stringp result) | ||
| 180 | (concat lead result) | ||
| 181 | result))))) | ||
| 182 | |||
| 183 | (defun crm-find-current-element () | ||
| 184 | "Parse the minibuffer to find the current element. | 164 | "Parse the minibuffer to find the current element. |
| 185 | If no element can be found, return nil. | 165 | Place an overlay on the element, with a `field' property, and return it." |
| 186 | 166 | (let* ((bob (minibuffer-prompt-end)) | |
| 187 | If an element is found, bind: | 167 | (start (save-excursion |
| 188 | 168 | (if (re-search-backward crm-separator bob t) | |
| 189 | -the variable `crm-current-element' to the current element, | 169 | (match-end 0) |
| 190 | 170 | bob))) | |
| 191 | -the variables `crm-left-of-element' and `crm-right-of-element' to | 171 | (end (save-excursion |
| 192 | the strings to the left and right of the current element, | 172 | (if (re-search-forward crm-separator nil t) |
| 193 | respectively, and | 173 | (match-beginning 0) |
| 194 | 174 | (point-max)))) | |
| 195 | -the variables `crm-beginning-of-element' and `crm-end-of-element' to | 175 | (ol (make-overlay start end nil nil t))) |
| 196 | the buffer positions of the beginning and end of the current element | 176 | (overlay-put ol 'field (make-symbol "crm")) |
| 197 | respectively, | 177 | ol)) |
| 198 | 178 | ||
| 199 | and return t." | 179 | (defun crm-completion-help () |
| 200 | (let* ((prompt-end (minibuffer-prompt-end)) | ||
| 201 | (minibuffer-string (buffer-substring prompt-end (point-max))) | ||
| 202 | (end-index (or (string-match "," minibuffer-string (- (point) prompt-end)) | ||
| 203 | (- (point-max) prompt-end))) | ||
| 204 | (target-string (substring minibuffer-string 0 end-index)) | ||
| 205 | (index (or (string-match | ||
| 206 | (concat crm-separator "\\([^" crm-separator "]*\\)$") | ||
| 207 | target-string) | ||
| 208 | (string-match | ||
| 209 | (concat "^\\([^" crm-separator "]*\\)$") | ||
| 210 | target-string)))) | ||
| 211 | (if (not (numberp index)) | ||
| 212 | ;; no candidate found | ||
| 213 | nil | ||
| 214 | (progn | ||
| 215 | ;; | ||
| 216 | (setq crm-beginning-of-element (match-beginning 1)) | ||
| 217 | (setq crm-end-of-element (+ end-index prompt-end)) | ||
| 218 | ;; string to the left of the current element | ||
| 219 | (setq crm-left-of-element | ||
| 220 | (substring target-string 0 (match-beginning 1))) | ||
| 221 | ;; the current element | ||
| 222 | (setq crm-current-element (match-string 1 target-string)) | ||
| 223 | ;; string to the right of the current element | ||
| 224 | (setq crm-right-of-element (substring minibuffer-string end-index)) | ||
| 225 | t)))) | ||
| 226 | |||
| 227 | (defun crm-test-completion (candidate) | ||
| 228 | "Return t if CANDIDATE is an exact match for a valid completion." | ||
| 229 | (let ((completions | ||
| 230 | ;; TODO: verify whether the arguments are appropriate | ||
| 231 | (all-completions | ||
| 232 | candidate crm-completion-table minibuffer-completion-predicate))) | ||
| 233 | (if (member candidate completions) | ||
| 234 | t | ||
| 235 | nil))) | ||
| 236 | |||
| 237 | (defun crm-minibuffer-completion-help () | ||
| 238 | "Display a list of possible completions of the current minibuffer element." | 180 | "Display a list of possible completions of the current minibuffer element." |
| 239 | (interactive) | 181 | (interactive) |
| 240 | (message "Making completion list...") | 182 | (let ((ol (crm-select-current-element))) |
| 241 | (if (not (crm-find-current-element)) | 183 | (unwind-protect |
| 242 | nil | 184 | (minibuffer-completion-help) |
| 243 | (let ((completions (all-completions crm-current-element | 185 | (delete-overlay ol))) |
| 244 | minibuffer-completion-table | ||
| 245 | minibuffer-completion-predicate))) | ||
| 246 | (message nil) | ||
| 247 | (if (null completions) | ||
| 248 | (crm-temp-echo-area-glyphs " [No completions]") | ||
| 249 | (with-output-to-temp-buffer "*Completions*" | ||
| 250 | (display-completion-list | ||
| 251 | (sort completions 'string-lessp) | ||
| 252 | crm-current-element))))) | ||
| 253 | nil) | 186 | nil) |
| 254 | 187 | ||
| 255 | (defun crm-do-completion () | 188 | (defun crm-complete () |
| 256 | "This is the internal completion engine. | ||
| 257 | This function updates the text in the minibuffer | ||
| 258 | to complete the current string, and returns a number between 0 and 6. | ||
| 259 | The meanings of the return values are: | ||
| 260 | |||
| 261 | 0 - the string has no possible completion | ||
| 262 | 1 - the string is already a valid and unique match | ||
| 263 | 2 - not used | ||
| 264 | 3 - the string is already a valid match (but longer matches exist too) | ||
| 265 | 4 - the string was completed to a valid match | ||
| 266 | 5 - some completion has been done, but the result is not a match | ||
| 267 | 6 - no completion was done, and the string is not an exact match" | ||
| 268 | |||
| 269 | (if (not (crm-find-current-element)) | ||
| 270 | nil | ||
| 271 | (let (last completion completedp) | ||
| 272 | (setq completion | ||
| 273 | (try-completion crm-current-element | ||
| 274 | minibuffer-completion-table | ||
| 275 | minibuffer-completion-predicate)) | ||
| 276 | (setq last crm-last-exact-completion) | ||
| 277 | (setq crm-last-exact-completion nil) | ||
| 278 | |||
| 279 | (catch 'crm-exit | ||
| 280 | |||
| 281 | (if (null completion) ; no possible completion | ||
| 282 | (progn | ||
| 283 | (crm-temp-echo-area-glyphs " [No match]") | ||
| 284 | (throw 'crm-exit 0))) | ||
| 285 | |||
| 286 | (if (eq completion t) ; was already an exact and unique completion | ||
| 287 | (throw 'crm-exit 1)) | ||
| 288 | |||
| 289 | (setq completedp | ||
| 290 | (null (string-equal completion crm-current-element))) | ||
| 291 | |||
| 292 | (if completedp | ||
| 293 | (progn | ||
| 294 | (delete-region (minibuffer-prompt-end) (point-max)) | ||
| 295 | (insert crm-left-of-element completion) | ||
| 296 | ;; (if crm-complete-up-to-point | ||
| 297 | ;; (insert crm-separator)) | ||
| 298 | (insert crm-right-of-element) | ||
| 299 | (backward-char (length crm-right-of-element)) | ||
| 300 | ;; TODO: is this correct? | ||
| 301 | (setq crm-current-element completion))) | ||
| 302 | |||
| 303 | (if (null (crm-test-completion crm-current-element)) | ||
| 304 | (progn | ||
| 305 | (if completedp ; some completion happened | ||
| 306 | (throw 'crm-exit 5) | ||
| 307 | (if completion-auto-help | ||
| 308 | (crm-minibuffer-completion-help) | ||
| 309 | (crm-temp-echo-area-glyphs " [Next char not unique]"))) | ||
| 310 | (throw 'crm-exit 6)) | ||
| 311 | (if completedp | ||
| 312 | (throw 'crm-exit 4))) | ||
| 313 | |||
| 314 | (setq crm-last-exact-completion completion) | ||
| 315 | (if (not (null last)) | ||
| 316 | (progn | ||
| 317 | (if (not (null (equal crm-current-element last))) | ||
| 318 | (crm-minibuffer-completion-help)))) | ||
| 319 | |||
| 320 | ;; returning -- was already an exact completion | ||
| 321 | (throw 'crm-exit 3))))) | ||
| 322 | |||
| 323 | (defun crm-minibuffer-complete () | ||
| 324 | "Complete the current element. | 189 | "Complete the current element. |
| 325 | If no characters can be completed, display a list of possible completions. | 190 | If no characters can be completed, display a list of possible completions. |
| 326 | 191 | ||
| 327 | Return t if the current element is now a valid match; otherwise return nil." | 192 | Return t if the current element is now a valid match; otherwise return nil." |
| 328 | (interactive) | 193 | (interactive) |
| 329 | ;; take care of scrolling if necessary -- completely cribbed from minibuf.c | 194 | (let ((ol (crm-select-current-element))) |
| 330 | (if (not (eq last-command this-command)) | 195 | (unwind-protect |
| 331 | ;; ok? | 196 | (minibuffer-complete) |
| 332 | (setq minibuffer-scroll-window nil)) | 197 | (delete-overlay ol)))) |
| 333 | (let ((window minibuffer-scroll-window)) | 198 | |
| 334 | (if (and (not (null window)) | 199 | (defun crm-complete-word () |
| 335 | ;; ok? | 200 | "Complete the current element at most a single word. |
| 336 | (not (null (window-buffer window)))) | 201 | Like `minibuffer-complete-word' but for `completing-read-multiple'." |
| 337 | (let (tem) | 202 | (interactive) |
| 338 | (set-buffer (window-buffer window)) | 203 | (let ((ol (crm-select-current-element))) |
| 339 | ;; ok? | 204 | (unwind-protect |
| 340 | (setq tem (pos-visible-in-window-p (point-max) window)) | 205 | (minibuffer-complete-word) |
| 341 | (if (not (null tem)) | 206 | (delete-overlay ol)))) |
| 342 | ;; ok? | 207 | |
| 343 | (set-window-start window (point-min) nil) | 208 | (defun crm-complete-and-exit () |
| 344 | (scroll-other-window nil)) | ||
| 345 | ;; reaching here means exiting the function w/ return value of nil | ||
| 346 | nil) | ||
| 347 | |||
| 348 | (let* ( | ||
| 349 | ;(crm-end-of-element nil) | ||
| 350 | (result (crm-do-completion))) | ||
| 351 | (cond | ||
| 352 | ((eq 0 result) | ||
| 353 | nil) | ||
| 354 | ((eq 1 result) | ||
| 355 | ;; adapted from Emacs 21 | ||
| 356 | (if (not (eq (point) crm-end-of-element)) | ||
| 357 | (goto-char (+ 1 crm-end-of-element))) | ||
| 358 | (crm-temp-echo-area-glyphs " [Sole completion]") | ||
| 359 | t) | ||
| 360 | ((eq 3 result) | ||
| 361 | ;; adapted from Emacs 21 | ||
| 362 | (if (not (eq (point) crm-end-of-element)) | ||
| 363 | (goto-char (+ 1 crm-end-of-element))) | ||
| 364 | (crm-temp-echo-area-glyphs " [Complete, but not unique]") | ||
| 365 | t)))))) | ||
| 366 | |||
| 367 | ;; i love traffic lights...but only when they're green | ||
| 368 | (defun crm-find-longest-completable-substring (string) | ||
| 369 | "Determine the longest completable (left-anchored) substring of STRING. | ||
| 370 | The description \"left-anchored\" means the positions of the characters | ||
| 371 | in the substring must be the same as those of the corresponding characters | ||
| 372 | in STRING. Anchoring is what `^' does in a regular expression. | ||
| 373 | |||
| 374 | The table and predicate used for completion are | ||
| 375 | `minibuffer-completion-table' and `minibuffer-completion-predicate', | ||
| 376 | respectively. | ||
| 377 | |||
| 378 | A non-nil return value means that there is some substring which is | ||
| 379 | completable. A return value of t means that STRING itself is | ||
| 380 | completable. If a string value is returned it is the longest | ||
| 381 | completable proper substring of STRING. If nil is returned, STRING | ||
| 382 | does not have any non-empty completable substrings. | ||
| 383 | |||
| 384 | Remember: \"left-anchored\" substring" | ||
| 385 | (let* ((length-of-string (length string)) | ||
| 386 | (index length-of-string) | ||
| 387 | (done (if (> length-of-string 0) | ||
| 388 | nil | ||
| 389 | t)) | ||
| 390 | (first t) ; ugh, special handling for first time through... | ||
| 391 | goal-string | ||
| 392 | result) | ||
| 393 | ;; loop through left-anchored substrings in order of descending length, | ||
| 394 | ;; find the first substring that is completable | ||
| 395 | (while (not done) | ||
| 396 | (setq result (try-completion (substring string 0 index) | ||
| 397 | minibuffer-completion-table | ||
| 398 | minibuffer-completion-predicate)) | ||
| 399 | (if result | ||
| 400 | ;; found completable substring | ||
| 401 | (progn | ||
| 402 | (setq done t) | ||
| 403 | (if (and (eq result t) first) | ||
| 404 | ;; exactly matching string first time through | ||
| 405 | (setq goal-string t) | ||
| 406 | ;; fully-completed proper substring | ||
| 407 | (setq goal-string (substring string 0 index))))) | ||
| 408 | (setq index (1- index)) | ||
| 409 | (if first | ||
| 410 | (setq first nil)) | ||
| 411 | (if (<= index 0) | ||
| 412 | (setq done t))) | ||
| 413 | ;; possible values include: t, nil, some string | ||
| 414 | goal-string)) | ||
| 415 | |||
| 416 | ;; TODO: decide whether trailing separator is allowed. current | ||
| 417 | ;; implementation appears to allow it | ||
| 418 | (defun crm-strings-completed-p (separated-string) | ||
| 419 | "Verify that strings in SEPARATED-STRING are completed strings. | ||
| 420 | A return value of t means that all strings were verified. A number is | ||
| 421 | returned if verification was unsuccessful. This number represents the | ||
| 422 | position in SEPARATED-STRING up to where completion was successful." | ||
| 423 | (let ((strings (split-string separated-string crm-separator)) | ||
| 424 | ;; buffers start at 1, not 0 | ||
| 425 | (current-position 1) | ||
| 426 | current-string | ||
| 427 | result | ||
| 428 | done) | ||
| 429 | (while (and strings (not done)) | ||
| 430 | (setq current-string (car strings) | ||
| 431 | result (try-completion current-string | ||
| 432 | minibuffer-completion-table | ||
| 433 | minibuffer-completion-predicate)) | ||
| 434 | (if (eq result t) | ||
| 435 | (setq strings (cdr strings) | ||
| 436 | current-position (+ current-position | ||
| 437 | (length current-string) | ||
| 438 | ;; automatically adding 1 for separator | ||
| 439 | ;; character | ||
| 440 | 1)) | ||
| 441 | ;; still one more case of a match | ||
| 442 | (if (stringp result) | ||
| 443 | (let ((string-list | ||
| 444 | (all-completions result | ||
| 445 | minibuffer-completion-table | ||
| 446 | minibuffer-completion-predicate))) | ||
| 447 | (if (member result string-list) | ||
| 448 | ;; ho ho, code duplication... | ||
| 449 | (setq strings (cdr strings) | ||
| 450 | current-position (+ current-position | ||
| 451 | (length current-string) | ||
| 452 | 1)) | ||
| 453 | (progn | ||
| 454 | (setq done t) | ||
| 455 | ;; current-string is a partially-completed string | ||
| 456 | (setq current-position (+ current-position | ||
| 457 | (length current-string)))))) | ||
| 458 | ;; current-string cannot be completed | ||
| 459 | (let ((completable-substring | ||
| 460 | (crm-find-longest-completable-substring current-string))) | ||
| 461 | (setq done t) | ||
| 462 | (setq current-position (+ current-position | ||
| 463 | (length completable-substring))))))) | ||
| 464 | ;; return our result | ||
| 465 | (if (null strings) | ||
| 466 | t | ||
| 467 | current-position))) | ||
| 468 | |||
| 469 | ;; try to complete candidate, then check all separated strings. move | ||
| 470 | ;; point to problem position if checking fails for some string. if | ||
| 471 | ;; checking succeeds for all strings, exit. | ||
| 472 | (defun crm-minibuffer-complete-and-exit () | ||
| 473 | "If all of the minibuffer elements are valid completions then exit. | 209 | "If all of the minibuffer elements are valid completions then exit. |
| 474 | All elements in the minibuffer must match. If there is a mismatch, move point | 210 | All elements in the minibuffer must match. If there is a mismatch, move point |
| 475 | to the location of mismatch and do not exit. | 211 | to the location of mismatch and do not exit. |
| 476 | 212 | ||
| 477 | This function is modeled after `minibuffer_complete_and_exit' in src/minibuf.c" | 213 | This function is modeled after `minibuffer-complete-and-exit'." |
| 478 | (interactive) | 214 | (interactive) |
| 479 | 215 | (let ((doexit t)) | |
| 480 | (if (not (crm-find-current-element)) | 216 | (goto-char (minibuffer-prompt-end)) |
| 481 | nil | 217 | (while |
| 482 | (let (result) | 218 | (and doexit |
| 483 | 219 | (let ((ol (crm-select-current-element))) | |
| 484 | (setq result | 220 | (goto-char (overlay-end ol)) |
| 485 | (catch 'crm-exit | 221 | (unwind-protect |
| 486 | 222 | (catch 'exit | |
| 487 | (if (eq (minibuffer-prompt-end) (point-max)) | 223 | (minibuffer-complete-and-exit) |
| 488 | (throw 'crm-exit t)) | 224 | ;; This did not throw `exit', so there was a problem. |
| 489 | 225 | (setq doexit nil)) | |
| 490 | ;; TODO: this test is suspect? | 226 | (goto-char (overlay-end ol)) |
| 491 | (if (not (null (crm-test-completion crm-current-element))) | 227 | (delete-overlay ol)) |
| 492 | (throw 'crm-exit "check")) | 228 | (not (eobp)))) |
| 493 | 229 | ;; Skip to the next element. | |
| 494 | ;; TODO: determine how to detect errors | 230 | (forward-char 1)) |
| 495 | (let ((result (crm-do-completion))) | 231 | (if doexit (exit-minibuffer)))) |
| 496 | |||
| 497 | (cond | ||
| 498 | ((or (eq 1 result) | ||
| 499 | (eq 3 result)) | ||
| 500 | (throw 'crm-exit "check")) | ||
| 501 | ((eq 4 result) | ||
| 502 | (if (not (null minibuffer-completion-confirm)) | ||
| 503 | (progn | ||
| 504 | (crm-temp-echo-area-glyphs " [Confirm]") | ||
| 505 | nil) | ||
| 506 | (throw 'crm-exit "check"))) | ||
| 507 | (nil))))) | ||
| 508 | |||
| 509 | (if (null result) | ||
| 510 | nil | ||
| 511 | (if (equal result "check") | ||
| 512 | (let ((check-strings | ||
| 513 | (crm-strings-completed-p | ||
| 514 | (buffer-substring (minibuffer-prompt-end) (point-max))))) | ||
| 515 | ;; check all of minibuffer | ||
| 516 | (if (eq check-strings t) | ||
| 517 | (throw 'exit nil) | ||
| 518 | (if (numberp check-strings) | ||
| 519 | (progn | ||
| 520 | (goto-char check-strings) | ||
| 521 | (crm-temp-echo-area-glyphs " [An element did not match]")) | ||
| 522 | (message "Unexpected error")))) | ||
| 523 | (if (eq result t) | ||
| 524 | (throw 'exit nil) | ||
| 525 | (message "Unexpected error"))))))) | ||
| 526 | |||
| 527 | (defun crm-init-keymaps () | ||
| 528 | "Initialize the keymaps used by `completing-read-multiple'. | ||
| 529 | Two keymaps are used depending on the value of the REQUIRE-MATCH | ||
| 530 | argument of the function `completing-read-multiple'. | ||
| 531 | |||
| 532 | If REQUIRE-MATCH is nil, the keymap `crm-local-completion-map' is used. | ||
| 533 | This keymap inherits from the keymap named `minibuffer-local-completion-map'. | ||
| 534 | The only difference is that TAB is bound to `crm-minibuffer-complete' in | ||
| 535 | the inheriting keymap. | ||
| 536 | |||
| 537 | If REQUIRE-MATCH is non-nil, the keymap `crm-local-must-match-map' is used. | ||
| 538 | This keymap inherits from the keymap named `minibuffer-local-must-match-map'. | ||
| 539 | The inheriting keymap binds RET to `crm-minibuffer-complete-and-exit' | ||
| 540 | and TAB to `crm-minibuffer-complete'." | ||
| 541 | (unless crm-local-completion-map | ||
| 542 | (setq crm-local-completion-map (make-sparse-keymap)) | ||
| 543 | (set-keymap-parent crm-local-completion-map | ||
| 544 | minibuffer-local-completion-map) | ||
| 545 | ;; key definitions | ||
| 546 | (define-key crm-local-completion-map | ||
| 547 | (kbd "TAB") | ||
| 548 | (function crm-minibuffer-complete))) | ||
| 549 | |||
| 550 | (unless crm-local-must-match-map | ||
| 551 | (setq crm-local-must-match-map (make-sparse-keymap)) | ||
| 552 | (set-keymap-parent crm-local-must-match-map | ||
| 553 | minibuffer-local-must-match-map) | ||
| 554 | ;; key definitions | ||
| 555 | (define-key crm-local-must-match-map | ||
| 556 | (kbd "RET") | ||
| 557 | (function crm-minibuffer-complete-and-exit)) | ||
| 558 | (define-key crm-local-must-match-map | ||
| 559 | (kbd "TAB") | ||
| 560 | (function crm-minibuffer-complete)))) | ||
| 561 | |||
| 562 | (crm-init-keymaps) | ||
| 563 | 232 | ||
| 564 | ;; superemulates behavior of completing_read in src/minibuf.c | 233 | ;; superemulates behavior of completing_read in src/minibuf.c |
| 565 | ;;;###autoload | 234 | ;;;###autoload |
| @@ -592,18 +261,12 @@ The return value of this function is a list of the read strings. | |||
| 592 | See the documentation for `completing-read' for details on the arguments: | 261 | See the documentation for `completing-read' for details on the arguments: |
| 593 | PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HIST, DEF, and | 262 | PROMPT, TABLE, PREDICATE, REQUIRE-MATCH, INITIAL-INPUT, HIST, DEF, and |
| 594 | INHERIT-INPUT-METHOD." | 263 | INHERIT-INPUT-METHOD." |
| 595 | (let* ((minibuffer-completion-table (function crm-collection-fn)) | 264 | (let* ((minibuffer-completion-table #'crm--collection-fn) |
| 596 | (minibuffer-completion-predicate predicate) | 265 | (minibuffer-completion-predicate predicate) |
| 597 | ;; see completing_read in src/minibuf.c | 266 | ;; see completing_read in src/minibuf.c |
| 598 | (minibuffer-completion-confirm | 267 | (minibuffer-completion-confirm |
| 599 | (unless (eq require-match t) require-match)) | 268 | (unless (eq require-match t) require-match)) |
| 600 | (crm-completion-table table) | 269 | (crm-completion-table table) |
| 601 | crm-last-exact-completion | ||
| 602 | crm-current-element | ||
| 603 | crm-left-of-element | ||
| 604 | crm-right-of-element | ||
| 605 | crm-beginning-of-element | ||
| 606 | crm-end-of-element | ||
| 607 | (map (if require-match | 270 | (map (if require-match |
| 608 | crm-local-must-match-map | 271 | crm-local-must-match-map |
| 609 | crm-local-completion-map)) | 272 | crm-local-completion-map)) |
| @@ -615,6 +278,12 @@ INHERIT-INPUT-METHOD." | |||
| 615 | (and def (string-equal input "") (setq input def)) | 278 | (and def (string-equal input "") (setq input def)) |
| 616 | (split-string input crm-separator))) | 279 | (split-string input crm-separator))) |
| 617 | 280 | ||
| 281 | (define-obsolete-function-alias 'crm-minibuffer-complete 'crm-complete "23.1") | ||
| 282 | (define-obsolete-function-alias | ||
| 283 | 'crm-minibuffer-completion-help 'crm-completion-help "23.1") | ||
| 284 | (define-obsolete-function-alias | ||
| 285 | 'crm-minibuffer-complete-and-exit 'crm-complete-and-exit "23.1") | ||
| 286 | |||
| 618 | ;; testing and debugging | 287 | ;; testing and debugging |
| 619 | ;; (defun crm-init-test-environ () | 288 | ;; (defun crm-init-test-environ () |
| 620 | ;; "Set up some variables for testing." | 289 | ;; "Set up some variables for testing." |