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