]> code.delx.au - gnu-emacs/blob - lisp/cus-dep.el
custom-make-dependencies tweaks
[gnu-emacs] / lisp / cus-dep.el
1 ;;; cus-dep.el --- find customization dependencies
2 ;;
3 ;; Copyright (C) 1997, 2001-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Keywords: internal
7 ;; Package: emacs
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'widget)
29 (require 'cus-face)
30
31 (defvar generated-custom-dependencies-file "cus-load.el"
32 "Output file for `custom-make-dependencies'.")
33
34 ;; See finder-no-scan-regexp in finder.el.
35 (defvar custom-dependencies-no-scan-regexp "\\(^\\.#\\|\\(loaddefs\\|\
36 ldefs-boot\\|cus-load\\|finder-inf\\|esh-groups\\|subdirs\\)\\.el$\\)"
37 "Regexp matching file names not to scan for `custom-make-dependencies'.")
38
39 (require 'autoload)
40
41 ;; Hack workaround for bug#14384.
42 ;; Define defcustom-mh as an alias for defcustom, etc.
43 ;; Only do this in batch mode to avoid messing up a normal Emacs session.
44 ;; Alternative would be to load mh-e when making cus-load.
45 ;; (Would be better to split just the necessary parts of mh-e into a
46 ;; separate file and only load that.)
47 (when (and noninteractive)
48 (mapc (lambda (e) (let ((sym (intern (format "%s-mh" e))))
49 (or (fboundp sym)
50 (defalias sym e))))
51 '(defcustom defface defgroup)))
52
53 (defun custom-make-dependencies ()
54 "Batch function to extract custom dependencies from .el files.
55 Usage: emacs -batch -l ./cus-dep.el -f custom-make-dependencies DIRS"
56 (let ((enable-local-eval nil)
57 (enable-local-variables :safe)
58 subdir)
59 (with-temp-buffer
60 ;; Use up command-line-args-left else Emacs can try to open
61 ;; the args as directories after we are done.
62 (while (setq subdir (pop command-line-args-left))
63 (message "Directory %s" subdir)
64 (let ((files (directory-files subdir nil "\\`[^=.].*\\.el\\'"))
65 (default-directory (expand-file-name subdir))
66 (preloaded (concat "\\`\\(\\./+\\)?"
67 (regexp-opt preloaded-file-list t)
68 "\\.el\\'")))
69 (dolist (file files)
70 (unless (or (string-match custom-dependencies-no-scan-regexp file)
71 (string-match preloaded (format "%s/%s" subdir file))
72 (not (file-exists-p file)))
73 (erase-buffer)
74 (kill-all-local-variables)
75 (insert-file-contents file)
76 (hack-local-variables)
77 (goto-char (point-min))
78 (string-match "\\`\\(.*\\)\\.el\\'" file)
79 (let ((name (or generated-autoload-load-name ; see bug#5277
80 (file-name-nondirectory (match-string 1 file))))
81 (load-file-name file))
82 (if (save-excursion
83 (re-search-forward
84 (concat "(\\(cc-\\)?provide[ \t\n]+\\('\\|(quote[ \t\n]\\)[ \t\n]*"
85 (regexp-quote name) "[ \t\n)]")
86 nil t))
87 (setq name (intern name)))
88 (condition-case nil
89 (while (re-search-forward
90 "^(def\\(custom\\|face\\|group\\)" nil t)
91 (beginning-of-line)
92 (let ((expr (read (current-buffer))))
93 (condition-case nil
94 (let ((custom-dont-initialize t))
95 ;; Why do we need to eval just for the name?
96 (eval expr)
97 (put (nth 1 expr) 'custom-where name))
98 (error nil))))
99 (error nil)))))))))
100 (message "Generating %s..." generated-custom-dependencies-file)
101 (set-buffer (find-file-noselect generated-custom-dependencies-file))
102 (setq buffer-undo-list t)
103 (erase-buffer)
104 (insert (autoload-rubric generated-custom-dependencies-file
105 "custom dependencies" t))
106 (search-backward "\f")
107 (mapatoms (lambda (symbol)
108 (let ((members (get symbol 'custom-group))
109 where found)
110 (when members
111 (dolist (member
112 ;; So x and no-x builds won't differ.
113 (sort (mapcar 'car members) 'string<))
114 (setq where (get member 'custom-where))
115 (unless (or (null where)
116 (member where found))
117 (push where found)))
118 (when found
119 (insert "(put '" (symbol-name symbol)
120 " 'custom-loads '")
121 (prin1 (nreverse found) (current-buffer))
122 (insert ")\n"))))))
123 (insert "\
124 ;; These are for handling :version. We need to have a minimum of
125 ;; information so `customize-changed-options' could do its job.
126
127 ;; For groups we set `custom-version', `group-documentation' and
128 ;; `custom-tag' (which are shown in the customize buffer), so we
129 ;; don't have to load the file containing the group.
130
131 ;; `custom-versions-load-alist' is an alist that has as car a version
132 ;; number and as elts the files that have variables or faces that
133 ;; contain that version. These files should be loaded before showing
134 ;; the customization buffer that `customize-changed-options'
135 ;; generates.
136
137 ;; This macro is used so we don't modify the information about
138 ;; variables and groups if it's already set. (We don't know when
139 ;; " (file-name-nondirectory generated-custom-dependencies-file)
140 " is going to be loaded and at that time some of the
141 ;; files might be loaded and some others might not).
142 \(defmacro custom-put-if-not (symbol propname value)
143 `(unless (get ,symbol ,propname)
144 (put ,symbol ,propname ,value)))
145
146 ")
147 (let ((version-alist nil))
148 (mapatoms (lambda (symbol)
149 (let ((version (get symbol 'custom-version))
150 where)
151 (when version
152 (setq where (get symbol 'custom-where))
153 (when where
154 (if (or (custom-variable-p symbol)
155 (custom-facep symbol))
156 ;; This means it's a variable or a face.
157 (progn
158 (if (assoc version version-alist)
159 (unless
160 (member where
161 (cdr (assoc version version-alist)))
162 (push where (cdr (assoc version version-alist))))
163 (push (list version where) version-alist)))
164 ;; This is a group
165 (insert "(custom-put-if-not '" (symbol-name symbol)
166 " 'custom-version ")
167 (prin1 version (current-buffer))
168 (insert ")\n")
169 (insert "(custom-put-if-not '" (symbol-name symbol))
170 (insert " 'group-documentation ")
171 (prin1 (get symbol 'group-documentation) (current-buffer))
172 (insert ")\n")
173 (when (get symbol 'custom-tag)
174 (insert "(custom-put-if-not '" (symbol-name symbol))
175 (insert " 'custom-tag ")
176 (prin1 (get symbol 'custom-tag) (current-buffer))
177 (insert ")\n"))
178 ))))))
179
180 (insert "\n(defvar custom-versions-load-alist "
181 (if version-alist "'" ""))
182 (prin1 version-alist (current-buffer))
183 (insert "\n \"For internal use by custom.\")\n"))
184 (save-buffer)
185 (message "Generating %s...done" generated-custom-dependencies-file))
186
187 \f
188
189 ;;; cus-dep.el ends here