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