aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStefan Monnier2009-10-22 15:17:48 +0000
committerStefan Monnier2009-10-22 15:17:48 +0000
commit3b067af114b9a47321b173f37ef69edc7e2df8f0 (patch)
tree99fba506890e994a3fa0e71f18424479760efa9f
parent550d95a079c3b5b123c1293ef6f10824b57dc402 (diff)
downloademacs-3b067af114b9a47321b173f37ef69edc7e2df8f0.tar.gz
emacs-3b067af114b9a47321b173f37ef69edc7e2df8f0.zip
Allow the use of completion-tables.
(pcomplete-std-complete): New command. (pcomplete-dirs-or-entries): Use a single call to pcomplete-entries. (pcomplete--here): Use a function for `form' rather than an expression, so it can be byte-compiled. (pcomplete-here, pcomplete-here*): Adjust accordingly. Add edebug declaration. (pcomplete-show-completions): Remove unused var `curbuf'. (pcomplete-do-complete, pcomplete-stub): Don't assume `completions' is a list of strings any more.
-rw-r--r--lisp/ChangeLog13
-rw-r--r--lisp/pcomplete.el308
2 files changed, 185 insertions, 136 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 85449aea396..845fdf11be2 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,16 @@
12009-10-22 Stefan Monnier <monnier@iro.umontreal.ca>
2
3 * pcomplete.el: Allow the use of completion-tables.
4 (pcomplete-std-complete): New command.
5 (pcomplete-dirs-or-entries): Use a single call to pcomplete-entries.
6 (pcomplete--here): Use a function for `form' rather than an expression,
7 so it can be byte-compiled.
8 (pcomplete-here, pcomplete-here*): Adjust accordingly.
9 Add edebug declaration.
10 (pcomplete-show-completions): Remove unused var `curbuf'.
11 (pcomplete-do-complete, pcomplete-stub):
12 Don't assume `completions' is a list of strings any more.
13
12009-10-22 Juanma Barranquero <lekktu@gmail.com> 142009-10-22 Juanma Barranquero <lekktu@gmail.com>
2 15
3 * find-dired.el (find-name-arg): Fix typo in docstring. 16 * find-dired.el (find-name-arg): Fix typo in docstring.
diff --git a/lisp/pcomplete.el b/lisp/pcomplete.el
index c24f3cedae5..ae2ef4b49ed 100644
--- a/lisp/pcomplete.el
+++ b/lisp/pcomplete.el
@@ -60,8 +60,9 @@
60;; it means no completions were available. 60;; it means no completions were available.
61;; 61;;
62;; @ In order to provide completions, they must throw the tag 62;; @ In order to provide completions, they must throw the tag
63;; `pcomplete-completions'. The value must be the list of possible 63;; `pcomplete-completions'. The value must be a completion table
64;; completions for the final argument. 64;; (i.e. a table that can be passed to try-completion and friends)
65;; for the final argument.
65;; 66;;
66;; @ To simplify completion function logic, the tag `pcompleted' may 67;; @ To simplify completion function logic, the tag `pcompleted' may
67;; be thrown with a value of nil in order to abort the function. It 68;; be thrown with a value of nil in order to abort the function. It
@@ -118,7 +119,7 @@
118 119
119;;; Code: 120;;; Code:
120 121
121(provide 'pcomplete) 122(eval-when-compile (require 'cl))
122 123
123(defgroup pcomplete nil 124(defgroup pcomplete nil
124 "Programmable completion." 125 "Programmable completion."
@@ -373,7 +374,7 @@ completion functions list (it should occur fairly early in the list)."
373 (setq pcomplete-current-completions 374 (setq pcomplete-current-completions
374 (cdr pcomplete-current-completions))) 375 (cdr pcomplete-current-completions)))
375 (pcomplete-insert-entry pcomplete-last-completion-stub 376 (pcomplete-insert-entry pcomplete-last-completion-stub
376 (car pcomplete-current-completions) 377 (car pcomplete-current-completions)
377 nil pcomplete-last-completion-raw)) 378 nil pcomplete-last-completion-raw))
378 (setq pcomplete-current-completions nil 379 (setq pcomplete-current-completions nil
379 pcomplete-last-completion-raw nil) 380 pcomplete-last-completion-raw nil)
@@ -393,6 +394,41 @@ completion functions list (it should occur fairly early in the list)."
393 '(sole shortest)) 394 '(sole shortest))
394 pcomplete-last-completion-raw)))))) 395 pcomplete-last-completion-raw))))))
395 396
397(defun pcomplete-std-complete ()
398 "Provide standard completion using pcomplete's completion tables.
399Same as `pcomplete' but using the standard completion UI."
400 (interactive)
401 ;; FIXME: it fails to unquote/requote the arguments.
402 ;; FIXME: it doesn't implement paring.
403 ;; FIXME: when we bring up *Completions* we never bring it back down.
404 (catch 'pcompleted
405 (let* ((pcomplete-stub)
406 pcomplete-seen pcomplete-norm-func
407 pcomplete-args pcomplete-last pcomplete-index
408 (pcomplete-autolist pcomplete-autolist)
409 (pcomplete-suffix-list pcomplete-suffix-list)
410 ;; Apparently the vars above are global vars modified by
411 ;; side-effects, whereas pcomplete-completions is the core
412 ;; function that finds the chunk of text to complete
413 ;; (returned indirectly in pcomplete-stub) and the set of
414 ;; possible completions.
415 (completions (pcomplete-completions))
416 ;; The pcomplete code seems to presume that pcomplete-stub
417 ;; is always the text before point.
418 (ol (make-overlay (- (point) (length pcomplete-stub))
419 (point) nil nil t))
420 (minibuffer-completion-table
421 ;; Add a space at the end of completion. Use a terminator-regexp
422 ;; that never matches since the terminator cannot appear
423 ;; within the completion field anyway.
424 (apply-partially 'completion-table-with-terminator
425 '(" " . "\\`a\\`") completions))
426 (minibuffer-completion-predicate nil))
427 (overlay-put ol 'field 'pcomplete)
428 (unwind-protect
429 (call-interactively 'minibuffer-complete)
430 (delete-overlay ol)))))
431
396;;;###autoload 432;;;###autoload
397(defun pcomplete-reverse () 433(defun pcomplete-reverse ()
398 "If cycling completion is in use, cycle backwards." 434 "If cycling completion is in use, cycle backwards."
@@ -424,12 +460,12 @@ This will modify the current buffer."
424 (pcomplete-expand-only-p t)) 460 (pcomplete-expand-only-p t))
425 (pcomplete) 461 (pcomplete)
426 (when (and pcomplete-current-completions 462 (when (and pcomplete-current-completions
427 (> (length pcomplete-current-completions) 0)) 463 (> (length pcomplete-current-completions) 0)) ;??
428 (delete-backward-char pcomplete-last-completion-length) 464 (delete-backward-char pcomplete-last-completion-length)
429 (while pcomplete-current-completions 465 (while pcomplete-current-completions
430 (unless (pcomplete-insert-entry 466 (unless (pcomplete-insert-entry
431 "" (car pcomplete-current-completions) t 467 "" (car pcomplete-current-completions) t
432 pcomplete-last-completion-raw) 468 pcomplete-last-completion-raw)
433 (insert-and-inherit pcomplete-termination-string)) 469 (insert-and-inherit pcomplete-termination-string))
434 (setq pcomplete-current-completions 470 (setq pcomplete-current-completions
435 (cdr pcomplete-current-completions)))))) 471 (cdr pcomplete-current-completions))))))
@@ -599,7 +635,7 @@ this is `comint-dynamic-complete-functions'."
599 635
600;;;###autoload 636;;;###autoload
601(defun pcomplete-shell-setup () 637(defun pcomplete-shell-setup ()
602 "Setup shell-mode to use pcomplete." 638 "Setup `shell-mode' to use pcomplete."
603 (pcomplete-comint-setup 'shell-dynamic-complete-functions)) 639 (pcomplete-comint-setup 'shell-dynamic-complete-functions))
604 640
605(declare-function comint-bol "comint" (&optional arg)) 641(declare-function comint-bol "comint" (&optional arg))
@@ -699,13 +735,15 @@ Magic characters are those in `pcomplete-arg-quote-list'."
699 735
700(defsubst pcomplete-dirs-or-entries (&optional regexp predicate) 736(defsubst pcomplete-dirs-or-entries (&optional regexp predicate)
701 "Return either directories, or qualified entries." 737 "Return either directories, or qualified entries."
702 (append (let ((pcomplete-stub pcomplete-stub)) 738 ;; FIXME: pcomplete-entries doesn't return a list any more.
703 (pcomplete-entries 739 (pcomplete-entries
704 regexp (or predicate 740 nil
705 (function 741 (lexical-let ((re regexp)
706 (lambda (path) 742 (pred predicate))
707 (not (file-directory-p path))))))) 743 (lambda (f)
708 (pcomplete-entries nil 'file-directory-p))) 744 (or (file-directory-p f)
745 (and (if (not re) t (string-match re f))
746 (if (not pred) t (funcall pred f))))))))
709 747
710(defun pcomplete-entries (&optional regexp predicate) 748(defun pcomplete-entries (&optional regexp predicate)
711 "Complete against a list of directory candidates. 749 "Complete against a list of directory candidates.
@@ -873,7 +911,7 @@ See the documentation for `pcomplete-here'."
873 (setq pcomplete-seen nil) 911 (setq pcomplete-seen nil)
874 (unless (eq paring t) 912 (unless (eq paring t)
875 (let ((arg (pcomplete-arg))) 913 (let ((arg (pcomplete-arg)))
876 (unless (not (stringp arg)) 914 (when (stringp arg)
877 (setq pcomplete-seen 915 (setq pcomplete-seen
878 (cons (if paring 916 (cons (if paring
879 (funcall paring arg) 917 (funcall paring arg)
@@ -891,12 +929,17 @@ See the documentation for `pcomplete-here'."
891 (setq pcomplete-norm-func (or paring 'file-truename))) 929 (setq pcomplete-norm-func (or paring 'file-truename)))
892 (unless form-only 930 (unless form-only
893 (run-hooks 'pcomplete-try-first-hook)) 931 (run-hooks 'pcomplete-try-first-hook))
894 (throw 'pcomplete-completions (eval form)))) 932 (throw 'pcomplete-completions
933 (if (functionp form)
934 (funcall form)
935 ;; Old calling convention, might still be used by files
936 ;; byte-compiled with the older code.
937 (eval form)))))
895 938
896(defmacro pcomplete-here (&optional form stub paring form-only) 939(defmacro pcomplete-here (&optional form stub paring form-only)
897 "Complete against the current argument, if at the end. 940 "Complete against the current argument, if at the end.
898If completion is to be done here, evaluate FORM to generate the list 941If completion is to be done here, evaluate FORM to generate the completion
899of strings which will be used for completion purposes. If STUB is a 942table which will be used for completion purposes. If STUB is a
900string, use it as the completion stub instead of the default (which is 943string, use it as the completion stub instead of the default (which is
901the entire text of the current argument). 944the entire text of the current argument).
902 945
@@ -904,7 +947,7 @@ For an example of when you might want to use STUB: if the current
904argument text is 'long-path-name/', you don't want the completions 947argument text is 'long-path-name/', you don't want the completions
905list display to be cluttered by 'long-path-name/' appearing at the 948list display to be cluttered by 'long-path-name/' appearing at the
906beginning of every alternative. Not only does this make things less 949beginning of every alternative. Not only does this make things less
907intelligle, but it is also inefficient. Yet, if the completion list 950intelligible, but it is also inefficient. Yet, if the completion list
908does not begin with this string for every entry, the current argument 951does not begin with this string for every entry, the current argument
909won't complete correctly. 952won't complete correctly.
910 953
@@ -923,11 +966,14 @@ cleared.
923If FORM-ONLY is non-nil, only the result of FORM will be used to 966If FORM-ONLY is non-nil, only the result of FORM will be used to
924generate the completions list. This means that the hook 967generate the completions list. This means that the hook
925`pcomplete-try-first-hook' will not be run." 968`pcomplete-try-first-hook' will not be run."
926 `(pcomplete--here (quote ,form) ,stub ,paring ,form-only)) 969 (declare (debug t))
970 `(pcomplete--here (lambda () ,form) ,stub ,paring ,form-only))
971
927 972
928(defmacro pcomplete-here* (&optional form stub form-only) 973(defmacro pcomplete-here* (&optional form stub form-only)
929 "An alternate form which does not participate in argument paring." 974 "An alternate form which does not participate in argument paring."
930 `(pcomplete-here ,form ,stub t ,form-only)) 975 (declare (debug t))
976 `(pcomplete-here (lambda () ,form) ,stub t ,form-only))
931 977
932;; display support 978;; display support
933 979
@@ -958,44 +1004,43 @@ generate the completions list. This means that the hook
958(defun pcomplete-show-completions (completions) 1004(defun pcomplete-show-completions (completions)
959 "List in help buffer sorted COMPLETIONS. 1005 "List in help buffer sorted COMPLETIONS.
960Typing SPC flushes the help buffer." 1006Typing SPC flushes the help buffer."
961 (let* ((curbuf (current-buffer))) 1007 (when pcomplete-window-restore-timer
962 (when pcomplete-window-restore-timer 1008 (cancel-timer pcomplete-window-restore-timer)
963 (cancel-timer pcomplete-window-restore-timer) 1009 (setq pcomplete-window-restore-timer nil))
964 (setq pcomplete-window-restore-timer nil)) 1010 (unless pcomplete-last-window-config
965 (unless pcomplete-last-window-config 1011 (setq pcomplete-last-window-config (current-window-configuration)))
966 (setq pcomplete-last-window-config (current-window-configuration))) 1012 (with-output-to-temp-buffer "*Completions*"
967 (with-output-to-temp-buffer "*Completions*" 1013 (display-completion-list completions))
968 (display-completion-list completions)) 1014 (message "Hit space to flush")
969 (message "Hit space to flush") 1015 (let (event)
970 (let (event) 1016 (prog1
971 (prog1 1017 (catch 'done
972 (catch 'done 1018 (while (with-current-buffer (get-buffer "*Completions*")
973 (while (with-current-buffer (get-buffer "*Completions*") 1019 (setq event (pcomplete-read-event)))
974 (setq event (pcomplete-read-event))) 1020 (cond
975 (cond 1021 ((pcomplete-event-matches-key-specifier-p event ?\s)
976 ((pcomplete-event-matches-key-specifier-p event ?\s) 1022 (set-window-configuration pcomplete-last-window-config)
977 (set-window-configuration pcomplete-last-window-config) 1023 (setq pcomplete-last-window-config nil)
978 (setq pcomplete-last-window-config nil) 1024 (throw 'done nil))
979 (throw 'done nil)) 1025 ((or (pcomplete-event-matches-key-specifier-p event 'tab)
980 ((or (pcomplete-event-matches-key-specifier-p event 'tab) 1026 ;; Needed on a terminal
981 ;; Needed on a terminal 1027 (pcomplete-event-matches-key-specifier-p event 9))
982 (pcomplete-event-matches-key-specifier-p event 9)) 1028 (let ((win (or (get-buffer-window "*Completions*" 0)
983 (let ((win (or (get-buffer-window "*Completions*" 0) 1029 (display-buffer "*Completions*"
984 (display-buffer "*Completions*" 1030 'not-this-window))))
985 'not-this-window)))) 1031 (with-selected-window win
986 (with-selected-window win 1032 (if (pos-visible-in-window-p (point-max))
987 (if (pos-visible-in-window-p (point-max)) 1033 (goto-char (point-min))
988 (goto-char (point-min)) 1034 (scroll-up))))
989 (scroll-up)))) 1035 (message ""))
990 (message "")) 1036 (t
991 (t 1037 (setq unread-command-events (list event))
992 (setq unread-command-events (list event)) 1038 (throw 'done nil)))))
993 (throw 'done nil))))) 1039 (if (and pcomplete-last-window-config
994 (if (and pcomplete-last-window-config 1040 pcomplete-restore-window-delay)
995 pcomplete-restore-window-delay) 1041 (setq pcomplete-window-restore-timer
996 (setq pcomplete-window-restore-timer 1042 (run-with-timer pcomplete-restore-window-delay nil
997 (run-with-timer pcomplete-restore-window-delay nil 1043 'pcomplete-restore-windows))))))
998 'pcomplete-restore-windows)))))))
999 1044
1000;; insert completion at point 1045;; insert completion at point
1001 1046
@@ -1043,40 +1088,25 @@ extra checking, and munging of the COMPLETIONS list."
1043 (message "No completions of %s" stub) 1088 (message "No completions of %s" stub)
1044 (message "No completions"))) 1089 (message "No completions")))
1045 ;; pare it down, if applicable 1090 ;; pare it down, if applicable
1046 (if (and pcomplete-use-paring pcomplete-seen) 1091 (when (and pcomplete-use-paring pcomplete-seen)
1047 (let* ((arg (pcomplete-arg)) 1092 (setq pcomplete-seen
1048 (prefix 1093 (mapcar 'directory-file-name pcomplete-seen))
1049 (file-name-as-directory 1094 (dolist (p pcomplete-seen)
1050 (funcall pcomplete-norm-func 1095 (add-to-list 'pcomplete-seen
1051 (substring arg 0 (- (length arg) 1096 (funcall pcomplete-norm-func p)))
1052 (length pcomplete-stub))))))) 1097 (setq completions
1053 (setq pcomplete-seen 1098 (apply-partially 'completion-table-with-predicate
1054 (mapcar 'directory-file-name pcomplete-seen)) 1099 completions
1055 (let ((p pcomplete-seen)) 1100 (lambda (f)
1056 (while p 1101 (not (member
1057 (add-to-list 'pcomplete-seen 1102 (funcall pcomplete-norm-func
1058 (funcall pcomplete-norm-func (car p))) 1103 (directory-file-name f))
1059 (setq p (cdr p)))) 1104 pcomplete-seen)))
1060 (setq completions 1105 'strict)))
1061 (mapcar
1062 (function
1063 (lambda (elem)
1064 (file-relative-name elem prefix)))
1065 (pcomplete-pare-list
1066 (mapcar
1067 (function
1068 (lambda (elem)
1069 (expand-file-name elem prefix)))
1070 completions)
1071 pcomplete-seen
1072 (function
1073 (lambda (elem)
1074 (member (directory-file-name
1075 (funcall pcomplete-norm-func elem))
1076 pcomplete-seen))))))))
1077 ;; OK, we've got a list of completions. 1106 ;; OK, we've got a list of completions.
1078 (if pcomplete-show-list 1107 (if pcomplete-show-list
1079 (pcomplete-show-completions completions) 1108 ;; FIXME: pay attention to boundaries.
1109 (pcomplete-show-completions (all-completions stub completions))
1080 (pcomplete-stub stub completions)))) 1110 (pcomplete-stub stub completions))))
1081 1111
1082(defun pcomplete-stub (stub candidates &optional cycle-p) 1112(defun pcomplete-stub (stub candidates &optional cycle-p)
@@ -1093,43 +1123,47 @@ Returns `listed' if a completion listing was shown.
1093 1123
1094See also `pcomplete-filename'." 1124See also `pcomplete-filename'."
1095 (let* ((completion-ignore-case pcomplete-ignore-case) 1125 (let* ((completion-ignore-case pcomplete-ignore-case)
1096 (candidates (mapcar 'list candidates)) 1126 (completions (all-completions stub candidates))
1097 (completions (all-completions stub candidates))) 1127 (entry (try-completion stub candidates))
1098 (let (result entry) 1128 result)
1099 (cond 1129 (cond
1100 ((null completions) 1130 ((null entry)
1101 (if (and stub (> (length stub) 0)) 1131 (if (and stub (> (length stub) 0))
1102 (message "No completions of %s" stub) 1132 (message "No completions of %s" stub)
1103 (message "No completions"))) 1133 (message "No completions")))
1104 ((= 1 (length completions)) 1134 ((eq entry t)
1105 (setq entry (car completions)) 1135 (setq entry stub)
1106 (if (string-equal entry stub) 1136 (message "Sole completion")
1107 (message "Sole completion")) 1137 (setq result 'sole))
1108 (setq result 'sole)) 1138 ((= 1 (length completions))
1109 ((and pcomplete-cycle-completions 1139 (setq result 'sole))
1110 (or cycle-p 1140 ((and pcomplete-cycle-completions
1111 (not pcomplete-cycle-cutoff-length) 1141 (or cycle-p
1112 (<= (length completions) 1142 (not pcomplete-cycle-cutoff-length)
1113 pcomplete-cycle-cutoff-length))) 1143 (<= (length completions)
1114 (setq entry (car completions) 1144 pcomplete-cycle-cutoff-length)))
1115 pcomplete-current-completions completions)) 1145 (let ((bound (car (completion-boundaries stub candidates nil ""))))
1116 (t ; There's no unique completion; use longest substring 1146 (unless (zerop bound)
1117 (setq entry (try-completion stub candidates)) 1147 (setq completions (mapcar (lambda (c) (concat (substring stub 0 bound) c))
1118 (cond ((and pcomplete-recexact 1148 completions)))
1119 (string-equal stub entry) 1149 (setq entry (car completions)
1120 (member entry completions)) 1150 pcomplete-current-completions completions)))
1121 ;; It's not unique, but user wants shortest match. 1151 ((and pcomplete-recexact
1122 (message "Completed shortest") 1152 (string-equal stub entry)
1123 (setq result 'shortest)) 1153 (member entry completions))
1124 ((or pcomplete-autolist 1154 ;; It's not unique, but user wants shortest match.
1125 (string-equal stub entry)) 1155 (message "Completed shortest")
1126 ;; It's not unique, list possible completions. 1156 (setq result 'shortest))
1127 (pcomplete-show-completions completions) 1157 ((or pcomplete-autolist
1128 (setq result 'listed)) 1158 (string-equal stub entry))
1129 (t 1159 ;; It's not unique, list possible completions.
1130 (message "Partially completed") 1160 ;; FIXME: pay attention to boundaries.
1131 (setq result 'partial))))) 1161 (pcomplete-show-completions completions)
1132 (cons result entry)))) 1162 (setq result 'listed))
1163 (t
1164 (message "Partially completed")
1165 (setq result 'partial)))
1166 (cons result entry)))
1133 1167
1134;; context sensitive help 1168;; context sensitive help
1135 1169
@@ -1194,14 +1228,16 @@ Returns the resultant list."
1194;; create a set of aliases which allow completion functions to be not 1228;; create a set of aliases which allow completion functions to be not
1195;; quite so verbose 1229;; quite so verbose
1196 1230
1197;; jww (1999-10-20): are these a good idea? 1231;;; jww (1999-10-20): are these a good idea?
1198; (defalias 'pc-here 'pcomplete-here) 1232;; (defalias 'pc-here 'pcomplete-here)
1199; (defalias 'pc-test 'pcomplete-test) 1233;; (defalias 'pc-test 'pcomplete-test)
1200; (defalias 'pc-opt 'pcomplete-opt) 1234;; (defalias 'pc-opt 'pcomplete-opt)
1201; (defalias 'pc-match 'pcomplete-match) 1235;; (defalias 'pc-match 'pcomplete-match)
1202; (defalias 'pc-match-string 'pcomplete-match-string) 1236;; (defalias 'pc-match-string 'pcomplete-match-string)
1203; (defalias 'pc-match-beginning 'pcomplete-match-beginning) 1237;; (defalias 'pc-match-beginning 'pcomplete-match-beginning)
1204; (defalias 'pc-match-end 'pcomplete-match-end) 1238;; (defalias 'pc-match-end 'pcomplete-match-end)
1239
1240(provide 'pcomplete)
1205 1241
1206;; arch-tag: ae32ef2d-dbed-4244-8b0f-cf5a2a3b07a4 1242;; arch-tag: ae32ef2d-dbed-4244-8b0f-cf5a2a3b07a4
1207;;; pcomplete.el ends here 1243;;; pcomplete.el ends here