annotate lisp/modes/list-mode.el @ 124:9b50b4588a93 r20-1b15

Import from CVS: tag r20-1b15
author cvs
date Mon, 13 Aug 2007 09:26:39 +0200
parents 360340f9fd5f
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; list-mode.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;; Copyright (C) 1996 Ben Wing.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;; This file is part of XEmacs.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;; XEmacs is free software; you can redistribute it and/or modify it
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;; under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;; the Free Software Foundation; either version 2, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ;; any later version.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;; XEmacs is distributed in the hope that it will be useful, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;; General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;; You should have received a copy of the GNU General Public License
16
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 2
diff changeset
18 ;; along with XEmacs; see the file COPYING. If not, write to the
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 2
diff changeset
19 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
0293115a14e9 Import from CVS: tag r19-15b91
cvs
parents: 2
diff changeset
20 ;; Boston, MA 02111-1307, USA.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;; Cleanup, merging with FSF by Ben Wing, January 1996
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 (defvar list-mode-extent nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 (make-variable-buffer-local 'list-mode-extent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 (defvar list-mode-map nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 "Local map for buffers containing lists of items.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 (or list-mode-map
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 (let ((map (setq list-mode-map (make-sparse-keymap 'list-mode-map))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 (suppress-keymap map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 (define-key map 'button2up 'list-mode-item-mouse-selected)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 (define-key map 'button2 'undefined)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 (define-key map "\C-m" 'list-mode-item-keyboard-selected)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 (substitute-key-definition 'forward-char 'next-list-mode-item map
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 global-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 (substitute-key-definition 'backward-char 'previous-list-mode-item map
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 global-map)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 (defun list-mode ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 "Major mode for buffer containing lists of items."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 (kill-all-local-variables)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 (use-local-map list-mode-map)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 (setq mode-name "List")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (setq major-mode 'list-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 (make-local-hook 'post-command-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (add-hook 'post-command-hook 'set-list-mode-extent nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (make-local-hook 'pre-command-hook)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (add-hook 'pre-command-hook 'list-mode-extent-pre-hook nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (make-local-variable 'next-line-add-newlines)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (setq next-line-add-newlines nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 (setq list-mode-extent nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 (set-specifier text-cursor-visible-p nil (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (setq buffer-read-only t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (run-hooks 'list-mode-hook))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 ;; List mode is suitable only for specially formatted data.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (put 'list-mode 'mode-class 'special)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (defvar list-mode-extent-old-point nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 "The value of point when pre-command-hook is called.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 Used to determine the direction of motion.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (make-variable-buffer-local 'list-mode-extent-old-point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 (defun list-mode-extent-pre-hook ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (setq list-mode-extent-old-point (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 ;(setq atomic-extent-goto-char-p nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (defun set-list-mode-extent ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 "Move to the closest list item and set up the extent for it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 This is called from `post-command-hook'."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (cond ((get-char-property (point) 'list-mode-item))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 ((and (> (point) (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (get-char-property (1- (point)) 'list-mode-item))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 (goto-char (1- (point))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 (let ((pos (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 dirflag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 ;this fucks things up more than it helps.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;atomic-extent-goto-char-p as currently defined is all broken,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 ;since it will be triggered if the command *ever* runs goto-char!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 ;(if atomic-extent-goto-char-p
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 ; (setq dirflag 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (if (and list-mode-extent-old-point
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (> pos list-mode-extent-old-point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (setq dirflag 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (setq dirflag -1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (next-list-mode-item dirflag)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 (or (get-char-property (point) 'list-mode-item)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 (next-list-mode-item (- dirflag))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (or (and list-mode-extent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (eq (current-buffer) (extent-object list-mode-extent)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (setq list-mode-extent (make-extent nil nil (current-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (set-extent-face list-mode-extent 'list-mode-item-selected)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (let ((ex (extent-at (point) nil 'list-mode-item nil 'at)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (if ex
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102 (set-extent-endpoints list-mode-extent
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103 (extent-start-position ex)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (extent-end-position ex))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (auto-show-make-region-visible (extent-start-position ex)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106 (extent-end-position ex)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (detach-extent list-mode-extent))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 (defun previous-list-mode-item (n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 "Move to the previous item in list-mode."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (next-list-mode-item (- n)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (defun next-list-mode-item (n)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 "Move to the next item in list-mode.
108
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
116 With prefix argument N, move N items (negative N means move backward)."
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 (interactive "p")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (while (and (> n 0) (not (eobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (let ((prop (get-char-property (point) 'list-mode-item))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 (end (point-max)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 ;; If in a completion, move to the end of it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (if prop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (goto-char (next-single-property-change (point) 'list-mode-item
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 nil end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125 ;; Move to start of next one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 (goto-char (next-single-property-change (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 'list-mode-item nil end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 (setq n (1- n)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (while (and (< n 0) (not (bobp)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 (let ((prop (get-char-property (1- (point)) 'list-mode-item))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (end (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 ;; If in a completion, move to the start of it.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133 (if prop
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134 (goto-char (previous-single-property-change
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (point) 'list-mode-item nil end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 ;; Move to end of the previous completion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 (goto-char (previous-single-property-change (point) 'list-mode-item
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 nil end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 ;; Move to the start of that one.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 (goto-char (previous-single-property-change (point) 'list-mode-item nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 (setq n (1+ n))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 (defun list-mode-item-selected-1 (extent event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 (let ((func (extent-property extent 'list-mode-item-activate-callback))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 (user-data (extent-property extent 'list-mode-item-user-data)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 (if func
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 (funcall func event extent user-data))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 ;; we could make these two be just one function, but we want to be
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 ;; able to refer to them in DOC strings.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (defun list-mode-item-keyboard-selected ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (list-mode-item-selected-1 (extent-at (point) (current-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 'list-mode-item nil 'at)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (defun list-mode-item-mouse-selected (event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (interactive "e")
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 108
diff changeset
161 ;; Sometimes event-closest-point returns nil.
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 108
diff changeset
162 ;; So beep instead of bombing.
2
ac2d302a0011 Import from CVS: tag r19-15b2
cvs
parents: 0
diff changeset
163 (let ((point (event-closest-point event)))
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 108
diff changeset
164 (if point
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 108
diff changeset
165 (list-mode-item-selected-1 (extent-at point
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 108
diff changeset
166 (event-buffer event)
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 108
diff changeset
167 'list-mode-item nil 'at)
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 108
diff changeset
168 event)
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 108
diff changeset
169 (ding))))
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (defun add-list-mode-item (start end &optional buffer activate-callback
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 user-data)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 "Add a new list item in list-mode, from START to END in BUFFER.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 BUFFER defaults to the current buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 This works by creating an extent for the span of text in question.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 If ACTIVATE-CALLBACK is non-nil, it should be a function of three
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 arguments (EVENT EXTENT USER-DATA) that will be called when button2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 is pressed on the extent. USER-DATA comes from the optional
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 USER-DATA argument."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 (let ((extent (make-extent start end buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 (set-extent-property extent 'list-mode-item t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 (set-extent-property extent 'start-open t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 (if activate-callback
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 (set-extent-property extent 'mouse-face 'highlight)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (set-extent-property extent 'list-mode-item-activate-callback
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 activate-callback)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 (set-extent-property extent 'list-mode-item-user-data user-data)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 extent))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 ;; Define the major mode for lists of completions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 (defvar completion-highlight-first-word-only nil
108
360340f9fd5f Import from CVS: tag r20-1b6
cvs
parents: 70
diff changeset
196 "*Completion will only highlight the first blank delimited word if t.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 If the variable in not t or nil, the string is taken as a regexp to match for end
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 of highlight")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
200 (defvar completion-setup-hook nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
201 "Normal hook run at the end of setting up the text of a completion buffer.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 ; Unnecessary FSFmacs crock. We frob the extents directly in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204 ; display-completion-list, so no "heuristics" like this are necessary.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 ;(defvar completion-fixup-function nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 ; "A function to customize how completions are identified in completion lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 ;`completion-setup-function' calls this function with no arguments
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 ;each time it has found what it thinks is one completion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 ;Point is at the end of the completion in the completion list buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 ;If this function moves point, it can alter the end of that completion.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 (defvar completion-default-help-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 '(concat
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 (if (device-on-window-system-p)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 (substitute-command-keys
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 "Click \\<list-mode-map>\\[list-mode-item-mouse-selected] on a completion to select it.\n") "")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217 (substitute-command-keys
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 "Type \\<minibuffer-local-completion-map>\\[advertised-switch-to-completions] or \\[switch-to-completions] to move to this buffer, for keyboard selection.\n\n"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 "Form the evaluate to get a help string for completion lists.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 This string is inserted at the beginning of the buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 See `display-completion-list'.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 (defun display-completion-list (completions &rest cl-keys)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 "Display the list of completions, COMPLETIONS, using `standard-output'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 Each element may be just a symbol or string or may be a list of two
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 strings to be printed as if concatenated.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 Frob a mousable extent onto each completion. This extent has properties
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 'mouse-face (so it highlights when the mouse passes over it) and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 'list-mode-item (so it can be located).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 Keywords:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 :activate-callback (default is `default-choose-completion')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 See `add-list-mode-item'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 :user-data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 Value passed to activation callback.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 :window-width
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 If non-nil, width to use in displaying the list, instead of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 actual window's width.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 :help-string (default is the value of `completion-default-help-string')
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 Form to evaluate to get a string to insert at the beginning of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 the completion list buffer. This is evaluated when that buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 is the current buffer and after it has been put into
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 completion-list-mode.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 :reference-buffer (default is the current buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 This specifies the value of `completion-reference-buffer' in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 the completion buffer. This specifies the buffer (normally a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 minibuffer) that `default-choose-completion' will insert the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 completion into.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 At the end, run the normal hook `completion-setup-hook'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 It can find the completion buffer in `standard-output'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 If `completion-highlight-first-word-only' is non-nil, then only the start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 of the string is highlighted."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 ;; #### I18N3 should set standard-output to be (temporarily)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 ;; output-translating.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 (cl-parsing-keywords
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 ((:activate-callback 'default-choose-completion)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 :user-data
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 :reference-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 (:help-string completion-default-help-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 :window-width)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 (let ((old-buffer (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 (bufferp (bufferp standard-output)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (if bufferp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 (set-buffer standard-output))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 (if (null completions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 (princ (gettext
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 "There are no possible completions of what you have typed."))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 (let ((win-width
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
271 (or cl-window-width
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 (if bufferp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 ;; This needs fixing for the case of windows
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 ;; that aren't the same width's the frame.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 ;; Sadly, the window it will appear in is not known
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 ;; until after the text has been made.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 ;; We have to use last-nonminibuf-frame here
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 ;; and not selected-frame because if a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
280 ;; minibuffer-only frame is being used it will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 ;; be the selected-frame at the point this is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 ;; run. We keep the selected-frame call around
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 ;; just in case.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 (frame-width (or (last-nonminibuf-frame)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 (selected-frame)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 80))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 (let ((count 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 (max-width 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
289 ;; Find longest completion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 (let ((tail completions))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 (while tail
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 (let* ((elt (car tail))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 (len (cond ((stringp elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 (length elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 ((and (consp elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
296 (stringp (car elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
297 (stringp (car (cdr elt))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 (+ (length (car elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 (length (car (cdr elt)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 (t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 (signal 'wrong-type-argument
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 (list 'stringp elt))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 (if (> len max-width)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 (setq max-width len))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 (setq count (1+ count)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
306 tail (cdr tail)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 (setq max-width (+ 2 max-width)) ; at least two chars between cols
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 (let ((rows (let ((cols (min (/ win-width max-width) count)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 (if (<= cols 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 count
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 ;; re-space the columns
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 (setq max-width (/ win-width cols))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 (if (/= (% count cols) 0) ; want ceiling...
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 (1+ (/ count cols))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 (/ count cols)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 (princ (gettext "Possible completions are:"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319 (let ((tail completions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 (r 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 (regexp-string
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 (if (eq t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 completion-highlight-first-word-only)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 "[ \t]"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 completion-highlight-first-word-only)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 (while (< r rows)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 (terpri)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 (let ((indent 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 (column 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
330 (tail2 tail))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 (while tail2
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 (let ((elt (car tail2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 (if (/= indent 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 (if bufferp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 (indent-to indent 2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 (while (progn (write-char ?\ )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 (setq column (1+ column))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 (< column indent)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 (setq indent (+ indent max-width))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 (let ((start (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 ;; Frob some mousable extents in there too!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 (if (consp elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 (princ (car elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 (princ (car (cdr elt)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 (or bufferp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 (setq column
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349 (+ column
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 (length (car elt))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 (length (car (cdr elt)))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 (princ elt)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 (or bufferp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 (setq column (+ column (length
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 elt))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 (add-list-mode-item
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 (setq end (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 (or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362 (and completion-highlight-first-word-only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 (goto-char start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 (re-search-forward regexp-string end t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 end))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
367 nil cl-activate-callback cl-user-data)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 (goto-char end)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 (setq tail2 (nthcdr rows tail2)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 (setq tail (cdr tail)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 r (1+ r)))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 (if bufferp
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 (set-buffer old-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 (let ((mainbuf (or cl-reference-buffer (current-buffer))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 (set-buffer standard-output)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 (completion-list-mode)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 (make-local-variable 'completion-reference-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 (setq completion-reference-buffer mainbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380 ;;; The value 0 is right in most cases, but not for file name completion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 ;;; so this has to be turned off.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 ;;; (setq completion-base-size 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 (let ((buffer-read-only nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 (insert (eval cl-help-string)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
386 ;; unnecessary FSFmacs crock
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 ;;(forward-line 1)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 ;;(while (re-search-forward "[^ \t\n]+\\( [^ \t\n]+\\)*" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 ;; (let ((beg (match-beginning 0))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 ;; (end (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 ;; (if completion-fixup-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 ;; (funcall completion-fixup-function))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 ;; (put-text-property beg (point) 'mouse-face 'highlight)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 ;; (put-text-property beg (point) 'list-mode-item t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 ;; (goto-char end)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 (run-hooks 'completion-setup-hook)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 (defvar completion-display-completion-list-function 'display-completion-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 "Function to set up the list of completions in the completion buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 The function is called with one argument, the sorted list of completions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 Particular minibuffer interface functions (e.g. `read-file-name') may
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 want to change this. To do that, set a local value for this variable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404 in the minibuffer; that ensures that other minibuffer invocations will
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 not be affected.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 (defun minibuffer-completion-help ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 "Display a list of possible completions of the current minibuffer contents.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 The list of completions is determined by calling `all-completions',
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 passing it the current minibuffer contents, the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411 `minibuffer-completion-table', and the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 `minibuffer-completion-predicate'. The list is displayed by calling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 the value of `completion-display-completion-list-function' on the sorted
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 list of completions, with the standard output set to the completion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 buffer."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 (message "Making completion list...")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 (let ((completions (all-completions (buffer-string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 minibuffer-completion-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
420 minibuffer-completion-predicate)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
421 (message nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 (if (null completions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423 (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 (ding nil 'no-completion)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 (temp-minibuffer-message " [No completions]"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 (with-output-to-temp-buffer "*Completions*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 (funcall completion-display-completion-list-function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
428 (sort completions #'string-lessp))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
430 (define-derived-mode completion-list-mode list-mode
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 "Completion List"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
432 "Major mode for buffers showing lists of possible completions.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
433 Type \\<completion-list-mode-map>\\[choose-completion] in the completion list\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 to select the completion near point.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 Use \\<completion-list-mode-map>\\[mouse-choose-completion] to select one\
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 with the mouse."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437 (make-local-variable 'completion-base-size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 (setq completion-base-size nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 (let ((map completion-list-mode-map))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
441 (define-key map "\e\e\e" 'delete-completion-window)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
442 (define-key map "\C-g" 'minibuffer-keyboard-quit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
443 (define-key map "q" 'abort-recursive-edit)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444 (define-key map " " (lambda () (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 (select-window (minibuffer-window))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 (define-key map "\t" (lambda () (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 (select-window (minibuffer-window)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
449 (defvar completion-reference-buffer nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 "Record the buffer that was current when the completion list was requested.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
451 This is a local variable in the completion list buffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 Initial value is nil to avoid some compiler warnings.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
453
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
454 (defvar completion-base-size nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 "Number of chars at beginning of minibuffer not involved in completion.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 This is a local variable in the completion list buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 but it talks about the buffer in `completion-reference-buffer'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458 If this is nil, it means to compare text to determine which part
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 of the tail end of the buffer's text is involved in completion.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 (defun delete-completion-window ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
462 "Delete the completion list window.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
463 Go to the window from which completion was requested."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465 (let ((buf completion-reference-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 (delete-window (selected-window))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 (if (get-buffer-window buf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 (select-window (get-buffer-window buf)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
470 (defun completion-do-in-minibuffer ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 (interactive "_")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
472 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 (set-buffer (window-buffer (minibuffer-window)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
474 (call-interactively (key-binding (this-command-keys)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
475
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 (defun default-choose-completion (event extent buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 "Click on an alternative in the `*Completions*' buffer to choose it."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 (and (button-event-p event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479 ;; Give temporary modes such as isearch a chance to turn off.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 (run-hooks 'mouse-leave-buffer-hook))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 (or buffer (setq buffer (symbol-value-in-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 'completion-reference-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
483 (or (and (button-event-p event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
484 (event-buffer event))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 (current-buffer)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486 (save-selected-window
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 (and (button-event-p event)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 (select-window (event-window event)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 (if (and (one-window-p t 'selected-frame)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 (window-dedicated-p (selected-window)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
491 ;; This is a special buffer's frame
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 (iconify-frame (selected-frame))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
493 (or (window-dedicated-p (selected-window))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 (bury-buffer))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
495 (choose-completion-string (extent-string extent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 completion-base-size))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499 ;; Delete the longest partial match for STRING
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 ;; that can be found before POINT.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 (defun choose-completion-delete-max-match (string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 (let ((len (min (length string)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 (- (point) (point-min)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 (goto-char (- (point) (length string)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 (if completion-ignore-case
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 (setq string (downcase string)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 (while (and (> len 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 (let ((tail (buffer-substring (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 (+ (point) len))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
510 (if completion-ignore-case
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511 (setq tail (downcase tail)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 (not (string= tail (substring string 0 len)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 (setq len (1- len))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 (forward-char 1))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 (delete-char len)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 ;; Switch to BUFFER and insert the completion choice CHOICE.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 ;; BASE-SIZE, if non-nil, says how many characters of BUFFER's text
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 ;; to keep. If it is nil, use choose-completion-delete-max-match instead.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 (defun choose-completion-string (choice &optional buffer base-size)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 (let ((buffer (or buffer completion-reference-buffer)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522 ;; If BUFFER is a minibuffer, barf unless it's the currently
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 ;; active minibuffer.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 (if (and (string-match "\\` \\*Minibuf-[0-9]+\\*\\'" (buffer-name buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 (or (not (active-minibuffer-window))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526 (not (equal buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 (window-buffer (active-minibuffer-window))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 (error "Minibuffer is not active for completion")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 ;; Insert the completion into the buffer where completion was requested.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 (set-buffer buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 (if base-size
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 (delete-region (+ base-size (point-min)) (point))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 (choose-completion-delete-max-match choice))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 (insert choice)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 (remove-text-properties (- (point) (length choice)) (point)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 '(highlight nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 ;; Update point in the window that BUFFER is showing in.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 (let ((window (get-buffer-window buffer t)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 (set-window-point window (point)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 ;; If completing for the minibuffer, exit it with this choice.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 (and (equal buffer (window-buffer (minibuffer-window)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 minibuffer-completion-table
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 (exit-minibuffer)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 (define-key minibuffer-local-completion-map [prior]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 'switch-to-completions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 (define-key minibuffer-local-must-match-map [prior]
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 'switch-to-completions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 (define-key minibuffer-local-completion-map "\M-v"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 'advertised-switch-to-completions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 (define-key minibuffer-local-must-match-map "\M-v"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 'advertised-switch-to-completions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 (defalias 'advertised-switch-to-completions 'switch-to-completions)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 (defun switch-to-completions ()
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 "Select the completion list window."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 (interactive)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 ;; Make sure we have a completions window.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 (or (get-buffer-window "*Completions*")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 (minibuffer-completion-help))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561 (if (not (get-buffer-window "*Completions*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
562 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
563 (select-window (get-buffer-window "*Completions*"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 (goto-char (next-single-property-change (point-min) 'list-mode-item nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565 (point-max)))))