(font-lock-keywords): Fix doc for multiline matches.
[emacs.git] / lisp / add-log.el
blob1ded7dbe35d5755d9cc9e027808df7ffd1a8e7ef
1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 86, 88, 93, 94, 97, 1998, 2000 Free Software Foundation, Inc.
5 ;; Keywords: tools
7 ;; This file is part of GNU Emacs.
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.
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.
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.
24 ;;; Commentary:
26 ;; This facility is documented in the Emacs Manual.
28 ;;; Code:
30 (eval-when-compile
31 (require 'fortran)
32 (require 'timezone)
33 (require 'cl))
35 (defgroup change-log nil
36 "Change log maintenance"
37 :group 'tools
38 :link '(custom-manual "(emacs)Change Log")
39 :prefix "change-log-"
40 :prefix "add-log-")
43 (defcustom change-log-default-name nil
44 "*Name of a change log file for \\[add-change-log-entry]."
45 :type '(choice (const :tag "default" nil)
46 string)
47 :group 'change-log)
49 (defcustom change-log-mode-hook nil
50 "Normal hook run by `change-log-mode'."
51 :type 'hook
52 :group 'change-log)
54 (defcustom add-log-current-defun-function nil
56 *If non-nil, function to guess name of current function from surrounding text.
57 \\[add-change-log-entry] calls this function (if nil, `add-log-current-defun'
58 instead) with no arguments. It returns a string or nil if it cannot guess."
59 :type 'function
60 :group 'change-log)
62 ;;;###autoload
63 (defcustom add-log-full-name nil
64 "*Full name of user, for inclusion in ChangeLog daily headers.
65 This defaults to the value returned by the function `user-full-name'."
66 :type '(choice (const :tag "Default" nil)
67 string)
68 :group 'change-log)
70 ;;;###autoload
71 (defcustom add-log-mailing-address nil
72 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
73 This defaults to the value of `user-mail-address'."
74 :type '(choice (const :tag "Default" nil)
75 string)
76 :group 'change-log)
78 (defcustom add-log-time-format 'add-log-iso8601-time-string
79 "*Function that defines the time format.
80 For example, `add-log-iso8601-time-string', which gives the
81 date in international ISO 8601 format,
82 and `current-time-string' are two valid values."
83 :type '(radio (const :tag "International ISO 8601 format"
84 add-log-iso8601-time-string)
85 (const :tag "Old format, as returned by `current-time-string'"
86 current-time-string)
87 (function :tag "Other"))
88 :group 'change-log)
90 (defcustom add-log-keep-changes-together nil
91 "*If non-nil, normally keep day's log entries for one file together.
93 Log entries for a given file made with \\[add-change-log-entry] or
94 \\[add-change-log-entry-other-window] will only be added to others \
95 for that file made
96 today if this variable is non-nil or that file comes first in today's
97 entries. Otherwise another entry for that file will be started. An
98 original log:
100 * foo (...): ...
101 * bar (...): change 1
103 in the latter case, \\[add-change-log-entry-other-window] in a \
104 buffer visiting `bar', yields:
106 * bar (...): -!-
107 * foo (...): ...
108 * bar (...): change 1
110 and in the former:
112 * foo (...): ...
113 * bar (...): change 1
114 (...): -!-
116 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
117 this variable."
118 :version "20.3"
119 :type 'boolean
120 :group 'change-log)
122 (defcustom add-log-file-name-function nil
123 "*If non-nil, function to call to identify the filename for a ChangeLog entry.
124 This function is called with one argument, the value of variable
125 `buffer-file-name' in that buffer. If this is nil, the default is to
126 use the file's name relative to the directory of the change log file."
127 :type 'function
128 :group 'change-log)
131 (defcustom change-log-version-info-enabled nil
132 "*If non-nil, enable recording version numbers with the changes."
133 :version "21.1"
134 :type 'boolean
135 :group 'change-log)
137 (defcustom change-log-version-number-regexp-list
138 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
139 (list
140 ;; (defconst ad-version "2.15"
141 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
142 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
143 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)
144 ;; SCCS @(#)igrep.el 2.83
145 (concat "SCCS[ \t]+@(#).*[ \t]+" re)
147 "*List of regexps to search for version number.
148 Note: The search is conducted only within 10%, at the beginning of the file."
149 :version "21.1"
150 :type '(repeat regexp)
151 :group 'change-log)
154 (defvar change-log-font-lock-keywords
155 '(;;
156 ;; Date lines, new and old styles.
157 ("^\\sw.........[0-9:+ ]*"
158 (0 font-lock-string-face)
159 ;; Name and e-mail; some people put e-mail in parens, not angles.
160 ("\\([^<(]+\\)[(<]\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
161 (1 font-lock-constant-face)
162 (2 font-lock-variable-name-face)))
164 ;; File names.
165 ("^\t\\* \\([^ ,:([\n]+\\)"
166 (1 font-lock-function-name-face)
167 ;; Possibly further names in a list:
168 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 font-lock-function-name-face))
169 ;; Possibly a parenthesized list of names:
170 ("\\= (\\([^) ,:\n]+\\)" nil nil (1 font-lock-keyword-face))
171 ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 font-lock-keyword-face)))
173 ;; Function or variable names.
174 ("^\t(\\([^) ,:\n]+\\)"
175 (1 font-lock-keyword-face)
176 ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 font-lock-keyword-face)))
178 ;; Conditionals.
179 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 font-lock-variable-name-face))
181 ;; Acknowledgements.
182 ("^\t\\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
183 1 font-lock-comment-face)
184 (" \\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
185 1 font-lock-comment-face))
186 "Additional expressions to highlight in Change Log mode.")
188 (defvar change-log-mode-map nil
189 "Keymap for Change Log major mode.")
190 (if change-log-mode-map
192 (setq change-log-mode-map (make-sparse-keymap)))
194 (defvar change-log-time-zone-rule nil
195 "Time zone used for calculating change log time stamps.
196 It takes the same format as the TZ argument of `set-time-zone-rule'.
197 If nil, use local time.")
199 (defun add-log-iso8601-time-zone (time)
200 (let* ((utc-offset (or (car (current-time-zone time)) 0))
201 (sign (if (< utc-offset 0) ?- ?+))
202 (sec (abs utc-offset))
203 (ss (% sec 60))
204 (min (/ sec 60))
205 (mm (% min 60))
206 (hh (/ min 60)))
207 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
208 ((not (zerop mm)) "%c%02d:%02d")
209 (t "%c%02d"))
210 sign hh mm ss)))
212 (defun add-log-iso8601-time-string ()
213 (if change-log-time-zone-rule
214 (let ((tz (getenv "TZ"))
215 (now (current-time)))
216 (unwind-protect
217 (progn
218 (set-time-zone-rule
219 change-log-time-zone-rule)
220 (concat
221 (format-time-string "%Y-%m-%d " now)
222 (add-log-iso8601-time-zone now)))
223 (set-time-zone-rule tz)))
224 (format-time-string "%Y-%m-%d")))
226 (defun change-log-name ()
227 "Return (system-dependent) default name for a change log file."
228 (or change-log-default-name
229 (if (eq system-type 'vax-vms)
230 "$CHANGE_LOG$.TXT"
231 "ChangeLog")))
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))))
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))))))
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.
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)
279 ;; Search RCS, CVS version strings
281 (dolist (choice '("Revision" "Id"))
282 (when (setq version (change-log-version-rcs choice end))
283 (return)))
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)))))
293 version
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.
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).
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.
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)
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.
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.
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)
390 (setq file-name (expand-file-name (find-change-log file-name)))
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 (if add-log-file-name-function
397 (setq entry
398 (funcall add-log-file-name-function buffer-file-name))
399 (setq entry
400 (if (string-match
401 (concat "^" (regexp-quote (file-name-directory
402 file-name)))
403 buffer-file-name)
404 (substring buffer-file-name (match-end 0))
405 (file-name-nondirectory buffer-file-name)))
406 ;; If we have a backup file, it's presumably because we're
407 ;; comparing old and new versions (e.g. for deleted
408 ;; functions) and we'll want to use the original name.
409 (if (backup-file-name-p entry)
410 (setq entry (file-name-sans-versions entry)))))
412 (if (and other-window (not (equal file-name buffer-file-name)))
413 (find-file-other-window file-name)
414 (find-file file-name))
415 (or (eq major-mode 'change-log-mode)
416 (change-log-mode))
417 (undo-boundary)
418 (goto-char (point-min))
419 (let ((new-entry (concat (funcall add-log-time-format)
420 " " add-log-full-name
421 " <" add-log-mailing-address ">")))
422 (if (looking-at (regexp-quote new-entry))
423 (forward-line 1)
424 (insert new-entry "\n\n")))
426 (setq bound
427 (progn
428 (if (looking-at "\n*[^\n* \t]")
429 (skip-chars-forward "\n")
430 (if add-log-keep-changes-together
431 (forward-page) ; page delimits entries for date
432 (forward-paragraph))) ; paragraph delimits entries for file
433 (point)))
434 (goto-char (point-min))
435 ;; Now insert the new line for this entry.
436 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
437 ;; Put this file name into the existing empty entry.
438 (if entry
439 (insert entry)))
440 ((and (not new-entry)
441 (let (case-fold-search)
442 (re-search-forward
443 (concat (regexp-quote (concat "* " entry))
444 ;; Don't accept `foo.bar' when
445 ;; looking for `foo':
446 "\\(\\s \\|[(),:]\\)")
447 bound t)))
448 ;; Add to the existing entry for the same file.
449 (re-search-forward "^\\s *$\\|^\\s \\*")
450 (goto-char (match-beginning 0))
451 ;; Delete excess empty lines; make just 2.
452 (while (and (not (eobp)) (looking-at "^\\s *$"))
453 (delete-region (point) (save-excursion (forward-line 1) (point))))
454 (insert "\n\n")
455 (forward-line -2)
456 (indent-relative-maybe))
458 ;; Make a new entry.
459 (forward-line 1)
460 (while (looking-at "\\sW")
461 (forward-line 1))
462 (while (and (not (eobp)) (looking-at "^\\s *$"))
463 (delete-region (point) (save-excursion (forward-line 1) (point))))
464 (insert "\n\n\n")
465 (forward-line -2)
466 (indent-to left-margin)
467 (insert "* " (or entry ""))
469 ;; Now insert the function name, if we have one.
470 ;; Point is at the entry for this file,
471 ;; either at the end of the line or at the first blank line.
472 (if defun
473 (progn
474 ;; Make it easy to get rid of the function name.
475 (undo-boundary)
476 (insert (if (save-excursion
477 (beginning-of-line 1)
478 (looking-at "\\s *$"))
480 " ")
481 "(" defun "): "
482 (if version
483 (concat version " ")
484 "")))
485 ;; No function name, so put in a colon unless we have just a star.
486 (if (not (save-excursion
487 (beginning-of-line 1)
488 (looking-at "\\s *\\(\\*\\s *\\)?$")))
489 (insert ": "
490 (if version
491 (concat version " ") ""))))))
493 ;;;###autoload
494 (defun add-change-log-entry-other-window (&optional whoami file-name)
495 "Find change log file in other window and add an entry for today.
496 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
497 name and site.
498 Second optional arg FILE-NAME is file name of change log.
499 If nil, use `change-log-default-name'.
501 Affected by the same options as `add-change-log-entry'."
502 (interactive (if current-prefix-arg
503 (list current-prefix-arg
504 (prompt-for-change-log-name))))
505 (add-change-log-entry whoami file-name t))
506 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
508 ;;;###autoload
509 (defun change-log-mode ()
510 "Major mode for editing change logs; like Indented Text Mode.
511 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
512 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
513 Each entry behaves as a paragraph, and the entries for one day as a page.
514 Runs `change-log-mode-hook'."
515 (interactive)
516 (kill-all-local-variables)
517 (indented-text-mode)
518 (setq major-mode 'change-log-mode
519 mode-name "Change Log"
520 left-margin 8
521 fill-column 74
522 indent-tabs-mode t
523 tab-width 8)
524 (use-local-map change-log-mode-map)
525 (set (make-local-variable 'fill-paragraph-function)
526 'change-log-fill-paragraph)
527 ;; We really do want "^" in paragraph-start below: it is only the
528 ;; lines that begin at column 0 (despite the left-margin of 8) that
529 ;; we are looking for. Adding `* ' allows eliding the blank line
530 ;; between entries for different files.
531 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
532 (set (make-local-variable 'paragraph-separate) paragraph-start)
533 ;; Match null string on the date-line so that the date-line
534 ;; is grouped with what follows.
535 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
536 (set (make-local-variable 'version-control) 'never)
537 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
538 (set (make-local-variable 'font-lock-defaults)
539 '(change-log-font-lock-keywords t))
540 (run-hooks 'change-log-mode-hook))
542 ;; It might be nice to have a general feature to replace this. The idea I
543 ;; have is a variable giving a regexp matching text which should not be
544 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
545 ;; But I don't feel up to implementing that today.
546 (defun change-log-fill-paragraph (&optional justify)
547 "Fill the paragraph, but preserve open parentheses at beginning of lines.
548 Prefix arg means justify as well."
549 (interactive "P")
550 (let ((end (progn (forward-paragraph) (point)))
551 (beg (progn (backward-paragraph) (point)))
552 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
553 (fill-region beg end justify)
556 (defcustom add-log-current-defun-header-regexp
557 "^\\([A-Z][A-Z_ ]*[A-Z_]\\|[-_a-zA-Z]+\\)[ \t]*[:=]"
558 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
559 :type 'regexp
560 :group 'change-log)
562 ;;;###autoload
563 (defvar add-log-lisp-like-modes
564 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
565 "*Modes that look like Lisp to `add-log-current-defun'.")
567 ;;;###autoload
568 (defvar add-log-c-like-modes
569 '(c-mode c++-mode c++-c-mode objc-mode)
570 "*Modes that look like C to `add-log-current-defun'.")
572 ;;;###autoload
573 (defvar add-log-tex-like-modes
574 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode)
575 "*Modes that look like TeX to `add-log-current-defun'.")
577 ;;;###autoload
578 (defun add-log-current-defun ()
579 "Return name of function definition point is in, or nil.
581 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
582 Texinfo (@node titles), Perl, and Fortran.
584 Other modes are handled by a heuristic that looks in the 10K before
585 point for uppercase headings starting in the first column or
586 identifiers followed by `:' or `=', see variables
587 `add-log-current-defun-header-regexp' and
588 `add-log-current-defun-function'
590 Has a preference of looking backwards."
591 (condition-case nil
592 (save-excursion
593 (let ((location (point)))
594 (cond ((and (functionp add-log-current-defun-function)
595 (funcall add-log-current-defun-function)))
596 ((memq major-mode add-log-lisp-like-modes)
597 ;; If we are now precisely at the beginning of a defun,
598 ;; make sure beginning-of-defun finds that one
599 ;; rather than the previous one.
600 (or (eobp) (forward-char 1))
601 (beginning-of-defun)
602 ;; Make sure we are really inside the defun found, not after it.
603 (when (and (looking-at "\\s(")
604 (progn (end-of-defun)
605 (< location (point)))
606 (progn (forward-sexp -1)
607 (>= location (point))))
608 (if (looking-at "\\s(")
609 (forward-char 1))
610 ;; Skip the defining construct name, typically "defun"
611 ;; or "defvar".
612 (forward-sexp 1)
613 ;; The second element is usually a symbol being defined.
614 ;; If it is not, use the first symbol in it.
615 (skip-chars-forward " \t\n'(")
616 (buffer-substring (point)
617 (progn (forward-sexp 1)
618 (point)))))
619 ((and (memq major-mode add-log-c-like-modes)
620 (save-excursion
621 (beginning-of-line)
622 ;; Use eq instead of = here to avoid
623 ;; error when at bob and char-after
624 ;; returns nil.
625 (while (eq (char-after (- (point) 2)) ?\\)
626 (forward-line -1))
627 (looking-at "[ \t]*#[ \t]*define[ \t]")))
628 ;; Handle a C macro definition.
629 (beginning-of-line)
630 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
631 (forward-line -1))
632 (search-forward "define")
633 (skip-chars-forward " \t")
634 (buffer-substring (point)
635 (progn (forward-sexp 1) (point))))
636 ((memq major-mode add-log-c-like-modes)
637 (beginning-of-line)
638 ;; See if we are in the beginning part of a function,
639 ;; before the open brace. If so, advance forward.
640 (while (not (looking-at "{\\|\\(\\s *$\\)"))
641 (forward-line 1))
642 (or (eobp)
643 (forward-char 1))
644 (beginning-of-defun)
645 (if (progn (end-of-defun)
646 (< location (point)))
647 (progn
648 (backward-sexp 1)
649 (let (beg tem)
651 (forward-line -1)
652 ;; Skip back over typedefs of arglist.
653 (while (and (not (bobp))
654 (looking-at "[ \t\n]"))
655 (forward-line -1))
656 ;; See if this is using the DEFUN macro used in Emacs,
657 ;; or the DEFUN macro used by the C library.
658 (if (condition-case nil
659 (and (save-excursion
660 (end-of-line)
661 (while (= (preceding-char) ?\\)
662 (end-of-line 2))
663 (backward-sexp 1)
664 (beginning-of-line)
665 (setq tem (point))
666 (looking-at "DEFUN\\b"))
667 (>= location tem))
668 (error nil))
669 (progn
670 (goto-char tem)
671 (down-list 1)
672 (if (= (char-after (point)) ?\")
673 (progn
674 (forward-sexp 1)
675 (skip-chars-forward " ,")))
676 (buffer-substring (point)
677 (progn (forward-sexp 1) (point))))
678 (if (looking-at "^[+-]")
679 (change-log-get-method-definition)
680 ;; Ordinary C function syntax.
681 (setq beg (point))
682 (if (and (condition-case nil
683 ;; Protect against "Unbalanced parens" error.
684 (progn
685 (down-list 1) ; into arglist
686 (backward-up-list 1)
687 (skip-chars-backward " \t")
689 (error nil))
690 ;; Verify initial pos was after
691 ;; real start of function.
692 (save-excursion
693 (goto-char beg)
694 ;; For this purpose, include the line
695 ;; that has the decl keywords. This
696 ;; may also include some of the
697 ;; comments before the function.
698 (while (and (not (bobp))
699 (save-excursion
700 (forward-line -1)
701 (looking-at "[^\n\f]")))
702 (forward-line -1))
703 (>= location (point)))
704 ;; Consistency check: going down and up
705 ;; shouldn't take us back before BEG.
706 (> (point) beg))
707 (let (end middle)
708 ;; Don't include any final whitespace
709 ;; in the name we use.
710 (skip-chars-backward " \t\n")
711 (setq end (point))
712 (backward-sexp 1)
713 ;; Now find the right beginning of the name.
714 ;; Include certain keywords if they
715 ;; precede the name.
716 (setq middle (point))
717 (forward-word -1)
718 ;; Ignore these subparts of a class decl
719 ;; and move back to the class name itself.
720 (while (looking-at "public \\|private ")
721 (skip-chars-backward " \t:")
722 (setq end (point))
723 (backward-sexp 1)
724 (setq middle (point))
725 (forward-word -1))
726 (and (bolp)
727 (looking-at "enum \\|struct \\|union \\|class ")
728 (setq middle (point)))
729 (goto-char end)
730 (when (eq (preceding-char) ?=)
731 (forward-char -1)
732 (skip-chars-backward " \t")
733 (setq end (point)))
734 (buffer-substring middle end)))))))))
735 ((memq major-mode add-log-tex-like-modes)
736 (if (re-search-backward
737 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" nil t)
738 (progn
739 (goto-char (match-beginning 0))
740 (buffer-substring (1+ (point));; without initial backslash
741 (progn
742 (end-of-line)
743 (point))))))
744 ((eq major-mode 'texinfo-mode)
745 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
746 (buffer-substring (match-beginning 1)
747 (match-end 1))))
748 ((eq major-mode 'perl-mode)
749 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
750 (buffer-substring (match-beginning 1)
751 (match-end 1))))
752 ((or (eq major-mode 'fortran-mode)
753 ;; Needs work for f90, but better than nothing.
754 (eq major-mode 'f90-mode))
755 ;; must be inside function body for this to work
756 (fortran-beginning-of-subprogram)
757 (let ((case-fold-search t)) ; case-insensitive
758 ;; search for fortran subprogram start
759 (if (re-search-forward
760 "^[ \t]*\\(program\\|subroutine\\|function\
761 \\|[ \ta-z0-9*()]*[ \t]+function\\|\\(block[ \t]*data\\)\\)"
762 (save-excursion (fortran-end-of-subprogram)
763 (point))
765 (or (match-string 2)
766 (progn
767 ;; move to EOL or before first left paren
768 (if (re-search-forward "[(\n]" nil t)
769 (progn (backward-char)
770 (skip-chars-backward " \t"))
771 (end-of-line))
772 ;; Use the name preceding that.
773 (buffer-substring (point)
774 (progn (backward-sexp)
775 (point)))))
776 "main")))
778 ;; If all else fails, try heuristics
779 (let (case-fold-search
780 result)
781 (end-of-line)
782 (when (re-search-backward
783 add-log-current-defun-header-regexp
784 (- (point) 10000)
786 (setq result (or (buffer-substring (match-beginning 1)
787 (match-end 1))
788 (buffer-substring (match-beginning 0)
789 (match-end 0))))
790 ;; Strip whitespace away
791 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
792 result)
793 (setq result (match-string 1 result)))
794 result))))))
795 (error nil)))
797 (defvar change-log-get-method-definition-md)
799 ;; Subroutine used within change-log-get-method-definition.
800 ;; Add the last match in the buffer to the end of `md',
801 ;; followed by the string END; move to the end of that match.
802 (defun change-log-get-method-definition-1 (end)
803 (setq change-log-get-method-definition-md
804 (concat change-log-get-method-definition-md
805 (buffer-substring (match-beginning 1) (match-end 1))
806 end))
807 (goto-char (match-end 0)))
809 (defun change-log-get-method-definition ()
810 "For objective C, return the method name if we are in a method."
811 (let ((change-log-get-method-definition-md "["))
812 (save-excursion
813 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
814 (change-log-get-method-definition-1 " ")))
815 (save-excursion
816 (cond
817 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
818 (change-log-get-method-definition-1 "")
819 (while (not (looking-at "[{;]"))
820 (looking-at
821 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
822 (change-log-get-method-definition-1 ""))
823 (concat change-log-get-method-definition-md "]"))))))
825 (defun change-log-sortable-date-at ()
826 "Return date of log entry in a consistent form for sorting.
827 Point is assumed to be at the start of the entry."
828 (require 'timezone)
829 (if (looking-at "^\\sw.........[0-9:+ ]*")
830 (let ((date (match-string-no-properties 0)))
831 (if date
832 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
833 (concat (match-string 1 date) (match-string 2 date)
834 (match-string 3 date))
835 (condition-case nil
836 (timezone-make-date-sortable date)
837 (error nil)))))
838 (error "Bad date")))
840 ;;;###autoload
841 (defun change-log-merge (other-log)
842 "Merge the contents of ChangeLog file OTHER-LOG with this buffer.
843 Both must be found in Change Log mode (since the merging depends on
844 the appropriate motion commands).
846 Entries are inserted in chronological order.
848 Both the current and old-style time formats for entries are supported,
849 so this command could be used to convert old-style logs by merging
850 with an empty log."
851 (interactive "*fLog file name to merge: ")
852 (if (not (eq major-mode 'change-log-mode))
853 (error "Not in Change Log mode"))
854 (let ((other-buf (find-file-noselect other-log))
855 (buf (current-buffer))
856 date1 start end)
857 (save-excursion
858 (goto-char (point-min))
859 (set-buffer other-buf)
860 (goto-char (point-min))
861 (if (not (eq major-mode 'change-log-mode))
862 (error "%s not found in Change Log mode" other-log))
863 ;; Loop through all the entries in OTHER-LOG.
864 (while (not (eobp))
865 (setq date1 (change-log-sortable-date-at))
866 (setq start (point)
867 end (progn (forward-page) (point)))
868 ;; Look for an entry in original buffer that isn't later.
869 (with-current-buffer buf
870 (while (and (not (eobp))
871 (string< date1 (change-log-sortable-date-at)))
872 (forward-page))
873 (if (not (eobp))
874 (insert-buffer-substring other-buf start end)
875 ;; At the end of the original buffer, insert a newline to
876 ;; separate entries and then the rest of the file being
877 ;; merged. Move to the end of it to terminate outer loop.
878 (insert "\n")
879 (insert-buffer-substring other-buf start
880 (with-current-buffer other-buf
881 (goto-char (point-max))
882 (point)))))))))
884 (provide 'add-log)
886 ;;; add-log.el ends here