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