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