autoload.el 35 KB

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