cus-dep.el 6.9 KB

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