]> code.delx.au - gnu-emacs/blob - lisp/cus-dep.el
* cus-dep.el (defcustom-mh, defgroup-mh, defface-mh): Add compat aliases
[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 (mapcar
68 'file-name-base
69 preloaded-file-list) t)
70 "\\.el\\'")))
71 (dolist (file files)
72 (unless (or (string-match custom-dependencies-no-scan-regexp file)
73 (string-match preloaded file)
74 (not (file-exists-p file)))
75 (erase-buffer)
76 (kill-all-local-variables)
77 (insert-file-contents file)
78 (hack-local-variables)
79 (goto-char (point-min))
80 (string-match "\\`\\(.*\\)\\.el\\'" file)
81 (let ((name (or generated-autoload-load-name ; see bug#5277
82 (file-name-nondirectory (match-string 1 file))))
83 (load-file-name file))
84 (if (save-excursion
85 (re-search-forward
86 (concat "(\\(cc-\\)?provide[ \t\n]+\\('\\|(quote[ \t\n]\\)[ \t\n]*"
87 (regexp-quote name) "[ \t\n)]")
88 nil t))
89 (setq name (intern name)))
90 (condition-case nil
91 (while (re-search-forward
92 "^(def\\(custom\\|face\\|group\\)" nil t)
93 (beginning-of-line)
94 (let ((expr (read (current-buffer))))
95 (condition-case nil
96 (let ((custom-dont-initialize t))
97 ;; Why do we need to eval just for the name?
98 (eval expr)
99 (put (nth 1 expr) 'custom-where name))
100 (error nil))))
101 (error nil)))))))))
102 (message "Generating %s..." generated-custom-dependencies-file)
103 (set-buffer (find-file-noselect generated-custom-dependencies-file))
104 (setq buffer-undo-list t)
105 (erase-buffer)
106 (insert (autoload-rubric generated-custom-dependencies-file
107 "custom dependencies" t))
108 (search-backward "\f")
109 (mapatoms (lambda (symbol)
110 (let ((members (get symbol 'custom-group))
111 where found)
112 (when members
113 (dolist (member
114 ;; So x and no-x builds won't differ.
115 (sort (mapcar 'car members) 'string<))
116 (setq where (get member 'custom-where))
117 (unless (or (null where)
118 (member where found))
119 (push where found)))
120 (when found
121 (insert "(put '" (symbol-name symbol)
122 " 'custom-loads '")
123 (prin1 (nreverse found) (current-buffer))
124 (insert ")\n"))))))
125 (insert "\
126 ;; These are for handling :version. We need to have a minimum of
127 ;; information so `customize-changed-options' could do its job.
128
129 ;; For groups we set `custom-version', `group-documentation' and
130 ;; `custom-tag' (which are shown in the customize buffer), so we
131 ;; don't have to load the file containing the group.
132
133 ;; `custom-versions-load-alist' is an alist that has as car a version
134 ;; number and as elts the files that have variables or faces that
135 ;; contain that version. These files should be loaded before showing
136 ;; the customization buffer that `customize-changed-options'
137 ;; generates.
138
139 ;; This macro is used so we don't modify the information about
140 ;; variables and groups if it's already set. (We don't know when
141 ;; " (file-name-nondirectory generated-custom-dependencies-file)
142 " is going to be loaded and at that time some of the
143 ;; files might be loaded and some others might not).
144 \(defmacro custom-put-if-not (symbol propname value)
145 `(unless (get ,symbol ,propname)
146 (put ,symbol ,propname ,value)))
147
148 ")
149 (let ((version-alist nil))
150 (mapatoms (lambda (symbol)
151 (let ((version (get symbol 'custom-version))
152 where)
153 (when version
154 (setq where (get symbol 'custom-where))
155 (when where
156 (if (or (custom-variable-p symbol)
157 (custom-facep symbol))
158 ;; This means it's a variable or a face.
159 (progn
160 (if (assoc version version-alist)
161 (unless
162 (member where
163 (cdr (assoc version version-alist)))
164 (push where (cdr (assoc version version-alist))))
165 (push (list version where) version-alist)))
166 ;; This is a group
167 (insert "(custom-put-if-not '" (symbol-name symbol)
168 " 'custom-version ")
169 (prin1 version (current-buffer))
170 (insert ")\n")
171 (insert "(custom-put-if-not '" (symbol-name symbol))
172 (insert " 'group-documentation ")
173 (prin1 (get symbol 'group-documentation) (current-buffer))
174 (insert ")\n")
175 (when (get symbol 'custom-tag)
176 (insert "(custom-put-if-not '" (symbol-name symbol))
177 (insert " 'custom-tag ")
178 (prin1 (get symbol 'custom-tag) (current-buffer))
179 (insert ")\n"))
180 ))))))
181
182 (insert "\n(defvar custom-versions-load-alist "
183 (if version-alist "'" ""))
184 (prin1 version-alist (current-buffer))
185 (insert "\n \"For internal use by custom.\")\n"))
186 (save-buffer)
187 (message "Generating %s...done" generated-custom-dependencies-file))
188
189 \f
190
191 ;;; cus-dep.el ends here