(define-ccl-program): Move `doc-string' decl down.
[bpt/emacs.git] / lisp / add-log.el
CommitLineData
5abdc915 1;;; add-log.el --- change log maintenance commands for Emacs
84fc2cfa 2
e91081eb 3;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2001,
409cc4a3 4;; 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
84fc2cfa 5
6228c05b 6;; Maintainer: FSF
df63ae66 7;; Keywords: tools
e9571d2a 8
84fc2cfa
ER
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
84fc2cfa 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
84fc2cfa
ER
15
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.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
84fc2cfa 23
e41b2db1
ER
24;;; Commentary:
25
26;; This facility is documented in the Emacs Manual.
27
b0a96f7b
SM
28;; Todo:
29
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.
36
fd7fa35a 37;;; Code:
84fc2cfa 38
776d8e16 39(eval-when-compile
5960adc7 40 (require 'timezone))
3697b807 41
fcad5199 42(defgroup change-log nil
7b297602 43 "Change log maintenance."
fcad5199 44 :group 'tools
3697b807 45 :link '(custom-manual "(emacs)Change Log")
fcad5199
RS
46 :prefix "change-log-"
47 :prefix "add-log-")
84fc2cfa 48
fcad5199
RS
49
50(defcustom change-log-default-name nil
b0a96f7b 51 "Name of a change log file for \\[add-change-log-entry]."
fcad5199
RS
52 :type '(choice (const :tag "default" nil)
53 string)
54 :group 'change-log)
10b35c39 55(put 'change-log-default-name 'safe-local-variable 'string-or-null-p)
fcad5199 56
c3979f12
DL
57(defcustom change-log-mode-hook nil
58 "Normal hook run by `change-log-mode'."
59 :type 'hook
60 :group 'change-log)
61
c882e115
RS
62;; Many modes set this variable, so avoid warnings.
63;;;###autoload
fcad5199 64(defcustom add-log-current-defun-function nil
bb042dc6 65 "If non-nil, function to guess name of surrounding function.
5960adc7
DL
66It is used by `add-log-current-defun' in preference to built-in rules.
67Returns function's name as a string, or nil if outside a function."
cea3855a 68 :type '(choice (const nil) function)
fcad5199 69 :group 'change-log)
6258d3af 70
29db528b 71;;;###autoload
fcad5199 72(defcustom add-log-full-name nil
bb042dc6 73 "Full name of user, for inclusion in ChangeLog daily headers.
075a6629 74This defaults to the value returned by the function `user-full-name'."
fcad5199
RS
75 :type '(choice (const :tag "Default" nil)
76 string)
77 :group 'change-log)
02ec1592 78
29db528b 79;;;###autoload
fcad5199 80(defcustom add-log-mailing-address nil
d1921057 81 "Email addresses of user, for inclusion in ChangeLog headers.
c1b3ae42
CW
82This defaults to the value of `user-mail-address'. In addition to
83being a simple string, this value can also be a list. All elements
84will be recognized as referring to the same user; when creating a new
85ChangeLog entry, one element will be chosen at random."
fcad5199 86 :type '(choice (const :tag "Default" nil)
24f4201f
MR
87 (string :tag "String")
88 (repeat :tag "List of Strings" string))
fcad5199
RS
89 :group 'change-log)
90
df63ae66 91(defcustom add-log-time-format 'add-log-iso8601-time-string
d1921057 92 "Function that defines the time format.
df63ae66
RS
93For example, `add-log-iso8601-time-string', which gives the
94date in international ISO 8601 format,
95and `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)
02ec1592 102
83afd62c 103(defcustom add-log-keep-changes-together nil
d1921057 104 "If non-nil, normally keep day's log entries for one file together.
3697b807
DL
105
106Log 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 \
108for that file made
109today if this variable is non-nil or that file comes first in today's
110entries. Otherwise another entry for that file will be started. An
111original log:
112
113 * foo (...): ...
114 * bar (...): change 1
83afd62c 115
3697b807
DL
116in the latter case, \\[add-change-log-entry-other-window] in a \
117buffer visiting `bar', yields:
83afd62c 118
3697b807
DL
119 * bar (...): -!-
120 * foo (...): ...
121 * bar (...): change 1
83afd62c 122
3697b807 123and in the former:
83afd62c 124
3697b807
DL
125 * foo (...): ...
126 * bar (...): change 1
127 (...): -!-
83afd62c 128
3697b807
DL
129The NEW-ENTRY arg to `add-change-log-entry' can override the effect of
130this variable."
131 :version "20.3"
83afd62c
KH
132 :type 'boolean
133 :group 'change-log)
134
598f34fa 135(defcustom add-log-always-start-new-record nil
d1921057 136 "If non-nil, `add-change-log-entry' will always start a new record."
bf247b6e 137 :version "22.1"
598f34fa
SS
138 :type 'boolean
139 :group 'change-log)
140
d68f7f1b 141(defcustom add-log-buffer-file-name-function nil
d1921057 142 "If non-nil, function to call to identify the full filename of a buffer.
d68f7f1b
SM
143This function is called with no argument. If this is nil, the default is to
144use `buffer-file-name'."
cea3855a 145 :type '(choice (const nil) function)
d68f7f1b
SM
146 :group 'change-log)
147
666f4056 148(defcustom add-log-file-name-function nil
d1921057 149 "If non-nil, function to call to identify the filename for a ChangeLog entry.
075a6629
DL
150This 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
152use the file's name relative to the directory of the change log file."
cea3855a 153 :type '(choice (const nil) function)
666f4056
RS
154 :group 'change-log)
155
776d8e16
GM
156
157(defcustom change-log-version-info-enabled nil
bb042dc6 158 "If non-nil, enable recording version numbers with the changes."
776d8e16
GM
159 :version "21.1"
160 :type 'boolean
161 :group 'change-log)
162
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
5960adc7 169 (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re)))
bb042dc6 170 "List of regexps to search for version number.
5960adc7 171The version number must be in group 1.
776d8e16
GM
172Note: 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)
176
fe735a8d 177(defface change-log-date
163f7b71
GM
178 '((t (:inherit font-lock-string-face)))
179 "Face used to highlight dates in date lines."
180 :version "21.1"
181 :group 'change-log)
fe735a8d
MB
182;; backward-compatibility alias
183(put 'change-log-date-face 'face-alias 'change-log-date)
163f7b71 184
fe735a8d 185(defface change-log-name
163f7b71
GM
186 '((t (:inherit font-lock-constant-face)))
187 "Face for highlighting author names."
188 :version "21.1"
189 :group 'change-log)
fe735a8d
MB
190;; backward-compatibility alias
191(put 'change-log-name-face 'face-alias 'change-log-name)
163f7b71 192
fe735a8d 193(defface change-log-email
163f7b71
GM
194 '((t (:inherit font-lock-variable-name-face)))
195 "Face for highlighting author email addresses."
196 :version "21.1"
197 :group 'change-log)
fe735a8d
MB
198;; backward-compatibility alias
199(put 'change-log-email-face 'face-alias 'change-log-email)
163f7b71 200
fe735a8d 201(defface change-log-file
163f7b71
GM
202 '((t (:inherit font-lock-function-name-face)))
203 "Face for highlighting file names."
204 :version "21.1"
205 :group 'change-log)
fe735a8d
MB
206;; backward-compatibility alias
207(put 'change-log-file-face 'face-alias 'change-log-file)
163f7b71 208
fe735a8d 209(defface change-log-list
163f7b71
GM
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)
fe735a8d
MB
214;; backward-compatibility alias
215(put 'change-log-list-face 'face-alias 'change-log-list)
598f34fa 216
fe735a8d 217(defface change-log-conditionals
163f7b71
GM
218 '((t (:inherit font-lock-variable-name-face)))
219 "Face for highlighting conditionals of the form `[...]'."
220 :version "21.1"
221 :group 'change-log)
fe735a8d
MB
222;; backward-compatibility alias
223(put 'change-log-conditionals-face 'face-alias 'change-log-conditionals)
163f7b71 224
fe735a8d 225(defface change-log-function
163f7b71
GM
226 '((t (:inherit font-lock-variable-name-face)))
227 "Face for highlighting items of the form `<....>'."
228 :version "21.1"
229 :group 'change-log)
fe735a8d
MB
230;; backward-compatibility alias
231(put 'change-log-function-face 'face-alias 'change-log-function)
163f7b71 232
fe735a8d 233(defface change-log-acknowledgement
163f7b71
GM
234 '((t (:inherit font-lock-comment-face)))
235 "Face for highlighting acknowledgments."
236 :version "21.1"
237 :group 'change-log)
fe735a8d
MB
238;; backward-compatibility alias
239(put 'change-log-acknowledgement-face 'face-alias 'change-log-acknowledgement)
776d8e16 240
a28ed9e5 241(defconst change-log-file-names-re "^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)")
000605b3 242(defconst change-log-start-entry-re "^\\sw.........[0-9:+ ]*")
a28ed9e5 243
5f562719 244(defvar change-log-font-lock-keywords
a28ed9e5 245 `(;;
080525ae
KH
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)
97a3278b 253 ;; Name and e-mail; some people put e-mail in parens, not angles.
7397a79f 254 ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil
fe735a8d
MB
255 (1 'change-log-name)
256 (2 'change-log-email)))
5572c1d1
SM
257 ;;
258 ;; File names.
a28ed9e5 259 (,change-log-file-names-re
fe735a8d 260 (2 'change-log-file)
97a3278b 261 ;; Possibly further names in a list:
fe735a8d 262 ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file))
97a3278b 263 ;; Possibly a parenthesized list of names:
171c707b 264 ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
fe735a8d 265 nil nil (1 'change-log-list))
171c707b 266 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
fe735a8d 267 nil nil (1 'change-log-list)))
5572c1d1
SM
268 ;;
269 ;; Function or variable names.
9e4b54a0 270 ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)"
fe735a8d 271 (2 'change-log-list)
171c707b 272 ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil
fe735a8d 273 (1 'change-log-list)))
5572c1d1
SM
274 ;;
275 ;; Conditionals.
fe735a8d 276 ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals))
5572c1d1 277 ;;
a8d693d8 278 ;; Function of change.
fe735a8d 279 ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function))
a8d693d8 280 ;;
a3cab9f0 281 ;; Acknowledgements.
a8e10524
RS
282 ;; Don't include plain "From" because that is vague;
283 ;; we want to encourage people to say something more specific.
c50089c9
RS
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.
9e4b54a0 287 ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)"
fe735a8d 288 3 'change-log-acknowledgement))
5f562719
RS
289 "Additional expressions to highlight in Change Log mode.")
290
a28ed9e5
DN
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)
000605b3
DN
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 2))
302 (if (looking-at change-log-file-names-re)
303 ;; We found a file name.
304 (match-string 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 2))
316 (match-string 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 2))))))
a28ed9e5
DN
320
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)
000605b3 327 (message "No such file or directory: %s" file))))
a28ed9e5 328
3066d4ad
SM
329(defvar change-log-mode-map
330 (let ((map (make-sparse-keymap)))
331 (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment)
332 (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment)
a28ed9e5 333 (define-key map [?\C-c ?\C-f] 'change-log-find-file)
3066d4ad 334 map)
45c50c5d 335 "Keymap for Change Log major mode.")
45c50c5d 336
d1921057
SM
337;; It used to be called change-log-time-zone-rule but really should be
338;; called add-log-time-zone-rule since it's only used from add-log-* code.
339(defvaralias 'change-log-time-zone-rule 'add-log-time-zone-rule)
340(defvar add-log-time-zone-rule nil
51bd1843
EN
341 "Time zone used for calculating change log time stamps.
342It takes the same format as the TZ argument of `set-time-zone-rule'.
d1921057
SM
343If nil, use local time.
344If t, use universal time.")
d52c204b
RS
345(put 'add-log-time-zone-rule 'safe-local-variable
346 '(lambda (x) (or (booleanp x) (stringp x))))
51bd1843 347
0739a962 348(defun add-log-iso8601-time-zone (&optional time)
51bd1843
EN
349 (let* ((utc-offset (or (car (current-time-zone time)) 0))
350 (sign (if (< utc-offset 0) ?- ?+))
351 (sec (abs utc-offset))
352 (ss (% sec 60))
353 (min (/ sec 60))
354 (mm (% min 60))
355 (hh (/ min 60)))
356 (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d")
357 ((not (zerop mm)) "%c%02d:%02d")
358 (t "%c%02d"))
359 sign hh mm ss)))
360
d1921057
SM
361(defvar add-log-iso8601-with-time-zone nil)
362
df63ae66 363(defun add-log-iso8601-time-string ()
0739a962
SM
364 (let ((time (format-time-string "%Y-%m-%d"
365 nil (eq t add-log-time-zone-rule))))
d1921057
SM
366 (if add-log-iso8601-with-time-zone
367 (concat time " " (add-log-iso8601-time-zone))
368 time)))
df63ae66 369
84fc2cfa 370(defun change-log-name ()
075a6629 371 "Return (system-dependent) default name for a change log file."
84fc2cfa 372 (or change-log-default-name
513063cf 373 (if (eq system-type 'vax-vms)
4a047d23
RS
374 "$CHANGE_LOG$.TXT"
375 "ChangeLog")))
84fc2cfa 376
3066d4ad
SM
377(defun add-log-edit-prev-comment (arg)
378 "Cycle backward through Log-Edit mode comment history.
379With a numeric prefix ARG, go back ARG comments."
380 (interactive "*p")
381 (save-restriction
382 (narrow-to-region (point)
383 (if (memq last-command '(add-log-edit-prev-comment
384 add-log-edit-next-comment))
385 (mark) (point)))
386 (when (fboundp 'log-edit-previous-comment)
387 (log-edit-previous-comment arg)
388 (indent-region (point-min) (point-max))
389 (goto-char (point-min))
390 (unless (save-restriction (widen) (bolp))
391 (delete-region (point) (progn (skip-chars-forward " \t\n") (point))))
392 (set-mark (point-min))
393 (goto-char (point-max))
394 (delete-region (point) (progn (skip-chars-backward " \t\n") (point))))))
395
396(defun add-log-edit-next-comment (arg)
397 "Cycle forward through Log-Edit mode comment history.
398With a numeric prefix ARG, go back ARG comments."
399 (interactive "*p")
400 (add-log-edit-prev-comment (- arg)))
401
287d149f 402;;;###autoload
84fc2cfa
ER
403(defun prompt-for-change-log-name ()
404 "Prompt for a change log name."
117aaf60
KH
405 (let* ((default (change-log-name))
406 (name (expand-file-name
407 (read-file-name (format "Log file (default %s): " default)
408 nil default))))
409 ;; Handle something that is syntactically a directory name.
410 ;; Look for ChangeLog or whatever in that directory.
411 (if (string= (file-name-nondirectory name) "")
412 (expand-file-name (file-name-nondirectory default)
413 name)
414 ;; Handle specifying a file that is a directory.
415 (if (file-directory-p name)
416 (expand-file-name (file-name-nondirectory default)
417 (file-name-as-directory name))
418 name))))
84fc2cfa 419
776d8e16 420(defun change-log-version-number-search ()
5960adc7 421 "Return version number of current buffer's file.
ac3f4c6f 422This is the value returned by `vc-working-revision' or, if that is
5960adc7 423nil, by matching `change-log-version-number-regexp-list'."
776d8e16 424 (let* ((size (buffer-size))
fc9b0554 425 (limit
5960adc7
DL
426 ;; The version number can be anywhere in the file, but
427 ;; restrict search to the file beginning: 10% should be
428 ;; enough to prevent some mishits.
776d8e16 429 ;;
5960adc7
DL
430 ;; Apply percentage only if buffer size is bigger than
431 ;; approx 100 lines.
fc9b0554 432 (if (> size (* 100 80)) (+ (point) (/ size 10)))))
ac3f4c6f 433 (or (and buffer-file-name (vc-working-revision buffer-file-name))
5960adc7
DL
434 (save-restriction
435 (widen)
fc9b0554
SM
436 (let ((regexps change-log-version-number-regexp-list)
437 version)
5960adc7
DL
438 (while regexps
439 (save-excursion
440 (goto-char (point-min))
fc9b0554 441 (when (re-search-forward (pop regexps) limit t)
5960adc7 442 (setq version (match-string 1)
fc9b0554
SM
443 regexps nil))))
444 version)))))
776d8e16
GM
445
446
45a13f0d 447;;;###autoload
d68f7f1b 448(defun find-change-log (&optional file-name buffer-file)
45a13f0d 449 "Find a change log file for \\[add-change-log-entry] and return the name.
a82e2ed5
RS
450
451Optional arg FILE-NAME specifies the file to use.
de98fcaf 452If FILE-NAME is nil, use the value of `change-log-default-name'.
218cf475 453If `change-log-default-name' is nil, behave as though it were 'ChangeLog'
de98fcaf
RS
454\(or whatever we use on this operating system).
455
218cf475 456If `change-log-default-name' contains a leading directory component, then
513063cf 457simply find it in the current directory. Otherwise, search in the current
de98fcaf 458directory and its successive parents for a file so named.
45a13f0d
RM
459
460Once a file is found, `change-log-default-name' is set locally in the
d68f7f1b
SM
461current buffer to the complete file name.
462Optional arg BUFFER-FILE overrides `buffer-file-name'."
a82e2ed5
RS
463 ;; If user specified a file name or if this buffer knows which one to use,
464 ;; just use that.
45a13f0d 465 (or file-name
de98fcaf
RS
466 (setq file-name (and change-log-default-name
467 (file-name-directory change-log-default-name)
468 change-log-default-name))
a82e2ed5
RS
469 (progn
470 ;; Chase links in the source file
471 ;; and use the change log in the dir where it points.
d68f7f1b 472 (setq file-name (or (and (or buffer-file buffer-file-name)
a82e2ed5 473 (file-name-directory
d68f7f1b
SM
474 (file-chase-links
475 (or buffer-file buffer-file-name))))
a82e2ed5
RS
476 default-directory))
477 (if (file-directory-p file-name)
478 (setq file-name (expand-file-name (change-log-name) file-name)))
479 ;; Chase links before visiting the file.
480 ;; This makes it easier to use a single change log file
481 ;; for several related directories.
482 (setq file-name (file-chase-links file-name))
483 (setq file-name (expand-file-name file-name))
484 ;; Move up in the dir hierarchy till we find a change log file.
485 (let ((file1 file-name)
486 parent-dir)
487 (while (and (not (or (get-file-buffer file1) (file-exists-p file1)))
488 (progn (setq parent-dir
489 (file-name-directory
490 (directory-file-name
491 (file-name-directory file1))))
492 ;; Give up if we are already at the root dir.
493 (not (string= (file-name-directory file1)
494 parent-dir))))
495 ;; Move up to the parent dir and try again.
513063cf 496 (setq file1 (expand-file-name
a82e2ed5
RS
497 (file-name-nondirectory (change-log-name))
498 parent-dir)))
499 ;; If we found a change log in a parent, use that.
500 (if (or (get-file-buffer file1) (file-exists-p file1))
501 (setq file-name file1)))))
502 ;; Make a local variable in this buffer so we needn't search again.
503 (set (make-local-variable 'change-log-default-name) file-name)
504 file-name)
45a13f0d 505
2eb7ccf4
SM
506(defun add-log-file-name (buffer-file log-file)
507 ;; Never want to add a change log entry for the ChangeLog file itself.
508 (unless (or (null buffer-file) (string= buffer-file log-file))
d68f7f1b
SM
509 (if add-log-file-name-function
510 (funcall add-log-file-name-function buffer-file)
511 (setq buffer-file
e1f5b0ed 512 (file-relative-name buffer-file (file-name-directory log-file)))
d68f7f1b
SM
513 ;; If we have a backup file, it's presumably because we're
514 ;; comparing old and new versions (e.g. for deleted
515 ;; functions) and we'll want to use the original name.
516 (if (backup-file-name-p buffer-file)
517 (file-name-sans-versions buffer-file)
518 buffer-file))))
2eb7ccf4 519
84fc2cfa 520;;;###autoload
287d149f 521(defun add-change-log-entry (&optional whoami file-name other-window new-entry)
d882f144 522 "Find change log file, and add an entry for today and an item for this file.
83afd62c 523Optional arg WHOAMI (interactive prefix) non-nil means prompt for user
d9ee5172 524name and email (stored in `add-log-full-name' and `add-log-mailing-address').
83afd62c 525
d882f144
RS
526Second arg FILE-NAME is file name of the change log.
527If nil, use the value of `change-log-default-name'.
528
287d149f 529Third arg OTHER-WINDOW non-nil means visit in other window.
d882f144 530
287d149f 531Fourth arg NEW-ENTRY non-nil means always create a new entry at the front;
3697b807
DL
532never append to an existing entry. Option `add-log-keep-changes-together'
533otherwise affects whether a new entry is created.
534
598f34fa
SS
535Option `add-log-always-start-new-record' non-nil means always create a
536new record, even when the last record was made on the same date and by
537the same person.
538
d882f144
RS
539The change log file can start with a copyright notice and a copying
540permission notice. The first blank line indicates the end of these
541notices.
542
d1921057 543Today's date is calculated according to `add-log-time-zone-rule' if
3697b807 544non-nil, otherwise in local time."
84fc2cfa
ER
545 (interactive (list current-prefix-arg
546 (prompt-for-change-log-name)))
d68f7f1b
SM
547 (let* ((defun (add-log-current-defun))
548 (version (and change-log-version-info-enabled
549 (change-log-version-number-search)))
8f530b95
SM
550 (buf-file-name (if add-log-buffer-file-name-function
551 (funcall add-log-buffer-file-name-function)
552 buffer-file-name))
553 (buffer-file (if buf-file-name (expand-file-name buf-file-name)))
dd816e0d 554 (file-name (expand-file-name (find-change-log file-name buffer-file)))
d882f144 555 ;; Set ITEM to the file name to use in the new item.
ad4573c7 556 (item (add-log-file-name buffer-file file-name)))
2eb7ccf4 557
7cd017ba
SM
558 (unless (equal file-name buffer-file-name)
559 (if (or other-window (window-dedicated-p (selected-window)))
560 (find-file-other-window file-name)
561 (find-file file-name)))
bb042dc6 562 (or (derived-mode-p 'change-log-mode)
675a998f 563 (change-log-mode))
84fc2cfa
ER
564 (undo-boundary)
565 (goto-char (point-min))
d882f144 566
ad4573c7
SM
567 (let ((full-name (or add-log-full-name (user-full-name)))
568 (mailing-address (or add-log-mailing-address user-mail-address)))
569
570 (when whoami
1e899515 571 (setq full-name (read-string "Full name: " full-name))
ad4573c7
SM
572 ;; Note that some sites have room and phone number fields in
573 ;; full name which look silly when inserted. Rather than do
574 ;; anything about that here, let user give prefix argument so that
575 ;; s/he can edit the full name field in prompter if s/he wants.
576 (setq mailing-address
1e899515 577 (read-string "Mailing address: " mailing-address)))
ad4573c7
SM
578
579 ;; If file starts with a copyright and permission notice, skip them.
580 ;; Assume they end at first blank line.
581 (when (looking-at "Copyright")
582 (search-forward "\n\n")
583 (skip-chars-forward "\n"))
584
585 ;; Advance into first entry if it is usable; else make new one.
586 (let ((new-entries
587 (mapcar (lambda (addr)
588 (concat
589 (if (stringp add-log-time-zone-rule)
590 (let ((tz (getenv "TZ")))
591 (unwind-protect
592 (progn
593 (set-time-zone-rule add-log-time-zone-rule)
594 (funcall add-log-time-format))
595 (set-time-zone-rule tz)))
596 (funcall add-log-time-format))
597 " " full-name
598 " <" addr ">"))
599 (if (consp mailing-address)
600 mailing-address
601 (list mailing-address)))))
602 (if (and (not add-log-always-start-new-record)
603 (let ((hit nil))
604 (dolist (entry new-entries hit)
605 (when (looking-at (regexp-quote entry))
606 (setq hit t)))))
607 (forward-line 1)
608 (insert (nth (random (length new-entries))
609 new-entries)
610 (if use-hard-newlines hard-newline "\n")
611 (if use-hard-newlines hard-newline "\n"))
612 (forward-line -1))))
82f4acaf 613
d882f144
RS
614 ;; Determine where we should stop searching for a usable
615 ;; item to add to, within this entry.
ad4573c7
SM
616 (let ((bound
617 (save-excursion
618 (if (looking-at "\n*[^\n* \t]")
619 (skip-chars-forward "\n")
620 (if add-log-keep-changes-together
621 (forward-page) ; page delimits entries for date
622 (forward-paragraph))) ; paragraph delimits entries for file
623 (point))))
624
625 ;; Now insert the new line for this item.
626 (cond ((re-search-forward "^\\s *\\*\\s *$" bound t)
627 ;; Put this file name into the existing empty item.
628 (if item
629 (insert item)))
630 ((and (not new-entry)
631 (let (case-fold-search)
632 (re-search-forward
633 (concat (regexp-quote (concat "* " item))
634 ;; Don't accept `foo.bar' when
635 ;; looking for `foo':
636 "\\(\\s \\|[(),:]\\)")
637 bound t)))
638 ;; Add to the existing item for the same file.
639 (re-search-forward "^\\s *$\\|^\\s \\*")
640 (goto-char (match-beginning 0))
641 ;; Delete excess empty lines; make just 2.
642 (while (and (not (eobp)) (looking-at "^\\s *$"))
643 (delete-region (point) (line-beginning-position 2)))
644 (insert (if use-hard-newlines hard-newline "\n")
645 (if use-hard-newlines hard-newline "\n"))
646 (forward-line -2)
647 (indent-relative-maybe))
648 (t
649 ;; Make a new item.
650 (while (looking-at "\\sW")
651 (forward-line 1))
652 (while (and (not (eobp)) (looking-at "^\\s *$"))
653 (delete-region (point) (line-beginning-position 2)))
654 (insert (if use-hard-newlines hard-newline "\n")
655 (if use-hard-newlines hard-newline "\n")
656 (if use-hard-newlines hard-newline "\n"))
657 (forward-line -2)
658 (indent-to left-margin)
659 (insert "* ")
660 (if item (insert item)))))
1832dbd1 661 ;; Now insert the function name, if we have one.
d882f144 662 ;; Point is at the item for this file,
21d7e080 663 ;; either at the end of the line or at the first blank line.
5a9ac14b
SM
664 (if (not defun)
665 ;; No function name, so put in a colon unless we have just a star.
666 (unless (save-excursion
667 (beginning-of-line 1)
668 (looking-at "\\s *\\(\\*\\s *\\)?$"))
669 (insert ": ")
7b297602 670 (if version (insert version ?\s)))
5a9ac14b
SM
671 ;; Make it easy to get rid of the function name.
672 (undo-boundary)
5960adc7
DL
673 (unless (save-excursion
674 (beginning-of-line 1)
5a9ac14b 675 (looking-at "\\s *$"))
7b297602 676 (insert ?\s))
5a9ac14b
SM
677 ;; See if the prev function name has a message yet or not.
678 ;; If not, merge the two items.
679 (let ((pos (point-marker)))
680 (skip-syntax-backward " ")
681 (skip-chars-backward "):")
682 (if (and (looking-at "):")
fc9b0554
SM
683 (let ((pos (save-excursion (backward-sexp 1) (point))))
684 (when (equal (buffer-substring pos (point)) defun)
685 (delete-region pos (point)))
686 (> fill-column (+ (current-column) (length defun) 4))))
687 (progn (skip-chars-backward ", ")
688 (delete-region (point) pos)
689 (unless (memq (char-before) '(?\()) (insert ", ")))
5a9ac14b
SM
690 (if (looking-at "):")
691 (delete-region (+ 1 (point)) (line-end-position)))
692 (goto-char pos)
693 (insert "("))
694 (set-marker pos nil))
695 (insert defun "): ")
7b297602 696 (if version (insert version ?\s)))))
84fc2cfa 697
84fc2cfa
ER
698;;;###autoload
699(defun add-change-log-entry-other-window (&optional whoami file-name)
d882f144
RS
700 "Find change log file in other window and add entry and item.
701This is just like `add-change-log-entry' except that it displays
702the change log file in another window."
84fc2cfa
ER
703 (interactive (if current-prefix-arg
704 (list current-prefix-arg
705 (prompt-for-change-log-name))))
706 (add-change-log-entry whoami file-name t))
707
6dbf6147 708
d1921057 709(defvar change-log-indent-text 0)
fc9b0554 710
6dbf6147
MR
711(defun change-log-fill-parenthesized-list ()
712 ;; Fill parenthesized lists of names according to GNU standards.
713 ;; * file-name.ext (very-long-foo, very-long-bar, very-long-foobar):
714 ;; should be filled as
715 ;; * file-name.ext (very-long-foo, very-long-bar)
716 ;; (very-long-foobar):
717 (save-excursion
718 (end-of-line 0)
719 (skip-chars-backward " \t")
720 (when (and (equal (char-before) ?\,)
721 (> (point) (1+ (point-min))))
722 (condition-case nil
723 (when (save-excursion
724 (and (prog2
725 (up-list -1)
726 (equal (char-after) ?\()
727 (skip-chars-backward " \t"))
728 (or (bolp)
729 ;; Skip everything but a whitespace or asterisk.
730 (and (not (zerop (skip-chars-backward "^ \t\n*")))
731 (skip-chars-backward " \t")
732 ;; We want one asterisk here.
733 (= (skip-chars-backward "*") -1)
734 (skip-chars-backward " \t")
735 (bolp)))))
736 ;; Delete the comma.
737 (delete-char -1)
738 ;; Close list on previous line.
739 (insert ")")
740 (skip-chars-forward " \t\n")
741 ;; Start list on new line.
742 (insert-before-markers "("))
743 (error nil)))))
744
d1921057 745(defun change-log-indent ()
6dbf6147 746 (change-log-fill-parenthesized-list)
fc9b0554
SM
747 (let* ((indent
748 (save-excursion
749 (beginning-of-line)
750 (skip-chars-forward " \t")
751 (cond
d1921057 752 ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>\\(?: +(.*)\\)? *$")
fc9b0554
SM
753 ;; Matching the output of add-log-time-format is difficult,
754 ;; but I'll get it has at least two adjacent digits.
755 (string-match "[[:digit:]][[:digit:]]" (match-string 1)))
756 0)
757 ((looking-at "[^*(]")
d1921057 758 (+ (current-left-margin) change-log-indent-text))
fc9b0554
SM
759 (t (current-left-margin)))))
760 (pos (save-excursion (indent-line-to indent) (point))))
761 (if (> pos (point)) (goto-char pos))))
762
763
3066d4ad 764(defvar smerge-resolve-function)
8bb4ed88 765(defvar copyright-at-end-flag)
3066d4ad 766
1da56800 767;;;###autoload
7cd017ba 768(define-derived-mode change-log-mode text-mode "Change Log"
eb8c3be9 769 "Major mode for editing change logs; like Indented Text Mode.
09b389d0 770Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74.
3697b807 771New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window].
5516387c 772Each entry behaves as a paragraph, and the entries for one day as a page.
3066d4ad
SM
773Runs `change-log-mode-hook'.
774\\{change-log-mode-map}"
7cd017ba 775 (setq left-margin 8
4f675a8c 776 fill-column 74
60f10a06 777 indent-tabs-mode t
ccf0d2ca
JB
778 tab-width 8
779 show-trailing-whitespace t)
60f10a06
KH
780 (set (make-local-variable 'fill-paragraph-function)
781 'change-log-fill-paragraph)
3ee9a09c
MR
782 ;; Avoid that filling leaves behind a single "*" on a line.
783 (add-hook 'fill-nobreak-predicate
784 '(lambda ()
8bb4ed88 785 (looking-back "^\\s *\\*\\s *" (line-beginning-position)))
3ee9a09c 786 nil t)
d1921057 787 (set (make-local-variable 'indent-line-function) 'change-log-indent)
fc9b0554 788 (set (make-local-variable 'tab-always-indent) nil)
8bb4ed88 789 (set (make-local-variable 'copyright-at-end-flag) t)
4b7d4d0d
DL
790 ;; We really do want "^" in paragraph-start below: it is only the
791 ;; lines that begin at column 0 (despite the left-margin of 8) that
792 ;; we are looking for. Adding `* ' allows eliding the blank line
793 ;; between entries for different files.
c9cfb0f2 794 (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<")
4b7d4d0d 795 (set (make-local-variable 'paragraph-separate) paragraph-start)
2c91c85c
RS
796 ;; Match null string on the date-line so that the date-line
797 ;; is grouped with what follows.
964141f2 798 (set (make-local-variable 'page-delimiter) "^\\<\\|^\f")
dd309224 799 (set (make-local-variable 'version-control) 'never)
7cd017ba
SM
800 (set (make-local-variable 'smerge-resolve-function)
801 'change-log-resolve-conflict)
dd309224 802 (set (make-local-variable 'adaptive-fill-regexp) "\\s *")
4b286eca 803 (set (make-local-variable 'font-lock-defaults)
73b27641
JL
804 '(change-log-font-lock-keywords t nil nil backward-paragraph))
805 (set (make-local-variable 'isearch-buffers-next-buffer-function)
806 'change-log-next-buffer)
0bde6a03
DN
807 (set (make-local-variable 'beginning-of-defun-function)
808 'change-log-beginning-of-defun)
809 (set (make-local-variable 'end-of-defun-function)
810 'change-log-end-of-defun)
73b27641
JL
811 (isearch-buffers-minor-mode))
812
813(defun change-log-next-buffer (&optional buffer wrap)
814 "Return the next buffer in the series of ChangeLog file buffers.
815This function is used for multiple buffers isearch.
816A sequence of buffers is formed by ChangeLog files with decreasing
817numeric file name suffixes in the directory of the initial ChangeLog
818file were isearch was started."
819 (let* ((name (change-log-name))
820 (files (cons name (sort (file-expand-wildcards
821 (concat name "[-.][0-9]*"))
822 (lambda (a b)
bac2f6bc
SM
823 ;; The file's extension may not have a valid
824 ;; version form (e.g. VC backup revisions).
825 (ignore-errors
826 (version< (substring b (length name))
827 (substring a (length name))))))))
73b27641
JL
828 (files (if isearch-forward files (reverse files))))
829 (find-file-noselect
830 (if wrap
831 (car files)
832 (cadr (member (file-name-nondirectory (buffer-file-name buffer))
833 files))))))
21d7e080 834
287d149f
RM
835;; It might be nice to have a general feature to replace this. The idea I
836;; have is a variable giving a regexp matching text which should not be
837;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(".
838;; But I don't feel up to implementing that today.
839(defun change-log-fill-paragraph (&optional justify)
840 "Fill the paragraph, but preserve open parentheses at beginning of lines.
841Prefix arg means justify as well."
842 (interactive "P")
8d6467a4
RS
843 (let ((end (progn (forward-paragraph) (point)))
844 (beg (progn (backward-paragraph) (point)))
6dbf6147
MR
845 ;; Add lines starting with whitespace followed by a left paren or an
846 ;; asterisk.
847 (paragraph-start (concat paragraph-start "\\|\\s *\\(?:\\s(\\|\\*\\)"))
848 ;; Make sure we call `change-log-indent'.
849 (fill-indent-according-to-mode t))
8d6467a4
RS
850 (fill-region beg end justify)
851 t))
287d149f 852\f
fcad5199 853(defcustom add-log-current-defun-header-regexp
a8d693d8 854 "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]"
d1921057 855 "Heuristic regexp used by `add-log-current-defun' for unknown major modes."
fcad5199
RS
856 :type 'regexp
857 :group 'change-log)
21d7e080 858
fb644f48
EN
859;;;###autoload
860(defvar add-log-lisp-like-modes
d1921057 861 '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode)
fb644f48
EN
862 "*Modes that look like Lisp to `add-log-current-defun'.")
863
864;;;###autoload
865(defvar add-log-c-like-modes
d1921057 866 '(c-mode c++-mode c++-c-mode objc-mode)
fb644f48
EN
867 "*Modes that look like C to `add-log-current-defun'.")
868
869;;;###autoload
870(defvar add-log-tex-like-modes
bb042dc6 871 '(TeX-mode plain-TeX-mode LaTeX-mode tex-mode)
fb644f48
EN
872 "*Modes that look like TeX to `add-log-current-defun'.")
873
ab319633
GM
874(declare-function c-cpp-define-name "cc-cmds" ())
875(declare-function c-defun-name "cc-cmds" ())
004a00f4 876
e332f80b 877;;;###autoload
21d7e080
ER
878(defun add-log-current-defun ()
879 "Return name of function definition point is in, or nil.
880
63314951 881Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...),
5960adc7 882Texinfo (@node titles) and Perl.
21d7e080
ER
883
884Other modes are handled by a heuristic that looks in the 10K before
885point for uppercase headings starting in the first column or
5960adc7 886identifiers followed by `:' or `='. See variables
c1356086 887`add-log-current-defun-header-regexp' and
5ef08021 888`add-log-current-defun-function'.
21d7e080
ER
889
890Has a preference of looking backwards."
2cc0b765
RS
891 (condition-case nil
892 (save-excursion
893 (let ((location (point)))
5960adc7
DL
894 (cond (add-log-current-defun-function
895 (funcall add-log-current-defun-function))
bb042dc6 896 ((apply 'derived-mode-p add-log-lisp-like-modes)
a0151877 897 ;; If we are now precisely at the beginning of a defun,
2cc0b765
RS
898 ;; make sure beginning-of-defun finds that one
899 ;; rather than the previous one.
900 (or (eobp) (forward-char 1))
901 (beginning-of-defun)
5960adc7
DL
902 ;; Make sure we are really inside the defun found,
903 ;; not after it.
42b1fc29
RS
904 (when (and (looking-at "\\s(")
905 (progn (end-of-defun)
906 (< location (point)))
907 (progn (forward-sexp -1)
908 (>= location (point))))
909 (if (looking-at "\\s(")
910 (forward-char 1))
911 ;; Skip the defining construct name, typically "defun"
912 ;; or "defvar".
913 (forward-sexp 1)
914 ;; The second element is usually a symbol being defined.
915 ;; If it is not, use the first symbol in it.
63c7727f 916 (skip-chars-forward " \t\n'(")
5960adc7
DL
917 (buffer-substring-no-properties (point)
918 (progn (forward-sexp 1)
919 (point)))))
bb042dc6 920 ((apply 'derived-mode-p add-log-c-like-modes)
221fcdaa
AM
921 (or (c-cpp-define-name)
922 (c-defun-name)))
923 ((memq major-mode add-log-tex-like-modes)
2cc0b765 924 (if (re-search-backward
5960adc7
DL
925 "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)"
926 nil t)
2cc0b765
RS
927 (progn
928 (goto-char (match-beginning 0))
5960adc7
DL
929 (buffer-substring-no-properties
930 (1+ (point)) ; without initial backslash
931 (line-end-position)))))
bb042dc6 932 ((derived-mode-p 'texinfo-mode)
3071ee28 933 (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t)
5960adc7 934 (match-string-no-properties 1)))
fb1b68a4 935 ((derived-mode-p 'perl-mode 'cperl-mode)
5a9ac14b 936 (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t)
5960adc7
DL
937 (match-string-no-properties 1)))
938 ;; Emacs's autoconf-mode installs its own
939 ;; `add-log-current-defun-function'. This applies to
940 ;; a different mode apparently for editing .m4
941 ;; autoconf source.
bb042dc6 942 ((derived-mode-p 'autoconf-mode)
5960adc7
DL
943 (if (re-search-backward
944 "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t)
945 (match-string-no-properties 3)))
2cc0b765
RS
946 (t
947 ;; If all else fails, try heuristics
c1356086
GM
948 (let (case-fold-search
949 result)
2cc0b765 950 (end-of-line)
c1356086
GM
951 (when (re-search-backward
952 add-log-current-defun-header-regexp
953 (- (point) 10000)
954 t)
5960adc7
DL
955 (setq result (or (match-string-no-properties 1)
956 (match-string-no-properties 0)))
c1356086
GM
957 ;; Strip whitespace away
958 (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)"
959 result)
5960adc7 960 (setq result (match-string-no-properties 1 result)))
c1356086 961 result))))))
2cc0b765 962 (error nil)))
ef15f270 963
83afd62c 964(defvar change-log-get-method-definition-md)
15319a8f 965
83afd62c 966;; Subroutine used within change-log-get-method-definition.
59c1a7de
RS
967;; Add the last match in the buffer to the end of `md',
968;; followed by the string END; move to the end of that match.
83afd62c
KH
969(defun change-log-get-method-definition-1 (end)
970 (setq change-log-get-method-definition-md
971 (concat change-log-get-method-definition-md
5960adc7 972 (match-string 1)
15319a8f 973 end))
59c1a7de
RS
974 (goto-char (match-end 0)))
975
83afd62c 976(defun change-log-get-method-definition ()
eba72fc1 977"For Objective C, return the method name if we are in a method."
83afd62c 978 (let ((change-log-get-method-definition-md "["))
59c1a7de 979 (save-excursion
f27f16ed 980 (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t)
83afd62c 981 (change-log-get-method-definition-1 " ")))
59c1a7de
RS
982 (save-excursion
983 (cond
f27f16ed 984 ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t)
83afd62c 985 (change-log-get-method-definition-1 "")
59c1a7de
RS
986 (while (not (looking-at "[{;]"))
987 (looking-at
f27f16ed 988 "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*")
83afd62c
KH
989 (change-log-get-method-definition-1 ""))
990 (concat change-log-get-method-definition-md "]"))))))
075a6629
DL
991\f
992(defun change-log-sortable-date-at ()
993 "Return date of log entry in a consistent form for sorting.
994Point is assumed to be at the start of the entry."
995 (require 'timezone)
0bde6a03 996 (if (looking-at change-log-start-entry-re)
075a6629
DL
997 (let ((date (match-string-no-properties 0)))
998 (if date
999 (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date)
1000 (concat (match-string 1 date) (match-string 2 date)
1001 (match-string 3 date))
1002 (condition-case nil
1003 (timezone-make-date-sortable date)
1004 (error nil)))))
1005 (error "Bad date")))
59c1a7de 1006
7cd017ba
SM
1007(defun change-log-resolve-conflict ()
1008 "Function to be used in `smerge-resolve-function'."
74dea9e1
SM
1009 (save-excursion
1010 (save-restriction
1011 (narrow-to-region (match-beginning 0) (match-end 0))
1012 (let ((mb1 (match-beginning 1))
1013 (me1 (match-end 1))
1014 (mb3 (match-beginning 3))
1015 (me3 (match-end 3))
1016 (tmp1 (generate-new-buffer " *changelog-resolve-1*"))
1017 (tmp2 (generate-new-buffer " *changelog-resolve-2*")))
1018 (unwind-protect
1019 (let ((buf (current-buffer)))
1020 (with-current-buffer tmp1
1021 (change-log-mode)
1022 (insert-buffer-substring buf mb1 me1))
1023 (with-current-buffer tmp2
1024 (change-log-mode)
1025 (insert-buffer-substring buf mb3 me3)
1026 ;; Do the merge here instead of inside `buf' so as to be
1027 ;; more robust in case change-log-merge fails.
1028 (change-log-merge tmp1))
1029 (goto-char (point-max))
1030 (delete-region (point-min)
1031 (prog1 (point)
1032 (insert-buffer-substring tmp2))))
1033 (kill-buffer tmp1)
1034 (kill-buffer tmp2))))))
7cd017ba 1035
075a6629
DL
1036;;;###autoload
1037(defun change-log-merge (other-log)
eba72fc1 1038 "Merge the contents of change log file OTHER-LOG with this buffer.
075a6629 1039Both must be found in Change Log mode (since the merging depends on
7cd017ba
SM
1040the appropriate motion commands). OTHER-LOG can be either a file name
1041or a buffer.
075a6629 1042
918f4ac3
DL
1043Entries are inserted in chronological order. Both the current and
1044old-style time formats for entries are supported."
075a6629 1045 (interactive "*fLog file name to merge: ")
bb042dc6 1046 (if (not (derived-mode-p 'change-log-mode))
075a6629 1047 (error "Not in Change Log mode"))
7cd017ba
SM
1048 (let ((other-buf (if (bufferp other-log) other-log
1049 (find-file-noselect other-log)))
075a6629
DL
1050 (buf (current-buffer))
1051 date1 start end)
1052 (save-excursion
1053 (goto-char (point-min))
1054 (set-buffer other-buf)
1055 (goto-char (point-min))
bb042dc6 1056 (if (not (derived-mode-p 'change-log-mode))
075a6629
DL
1057 (error "%s not found in Change Log mode" other-log))
1058 ;; Loop through all the entries in OTHER-LOG.
1059 (while (not (eobp))
1060 (setq date1 (change-log-sortable-date-at))
1061 (setq start (point)
1062 end (progn (forward-page) (point)))
1063 ;; Look for an entry in original buffer that isn't later.
1064 (with-current-buffer buf
1065 (while (and (not (eobp))
1066 (string< date1 (change-log-sortable-date-at)))
1067 (forward-page))
1068 (if (not (eobp))
1069 (insert-buffer-substring other-buf start end)
1070 ;; At the end of the original buffer, insert a newline to
1071 ;; separate entries and then the rest of the file being
7cd017ba
SM
1072 ;; merged.
1073 (unless (or (bobp)
1074 (and (= ?\n (char-before))
1075 (or (<= (1- (point)) (point-min))
1076 (= ?\n (char-before (1- (point)))))))
97f4e87c 1077 (insert (if use-hard-newlines hard-newline "\n")))
7cd017ba
SM
1078 ;; Move to the end of it to terminate outer loop.
1079 (with-current-buffer other-buf
1080 (goto-char (point-max)))
1081 (insert-buffer-substring other-buf start)))))))
ef15f270 1082
0bde6a03
DN
1083(defun change-log-beginning-of-defun ()
1084 (re-search-backward change-log-start-entry-re nil 'move))
1085
1086(defun change-log-end-of-defun ()
1087 ;; Look back and if there is no entry there it means we are before
1088 ;; the first ChangeLog entry, so go forward until finding one.
1089 (unless (save-excursion (re-search-backward change-log-start-entry-re nil t))
1090 (re-search-forward change-log-start-entry-re nil t))
1091
1092 ;; In case we are at the end of log entry going forward a line will
1093 ;; make us find the next entry when searching. If we are inside of
1094 ;; an entry going forward a line will still keep the point inside
1095 ;; the same entry.
1096 (forward-line 1)
1097
1098 ;; In case we are at the beginning of an entry, move past it.
1099 (when (looking-at change-log-start-entry-re)
1100 (goto-char (match-end 0))
1101 (forward-line 1))
1102
1103 ;; Search for the start of the next log entry. Go to the end of the
1104 ;; buffer if we could not find a next entry.
1105 (when (re-search-forward change-log-start-entry-re nil 'move)
1106 (goto-char (match-beginning 0))
1107 (forward-line -1)))
1108
1da56800
RS
1109(provide 'add-log)
1110
d1921057 1111;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762
fd7fa35a 1112;;; add-log.el ends here