428
|
1 ;;; cus-dep.el --- Find customization dependencies.
|
|
2 ;;
|
|
3 ;; Copyright (C) 1997 Free Software Foundation, Inc.
|
|
4 ;;
|
|
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>, then
|
|
6 ;; Richard Stallman <rms@gnu.ai.mit.edu>, then
|
|
7 ;; Hrvoje Niksic <hniksic@xemacs.org> (rewritten for XEmacs)
|
|
8 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
|
|
9 ;; Keywords: internal
|
|
10
|
|
11 ;; This file is part of XEmacs.
|
|
12
|
|
13 ;; XEmacs is free software; you can redistribute it and/or modify
|
|
14 ;; it under the terms of the GNU General Public License as published by
|
|
15 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
16 ;; any later version.
|
|
17
|
|
18 ;; XEmacs is distributed in the hope that it will be useful,
|
|
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
21 ;; GNU General Public License for more details.
|
|
22
|
|
23 ;; You should have received a copy of the GNU General Public License
|
|
24 ;; along with XEmacs; see the file COPYING. If not, write to the
|
|
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
26 ;; Boston, MA 02111-1307, USA.
|
|
27
|
|
28 ;;; Synched up with: Not synched with FSF.
|
|
29
|
|
30
|
|
31 ;;; Commentary:
|
|
32
|
|
33 ;; This file generates the custom-load files, loaded by cus-load.el.
|
442
|
34 ;; Entry points are `Custom-make-dependencies' and
|
|
35 ;; `Custom-make-one-dependency'.
|
428
|
36
|
|
37 ;; It works by scanning all the `.el' files in a directory, and
|
|
38 ;; evaluates any `defcustom', `defgroup', or `defface' expression that
|
|
39 ;; it finds. The symbol changed by this expression is stored to a
|
|
40 ;; hash table as the hash key, file name being the value.
|
|
41
|
|
42 ;; After all the files have been examined, custom-loads.el is
|
|
43 ;; generated by mapping all the atoms, and seeing if any of them
|
|
44 ;; contains a `custom-group' property. This property is a list whose
|
|
45 ;; each element's car is the "child" group symbol. If that property
|
|
46 ;; is in the hash-table, the file name will be looked up from the
|
|
47 ;; hash-table, and added to cusload-file. Because the hash-table is
|
|
48 ;; cleared whenever we process a new directory, we cannot get confused
|
|
49 ;; by custom-loads from another directory, or from a previous
|
|
50 ;; installation. This is also why it is perfectly safe to have old
|
|
51 ;; custom-loads around, and have them loaded by `cus-load.el' (as
|
|
52 ;; invoked by `cus-edit.el').
|
|
53
|
|
54 ;; A trivial, but useful optimization is that if cusload-file exists,
|
|
55 ;; and no .el files in the directory are newer than cusload-file, it
|
|
56 ;; will not be generated. This means that the directories where
|
|
57 ;; nothing has changed will be skipped.
|
|
58
|
|
59 ;; The `custom-add-loads' function, used by files generated by
|
|
60 ;; `Custom-make-dependencies', updates the symbol's `custom-loads'
|
|
61 ;; property (a list of strings) with a new list of strings,
|
|
62 ;; eliminating the duplicates. Additionally, it adds the symbol to
|
|
63 ;; `custom-group-hash-table'. It is defined in `cus-load.el'.
|
|
64
|
|
65 ;; Example:
|
|
66
|
|
67 ;; (custom-add-loads 'foo 'custom-loads '("bar" "baz"))
|
|
68 ;; (get 'foo 'custom-loads)
|
|
69 ;; => ("bar" "baz")
|
|
70 ;;
|
|
71 ;; (custom-add-loads 'foo 'custom-loads '("hmph" "baz" "quz"))
|
|
72 ;; (get 'foo 'custom-loads)
|
|
73 ;; => ("bar" "baz" "hmph" "qux")
|
|
74
|
|
75 ;; Obviously, this allows correct incremental loading of custom-load
|
|
76 ;; files. This is not necessary under FSF (they simply use `put'),
|
|
77 ;; since they have only one file with custom dependencies. With the
|
|
78 ;; advent of packages, we cannot afford the same luxury.
|
|
79
|
|
80
|
|
81 ;;; Code:
|
|
82
|
|
83 (require 'cl)
|
|
84 (require 'widget)
|
|
85 (require 'cus-face)
|
|
86
|
1244
|
87 ;; #### This and the autoloads file naming variables belong in a separate
|
|
88 ;; file to be required here.
|
|
89 ;; #### Compare this with the autoloads handling.
|
428
|
90 ;; Don't change this, unless you plan to change the code in
|
|
91 ;; cus-start.el, too.
|
|
92 (defconst cusload-base-file "custom-load.el")
|
|
93
|
|
94 ;; Be very careful when changing this function. It looks easy to
|
|
95 ;; understand, but is in fact very easy to break. Be sure to read and
|
|
96 ;; understand the commentary above!
|
|
97
|
442
|
98 (defun Custom-make-dependencies-1 (subdirs)
|
428
|
99 (setq subdirs (mapcar #'expand-file-name subdirs))
|
|
100 (with-temp-buffer
|
|
101 (let ((enable-local-eval nil)
|
|
102 (hash (make-hash-table :test 'eq)))
|
|
103 (dolist (dir subdirs)
|
|
104 (princ (format "Processing %s\n" dir))
|
|
105 (let ((cusload-file (expand-file-name cusload-base-file dir))
|
|
106 (files (directory-files dir t "\\`[^=].*\\.el\\'")))
|
|
107 ;; A trivial optimization: if no file in the directory is
|
|
108 ;; newer than custom-load.el, no need to do anything!
|
|
109 (if (and (file-exists-p cusload-file)
|
|
110 (dolist (file files t)
|
|
111 (when (file-newer-than-file-p file cusload-file)
|
|
112 (return nil))))
|
|
113 (princ "(No changes need to be written)\n")
|
|
114 ;; Process directory
|
|
115 (dolist (file files)
|
|
116 (when (file-exists-p file)
|
|
117 (erase-buffer)
|
|
118 (insert-file-contents file)
|
|
119 (goto-char (point-min))
|
|
120 (let ((name (file-name-sans-extension
|
|
121 (file-name-nondirectory file))))
|
|
122 ;; Search for defcustom/defface/defgroup
|
|
123 ;; expressions, and evaluate them.
|
|
124 (while (re-search-forward
|
|
125 "^(defcustom\\|^(defface\\|^(defgroup"
|
|
126 nil t)
|
|
127 (beginning-of-line)
|
|
128 (let ((expr (read (current-buffer))))
|
|
129 ;; We need to ignore errors here, so that
|
|
130 ;; defcustoms with :set don't bug out. Of
|
|
131 ;; course, their values will not be assigned in
|
|
132 ;; case of errors, but their `custom-group'
|
|
133 ;; properties will by that time be in place, and
|
|
134 ;; that's all we care about.
|
|
135 (ignore-errors
|
|
136 (eval expr))
|
|
137 ;; Hash the file of the affected symbol.
|
|
138 (setf (gethash (nth 1 expr) hash) name))))))
|
|
139 (cond
|
|
140 ((zerop (hash-table-count hash))
|
|
141 (princ "(No customization dependencies")
|
|
142 (when (file-exists-p cusload-file)
|
|
143 (princ (format ", deleting %s" cusload-file))
|
|
144 (delete-file cusload-file))
|
|
145 (princ ")\n"))
|
|
146 (t
|
|
147 (princ (format "Generating %s...\n" cusload-base-file))
|
|
148 (with-temp-file cusload-file
|
|
149 (insert ";;; " cusload-base-file
|
|
150 " --- automatically extracted custom dependencies\n"
|
442
|
151 "\n;;; Code:\n\n"
|
|
152 "(autoload 'custom-add-loads \"cus-load\")\n\n")
|
428
|
153 (mapatoms
|
|
154 (lambda (sym)
|
|
155 (let ((members (get sym 'custom-group))
|
|
156 item where found)
|
|
157 (when members
|
|
158 (while members
|
|
159 (setq item (car (car members))
|
|
160 members (cdr members)
|
|
161 where (gethash item hash))
|
|
162 (unless (or (null where)
|
|
163 (member where found))
|
|
164 (if found
|
|
165 (insert " ")
|
|
166 (insert "(custom-add-loads '"
|
|
167 (prin1-to-string sym) " '("))
|
|
168 (prin1 where (current-buffer))
|
|
169 (push where found)))
|
|
170 (when found
|
|
171 (insert "))\n"))))))
|
|
172 (insert "\n;;; custom-load.el ends here\n"))
|
|
173 (clrhash hash)))))))))
|
|
174
|
442
|
175 (defun Custom-make-one-dependency ()
|
|
176 "Extract custom dependencies from .el files in one dir, on the command line.
|
|
177 Like `Custom-make-dependencies' but snarfs only one command-line argument,
|
|
178 making it useful in a chain of batch commands in a single XEmacs invocation."
|
|
179 (let ((subdir (car command-line-args-left)))
|
|
180 (setq command-line-args-left (cdr command-line-args-left))
|
|
181 (Custom-make-dependencies-1 (list subdir))))
|
|
182
|
|
183 ;;;###autoload
|
|
184 (defun Custom-make-dependencies (&optional subdirs)
|
|
185 "Extract custom dependencies from .el files in SUBDIRS.
|
|
186 SUBDIRS is a list of directories. If it is nil, the command-line
|
|
187 arguments are used. If it is a string, only that directory is
|
|
188 processed. This function is especially useful in batch mode.
|
|
189
|
|
190 Batch usage: xemacs -batch -l cus-dep.el -f Custom-make-dependencies DIRS"
|
|
191 (interactive "DDirectory: ")
|
|
192 (and (stringp subdirs)
|
|
193 (setq subdirs (list subdirs)))
|
|
194 (or subdirs
|
|
195 ;; Usurp the command-line-args
|
|
196 (setq subdirs command-line-args-left
|
|
197 command-line-args-left nil))
|
|
198 (Custom-make-dependencies-1 subdirs))
|
|
199
|
428
|
200 (provide 'cus-dep)
|
|
201
|
|
202 ;;; cus-dep.el ends here
|