annotate lisp/utils/lib-complete.el @ 194:2947057885e5

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