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