Use nicer comment syntax for arch taglines in Objective-C files
[emacs.git] / lisp / add-log.el
blob85293b2ec687c04b0db5cc319a433b4459b1440f
1 ;;; add-log.el --- change log maintenance commands for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2001,
4 ;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: tools
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This facility is documented in the Emacs Manual.
28 ;; Todo:
30 ;; - Find/use/create _MTN/log if there's a _MTN directory.
31 ;; - Find/use/create ++log.* if there's an {arch} directory.
32 ;; - Use an open *VC-Log* or *cvs-commit* buffer if it's related to the
33 ;; source file.
34 ;; - Don't add TAB indents (and username?) if inserting entries in those
35 ;; special places.
37 ;;; Code:
39 (eval-when-compile
40 (require 'timezone))
42 (defgroup change-log nil
43 "Change log maintenance."
44 :group 'tools
45 :link '(custom-manual "(emacs)Change Log")
46 :prefix "change-log-"
47 :prefix "add-log-")
50 (defcustom change-log-default-name nil
51 "Name of a change log file for \\[add-change-log-entry]."
52 :type '(choice (const :tag "default" nil)
53 string)
54 :group 'change-log)
55 (put 'change-log-default-name 'safe-local-variable 'string-or-null-p)
57 (defcustom change-log-mode-hook nil
58 "Normal hook run by `change-log-mode'."
59 :type 'hook
60 :group 'change-log)
62 ;; Many modes set this variable, so avoid warnings.
63 ;;;###autoload
64 (defcustom add-log-current-defun-function nil
65 "If non-nil, function to guess name of surrounding function.
66 It is used by `add-log-current-defun' in preference to built-in rules.
67 Returns function's name as a string, or nil if outside a function."
68 :type '(choice (const nil) function)
69 :group 'change-log)
71 ;;;###autoload
72 (defcustom add-log-full-name nil
73 "Full name of user, for inclusion in ChangeLog daily headers.
74 This defaults to the value returned by the function `user-full-name'."
75 :type '(choice (const :tag "Default" nil)
76 string)
77 :group 'change-log)
79 ;;;###autoload
80 (defcustom add-log-mailing-address nil
81 "Email addresses of user, for inclusion in ChangeLog headers.
82 This defaults to the value of `user-mail-address'. In addition to
83 being a simple string, this value can also be a list. All elements
84 will be recognized as referring to the same user; when creating a new
85 ChangeLog entry, one element will be chosen at random."
86 :type '(choice (const :tag "Default" nil)
87 (string :tag "String")
88 (repeat :tag "List of Strings" string))
89 :group 'change-log)
91 (defcustom add-log-time-format 'add-log-iso8601-time-string
92 "Function that defines the time format.
93 For example, `add-log-iso8601-time-string', which gives the
94 date in international ISO 8601 format,
95 and `current-time-string' are two valid values."
96 :type '(radio (const :tag "International ISO 8601 format"
97 add-log-iso8601-time-string)
98 (const :tag "Old format, as returned by `current-time-string'"
99 current-time-string)
100 (function :tag "Other"))
101 :group 'change-log)
103 (defcustom add-log-keep-changes-together nil
104 "If non-nil, normally keep day's log entries for one file together.
106 Log entries for a given file made with \\[add-change-log-entry] or
107 \\[add-change-log-entry-other-window] will only be added to others \
108 for that file made
109 today if this variable is non-nil or that file comes first in today's
110 entries. Otherwise another entry for that file will be started. An
111 original log:
113 * foo (...): ...
114 * bar (...): change 1
116 in the latter case, \\[add-change-log-entry-other-window] in a \
117 buffer visiting `bar', yields:
119 * bar (...): -!-
120 * foo (...): ...
121 * bar (...): change 1
123 and in the former:
125 * foo (...): ...
126 * bar (...): change 1
127 (...): -!-
129 The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
130 this variable."
131 :version "20.3"
132 :type 'boolean
133 :group 'change-log)
135 (defcustom add-log-always-start-new-record nil
136 "If non-nil, `add-change-log-entry' will always start a new record."
137 :version "22.1"
138 :type 'boolean
139 :group 'change-log)
141 (defcustom add-log-buffer-file-name-function nil
142 "If non-nil, function to call to identify the full filename of a buffer.
143 This function is called with no argument. If this is nil, the default is to
144 use `buffer-file-name'."
145 :type '(choice (const nil) function)
146 :group 'change-log)
148 (defcustom add-log-file-name-function nil
149 "If non-nil, function to call to identify the filename for a ChangeLog entry.
150 This function is called with one argument, the value of variable
151 `buffer-file-name' in that buffer. If this is nil, the default is to
152 use the file's name relative to the directory of the change log file."
153 :type '(choice (const nil) function)
154 :group 'change-log)
157 (defcustom change-log-version-info-enabled nil
158 "If non-nil, enable recording version numbers with the changes."
159 :version "21.1"
160 :type 'boolean
161 :group 'change-log)
163 (defcustom change-log-version-number-regexp-list
164 (let ((re "\\([0-9]+\.[0-9.]+\\)"))
165 (list
166 ;; (defconst ad-version "2.15"
167 (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re)
168 ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp
169 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
170 "List of regexps to search for version number.
171 The version number must be in group 1.
172 Note: The search is conducted only within 10%, at the beginning of the file."
173 :version "21.1"
174 :type '(repeat regexp)
175 :group 'change-log)
177 (defface change-log-date
178 '((t (:inherit font-lock-string-face)))
179 "Face used to highlight dates in date lines."
180 :version "21.1"
181 :group 'change-log)
182 ;; backward-compatibility alias
183 (put 'change-log-date-face 'face-alias 'change-log-date)
185 (defface change-log-name
186 '((t (:inherit font-lock-constant-face)))
187 "Face for highlighting author names."
188 :version "21.1"
189 :group 'change-log)
190 ;; backward-compatibility alias
191 (put 'change-log-name-face 'face-alias 'change-log-name)
193 (defface change-log-email
194 '((t (:inherit font-lock-variable-name-face)))
195 "Face for highlighting author email addresses."
196 :version "21.1"
197 :group 'change-log)
198 ;; backward-compatibility alias
199 (put 'change-log-email-face 'face-alias 'change-log-email)
201 (defface change-log-file
202 '((t (:inherit font-lock-function-name-face)))
203 "Face for highlighting file names."
204 :version "21.1"
205 :group 'change-log)
206 ;; backward-compatibility alias
207 (put 'change-log-file-face 'face-alias 'change-log-file)
209 (defface change-log-list
210 '((t (:inherit font-lock-keyword-face)))
211 "Face for highlighting parenthesized lists of functions or variables."
212 :version "21.1"
213 :group 'change-log)
214 ;; backward-compatibility alias
215 (put 'change-log-list-face 'face-alias 'change-log-list)
217 (defface change-log-conditionals
218 '((t (:inherit font-lock-variable-name-face)))
219 "Face for highlighting conditionals of the form `[...]'."
220 :version "21.1"
221 :group 'change-log)
222 ;; backward-compatibility alias
223 (put 'change-log-conditionals-face 'face-alias 'change-log-conditionals)
225 (defface change-log-function
226 '((t (:inherit font-lock-variable-name-face)))
227 "Face for highlighting items of the form `<....>'."
228 :version "21.1"
229 :group 'change-log)
230 ;; backward-compatibility alias
231 (put 'change-log-function-face 'face-alias 'change-log-function)
233 (defface change-log-acknowledgement
234 '((t (:inherit font-lock-comment-face)))
235 "Face for highlighting acknowledgments."
236 :version "21.1"
237 :group 'change-log)
238 ;; backward-compatibility alias
239 (put 'change-log-acknowledgement-face 'face-alias 'change-log-acknowledgement)
241 (defconst change-log-file-names-re "^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
242 (defconst change-log-start-entry-re "^\\sw.........[0-9:+ ]*")
244 (defvar change-log-font-lock-keywords
245 `(;;
246 ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles.
247 ;; Fixme: this regepx is just an approximate one and may match
248 ;; wrongly with a non-date line existing as a random note. In
249 ;; addition, using any kind of fixed setting like this doesn't
250 ;; work if a user customizes add-log-time-format.
251 ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+"
252 (0 'change-log-date-face)
253 ;; Name and e-mail; some people put e-mail in parens, not angles.
254 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
255 (1 'change-log-name)
256 (2 'change-log-email)))
258 ;; File names.
259 (,change-log-file-names-re
260 (2 'change-log-file)
261 ;; Possibly further names in a list:
262 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file))
263 ;; Possibly a parenthesized list of names:
264 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
265 nil nil (1 'change-log-list))
266 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
267 nil nil (1 'change-log-list)))
269 ;; Function or variable names.
270 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
271 (2 'change-log-list)
272 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
273 (1 'change-log-list)))
275 ;; Conditionals.
276 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
278 ;; Function of change.
279 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
281 ;; Acknowledgements.
282 ;; Don't include plain "From" because that is vague;
283 ;; we want to encourage people to say something more specific.
284 ;; Note that the FSF does not use "Patches by"; our convention
285 ;; is to put the name of the author of the changes at the top
286 ;; of the change log entry.
287 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
288 3 'change-log-acknowledgement))
289 "Additional expressions to highlight in Change Log mode.")
291 (defun change-log-search-file-name (where)
292 "Return the file-name for the change under point."
293 (save-excursion
294 (goto-char where)
295 (beginning-of-line 1)
296 (if (looking-at change-log-start-entry-re)
297 ;; We are at the start of an entry, search forward for a file
298 ;; name.
299 (progn
300 (re-search-forward change-log-file-names-re nil t)
301 (match-string-no-properties 2))
302 (if (looking-at change-log-file-names-re)
303 ;; We found a file name.
304 (match-string-no-properties 2)
305 ;; Look backwards for either a file name or the log entry start.
306 (if (re-search-backward
307 (concat "\\(" change-log-start-entry-re
308 "\\)\\|\\("
309 change-log-file-names-re "\\)") nil t)
310 (if (match-beginning 1)
311 ;; We got the start of the entry, look forward for a
312 ;; file name.
313 (progn
314 (re-search-forward change-log-file-names-re nil t)
315 (match-string-no-properties 2))
316 (match-string-no-properties 4))
317 ;; We must be before any file name, look forward.
318 (re-search-forward change-log-file-names-re nil t)
319 (match-string-no-properties 2))))))
321 (defun change-log-find-file ()
322 "Visit the file for the change under point."
323 (interactive)
324 (let ((file (change-log-search-file-name (point))))
325 (if (and file (file-exists-p file))
326 (find-file file)
327 (message "No such file or directory: %s" file))))
329 (defun change-log-search-tag-name-1 (&optional from)
330 "Search for a tag name within subexpression 1 of last match.
331 Optional argument FROM specifies a buffer position where the tag
332 name should be located. Return value is a cons whose car is the
333 string representing the tag and whose cdr is the position where
334 the tag was found."
335 (save-restriction
336 (narrow-to-region (match-beginning 1) (match-end 1))
337 (when from (goto-char from))
338 ;; The regexp below skips any symbol near `point' (FROM) followed by
339 ;; whitespace and another symbol. This should skip, for example,
340 ;; "struct" in a specification like "(struct buffer)" and move to
341 ;; "buffer". A leading paren is ignored.
342 (when (looking-at
343 "[(]?\\(?:\\(?:\\sw\\|\\s_\\)+\\(?:[ \t]+\\(\\sw\\|\\s_\\)+\\)\\)")
344 (goto-char (match-beginning 1)))
345 (cons (find-tag-default) (point))))
347 (defconst change-log-tag-re
348 "(\\(\\(?:\\sw\\|\\s_\\)+\\(?:[, \t]+\\(?:\\sw\\|\\s_\\)+\\)*\\))"
349 "Regexp matching a tag name in change log entries.")
351 (defun change-log-search-tag-name (&optional at)
352 "Search for a tag name near `point'.
353 Optional argument AT non-nil means search near buffer position
354 AT. Return value is a cons whose car is the string representing
355 the tag and whose cdr is the position where the tag was found."
356 (save-excursion
357 (goto-char (setq at (or at (point))))
358 (save-restriction
359 (widen)
360 (or (condition-case nil
361 ;; Within parenthesized list?
362 (save-excursion
363 (backward-up-list)
364 (when (looking-at change-log-tag-re)
365 (change-log-search-tag-name-1 at)))
366 (error nil))
367 (condition-case nil
368 ;; Before parenthesized list?
369 (save-excursion
370 (when (and (skip-chars-forward " \t")
371 (looking-at change-log-tag-re))
372 (change-log-search-tag-name-1)))
373 (error nil))
374 (condition-case nil
375 ;; Near filename?
376 (save-excursion
377 (when (and (progn
378 (beginning-of-line)
379 (looking-at change-log-file-names-re))
380 (goto-char (match-end 0))
381 (skip-syntax-forward " ")
382 (looking-at change-log-tag-re))
383 (change-log-search-tag-name-1)))
384 (error nil))
385 (condition-case nil
386 ;; Before filename?
387 (save-excursion
388 (when (and (progn
389 (skip-syntax-backward " ")
390 (beginning-of-line)
391 (looking-at change-log-file-names-re))
392 (goto-char (match-end 0))
393 (skip-syntax-forward " ")
394 (looking-at change-log-tag-re))
395 (change-log-search-tag-name-1)))
396 (error nil))
397 (condition-case nil
398 ;; Near start entry?
399 (save-excursion
400 (when (and (progn
401 (beginning-of-line)
402 (looking-at change-log-start-entry-re))
403 (forward-line) ; Won't work for multiple
404 ; names, etc.
405 (skip-syntax-forward " ")
406 (progn
407 (beginning-of-line)
408 (looking-at change-log-file-names-re))
409 (goto-char (match-end 0))
410 (re-search-forward change-log-tag-re))
411 (change-log-search-tag-name-1)))
412 (error nil))
413 (condition-case nil
414 ;; After parenthesized list?.
415 (when (re-search-backward change-log-tag-re)
416 (save-restriction
417 (narrow-to-region (match-beginning 1) (match-end 1))
418 (goto-char (point-max))
419 (cons (find-tag-default) (point-max))))
420 (error nil))))))
422 (defvar change-log-find-head nil)
423 (defvar change-log-find-tail nil)
425 (defun change-log-goto-source-1 (tag regexp file buffer
426 &optional window first last)
427 "Search for tag TAG in buffer BUFFER visiting file FILE.
428 REGEXP is a regular expression for TAG. The remaining arguments
429 are optional: WINDOW denotes the window to display the results of
430 the search. FIRST is a position in BUFFER denoting the first
431 match from previous searches for TAG. LAST is the position in
432 BUFFER denoting the last match for TAG in the last search."
433 (with-current-buffer buffer
434 (save-excursion
435 (save-restriction
436 (widen)
437 (if last
438 (progn
439 ;; When LAST is set make sure we continue from the next
440 ;; line end to not find the same tag again.
441 (goto-char last)
442 (end-of-line)
443 (condition-case nil
444 ;; Try to go to the end of the current defun to avoid
445 ;; false positives within the current defun's body
446 ;; since these would match `add-log-current-defun'.
447 (end-of-defun)
448 ;; Don't fall behind when `end-of-defun' fails.
449 (error (progn (goto-char last) (end-of-line))))
450 (setq last nil))
451 ;; When LAST was not set start at beginning of BUFFER.
452 (goto-char (point-min)))
453 (let (current-defun)
454 (while (and (not last) (re-search-forward regexp nil t))
455 ;; Verify that `add-log-current-defun' invoked at the end
456 ;; of the match returns TAG. This heuristic works well
457 ;; whenever the name of the defun occurs within the first
458 ;; line of the defun.
459 (setq current-defun (add-log-current-defun))
460 (when (and current-defun (string-equal current-defun tag))
461 ;; Record this as last match.
462 (setq last (line-beginning-position))
463 ;; Record this as first match when there's none.
464 (unless first (setq first last)))))))
465 (if (or last first)
466 (with-selected-window (or window (display-buffer buffer))
467 (if last
468 (progn
469 (when (or (< last (point-min)) (> last (point-max)))
470 ;; Widen to show TAG.
471 (widen))
472 (push-mark)
473 (goto-char last))
474 ;; When there are no more matches go (back) to FIRST.
475 (message "No more matches for tag `%s' in file `%s'" tag file)
476 (setq last first)
477 (goto-char first))
478 ;; Return new "tail".
479 (list (selected-window) first last))
480 (message "Source location of tag `%s' not found in file `%s'" tag file)
481 nil)))
483 (defun change-log-goto-source ()
484 "Go to source location of change log tag near `point'.
485 A change log tag is a symbol within a parenthesized,
486 comma-separated list."
487 (interactive)
488 (if (and (eq last-command 'change-log-goto-source)
489 change-log-find-tail)
490 (setq change-log-find-tail
491 (condition-case nil
492 (apply 'change-log-goto-source-1
493 (append change-log-find-head change-log-find-tail))
494 (error
495 (format "Cannot find more matches for tag `%s' in file `%s'"
496 (car change-log-find-head)
497 (nth 2 change-log-find-head)))))
498 (save-excursion
499 (let* ((tag-at (change-log-search-tag-name))
500 (tag (car tag-at))
501 (file (when tag-at
502 (change-log-search-file-name (cdr tag-at)))))
503 (if (or (not tag) (not file))
504 (error "No suitable tag near `point'")
505 (setq change-log-find-head
506 (list tag (concat "\\_<" (regexp-quote tag) "\\_>")
507 file (find-file-noselect file)))
508 (condition-case nil
509 (setq change-log-find-tail
510 (apply 'change-log-goto-source-1 change-log-find-head))
511 (error (format "Cannot find matches for tag `%s' in `%s'"
512 tag file))))))))
514 (defvar change-log-mode-map
515 (let ((map (make-sparse-keymap)))
516 (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment)
517 (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment)
518 (define-key map [?\C-c ?\C-f] 'change-log-find-file)
519 (define-key map [?\C-c ?\C-c] 'change-log-goto-source)
520 map)
521 "Keymap for Change Log major mode.")
523 ;; It used to be called change-log-time-zone-rule but really should be
524 ;; called add-log-time-zone-rule since it's only used from add-log-* code.
525 (defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
526 (defvar add-log-time-zone-rule nil
527 "Time zone used for calculating change log time stamps.
528 It takes the same format as the TZ argument of `set-time-zone-rule'.
529 If nil, use local time.
530 If t, use universal time.")
531 (put 'add-log-time-zone-rule 'safe-local-variable
532 '(lambda (x) (or (booleanp x) (stringp x))))
534 (defun add-log-iso8601-time-zone (&optional time)
535 (let* ((utc-offset (or (car (current-time-zone time)) 0))
536 (sign (if (< utc-offset 0) ?- ?+))
537 (sec (abs utc-offset))
538 (ss (% sec 60))
539 (min (/ sec 60))
540 (mm (% min 60))
541 (hh (/ min 60)))
542 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
543 ((not (zerop mm)) "%c%02d:%02d")
544 (t "%c%02d"))
545 sign hh mm ss)))
547 (defvar add-log-iso8601-with-time-zone nil)
549 (defun add-log-iso8601-time-string ()
550 (let ((time (format-time-string "%Y-%m-%d"
551 nil (eq t add-log-time-zone-rule))))
552 (if add-log-iso8601-with-time-zone
553 (concat time " " (add-log-iso8601-time-zone))
554 time)))
556 (defun change-log-name ()
557 "Return (system-dependent) default name for a change log file."
558 (or change-log-default-name
559 (if (eq system-type 'vax-vms)
560 "$CHANGE_LOG$.TXT"
561 "ChangeLog")))
563 (defun add-log-edit-prev-comment (arg)
564 "Cycle backward through Log-Edit mode comment history.
565 With a numeric prefix ARG, go back ARG comments."
566 (interactive "*p")
567 (save-restriction
568 (narrow-to-region (point)
569 (if (memq last-command '(add-log-edit-prev-comment
570 add-log-edit-next-comment))
571 (mark) (point)))
572 (when (fboundp 'log-edit-previous-comment)
573 (log-edit-previous-comment arg)
574 (indent-region (point-min) (point-max))
575 (goto-char (point-min))
576 (unless (save-restriction (widen) (bolp))
577 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
578 (set-mark (point-min))
579 (goto-char (point-max))
580 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
582 (defun add-log-edit-next-comment (arg)
583 "Cycle forward through Log-Edit mode comment history.
584 With a numeric prefix ARG, go back ARG comments."
585 (interactive "*p")
586 (add-log-edit-prev-comment (- arg)))
588 ;;;###autoload
589 (defun prompt-for-change-log-name ()
590 "Prompt for a change log name."
591 (let* ((default (change-log-name))
592 (name (expand-file-name
593 (read-file-name (format "Log file (default %s): " default)
594 nil default))))
595 ;; Handle something that is syntactically a directory name.
596 ;; Look for ChangeLog or whatever in that directory.
597 (if (string= (file-name-nondirectory name) "")
598 (expand-file-name (file-name-nondirectory default)
599 name)
600 ;; Handle specifying a file that is a directory.
601 (if (file-directory-p name)
602 (expand-file-name (file-name-nondirectory default)
603 (file-name-as-directory name))
604 name))))
606 (defun change-log-version-number-search ()
607 "Return version number of current buffer's file.
608 This is the value returned by `vc-working-revision' or, if that is
609 nil, by matching `change-log-version-number-regexp-list'."
610 (let* ((size (buffer-size))
611 (limit
612 ;; The version number can be anywhere in the file, but
613 ;; restrict search to the file beginning: 10% should be
614 ;; enough to prevent some mishits.
616 ;; Apply percentage only if buffer size is bigger than
617 ;; approx 100 lines.
618 (if (> size (* 100 80)) (+ (point) (/ size 10)))))
619 (or (and buffer-file-name (vc-working-revision buffer-file-name))
620 (save-restriction
621 (widen)
622 (let ((regexps change-log-version-number-regexp-list)
623 version)
624 (while regexps
625 (save-excursion
626 (goto-char (point-min))
627 (when (re-search-forward (pop regexps) limit t)
628 (setq version (match-string 1)
629 regexps nil))))
630 version)))))
633 ;;;###autoload
634 (defun find-change-log (&optional file-name buffer-file)
635 "Find a change log file for \\[add-change-log-entry] and return the name.
637 Optional arg FILE-NAME specifies the file to use.
638 If FILE-NAME is nil, use the value of `change-log-default-name'.
639 If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
640 \(or whatever we use on this operating system).
642 If `change-log-default-name' contains a leading directory component, then
643 simply find it in the current directory. Otherwise, search in the current
644 directory and its successive parents for a file so named.
646 Once a file is found, `change-log-default-name' is set locally in the
647 current buffer to the complete file name.
648 Optional arg BUFFER-FILE overrides `buffer-file-name'."
649 ;; If user specified a file name or if this buffer knows which one to use,
650 ;; just use that.
651 (or file-name
652 (setq file-name (and change-log-default-name
653 (file-name-directory change-log-default-name)
654 change-log-default-name))
655 (progn
656 ;; Chase links in the source file
657 ;; and use the change log in the dir where it points.
658 (setq file-name (or (and (or buffer-file buffer-file-name)
659 (file-name-directory
660 (file-chase-links
661 (or buffer-file buffer-file-name))))
662 default-directory))
663 (if (file-directory-p file-name)
664 (setq file-name (expand-file-name (change-log-name) file-name)))
665 ;; Chase links before visiting the file.
666 ;; This makes it easier to use a single change log file
667 ;; for several related directories.
668 (setq file-name (file-chase-links file-name))
669 (setq file-name (expand-file-name file-name))
670 ;; Move up in the dir hierarchy till we find a change log file.
671 (let ((file1 file-name)
672 parent-dir)
673 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
674 (progn (setq parent-dir
675 (file-name-directory
676 (directory-file-name
677 (file-name-directory file1))))
678 ;; Give up if we are already at the root dir.
679 (not (string= (file-name-directory file1)
680 parent-dir))))
681 ;; Move up to the parent dir and try again.
682 (setq file1 (expand-file-name
683 (file-name-nondirectory (change-log-name))
684 parent-dir)))
685 ;; If we found a change log in a parent, use that.
686 (if (or (get-file-buffer file1) (file-exists-p file1))
687 (setq file-name file1)))))
688 ;; Make a local variable in this buffer so we needn't search again.
689 (set (make-local-variable 'change-log-default-name) file-name)
690 file-name)
692 (defun add-log-file-name (buffer-file log-file)
693 ;; Never want to add a change log entry for the ChangeLog file itself.
694 (unless (or (null buffer-file) (string= buffer-file log-file))
695 (if add-log-file-name-function
696 (funcall add-log-file-name-function buffer-file)
697 (setq buffer-file
698 (file-relative-name buffer-file (file-name-directory log-file)))
699 ;; If we have a backup file, it's presumably because we're
700 ;; comparing old and new versions (e.g. for deleted
701 ;; functions) and we'll want to use the original name.
702 (if (backup-file-name-p buffer-file)
703 (file-name-sans-versions buffer-file)
704 buffer-file))))
706 ;;;###autoload
707 (defun add-change-log-entry (&optional whoami file-name other-window new-entry
708 put-new-entry-on-new-line)
709 "Find change log file, and add an entry for today and an item for this file.
710 Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
711 name and email (stored in `add-log-full-name' and `add-log-mailing-address').
713 Second arg FILE-NAME is file name of the change log.
714 If nil, use the value of `change-log-default-name'.
716 Third arg OTHER-WINDOW non-nil means visit in other window.
718 Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
719 never append to an existing entry. Option `add-log-keep-changes-together'
720 otherwise affects whether a new entry is created.
722 Fifth arg PUT-NEW-ENTRY-ON-NEW-LINE non-nil means that if a new
723 entry is created, put it on a new line by itself, do not put it
724 after a comma on an existing line.
726 Option `add-log-always-start-new-record' non-nil means always create a
727 new record, even when the last record was made on the same date and by
728 the same person.
730 The change log file can start with a copyright notice and a copying
731 permission notice. The first blank line indicates the end of these
732 notices.
734 Today's date is calculated according to `add-log-time-zone-rule' if
735 non-nil, otherwise in local time."
736 (interactive (list current-prefix-arg
737 (prompt-for-change-log-name)))
738 (let* ((defun (add-log-current-defun))
739 (version (and change-log-version-info-enabled
740 (change-log-version-number-search)))
741 (buf-file-name (if add-log-buffer-file-name-function
742 (funcall add-log-buffer-file-name-function)
743 buffer-file-name))
744 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
745 (file-name (expand-file-name (find-change-log file-name buffer-file)))
746 ;; Set ITEM to the file name to use in the new item.
747 (item (add-log-file-name buffer-file file-name)))
749 (unless (equal file-name buffer-file-name)
750 (if (or other-window (window-dedicated-p (selected-window)))
751 (find-file-other-window file-name)
752 (find-file file-name)))
753 (or (derived-mode-p 'change-log-mode)
754 (change-log-mode))
755 (undo-boundary)
756 (goto-char (point-min))
758 (let ((full-name (or add-log-full-name (user-full-name)))
759 (mailing-address (or add-log-mailing-address user-mail-address)))
761 (when whoami
762 (setq full-name (read-string "Full name: " full-name))
763 ;; Note that some sites have room and phone number fields in
764 ;; full name which look silly when inserted. Rather than do
765 ;; anything about that here, let user give prefix argument so that
766 ;; s/he can edit the full name field in prompter if s/he wants.
767 (setq mailing-address
768 (read-string "Mailing address: " mailing-address)))
770 ;; If file starts with a copyright and permission notice, skip them.
771 ;; Assume they end at first blank line.
772 (when (looking-at "Copyright")
773 (search-forward "\n\n")
774 (skip-chars-forward "\n"))
776 ;; Advance into first entry if it is usable; else make new one.
777 (let ((new-entries
778 (mapcar (lambda (addr)
779 (concat
780 (if (stringp add-log-time-zone-rule)
781 (let ((tz (getenv "TZ")))
782 (unwind-protect
783 (progn
784 (set-time-zone-rule add-log-time-zone-rule)
785 (funcall add-log-time-format))
786 (set-time-zone-rule tz)))
787 (funcall add-log-time-format))
788 " " full-name
789 " <" addr ">"))
790 (if (consp mailing-address)
791 mailing-address
792 (list mailing-address)))))
793 (if (and (not add-log-always-start-new-record)
794 (let ((hit nil))
795 (dolist (entry new-entries hit)
796 (when (looking-at (regexp-quote entry))
797 (setq hit t)))))
798 (forward-line 1)
799 (insert (nth (random (length new-entries))
800 new-entries)
801 (if use-hard-newlines hard-newline "\n")
802 (if use-hard-newlines hard-newline "\n"))
803 (forward-line -1))))
805 ;; Determine where we should stop searching for a usable
806 ;; item to add to, within this entry.
807 (let ((bound
808 (save-excursion
809 (if (looking-at "\n*[^\n* \t]")
810 (skip-chars-forward "\n")
811 (if add-log-keep-changes-together
812 (forward-page) ; page delimits entries for date
813 (forward-paragraph))) ; paragraph delimits entries for file
814 (point))))
816 ;; Now insert the new line for this item.
817 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
818 ;; Put this file name into the existing empty item.
819 (if item
820 (insert item)))
821 ((and (not new-entry)
822 (let (case-fold-search)
823 (re-search-forward
824 (concat (regexp-quote (concat "* " item))
825 ;; Don't accept `foo.bar' when
826 ;; looking for `foo':
827 "\\(\\s \\|[(),:]\\)")
828 bound t)))
829 ;; Add to the existing item for the same file.
830 (re-search-forward "^\\s *$\\|^\\s \\*")
831 (goto-char (match-beginning 0))
832 ;; Delete excess empty lines; make just 2.
833 (while (and (not (eobp)) (looking-at "^\\s *$"))
834 (delete-region (point) (line-beginning-position 2)))
835 (insert (if use-hard-newlines hard-newline "\n")
836 (if use-hard-newlines hard-newline "\n"))
837 (forward-line -2)
838 (indent-relative-maybe))
840 ;; Make a new item.
841 (while (looking-at "\\sW")
842 (forward-line 1))
843 (while (and (not (eobp)) (looking-at "^\\s *$"))
844 (delete-region (point) (line-beginning-position 2)))
845 (insert (if use-hard-newlines hard-newline "\n")
846 (if use-hard-newlines hard-newline "\n")
847 (if use-hard-newlines hard-newline "\n"))
848 (forward-line -2)
849 (indent-to left-margin)
850 (insert "* ")
851 (if item (insert item)))))
852 ;; Now insert the function name, if we have one.
853 ;; Point is at the item for this file,
854 ;; either at the end of the line or at the first blank line.
855 (if (not defun)
856 ;; No function name, so put in a colon unless we have just a star.
857 (unless (save-excursion
858 (beginning-of-line 1)
859 (looking-at "\\s *\\(\\*\\s *\\)?$"))
860 (insert ": ")
861 (if version (insert version ?\s)))
862 ;; Make it easy to get rid of the function name.
863 (undo-boundary)
864 (unless (save-excursion
865 (beginning-of-line 1)
866 (looking-at "\\s *$"))
867 (insert ?\s))
868 ;; See if the prev function name has a message yet or not.
869 ;; If not, merge the two items.
870 (let ((pos (point-marker)))
871 (skip-syntax-backward " ")
872 (skip-chars-backward "):")
873 (if (and (not put-new-entry-on-new-line)
874 (looking-at "):")
875 (let ((pos (save-excursion (backward-sexp 1) (point))))
876 (when (equal (buffer-substring pos (point)) defun)
877 (delete-region pos (point)))
878 (> fill-column (+ (current-column) (length defun) 4))))
879 (progn (skip-chars-backward ", ")
880 (delete-region (point) pos)
881 (unless (memq (char-before) '(?\()) (insert ", ")))
882 (when (and (not put-new-entry-on-new-line) (looking-at "):"))
883 (delete-region (+ 1 (point)) (line-end-position)))
884 (goto-char pos)
885 (insert "("))
886 (set-marker pos nil))
887 (insert defun "): ")
888 (if version (insert version ?\s)))))
890 ;;;###autoload
891 (defun add-change-log-entry-other-window (&optional whoami file-name)
892 "Find change log file in other window and add entry and item.
893 This is just like `add-change-log-entry' except that it displays
894 the change log file in another window."
895 (interactive (if current-prefix-arg
896 (list current-prefix-arg
897 (prompt-for-change-log-name))))
898 (add-change-log-entry whoami file-name t))
901 (defvar change-log-indent-text 0)
903 (defun change-log-fill-parenthesized-list ()
904 ;; Fill parenthesized lists of names according to GNU standards.
905 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
906 ;; should be filled as
907 ;; * file-name.ext (very-long-foo, very-long-bar)
908 ;; (very-long-foobar):
909 (save-excursion
910 (end-of-line 0)
911 (skip-chars-backward " \t")
912 (when (and (equal (char-before) ?\,)
913 (> (point) (1+ (point-min))))
914 (condition-case nil
915 (when (save-excursion
916 (and (prog2
917 (up-list -1)
918 (equal (char-after) ?\()
919 (skip-chars-backward " \t"))
920 (or (bolp)
921 ;; Skip everything but a whitespace or asterisk.
922 (and (not (zerop (skip-chars-backward "^ \t\n*")))
923 (skip-chars-backward " \t")
924 ;; We want one asterisk here.
925 (= (skip-chars-backward "*") -1)
926 (skip-chars-backward " \t")
927 (bolp)))))
928 ;; Delete the comma.
929 (delete-char -1)
930 ;; Close list on previous line.
931 (insert ")")
932 (skip-chars-forward " \t\n")
933 ;; Start list on new line.
934 (insert-before-markers "("))
935 (error nil)))))
937 (defun change-log-indent ()
938 (change-log-fill-parenthesized-list)
939 (let* ((indent
940 (save-excursion
941 (beginning-of-line)
942 (skip-chars-forward " \t")
943 (cond
944 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
945 ;; Matching the output of add-log-time-format is difficult,
946 ;; but I'll get it has at least two adjacent digits.
947 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
949 ((looking-at "[^*(]")
950 (+ (current-left-margin) change-log-indent-text))
951 (t (current-left-margin)))))
952 (pos (save-excursion (indent-line-to indent) (point))))
953 (if (> pos (point)) (goto-char pos))))
956 (defvar smerge-resolve-function)
957 (defvar copyright-at-end-flag)
959 ;;;###autoload
960 (define-derived-mode change-log-mode text-mode "Change Log"
961 "Major mode for editing change logs; like Indented Text Mode.
962 Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
963 New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
964 Each entry behaves as a paragraph, and the entries for one day as a page.
965 Runs `change-log-mode-hook'.
966 \\{change-log-mode-map}"
967 (setq left-margin 8
968 fill-column 74
969 indent-tabs-mode t
970 tab-width 8
971 show-trailing-whitespace t)
972 (set (make-local-variable 'fill-paragraph-function)
973 'change-log-fill-paragraph)
974 ;; Avoid that filling leaves behind a single "*" on a line.
975 (add-hook 'fill-nobreak-predicate
976 '(lambda ()
977 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
978 nil t)
979 (set (make-local-variable 'indent-line-function) 'change-log-indent)
980 (set (make-local-variable 'tab-always-indent) nil)
981 (set (make-local-variable 'copyright-at-end-flag) t)
982 ;; We really do want "^" in paragraph-start below: it is only the
983 ;; lines that begin at column 0 (despite the left-margin of 8) that
984 ;; we are looking for. Adding `* ' allows eliding the blank line
985 ;; between entries for different files.
986 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
987 (set (make-local-variable 'paragraph-separate) paragraph-start)
988 ;; Match null string on the date-line so that the date-line
989 ;; is grouped with what follows.
990 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
991 (set (make-local-variable 'version-control) 'never)
992 (set (make-local-variable 'smerge-resolve-function)
993 'change-log-resolve-conflict)
994 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
995 (set (make-local-variable 'font-lock-defaults)
996 '(change-log-font-lock-keywords t nil nil backward-paragraph))
997 (set (make-local-variable 'isearch-buffers-next-buffer-function)
998 'change-log-next-buffer)
999 (set (make-local-variable 'beginning-of-defun-function)
1000 'change-log-beginning-of-defun)
1001 (set (make-local-variable 'end-of-defun-function)
1002 'change-log-end-of-defun)
1003 (isearch-buffers-minor-mode))
1005 (defun change-log-next-buffer (&optional buffer wrap)
1006 "Return the next buffer in the series of ChangeLog file buffers.
1007 This function is used for multiple buffers isearch.
1008 A sequence of buffers is formed by ChangeLog files with decreasing
1009 numeric file name suffixes in the directory of the initial ChangeLog
1010 file were isearch was started."
1011 (let* ((name (change-log-name))
1012 (files (cons name (sort (file-expand-wildcards
1013 (concat name "[-.][0-9]*"))
1014 (lambda (a b)
1015 ;; The file's extension may not have a valid
1016 ;; version form (e.g. VC backup revisions).
1017 (ignore-errors
1018 (version< (substring b (length name))
1019 (substring a (length name))))))))
1020 (files (if isearch-forward files (reverse files))))
1021 (find-file-noselect
1022 (if wrap
1023 (car files)
1024 (cadr (member (file-name-nondirectory (buffer-file-name buffer))
1025 files))))))
1027 ;; It might be nice to have a general feature to replace this. The idea I
1028 ;; have is a variable giving a regexp matching text which should not be
1029 ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
1030 ;; But I don't feel up to implementing that today.
1031 (defun change-log-fill-paragraph (&optional justify)
1032 "Fill the paragraph, but preserve open parentheses at beginning of lines.
1033 Prefix arg means justify as well."
1034 (interactive "P")
1035 (let ((end (progn (forward-paragraph) (point)))
1036 (beg (progn (backward-paragraph) (point)))
1037 ;; Add lines starting with whitespace followed by a left paren or an
1038 ;; asterisk.
1039 (paragraph-start (concat paragraph-start "\\|\\s *\\(?:\\s(\\|\\*\\)"))
1040 ;; Make sure we call `change-log-indent'.
1041 (fill-indent-according-to-mode t))
1042 (fill-region beg end justify)
1045 (defcustom add-log-current-defun-header-regexp
1046 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
1047 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
1048 :type 'regexp
1049 :group 'change-log)
1051 ;;;###autoload
1052 (defvar add-log-lisp-like-modes
1053 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
1054 "*Modes that look like Lisp to `add-log-current-defun'.")
1056 ;;;###autoload
1057 (defvar add-log-c-like-modes
1058 '(c-mode c++-mode c++-c-mode objc-mode)
1059 "*Modes that look like C to `add-log-current-defun'.")
1061 ;;;###autoload
1062 (defvar add-log-tex-like-modes
1063 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode)
1064 "*Modes that look like TeX to `add-log-current-defun'.")
1066 (declare-function c-cpp-define-name "cc-cmds" ())
1067 (declare-function c-defun-name "cc-cmds" ())
1069 ;;;###autoload
1070 (defun add-log-current-defun ()
1071 "Return name of function definition point is in, or nil.
1073 Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
1074 Texinfo (@node titles) and Perl.
1076 Other modes are handled by a heuristic that looks in the 10K before
1077 point for uppercase headings starting in the first column or
1078 identifiers followed by `:' or `='. See variables
1079 `add-log-current-defun-header-regexp' and
1080 `add-log-current-defun-function'.
1082 Has a preference of looking backwards."
1083 (condition-case nil
1084 (save-excursion
1085 (let ((location (point)))
1086 (cond (add-log-current-defun-function
1087 (funcall add-log-current-defun-function))
1088 ((apply 'derived-mode-p add-log-lisp-like-modes)
1089 ;; If we are now precisely at the beginning of a defun,
1090 ;; make sure beginning-of-defun finds that one
1091 ;; rather than the previous one.
1092 (or (eobp) (forward-char 1))
1093 (beginning-of-defun)
1094 ;; Make sure we are really inside the defun found,
1095 ;; not after it.
1096 (when (and (looking-at "\\s(")
1097 (progn (end-of-defun)
1098 (< location (point)))
1099 (progn (forward-sexp -1)
1100 (>= location (point))))
1101 (if (looking-at "\\s(")
1102 (forward-char 1))
1103 ;; Skip the defining construct name, typically "defun"
1104 ;; or "defvar".
1105 (forward-sexp 1)
1106 ;; The second element is usually a symbol being defined.
1107 ;; If it is not, use the first symbol in it.
1108 (skip-chars-forward " \t\n'(")
1109 (buffer-substring-no-properties (point)
1110 (progn (forward-sexp 1)
1111 (point)))))
1112 ((apply 'derived-mode-p add-log-c-like-modes)
1113 (or (c-cpp-define-name)
1114 (c-defun-name)))
1115 ((memq major-mode add-log-tex-like-modes)
1116 (if (re-search-backward
1117 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
1118 nil t)
1119 (progn
1120 (goto-char (match-beginning 0))
1121 (buffer-substring-no-properties
1122 (1+ (point)) ; without initial backslash
1123 (line-end-position)))))
1124 ((derived-mode-p 'texinfo-mode)
1125 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
1126 (match-string-no-properties 1)))
1127 ((derived-mode-p 'perl-mode 'cperl-mode)
1128 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
1129 (match-string-no-properties 1)))
1130 ;; Emacs's autoconf-mode installs its own
1131 ;; `add-log-current-defun-function'. This applies to
1132 ;; a different mode apparently for editing .m4
1133 ;; autoconf source.
1134 ((derived-mode-p 'autoconf-mode)
1135 (if (re-search-backward
1136 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
1137 (match-string-no-properties 3)))
1139 ;; If all else fails, try heuristics
1140 (let (case-fold-search
1141 result)
1142 (end-of-line)
1143 (when (re-search-backward
1144 add-log-current-defun-header-regexp
1145 (- (point) 10000)
1147 (setq result (or (match-string-no-properties 1)
1148 (match-string-no-properties 0)))
1149 ;; Strip whitespace away
1150 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
1151 result)
1152 (setq result (match-string-no-properties 1 result)))
1153 result))))))
1154 (error nil)))
1156 (defvar change-log-get-method-definition-md)
1158 ;; Subroutine used within change-log-get-method-definition.
1159 ;; Add the last match in the buffer to the end of `md',
1160 ;; followed by the string END; move to the end of that match.
1161 (defun change-log-get-method-definition-1 (end)
1162 (setq change-log-get-method-definition-md
1163 (concat change-log-get-method-definition-md
1164 (match-string 1)
1165 end))
1166 (goto-char (match-end 0)))
1168 (defun change-log-get-method-definition ()
1169 "For Objective C, return the method name if we are in a method."
1170 (let ((change-log-get-method-definition-md "["))
1171 (save-excursion
1172 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
1173 (change-log-get-method-definition-1 " ")))
1174 (save-excursion
1175 (cond
1176 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
1177 (change-log-get-method-definition-1 "")
1178 (while (not (looking-at "[{;]"))
1179 (looking-at
1180 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
1181 (change-log-get-method-definition-1 ""))
1182 (concat change-log-get-method-definition-md "]"))))))
1184 (defun change-log-sortable-date-at ()
1185 "Return date of log entry in a consistent form for sorting.
1186 Point is assumed to be at the start of the entry."
1187 (require 'timezone)
1188 (if (looking-at change-log-start-entry-re)
1189 (let ((date (match-string-no-properties 0)))
1190 (if date
1191 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1192 (concat (match-string 1 date) (match-string 2 date)
1193 (match-string 3 date))
1194 (condition-case nil
1195 (timezone-make-date-sortable date)
1196 (error nil)))))
1197 (error "Bad date")))
1199 (defun change-log-resolve-conflict ()
1200 "Function to be used in `smerge-resolve-function'."
1201 (save-excursion
1202 (save-restriction
1203 (narrow-to-region (match-beginning 0) (match-end 0))
1204 (let ((mb1 (match-beginning 1))
1205 (me1 (match-end 1))
1206 (mb3 (match-beginning 3))
1207 (me3 (match-end 3))
1208 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1209 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1210 (unwind-protect
1211 (let ((buf (current-buffer)))
1212 (with-current-buffer tmp1
1213 (change-log-mode)
1214 (insert-buffer-substring buf mb1 me1))
1215 (with-current-buffer tmp2
1216 (change-log-mode)
1217 (insert-buffer-substring buf mb3 me3)
1218 ;; Do the merge here instead of inside `buf' so as to be
1219 ;; more robust in case change-log-merge fails.
1220 (change-log-merge tmp1))
1221 (goto-char (point-max))
1222 (delete-region (point-min)
1223 (prog1 (point)
1224 (insert-buffer-substring tmp2))))
1225 (kill-buffer tmp1)
1226 (kill-buffer tmp2))))))
1228 ;;;###autoload
1229 (defun change-log-merge (other-log)
1230 "Merge the contents of change log file OTHER-LOG with this buffer.
1231 Both must be found in Change Log mode (since the merging depends on
1232 the appropriate motion commands). OTHER-LOG can be either a file name
1233 or a buffer.
1235 Entries are inserted in chronological order. Both the current and
1236 old-style time formats for entries are supported."
1237 (interactive "*fLog file name to merge: ")
1238 (if (not (derived-mode-p 'change-log-mode))
1239 (error "Not in Change Log mode"))
1240 (let ((other-buf (if (bufferp other-log) other-log
1241 (find-file-noselect other-log)))
1242 (buf (current-buffer))
1243 date1 start end)
1244 (save-excursion
1245 (goto-char (point-min))
1246 (set-buffer other-buf)
1247 (goto-char (point-min))
1248 (if (not (derived-mode-p 'change-log-mode))
1249 (error "%s not found in Change Log mode" other-log))
1250 ;; Loop through all the entries in OTHER-LOG.
1251 (while (not (eobp))
1252 (setq date1 (change-log-sortable-date-at))
1253 (setq start (point)
1254 end (progn (forward-page) (point)))
1255 ;; Look for an entry in original buffer that isn't later.
1256 (with-current-buffer buf
1257 (while (and (not (eobp))
1258 (string< date1 (change-log-sortable-date-at)))
1259 (forward-page))
1260 (if (not (eobp))
1261 (insert-buffer-substring other-buf start end)
1262 ;; At the end of the original buffer, insert a newline to
1263 ;; separate entries and then the rest of the file being
1264 ;; merged.
1265 (unless (or (bobp)
1266 (and (= ?\n (char-before))
1267 (or (<= (1- (point)) (point-min))
1268 (= ?\n (char-before (1- (point)))))))
1269 (insert (if use-hard-newlines hard-newline "\n")))
1270 ;; Move to the end of it to terminate outer loop.
1271 (with-current-buffer other-buf
1272 (goto-char (point-max)))
1273 (insert-buffer-substring other-buf start)))))))
1275 (defun change-log-beginning-of-defun ()
1276 (re-search-backward change-log-start-entry-re nil 'move))
1278 (defun change-log-end-of-defun ()
1279 ;; Look back and if there is no entry there it means we are before
1280 ;; the first ChangeLog entry, so go forward until finding one.
1281 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t))
1282 (re-search-forward change-log-start-entry-re nil t))
1284 ;; In case we are at the end of log entry going forward a line will
1285 ;; make us find the next entry when searching. If we are inside of
1286 ;; an entry going forward a line will still keep the point inside
1287 ;; the same entry.
1288 (forward-line 1)
1290 ;; In case we are at the beginning of an entry, move past it.
1291 (when (looking-at change-log-start-entry-re)
1292 (goto-char (match-end 0))
1293 (forward-line 1))
1295 ;; Search for the start of the next log entry. Go to the end of the
1296 ;; buffer if we could not find a next entry.
1297 (when (re-search-forward change-log-start-entry-re nil 'move)
1298 (goto-char (match-beginning 0))
1299 (forward-line -1)))
1301 (provide 'add-log)
1303 ;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
1304 ;;; add-log.el ends here