annotate lisp/utils/tree-menu.el @ 30:ec9a17fef872 r19-15b98

Import from CVS: tag r19-15b98
author cvs
date Mon, 13 Aug 2007 08:52:29 +0200
parents 376386a54a3c
children 131b0175ea99
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1 ;;; tree-menu.el
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
2 ;;; v1.20; 10-May-1994
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
3 ;;; Copyright (C) 1994 Heiko Muenkel
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ;;; email: muenkel@tnt.uni-hannover.de
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ;;; This program is free software; you can redistribute it and/or modify
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ;;; it under the terms of the GNU General Public License as published by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ;;; the Free Software Foundation; either version 1, or (at your option)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 ;;; any later version.
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 program is distributed in the hope that it will be useful,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ;;; GNU General Public License for more details.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 ;;; You should have received a copy of the GNU General Public License
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ;;; along with this program; if not, write to the Free Software
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ;;; Synched up with: Not in FSF.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ;;; Description:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ;;; Provides the functions `tree-make-file-list' and `tree-make-menu'.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ;;; With these functions it is possible to generate file browsing menus,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ;;; where each menu-item calls the same function, but on different files.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ;;; Example:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ;;; (popup-menu (cons "Open File"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;; (tree-make-menu (tree-make-file-list "~/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;;; 'find-file
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;;; t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;;; t
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;;; '("\\..*"))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;;; Note: This function is very time consuming ! Therefore you should
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ;;; call `tree-make-file-list' once and make several menus
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;; from the same list. And you should only rebuild the menu if
30
ec9a17fef872 Import from CVS: tag r19-15b98
cvs
parents: 0
diff changeset
38 ;;; it is necessary, if you've a big directory tree.
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ;;; Installation:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ;;; Put this file in one of your lisp load directories.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ;;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 ;;; Changed 18-May-1995, Kyle Jones
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45 ;;; Removed the need for the utils.el package and references thereto.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 ;;; Changed file-truename calls to tree-menu-file-truename so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 ;;; the calls could be made compatible with FSF Emacs 19's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 ;;; file-truename function.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (defvar tree-ls-flags "-AFLR"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 "*A String with the flags used in the function tree-ls-in-temp-buffer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 for the ls command. Be careful if you want to change this variable.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 The ls command must append a / on all files which are directories.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 The original flags are -AFLR.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 (defun tree-ls-in-temp-buffer (dir temp-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 "List the directory DIR in the TEMP-BUFFER."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 (switch-to-buffer temp-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 (erase-buffer)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 (call-process "ls" nil temp-buffer nil tree-ls-flags dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 (goto-char (point-min))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 (while (search-forward "//" nil t)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 (replace-match "/"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 (goto-char (point-min)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
68 (defvar tree-temp-buffername "*tree*"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 "Name of the temp buffers in tree.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (defun tree-make-file-list-1 (root list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (let ((filename (buffer-substring (point) (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (while (not (string= filename ""))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (setq
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 (append
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (cond ((char-equal (char-after (- (point) 1)) ?/)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;; Directory
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 (setq filename (substring filename 0 (1- (length filename))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 (search-forward (concat root filename ":"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (forward-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (tree-make-file-list-1 (concat root filename "/")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 (list (tree-menu-file-truename
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 filename
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 root)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 ((char-equal (char-after (- (point) 1)) ?*)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 ;; Executable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (setq filename (substring filename 0 (1- (length filename))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (tree-menu-file-truename filename root))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 (t (tree-menu-file-truename filename root))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 (forward-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (setq filename (buffer-substring (point) (progn
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99 (end-of-line)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 (point)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
101 list))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
102
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
103
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
104 (defun tree-menu-file-truename (file &optional root)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
105 (file-truename (expand-file-name file root)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
106
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
107 (defun tree-make-file-list (dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
108 "Makes a list with the files and subdirectories of DIR.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 The list looks like: ((dirname1 file1 file2)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 file3
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 (dirname2 (dirname3 file4 file5) file6))"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
112 (save-window-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 (setq dir (expand-file-name dir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 (if (not (string= (substring dir -1) "/"))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 (setq dir (concat dir "/")))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 ;; (while (string-match "/$" dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 ;; (setq dir (substring dir 0 -1)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 (tree-ls-in-temp-buffer dir
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 (generate-new-buffer-name
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 tree-temp-buffername))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 (let ((list nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 (setq list (tree-make-file-list-1 dir nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 (kill-buffer (current-buffer))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 (defun tree-hide-file-p (filename re-hidden-file-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
128 "t, if one of the regexps in RE-HIDDEN-FILE-LIST matches the FILENAME."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129 (cond ((not re-hidden-file-list) nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
130 ((string-match (car re-hidden-file-list)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
131 (tree-menu-file-truename filename)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
132 (t (tree-hide-file-p filename (cdr re-hidden-file-list)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
133
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 (defun tree-make-menu (dirlist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136 function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 selectable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 &optional
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 no-hidden-dirs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 re-hidden-file-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 include-current-dir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 "Returns a menu list.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 Each item of the menu list has the form
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 [\"subdir\" (FUNCTION \"dir\") SELECTABLE].
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 Hidden directories (with a leading point) are suppressed,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 if NO-HIDDEN-DIRS are non nil. Also all files which are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 matching a regexp in RE-HIDDEN-FILE-LIST are suppressed.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 If INCLUDE-CURRENT-DIR non nil, then an additional command
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 for the current directory (.) is inserted."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 (let ((subdir nil)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 (menulist nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 (while (setq subdir (car dirlist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 (setq dirlist (cdr dirlist))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 (cond ((and (stringp subdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 (not (tree-hide-file-p subdir re-hidden-file-list)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 (setq menulist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 (append menulist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158 (list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 (vector (file-name-nondirectory subdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 (list function subdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 selectable)))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 ((and (listp subdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 (or (not no-hidden-dirs)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 (not (char-equal
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165 ?.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 (string-to-char
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 (file-name-nondirectory (car subdir))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 (setq menulist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 (append
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170 menulist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 (list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 (cons (file-name-nondirectory (car subdir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 (if include-current-dir
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 (cons
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 (vector "."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 (list function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 (car subdir))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 selectable)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 (tree-make-menu (cdr subdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180 function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 selectable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 no-hidden-dirs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 re-hidden-file-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 include-current-dir
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 (tree-make-menu (cdr subdir)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 function
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188 selectable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 no-hidden-dirs
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 re-hidden-file-list
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 ))))))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 (t nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 menulist
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 )
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
199 (provide 'tree-menu)