(find-composition): Check if (char-after POS)
[emacs.git] / lisp / add-log.el
blob61a29beda84f014ef2aff39228a9a7fb727862ce
1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 86, 88, 93, 94, 97, 98, 2000 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
6 ;; Keywords: tools
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
25 ;;; Commentary:
27 ;; This facility is documented in the Emacs Manual.
29 ;;; Code:
31 (eval-when-compile
32 (require 'timezone))
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-")
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)
48 (defcustom change-log-mode-hook nil
49 "Normal hook run by `change-log-mode'."
50 :type 'hook
51 :group 'change-log)
53 (defcustom add-log-current-defun-function nil
54 "*If non-nil, function to guess name of surrounding function.
55 It is used by `add-log-current-defun' in preference to built-in rules.
56 Returns function's name as a string, or nil if outside a function."
57 :type '(choice (const nil) function)
58 :group 'change-log)
60 ;;;###autoload
61 (defcustom add-log-full-name nil
62 "*Full name of user, for inclusion in ChangeLog daily headers.
63 This defaults to the value returned by the function `user-full-name'."
64 :type '(choice (const :tag "Default" nil)
65 string)
66 :group 'change-log)
68 ;;;###autoload
69 (defcustom add-log-mailing-address nil
70 "*Electronic mail address of user, for inclusion in ChangeLog daily headers.
71 This defaults to the value of `user-mail-address'."
72 :type '(choice (const :tag "Default" nil)
73 string)
74 :group 'change-log)
76 (defcustom add-log-time-format 'add-log-iso8601-time-string
77 "*Function that defines the time format.
78 For example, `add-log-iso8601-time-string', which gives the
79 date in international ISO 8601 format,
80 and `current-time-string' are two valid values."
81 :type '(radio (const :tag "International ISO 8601 format"
82 add-log-iso8601-time-string)
83 (const :tag "Old format, as returned by `current-time-string'"
84 current-time-string)
85 (function :tag "Other"))
86 :group 'change-log)
88 (defcustom add-log-keep-changes-together nil
89 "*If non-nil, normally keep day's log entries for one file together.
91 Log entries for a given file made with \\[add-change-log-entry] or
92 \\[add-change-log-entry-other-window] will only be added to others \
93 for that file made
94 today if this variable is non-nil or that file comes first in today's
95 entries. Otherwise another entry for that file will be started. An
96 original log:
98 * foo (...): ...
99 * bar (...): change 1
101 in the latter case, \\[add-change-log-entry-other-window] in a \
102 buffer visiting `bar', yields:
104 * bar (...): -!-
105 * foo (...): ...
106 * bar (...): change 1
108 and in the former:
110 * foo (...): ...
111 * bar (...): change 1
112 (...): -!-
114 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
115 this variable."
116 :version "20.3"
117 :type 'boolean
118 :group 'change-log)
120 (defcustom add-log-buffer-file-name-function nil
121 "*If non-nil, function to call to identify the full filename of a buffer.
122 This function is called with no argument. If this is nil, the default is to
123 use `buffer-file-name'."
124 :type '(choice (const nil) function)
125 :group 'change-log)
127 (defcustom add-log-file-name-function nil
128 "*If non-nil, function to call to identify the filename for a ChangeLog entry.
129 This function is called with one argument, the value of variable
130 `buffer-file-name' in that buffer. If this is nil, the default is to
131 use the file's name relative to the directory of the change log file."
132 :type '(choice (const nil) function)
133 :group 'change-log)
136 (defcustom change-log-version-info-enabled nil
137 "*If non-nil, enable recording version numbers with the changes."
138 :version "21.1"
139 :type 'boolean
140 :group 'change-log)
142 (defcustom change-log-version-number-regexp-list
143 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
144 (list
145 ;; (defconst ad-version "2.15"
146 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
147 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
148 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
149 "*List of regexps to search for version number.
150 The version number must be in group 1.
151 Note: The search is conducted only within 10%, at the beginning of the file."
152 :version "21.1"
153 :type '(repeat regexp)
154 :group 'change-log)
156 (defface change-log-date-face
157 '((t (:inherit font-lock-string-face)))
158 "Face used to highlight dates in date lines."
159 :version "21.1"
160 :group 'change-log)
162 (defface change-log-name-face
163 '((t (:inherit font-lock-constant-face)))
164 "Face for highlighting author names."
165 :version "21.1"
166 :group 'change-log)
168 (defface change-log-email-face
169 '((t (:inherit font-lock-variable-name-face)))
170 "Face for highlighting author email addresses."
171 :version "21.1"
172 :group 'change-log)
174 (defface change-log-file-face
175 '((t (:inherit font-lock-function-name-face)))
176 "Face for highlighting file names."
177 :version "21.1"
178 :group 'change-log)
180 (defface change-log-list-face
181 '((t (:inherit font-lock-keyword-face)))
182 "Face for highlighting parenthesized lists of functions or variables."
183 :version "21.1"
184 :group 'change-log)
186 (defface change-log-conditionals-face
187 '((t (:inherit font-lock-variable-name-face)))
188 "Face for highlighting conditionals of the form `[...]'."
189 :version "21.1"
190 :group 'change-log)
192 (defface change-log-function-face
193 '((t (:inherit font-lock-variable-name-face)))
194 "Face for highlighting items of the form `<....>'."
195 :version "21.1"
196 :group 'change-log)
198 (defface change-log-acknowledgement-face
199 '((t (:inherit font-lock-comment-face)))
200 "Face for highlighting acknowledgments."
201 :version "21.1"
202 :group 'change-log)
204 (defvar change-log-font-lock-keywords
205 '(;;
206 ;; Date lines, new and old styles.
207 ("^\\sw.........[0-9:+ ]*"
208 (0 'change-log-date-face)
209 ;; Name and e-mail; some people put e-mail in parens, not angles.
210 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
211 (1 'change-log-name-face)
212 (2 'change-log-email-face)))
214 ;; File names.
215 ("^\t\\* \\([^ ,:([\n]+\\)"
216 (1 'change-log-file-face)
217 ;; Possibly further names in a list:
218 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file-face))
219 ;; Possibly a parenthesized list of names:
220 ("\\= (\\([^) ,:\n]+\\)" nil nil (1 'change-log-list-face))
221 ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 'change-log-list-face)))
223 ;; Function or variable names.
224 ("^\t(\\([^) ,:\n]+\\)"
225 (1 'change-log-list-face)
226 ("\\=, *\\([^) ,:\n]+\\)" nil nil (1 'change-log-list-face)))
228 ;; Conditionals.
229 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals-face))
231 ;; Function of change.
232 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function-face))
234 ;; Acknowledgements.
235 ("\\(^\t\\| \\)\\(From\\|Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
236 2 'change-log-acknowledgement-face))
237 "Additional expressions to highlight in Change Log mode.")
239 (defvar change-log-mode-map (make-sparse-keymap)
240 "Keymap for Change Log major mode.")
242 (defvar change-log-time-zone-rule nil
243 "Time zone used for calculating change log time stamps.
244 It takes the same format as the TZ argument of `set-time-zone-rule'.
245 If nil, use local time.")
247 (defun add-log-iso8601-time-zone (time)
248 (let* ((utc-offset (or (car (current-time-zone time)) 0))
249 (sign (if (< utc-offset 0) ?- ?+))
250 (sec (abs utc-offset))
251 (ss (% sec 60))
252 (min (/ sec 60))
253 (mm (% min 60))
254 (hh (/ min 60)))
255 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
256 ((not (zerop mm)) "%c%02d:%02d")
257 (t "%c%02d"))
258 sign hh mm ss)))
260 (defun add-log-iso8601-time-string ()
261 (if change-log-time-zone-rule
262 (let ((tz (getenv "TZ"))
263 (now (current-time)))
264 (unwind-protect
265 (progn
266 (set-time-zone-rule change-log-time-zone-rule)
267 (concat
268 (format-time-string "%Y-%m-%d " now)
269 (add-log-iso8601-time-zone now)))
270 (set-time-zone-rule tz)))
271 (format-time-string "%Y-%m-%d")))
273 (defun change-log-name ()
274 "Return (system-dependent) default name for a change log file."
275 (or change-log-default-name
276 (if (eq system-type 'vax-vms)
277 "$CHANGE_LOG$.TXT"
278 "ChangeLog")))
280 ;;;###autoload
281 (defun prompt-for-change-log-name ()
282 "Prompt for a change log name."
283 (let* ((default (change-log-name))
284 (name (expand-file-name
285 (read-file-name (format "Log file (default %s): " default)
286 nil default))))
287 ;; Handle something that is syntactically a directory name.
288 ;; Look for ChangeLog or whatever in that directory.
289 (if (string= (file-name-nondirectory name) "")
290 (expand-file-name (file-name-nondirectory default)
291 name)
292 ;; Handle specifying a file that is a directory.
293 (if (file-directory-p name)
294 (expand-file-name (file-name-nondirectory default)
295 (file-name-as-directory name))
296 name))))
298 (defun change-log-version-number-search ()
299 "Return version number of current buffer's file.
300 This is the value returned by `vc-workfile-version' or, if that is
301 nil, by matching `change-log-version-number-regexp-list'."
302 (let* ((size (buffer-size))
303 (end
304 ;; The version number can be anywhere in the file, but
305 ;; restrict search to the file beginning: 10% should be
306 ;; enough to prevent some mishits.
308 ;; Apply percentage only if buffer size is bigger than
309 ;; approx 100 lines.
310 (if (> size (* 100 80))
311 (/ size 10)
312 size))
313 version)
314 (or (and buffer-file-name (vc-workfile-version buffer-file-name))
315 (save-restriction
316 (widen)
317 (let ((regexps change-log-version-number-regexp-list))
318 (while regexps
319 (save-excursion
320 (goto-char (point-min))
321 (when (re-search-forward (pop regexps) end t)
322 (setq version (match-string 1)
323 regexps nil)))))))))
326 ;;;###autoload
327 (defun find-change-log (&optional file-name buffer-file)
328 "Find a change log file for \\[add-change-log-entry] and return the name.
330 Optional arg FILE-NAME specifies the file to use.
331 If FILE-NAME is nil, use the value of `change-log-default-name'.
332 If 'change-log-default-name' is nil, behave as though it were 'ChangeLog'
333 \(or whatever we use on this operating system).
335 If 'change-log-default-name' contains a leading directory component, then
336 simply find it in the current directory. Otherwise, search in the current
337 directory and its successive parents for a file so named.
339 Once a file is found, `change-log-default-name' is set locally in the
340 current buffer to the complete file name.
341 Optional arg BUFFER-FILE overrides `buffer-file-name'."
342 ;; If user specified a file name or if this buffer knows which one to use,
343 ;; just use that.
344 (or file-name
345 (setq file-name (and change-log-default-name
346 (file-name-directory change-log-default-name)
347 change-log-default-name))
348 (progn
349 ;; Chase links in the source file
350 ;; and use the change log in the dir where it points.
351 (setq file-name (or (and (or buffer-file buffer-file-name)
352 (file-name-directory
353 (file-chase-links
354 (or buffer-file buffer-file-name))))
355 default-directory))
356 (if (file-directory-p file-name)
357 (setq file-name (expand-file-name (change-log-name) file-name)))
358 ;; Chase links before visiting the file.
359 ;; This makes it easier to use a single change log file
360 ;; for several related directories.
361 (setq file-name (file-chase-links file-name))
362 (setq file-name (expand-file-name file-name))
363 ;; Move up in the dir hierarchy till we find a change log file.
364 (let ((file1 file-name)
365 parent-dir)
366 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
367 (progn (setq parent-dir
368 (file-name-directory
369 (directory-file-name
370 (file-name-directory file1))))
371 ;; Give up if we are already at the root dir.
372 (not (string= (file-name-directory file1)
373 parent-dir))))
374 ;; Move up to the parent dir and try again.
375 (setq file1 (expand-file-name
376 (file-name-nondirectory (change-log-name))
377 parent-dir)))
378 ;; If we found a change log in a parent, use that.
379 (if (or (get-file-buffer file1) (file-exists-p file1))
380 (setq file-name file1)))))
381 ;; Make a local variable in this buffer so we needn't search again.
382 (set (make-local-variable 'change-log-default-name) file-name)
383 file-name)
385 (defun add-log-file-name (buffer-file log-file)
386 ;; Never want to add a change log entry for the ChangeLog file itself.
387 (unless (or (null buffer-file) (string= buffer-file log-file))
388 (if add-log-file-name-function
389 (funcall add-log-file-name-function buffer-file)
390 (setq buffer-file
391 (if (string-match
392 (concat "^" (regexp-quote (file-name-directory log-file)))
393 buffer-file)
394 (substring buffer-file (match-end 0))
395 (file-name-nondirectory buffer-file)))
396 ;; If we have a backup file, it's presumably because we're
397 ;; comparing old and new versions (e.g. for deleted
398 ;; functions) and we'll want to use the original name.
399 (if (backup-file-name-p buffer-file)
400 (file-name-sans-versions buffer-file)
401 buffer-file))))
403 ;;;###autoload
404 (defun add-change-log-entry (&optional whoami file-name other-window new-entry)
405 "Find change log file and add an entry for today.
406 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
407 name and site.
409 Second arg is FILE-NAME of change log. If nil, uses `change-log-default-name'.
410 Third arg OTHER-WINDOW non-nil means visit in other window.
411 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
412 never append to an existing entry. Option `add-log-keep-changes-together'
413 otherwise affects whether a new entry is created.
415 Today's date is calculated according to `change-log-time-zone-rule' if
416 non-nil, otherwise in local time."
417 (interactive (list current-prefix-arg
418 (prompt-for-change-log-name)))
419 (or add-log-full-name
420 (setq add-log-full-name (user-full-name)))
421 (or add-log-mailing-address
422 (setq add-log-mailing-address user-mail-address))
423 (if whoami
424 (progn
425 (setq add-log-full-name (read-input "Full name: " add-log-full-name))
426 ;; Note that some sites have room and phone number fields in
427 ;; full name which look silly when inserted. Rather than do
428 ;; anything about that here, let user give prefix argument so that
429 ;; s/he can edit the full name field in prompter if s/he wants.
430 (setq add-log-mailing-address
431 (read-input "Mailing address: " add-log-mailing-address))))
433 (let* ((defun (add-log-current-defun))
434 (version (and change-log-version-info-enabled
435 (change-log-version-number-search)))
436 (buf-file-name (if add-log-buffer-file-name-function
437 (funcall add-log-buffer-file-name-function)
438 buffer-file-name))
439 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
440 (file-name (expand-file-name
441 (or file-name (find-change-log file-name buffer-file))))
442 ;; Set ENTRY to the file name to use in the new entry.
443 (entry (add-log-file-name buffer-file file-name))
444 bound)
446 (if (or (and other-window (not (equal file-name buffer-file-name)))
447 (window-dedicated-p (selected-window)))
448 (find-file-other-window file-name)
449 (find-file file-name))
450 (or (eq major-mode 'change-log-mode)
451 (change-log-mode))
452 (undo-boundary)
453 (goto-char (point-min))
454 (let ((new-entry (concat (funcall add-log-time-format)
455 " " add-log-full-name
456 " <" add-log-mailing-address ">")))
457 (if (looking-at (regexp-quote new-entry))
458 (forward-line 1)
459 (insert new-entry "\n\n")))
461 (setq bound
462 (progn
463 (if (looking-at "\n*[^\n* \t]")
464 (skip-chars-forward "\n")
465 (if add-log-keep-changes-together
466 (forward-page) ; page delimits entries for date
467 (forward-paragraph))) ; paragraph delimits entries for file
468 (point)))
469 (goto-char (point-min))
470 ;; Now insert the new line for this entry.
471 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
472 ;; Put this file name into the existing empty entry.
473 (if entry
474 (insert entry)))
475 ((and (not new-entry)
476 (let (case-fold-search)
477 (re-search-forward
478 (concat (regexp-quote (concat "* " entry))
479 ;; Don't accept `foo.bar' when
480 ;; looking for `foo':
481 "\\(\\s \\|[(),:]\\)")
482 bound t)))
483 ;; Add to the existing entry for the same file.
484 (re-search-forward "^\\s *$\\|^\\s \\*")
485 (goto-char (match-beginning 0))
486 ;; Delete excess empty lines; make just 2.
487 (while (and (not (eobp)) (looking-at "^\\s *$"))
488 (delete-region (point) (line-beginning-position 2)))
489 (insert-char ?\n 2)
490 (forward-line -2)
491 (indent-relative-maybe))
493 ;; Make a new entry.
494 (forward-line 1)
495 (while (looking-at "\\sW")
496 (forward-line 1))
497 (while (and (not (eobp)) (looking-at "^\\s *$"))
498 (delete-region (point) (line-beginning-position 2)))
499 (insert-char ?\n 3)
500 (forward-line -2)
501 (indent-to left-margin)
502 (insert "* ")
503 (if entry (insert entry))))
504 ;; Now insert the function name, if we have one.
505 ;; Point is at the entry for this file,
506 ;; either at the end of the line or at the first blank line.
507 (if defun
508 (progn
509 ;; Make it easy to get rid of the function name.
510 (undo-boundary)
511 (unless (save-excursion
512 (beginning-of-line 1)
513 (looking-at "\\s *$"))
514 (insert ?\ ))
515 ;; See if the prev function name has a message yet or not
516 ;; If not, merge the two entries.
517 (let ((pos (point-marker)))
518 (if (and (skip-syntax-backward " ")
519 (skip-chars-backward "):")
520 (looking-at "):")
521 (progn (delete-region (+ 1 (point)) (+ 2 (point))) t)
522 (> fill-column (+ (current-column) (length defun) 3)))
523 (progn (delete-region (point) pos)
524 (insert ", "))
525 (goto-char pos)
526 (insert "("))
527 (set-marker pos nil))
528 (insert defun "): ")
529 (if version
530 (insert version ?\ )))
531 ;; No function name, so put in a colon unless we have just a star.
532 (unless (save-excursion
533 (beginning-of-line 1)
534 (looking-at "\\s *\\(\\*\\s *\\)?$"))
535 (insert ": ")
536 (if version (insert version ?\ ))))))
538 ;;;###autoload
539 (defun add-change-log-entry-other-window (&optional whoami file-name)
540 "Find change log file in other window and add an entry for today.
541 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
542 name and site.
543 Second optional arg FILE-NAME is file name of change log.
544 If nil, use `change-log-default-name'.
546 Affected by the same options as `add-change-log-entry'."
547 (interactive (if current-prefix-arg
548 (list current-prefix-arg
549 (prompt-for-change-log-name))))
550 (add-change-log-entry whoami file-name t))
551 ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window)
553 ;;;###autoload
554 (defun change-log-mode ()
555 "Major mode for editing change logs; like Indented Text Mode.
556 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
557 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
558 Each entry behaves as a paragraph, and the entries for one day as a page.
559 Runs `change-log-mode-hook'."
560 (interactive)
561 (kill-all-local-variables)
562 (indented-text-mode)
563 (setq major-mode 'change-log-mode
564 mode-name "Change Log"
565 left-margin 8
566 fill-column 74
567 indent-tabs-mode t
568 tab-width 8)
569 (use-local-map change-log-mode-map)
570 (set (make-local-variable 'fill-paragraph-function)
571 'change-log-fill-paragraph)
572 ;; We really do want "^" in paragraph-start below: it is only the
573 ;; lines that begin at column 0 (despite the left-margin of 8) that
574 ;; we are looking for. Adding `* ' allows eliding the blank line
575 ;; between entries for different files.
576 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
577 (set (make-local-variable 'paragraph-separate) paragraph-start)
578 ;; Match null string on the date-line so that the date-line
579 ;; is grouped with what follows.
580 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
581 (set (make-local-variable 'version-control) 'never)
582 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
583 (set (make-local-variable 'font-lock-defaults)
584 '(change-log-font-lock-keywords t nil nil backward-paragraph))
585 (run-hooks 'change-log-mode-hook))
587 ;; It might be nice to have a general feature to replace this. The idea I
588 ;; have is a variable giving a regexp matching text which should not be
589 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
590 ;; But I don't feel up to implementing that today.
591 (defun change-log-fill-paragraph (&optional justify)
592 "Fill the paragraph, but preserve open parentheses at beginning of lines.
593 Prefix arg means justify as well."
594 (interactive "P")
595 (let ((end (progn (forward-paragraph) (point)))
596 (beg (progn (backward-paragraph) (point)))
597 (paragraph-start (concat paragraph-start "\\|\\s *\\s(")))
598 (fill-region beg end justify)
601 (defcustom add-log-current-defun-header-regexp
602 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
603 "*Heuristic regexp used by `add-log-current-defun' for unknown major modes."
604 :type 'regexp
605 :group 'change-log)
607 ;;;###autoload
608 (defvar add-log-lisp-like-modes
609 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
610 "*Modes that look like Lisp to `add-log-current-defun'.")
612 ;;;###autoload
613 (defvar add-log-c-like-modes
614 '(c-mode c++-mode c++-c-mode objc-mode)
615 "*Modes that look like C to `add-log-current-defun'.")
617 ;;;###autoload
618 (defvar add-log-tex-like-modes
619 '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode)
620 "*Modes that look like TeX to `add-log-current-defun'.")
622 ;;;###autoload
623 (defun add-log-current-defun ()
624 "Return name of function definition point is in, or nil.
626 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
627 Texinfo (@node titles) and Perl.
629 Other modes are handled by a heuristic that looks in the 10K before
630 point for uppercase headings starting in the first column or
631 identifiers followed by `:' or `='. See variables
632 `add-log-current-defun-header-regexp' and
633 `add-log-current-defun-function'
635 Has a preference of looking backwards."
636 (condition-case nil
637 (save-excursion
638 (let ((location (point)))
639 (cond (add-log-current-defun-function
640 (funcall add-log-current-defun-function))
641 ((memq major-mode add-log-lisp-like-modes)
642 ;; If we are now precisely at the beginning of a defun,
643 ;; make sure beginning-of-defun finds that one
644 ;; rather than the previous one.
645 (or (eobp) (forward-char 1))
646 (beginning-of-defun)
647 ;; Make sure we are really inside the defun found,
648 ;; not after it.
649 (when (and (looking-at "\\s(")
650 (progn (end-of-defun)
651 (< location (point)))
652 (progn (forward-sexp -1)
653 (>= location (point))))
654 (if (looking-at "\\s(")
655 (forward-char 1))
656 ;; Skip the defining construct name, typically "defun"
657 ;; or "defvar".
658 (forward-sexp 1)
659 ;; The second element is usually a symbol being defined.
660 ;; If it is not, use the first symbol in it.
661 (skip-chars-forward " \t\n'(")
662 (buffer-substring-no-properties (point)
663 (progn (forward-sexp 1)
664 (point)))))
665 ((and (memq major-mode add-log-c-like-modes)
666 (save-excursion
667 (beginning-of-line)
668 ;; Use eq instead of = here to avoid
669 ;; error when at bob and char-after
670 ;; returns nil.
671 (while (eq (char-after (- (point) 2)) ?\\)
672 (forward-line -1))
673 (looking-at "[ \t]*#[ \t]*define[ \t]")))
674 ;; Handle a C macro definition.
675 (beginning-of-line)
676 (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above
677 (forward-line -1))
678 (search-forward "define")
679 (skip-chars-forward " \t")
680 (buffer-substring-no-properties (point)
681 (progn (forward-sexp 1)
682 (point))))
683 ((memq major-mode add-log-c-like-modes)
684 (beginning-of-line)
685 ;; See if we are in the beginning part of a function,
686 ;; before the open brace. If so, advance forward.
687 (while (not (looking-at "{\\|\\(\\s *$\\)"))
688 (forward-line 1))
689 (or (eobp)
690 (forward-char 1))
691 (beginning-of-defun)
692 (when (progn (end-of-defun)
693 (< location (point)))
694 (backward-sexp 1)
695 (let (beg tem)
697 (forward-line -1)
698 ;; Skip back over typedefs of arglist.
699 (while (and (not (bobp))
700 (looking-at "[ \t\n]"))
701 (forward-line -1))
702 ;; See if this is using the DEFUN macro used in Emacs,
703 ;; or the DEFUN macro used by the C library.
704 (if (condition-case nil
705 (and (save-excursion
706 (end-of-line)
707 (while (= (preceding-char) ?\\)
708 (end-of-line 2))
709 (backward-sexp 1)
710 (beginning-of-line)
711 (setq tem (point))
712 (looking-at "DEFUN\\b"))
713 (>= location tem))
714 (error nil))
715 (progn
716 (goto-char tem)
717 (down-list 1)
718 (if (= (char-after (point)) ?\")
719 (progn
720 (forward-sexp 1)
721 (skip-chars-forward " ,")))
722 (buffer-substring-no-properties
723 (point)
724 (progn (forward-sexp 1)
725 (point))))
726 (if (looking-at "^[+-]")
727 (change-log-get-method-definition)
728 ;; Ordinary C function syntax.
729 (setq beg (point))
730 (if (and
731 ;; Protect against "Unbalanced parens" error.
732 (condition-case nil
733 (progn
734 (down-list 1) ; into arglist
735 (backward-up-list 1)
736 (skip-chars-backward " \t")
738 (error nil))
739 ;; Verify initial pos was after
740 ;; real start of function.
741 (save-excursion
742 (goto-char beg)
743 ;; For this purpose, include the line
744 ;; that has the decl keywords. This
745 ;; may also include some of the
746 ;; comments before the function.
747 (while (and (not (bobp))
748 (save-excursion
749 (forward-line -1)
750 (looking-at "[^\n\f]")))
751 (forward-line -1))
752 (>= location (point)))
753 ;; Consistency check: going down and up
754 ;; shouldn't take us back before BEG.
755 (> (point) beg))
756 (let (end middle)
757 ;; Don't include any final whitespace
758 ;; in the name we use.
759 (skip-chars-backward " \t\n")
760 (setq end (point))
761 (backward-sexp 1)
762 ;; Now find the right beginning of the name.
763 ;; Include certain keywords if they
764 ;; precede the name.
765 (setq middle (point))
766 (forward-word -1)
767 ;; Ignore these subparts of a class decl
768 ;; and move back to the class name itself.
769 (while (looking-at "public \\|private ")
770 (skip-chars-backward " \t:")
771 (setq end (point))
772 (backward-sexp 1)
773 (setq middle (point))
774 (forward-word -1))
775 (and (bolp)
776 (looking-at
777 "enum \\|struct \\|union \\|class ")
778 (setq middle (point)))
779 (goto-char end)
780 (when (eq (preceding-char) ?=)
781 (forward-char -1)
782 (skip-chars-backward " \t")
783 (setq end (point)))
784 (buffer-substring-no-properties
785 middle end))))))))
786 ((memq major-mode add-log-tex-like-modes)
787 (if (re-search-backward
788 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
789 nil t)
790 (progn
791 (goto-char (match-beginning 0))
792 (buffer-substring-no-properties
793 (1+ (point)) ; without initial backslash
794 (line-end-position)))))
795 ((eq major-mode 'texinfo-mode)
796 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
797 (match-string-no-properties 1)))
798 ((memq major-mode '(perl-mode cperl-mode))
799 (if (re-search-backward "^sub[ \t]+\\([^ \t\n]+\\)" nil t)
800 (match-string-no-properties 1)))
801 ;; Emacs's autoconf-mode installs its own
802 ;; `add-log-current-defun-function'. This applies to
803 ;; a different mode apparently for editing .m4
804 ;; autoconf source.
805 ((eq major-mode 'autoconf-mode)
806 (if (re-search-backward
807 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
808 (match-string-no-properties 3)))
810 ;; If all else fails, try heuristics
811 (let (case-fold-search
812 result)
813 (end-of-line)
814 (when (re-search-backward
815 add-log-current-defun-header-regexp
816 (- (point) 10000)
818 (setq result (or (match-string-no-properties 1)
819 (match-string-no-properties 0)))
820 ;; Strip whitespace away
821 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
822 result)
823 (setq result (match-string-no-properties 1 result)))
824 result))))))
825 (error nil)))
827 (defvar change-log-get-method-definition-md)
829 ;; Subroutine used within change-log-get-method-definition.
830 ;; Add the last match in the buffer to the end of `md',
831 ;; followed by the string END; move to the end of that match.
832 (defun change-log-get-method-definition-1 (end)
833 (setq change-log-get-method-definition-md
834 (concat change-log-get-method-definition-md
835 (match-string 1)
836 end))
837 (goto-char (match-end 0)))
839 (defun change-log-get-method-definition ()
840 "For objective C, return the method name if we are in a method."
841 (let ((change-log-get-method-definition-md "["))
842 (save-excursion
843 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
844 (change-log-get-method-definition-1 " ")))
845 (save-excursion
846 (cond
847 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
848 (change-log-get-method-definition-1 "")
849 (while (not (looking-at "[{;]"))
850 (looking-at
851 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
852 (change-log-get-method-definition-1 ""))
853 (concat change-log-get-method-definition-md "]"))))))
855 (defun change-log-sortable-date-at ()
856 "Return date of log entry in a consistent form for sorting.
857 Point is assumed to be at the start of the entry."
858 (require 'timezone)
859 (if (looking-at "^\\sw.........[0-9:+ ]*")
860 (let ((date (match-string-no-properties 0)))
861 (if date
862 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
863 (concat (match-string 1 date) (match-string 2 date)
864 (match-string 3 date))
865 (condition-case nil
866 (timezone-make-date-sortable date)
867 (error nil)))))
868 (error "Bad date")))
870 ;;;###autoload
871 (defun change-log-merge (other-log)
872 "Merge the contents of ChangeLog file OTHER-LOG with this buffer.
873 Both must be found in Change Log mode (since the merging depends on
874 the appropriate motion commands).
876 Entries are inserted in chronological order. Both the current and
877 old-style time formats for entries are supported."
878 (interactive "*fLog file name to merge: ")
879 (if (not (eq major-mode 'change-log-mode))
880 (error "Not in Change Log mode"))
881 (let ((other-buf (find-file-noselect other-log))
882 (buf (current-buffer))
883 date1 start end)
884 (save-excursion
885 (goto-char (point-min))
886 (set-buffer other-buf)
887 (goto-char (point-min))
888 (if (not (eq major-mode 'change-log-mode))
889 (error "%s not found in Change Log mode" other-log))
890 ;; Loop through all the entries in OTHER-LOG.
891 (while (not (eobp))
892 (setq date1 (change-log-sortable-date-at))
893 (setq start (point)
894 end (progn (forward-page) (point)))
895 ;; Look for an entry in original buffer that isn't later.
896 (with-current-buffer buf
897 (while (and (not (eobp))
898 (string< date1 (change-log-sortable-date-at)))
899 (forward-page))
900 (if (not (eobp))
901 (insert-buffer-substring other-buf start end)
902 ;; At the end of the original buffer, insert a newline to
903 ;; separate entries and then the rest of the file being
904 ;; merged. Move to the end of it to terminate outer loop.
905 (insert "\n")
906 (insert-buffer-substring other-buf start
907 (with-current-buffer other-buf
908 (goto-char (point-max))
909 (point)))))))))
911 ;;;###autoload
912 (defun change-log-redate ()
913 "Fix any old-style date entries in the current log file to default format."
914 (interactive)
915 (require 'timezone)
916 (save-excursion
917 (goto-char (point-min))
918 (while (re-search-forward "^\\sw.........[0-9:+ ]*" nil t)
919 (unless (= 12 (- (match-end 0) (match-beginning 0)))
920 (let* ((date (save-match-data
921 (timezone-fix-time (match-string 0) nil nil)))
922 (zone (if (consp (aref date 6))
923 (nth 1 (aref date 6)))))
924 (replace-match (format-time-string
925 "%Y-%m-%d "
926 (encode-time (aref date 5)
927 (aref date 4)
928 (aref date 3)
929 (aref date 2)
930 (aref date 1)
931 (aref date 0)
932 zone))))))))
934 (provide 'add-log)
936 ;;; add-log.el ends here