annotate lisp/utils/lib-complete.el @ 151:59463afc5666 r20-3b2

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