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