]> code.delx.au - gnu-emacs/blob - lisp/add-log.el
(webjump-to-javaapi): Function deleted.
[gnu-emacs] / lisp / add-log.el
1 ;;; add-log.el --- change log maintenance commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 88, 93, 94, 1997 Free Software Foundation, Inc.
4
5 ;; Keywords: maint
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This facility is documented in the Emacs Manual.
27
28 ;;; Code:
29
30 (defgroup change-log nil
31 "Change log maintenance"
32 :group 'tools
33 :prefix "change-log-"
34 :prefix "add-log-")
35
36
37 (defcustom change-log-default-name nil
38 "*Name of a change log file for \\[add-change-log-entry]."
39 :type '(choice (const :tag "default" nil)
40 string)
41 :group 'change-log)
42
43 (defcustom add-log-current-defun-function nil
44 "\
45 *If non-nil, function to guess name of current function from surrounding text.
46 \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
47 instead) with no arguments. It returns a string or nil if it cannot guess."
48 :type 'boolean
49 :group 'change-log)
50
51 ;;;###autoload
52 (defcustom add-log-full-name nil
53 "*Full name of user, for inclusion in ChangeLog daily headers.
54 This defaults to the value returned by the `user-full-name' function."
55 :type '(choice (const :tag "Default" nil)
56 string)
57 :group 'change-log)
58
59 ;;;###autoload
60 (defcustom add-log-mailing-address nil
61 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
62 This defaults to the value of `user-mail-address'."
63 :type '(choice (const :tag "Default" nil)
64 string)
65 :group 'change-log)
66
67
68 (defvar change-log-font-lock-keywords
69 '(;;
70 ;; Date lines, new and old styles.
71 ("^\\sw........."
72 (0 font-lock-string-face)
73 ("[A-Z][^\n<]+" nil nil (0 font-lock-reference-face)))
74 ;;
75 ;; File names.
76 ("^\t\\* \\([^ ,:([\n]+\\)"
77 (1 font-lock-function-name-face)
78 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 font-lock-function-name-face)))
79 ;;
80 ;; Function or variable names.
81 ("(\\([^ ,:\n]+\\)"
82 (1 font-lock-keyword-face)
83 ("\\=, \\([^ ,:\n]+\\)" nil nil (1 font-lock-keyword-face)))
84 ;;
85 ;; Conditionals.
86 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face))
87 ;;
88 ;; Acknowledgments.
89 ("^\t\\(From\\|Reported by\\)" 1 font-lock-comment-face)
90 )
91 "Additional expressions to highlight in Change Log mode.")
92
93 (defvar change-log-mode-map nil
94 "Keymap for Change Log major mode.")
95 (if change-log-mode-map
96 nil
97 (setq change-log-mode-map (make-sparse-keymap)))
98
99 (defvar change-log-time-zone-rule nil
100 "Time zone used for calculating change log time stamps.
101 It takes the same format as the TZ argument of `set-time-zone-rule'.
102 If nil, use local time.")
103
104 (defun iso8601-time-zone (time)
105 (let* ((utc-offset (or (car (current-time-zone time)) 0))
106 (sign (if (< utc-offset 0) ?- ?+))
107 (sec (abs utc-offset))
108 (ss (% sec 60))
109 (min (/ sec 60))
110 (mm (% min 60))
111 (hh (/ min 60)))
112 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
113 ((not (zerop mm)) "%c%02d:%02d")
114 (t "%c%02d"))
115 sign hh mm ss)))
116
117 (defun change-log-name ()
118 (or change-log-default-name
119 (if (eq system-type 'vax-vms)
120 "$CHANGE_LOG$.TXT"
121 "ChangeLog")))
122
123 ;;;###autoload
124 (defun prompt-for-change-log-name ()
125 "Prompt for a change log name."
126 (let* ((default (change-log-name))
127 (name (expand-file-name
128 (read-file-name (format "Log file (default %s): " default)
129 nil default))))
130 ;; Handle something that is syntactically a directory name.
131 ;; Look for ChangeLog or whatever in that directory.
132 (if (string= (file-name-nondirectory name) "")
133 (expand-file-name (file-name-nondirectory default)
134 name)
135 ;; Handle specifying a file that is a directory.
136 (if (file-directory-p name)
137 (expand-file-name (file-name-nondirectory default)
138 (file-name-as-directory name))
139 name))))
140
141 ;;;###autoload
142 (defun find-change-log (&optional file-name)
143 "Find a change log file for \\[add-change-log-entry] and return the name.
144
145 Optional arg FILE-NAME specifies the file to use.
146 If FILE-NAME is nil, use the value of `change-log-default-name'.
147 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
148 \(or whatever we use on this operating system).
149
150 If 'change-log-default-name' contains a leading directory component, then
151 simply find it in the current directory. Otherwise, search in the current
152 directory and its successive parents for a file so named.
153
154 Once a file is found, `change-log-default-name' is set locally in the
155 current buffer to the complete file name."
156 ;; If user specified a file name or if this buffer knows which one to use,
157 ;; just use that.
158 (or file-name
159 (setq file-name (and change-log-default-name
160 (file-name-directory change-log-default-name)
161 change-log-default-name))
162 (progn
163 ;; Chase links in the source file
164 ;; and use the change log in the dir where it points.
165 (setq file-name (or (and buffer-file-name
166 (file-name-directory
167 (file-chase-links buffer-file-name)))
168 default-directory))
169 (if (file-directory-p file-name)
170 (setq file-name (expand-file-name (change-log-name) file-name)))
171 ;; Chase links before visiting the file.
172 ;; This makes it easier to use a single change log file
173 ;; for several related directories.
174 (setq file-name (file-chase-links file-name))
175 (setq file-name (expand-file-name file-name))
176 ;; Move up in the dir hierarchy till we find a change log file.
177 (let ((file1 file-name)
178 parent-dir)
179 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
180 (progn (setq parent-dir
181 (file-name-directory
182 (directory-file-name
183 (file-name-directory file1))))
184 ;; Give up if we are already at the root dir.
185 (not (string= (file-name-directory file1)
186 parent-dir))))
187 ;; Move up to the parent dir and try again.
188 (setq file1 (expand-file-name
189 (file-name-nondirectory (change-log-name))
190 parent-dir)))
191 ;; If we found a change log in a parent, use that.
192 (if (or (get-file-buffer file1) (file-exists-p file1))
193 (setq file-name file1)))))
194 ;; Make a local variable in this buffer so we needn't search again.
195 (set (make-local-variable 'change-log-default-name) file-name)
196 file-name)
197
198 ;;;###autoload
199 (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
200 "Find change log file and add an entry for today.
201 Optional arg (interactive prefix) non-nil means prompt for user name and site.
202 Second arg is file name of change log. If nil, uses `change-log-default-name'.
203 Third arg OTHER-WINDOW non-nil means visit in other window.
204 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
205 never append to an existing entry. Today's date is calculated according to
206 `change-log-time-zone-rule' if non-nil, otherwise in local time."
207 (interactive (list current-prefix-arg
208 (prompt-for-change-log-name)))
209 (or add-log-full-name
210 (setq add-log-full-name (user-full-name)))
211 (or add-log-mailing-address
212 (setq add-log-mailing-address user-mail-address))
213 (if whoami
214 (progn
215 (setq add-log-full-name (read-input "Full name: " add-log-full-name))
216 ;; Note that some sites have room and phone number fields in
217 ;; full name which look silly when inserted. Rather than do
218 ;; anything about that here, let user give prefix argument so that
219 ;; s/he can edit the full name field in prompter if s/he wants.
220 (setq add-log-mailing-address
221 (read-input "Mailing address: " add-log-mailing-address))))
222 (let ((defun (funcall (or add-log-current-defun-function
223 'add-log-current-defun)))
224 paragraph-end entry)
225
226 (setq file-name (expand-file-name (find-change-log file-name)))
227
228 ;; Set ENTRY to the file name to use in the new entry.
229 (and buffer-file-name
230 ;; Never want to add a change log entry for the ChangeLog file itself.
231 (not (string= buffer-file-name file-name))
232 (setq entry (if (string-match
233 (concat "^" (regexp-quote (file-name-directory
234 file-name)))
235 buffer-file-name)
236 (substring buffer-file-name (match-end 0))
237 (file-name-nondirectory buffer-file-name))))
238
239 (if (and other-window (not (equal file-name buffer-file-name)))
240 (find-file-other-window file-name)
241 (find-file file-name))
242 (or (eq major-mode 'change-log-mode)
243 (change-log-mode))
244 (undo-boundary)
245 (goto-char (point-min))
246 (let ((new-entry (concat (if change-log-time-zone-rule
247 (let ((tz (getenv "TZ"))
248 (now (current-time)))
249 (unwind-protect
250 (progn
251 (set-time-zone-rule
252 change-log-time-zone-rule)
253 (concat
254 (format-time-string "%Y-%m-%d " now)
255 (iso8601-time-zone now)))
256 (set-time-zone-rule tz)))
257 (format-time-string "%Y-%m-%d"))
258 " " add-log-full-name
259 " <" add-log-mailing-address ">")))
260 (if (looking-at (regexp-quote new-entry))
261 (forward-line 1)
262 (insert new-entry "\n\n")))
263
264 ;; Search only within the first paragraph.
265 (if (looking-at "\n*[^\n* \t]")
266 (skip-chars-forward "\n")
267 (forward-paragraph 1))
268 (setq paragraph-end (point))
269 (goto-char (point-min))
270
271 ;; Now insert the new line for this entry.
272 (cond ((re-search-forward "^\\s *\\*\\s *$" paragraph-end t)
273 ;; Put this file name into the existing empty entry.
274 (if entry
275 (insert entry)))
276 ((and (not new-entry)
277 (let (case-fold-search)
278 (re-search-forward
279 (concat (regexp-quote (concat "* " entry))
280 ;; Don't accept `foo.bar' when
281 ;; looking for `foo':
282 "\\(\\s \\|[(),:]\\)")
283 paragraph-end t)))
284 ;; Add to the existing entry for the same file.
285 (re-search-forward "^\\s *$\\|^\\s \\*")
286 (goto-char (match-beginning 0))
287 ;; Delete excess empty lines; make just 2.
288 (while (and (not (eobp)) (looking-at "^\\s *$"))
289 (delete-region (point) (save-excursion (forward-line 1) (point))))
290 (insert "\n\n")
291 (forward-line -2)
292 (indent-relative-maybe))
293 (t
294 ;; Make a new entry.
295 (forward-line 1)
296 (while (looking-at "\\sW")
297 (forward-line 1))
298 (while (and (not (eobp)) (looking-at "^\\s *$"))
299 (delete-region (point) (save-excursion (forward-line 1) (point))))
300 (insert "\n\n\n")
301 (forward-line -2)
302 (indent-to left-margin)
303 (insert "* " (or entry ""))))
304 ;; Now insert the function name, if we have one.
305 ;; Point is at the entry for this file,
306 ;; either at the end of the line or at the first blank line.
307 (if defun
308 (progn
309 ;; Make it easy to get rid of the function name.
310 (undo-boundary)
311 (insert (if (save-excursion
312 (beginning-of-line 1)
313 (looking-at "\\s *$"))
314 ""
315 " ")
316 "(" defun "): "))
317 ;; No function name, so put in a colon unless we have just a star.
318 (if (not (save-excursion
319 (beginning-of-line 1)
320 (looking-at "\\s *\\(\\*\\s *\\)?$")))
321 (insert ": ")))))
322
323 ;;;###autoload
324 (defun add-change-log-entry-other-window (&optional whoami file-name)
325 "Find change log file in other window and add an entry for today.
326 Optional arg (interactive prefix) non-nil means prompt for user name and site.
327 Second arg is file name of change log. \
328 If nil, uses `change-log-default-name'."
329 (interactive (if current-prefix-arg
330 (list current-prefix-arg
331 (prompt-for-change-log-name))))
332 (add-change-log-entry whoami file-name t))
333 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
334
335 ;;;###autoload
336 (defun change-log-mode ()
337 "Major mode for editing change logs; like Indented Text Mode.
338 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
339 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
340 Each entry behaves as a paragraph, and the entries for one day as a page.
341 Runs `change-log-mode-hook'."
342 (interactive)
343 (kill-all-local-variables)
344 (indented-text-mode)
345 (setq major-mode 'change-log-mode
346 mode-name "Change Log"
347 left-margin 8
348 fill-column 74
349 indent-tabs-mode t
350 tab-width 8)
351 (use-local-map change-log-mode-map)
352 (set (make-local-variable 'fill-paragraph-function)
353 'change-log-fill-paragraph)
354 ;; Let each entry behave as one paragraph:
355 ;; We really do want "^" in paragraph-start below: it is only the lines that
356 ;; begin at column 0 (despite the left-margin of 8) that we are looking for.
357 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
358 (set (make-local-variable 'paragraph-separate) "\\s *$\\|\f\\|^\\<")
359 ;; Let all entries for one day behave as one page.
360 ;; Match null string on the date-line so that the date-line
361 ;; is grouped with what follows.
362 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
363 (set (make-local-variable 'version-control) 'never)
364 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
365 (set (make-local-variable 'font-lock-defaults)
366 '(change-log-font-lock-keywords t))
367 (run-hooks 'change-log-mode-hook))
368
369 ;; It might be nice to have a general feature to replace this. The idea I
370 ;; have is a variable giving a regexp matching text which should not be
371 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
372 ;; But I don't feel up to implementing that today.
373 (defun change-log-fill-paragraph (&optional justify)
374 "Fill the paragraph, but preserve open parentheses at beginning of lines.
375 Prefix arg means justify as well."
376 (interactive "P")
377 (let ((end (progn (forward-paragraph) (point)))
378 (beg (progn (backward-paragraph) (point)))
379 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
380 (fill-region beg end justify)
381 t))
382 \f
383 (defcustom add-log-current-defun-header-regexp
384 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
385 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
386 :type 'regexp
387 :group 'change-log)
388
389 ;;;###autoload
390 (defun add-log-current-defun ()
391 "Return name of function definition point is in, or nil.
392
393 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
394 Texinfo (@node titles), Perl, and Fortran.
395
396 Other modes are handled by a heuristic that looks in the 10K before
397 point for uppercase headings starting in the first column or
398 identifiers followed by `:' or `=', see variable
399 `add-log-current-defun-header-regexp'.
400
401 Has a preference of looking backwards."
402 (condition-case nil
403 (save-excursion
404 (let ((location (point)))
405 (cond ((memq major-mode '(emacs-lisp-mode lisp-mode scheme-mode
406 lisp-interaction-mode))
407 ;; If we are now precisely at the beginning of a defun,
408 ;; make sure beginning-of-defun finds that one
409 ;; rather than the previous one.
410 (or (eobp) (forward-char 1))
411 (beginning-of-defun)
412 ;; Make sure we are really inside the defun found, not after it.
413 (if (and (looking-at "\\s(")
414 (progn (end-of-defun)
415 (< location (point)))
416 (progn (forward-sexp -1)
417 (>= location (point))))
418 (progn
419 (if (looking-at "\\s(")
420 (forward-char 1))
421 (forward-sexp 1)
422 (skip-chars-forward " '")
423 (buffer-substring (point)
424 (progn (forward-sexp 1) (point))))))
425 ((and (memq major-mode '(c-mode c++-mode c++-c-mode objc-mode))
426 (save-excursion (beginning-of-line)
427 ;; Use eq instead of = here to avoid
428 ;; error when at bob and char-after
429 ;; returns nil.
430 (while (eq (char-after (- (point) 2)) ?\\)
431 (forward-line -1))
432 (looking-at "[ \t]*#[ \t]*define[ \t]")))
433 ;; Handle a C macro definition.
434 (beginning-of-line)
435 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
436 (forward-line -1))
437 (search-forward "define")
438 (skip-chars-forward " \t")
439 (buffer-substring (point)
440 (progn (forward-sexp 1) (point))))
441 ((memq major-mode '(c-mode c++-mode c++-c-mode objc-mode))
442 (beginning-of-line)
443 ;; See if we are in the beginning part of a function,
444 ;; before the open brace. If so, advance forward.
445 (while (not (looking-at "{\\|\\(\\s *$\\)"))
446 (forward-line 1))
447 (or (eobp)
448 (forward-char 1))
449 (beginning-of-defun)
450 (if (progn (end-of-defun)
451 (< location (point)))
452 (progn
453 (backward-sexp 1)
454 (let (beg tem)
455
456 (forward-line -1)
457 ;; Skip back over typedefs of arglist.
458 (while (and (not (bobp))
459 (looking-at "[ \t\n]"))
460 (forward-line -1))
461 ;; See if this is using the DEFUN macro used in Emacs,
462 ;; or the DEFUN macro used by the C library.
463 (if (condition-case nil
464 (and (save-excursion
465 (end-of-line)
466 (while (= (preceding-char) ?\\)
467 (end-of-line 2))
468 (backward-sexp 1)
469 (beginning-of-line)
470 (setq tem (point))
471 (looking-at "DEFUN\\b"))
472 (>= location tem))
473 (error nil))
474 (progn
475 (goto-char tem)
476 (down-list 1)
477 (if (= (char-after (point)) ?\")
478 (progn
479 (forward-sexp 1)
480 (skip-chars-forward " ,")))
481 (buffer-substring (point)
482 (progn (forward-sexp 1) (point))))
483 (if (looking-at "^[+-]")
484 (get-method-definition)
485 ;; Ordinary C function syntax.
486 (setq beg (point))
487 (if (and (condition-case nil
488 ;; Protect against "Unbalanced parens" error.
489 (progn
490 (down-list 1) ; into arglist
491 (backward-up-list 1)
492 (skip-chars-backward " \t")
493 t)
494 (error nil))
495 ;; Verify initial pos was after
496 ;; real start of function.
497 (save-excursion
498 (goto-char beg)
499 ;; For this purpose, include the line
500 ;; that has the decl keywords. This
501 ;; may also include some of the
502 ;; comments before the function.
503 (while (and (not (bobp))
504 (save-excursion
505 (forward-line -1)
506 (looking-at "[^\n\f]")))
507 (forward-line -1))
508 (>= location (point)))
509 ;; Consistency check: going down and up
510 ;; shouldn't take us back before BEG.
511 (> (point) beg))
512 (let (end middle)
513 ;; Don't include any final newline
514 ;; in the name we use.
515 (if (= (preceding-char) ?\n)
516 (forward-char -1))
517 (setq end (point))
518 (backward-sexp 1)
519 ;; Now find the right beginning of the name.
520 ;; Include certain keywords if they
521 ;; precede the name.
522 (setq middle (point))
523 (forward-word -1)
524 ;; Ignore these subparts of a class decl
525 ;; and move back to the class name itself.
526 (while (looking-at "public \\|private ")
527 (skip-chars-backward " \t:")
528 (setq end (point))
529 (backward-sexp 1)
530 (setq middle (point))
531 (forward-word -1))
532 (and (bolp)
533 (looking-at "struct \\|union \\|class ")
534 (setq middle (point)))
535 (buffer-substring middle end)))))))))
536 ((memq major-mode
537 '(TeX-mode plain-TeX-mode LaTeX-mode;; tex-mode.el
538 plain-tex-mode latex-mode;; cmutex.el
539 ))
540 (if (re-search-backward
541 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
542 (progn
543 (goto-char (match-beginning 0))
544 (buffer-substring (1+ (point));; without initial backslash
545 (progn
546 (end-of-line)
547 (point))))))
548 ((eq major-mode 'texinfo-mode)
549 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
550 (buffer-substring (match-beginning 1)
551 (match-end 1))))
552 ((eq major-mode 'perl-mode)
553 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
554 (buffer-substring (match-beginning 1)
555 (match-end 1))))
556 ((eq major-mode 'fortran-mode)
557 ;; must be inside function body for this to work
558 (beginning-of-fortran-subprogram)
559 (let ((case-fold-search t)) ; case-insensitive
560 ;; search for fortran subprogram start
561 (if (re-search-forward
562 "^[ \t]*\\(program\\|subroutine\\|function\
563 \\|[ \ta-z0-9*]*[ \t]+function\\)"
564 nil t)
565 (progn
566 ;; move to EOL or before first left paren
567 (if (re-search-forward "[(\n]" nil t)
568 (progn (forward-char -1)
569 (skip-chars-backward " \t"))
570 (end-of-line))
571 ;; Use the name preceding that.
572 (buffer-substring (point)
573 (progn (forward-sexp -1)
574 (point)))))))
575 (t
576 ;; If all else fails, try heuristics
577 (let (case-fold-search)
578 (end-of-line)
579 (if (re-search-backward add-log-current-defun-header-regexp
580 (- (point) 10000)
581 t)
582 (buffer-substring (match-beginning 1)
583 (match-end 1))))))))
584 (error nil)))
585
586 (defvar get-method-definition-md)
587
588 ;; Subroutine used within get-method-definition.
589 ;; Add the last match in the buffer to the end of `md',
590 ;; followed by the string END; move to the end of that match.
591 (defun get-method-definition-1 (end)
592 (setq get-method-definition-md
593 (concat get-method-definition-md
594 (buffer-substring (match-beginning 1) (match-end 1))
595 end))
596 (goto-char (match-end 0)))
597
598 ;; For objective C, return the method name if we are in a method.
599 (defun get-method-definition ()
600 (let ((get-method-definition-md "["))
601 (save-excursion
602 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
603 (get-method-definition-1 " ")))
604 (save-excursion
605 (cond
606 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
607 (get-method-definition-1 "")
608 (while (not (looking-at "[{;]"))
609 (looking-at
610 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
611 (get-method-definition-1 ""))
612 (concat get-method-definition-md "]"))))))
613
614
615 (provide 'add-log)
616
617 ;;; add-log.el ends here