209
|
1 ;;; lib-complete.el --- Completion on the lisp search path
|
|
2
|
|
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) Mike Williams <mike-w@cs.aukuni.ac.nz> 1991
|
|
5
|
|
6 ;; Author: Mike Williams <mike-w@cs.aukuni.ac.nz>
|
|
7 ;; Maintainer: XEmacs Development Team
|
|
8 ;; Keywords: lisp, extensions, dumped
|
|
9 ;; Created: Sat Apr 20 17:47:21 1991
|
|
10
|
|
11 ;; This file is part of XEmacs.
|
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
14 ;; under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
|
17
|
|
18 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
21 ;; General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;; Boston, MA 02111-1307, USA.
|
|
27
|
|
28 ;;; Synched up with: Not in FSF.
|
|
29
|
|
30 ;;; Commentary:
|
|
31
|
|
32 ;; This file is dumped with XEmacs.
|
|
33
|
|
34 ;; ========================================================================
|
|
35 ;; lib-complete.el -- Completion on a search path
|
|
36 ;; Author : Mike Williams <mike-w@cs.aukuni.ac.nz>
|
|
37 ;; Created On : Sat Apr 20 17:47:21 1991
|
|
38 ;; Last Modified By: Heiko M|nkel <muenkel@tnt.uni-hannover.de>
|
|
39 ;; Additional XEmacs integration By: Chuck Thompson <cthomp@cs.uiuc.edu>
|
|
40 ;; Last Modified On: Thu Jul 1 14:23:00 1994
|
282
|
41 ;; RCS Info : $Revision: 1.3 $ $Locker: $
|
209
|
42 ;; ========================================================================
|
|
43 ;; NOTE: XEmacs must be redumped if this file is changed.
|
|
44 ;;
|
|
45 ;; Copyright (C) Mike Williams <mike-w@cs.aukuni.ac.nz> 1991
|
|
46 ;;
|
|
47 ;; Keywords: utility, lisp
|
|
48
|
|
49 ;; Many thanks to Hallvard Furuseth <hallvard@ifi.uio.no> for his
|
|
50 ;; helpful suggestions.
|
|
51
|
|
52 ;; The function locate-file is removed, because of its incompatibility
|
|
53 ;; with the buildin function of the lemacs 19.10 (Heiko M|nkel).
|
|
54
|
|
55 ;; There is now the new function find-library in this package.
|
|
56
|
|
57 ;;; ChangeLog:
|
|
58
|
|
59 ;; 4/26/97: sb Mule-ize.
|
|
60
|
|
61 ;;; Code:
|
|
62
|
|
63 ;;=== Determine completions for filename in search path ===================
|
|
64
|
|
65 (defun library-all-completions (FILE SEARCH-PATH &optional FULL FAST)
|
|
66 "Return all completions for FILE in any directory on SEARCH-PATH.
|
|
67 If optional third argument FULL is non-nil, returned pathnames should be
|
|
68 absolute rather than relative to some directory on the SEARCH-PATH.
|
|
69 If optional fourth argument FAST is non-nil, don't sort the completions,
|
|
70 or remove duplicates."
|
|
71 (setq FILE (or FILE ""))
|
|
72 (if (file-name-absolute-p FILE)
|
|
73 ;; It's an absolute file name, so don't need SEARCH-PATH
|
|
74 (progn
|
|
75 (setq FILE (expand-file-name FILE))
|
|
76 (file-name-all-completions
|
|
77 (file-name-nondirectory FILE) (file-name-directory FILE)))
|
|
78 (let ((subdir (file-name-directory FILE))
|
|
79 (file (file-name-nondirectory FILE))
|
|
80 all-completions)
|
|
81 ;; Make list of completions in each directory on SEARCH-PATH
|
|
82 (while SEARCH-PATH
|
|
83 (let* ((dir (concat (file-name-as-directory
|
|
84 (expand-file-name (car SEARCH-PATH)))
|
|
85 subdir))
|
|
86 (dir-prefix (if FULL dir subdir)))
|
|
87 (if (file-directory-p dir)
|
|
88 (let ((subdir-completions
|
|
89 (file-name-all-completions file dir)))
|
|
90 (while subdir-completions
|
|
91 (setq all-completions
|
|
92 (cons (concat dir-prefix (car subdir-completions))
|
|
93 all-completions))
|
|
94 (setq subdir-completions (cdr subdir-completions))))))
|
|
95 (setq SEARCH-PATH (cdr SEARCH-PATH)))
|
|
96 (if FAST all-completions
|
|
97 (let ((sorted (nreverse (sort all-completions 'string<)))
|
|
98 compressed)
|
|
99 (while sorted
|
|
100 (if (equal (car sorted) (car compressed)) nil
|
|
101 (setq compressed (cons (car sorted) compressed)))
|
|
102 (setq sorted (cdr sorted)))
|
|
103 compressed)))))
|
|
104
|
|
105 ;;=== Utilities ===========================================================
|
|
106
|
|
107 (defmacro progn-with-message (MESSAGE &rest FORMS)
|
|
108 "(progn-with-message MESSAGE FORMS ...)
|
|
109 Display MESSAGE and evaluate FORMS, returning value of the last one."
|
|
110 ;; based on Hallvard Furuseth's funcall-with-message
|
|
111 (`
|
|
112 (if (eq (selected-window) (minibuffer-window))
|
|
113 (save-excursion
|
|
114 (goto-char (point-max))
|
|
115 (let ((orig-pmax (point-max)))
|
|
116 (unwind-protect
|
|
117 (progn
|
|
118 (insert " " (, MESSAGE)) (goto-char orig-pmax)
|
|
119 (sit-for 0) ; Redisplay
|
|
120 (,@ FORMS))
|
|
121 (delete-region orig-pmax (point-max)))))
|
|
122 (prog2
|
|
123 (message "%s" (, MESSAGE))
|
|
124 (progn (,@ FORMS))
|
|
125 (message "")))))
|
|
126
|
|
127 (put 'progn-with-message 'lisp-indent-hook 1)
|
|
128
|
|
129 ;;=== Completion caching ==================================================
|
|
130
|
|
131 (defconst lib-complete:cache nil
|
|
132 "Used within read-library and read-library-internal to prevent
|
|
133 costly repeated calls to library-all-completions.
|
|
134 Format is a list of lists of the form
|
|
135
|
|
136 ([<path> <subdir>] <cache-record> <cache-record> ...)
|
|
137
|
|
138 where each <cache-record> has the form
|
|
139
|
|
140 (<root> <modtimes> <completion-table>)")
|
|
141
|
|
142 (defun lib-complete:better-root (ROOT1 ROOT2)
|
|
143 "Return non-nil if ROOT1 is a superset of ROOT2."
|
|
144 (and (equal (file-name-directory ROOT1) (file-name-directory ROOT2))
|
|
145 (string-match
|
|
146 (concat "^" (regexp-quote (file-name-nondirectory ROOT1)))
|
|
147 ROOT2)))
|
|
148
|
|
149 (defun lib-complete:get-completion-table (FILE PATH FILTER)
|
|
150 (let* ((subdir (file-name-directory FILE))
|
|
151 (root (file-name-nondirectory FILE))
|
|
152 (PATH
|
|
153 (mapcar
|
|
154 (function (lambda (dir) (file-name-as-directory
|
|
155 (expand-file-name (or dir "")))))
|
|
156 PATH))
|
|
157 (key (vector PATH subdir FILTER))
|
|
158 (real-dirs
|
|
159 (if subdir
|
|
160 (mapcar (function (lambda (dir) (concat dir subdir))) PATH)
|
|
161 PATH))
|
|
162 (path-modtimes
|
|
163 (mapcar
|
|
164 (function (lambda (fn) (if fn (nth 5 (file-attributes fn)))))
|
|
165 real-dirs))
|
|
166 (cache-entry (assoc key lib-complete:cache))
|
|
167 (cache-records (cdr cache-entry)))
|
|
168 ;; Look for cached entry
|
|
169 (catch 'table
|
|
170 (while cache-records
|
|
171 (if (and
|
|
172 (lib-complete:better-root (nth 0 (car cache-records)) root)
|
|
173 (equal (nth 1 (car cache-records)) path-modtimes))
|
|
174 (throw 'table (nth 2 (car cache-records))))
|
|
175 (setq cache-records (cdr cache-records)))
|
|
176 ;; Otherwise build completions
|
|
177 (let ((completion-list
|
|
178 (progn-with-message "(building completion table...)"
|
|
179 (library-all-completions FILE PATH nil 'fast)))
|
|
180 (completion-table (make-vector 127 0)))
|
|
181 (while completion-list
|
|
182 (let ((completion
|
|
183 (if (or (not FILTER)
|
|
184 (file-directory-p (car completion-list)))
|
|
185 (car completion-list)
|
|
186 (funcall FILTER (car completion-list)))))
|
|
187 (if completion
|
|
188 (intern completion completion-table)))
|
|
189 (setq completion-list (cdr completion-list)))
|
|
190 ;; Cache the completions
|
|
191 (lib-complete:cache-completions key root
|
|
192 path-modtimes completion-table)
|
|
193 completion-table))))
|
|
194
|
|
195 (defvar lib-complete:max-cache-size 40
|
|
196 "*Maximum number of search paths which are cached.")
|
|
197
|
|
198 (defun lib-complete:cache-completions (key root modtimes table)
|
|
199 (let* ((cache-entry (assoc key lib-complete:cache))
|
|
200 (cache-records (cdr cache-entry))
|
|
201 (new-cache-records (list (list root modtimes table))))
|
|
202 (if (not cache-entry) nil
|
|
203 ;; Remove old cache entry
|
|
204 (setq lib-complete:cache (delq cache-entry lib-complete:cache))
|
|
205 ;; Copy non-redundant entries from old cache entry
|
|
206 (while cache-records
|
|
207 (if (or (equal root (nth 0 (car cache-records)))
|
|
208 (lib-complete:better-root root (nth 0 (car cache-records))))
|
|
209 nil
|
|
210 (setq new-cache-records
|
|
211 (cons (car cache-records) new-cache-records)))
|
|
212 (setq cache-records (cdr cache-records))))
|
|
213 ;; Add entry to front of cache
|
|
214 (setq lib-complete:cache
|
|
215 (cons (cons key (nreverse new-cache-records)) lib-complete:cache))
|
|
216 ;; Trim cache
|
|
217 (let ((tail (nthcdr lib-complete:max-cache-size lib-complete:cache)))
|
|
218 (if tail (setcdr tail nil)))))
|
|
219
|
|
220 ;;=== Read a filename, with completion in a search path ===================
|
|
221
|
|
222 (defun read-library-internal (FILE FILTER FLAG)
|
|
223 "Don't call this."
|
|
224 ;; Relies on read-library-internal-search-path being let-bound
|
|
225 (let ((completion-table
|
|
226 (lib-complete:get-completion-table
|
|
227 FILE read-library-internal-search-path FILTER)))
|
|
228 (cond
|
|
229 ((not completion-table) nil)
|
|
230 ;; Completion table is filtered before use, so the PREDICATE
|
|
231 ;; argument is redundant.
|
|
232 ((eq FLAG nil) (try-completion FILE completion-table nil))
|
|
233 ((eq FLAG t) (all-completions FILE completion-table nil))
|
|
234 ((eq FLAG 'lambda) (and (intern-soft FILE completion-table) t))
|
|
235 )))
|
|
236
|
|
237 (defun read-library (PROMPT SEARCH-PATH &optional DEFAULT MUST-MATCH
|
|
238 FULL FILTER)
|
|
239 "Read library name, prompting with PROMPT and completing in directories
|
|
240 from SEARCH-PATH. A nil in the search path represents the current
|
|
241 directory. Completions for a given search-path are cached, with the
|
|
242 cache being invalidated whenever one of the directories on the path changes.
|
|
243 Default to DEFAULT if user enters a null string.
|
|
244 Optional fourth arg MUST-MATCH non-nil means require existing file's name.
|
|
245 Non-nil and non-t means also require confirmation after completion.
|
|
246 Optional fifth argument FULL non-nil causes a full pathname, rather than a
|
|
247 relative pathname, to be returned. Note that FULL implies MUST-MATCH.
|
|
248 Optional sixth argument FILTER can be used to provide a function to
|
|
249 filter the completions. This function is passed the filename, and should
|
|
250 return a transformed filename (possibly a null transformation) or nil,
|
|
251 indicating that the filename should not be included in the completions."
|
|
252 (let* ((read-library-internal-search-path SEARCH-PATH)
|
|
253 (library (completing-read PROMPT 'read-library-internal
|
|
254 FILTER (or MUST-MATCH FULL) nil)))
|
|
255 (cond
|
|
256 ((equal library "") DEFAULT)
|
|
257 (FULL (locate-file library read-library-internal-search-path
|
|
258 ;; decompression doesn't work with Mule -slb
|
|
259 (if (featurep 'mule)
|
|
260 ".el:.elc"
|
|
261 ".el:.el.gz:.elc")))
|
|
262 (t library))))
|
|
263
|
|
264 ;; NOTE: as a special case, read-library may be used to read a filename
|
|
265 ;; relative to the current directory, returning a *relative* pathname
|
|
266 ;; (read-file-name returns a full pathname).
|
|
267 ;;
|
|
268 ;; eg. (read-library "Local header: " '(nil) nil)
|
|
269
|
|
270 (defun get-library-path ()
|
|
271 "Front end to read-library"
|
|
272 (read-library "Find Library file: " load-path nil t t
|
|
273 (function (lambda (fn)
|
|
274 (cond
|
|
275 ;; decompression doesn't work with mule -slb
|
|
276 ((string-match (if (featurep 'mule)
|
|
277 "\\.el$"
|
|
278 "\\.el\\(\\.gz\\)?$") fn)
|
|
279 (substring fn 0 (match-beginning 0))))))
|
|
280 ))
|
|
281
|
|
282 ;;=== Replacement for load-library with completion ========================
|
|
283
|
|
284 (defun load-library (library)
|
|
285 "Load the library named LIBRARY.
|
|
286 This is an interface to the function `load'."
|
|
287 (interactive
|
|
288 (list (read-library "Load Library: " load-path nil nil nil
|
|
289 (function (lambda (fn)
|
|
290 (cond
|
|
291 ((string-match "\\.elc?$" fn)
|
|
292 (substring fn 0 (match-beginning 0))))))
|
|
293 )))
|
|
294 (load library))
|
|
295
|
|
296 ;;=== find-library with completion (Author: Heiko Muenkel) ===================
|
|
297
|
|
298 (defun find-library (library &optional codesys)
|
|
299 "Find and edit the source for the library named LIBRARY.
|
|
300 The extension of the LIBRARY must be omitted.
|
|
301 Under XEmacs/Mule, the optional second argument specifies the
|
|
302 coding system to use when decoding the file. Interactively,
|
|
303 with a prefix argument, you will be prompted for the coding system."
|
|
304 (interactive
|
|
305 (list (get-library-path)
|
|
306 (if current-prefix-arg
|
|
307 (read-coding-system "Coding System: "))))
|
|
308 (find-file library codesys))
|
|
309
|
|
310 (defun find-library-other-window (library &optional codesys)
|
|
311 "Load the library named LIBRARY in another window.
|
|
312 Under XEmacs/Mule, the optional second argument specifies the
|
|
313 coding system to use when decoding the file. Interactively,
|
|
314 with a prefix argument, you will be prompted for the coding system."
|
|
315 (interactive
|
|
316 (list (get-library-path)
|
|
317 (if current-prefix-arg
|
|
318 (read-coding-system "Coding System: "))))
|
|
319 (find-file-other-window library codesys))
|
|
320
|
|
321 (defun find-library-other-frame (library &optional codesys)
|
|
322 "Load the library named LIBRARY in a newly-created frame.
|
|
323 Under XEmacs/Mule, the optional second argument specifies the
|
|
324 coding system to use when decoding the file. Interactively,
|
|
325 with a prefix argument, you will be prompted for the coding system."
|
|
326 (interactive
|
|
327 (list (get-library-path)
|
|
328 (if current-prefix-arg
|
|
329 (read-coding-system "Coding System: "))))
|
|
330 (find-file-other-frame library codesys))
|
|
331
|
|
332 ; This conflicts with an existing binding
|
|
333 ;(define-key global-map "\C-xl" 'find-library)
|
|
334 (define-key global-map "\C-x4l" 'find-library-other-window)
|
|
335 (define-key global-map "\C-x5l" 'find-library-other-frame)
|
|
336
|
|
337 (provide 'lib-complete)
|
|
338
|
|
339 ;;; lib-complete.el ends here
|