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