1 ;;; lisp-mnt.el --- minor mode for Emacs Lisp maintainers
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
5 ;; Author: Eric S. Raymond <esr@snark.thyrsus.com>
6 ;; Maintainer: Eric S. Raymond <esr@snark.thyrsus.com>
7 ;; Created: 14 Jul 1992
8 ;; Keywords: docs, maint
9 ;; X-Modified-by: Bob Weiner <weiner@beopen.com>, 4/14/95, to support
11 ;; X-Bogus-Bureaucratic-Cruft: Gruad will get you if you don't watch out!
13 ;; This file is part of SXEmacs.
15 ;; SXEmacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; SXEmacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ;;; Synched up with: FSF 20.2.
32 ;; This minor mode adds some services to Emacs-Lisp editing mode.
34 ;; First, it knows about the header conventions for library packages.
35 ;; One entry point supports generating synopses from a library directory.
36 ;; Another can be used to check for missing headers in library files.
38 ;; Another entry point automatically addresses bug mail to a package's
39 ;; maintainer or author.
41 ;; This file can be loaded by your lisp-mode-hook. Have it (require 'lisp-mnt)
43 ;; This file is an example of the header conventions. Note the following
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 ;; * Author line --- contains the name and net address of at least
54 ;; the principal author.
56 ;; If there are multiple authors, they should be listed on continuation
57 ;; lines led by ;;<TAB><TAB> (or multiple blanks), like this:
59 ;; ;; Author: Ashwin Ram <Ram-Ashwin@cs.yale.edu>
60 ;; ;; Dave Sill <de5@ornl.gov>
61 ;; ;; David Lawrence <tale@pawl.rpi.edu>
62 ;; ;; Noah Friedman <friedman@ai.mit.edu>
63 ;; ;; Joe Wells <jbw@maverick.uswest.com>
64 ;; ;; Dave Brennan <brennan@hal.com>
65 ;; ;; Eric Raymond <esr@snark.thyrsus.com>
67 ;; This field may have some special values; notably "FSF", meaning
68 ;; "Free Software Foundation".
70 ;; * Maintainer line --- should be a single name/address as in the Author
71 ;; line, or an address only, or the string "FSF". If there is no maintainer
72 ;; line, the person(s) in the Author field are presumed to be it. The example
73 ;; in this file is mildly bogus because the maintainer line is redundant.
74 ;; The idea behind these two fields is to be able to write a Lisp function
75 ;; that does "send mail to the author" without having to mine the name out by
76 ;; hand. Please be careful about surrounding the network address with <> if
77 ;; there's also a name in the field.
79 ;; * Created line --- optional, gives the original creation date of the
80 ;; file. For historical interest, basically.
82 ;; * Version line --- intended to give the reader a clue if they're looking
83 ;; at a different version of the file than the one they're accustomed to. This
84 ;; may be an RCS or SCCS header.
86 ;; * Adapted-By line --- this is for FSF's internal use. The person named
87 ;; in this field was the one responsible for installing and adapting the
88 ;; package for the distribution. (This file doesn't have one because the
89 ;; author *is* one of the maintainers.)
91 ;; * Keywords line --- used by the finder code (now under construction)
92 ;; for finding Emacs Lisp code related to a topic.
94 ;; * X-Bogus-Bureaucratic-Cruft line --- this is a joke and an example
95 ;; of a comment header. Headers starting with `X-' should never be used
96 ;; for any real purpose; this is the way to safely add random headers
97 ;; without invoking the wrath of any program.
99 ;; * Commentary line --- enables Lisp code to find the developer's and
100 ;; maintainers' explanations of the package internals.
102 ;; * Change log line --- optional, exists to terminate the commentary
103 ;; section and start a change-log part, if one exists.
105 ;; * Code line --- exists so Lisp can know where commentary and/or
106 ;; change-log sections end.
108 ;; * Footer line --- marks end-of-file so it can be distinguished from
109 ;; an expanded formfeed or the results of truncation.
113 ;; Tue Jul 14 23:44:17 1992 ESR
118 (require 'picture) ; provides move-to-column-force
119 ;(require 'emacsbug) ; XEmacs, not needed for bytecompilation
123 (defvar lm-header-prefix "^;;*[ \t]+\\(@\(#\)\\)?[ \t]*\\([\$]\\)?"
124 "Prefix that is ignored before the tag.
125 For example, you can write the 1st line synopsis string and headers like this
126 in your Lisp package:
128 ;; @(#) package.el -- package description
130 ;; @(#) $Maintainer: Person Foo Bar $
132 The @(#) construct is used by unix what(1) and
133 then $identifier: doc string $ is used by GNU ident(1)")
135 (defvar lm-comment-column 16
136 "Column used for placing formatted output.")
138 (defvar lm-commentary-header "Commentary\\|Documentation"
139 "Regexp which matches start of documentation section.")
141 (defvar lm-history-header "Change Log\\|History"
142 "Regexp which matches the start of code log section.")
146 ;; These functions all parse the headers of the current buffer
148 (defsubst lm-get-header-re (header &optional mode)
149 "Returns regexp for matching HEADER.
150 If called with optional MODE and with value `section',
151 return section regexp instead."
152 (cond ((eq mode 'section)
153 (concat "^;;;;* " header ":[ \t]*$"))
155 (concat lm-header-prefix header ":[ \t]*"))))
157 (defsubst lm-get-package-name ()
158 "Returns package name by looking at the first line."
160 (goto-char (point-min))
161 (if (and (looking-at (concat lm-header-prefix))
162 (progn (goto-char (match-end 0))
163 (looking-at "\\([^\t ]+\\)")
165 (buffer-substring (match-beginning 1) (match-end 1))
168 (defun lm-section-mark (header &optional after)
169 "Return the buffer location of a given section start marker.
170 The HEADER is the section mark string to search for.
171 If AFTER is non-nil, return the location of the next line."
173 (let ((case-fold-search t))
174 (goto-char (point-min))
175 (if (re-search-forward (lm-get-header-re header 'section) nil t)
178 (if after (forward-line 1))
182 (defsubst lm-code-mark ()
183 "Return the buffer location of the `Code' start marker."
184 (lm-section-mark "Code"))
186 (defsubst lm-commentary-mark ()
187 "Return the buffer location of the `Commentary' start marker."
188 (lm-section-mark lm-commentary-header))
190 (defsubst lm-history-mark ()
191 "Return the buffer location of the `History' start marker."
192 (lm-section-mark lm-history-header))
194 (defun lm-header (header)
195 "Return the contents of the header named HEADER."
196 (goto-char (point-min))
197 (let ((case-fold-search t))
198 (if (and (re-search-forward (lm-get-header-re header) (lm-code-mark) t)
199 ;; RCS ident likes format "$identifier: data$"
200 (looking-at "\\([^$\n]+\\)")
202 (buffer-substring (match-beginning 1) (match-end 1))
205 (defun lm-header-multiline (header)
206 "Return the contents of the header named HEADER, with continuation lines.
207 The returned value is a list of strings, one per line."
209 (goto-char (point-min))
210 (let ((res (lm-header header)))
213 (setq res (list res))
216 (while (and (looking-at (concat lm-header-prefix "[\t ]+"))
218 (goto-char (match-end 0))
219 (looking-at #r"\(.*\)"))
221 (setq res (cons (buffer-substring
230 ;; These give us smart access to the header fields and commentary
232 (defun lm-summary (&optional file)
233 "Return the one-line summary of file FILE, or current buffer if FILE is nil."
237 (goto-char (point-min))
240 (looking-at lm-header-prefix)
241 (progn (goto-char (match-end 0))
242 (looking-at "[^ ]+[ \t]+--+[ \t]+\\(.*\\)")))
243 (buffer-substring (match-beginning 1) (match-end 1)))
245 (kill-buffer (current-buffer)))
248 (defun lm-crack-address (x)
249 "Split up an email address into full name and real email address.
250 The value is a cons of the form (FULLNAME . ADDRESS)."
251 (cond ((string-match #r"\(.+\) [(<]\(\S-+@\S-+\)[>)]" x)
252 (cons (substring x (match-beginning 1) (match-end 1))
253 (substring x (match-beginning 2) (match-end 2))))
254 ((string-match #r"\(\S-+@\S-+\) [(<]\(.*\)[>)]" x)
255 (cons (substring x (match-beginning 2) (match-end 2))
256 (substring x (match-beginning 1) (match-end 1))))
257 ((string-match #r"\S-+@\S-+" x)
262 (defun lm-authors (&optional file)
263 "Return the author list of file FILE, or current buffer if FILE is nil.
264 Each element of the list is a cons; the car is the full name,
265 the cdr is an email address."
269 ;; XEmacs change (Is E-MAIL an infodock header? -sb)
270 (let* ((authorlist (lm-header-multiline "author"))
271 (email-list (lm-header-multiline "E-MAIL"))
272 (authors authorlist))
274 (if (null email-list)
275 (mapcar 'lm-crack-address authorlist)
276 (while (and email-list authors)
277 (setcar authors (cons (car authors) (car email-list)))
278 (setq email-list (cdr email-list)
279 authors (cdr authors)))
282 (kill-buffer (current-buffer))))
285 (defun lm-maintainer (&optional file)
286 "Return the maintainer of file FILE, or current buffer if FILE is nil.
287 The return value has the form (NAME . ADDRESS)."
292 (let ((maint (lm-header "maintainer")))
294 (lm-crack-address maint)
297 (kill-buffer (current-buffer))))))
299 (defun lm-creation-date (&optional file)
300 "Return the created date given in file FILE, or current buffer if FILE is nil."
305 ;; XEmacs change (Is ORIG-DATE an Infodock header? -sb)
306 (or (lm-header "created")
307 (let ((date-and-time (lm-header "ORIG-DATE")))
309 (substring date-and-time 0
310 (string-match " " date-and-time)))))
312 (kill-buffer (current-buffer)))
315 (defun lm-last-modified-date (&optional file)
316 "Return the modify-date given in file FILE, or current buffer if FILE is nil."
322 (goto-char (point-min))
324 #r"\$[I]d: [^ ]+ [^ ]+ \([^/]+\)/\([^/]+\)/\([^ ]+\) "
327 (buffer-substring (match-beginning 3) (match-end 3))
329 (buffer-substring (match-beginning 2) (match-end 2)))
330 '("" "Jan" "Feb" "Mar" "Apr" "May" "Jun"
331 "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"))
332 (buffer-substring (match-beginning 1) (match-end 1)))
333 ;; XEmacs change (Infodock change? -sb)
334 (let ((date-and-time (lm-header "LAST-MOD")))
336 (substring date-and-time 0
337 (string-match " " date-and-time)))))
339 (kill-buffer (current-buffer)))
342 (defun lm-version (&optional file)
343 "Return the version listed in file FILE, or current buffer if FILE is nil.
344 This can be found in an RCS or SCCS header to crack it out of."
350 (lm-header "version")
351 (let ((header-max (lm-code-mark)))
352 (goto-char (point-min))
354 ;; Look for an RCS header
355 ((re-search-forward #r"\$[I]d: [^ ]+ \([^ ]+\) " header-max t)
356 (buffer-substring (match-beginning 1) (match-end 1)))
358 ;; Look for an SCCS header
361 (regexp-quote "@(#)")
362 (regexp-quote (file-name-nondirectory (buffer-file-name)))
363 "\t\\([012345679.]*\\)")
365 (buffer-substring (match-beginning 1) (match-end 1)))
369 (kill-buffer (current-buffer)))
372 (defun lm-keywords (&optional file)
373 "Return the keywords given in file FILE, or current buffer if FILE is nil."
378 (let ((keywords (lm-header "keywords")))
379 (and keywords (downcase keywords)))
381 (kill-buffer (current-buffer)))
384 (defun lm-adapted-by (&optional file)
385 "Return the adapted-by names in file FILE, or current buffer if FILE is nil.
386 This is the name of the person who cleaned up this package for
392 (lm-header "adapted-by")
394 (kill-buffer (current-buffer)))
397 (defun lm-commentary (&optional file)
398 "Return the commentary in file FILE, or current buffer if FILE is nil.
399 The value is returned as a string. In the text, the commentary starts
400 with tag `Commentary' and ends with tag `Change Log' or `History'."
405 (let ((commentary (lm-commentary-mark))
406 (change-log (lm-history-mark))
407 (code (lm-code-mark))
410 ((and commentary change-log)
411 (buffer-substring commentary change-log))
412 ((and commentary code)
413 (buffer-substring commentary code))
415 ;; XEmacs change (Infodock headers? -sb)
416 (setq commentary (lm-section-mark "DESCRIPTION" t))
417 (setq end (lm-section-mark "DESCRIP-END"))
418 (and commentary end (buffer-substring commentary end)))))
420 (kill-buffer (current-buffer)))
423 ;;; Verification and synopses
425 (defun lm-insert-at-column (col &rest strings)
426 "Insert list of STRINGS, at column COL."
427 (if (> (current-column) col) (insert "\n"))
428 (move-to-column-force col)
429 (apply 'insert strings))
431 (defun lm-verify (&optional file showok &optional verb)
432 "Check that the current buffer (or FILE if given) is in proper format.
433 If FILE is a directory, recurse on its files and generate a report in
436 (let* ((verb (or verb (interactive-p)))
441 (setq ret "Ok.")) ;init value
443 (if (and file (file-directory-p file))
447 (switch-to-buffer (get-buffer-create "*lm-verify*"))
451 (if (string-match ".*\\.el$" f)
452 (let ((status (lm-verify f)))
456 (lm-insert-at-column lm-comment-column status "\n"))
460 (lm-insert-at-column lm-comment-column "OK\n")))))))
461 (directory-files file))
466 (setq name (lm-get-package-name))
473 "Can't find a package NAME")
476 "Author: tag missing.")
478 ((not (lm-maintainer))
479 "Maintainer: tag missing.")
482 "Can't find a one-line 'Summary' description")
485 "Keywords: tag missing.")
487 ((not (lm-commentary-mark))
488 "Can't find a 'Commentary' section marker.")
490 ((not (lm-history-mark))
491 "Can't find a 'History' section marker.")
493 ((not (lm-code-mark))
494 "Can't find a 'Code' section marker")
497 (goto-char (point-max))
500 (concat "^;;;[ \t]+" name "[ \t]+ends here[ \t]*$"
501 "\\|^;;;[ \t]+ End of file[ \t]+" name)
504 (format "Can't find a footer line for [%s]" name))
508 (kill-buffer (current-buffer)))
515 (defun lm-synopsis (&optional file showall)
516 "Generate a synopsis listing for the buffer or the given FILE if given.
517 If FILE is a directory, recurse on its files and generate a report in
518 a temporary buffer. If SHOWALL is non-nil, also generate a line for files
519 which do not include a recognizable synopsis."
522 (read-file-name "Synopsis for (file or dir): ")))
524 (if (and file (file-directory-p file))
526 (switch-to-buffer (get-buffer-create "*lm-verify*"))
529 (lambda (f) ; XEmacs - dequote
530 (if (string-match ".*\\.el$" f)
531 (let ((syn (lm-synopsis f)))
535 (lm-insert-at-column lm-comment-column syn "\n"))
539 (lm-insert-at-column lm-comment-column "NA\n")))))))
540 (directory-files file))
548 (kill-buffer (current-buffer)))
551 (defun lm-report-bug (topic)
552 "Report a bug in the package currently being visited to its maintainer.
553 Prompts for bug subject. Leaves you in a mail buffer."
554 (interactive "sBug Subject: ")
555 (let ((package (lm-get-package-name))
556 (addr (lm-maintainer))
557 (version (lm-version)))
561 (concat (car addr) " <" (cdr addr) ">")
562 (or (and-boundp 'report-xemacs-bug-beta-address
563 report-xemacs-bug-beta-address)
564 "<xemacs-beta@xemacs.org>"))
566 (goto-char (point-max))
569 (if version (concat " version " version) "")
572 (substitute-command-keys "Type \\[mail-send] to send bug report."))))
576 ;;; lisp-mnt.el ends here