(comint-preoutput-filter-functions): 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
c3979f12
DL
46(defcustom change-log-mode-hook nil
47 "Normal hook run by `change-log-mode'."
48 :type 'hook
49 :group 'change-log)
50
fcad5199 51(defcustom add-log-current-defun-function nil
6258d3af
RM
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'
fcad5199 55instead) with no arguments. It returns a string or nil if it cannot guess."
5cb58f82 56 :type 'function
fcad5199 57 :group 'change-log)
6258d3af 58
29db528b 59;;;###autoload
fcad5199 60(defcustom add-log-full-name nil
02ec1592 61 "*Full name of user, for inclusion in ChangeLog daily headers.
fcad5199
RS
62This defaults to the value returned by the `user-full-name' function."
63 :type '(choice (const :tag "Default" nil)
64 string)
65 :group 'change-log)
02ec1592 66
29db528b 67;;;###autoload
fcad5199 68(defcustom add-log-mailing-address nil
02ec1592 69 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
fcad5199
RS
70This defaults to the value of `user-mail-address'."
71 :type '(choice (const :tag "Default" nil)
72 string)
73 :group 'change-log)
74
df63ae66
RS
75(defcustom add-log-time-format 'add-log-iso8601-time-string
76 "*Function that defines the time format.
77For example, `add-log-iso8601-time-string', which gives the
78date in international ISO 8601 format,
79and `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)
02ec1592 86
83afd62c 87(defcustom add-log-keep-changes-together nil
3697b807
DL
88 "*If non-nil, normally keep day's log entries for one file together.
89
90Log 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 \
92for that file made
93today if this variable is non-nil or that file comes first in today's
94entries. Otherwise another entry for that file will be started. An
95original log:
96
97 * foo (...): ...
98 * bar (...): change 1
83afd62c 99
3697b807
DL
100in the latter case, \\[add-change-log-entry-other-window] in a \
101buffer visiting `bar', yields:
83afd62c 102
3697b807
DL
103 * bar (...): -!-
104 * foo (...): ...
105 * bar (...): change 1
83afd62c 106
3697b807 107and in the former:
83afd62c 108
3697b807
DL
109 * foo (...): ...
110 * bar (...): change 1
111 (...): -!-
83afd62c 112
3697b807
DL
113The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
114this variable."
115 :version "20.3"
83afd62c
KH
116 :type 'boolean
117 :group 'change-log)
118
666f4056
RS
119(defcustom add-log-file-name-function nil
120 "*If non-nil, function to call to identify the filename for a ChangeLog entry.
121This function is called with one argument, `buffer-file-name' in that buffer.
122If this is nil, the default is to use the file's name
123relative to the directory of the change log file."
124 :type 'function
125 :group 'change-log)
126
5f562719 127(defvar change-log-font-lock-keywords
5572c1d1
SM
128 '(;;
129 ;; Date lines, new and old styles.
3307b0ca 130 ("^\\sw.........[0-9:+ ]*"
5572c1d1 131 (0 font-lock-string-face)
97a3278b
DL
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
883212ce 134 (1 font-lock-constant-face)
6f6a2641 135 (2 font-lock-variable-name-face)))
5572c1d1
SM
136 ;;
137 ;; File names.
138 ("^\t\\* \\([^ ,:([\n]+\\)"
139 (1 font-lock-function-name-face)
97a3278b
DL
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)))
5572c1d1
SM
145 ;;
146 ;; Function or variable names.
97a3278b 147 ("^\t(\\([^) ,:\n]+\\)"
5572c1d1 148 (1 font-lock-keyword-face)
6f187d8d 149 ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 font-lock-keyword-face)))
5572c1d1
SM
150 ;;
151 ;; Conditionals.
152 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face))
153 ;;
a3cab9f0 154 ;; Acknowledgements.
095fb03e 155 ("^\t\\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
b438e1cb 156 1 font-lock-comment-face)
095fb03e 157 (" \\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
b438e1cb 158 1 font-lock-comment-face))
5f562719
RS
159 "Additional expressions to highlight in Change Log mode.")
160
45c50c5d
KH
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
51bd1843
EN
167(defvar change-log-time-zone-rule nil
168 "Time zone used for calculating change log time stamps.
169It takes the same format as the TZ argument of `set-time-zone-rule'.
170If nil, use local time.")
171
5522b032
RS
172(defvar add-log-debugging)
173
df63ae66 174(defun add-log-iso8601-time-zone (time)
51bd1843
EN
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
df63ae66
RS
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
84fc2cfa
ER
201(defun change-log-name ()
202 (or change-log-default-name
513063cf 203 (if (eq system-type 'vax-vms)
4a047d23
RS
204 "$CHANGE_LOG$.TXT"
205 "ChangeLog")))
84fc2cfa 206
287d149f 207;;;###autoload
84fc2cfa
ER
208(defun prompt-for-change-log-name ()
209 "Prompt for a change log name."
117aaf60
KH
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))))
84fc2cfa 224
45a13f0d
RM
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.
a82e2ed5
RS
228
229Optional arg FILE-NAME specifies the file to use.
de98fcaf
RS
230If FILE-NAME is nil, use the value of `change-log-default-name'.
231If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
232\(or whatever we use on this operating system).
233
234If 'change-log-default-name' contains a leading directory component, then
513063cf 235simply find it in the current directory. Otherwise, search in the current
de98fcaf 236directory and its successive parents for a file so named.
45a13f0d
RM
237
238Once a file is found, `change-log-default-name' is set locally in the
239current buffer to the complete file name."
a82e2ed5
RS
240 ;; If user specified a file name or if this buffer knows which one to use,
241 ;; just use that.
45a13f0d 242 (or file-name
de98fcaf
RS
243 (setq file-name (and change-log-default-name
244 (file-name-directory change-log-default-name)
245 change-log-default-name))
a82e2ed5
RS
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.
513063cf 272 (setq file1 (expand-file-name
a82e2ed5
RS
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)
45a13f0d 281
84fc2cfa 282;;;###autoload
287d149f 283(defun add-change-log-entry (&optional whoami file-name other-window new-entry)
84fc2cfa 284 "Find change log file and add an entry for today.
83afd62c
KH
285Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
286name and site.
287
288Second arg is FILE-NAME of change log. If nil, uses `change-log-default-name'.
287d149f
RM
289Third arg OTHER-WINDOW non-nil means visit in other window.
290Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
3697b807
DL
291never append to an existing entry. Option `add-log-keep-changes-together'
292otherwise affects whether a new entry is created.
293
294Today's date is calculated according to `change-log-time-zone-rule' if
295non-nil, otherwise in local time."
84fc2cfa
ER
296 (interactive (list current-prefix-arg
297 (prompt-for-change-log-name)))
550d8777
RS
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))
02ec1592
BF
302 (if whoami
303 (progn
304 (setq add-log-full-name (read-input "Full name: " add-log-full-name))
84fc2cfa
ER
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.
02ec1592
BF
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)))
3697b807 313 bound entry)
45a13f0d 314
3e1c918b 315 (setq file-name (expand-file-name (find-change-log file-name)))
82f4acaf
RM
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))
666f4056
RS
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)))))
82f4acaf 330
5522b032
RS
331 (let ((buffer (find-buffer-visiting file-name)))
332 (setq add-log-debugging (list (gap-position) (gap-size))))
84fc2cfa
ER
333 (if (and other-window (not (equal file-name buffer-file-name)))
334 (find-file-other-window file-name)
335 (find-file file-name))
675a998f
RS
336 (or (eq major-mode 'change-log-mode)
337 (change-log-mode))
84fc2cfa
ER
338 (undo-boundary)
339 (goto-char (point-min))
df63ae66 340 (let ((new-entry (concat (funcall add-log-time-format)
51bd1843
EN
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")))
82f4acaf 346
3697b807
DL
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)))
84fc2cfa 355 (goto-char (point-min))
1832dbd1 356 ;; Now insert the new line for this entry.
3697b807 357 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
82f4acaf
RM
358 ;; Put this file name into the existing empty entry.
359 (if entry
3697b807 360 (insert entry)))
287d149f 361 ((and (not new-entry)
e172f546
KH
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 \\|[(),:]\\)")
3697b807 368 bound t)))
82f4acaf
RM
369 ;; Add to the existing entry for the same file.
370 (re-search-forward "^\\s *$\\|^\\s \\*")
e172f546 371 (goto-char (match-beginning 0))
3697b807
DL
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))
21d7e080 378 (t
dd309224
RM
379 ;; Make a new entry.
380 (forward-line 1)
381 (while (looking-at "\\sW")
382 (forward-line 1))
09b389d0 383 (while (and (not (eobp)) (looking-at "^\\s *$"))
1832dbd1
RS
384 (delete-region (point) (save-excursion (forward-line 1) (point))))
385 (insert "\n\n\n")
386 (forward-line -2)
dd309224
RM
387 (indent-to left-margin)
388 (insert "* " (or entry ""))))
1832dbd1 389 ;; Now insert the function name, if we have one.
21d7e080
ER
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
dd309224 394 ;; Make it easy to get rid of the function name.
21d7e080 395 (undo-boundary)
dd309224
RM
396 (insert (if (save-excursion
397 (beginning-of-line 1)
513063cf 398 (looking-at "\\s *$"))
dd309224
RM
399 ""
400 " ")
401 "(" defun "): "))
1832dbd1 402 ;; No function name, so put in a colon unless we have just a star.
dd309224
RM
403 (if (not (save-excursion
404 (beginning-of-line 1)
405 (looking-at "\\s *\\(\\*\\s *\\)?$")))
1832dbd1 406 (insert ": ")))))
84fc2cfa 407
84fc2cfa
ER
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.
83afd62c
KH
411Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
412name and site.
3697b807
DL
413Second optional arg FILE-NAME is file name of change log.
414If nil, use `change-log-default-name'.
415
416Affected by the same options as `add-change-log-entry'."
84fc2cfa
ER
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))
82f4acaf 421;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
84fc2cfa 422
1da56800 423;;;###autoload
21d7e080 424(defun change-log-mode ()
eb8c3be9 425 "Major mode for editing change logs; like Indented Text Mode.
09b389d0 426Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
3697b807 427New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
5516387c
RM
428Each entry behaves as a paragraph, and the entries for one day as a page.
429Runs `change-log-mode-hook'."
21d7e080
ER
430 (interactive)
431 (kill-all-local-variables)
432 (indented-text-mode)
82f4acaf
RM
433 (setq major-mode 'change-log-mode
434 mode-name "Change Log"
435 left-margin 8
4f675a8c 436 fill-column 74
60f10a06
KH
437 indent-tabs-mode t
438 tab-width 8)
45c50c5d 439 (use-local-map change-log-mode-map)
60f10a06
KH
440 (set (make-local-variable 'fill-paragraph-function)
441 'change-log-fill-paragraph)
4b7d4d0d
DL
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.
c9cfb0f2 446 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
4b7d4d0d 447 (set (make-local-variable 'paragraph-separate) paragraph-start)
2c91c85c
RS
448 ;; Match null string on the date-line so that the date-line
449 ;; is grouped with what follows.
964141f2 450 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
dd309224
RM
451 (set (make-local-variable 'version-control) 'never)
452 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
4b286eca
SM
453 (set (make-local-variable 'font-lock-defaults)
454 '(change-log-font-lock-keywords t))
21d7e080
ER
455 (run-hooks 'change-log-mode-hook))
456
287d149f
RM
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.
463Prefix arg means justify as well."
464 (interactive "P")
8d6467a4
RS
465 (let ((end (progn (forward-paragraph) (point)))
466 (beg (progn (backward-paragraph) (point)))
14ee1953 467 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
8d6467a4
RS
468 (fill-region beg end justify)
469 t))
287d149f 470\f
fcad5199 471(defcustom add-log-current-defun-header-regexp
26add1bf 472 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
fcad5199
RS
473 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
474 :type 'regexp
475 :group 'change-log)
21d7e080 476
fb644f48
EN
477;;;###autoload
478(defvar add-log-lisp-like-modes
3697b807 479 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
fb644f48
EN
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
e332f80b 492;;;###autoload
21d7e080
ER
493(defun add-log-current-defun ()
494 "Return name of function definition point is in, or nil.
495
63314951 496Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
64bd2d51 497Texinfo (@node titles), Perl, and Fortran.
21d7e080
ER
498
499Other modes are handled by a heuristic that looks in the 10K before
500point for uppercase headings starting in the first column or
501identifiers followed by `:' or `=', see variable
502`add-log-current-defun-header-regexp'.
503
504Has a preference of looking backwards."
2cc0b765
RS
505 (condition-case nil
506 (save-excursion
507 (let ((location (point)))
fb644f48 508 (cond ((memq major-mode add-log-lisp-like-modes)
a0151877 509 ;; If we are now precisely at the beginning of a defun,
2cc0b765
RS
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.
42b1fc29
RS
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.
63c7727f 527 (skip-chars-forward " \t\n'(")
42b1fc29
RS
528 (buffer-substring (point)
529 (progn (forward-sexp 1)
530 (point)))))
fb644f48
EN
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]")))
2cc0b765
RS
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))))
fb644f48 548 ((memq major-mode add-log-c-like-modes)
2cc0b765
RS
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)
fd7fa35a 562
2cc0b765
RS
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
019644ee
RS
572 (end-of-line)
573 (while (= (preceding-char) ?\\)
574 (end-of-line 2))
2cc0b765
RS
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))))
32e986d4 590 (if (looking-at "^[+-]")
83afd62c 591 (change-log-get-method-definition)
32e986d4
RS
592 ;; Ordinary C function syntax.
593 (setq beg (point))
e172f546
KH
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)))
32e986d4
RS
616 ;; Consistency check: going down and up
617 ;; shouldn't take us back before BEG.
618 (> (point) beg))
e172f546 619 (let (end middle)
fe44bc6d 620 ;; Don't include any final whitespace
e172f546 621 ;; in the name we use.
fe44bc6d 622 (skip-chars-backward " \t\n")
e172f546
KH
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)
6dfa1d83
RS
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))
e172f546 638 (and (bolp)
fe44bc6d 639 (looking-at "enum \\|struct \\|union \\|class ")
e172f546 640 (setq middle (point)))
a0bcdd7f
RS
641 (goto-char end)
642 (when (eq (preceding-char) ?=)
643 (forward-char -1)
644 (skip-chars-backward " \t")
645 (setq end (point)))
e172f546 646 (buffer-substring middle end)))))))))
fb644f48 647 ((memq major-mode add-log-tex-like-modes)
2cc0b765
RS
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)
3071ee28 657 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
2cc0b765
RS
658 (buffer-substring (match-beginning 1)
659 (match-end 1))))
64bd2d51
RS
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))))
3697b807
DL
664 ((or (eq major-mode 'fortran-mode)
665 ;; Needs work for f90, but better than nothing.
666 (eq major-mode 'f90-mode))
a9e2a7f2
RS
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
bfce6476 672 "^[ \t]*\\(program\\|subroutine\\|function\
3697b807 673\\|[ \ta-z0-9*()]*[ \t]+function\\|\\(block[ \t]*data\\)\\)"
bfce6476
RS
674 (save-excursion (end-of-fortran-subprogram)
675 (point))
676 t)
3697b807 677 (or (match-string 2)
bfce6476
RS
678 (progn
679 ;; move to EOL or before first left paren
680 (if (re-search-forward "[(\n]" nil t)
3697b807 681 (progn (backward-char)
bfce6476
RS
682 (skip-chars-backward " \t"))
683 (end-of-line))
684 ;; Use the name preceding that.
685 (buffer-substring (point)
3697b807
DL
686 (progn (backward-sexp)
687 (point)))))
688 "main")))
2cc0b765
RS
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)))
ef15f270 699
83afd62c 700(defvar change-log-get-method-definition-md)
15319a8f 701
83afd62c 702;; Subroutine used within change-log-get-method-definition.
59c1a7de
RS
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.
83afd62c
KH
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
15319a8f
RS
708 (buffer-substring (match-beginning 1) (match-end 1))
709 end))
59c1a7de
RS
710 (goto-char (match-end 0)))
711
712;; For objective C, return the method name if we are in a method.
83afd62c
KH
713(defun change-log-get-method-definition ()
714 (let ((change-log-get-method-definition-md "["))
59c1a7de 715 (save-excursion
f27f16ed 716 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
83afd62c 717 (change-log-get-method-definition-1 " ")))
59c1a7de
RS
718 (save-excursion
719 (cond
f27f16ed 720 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
83afd62c 721 (change-log-get-method-definition-1 "")
59c1a7de
RS
722 (while (not (looking-at "[{;]"))
723 (looking-at
f27f16ed 724 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
83afd62c
KH
725 (change-log-get-method-definition-1 ""))
726 (concat change-log-get-method-definition-md "]"))))))
59c1a7de 727
ef15f270 728
1da56800
RS
729(provide 'add-log)
730
fd7fa35a 731;;; add-log.el ends here