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