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