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