annotate lisp/lib-complete.el @ 288:e11d67e05968 r21-0b42

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