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