(lm-with-file): Use mode and syntax table for Emacs Lisp, not Lisp.
[emacs.git] / lisp / emacs-lisp / lisp-mnt.el
blobfa457acec966adb623697d57d1bc35cf59105551
1 ;;; lisp-mnt.el --- utility functions for Emacs Lisp maintainers
3 ;; Copyright (C) 1992, 1994, 1997, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Maintainer: FSF
8 ;; Created: 14 Jul 1992
9 ;; Keywords: docs
10 ;; X-Bogus-Bureaucratic-Cruft: Gruad will get you if you don't watch out!
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software; you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs; see the file COPYING. If not, write to
26 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
27 ;; Boston, MA 02110-1301, USA.
29 ;;; Commentary:
31 ;; This library adds some services to Emacs-Lisp editing mode.
33 ;; First, it knows about the header conventions for library packages.
34 ;; One entry point supports generating synopses from a library directory.
35 ;; Another can be used to check for missing headers in library files.
37 ;; Another entry point automatically addresses bug mail to a package's
38 ;; maintainer or author.
40 ;; This file can be loaded by your emacs-lisp-mode-hook. Have it
41 ;; (require 'lisp-mnt)
43 ;; This file is an example of the header conventions. Note the following
44 ;; features:
46 ;; * Header line --- makes it possible to extract a one-line summary of
47 ;; the package's uses automatically for use in library synopses, KWIC
48 ;; indexes and the like.
50 ;; Format is three semicolons, followed by the filename, followed by
51 ;; three dashes, followed by the summary. All fields space-separated.
53 ;; * A blank line
55 ;; * Copyright line, which looks more or less like this:
57 ;; ;; Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc.
59 ;; * A blank line
61 ;; * Author line --- contains the name and net address of at least
62 ;; the principal author.
64 ;; If there are multiple authors, they should be listed on continuation
65 ;; lines led by ;;<TAB>, like this:
67 ;; ;; Author: Ashwin Ram <Ram-Ashwin@cs.yale.edu>
68 ;; ;; Dave Sill <de5@ornl.gov>
69 ;; ;; David Lawrence <tale@pawl.rpi.edu>
70 ;; ;; Noah Friedman <friedman@ai.mit.edu>
71 ;; ;; Joe Wells <jbw@maverick.uswest.com>
72 ;; ;; Dave Brennan <brennan@hal.com>
73 ;; ;; Eric Raymond <esr@snark.thyrsus.com>
75 ;; This field may have some special values; notably "FSF", meaning
76 ;; "Free Software Foundation".
78 ;; * Maintainer line --- should be a single name/address as in the Author
79 ;; line, or an address only, or the string "FSF". If there is no maintainer
80 ;; line, the person(s) in the Author field are presumed to be it. The example
81 ;; in this file is mildly bogus because the maintainer line is redundant.
82 ;; The idea behind these two fields is to be able to write a Lisp function
83 ;; that does "send mail to the author" without having to mine the name out by
84 ;; hand. Please be careful about surrounding the network address with <> if
85 ;; there's also a name in the field.
87 ;; * Created line --- optional, gives the original creation date of the
88 ;; file. For historical interest, basically.
90 ;; * Version line --- intended to give the reader a clue if they're looking
91 ;; at a different version of the file than the one they're accustomed to. This
92 ;; may be an RCS or SCCS header.
94 ;; * Adapted-By line --- this is for FSF's internal use. The person named
95 ;; in this field was the one responsible for installing and adapting the
96 ;; package for the distribution. (This file doesn't have one because the
97 ;; author *is* one of the maintainers.)
99 ;; * Keywords line --- used by the finder code (now under construction)
100 ;; for finding Emacs Lisp code related to a topic.
102 ;; * X-Bogus-Bureaucratic-Cruft line --- this is a joke and an example
103 ;; of a comment header. Headers starting with `X-' should never be used
104 ;; for any real purpose; this is the way to safely add random headers
105 ;; without invoking the wrath of any program.
107 ;; * Commentary line --- enables Lisp code to find the developer's and
108 ;; maintainers' explanations of the package internals.
110 ;; * Change log line --- optional, exists to terminate the commentary
111 ;; section and start a change-log part, if one exists.
113 ;; * Code line --- exists so Lisp can know where commentary and/or
114 ;; change-log sections end.
116 ;; * Footer line --- marks end-of-file so it can be distinguished from
117 ;; an expanded formfeed or the results of truncation.
119 ;;; Change Log:
121 ;; Tue Jul 14 23:44:17 1992 ESR
122 ;; * Created.
124 ;;; Code:
126 ;;; Variables:
128 (defgroup lisp-mnt nil
129 "Utility functions for Emacs Lisp maintainers."
130 :prefix "lm-"
131 :group 'maint)
133 ;; At least some of these defcustoms should probably be defconsts,
134 ;; since they define, or are defined by, the header format. -- fx
136 (defcustom lm-header-prefix "^;+[ \t]+\\(@(#)\\)?[ \t]*\\$?"
137 "Prefix that is ignored before the tag.
138 For example, you can write the 1st line synopsis string and headers like this
139 in your Lisp package:
141 ;; @(#) package.el -- package description
143 ;; @(#) $Maintainer: Person Foo Bar $
145 The @(#) construct is used by unix what(1) and
146 then $identifier: doc string $ is used by GNU ident(1)"
147 :type 'regexp
148 :group 'lisp-mnt)
150 (defcustom lm-copyright-prefix "^\\(;+[ \t]\\)+Copyright (C) "
151 "Prefix that is ignored before the dates in a copyright.
152 Leading comment characters and whitespace should be in regexp group 1."
153 :type 'regexp
154 :group 'lisp-mnt)
156 (defcustom lm-comment-column 16
157 "Column used for placing formatted output."
158 :type 'integer
159 :group 'lisp-mnt)
161 (defcustom lm-any-header ".*"
162 "Regexp which matches start of any section."
163 :type 'regexp
164 :group 'lisp-mnt)
166 (defcustom lm-commentary-header "Commentary\\|Documentation"
167 "Regexp which matches start of documentation section."
168 :type 'regexp
169 :group 'lisp-mnt)
171 (defcustom lm-history-header "Change ?Log\\|History"
172 "Regexp which matches the start of code log section."
173 :type 'regexp
174 :group 'lisp-mnt)
176 ;;; Functions:
178 ;; These functions all parse the headers of the current buffer
180 (defun lm-get-header-re (header &optional mode)
181 "Return regexp for matching HEADER.
182 If called with optional MODE and with value `section',
183 return section regexp instead."
184 (if (eq mode 'section)
185 (concat "^;;;;* \\(" header "\\):[ \t]*$")
186 (concat lm-header-prefix "\\(" header "\\)[ \t]*:[ \t]*")))
188 (defun lm-get-package-name ()
189 "Return package name by looking at the first line."
190 (save-excursion
191 (goto-char (point-min))
192 (if (and (looking-at (concat lm-header-prefix))
193 (progn (goto-char (match-end 0))
194 (looking-at "\\([^\t ]+\\)")
195 (match-end 1)))
196 (match-string-no-properties 1))))
198 (defun lm-section-start (header &optional after)
199 "Return the buffer location of a given section start marker.
200 The HEADER is the section mark string to search for.
201 If AFTER is non-nil, return the location of the next line.
202 If the given section does not exist, return nil."
203 (save-excursion
204 (let ((case-fold-search t))
205 (goto-char (point-min))
206 (if (re-search-forward (lm-get-header-re header 'section) nil t)
207 (line-beginning-position (if after 2))))))
208 (defalias 'lm-section-mark 'lm-section-start)
210 (defun lm-section-end (header)
211 "Return the buffer location of the end of a given section.
212 The HEADER is the section string marking the beginning of the
213 section. If the given section does not exist, return nil.
215 The end of the section is defined as the beginning of the next
216 section of the same level or lower. The function
217 `lisp-outline-level' is used to compute the level of a section.
218 If no such section exists, return the end of the buffer."
219 (require 'outline) ;; for outline-regexp.
220 (let ((start (lm-section-start header)))
221 (when start
222 (save-excursion
223 (goto-char start)
224 (let ((level (lisp-outline-level))
225 (case-fold-search t)
226 next-section-found)
227 (beginning-of-line 2)
228 (while (and (setq next-section-found
229 (re-search-forward
230 (lm-get-header-re lm-any-header 'section)
231 nil t))
232 (> (save-excursion
233 (beginning-of-line)
234 (lisp-outline-level))
235 level)))
236 (if next-section-found
237 (line-beginning-position)
238 (point-max)))))))
240 (defsubst lm-code-start ()
241 "Return the buffer location of the `Code' start marker."
242 (lm-section-start "Code"))
243 (defalias 'lm-code-mark 'lm-code-start)
245 (defsubst lm-commentary-start ()
246 "Return the buffer location of the `Commentary' start marker."
247 (lm-section-start lm-commentary-header))
248 (defalias 'lm-commentary-mark 'lm-commentary-start)
250 (defsubst lm-commentary-end ()
251 "Return the buffer location of the `Commentary' section end."
252 (lm-section-end lm-commentary-header))
254 (defsubst lm-history-start ()
255 "Return the buffer location of the `History' start marker."
256 (lm-section-start lm-history-header))
257 (defalias 'lm-history-mark 'lm-history-start)
259 (defsubst lm-copyright-mark ()
260 "Return the buffer location of the `Copyright' line."
261 (save-excursion
262 (let ((case-fold-search t))
263 (goto-char (point-min))
264 (if (re-search-forward lm-copyright-prefix nil t)
265 (point)))))
267 (defun lm-header (header)
268 "Return the contents of the header named HEADER."
269 (goto-char (point-min))
270 (let ((case-fold-search t))
271 (when (and (re-search-forward (lm-get-header-re header) (lm-code-mark) t)
272 ;; RCS ident likes format "$identifier: data$"
273 (looking-at
274 (if (save-excursion
275 (skip-chars-backward "^$" (match-beginning 0))
276 (= (point) (match-beginning 0)))
277 "[^\n]+" "[^$\n]+")))
278 (match-string-no-properties 0))))
280 (defun lm-header-multiline (header)
281 "Return the contents of the header named HEADER, with continuation lines.
282 The returned value is a list of strings, one per line."
283 (save-excursion
284 (goto-char (point-min))
285 (let ((res (lm-header header)))
286 (when res
287 (setq res (list res))
288 (forward-line 1)
289 (while (and (or (looking-at (concat lm-header-prefix "[\t ]+"))
290 (and (not (looking-at
291 (lm-get-header-re "\\sw\\(\\sw\\|\\s_\\)*")))
292 (looking-at lm-header-prefix)))
293 (goto-char (match-end 0))
294 (looking-at ".+"))
295 (setq res (cons (match-string-no-properties 0) res))
296 (forward-line 1)))
297 (nreverse res))))
299 ;; These give us smart access to the header fields and commentary
301 (defmacro lm-with-file (file &rest body)
302 "Execute BODY in a buffer containing the contents of FILE.
303 If FILE is nil, execute BODY in the current buffer."
304 (let ((filesym (make-symbol "file")))
305 `(let ((,filesym ,file))
306 (if ,filesym
307 (with-temp-buffer
308 (insert-file-contents ,filesym)
309 (emacs-lisp-mode)
310 ,@body)
311 (save-excursion
312 ;; Switching major modes is too drastic, so just switch
313 ;; temporarily to the Emacs Lisp mode syntax table.
314 (with-syntax-table emacs-lisp-mode-syntax-table
315 ,@body))))))
317 (put 'lm-with-file 'lisp-indent-function 1)
318 (put 'lm-with-file 'edebug-form-spec t)
320 ;; Fixme: Probably this should be amalgamated with copyright.el; also
321 ;; we need a check for ranges in copyright years.
323 (defun lm-crack-copyright (&optional file)
324 "Return the copyright holder, and a list of copyright years.
325 Use the current buffer if FILE is nil.
326 Return argument is of the form (\"HOLDER\" \"YEAR1\" ... \"YEARN\")"
327 (lm-with-file file
328 (goto-char (lm-copyright-mark))
329 (let ((holder nil)
330 (years nil)
331 (start (point))
332 (end (line-end-position)))
333 ;; Cope with multi-line copyright `lines'. Assume the second
334 ;; line is indented (with the same commenting style).
335 (save-excursion
336 (beginning-of-line 2)
337 (let ((str (concat (match-string-no-properties 1) "[ \t]+")))
338 (beginning-of-line)
339 (while (looking-at str)
340 (setq end (line-end-position))
341 (beginning-of-line 2))))
342 ;; Make a single line and parse that.
343 (let ((buff (current-buffer)))
344 (with-temp-buffer
345 (insert-buffer-substring buff start end)
346 (goto-char (point-min))
347 (while (re-search-forward "^;+[ \t]+" nil t)
348 (replace-match ""))
349 (goto-char (point-min))
350 (while (re-search-forward " *\n" nil t)
351 (replace-match " "))
352 (goto-char (point-min))
353 (while (re-search-forward "\\([0-9]+\\),? +" nil t)
354 (setq years (cons (match-string-no-properties 1) years)))
355 (if (looking-at ".*$")
356 (setq holder (match-string-no-properties 0)))))
357 (cons holder (nreverse years)))))
359 (defun lm-summary (&optional file)
360 "Return the one-line summary of file FILE, or current buffer if FILE is nil."
361 (lm-with-file file
362 (goto-char (point-min))
363 (if (and (looking-at lm-header-prefix)
364 (progn (goto-char (match-end 0))
365 (looking-at "[^ ]+[ \t]+--+[ \t]+\\(.*\\)")))
366 (let ((summary (match-string-no-properties 1)))
367 ;; Strip off -*- specifications.
368 (if (string-match "[ \t]*-\\*-.*-\\*-" summary)
369 (substring summary 0 (match-beginning 0))
370 summary)))))
372 (defun lm-crack-address (x)
373 "Split up an email address X into full name and real email address.
374 The value is a cons of the form (FULLNAME . ADDRESS)."
375 (cond ((string-match "\\(.+\\) [(<]\\(\\S-+@\\S-+\\)[>)]" x)
376 (cons (match-string 1 x)
377 (match-string 2 x)))
378 ((string-match "\\(\\S-+@\\S-+\\) [(<]\\(.*\\)[>)]" x)
379 (cons (match-string 2 x)
380 (match-string 1 x)))
381 ((string-match "\\S-+@\\S-+" x)
382 (cons nil x))
384 (cons x nil))))
386 (defun lm-authors (&optional file)
387 "Return the author list of file FILE, or current buffer if FILE is nil.
388 Each element of the list is a cons; the car is the full name,
389 the cdr is an email address."
390 (lm-with-file file
391 (let ((authorlist (lm-header-multiline "author")))
392 (mapcar 'lm-crack-address authorlist))))
394 (defun lm-maintainer (&optional file)
395 "Return the maintainer of file FILE, or current buffer if FILE is nil.
396 The return value has the form (NAME . ADDRESS)."
397 (lm-with-file file
398 (let ((maint (lm-header "maintainer")))
399 (if maint
400 (lm-crack-address maint)
401 (car (lm-authors))))))
403 (defun lm-creation-date (&optional file)
404 "Return the created date given in file FILE, or current buffer if FILE is nil."
405 (lm-with-file file
406 (lm-header "created")))
408 (defun lm-last-modified-date (&optional file iso-date)
409 "Return the modify-date given in file FILE, or current buffer if FILE is nil.
410 ISO-DATE non-nil means return the date in ISO 8601 format."
411 (lm-with-file file
412 (when (progn (goto-char (point-min))
413 (re-search-forward
414 "\\$[I]d: [^ ]+ [^ ]+ \\([^/]+\\)/\\([^/]+\\)/\\([^ ]+\\) "
415 (lm-code-mark) t))
416 (let ((dd (match-string 3))
417 (mm (match-string 2))
418 (yyyy (match-string 1)))
419 (if iso-date
420 (format "%s-%s-%s" yyyy mm dd)
421 (format "%s %s %s"
423 (nth (string-to-number mm)
424 '("" "Jan" "Feb" "Mar" "Apr" "May" "Jun"
425 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))
426 yyyy))))))
428 (defun lm-version (&optional file)
429 "Return the version listed in file FILE, or current buffer if FILE is nil.
430 This can be found in an RCS or SCCS header."
431 (lm-with-file file
432 (or (lm-header "version")
433 (let ((header-max (lm-code-mark)))
434 (goto-char (point-min))
435 (cond
436 ;; Look for an RCS header
437 ((re-search-forward "\\$[I]d: [^ ]+ \\([^ ]+\\) " header-max t)
438 (match-string-no-properties 1))
439 ((re-search-forward "\\$Revision: +\\([^ ]+\\) " header-max t)
440 (match-string-no-properties 1))
441 ;; Look for an SCCS header
442 ((re-search-forward
443 (concat
444 (regexp-quote "@(#)")
445 (regexp-quote (file-name-nondirectory (buffer-file-name)))
446 "\t\\([012345679.]*\\)")
447 header-max t)
448 (match-string-no-properties 1)))))))
450 (defun lm-keywords (&optional file)
451 "Return the keywords given in file FILE, or current buffer if FILE is nil."
452 (lm-with-file file
453 (let ((keywords (lm-header "keywords")))
454 (and keywords (downcase keywords)))))
456 (defun lm-keywords-list (&optional file)
457 "Return list of keywords given in file FILE."
458 (let ((keywords (lm-keywords file)))
459 (if keywords
460 (split-string keywords ",?[ \t]"))))
462 (defvar finder-known-keywords)
463 (defun lm-keywords-finder-p (&optional file)
464 "Return non-nil if any keywords in FILE are known to finder."
465 (require 'finder)
466 (let ((keys (lm-keywords-list file)))
467 (catch 'keyword-found
468 (while keys
469 (if (assoc (intern (car keys)) finder-known-keywords)
470 (throw 'keyword-found t))
471 (setq keys (cdr keys)))
472 nil)))
474 (defun lm-adapted-by (&optional file)
475 "Return the adapted-by names in file FILE, or current buffer if FILE is nil.
476 This is the name of the person who cleaned up this package for
477 distribution."
478 (lm-with-file file
479 (lm-header "adapted-by")))
481 (defun lm-commentary (&optional file)
482 "Return the commentary in file FILE, or current buffer if FILE is nil.
483 Return the value as a string. In the file, the commentary
484 section starts with the tag `Commentary' or `Documentation' and
485 ends just before the next section. If the commentary section is
486 absent, return nil."
487 (lm-with-file file
488 (let ((start (lm-commentary-start)))
489 (when start
490 (buffer-substring-no-properties start (lm-commentary-end))))))
492 ;;; Verification and synopses
494 (defun lm-insert-at-column (col &rest strings)
495 "Insert, at column COL, list of STRINGS."
496 (if (> (current-column) col) (insert "\n"))
497 (move-to-column col t)
498 (apply 'insert strings))
500 (defun lm-verify (&optional file showok verbose non-fsf-ok)
501 "Check that the current buffer (or FILE if given) is in proper format.
502 If FILE is a directory, recurse on its files and generate a report in a
503 temporary buffer. In that case, the optional argument SHOWOK
504 says display \"OK\" in temp buffer for files that have no problems.
506 Optional argument VERBOSE specifies verbosity level.
507 Optional argument NON-FSF-OK if non-nil means a non-FSF
508 copyright notice is allowed."
509 (interactive (list nil nil t))
510 (let* ((ret (and verbose "Ok"))
511 name)
512 (if (and file (file-directory-p file))
513 (setq ret
514 (with-temp-buffer
515 (dolist (f (directory-files file nil "\\.el\\'")
516 (buffer-string))
517 (when (file-regular-p f)
518 (let ((status (lm-verify f)))
519 (insert f ":")
520 (if status
521 (lm-insert-at-column lm-comment-column status
522 "\n")
523 (if showok
524 (lm-insert-at-column lm-comment-column
525 "OK\n"))))))))
526 (lm-with-file file
527 (setq name (lm-get-package-name))
528 (setq ret
529 (cond
530 ((null name)
531 "Can't find package name")
532 ((not (lm-authors))
533 "`Author:' tag missing")
534 ((not (lm-maintainer))
535 "`Maintainer:' tag missing")
536 ((not (lm-summary))
537 "Can't find the one-line summary description")
538 ((not (lm-keywords))
539 "`Keywords:' tag missing")
540 ((not (lm-keywords-finder-p))
541 "`Keywords:' has no valid finder keywords (see `finder-known-keywords')")
542 ((not (lm-commentary-mark))
543 "Can't find a 'Commentary' section marker")
544 ((not (lm-history-mark))
545 "Can't find a 'History' section marker")
546 ((not (lm-code-mark))
547 "Can't find a 'Code' section marker")
548 ((progn
549 (goto-char (point-max))
550 (not
551 (re-search-backward
552 (concat "^;;;[ \t]+" name "[ \t]+ends here[ \t]*$"
553 "\\|^;;;[ \t]+ End of file[ \t]+" name)
554 nil t)))
555 "Can't find the footer line")
556 ((not (and (lm-copyright-mark) (lm-crack-copyright)))
557 "Can't find a valid copyright notice")
558 ((not (or non-fsf-ok
559 (string-match "Free Software Foundation"
560 (car (lm-crack-copyright)))))
561 "Copyright holder is not the Free Software Foundation")
563 ret)))))
564 (if verbose
565 (message "%s" ret))
566 ret))
568 (defun lm-synopsis (&optional file showall)
569 "Generate a synopsis listing for the buffer or the given FILE if given.
570 If FILE is a directory, recurse on its files and generate a report in
571 a temporary buffer. If SHOWALL is non-nil, also generate a line for files
572 which do not include a recognizable synopsis."
573 (interactive
574 (list
575 (read-file-name "Synopsis for (file or dir): ")))
577 (if (and file (file-directory-p file))
578 (with-output-to-temp-buffer "*Synopsis*"
579 (set-buffer standard-output)
580 (dolist (f (directory-files file nil ".*\\.el\\'"))
581 (let ((syn (lm-synopsis (expand-file-name f file))))
582 (when (or syn showall)
583 (insert f ":")
584 (lm-insert-at-column lm-comment-column (or syn "NA") "\n")))))
585 (save-excursion
586 (let ((must-kill (and file (not (get-file-buffer file)))))
587 (when file (find-file file))
588 (prog1
589 (if (interactive-p)
590 (message "%s" (lm-summary))
591 (lm-summary))
592 (when must-kill (kill-buffer (current-buffer))))))))
594 (eval-when-compile (defvar report-emacs-bug-address))
596 (defun lm-report-bug (topic)
597 "Report a bug in the package currently being visited to its maintainer.
598 Prompts for bug subject TOPIC. Leaves you in a mail buffer."
599 (interactive "sBug Subject: ")
600 (require 'emacsbug)
601 (let ((package (lm-get-package-name))
602 (addr (lm-maintainer))
603 (version (lm-version)))
604 (compose-mail (if addr
605 (concat (car addr) " <" (cdr addr) ">")
606 report-emacs-bug-address)
607 topic)
608 (goto-char (point-max))
609 (insert "\nIn " package)
610 (if version
611 (insert " version " version))
612 (newline 2)
613 (message "%s"
614 (substitute-command-keys "Type \\[mail-send] to send bug report."))))
616 (provide 'lisp-mnt)
618 ;;; arch-tag: fa3c5ab4-a37b-4e46-b7cf-b6d78b90e69e
619 ;;; lisp-mnt.el ends here