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