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