comparison lisp/emacs-lisp/autoload.el @ 81590:ffabac9ec014

(autoload-generated-file): New function. (update-file-autoloads, update-directory-autoloads): Use it. (autoload-file-load-name): New function. (generate-file-autoloads, update-file-autoloads): Use it. (autoload-find-file): Accept non-absolute argument. Set default-dir. (generate-file-autoloads): If the autoloaded form is malformed, indicate the problem with a warning instead of aborting.
author Stefan Monnier <monnier@iro.umontreal.ca>
date Sat, 23 Jun 2007 20:31:33 +0000
parents a70527c58a3f
children ea507ef94ad6
comparison
equal deleted inserted replaced
81589:cf8be6c6e026 81590:ffabac9ec014
39 (defvar generated-autoload-file "loaddefs.el" 39 (defvar generated-autoload-file "loaddefs.el"
40 "*File \\[update-file-autoloads] puts autoloads into. 40 "*File \\[update-file-autoloads] puts autoloads into.
41 A `.el' file can set this in its local variables section to make its 41 A `.el' file can set this in its local variables section to make its
42 autoloads go somewhere else. The autoload file is assumed to contain a 42 autoloads go somewhere else. The autoload file is assumed to contain a
43 trailer starting with a FormFeed character.") 43 trailer starting with a FormFeed character.")
44 44 (put 'generated-autoload-file 'safe-local-variable 'stringp)
45 (defconst generate-autoload-cookie ";;;###autoload" 45
46 ;; This feels like it should be a defconst, but MH-E sets it to
47 ;; ";;;###mh-autoload" for the autoloads that are to go into mh-loaddefs.el.
48 (defvar generate-autoload-cookie ";;;###autoload"
46 "Magic comment indicating the following form should be autoloaded. 49 "Magic comment indicating the following form should be autoloaded.
47 Used by \\[update-file-autoloads]. This string should be 50 Used by \\[update-file-autoloads]. This string should be
48 meaningless to Lisp (e.g., a comment). 51 meaningless to Lisp (e.g., a comment).
49 52
50 This string is used: 53 This string is used:
51 54
52 ;;;###autoload 55 \;;;###autoload
53 \(defun function-to-be-autoloaded () ...) 56 \(defun function-to-be-autoloaded () ...)
54 57
55 If this string appears alone on a line, the following form will be 58 If this string appears alone on a line, the following form will be
56 read and an autoload made for it. If there is further text on the line, 59 read and an autoload made for it. If there is further text on the line,
57 that text will be copied verbatim to `generated-autoload-file'.") 60 that text will be copied verbatim to `generated-autoload-file'.")
147 ;; Forms which have doc-strings which should be printed specially. 150 ;; Forms which have doc-strings which should be printed specially.
148 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is 151 ;; A doc-string-elt property of ELT says that (nth ELT FORM) is
149 ;; the doc-string in FORM. 152 ;; the doc-string in FORM.
150 ;; Those properties are now set in lisp-mode.el. 153 ;; Those properties are now set in lisp-mode.el.
151 154
155 (defun autoload-generated-file ()
156 (expand-file-name generated-autoload-file
157 (expand-file-name "lisp"
158 source-directory)))
152 159
153 (defun autoload-trim-file-name (file) 160 (defun autoload-trim-file-name (file)
154 ;; Returns a relative file path for FILE 161 ;; Returns a relative file path for FILE
155 ;; starting from the directory that loaddefs.el is in. 162 ;; starting from the directory that loaddefs.el is in.
156 ;; That is normally a directory in load-path, 163 ;; That is normally a directory in load-path,
270 (insert "\n" generate-autoload-section-continuation)))))) 277 (insert "\n" generate-autoload-section-continuation))))))
271 278
272 (defun autoload-find-file (file) 279 (defun autoload-find-file (file)
273 "Fetch file and put it in a temp buffer. Return the buffer." 280 "Fetch file and put it in a temp buffer. Return the buffer."
274 ;; It is faster to avoid visiting the file. 281 ;; It is faster to avoid visiting the file.
282 (setq file (expand-file-name file))
275 (with-current-buffer (get-buffer-create " *autoload-file*") 283 (with-current-buffer (get-buffer-create " *autoload-file*")
276 (kill-all-local-variables) 284 (kill-all-local-variables)
277 (erase-buffer) 285 (erase-buffer)
278 (setq buffer-undo-list t 286 (setq buffer-undo-list t
279 buffer-read-only nil) 287 buffer-read-only nil)
280 (emacs-lisp-mode) 288 (emacs-lisp-mode)
289 (setq default-directory (file-name-directory file))
281 (insert-file-contents file nil) 290 (insert-file-contents file nil)
282 (let ((enable-local-variables :safe)) 291 (let ((enable-local-variables :safe))
283 (hack-local-variables)) 292 (hack-local-variables))
284 (current-buffer))) 293 (current-buffer)))
285 294
286 (defvar no-update-autoloads nil 295 (defvar no-update-autoloads nil
287 "File local variable to prevent scanning this file for autoload cookies.") 296 "File local variable to prevent scanning this file for autoload cookies.")
297
298 (defun autoload-file-load-name (file)
299 (let ((name (file-name-nondirectory file)))
300 (if (string-match "\\.elc?\\(\\.\\|\\'\\)" name)
301 (substring name 0 (match-beginning 0))
302 name)))
288 303
289 (defun generate-file-autoloads (file) 304 (defun generate-file-autoloads (file)
290 "Insert at point a loaddefs autoload section for FILE. 305 "Insert at point a loaddefs autoload section for FILE.
291 Autoloads are generated for defuns and defmacros in FILE 306 Autoloads are generated for defuns and defmacros in FILE
292 marked by `generate-autoload-cookie' (which see). 307 marked by `generate-autoload-cookie' (which see).
294 are used. 309 are used.
295 Return non-nil in the case where no autoloads were added at point." 310 Return non-nil in the case where no autoloads were added at point."
296 (interactive "fGenerate autoloads for file: ") 311 (interactive "fGenerate autoloads for file: ")
297 (let ((outbuf (current-buffer)) 312 (let ((outbuf (current-buffer))
298 (autoloads-done '()) 313 (autoloads-done '())
299 (load-name (let ((name (file-name-nondirectory file))) 314 (load-name (autoload-file-load-name file))
300 (if (string-match "\\.elc?\\(\\.\\|$\\)" name)
301 (substring name 0 (match-beginning 0))
302 name)))
303 (print-length nil) 315 (print-length nil)
304 (print-readably t) ; This does something in Lucid Emacs. 316 (print-readably t) ; This does something in Lucid Emacs.
305 (float-output-format nil) 317 (float-output-format nil)
306 (done-any nil) 318 (done-any nil)
307 (visited (get-file-buffer file)) 319 (visited (get-file-buffer file))
340 ((looking-at (regexp-quote generate-autoload-cookie)) 352 ((looking-at (regexp-quote generate-autoload-cookie))
341 (search-forward generate-autoload-cookie) 353 (search-forward generate-autoload-cookie)
342 (skip-chars-forward " \t") 354 (skip-chars-forward " \t")
343 (setq done-any t) 355 (setq done-any t)
344 (if (eolp) 356 (if (eolp)
345 ;; Read the next form and make an autoload. 357 (condition-case err
346 (let* ((form (prog1 (read (current-buffer)) 358 ;; Read the next form and make an autoload.
347 (or (bolp) (forward-line 1)))) 359 (let* ((form (prog1 (read (current-buffer))
348 (autoload (make-autoload form load-name))) 360 (or (bolp) (forward-line 1))))
349 (if autoload 361 (autoload (make-autoload form load-name)))
350 (push (nth 1 form) autoloads-done) 362 (if autoload
351 (setq autoload form)) 363 (push (nth 1 form) autoloads-done)
352 (let ((autoload-print-form-outbuf outbuf)) 364 (setq autoload form))
353 (autoload-print-form autoload))) 365 (let ((autoload-print-form-outbuf outbuf))
366 (autoload-print-form autoload)))
367 (error
368 (message "Error in %s: %S" file err)))
354 369
355 ;; Copy the rest of the line to the output. 370 ;; Copy the rest of the line to the output.
356 (princ (buffer-substring 371 (princ (buffer-substring
357 (progn 372 (progn
358 ;; Back up over whitespace, to preserve it. 373 ;; Back up over whitespace, to preserve it.
395 If SAVE-AFTER is non-nil (which is always, when called interactively), 410 If SAVE-AFTER is non-nil (which is always, when called interactively),
396 save the buffer too. 411 save the buffer too.
397 412
398 Return FILE if there was no autoload cookie in it, else nil." 413 Return FILE if there was no autoload cookie in it, else nil."
399 (interactive "fUpdate autoloads for file: \np") 414 (interactive "fUpdate autoloads for file: \np")
400 (let ((load-name (let ((name (file-name-nondirectory file))) 415 (let ((load-name (autoload-file-load-name file))
401 (if (string-match "\\.elc?\\(\\.\\|$\\)" name)
402 (substring name 0 (match-beginning 0))
403 name)))
404 (found nil) 416 (found nil)
405 (existing-buffer (get-file-buffer file)) 417 (existing-buffer (get-file-buffer file))
406 (no-autoloads nil)) 418 (no-autoloads nil))
407 (save-excursion 419 (save-excursion
408 ;; We want to get a value for generated-autoload-file from 420 ;; We want to get a value for generated-autoload-file from
411 (set-buffer existing-buffer)) 423 (set-buffer existing-buffer))
412 ;; We must read/write the file without any code conversion, 424 ;; We must read/write the file without any code conversion,
413 ;; but still decode EOLs. 425 ;; but still decode EOLs.
414 (let ((coding-system-for-read 'raw-text)) 426 (let ((coding-system-for-read 'raw-text))
415 (set-buffer (find-file-noselect 427 (set-buffer (find-file-noselect
416 (autoload-ensure-default-file 428 (autoload-ensure-default-file (autoload-generated-file))))
417 (expand-file-name generated-autoload-file
418 (expand-file-name "lisp"
419 source-directory)))))
420 ;; This is to make generated-autoload-file have Unix EOLs, so 429 ;; This is to make generated-autoload-file have Unix EOLs, so
421 ;; that it is portable to all platforms. 430 ;; that it is portable to all platforms.
422 (setq buffer-file-coding-system 'raw-text-unix)) 431 (setq buffer-file-coding-system 'raw-text-unix))
423 (or (> (buffer-size) 0) 432 (or (> (buffer-size) 0)
424 (error "Autoloads file %s does not exist" buffer-file-name)) 433 (error "Autoloads file %s does not exist" buffer-file-name))
498 (directory-files (expand-file-name dir) 507 (directory-files (expand-file-name dir)
499 t files-re)) 508 t files-re))
500 dirs))) 509 dirs)))
501 (this-time (current-time)) 510 (this-time (current-time))
502 (no-autoloads nil) ;files with no autoload cookies. 511 (no-autoloads nil) ;files with no autoload cookies.
503 (autoloads-file 512 (autoloads-file (autoload-generated-file))
504 (expand-file-name generated-autoload-file
505 (expand-file-name "lisp" source-directory)))
506 (top-dir (file-name-directory autoloads-file))) 513 (top-dir (file-name-directory autoloads-file)))
507 514
508 (with-current-buffer 515 (with-current-buffer
509 (find-file-noselect (autoload-ensure-default-file autoloads-file)) 516 (find-file-noselect (autoload-ensure-default-file autoloads-file))
510 (save-excursion 517 (save-excursion