]> code.delx.au - gnu-emacs/blob - lisp/cus-dep.el
Merge from trunk.
[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 (autoload 'autoload-rubric "autoload")
40
41 (defun set-generated-custom-dependencies-file (file)
42 "Set value of `generated-custom-dependencies-file' from FILE.
43
44 On systems other than MS-Windows, just sets the value
45 of `generated-custom-dependencies-file'. On MS-Windows, converts
46 /d/foo/bar form passed by MSYS Make into d:/foo/bar that Emacs can
47 grok. This function is called from lisp/Makefile."
48 (when (and (eq system-type 'windows-nt)
49 (string-match "\\`/[a-zA-Z]/" file))
50 (setq file (concat (substring file 1 2) ":" (substring file 2))))
51 (setq generated-custom-dependencies-file file))
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 subdir)
58 (with-temp-buffer
59 ;; Use up command-line-args-left else Emacs can try to open
60 ;; the args as directories after we are done.
61 (while (setq subdir (pop command-line-args-left))
62 (message "Directory %s" subdir)
63 (let ((files (directory-files subdir nil "\\`[^=].*\\.el\\'"))
64 (default-directory (expand-file-name subdir))
65 (preloaded (concat "\\`"
66 (regexp-opt (mapcar
67 'file-name-base
68 preloaded-file-list) t)
69 "\\.el\\'")))
70 (dolist (file files)
71 (unless (or (string-match custom-dependencies-no-scan-regexp file)
72 (string-match preloaded file)
73 (not (file-exists-p file)))
74 (erase-buffer)
75 (insert-file-contents file)
76 (goto-char (point-min))
77 (string-match "\\`\\(.*\\)\\.el\\'" file)
78 (let ((name (file-name-nondirectory (match-string 1 file)))
79 (load-file-name file))
80 (if (save-excursion
81 (re-search-forward
82 (concat "(provide[ \t\n]+\\('\\|(quote[ \t\n]\\)[ \t\n]*"
83 (regexp-quote name) "[ \t\n)]")
84 nil t))
85 (setq name (intern name)))
86 (condition-case nil
87 (while (re-search-forward
88 "^(def\\(custom\\|face\\|group\\)" nil t)
89 (beginning-of-line)
90 (let ((expr (read (current-buffer))))
91 (condition-case nil
92 (let ((custom-dont-initialize t))
93 (eval expr)
94 (put (nth 1 expr) 'custom-where name))
95 (error nil))))
96 (error nil)))))))))
97 (message "Generating %s..." generated-custom-dependencies-file)
98 (set-buffer (find-file-noselect generated-custom-dependencies-file))
99 (setq buffer-undo-list t)
100 (erase-buffer)
101 (insert (autoload-rubric generated-custom-dependencies-file
102 "custom dependencies" t))
103 (search-backward "\f")
104 (mapatoms (lambda (symbol)
105 (let ((members (get symbol 'custom-group))
106 where found)
107 (when members
108 (dolist (member
109 ;; So x and no-x builds won't differ.
110 (sort (mapcar 'car members) 'string<))
111 (setq where (get member 'custom-where))
112 (unless (or (null where)
113 (member where found))
114 (push where found)))
115 (when found
116 (insert "(put '" (symbol-name symbol)
117 " 'custom-loads '")
118 (prin1 (nreverse found) (current-buffer))
119 (insert ")\n"))))))
120 (insert "\
121 ;; These are for handling :version. We need to have a minimum of
122 ;; information so `customize-changed-options' could do its job.
123
124 ;; For groups we set `custom-version', `group-documentation' and
125 ;; `custom-tag' (which are shown in the customize buffer), so we
126 ;; don't have to load the file containing the group.
127
128 ;; `custom-versions-load-alist' is an alist that has as car a version
129 ;; number and as elts the files that have variables or faces that
130 ;; contain that version. These files should be loaded before showing
131 ;; the customization buffer that `customize-changed-options'
132 ;; generates.
133
134 ;; This macro is used so we don't modify the information about
135 ;; variables and groups if it's already set. (We don't know when
136 ;; " (file-name-nondirectory generated-custom-dependencies-file)
137 " is going to be loaded and at that time some of the
138 ;; files might be loaded and some others might not).
139 \(defmacro custom-put-if-not (symbol propname value)
140 `(unless (get ,symbol ,propname)
141 (put ,symbol ,propname ,value)))
142
143 ")
144 (let ((version-alist nil))
145 (mapatoms (lambda (symbol)
146 (let ((version (get symbol 'custom-version))
147 where)
148 (when version
149 (setq where (get symbol 'custom-where))
150 (when where
151 (if (or (custom-variable-p symbol)
152 (custom-facep symbol))
153 ;; This means it's a variable or a face.
154 (progn
155 (if (assoc version version-alist)
156 (unless
157 (member where
158 (cdr (assoc version version-alist)))
159 (push where (cdr (assoc version version-alist))))
160 (push (list version where) version-alist)))
161 ;; This is a group
162 (insert "(custom-put-if-not '" (symbol-name symbol)
163 " 'custom-version ")
164 (prin1 version (current-buffer))
165 (insert ")\n")
166 (insert "(custom-put-if-not '" (symbol-name symbol))
167 (insert " 'group-documentation ")
168 (prin1 (get symbol 'group-documentation) (current-buffer))
169 (insert ")\n")
170 (when (get symbol 'custom-tag)
171 (insert "(custom-put-if-not '" (symbol-name symbol))
172 (insert " 'custom-tag ")
173 (prin1 (get symbol 'custom-tag) (current-buffer))
174 (insert ")\n"))
175 ))))))
176
177 (insert "\n(defvar custom-versions-load-alist "
178 (if version-alist "'" ""))
179 (prin1 version-alist (current-buffer))
180 (insert "\n \"For internal use by custom.\")\n"))
181 (save-buffer)
182 (message "Generating %s...done" generated-custom-dependencies-file))
183
184 \f
185
186 ;;; cus-dep.el ends here