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