Mercurial > emacs
comparison lisp/minibuffer.el @ 105697:136cf2d23c90
* minibuffer.el (completion-table-with-terminator): Properly implement
boundaries, in case `terminator' appears in the suffix.
(completion--embedded-envvar-table): Don't return boundaries if
there's no valid completion. Simplify.
(completion-file-name-table): New completion table extracted from
completion--file-name-table.
(completion--file-name-table): Use it.
(read-file-name-predicate): Declare obsolete.
(read-file-name): Use the pred arg i.s.o read-file-name-predicate.
* vc-bzr.el (vc-bzr-revision-completion-table): Use the new
completion-file-name-table, and use the `pred' argument.
* files.el (locate-file-completion-table): Use the `pred' arg rather
than read-file-name-predicate.
(abbreviate-file-name): Use \` rather than ^ for BOS.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Wed, 21 Oct 2009 20:03:57 +0000 |
parents | 6a6fcf3e8e4d |
children | 834e4fdbe74a |
comparison
equal
deleted
inserted
replaced
105696:56d1856a3ea9 | 105697:136cf2d23c90 |
---|---|
35 | 35 |
36 ;; - completion-all-sorted-completions list all the completions, whereas | 36 ;; - completion-all-sorted-completions list all the completions, whereas |
37 ;; it should only lists the ones that `try-completion' would consider. | 37 ;; it should only lists the ones that `try-completion' would consider. |
38 ;; E.g. it should honor completion-ignored-extensions. | 38 ;; E.g. it should honor completion-ignored-extensions. |
39 ;; - choose-completion can't automatically figure out the boundaries | 39 ;; - choose-completion can't automatically figure out the boundaries |
40 ;; corresponding to the displayed completions. `base-size' gives the left | 40 ;; corresponding to the displayed completions because we only |
41 ;; boundary, but not the righthand one. So we need to add | 41 ;; provide the start info but not the end info in |
42 ;; completion-extra-size. | 42 ;; completion-base-position. |
43 ;; - choose-completion doesn't know how to quote the text it inserts. | |
44 ;; E.g. it fails to double the dollars in file-name completion, or | |
45 ;; to backslash-escape spaces and other chars in comint completion. | |
46 ;; - C-x C-f ~/*/sr ? should not list "~/./src". | |
47 ;; - minibuffer-force-complete completes ~/src/emacs/t<!>/lisp/minibuffer.el | |
48 ;; to ~/src/emacs/trunk/ and throws away lisp/minibuffer.el. | |
43 | 49 |
44 ;;; Todo: | 50 ;;; Todo: |
45 | 51 |
46 ;; - make partial-complete-mode obsolete: | 52 ;; - make partial-complete-mode obsolete: |
47 ;; - (?) <foo.h> style completion for file names. | 53 ;; - (?) <foo.h> style completion for file names. |
48 | 54 ;; This can't be done identically just by tweaking completion, |
49 ;; - case-sensitivity is currently confuses two issues: | 55 ;; because partial-completion-mode's behavior is to expand <string.h> |
56 ;; to /usr/include/string.h only when exiting the minibuffer, at which | |
57 ;; point the completion code is actually not involved normally. | |
58 ;; Partial-completion-mode does it via a find-file-not-found-function. | |
59 ;; - special code for C-x C-f <> to visit the file ref'd at point | |
60 ;; via (require 'foo) or #include "foo". ffap seems like a better | |
61 ;; place for this feature (supplemented with major-mode-provided | |
62 ;; functions to find the file ref'd at point). | |
63 | |
64 ;; - case-sensitivity currently confuses two issues: | |
50 ;; - whether or not a particular completion table should be case-sensitive | 65 ;; - whether or not a particular completion table should be case-sensitive |
51 ;; (i.e. whether strings that different only by case are semantically | 66 ;; (i.e. whether strings that differ only by case are semantically |
52 ;; equivalent) | 67 ;; equivalent) |
53 ;; - whether the user wants completion to pay attention to case. | 68 ;; - whether the user wants completion to pay attention to case. |
54 ;; e.g. we may want to make it possible for the user to say "first try | 69 ;; e.g. we may want to make it possible for the user to say "first try |
55 ;; completion case-sensitively, and if that fails, try to ignore case". | 70 ;; completion case-sensitively, and if that fails, try to ignore case". |
56 | 71 |
57 ;; - make lisp-complete-symbol and sym-comp use it. | |
58 ;; - add support for ** to pcm. | 72 ;; - add support for ** to pcm. |
59 ;; - Make read-file-name-predicate obsolete. | |
60 ;; - Add vc-file-name-completion-table to read-file-name-internal. | 73 ;; - Add vc-file-name-completion-table to read-file-name-internal. |
61 ;; - A feature like completing-help.el. | 74 ;; - A feature like completing-help.el. |
62 ;; - make lisp/complete.el obsolete. | 75 ;; - make lisp/complete.el obsolete. |
63 ;; - Make the `hide-spaces' arg of all-completions obsolete? | 76 ;; - Make the `hide-spaces' arg of all-completions obsolete? |
64 | 77 |
180 ;; In case of try-completion, add the prefix. | 193 ;; In case of try-completion, add the prefix. |
181 ((stringp comp) (concat prefix comp)) | 194 ((stringp comp) (concat prefix comp)) |
182 (t comp))))) | 195 (t comp))))) |
183 | 196 |
184 (defun completion-table-with-terminator (terminator table string pred action) | 197 (defun completion-table-with-terminator (terminator table string pred action) |
198 "Construct a completion table like TABLE but with an extra TERMINATOR. | |
199 This is meant to be called in a curried way by first passing TERMINATOR | |
200 and TABLE only (via `apply-partially'). | |
201 TABLE is a completion table, and TERMINATOR is a string appended to TABLE's | |
202 completion if it is complete. TERMINATOR is also used to determine the | |
203 completion suffix's boundary." | |
185 (cond | 204 (cond |
205 ((eq (car-safe action) 'boundaries) | |
206 (let* ((suffix (cdr action)) | |
207 (bounds (completion-boundaries string table pred suffix)) | |
208 (max (string-match (regexp-quote terminator) suffix))) | |
209 (list* 'boundaries (car bounds) | |
210 (min (cdr bounds) (or max (length suffix)))))) | |
186 ((eq action nil) | 211 ((eq action nil) |
187 (let ((comp (try-completion string table pred))) | 212 (let ((comp (try-completion string table pred))) |
188 (if (eq comp t) | 213 (if (eq comp t) |
189 (concat string terminator) | 214 (concat string terminator) |
190 (if (and (stringp comp) | 215 (if (and (stringp comp) |
216 ;; FIXME: Try to avoid this second call, especially since | |
217 ;; it may be very inefficient (because `comp' made us | |
218 ;; jump to a new boundary, so we complete in that | |
219 ;; boundary with an empty start string). | |
220 ;; completion-boundaries might help. | |
191 (eq (try-completion comp table pred) t)) | 221 (eq (try-completion comp table pred) t)) |
192 (concat comp terminator) | 222 (concat comp terminator) |
193 comp)))) | 223 comp)))) |
194 ((eq action t) | 224 ((eq action t) |
195 ;; FIXME: We generally want the `try' and `all' behaviors to be | 225 ;; FIXME: We generally want the `try' and `all' behaviors to be |
230 (and (not strict) | 260 (and (not strict) |
231 (complete-with-action action table string pred2)))))) | 261 (complete-with-action action table string pred2)))))) |
232 | 262 |
233 (defun completion-table-in-turn (&rest tables) | 263 (defun completion-table-in-turn (&rest tables) |
234 "Create a completion table that tries each table in TABLES in turn." | 264 "Create a completion table that tries each table in TABLES in turn." |
265 ;; FIXME: the boundaries may come from TABLE1 even when the completion list | |
266 ;; is returned by TABLE2 (because TABLE1 returned an empty list). | |
235 (lexical-let ((tables tables)) | 267 (lexical-let ((tables tables)) |
236 (lambda (string pred action) | 268 (lambda (string pred action) |
237 (completion--some (lambda (table) | 269 (completion--some (lambda (table) |
238 (complete-with-action action table string pred)) | 270 (complete-with-action action table string pred)) |
239 tables)))) | 271 tables)))) |
531 (defun minibuffer-force-complete () | 563 (defun minibuffer-force-complete () |
532 "Complete the minibuffer to an exact match. | 564 "Complete the minibuffer to an exact match. |
533 Repeated uses step through the possible completions." | 565 Repeated uses step through the possible completions." |
534 (interactive) | 566 (interactive) |
535 ;; FIXME: Need to deal with the extra-size issue here as well. | 567 ;; FIXME: Need to deal with the extra-size issue here as well. |
568 ;; FIXME: ~/src/emacs/t<M-TAB>/lisp/minibuffer.el completes to | |
569 ;; ~/src/emacs/trunk/ and throws away lisp/minibuffer.el. | |
536 (let* ((start (field-beginning)) | 570 (let* ((start (field-beginning)) |
537 (end (field-end)) | 571 (end (field-end)) |
538 (all (completion-all-sorted-completions))) | 572 (all (completion-all-sorted-completions))) |
539 (if (not (consp all)) | 573 (if (not (consp all)) |
540 (minibuffer-message (if all "No more completions" "No completions")) | 574 (minibuffer-message (if all "No more completions" "No completions")) |
1024 (defconst completion--embedded-envvar-re | 1058 (defconst completion--embedded-envvar-re |
1025 (concat "\\(?:^\\|[^$]\\(?:\\$\\$\\)*\\)" | 1059 (concat "\\(?:^\\|[^$]\\(?:\\$\\$\\)*\\)" |
1026 "$\\([[:alnum:]_]*\\|{\\([^}]*\\)\\)\\'")) | 1060 "$\\([[:alnum:]_]*\\|{\\([^}]*\\)\\)\\'")) |
1027 | 1061 |
1028 (defun completion--embedded-envvar-table (string pred action) | 1062 (defun completion--embedded-envvar-table (string pred action) |
1029 (if (eq (car-safe action) 'boundaries) | 1063 (when (string-match completion--embedded-envvar-re string) |
1030 ;; Compute the boundaries of the subfield to which this | 1064 (let* ((beg (or (match-beginning 2) (match-beginning 1))) |
1031 ;; completion applies. | 1065 (table (completion--make-envvar-table)) |
1032 (let ((suffix (cdr action))) | 1066 (prefix (substring string 0 beg))) |
1033 (if (string-match completion--embedded-envvar-re string) | 1067 (if (eq (car-safe action) 'boundaries) |
1034 (list* 'boundaries | 1068 ;; Only return boundaries if there's something to complete, |
1035 (or (match-beginning 2) (match-beginning 1)) | 1069 ;; since otherwise when we're used in |
1036 (when (string-match "[^[:alnum:]_]" suffix) | 1070 ;; completion-table-in-turn, we could return boundaries and |
1037 (match-beginning 0))))) | 1071 ;; let some subsequent table return a list of completions. |
1038 (when (string-match completion--embedded-envvar-re string) | 1072 ;; FIXME: Maybe it should rather be fixed in |
1039 (let* ((beg (or (match-beginning 2) (match-beginning 1))) | 1073 ;; completion-table-in-turn instead, but it's difficult to |
1040 (table (completion--make-envvar-table)) | 1074 ;; do it efficiently there. |
1041 (prefix (substring string 0 beg))) | 1075 (when (try-completion prefix table pred) |
1076 ;; Compute the boundaries of the subfield to which this | |
1077 ;; completion applies. | |
1078 (let ((suffix (cdr action))) | |
1079 (list* 'boundaries | |
1080 (or (match-beginning 2) (match-beginning 1)) | |
1081 (when (string-match "[^[:alnum:]_]" suffix) | |
1082 (match-beginning 0))))) | |
1042 (if (eq (aref string (1- beg)) ?{) | 1083 (if (eq (aref string (1- beg)) ?{) |
1043 (setq table (apply-partially 'completion-table-with-terminator | 1084 (setq table (apply-partially 'completion-table-with-terminator |
1044 "}" table))) | 1085 "}" table))) |
1045 ;; Even if file-name completion is case-insensitive, we want | 1086 ;; Even if file-name completion is case-insensitive, we want |
1046 ;; envvar completion to be case-sensitive. | 1087 ;; envvar completion to be case-sensitive. |
1047 (let ((completion-ignore-case nil)) | 1088 (let ((completion-ignore-case nil)) |
1048 (completion-table-with-context | 1089 (completion-table-with-context |
1049 prefix table (substring string beg) pred action)))))) | 1090 prefix table (substring string beg) pred action)))))) |
1050 | 1091 |
1051 (defun completion--file-name-table (string pred action) | 1092 (defun completion-file-name-table (string pred action) |
1052 "Internal subroutine for `read-file-name'. Do not call this." | 1093 "Completion table for file names." |
1094 (ignore-errors | |
1053 (cond | 1095 (cond |
1054 ((and (zerop (length string)) (eq 'lambda action)) | |
1055 nil) ; FIXME: why? | |
1056 ((eq (car-safe action) 'boundaries) | 1096 ((eq (car-safe action) 'boundaries) |
1057 ;; FIXME: Actually, this is not always right in the presence of | |
1058 ;; envvars, but there's not much we can do, I think. | |
1059 (let ((start (length (file-name-directory string))) | 1097 (let ((start (length (file-name-directory string))) |
1060 (end (string-match-p "/" (cdr action)))) | 1098 (end (string-match-p "/" (cdr action)))) |
1061 (list* 'boundaries start end))) | 1099 (list* 'boundaries start end))) |
1062 | 1100 |
1101 ((eq action 'lambda) | |
1102 (if (zerop (length string)) | |
1103 nil ;Not sure why it's here, but it probably doesn't harm. | |
1104 (funcall (or pred 'file-exists-p) string))) | |
1105 | |
1063 (t | 1106 (t |
1064 (let* ((dir (if (stringp pred) | 1107 (let* ((name (file-name-nondirectory string)) |
1065 ;; It used to be that `pred' was abused to pass `dir' | 1108 (specdir (file-name-directory string)) |
1066 ;; as an argument. | 1109 (realdir (or specdir default-directory))) |
1067 (prog1 (expand-file-name pred) (setq pred nil)) | |
1068 default-directory)) | |
1069 (str (condition-case nil | |
1070 (substitute-in-file-name string) | |
1071 (error string))) | |
1072 (name (file-name-nondirectory str)) | |
1073 (specdir (file-name-directory str)) | |
1074 (realdir (if specdir (expand-file-name specdir dir) | |
1075 (file-name-as-directory dir)))) | |
1076 | 1110 |
1077 (cond | 1111 (cond |
1078 ((null action) | 1112 ((null action) |
1079 (let ((comp (file-name-completion name realdir | 1113 (let ((comp (file-name-completion name realdir pred))) |
1080 read-file-name-predicate))) | 1114 (if (stringp comp) |
1081 (cond | 1115 (concat specdir comp) |
1082 ((stringp comp) | 1116 comp))) |
1083 ;; Requote the $s before returning the completion. | |
1084 (minibuffer--double-dollars (concat specdir comp))) | |
1085 (comp | |
1086 ;; Requote the $s before checking for changes. | |
1087 (setq str (minibuffer--double-dollars str)) | |
1088 (if (string-equal string str) | |
1089 comp | |
1090 ;; If there's no real completion, but substitute-in-file-name | |
1091 ;; changed the string, then return the new string. | |
1092 str))))) | |
1093 | 1117 |
1094 ((eq action t) | 1118 ((eq action t) |
1095 (let ((all (file-name-all-completions name realdir))) | 1119 (let ((all (file-name-all-completions name realdir))) |
1096 | 1120 |
1097 ;; Check the predicate, if necessary. | 1121 ;; Check the predicate, if necessary. |
1098 (unless (memq read-file-name-predicate '(nil file-exists-p)) | 1122 (unless (memq pred '(nil file-exists-p)) |
1099 (let ((comp ()) | 1123 (let ((comp ()) |
1100 (pred | 1124 (pred |
1101 (if (eq read-file-name-predicate 'file-directory-p) | 1125 (if (eq pred 'file-directory-p) |
1102 ;; Brute-force speed up for directory checking: | 1126 ;; Brute-force speed up for directory checking: |
1103 ;; Discard strings which don't end in a slash. | 1127 ;; Discard strings which don't end in a slash. |
1104 (lambda (s) | 1128 (lambda (s) |
1105 (let ((len (length s))) | 1129 (let ((len (length s))) |
1106 (and (> len 0) (eq (aref s (1- len)) ?/)))) | 1130 (and (> len 0) (eq (aref s (1- len)) ?/)))) |
1107 ;; Must do it the hard (and slow) way. | 1131 ;; Must do it the hard (and slow) way. |
1108 read-file-name-predicate))) | 1132 pred))) |
1109 (let ((default-directory realdir)) | 1133 (let ((default-directory (expand-file-name realdir))) |
1110 (dolist (tem all) | 1134 (dolist (tem all) |
1111 (if (funcall pred tem) (push tem comp)))) | 1135 (if (funcall pred tem) (push tem comp)))) |
1112 (setq all (nreverse comp)))) | 1136 (setq all (nreverse comp)))) |
1113 | 1137 |
1114 all)) | 1138 all)))))))) |
1139 | |
1140 (defvar read-file-name-predicate nil | |
1141 "Current predicate used by `read-file-name-internal'.") | |
1142 (make-obsolete-variable 'read-file-name-predicate | |
1143 "use the regular PRED argument" "23.2") | |
1144 | |
1145 (defun completion--file-name-table (string pred action) | |
1146 "Internal subroutine for `read-file-name'. Do not call this. | |
1147 This is a completion table for file names, like `completion-file-name-table' | |
1148 except that it passes the file name through `substitute-in-file-name'." | |
1149 (cond | |
1150 ((eq (car-safe action) 'boundaries) | |
1151 ;; For the boundaries, we can't really delegate to | |
1152 ;; completion-file-name-table and then fix them up, because it | |
1153 ;; would require us to track the relationship between `str' and | |
1154 ;; `string', which is difficult. And in any case, if | |
1155 ;; substitute-in-file-name turns "fo-$TO-ba" into "fo-o/b-ba", there's | |
1156 ;; no way for us to return proper boundaries info, because the | |
1157 ;; boundary is not (yet) in `string'. | |
1158 (let ((start (length (file-name-directory string))) | |
1159 (end (string-match-p "/" (cdr action)))) | |
1160 (list* 'boundaries start end))) | |
1115 | 1161 |
1116 (t | 1162 (t |
1117 ;; Only other case actually used is ACTION = lambda. | 1163 (let* ((default-directory |
1118 (let ((default-directory dir)) | 1164 (if (stringp pred) |
1119 (funcall (or read-file-name-predicate 'file-exists-p) str)))))))) | 1165 ;; It used to be that `pred' was abused to pass `dir' |
1166 ;; as an argument. | |
1167 (prog1 (file-name-as-directory (expand-file-name pred)) | |
1168 (setq pred nil)) | |
1169 default-directory)) | |
1170 (str (condition-case nil | |
1171 (substitute-in-file-name string) | |
1172 (error string))) | |
1173 (comp (completion-file-name-table | |
1174 str (or pred read-file-name-predicate) action))) | |
1175 | |
1176 (cond | |
1177 ((stringp comp) | |
1178 ;; Requote the $s before returning the completion. | |
1179 (minibuffer--double-dollars comp)) | |
1180 ((and (null action) comp | |
1181 ;; Requote the $s before checking for changes. | |
1182 (setq str (minibuffer--double-dollars str)) | |
1183 (not (string-equal string str))) | |
1184 ;; If there's no real completion, but substitute-in-file-name | |
1185 ;; changed the string, then return the new string. | |
1186 str) | |
1187 (t comp)))))) | |
1120 | 1188 |
1121 (defalias 'read-file-name-internal | 1189 (defalias 'read-file-name-internal |
1122 (completion-table-in-turn 'completion--embedded-envvar-table | 1190 (completion-table-in-turn 'completion--embedded-envvar-table |
1123 'completion--file-name-table) | 1191 'completion--file-name-table) |
1124 "Internal subroutine for `read-file-name'. Do not call this.") | 1192 "Internal subroutine for `read-file-name'. Do not call this.") |
1125 | 1193 |
1126 (defvar read-file-name-function nil | 1194 (defvar read-file-name-function nil |
1127 "If this is non-nil, `read-file-name' does its work by calling this function.") | 1195 "If this is non-nil, `read-file-name' does its work by calling this function.") |
1128 | |
1129 (defvar read-file-name-predicate nil | |
1130 "Current predicate used by `read-file-name-internal'.") | |
1131 | 1196 |
1132 (defcustom read-file-name-completion-ignore-case | 1197 (defcustom read-file-name-completion-ignore-case |
1133 (if (memq system-type '(ms-dos windows-nt darwin cygwin)) | 1198 (if (memq system-type '(ms-dos windows-nt darwin cygwin)) |
1134 t nil) | 1199 t nil) |
1135 "Non-nil means when reading a file name completion ignores case." | 1200 "Non-nil means when reading a file name completion ignores case." |
1225 (if read-file-name-function | 1290 (if read-file-name-function |
1226 (funcall read-file-name-function | 1291 (funcall read-file-name-function |
1227 prompt dir default-filename mustmatch initial predicate) | 1292 prompt dir default-filename mustmatch initial predicate) |
1228 (let ((completion-ignore-case read-file-name-completion-ignore-case) | 1293 (let ((completion-ignore-case read-file-name-completion-ignore-case) |
1229 (minibuffer-completing-file-name t) | 1294 (minibuffer-completing-file-name t) |
1230 (read-file-name-predicate (or predicate 'file-exists-p)) | 1295 (pred (or predicate 'file-exists-p)) |
1231 (add-to-history nil)) | 1296 (add-to-history nil)) |
1232 | 1297 |
1233 (let* ((val | 1298 (let* ((val |
1234 (if (not (next-read-file-uses-dialog-p)) | 1299 (if (not (next-read-file-uses-dialog-p)) |
1235 ;; We used to pass `dir' to `read-file-name-internal' by | 1300 ;; We used to pass `dir' to `read-file-name-internal' by |
1240 (lexical-let ((dir (file-name-as-directory | 1305 (lexical-let ((dir (file-name-as-directory |
1241 (expand-file-name dir)))) | 1306 (expand-file-name dir)))) |
1242 (minibuffer-with-setup-hook | 1307 (minibuffer-with-setup-hook |
1243 (lambda () (setq default-directory dir)) | 1308 (lambda () (setq default-directory dir)) |
1244 (completing-read prompt 'read-file-name-internal | 1309 (completing-read prompt 'read-file-name-internal |
1245 nil mustmatch insdef 'file-name-history | 1310 pred mustmatch insdef |
1246 default-filename))) | 1311 'file-name-history default-filename))) |
1247 ;; If DEFAULT-FILENAME not supplied and DIR contains | 1312 ;; If DEFAULT-FILENAME not supplied and DIR contains |
1248 ;; a file name, split it. | 1313 ;; a file name, split it. |
1249 (let ((file (file-name-nondirectory dir)) | 1314 (let ((file (file-name-nondirectory dir)) |
1250 ;; When using a dialog, revert to nil and non-nil | 1315 ;; When using a dialog, revert to nil and non-nil |
1251 ;; interpretation of mustmatch. confirm options | 1316 ;; interpretation of mustmatch. confirm options |
1252 ;; need to be interpreted as nil, otherwise | 1317 ;; need to be interpreted as nil, otherwise |
1253 ;; it is impossible to create new files using | 1318 ;; it is impossible to create new files using |
1254 ;; dialogs with the default settings. | 1319 ;; dialogs with the default settings. |
1255 (dialog-mustmatch | 1320 (dialog-mustmatch |
1256 (and (not (eq mustmatch 'confirm)) | 1321 (not (memq mustmatch |
1257 (not (eq mustmatch 'confirm-after-completion)) | 1322 '(nil confirm confirm-after-completion))))) |
1258 mustmatch))) | |
1259 (when (and (not default-filename) | 1323 (when (and (not default-filename) |
1260 (not (zerop (length file)))) | 1324 (not (zerop (length file)))) |
1261 (setq default-filename file) | 1325 (setq default-filename file) |
1262 (setq dir (file-name-directory dir))) | 1326 (setq dir (file-name-directory dir))) |
1263 (if default-filename | 1327 (if default-filename |