autoload.el 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. ;; autoload.el --- maintain autoloads in loaddefs.el
  2. ;; Copyright (C) 1991-1997, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Roland McGrath <roland@gnu.org>
  4. ;; Keywords: maint
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; This code helps GNU Emacs maintainers keep the loaddefs.el file up to
  19. ;; date. It interprets magic cookies of the form ";;;###autoload" in
  20. ;; lisp source files in various useful ways. To learn more, read the
  21. ;; source; if you're going to use this, you'd better be able to.
  22. ;;; Code:
  23. (require 'lisp-mode) ;for `doc-string-elt' properties.
  24. (require 'help-fns) ;for help-add-fundoc-usage.
  25. (eval-when-compile (require 'cl))
  26. (defvar generated-autoload-file nil
  27. "File into which to write autoload definitions.
  28. A Lisp file can set this in its local variables section to make
  29. its autoloads go somewhere else.
  30. If this is a relative file name, the directory is determined as
  31. follows:
  32. - If a Lisp file defined `generated-autoload-file' as a
  33. file-local variable, use its containing directory.
  34. - Otherwise use the \"lisp\" subdirectory of `source-directory'.
  35. The autoload file is assumed to contain a trailer starting with a
  36. FormFeed character.")
  37. ;;;###autoload
  38. (put 'generated-autoload-file 'safe-local-variable 'stringp)
  39. (defvar generated-autoload-load-name nil
  40. "Load name for `autoload' statements generated from autoload cookies.
  41. If nil, this defaults to the file name, sans extension.")
  42. ;;;###autoload
  43. (put 'generated-autoload-load-name 'safe-local-variable 'stringp)
  44. ;; This feels like it should be a defconst, but MH-E sets it to
  45. ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
  46. (defvar generate-autoload-cookie ";;;###autoload"
  47. "Magic comment indicating the following form should be autoloaded.
  48. Used by \\[update-file-autoloads]. This string should be
  49. meaningless to Lisp (e.g., a comment).
  50. This string is used:
  51. \;;;###autoload
  52. \(defun function-to-be-autoloaded () ...)
  53. If this string appears alone on a line, the following form will be
  54. read and an autoload made for it. If there is further text on the line,
  55. that text will be copied verbatim to `generated-autoload-file'.")
  56. (defvar autoload-excludes nil
  57. "If non-nil, list of absolute file names not to scan for autoloads.")
  58. (defconst generate-autoload-section-header "\f\n;;;### "
  59. "String that marks the form at the start of a new file's autoload section.")
  60. (defconst generate-autoload-section-trailer "\n;;;***\n"
  61. "String which indicates the end of the section of autoloads for a file.")
  62. (defconst generate-autoload-section-continuation ";;;;;; "
  63. "String to add on each continuation of the section header form.")
  64. (defvar autoload-modified-buffers) ;Dynamically scoped var.
  65. (defun make-autoload (form file)
  66. "Turn FORM into an autoload or defvar for source file FILE.
  67. Returns nil if FORM is not a special autoload form (i.e. a function definition
  68. or macro definition or a defcustom)."
  69. (let ((car (car-safe form)) expand)
  70. (cond
  71. ;; For complex cases, try again on the macro-expansion.
  72. ((and (memq car '(easy-mmode-define-global-mode define-global-minor-mode
  73. define-globalized-minor-mode
  74. easy-mmode-define-minor-mode define-minor-mode))
  75. (setq expand (let ((load-file-name file)) (macroexpand form)))
  76. (eq (car expand) 'progn)
  77. (memq :autoload-end expand))
  78. (let ((end (memq :autoload-end expand)))
  79. ;; Cut-off anything after the :autoload-end marker.
  80. (setcdr end nil)
  81. (cons 'progn
  82. (mapcar (lambda (form) (make-autoload form file))
  83. (cdr expand)))))
  84. ;; For special function-like operators, use the `autoload' function.
  85. ((memq car '(defun define-skeleton defmacro define-derived-mode
  86. define-compilation-mode define-generic-mode
  87. easy-mmode-define-global-mode define-global-minor-mode
  88. define-globalized-minor-mode
  89. easy-mmode-define-minor-mode define-minor-mode
  90. defun* defmacro* define-overloadable-function))
  91. (let* ((macrop (memq car '(defmacro defmacro*)))
  92. (name (nth 1 form))
  93. (args (case car
  94. ((defun defmacro defun* defmacro*
  95. define-overloadable-function) (nth 2 form))
  96. ((define-skeleton) '(&optional str arg))
  97. ((define-generic-mode define-derived-mode
  98. define-compilation-mode) nil)
  99. (t)))
  100. (body (nthcdr (get car 'doc-string-elt) form))
  101. (doc (if (stringp (car body)) (pop body))))
  102. (when (listp args)
  103. ;; Add the usage form at the end where describe-function-1
  104. ;; can recover it.
  105. (setq doc (help-add-fundoc-usage doc args)))
  106. (let ((exp
  107. ;; `define-generic-mode' quotes the name, so take care of that
  108. (list 'autoload (if (listp name) name (list 'quote name))
  109. file doc
  110. (or (and (memq car '(define-skeleton define-derived-mode
  111. define-generic-mode
  112. easy-mmode-define-global-mode
  113. define-global-minor-mode
  114. define-globalized-minor-mode
  115. easy-mmode-define-minor-mode
  116. define-minor-mode)) t)
  117. (eq (car-safe (car body)) 'interactive))
  118. (if macrop (list 'quote 'macro) nil))))
  119. (when macrop
  120. ;; Special case to autoload some of the macro's declarations.
  121. (let ((decls (nth (if (stringp (nth 3 form)) 4 3) form))
  122. (exps '()))
  123. (when (eq (car-safe decls) 'declare)
  124. ;; FIXME: We'd like to reuse macro-declaration-function,
  125. ;; but we can't since it doesn't return anything.
  126. (dolist (decl decls)
  127. (case (car-safe decl)
  128. (indent
  129. (push `(put ',name 'lisp-indent-function ',(cadr decl))
  130. exps))
  131. (doc-string
  132. (push `(put ',name 'doc-string-elt ',(cadr decl)) exps))))
  133. (when exps
  134. (setq exp `(progn ,exp ,@exps))))))
  135. exp)))
  136. ;; For defclass forms, use `eieio-defclass-autoload'.
  137. ((eq car 'defclass)
  138. (let ((name (nth 1 form))
  139. (superclasses (nth 2 form))
  140. (doc (nth 4 form)))
  141. (list 'eieio-defclass-autoload (list 'quote name)
  142. (list 'quote superclasses) file doc)))
  143. ;; Convert defcustom to less space-consuming data.
  144. ((eq car 'defcustom)
  145. (let ((varname (car-safe (cdr-safe form)))
  146. (init (car-safe (cdr-safe (cdr-safe form))))
  147. (doc (car-safe (cdr-safe (cdr-safe (cdr-safe form)))))
  148. ;; (rest (cdr-safe (cdr-safe (cdr-safe (cdr-safe form)))))
  149. )
  150. `(progn
  151. (defvar ,varname ,init ,doc)
  152. (custom-autoload ',varname ,file
  153. ,(condition-case nil
  154. (null (cadr (memq :set form)))
  155. (error nil))))))
  156. ((eq car 'defgroup)
  157. ;; In Emacs this is normally handled separately by cus-dep.el, but for
  158. ;; third party packages, it can be convenient to explicitly autoload
  159. ;; a group.
  160. (let ((groupname (nth 1 form)))
  161. `(let ((loads (get ',groupname 'custom-loads)))
  162. (if (member ',file loads) nil
  163. (put ',groupname 'custom-loads (cons ',file loads))))))
  164. ;; nil here indicates that this is not a special autoload form.
  165. (t nil))))
  166. ;; Forms which have doc-strings which should be printed specially.
  167. ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
  168. ;; the doc-string in FORM.
  169. ;; Those properties are now set in lisp-mode.el.
  170. (defun autoload-find-generated-file ()
  171. "Visit the autoload file for the current buffer, and return its buffer.
  172. If a buffer is visiting the desired autoload file, return it."
  173. (let ((enable-local-variables :safe)
  174. (enable-local-eval nil))
  175. ;; We used to use `raw-text' to read this file, but this causes
  176. ;; problems when the file contains non-ASCII characters.
  177. (find-file-noselect
  178. (autoload-ensure-default-file (autoload-generated-file)))))
  179. (defun autoload-generated-file ()
  180. (expand-file-name generated-autoload-file
  181. ;; File-local settings of generated-autoload-file should
  182. ;; be interpreted relative to the file's location,
  183. ;; of course.
  184. (if (not (local-variable-p 'generated-autoload-file))
  185. (expand-file-name "lisp" source-directory))))
  186. (defun autoload-read-section-header ()
  187. "Read a section header form.
  188. Since continuation lines have been marked as comments,
  189. we must copy the text of the form and remove those comment
  190. markers before we call `read'."
  191. (save-match-data
  192. (let ((beginning (point))
  193. string)
  194. (forward-line 1)
  195. (while (looking-at generate-autoload-section-continuation)
  196. (forward-line 1))
  197. (setq string (buffer-substring beginning (point)))
  198. (with-current-buffer (get-buffer-create " *autoload*")
  199. (erase-buffer)
  200. (insert string)
  201. (goto-char (point-min))
  202. (while (search-forward generate-autoload-section-continuation nil t)
  203. (replace-match " "))
  204. (goto-char (point-min))
  205. (read (current-buffer))))))
  206. (defvar autoload-print-form-outbuf nil
  207. "Buffer which gets the output of `autoload-print-form'.")
  208. (defun autoload-print-form (form)
  209. "Print FORM such that `make-docfile' will find the docstrings.
  210. The variable `autoload-print-form-outbuf' specifies the buffer to
  211. put the output in."
  212. (cond
  213. ;; If the form is a sequence, recurse.
  214. ((eq (car form) 'progn) (mapcar 'autoload-print-form (cdr form)))
  215. ;; Symbols at the toplevel are meaningless.
  216. ((symbolp form) nil)
  217. (t
  218. (let ((doc-string-elt (get (car-safe form) 'doc-string-elt))
  219. (outbuf autoload-print-form-outbuf))
  220. (if (and doc-string-elt (stringp (nth doc-string-elt form)))
  221. ;; We need to hack the printing because the
  222. ;; doc-string must be printed specially for
  223. ;; make-docfile (sigh).
  224. (let* ((p (nthcdr (1- doc-string-elt) form))
  225. (elt (cdr p)))
  226. (setcdr p nil)
  227. (princ "\n(" outbuf)
  228. (let ((print-escape-newlines t)
  229. (print-quoted t)
  230. (print-escape-nonascii t))
  231. (dolist (elt form)
  232. (prin1 elt outbuf)
  233. (princ " " outbuf)))
  234. (princ "\"\\\n" outbuf)
  235. (let ((begin (with-current-buffer outbuf (point))))
  236. (princ (substring (prin1-to-string (car elt)) 1)
  237. outbuf)
  238. ;; Insert a backslash before each ( that
  239. ;; appears at the beginning of a line in
  240. ;; the doc string.
  241. (with-current-buffer outbuf
  242. (save-excursion
  243. (while (re-search-backward "\n[[(]" begin t)
  244. (forward-char 1)
  245. (insert "\\"))))
  246. (if (null (cdr elt))
  247. (princ ")" outbuf)
  248. (princ " " outbuf)
  249. (princ (substring (prin1-to-string (cdr elt)) 1)
  250. outbuf))
  251. (terpri outbuf)))
  252. (let ((print-escape-newlines t)
  253. (print-quoted t)
  254. (print-escape-nonascii t))
  255. (print form outbuf)))))))
  256. (defun autoload-rubric (file &optional type feature)
  257. "Return a string giving the appropriate autoload rubric for FILE.
  258. TYPE (default \"autoloads\") is a string stating the type of
  259. information contained in FILE. If FEATURE is non-nil, FILE
  260. will provide a feature. FEATURE may be a string naming the
  261. feature, otherwise it will be based on FILE's name.
  262. At present, a feature is in fact always provided, but this should
  263. not be relied upon."
  264. (let ((basename (file-name-nondirectory file)))
  265. (concat ";;; " basename
  266. " --- automatically extracted " (or type "autoloads") "\n"
  267. ";;\n"
  268. ";;; Code:\n\n"
  269. " \n"
  270. ;; This is used outside of autoload.el, eg cus-dep, finder.
  271. "(provide '"
  272. (if (stringp feature)
  273. feature
  274. (file-name-sans-extension basename))
  275. ")\n"
  276. ";; Local Variables:\n"
  277. ";; version-control: never\n"
  278. ";; no-byte-compile: t\n"
  279. ";; no-update-autoloads: t\n"
  280. ";; coding: utf-8\n"
  281. ";; End:\n"
  282. ";;; " basename
  283. " ends here\n")))
  284. (defun autoload-ensure-default-file (file)
  285. "Make sure that the autoload file FILE exists and if not create it."
  286. (unless (file-exists-p file)
  287. (write-region (autoload-rubric file) nil file))
  288. file)
  289. (defun autoload-insert-section-header (outbuf autoloads load-name file time)
  290. "Insert the section-header line,
  291. which lists the file name and which functions are in it, etc."
  292. (insert generate-autoload-section-header)
  293. (prin1 (list 'autoloads autoloads load-name file time)
  294. outbuf)
  295. (terpri outbuf)
  296. ;; Break that line at spaces, to avoid very long lines.
  297. ;; Make each sub-line into a comment.
  298. (with-current-buffer outbuf
  299. (save-excursion
  300. (forward-line -1)
  301. (while (not (eolp))
  302. (move-to-column 64)
  303. (skip-chars-forward "^ \n")
  304. (or (eolp)
  305. (insert "\n" generate-autoload-section-continuation))))))
  306. (defun autoload-find-file (file)
  307. "Fetch file and put it in a temp buffer. Return the buffer."
  308. ;; It is faster to avoid visiting the file.
  309. (setq file (expand-file-name file))
  310. (with-current-buffer (get-buffer-create " *autoload-file*")
  311. (kill-all-local-variables)
  312. (erase-buffer)
  313. (setq buffer-undo-list t
  314. buffer-read-only nil)
  315. (emacs-lisp-mode)
  316. (setq default-directory (file-name-directory file))
  317. (insert-file-contents file nil)
  318. (let ((enable-local-variables :safe)
  319. (enable-local-eval nil))
  320. (hack-local-variables))
  321. (current-buffer)))
  322. (defvar no-update-autoloads nil
  323. "File local variable to prevent scanning this file for autoload cookies.")
  324. (defun autoload-file-load-name (file)
  325. "Compute the name that will be used to load FILE."
  326. ;; OUTFILE should be the name of the global loaddefs.el file, which
  327. ;; is expected to be at the root directory of the files we're
  328. ;; scanning for autoloads and will be in the `load-path'.
  329. (let* ((outfile (default-value 'generated-autoload-file))
  330. (name (file-relative-name file (file-name-directory outfile)))
  331. (names '())
  332. (dir (file-name-directory outfile)))
  333. ;; If `name' has directory components, only keep the
  334. ;; last few that are really needed.
  335. (while name
  336. (setq name (directory-file-name name))
  337. (push (file-name-nondirectory name) names)
  338. (setq name (file-name-directory name)))
  339. (while (not name)
  340. (cond
  341. ((null (cdr names)) (setq name (car names)))
  342. ((file-exists-p (expand-file-name "subdirs.el" dir))
  343. ;; FIXME: here we only check the existence of subdirs.el,
  344. ;; without checking its content. This makes it generate wrong load
  345. ;; names for cases like lisp/term which is not added to load-path.
  346. (setq dir (expand-file-name (pop names) dir)))
  347. (t (setq name (mapconcat 'identity names "/")))))
  348. (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
  349. (substring name 0 (match-beginning 0))
  350. name)))
  351. (defun generate-file-autoloads (file)
  352. "Insert at point a loaddefs autoload section for FILE.
  353. Autoloads are generated for defuns and defmacros in FILE
  354. marked by `generate-autoload-cookie' (which see).
  355. If FILE is being visited in a buffer, the contents of the buffer
  356. are used.
  357. Return non-nil in the case where no autoloads were added at point."
  358. (interactive "fGenerate autoloads for file: ")
  359. (let ((generated-autoload-file buffer-file-name))
  360. (autoload-generate-file-autoloads file (current-buffer))))
  361. (defvar print-readably)
  362. ;; When called from `generate-file-autoloads' we should ignore
  363. ;; `generated-autoload-file' altogether. When called from
  364. ;; `update-file-autoloads' we don't know `outbuf'. And when called from
  365. ;; `update-directory-autoloads' it's in between: we know the default
  366. ;; `outbuf' but we should obey any file-local setting of
  367. ;; `generated-autoload-file'.
  368. (defun autoload-generate-file-autoloads (file &optional outbuf outfile)
  369. "Insert an autoload section for FILE in the appropriate buffer.
  370. Autoloads are generated for defuns and defmacros in FILE
  371. marked by `generate-autoload-cookie' (which see).
  372. If FILE is being visited in a buffer, the contents of the buffer are used.
  373. OUTBUF is the buffer in which the autoload statements should be inserted.
  374. If OUTBUF is nil, it will be determined by `autoload-generated-file'.
  375. If provided, OUTFILE is expected to be the file name of OUTBUF.
  376. If OUTFILE is non-nil and FILE specifies a `generated-autoload-file'
  377. different from OUTFILE, then OUTBUF is ignored.
  378. Return non-nil if and only if FILE adds no autoloads to OUTFILE
  379. \(or OUTBUF if OUTFILE is nil)."
  380. (catch 'done
  381. (let ((autoloads-done '())
  382. load-name
  383. (print-length nil)
  384. (print-level nil)
  385. (print-readably t) ; This does something in Lucid Emacs.
  386. (float-output-format nil)
  387. (visited (get-file-buffer file))
  388. (otherbuf nil)
  389. (absfile (expand-file-name file))
  390. ;; nil until we found a cookie.
  391. output-start ostart)
  392. (with-current-buffer (or visited
  393. ;; It is faster to avoid visiting the file.
  394. (autoload-find-file file))
  395. ;; Obey the no-update-autoloads file local variable.
  396. (unless no-update-autoloads
  397. (message "Generating autoloads for %s..." file)
  398. (setq load-name
  399. (if (stringp generated-autoload-load-name)
  400. generated-autoload-load-name
  401. (autoload-file-load-name absfile)))
  402. (when (and outfile
  403. (not
  404. (if (memq system-type '(ms-dos windows-nt))
  405. (equal (downcase outfile)
  406. (downcase (autoload-generated-file)))
  407. (equal outfile (autoload-generated-file)))))
  408. (setq otherbuf t))
  409. (save-excursion
  410. (save-restriction
  411. (widen)
  412. (goto-char (point-min))
  413. (while (not (eobp))
  414. (skip-chars-forward " \t\n\f")
  415. (cond
  416. ((looking-at (regexp-quote generate-autoload-cookie))
  417. ;; If not done yet, figure out where to insert this text.
  418. (unless output-start
  419. (let ((outbuf
  420. (or (if otherbuf
  421. ;; A file-local setting of
  422. ;; autoload-generated-file says we
  423. ;; should ignore OUTBUF.
  424. nil
  425. outbuf)
  426. (autoload-find-destination absfile load-name)
  427. ;; The file has autoload cookies, but they're
  428. ;; already up-to-date. If OUTFILE is nil, the
  429. ;; entries are in the expected OUTBUF,
  430. ;; otherwise they're elsewhere.
  431. (throw 'done otherbuf))))
  432. (with-current-buffer outbuf
  433. (setq output-start (point-marker)
  434. ostart (point)))))
  435. (search-forward generate-autoload-cookie)
  436. (skip-chars-forward " \t")
  437. (if (eolp)
  438. (condition-case err
  439. ;; Read the next form and make an autoload.
  440. (let* ((form (prog1 (read (current-buffer))
  441. (or (bolp) (forward-line 1))))
  442. (autoload (make-autoload form load-name)))
  443. (if autoload
  444. (push (nth 1 form) autoloads-done)
  445. (setq autoload form))
  446. (let ((autoload-print-form-outbuf
  447. (marker-buffer output-start)))
  448. (autoload-print-form autoload)))
  449. (error
  450. (message "Autoload cookie error in %s:%s %S"
  451. file (count-lines (point-min) (point)) err)))
  452. ;; Copy the rest of the line to the output.
  453. (princ (buffer-substring
  454. (progn
  455. ;; Back up over whitespace, to preserve it.
  456. (skip-chars-backward " \f\t")
  457. (if (= (char-after (1+ (point))) ? )
  458. ;; Eat one space.
  459. (forward-char 1))
  460. (point))
  461. (progn (forward-line 1) (point)))
  462. (marker-buffer output-start))))
  463. ((looking-at ";")
  464. ;; Don't read the comment.
  465. (forward-line 1))
  466. (t
  467. (forward-sexp 1)
  468. (forward-line 1))))))
  469. (when output-start
  470. (let ((secondary-autoloads-file-buf
  471. (if otherbuf (current-buffer))))
  472. (with-current-buffer (marker-buffer output-start)
  473. (save-excursion
  474. ;; Insert the section-header line which lists the file name
  475. ;; and which functions are in it, etc.
  476. (assert (= ostart output-start))
  477. (goto-char output-start)
  478. (let ((relfile (file-relative-name absfile)))
  479. (autoload-insert-section-header
  480. (marker-buffer output-start)
  481. autoloads-done load-name relfile
  482. (if secondary-autoloads-file-buf
  483. ;; MD5 checksums are much better because they do not
  484. ;; change unless the file changes (so they'll be
  485. ;; equal on two different systems and will change
  486. ;; less often than time-stamps, thus leading to fewer
  487. ;; unneeded changes causing spurious conflicts), but
  488. ;; using time-stamps is a very useful optimization,
  489. ;; so we use time-stamps for the main autoloads file
  490. ;; (loaddefs.el) where we have special ways to
  491. ;; circumvent the "random change problem", and MD5
  492. ;; checksum in secondary autoload files where we do
  493. ;; not need the time-stamp optimization because it is
  494. ;; already provided by the primary autoloads file.
  495. (md5 secondary-autoloads-file-buf
  496. ;; We'd really want to just use
  497. ;; `emacs-internal' instead.
  498. nil nil 'emacs-mule-unix)
  499. (nth 5 (file-attributes relfile))))
  500. (insert ";;; Generated autoloads from " relfile "\n")))
  501. (insert generate-autoload-section-trailer))))
  502. (message "Generating autoloads for %s...done" file))
  503. (or visited
  504. ;; We created this buffer, so we should kill it.
  505. (kill-buffer (current-buffer))))
  506. (or (not output-start)
  507. ;; If the entries were added to some other buffer, then the file
  508. ;; doesn't add entries to OUTFILE.
  509. otherbuf))))
  510. (defun autoload-save-buffers ()
  511. (while autoload-modified-buffers
  512. (with-current-buffer (pop autoload-modified-buffers)
  513. (let ((version-control 'never))
  514. (save-buffer)))))
  515. ;;;###autoload
  516. (defun update-file-autoloads (file &optional save-after outfile)
  517. "Update the autoloads for FILE.
  518. If prefix arg SAVE-AFTER is non-nil, save the buffer too.
  519. If FILE binds `generated-autoload-file' as a file-local variable,
  520. autoloads are written into that file. Otherwise, the autoloads
  521. file is determined by OUTFILE. If called interactively, prompt
  522. for OUTFILE; if called from Lisp with OUTFILE nil, use the
  523. existing value of `generated-autoload-file'.
  524. Return FILE if there was no autoload cookie in it, else nil."
  525. (interactive (list (read-file-name "Update autoloads for file: ")
  526. current-prefix-arg
  527. (read-file-name "Write autoload definitions to file: ")))
  528. (let* ((generated-autoload-file (or outfile generated-autoload-file))
  529. (autoload-modified-buffers nil)
  530. (no-autoloads (autoload-generate-file-autoloads file)))
  531. (if autoload-modified-buffers
  532. (if save-after (autoload-save-buffers))
  533. (if (called-interactively-p 'interactive)
  534. (message "Autoload section for %s is up to date." file)))
  535. (if no-autoloads file)))
  536. (defun autoload-find-destination (file load-name)
  537. "Find the destination point of the current buffer's autoloads.
  538. FILE is the file name of the current buffer.
  539. Returns a buffer whose point is placed at the requested location.
  540. Returns nil if the file's autoloads are uptodate, otherwise
  541. removes any prior now out-of-date autoload entries."
  542. (catch 'up-to-date
  543. (let* ((buf (current-buffer))
  544. (existing-buffer (if buffer-file-name buf))
  545. (found nil))
  546. (with-current-buffer (autoload-find-generated-file)
  547. ;; This is to make generated-autoload-file have Unix EOLs, so
  548. ;; that it is portable to all platforms.
  549. (or (eq 0 (coding-system-eol-type buffer-file-coding-system))
  550. (set-buffer-file-coding-system 'unix))
  551. (or (> (buffer-size) 0)
  552. (error "Autoloads file %s lacks boilerplate" buffer-file-name))
  553. (or (file-writable-p buffer-file-name)
  554. (error "Autoloads file %s is not writable" buffer-file-name))
  555. (widen)
  556. (goto-char (point-min))
  557. ;; Look for the section for LOAD-NAME.
  558. (while (and (not found)
  559. (search-forward generate-autoload-section-header nil t))
  560. (let ((form (autoload-read-section-header)))
  561. (cond ((string= (nth 2 form) load-name)
  562. ;; We found the section for this file.
  563. ;; Check if it is up to date.
  564. (let ((begin (match-beginning 0))
  565. (last-time (nth 4 form))
  566. (file-time (nth 5 (file-attributes file))))
  567. (if (and (or (null existing-buffer)
  568. (not (buffer-modified-p existing-buffer)))
  569. (or
  570. ;; last-time is the time-stamp (specifying
  571. ;; the last time we looked at the file) and
  572. ;; the file hasn't been changed since.
  573. (and (listp last-time) (= (length last-time) 2)
  574. (not (time-less-p last-time file-time)))
  575. ;; last-time is an MD5 checksum instead.
  576. (and (stringp last-time)
  577. (equal last-time
  578. (md5 buf nil nil 'emacs-mule)))))
  579. (throw 'up-to-date nil)
  580. (autoload-remove-section begin)
  581. (setq found t))))
  582. ((string< load-name (nth 2 form))
  583. ;; We've come to a section alphabetically later than
  584. ;; LOAD-NAME. We assume the file is in order and so
  585. ;; there must be no section for LOAD-NAME. We will
  586. ;; insert one before the section here.
  587. (goto-char (match-beginning 0))
  588. (setq found t)))))
  589. (or found
  590. (progn
  591. ;; No later sections in the file. Put before the last page.
  592. (goto-char (point-max))
  593. (search-backward "\f" nil t)))
  594. (unless (memq (current-buffer) autoload-modified-buffers)
  595. (push (current-buffer) autoload-modified-buffers))
  596. (current-buffer)))))
  597. (defun autoload-remove-section (begin)
  598. (goto-char begin)
  599. (search-forward generate-autoload-section-trailer)
  600. (delete-region begin (point)))
  601. ;;;###autoload
  602. (defun update-directory-autoloads (&rest dirs)
  603. "Update autoload definitions for Lisp files in the directories DIRS.
  604. In an interactive call, you must give one argument, the name of a
  605. single directory. In a call from Lisp, you can supply multiple
  606. directories as separate arguments, but this usage is discouraged.
  607. The function does NOT recursively descend into subdirectories of the
  608. directory or directories specified.
  609. In an interactive call, prompt for a default output file for the
  610. autoload definitions, and temporarily bind the variable
  611. `generated-autoload-file' to this value. When called from Lisp,
  612. use the existing value of `generated-autoload-file'. If any Lisp
  613. file binds `generated-autoload-file' as a file-local variable,
  614. write its autoloads into the specified file instead."
  615. (interactive "DUpdate autoloads from directory: ")
  616. (let* ((files-re (let ((tmp nil))
  617. (dolist (suf (get-load-suffixes)
  618. (concat "^[^=.].*" (regexp-opt tmp t) "\\'"))
  619. (unless (string-match "\\.elc" suf) (push suf tmp)))))
  620. (files (apply 'nconc
  621. (mapcar (lambda (dir)
  622. (directory-files (expand-file-name dir)
  623. t files-re))
  624. dirs)))
  625. (done ())
  626. (this-time (current-time))
  627. ;; Files with no autoload cookies or whose autoloads go to other
  628. ;; files because of file-local autoload-generated-file settings.
  629. (no-autoloads nil)
  630. (autoload-modified-buffers nil)
  631. (generated-autoload-file
  632. (if (called-interactively-p 'interactive)
  633. (read-file-name "Write autoload definitions to file: ")
  634. generated-autoload-file)))
  635. (with-current-buffer (autoload-find-generated-file)
  636. (save-excursion
  637. ;; Canonicalize file names and remove the autoload file itself.
  638. (setq files (delete (file-relative-name buffer-file-name)
  639. (mapcar 'file-relative-name files)))
  640. (goto-char (point-min))
  641. (while (search-forward generate-autoload-section-header nil t)
  642. (let* ((form (autoload-read-section-header))
  643. (file (nth 3 form)))
  644. (cond ((and (consp file) (stringp (car file)))
  645. ;; This is a list of files that have no autoload cookies.
  646. ;; There shouldn't be more than one such entry.
  647. ;; Remove the obsolete section.
  648. (autoload-remove-section (match-beginning 0))
  649. (let ((last-time (nth 4 form)))
  650. (dolist (file file)
  651. (let ((file-time (nth 5 (file-attributes file))))
  652. (when (and file-time
  653. (not (time-less-p last-time file-time)))
  654. ;; file unchanged
  655. (push file no-autoloads)
  656. (setq files (delete file files)))))))
  657. ((not (stringp file)))
  658. ((or (not (file-exists-p file))
  659. ;; Remove duplicates as well, just in case.
  660. (member file done)
  661. ;; If the file is actually excluded.
  662. (member (expand-file-name file) autoload-excludes))
  663. ;; Remove the obsolete section.
  664. (autoload-remove-section (match-beginning 0)))
  665. ((not (time-less-p (nth 4 form)
  666. (nth 5 (file-attributes file))))
  667. ;; File hasn't changed.
  668. nil)
  669. (t
  670. (autoload-remove-section (match-beginning 0))
  671. (if (autoload-generate-file-autoloads
  672. ;; Passing `current-buffer' makes it insert at point.
  673. file (current-buffer) buffer-file-name)
  674. (push file no-autoloads))))
  675. (push file done)
  676. (setq files (delete file files)))))
  677. ;; Elements remaining in FILES have no existing autoload sections yet.
  678. (dolist (file files)
  679. (cond
  680. ((member (expand-file-name file) autoload-excludes) nil)
  681. ;; Passing nil as second argument forces
  682. ;; autoload-generate-file-autoloads to look for the right
  683. ;; spot where to insert each autoloads section.
  684. ((autoload-generate-file-autoloads file nil buffer-file-name)
  685. (push file no-autoloads))))
  686. (when no-autoloads
  687. ;; Sort them for better readability.
  688. (setq no-autoloads (sort no-autoloads 'string<))
  689. ;; Add the `no-autoloads' section.
  690. (goto-char (point-max))
  691. (search-backward "\f" nil t)
  692. (autoload-insert-section-header
  693. (current-buffer) nil nil no-autoloads this-time)
  694. (insert generate-autoload-section-trailer))
  695. (let ((version-control 'never))
  696. (save-buffer))
  697. ;; In case autoload entries were added to other files because of
  698. ;; file-local autoload-generated-file settings.
  699. (autoload-save-buffers))))
  700. (define-obsolete-function-alias 'update-autoloads-from-directories
  701. 'update-directory-autoloads "22.1")
  702. (defvar autoload-make-program (or (getenv "MAKE") "make")
  703. "Name of the make program in use during the Emacs build process.")
  704. ;;;###autoload
  705. (defun batch-update-autoloads ()
  706. "Update loaddefs.el autoloads in batch mode.
  707. Calls `update-directory-autoloads' on the command line arguments.
  708. Definitions are written to `generated-autoload-file' (which
  709. should be non-nil)."
  710. ;; For use during the Emacs build process only.
  711. ;; Exclude those files that are preloaded on ALL platforms.
  712. ;; These are the ones in loadup.el where "(load" is at the start
  713. ;; of the line (crude, but it works).
  714. (unless autoload-excludes
  715. (let ((default-directory (file-name-directory generated-autoload-file))
  716. file)
  717. (when (file-readable-p "loadup.el")
  718. (with-temp-buffer
  719. (insert-file-contents "loadup.el")
  720. (while (re-search-forward "^(load \"\\([^\"]+\\)\"" nil t)
  721. (setq file (match-string 1))
  722. (or (string-match "\\.el\\'" file)
  723. (setq file (format "%s.el" file)))
  724. (or (string-match "\\`site-" file)
  725. (push (expand-file-name file) autoload-excludes)))))))
  726. (let ((args command-line-args-left))
  727. (setq command-line-args-left nil)
  728. (apply 'update-directory-autoloads args)))
  729. (provide 'autoload)
  730. ;;; autoload.el ends here