Commit | Line | Data |
---|---|---|
5abdc915 | 1 | ;;; add-log.el --- change log maintenance commands for Emacs |
84fc2cfa | 2 | |
0d30b337 TTN |
3 | ;; Copyright (C) 1985, 1986, 1988, 1993, 1994, 1997, 1998, 2000, 2002, |
4 | ;; 2003, 2004, 2005 Free Software Foundation, Inc. | |
84fc2cfa | 5 | |
6228c05b | 6 | ;; Maintainer: FSF |
df63ae66 | 7 | ;; Keywords: tools |
e9571d2a | 8 | |
84fc2cfa ER |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
fd7fa35a | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
84fc2cfa ER |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
24 | ;; Boston, MA 02110-1301, USA. | |
84fc2cfa | 25 | |
e41b2db1 ER |
26 | ;;; Commentary: |
27 | ||
28 | ;; This facility is documented in the Emacs Manual. | |
29 | ||
fd7fa35a | 30 | ;;; Code: |
84fc2cfa | 31 | |
776d8e16 | 32 | (eval-when-compile |
5960adc7 | 33 | (require 'timezone)) |
3697b807 | 34 | |
fcad5199 | 35 | (defgroup change-log nil |
7b297602 | 36 | "Change log maintenance." |
fcad5199 | 37 | :group 'tools |
3697b807 | 38 | :link '(custom-manual "(emacs)Change Log") |
fcad5199 RS |
39 | :prefix "change-log-" |
40 | :prefix "add-log-") | |
84fc2cfa | 41 | |
fcad5199 RS |
42 | |
43 | (defcustom change-log-default-name nil | |
44 | "*Name of a change log file for \\[add-change-log-entry]." | |
45 | :type '(choice (const :tag "default" nil) | |
46 | string) | |
47 | :group 'change-log) | |
48 | ||
c3979f12 DL |
49 | (defcustom change-log-mode-hook nil |
50 | "Normal hook run by `change-log-mode'." | |
51 | :type 'hook | |
52 | :group 'change-log) | |
53 | ||
c882e115 RS |
54 | ;; Many modes set this variable, so avoid warnings. |
55 | ;;;###autoload | |
fcad5199 | 56 | (defcustom add-log-current-defun-function nil |
5960adc7 DL |
57 | "*If non-nil, function to guess name of surrounding function. |
58 | It is used by `add-log-current-defun' in preference to built-in rules. | |
59 | Returns function's name as a string, or nil if outside a function." | |
cea3855a | 60 | :type '(choice (const nil) function) |
fcad5199 | 61 | :group 'change-log) |
6258d3af | 62 | |
29db528b | 63 | ;;;###autoload |
fcad5199 | 64 | (defcustom add-log-full-name nil |
02ec1592 | 65 | "*Full name of user, for inclusion in ChangeLog daily headers. |
075a6629 | 66 | This defaults to the value returned by the function `user-full-name'." |
fcad5199 RS |
67 | :type '(choice (const :tag "Default" nil) |
68 | string) | |
69 | :group 'change-log) | |
02ec1592 | 70 | |
29db528b | 71 | ;;;###autoload |
fcad5199 | 72 | (defcustom add-log-mailing-address nil |
eba72fc1 | 73 | "*Email addresses of user, for inclusion in ChangeLog headers. |
c1b3ae42 CW |
74 | This defaults to the value of `user-mail-address'. In addition to |
75 | being a simple string, this value can also be a list. All elements | |
76 | will be recognized as referring to the same user; when creating a new | |
77 | ChangeLog entry, one element will be chosen at random." | |
fcad5199 | 78 | :type '(choice (const :tag "Default" nil) |
24f4201f MR |
79 | (string :tag "String") |
80 | (repeat :tag "List of Strings" string)) | |
fcad5199 RS |
81 | :group 'change-log) |
82 | ||
df63ae66 RS |
83 | (defcustom add-log-time-format 'add-log-iso8601-time-string |
84 | "*Function that defines the time format. | |
85 | For example, `add-log-iso8601-time-string', which gives the | |
86 | date in international ISO 8601 format, | |
87 | and `current-time-string' are two valid values." | |
88 | :type '(radio (const :tag "International ISO 8601 format" | |
89 | add-log-iso8601-time-string) | |
90 | (const :tag "Old format, as returned by `current-time-string'" | |
91 | current-time-string) | |
92 | (function :tag "Other")) | |
93 | :group 'change-log) | |
02ec1592 | 94 | |
83afd62c | 95 | (defcustom add-log-keep-changes-together nil |
3697b807 DL |
96 | "*If non-nil, normally keep day's log entries for one file together. |
97 | ||
98 | Log entries for a given file made with \\[add-change-log-entry] or | |
99 | \\[add-change-log-entry-other-window] will only be added to others \ | |
100 | for that file made | |
101 | today if this variable is non-nil or that file comes first in today's | |
102 | entries. Otherwise another entry for that file will be started. An | |
103 | original log: | |
104 | ||
105 | * foo (...): ... | |
106 | * bar (...): change 1 | |
83afd62c | 107 | |
3697b807 DL |
108 | in the latter case, \\[add-change-log-entry-other-window] in a \ |
109 | buffer visiting `bar', yields: | |
83afd62c | 110 | |
3697b807 DL |
111 | * bar (...): -!- |
112 | * foo (...): ... | |
113 | * bar (...): change 1 | |
83afd62c | 114 | |
3697b807 | 115 | and in the former: |
83afd62c | 116 | |
3697b807 DL |
117 | * foo (...): ... |
118 | * bar (...): change 1 | |
119 | (...): -!- | |
83afd62c | 120 | |
3697b807 DL |
121 | The NEW-ENTRY arg to `add-change-log-entry' can override the effect of |
122 | this variable." | |
123 | :version "20.3" | |
83afd62c KH |
124 | :type 'boolean |
125 | :group 'change-log) | |
126 | ||
598f34fa SS |
127 | (defcustom add-log-always-start-new-record nil |
128 | "*If non-nil, `add-change-log-entry' will always start a new record." | |
bf247b6e | 129 | :version "22.1" |
598f34fa SS |
130 | :type 'boolean |
131 | :group 'change-log) | |
132 | ||
d68f7f1b SM |
133 | (defcustom add-log-buffer-file-name-function nil |
134 | "*If non-nil, function to call to identify the full filename of a buffer. | |
135 | This function is called with no argument. If this is nil, the default is to | |
136 | use `buffer-file-name'." | |
cea3855a | 137 | :type '(choice (const nil) function) |
d68f7f1b SM |
138 | :group 'change-log) |
139 | ||
666f4056 RS |
140 | (defcustom add-log-file-name-function nil |
141 | "*If non-nil, function to call to identify the filename for a ChangeLog entry. | |
075a6629 DL |
142 | This function is called with one argument, the value of variable |
143 | `buffer-file-name' in that buffer. If this is nil, the default is to | |
144 | use the file's name relative to the directory of the change log file." | |
cea3855a | 145 | :type '(choice (const nil) function) |
666f4056 RS |
146 | :group 'change-log) |
147 | ||
776d8e16 GM |
148 | |
149 | (defcustom change-log-version-info-enabled nil | |
150 | "*If non-nil, enable recording version numbers with the changes." | |
151 | :version "21.1" | |
152 | :type 'boolean | |
153 | :group 'change-log) | |
154 | ||
155 | (defcustom change-log-version-number-regexp-list | |
156 | (let ((re "\\([0-9]+\.[0-9.]+\\)")) | |
157 | (list | |
158 | ;; (defconst ad-version "2.15" | |
159 | (concat "^(def[^ \t\n]+[ \t]+[^ \t\n][ \t]\"" re) | |
160 | ;; Revision: pcl-cvs.el,v 1.72 1999/09/05 20:21:54 monnier Exp | |
5960adc7 | 161 | (concat "^;+ *Revision: +[^ \t\n]+[ \t]+" re))) |
776d8e16 | 162 | "*List of regexps to search for version number. |
5960adc7 | 163 | The version number must be in group 1. |
776d8e16 GM |
164 | Note: The search is conducted only within 10%, at the beginning of the file." |
165 | :version "21.1" | |
166 | :type '(repeat regexp) | |
167 | :group 'change-log) | |
168 | ||
fe735a8d | 169 | (defface change-log-date |
163f7b71 GM |
170 | '((t (:inherit font-lock-string-face))) |
171 | "Face used to highlight dates in date lines." | |
172 | :version "21.1" | |
173 | :group 'change-log) | |
fe735a8d MB |
174 | ;; backward-compatibility alias |
175 | (put 'change-log-date-face 'face-alias 'change-log-date) | |
163f7b71 | 176 | |
fe735a8d | 177 | (defface change-log-name |
163f7b71 GM |
178 | '((t (:inherit font-lock-constant-face))) |
179 | "Face for highlighting author names." | |
180 | :version "21.1" | |
181 | :group 'change-log) | |
fe735a8d MB |
182 | ;; backward-compatibility alias |
183 | (put 'change-log-name-face 'face-alias 'change-log-name) | |
163f7b71 | 184 | |
fe735a8d | 185 | (defface change-log-email |
163f7b71 GM |
186 | '((t (:inherit font-lock-variable-name-face))) |
187 | "Face for highlighting author email addresses." | |
188 | :version "21.1" | |
189 | :group 'change-log) | |
fe735a8d MB |
190 | ;; backward-compatibility alias |
191 | (put 'change-log-email-face 'face-alias 'change-log-email) | |
163f7b71 | 192 | |
fe735a8d | 193 | (defface change-log-file |
163f7b71 GM |
194 | '((t (:inherit font-lock-function-name-face))) |
195 | "Face for highlighting file names." | |
196 | :version "21.1" | |
197 | :group 'change-log) | |
fe735a8d MB |
198 | ;; backward-compatibility alias |
199 | (put 'change-log-file-face 'face-alias 'change-log-file) | |
163f7b71 | 200 | |
fe735a8d | 201 | (defface change-log-list |
163f7b71 GM |
202 | '((t (:inherit font-lock-keyword-face))) |
203 | "Face for highlighting parenthesized lists of functions or variables." | |
204 | :version "21.1" | |
205 | :group 'change-log) | |
fe735a8d MB |
206 | ;; backward-compatibility alias |
207 | (put 'change-log-list-face 'face-alias 'change-log-list) | |
598f34fa | 208 | |
fe735a8d | 209 | (defface change-log-conditionals |
163f7b71 GM |
210 | '((t (:inherit font-lock-variable-name-face))) |
211 | "Face for highlighting conditionals of the form `[...]'." | |
212 | :version "21.1" | |
213 | :group 'change-log) | |
fe735a8d MB |
214 | ;; backward-compatibility alias |
215 | (put 'change-log-conditionals-face 'face-alias 'change-log-conditionals) | |
163f7b71 | 216 | |
fe735a8d | 217 | (defface change-log-function |
163f7b71 GM |
218 | '((t (:inherit font-lock-variable-name-face))) |
219 | "Face for highlighting items of the form `<....>'." | |
220 | :version "21.1" | |
221 | :group 'change-log) | |
fe735a8d MB |
222 | ;; backward-compatibility alias |
223 | (put 'change-log-function-face 'face-alias 'change-log-function) | |
163f7b71 | 224 | |
fe735a8d | 225 | (defface change-log-acknowledgement |
163f7b71 GM |
226 | '((t (:inherit font-lock-comment-face))) |
227 | "Face for highlighting acknowledgments." | |
228 | :version "21.1" | |
229 | :group 'change-log) | |
fe735a8d MB |
230 | ;; backward-compatibility alias |
231 | (put 'change-log-acknowledgement-face 'face-alias 'change-log-acknowledgement) | |
776d8e16 | 232 | |
5f562719 | 233 | (defvar change-log-font-lock-keywords |
5572c1d1 | 234 | '(;; |
080525ae KH |
235 | ;; Date lines, new (2000-01-01) and old (Sat Jan 1 00:00:00 2000) styles. |
236 | ;; Fixme: this regepx is just an approximate one and may match | |
237 | ;; wrongly with a non-date line existing as a random note. In | |
238 | ;; addition, using any kind of fixed setting like this doesn't | |
239 | ;; work if a user customizes add-log-time-format. | |
240 | ("^[0-9-]+ +\\|^\\(Sun\\|Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\) [A-z][a-z][a-z] [0-9:+ ]+" | |
241 | (0 'change-log-date-face) | |
97a3278b | 242 | ;; Name and e-mail; some people put e-mail in parens, not angles. |
7397a79f | 243 | ("\\([^<(]+?\\)[ \t]*[(<]\\([A-Za-z0-9_.+-]+@[A-Za-z0-9_.-]+\\)[>)]" nil nil |
fe735a8d MB |
244 | (1 'change-log-name) |
245 | (2 'change-log-email))) | |
5572c1d1 SM |
246 | ;; |
247 | ;; File names. | |
9e4b54a0 | 248 | ("^\\( +\\|\t\\)\\* \\([^ ,:([\n]+\\)" |
fe735a8d | 249 | (2 'change-log-file) |
97a3278b | 250 | ;; Possibly further names in a list: |
fe735a8d | 251 | ("\\=, \\([^ ,:([\n]+\\)" nil nil (1 'change-log-file)) |
97a3278b | 252 | ;; Possibly a parenthesized list of names: |
171c707b | 253 | ("\\= (\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" |
fe735a8d | 254 | nil nil (1 'change-log-list)) |
171c707b | 255 | ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" |
fe735a8d | 256 | nil nil (1 'change-log-list))) |
5572c1d1 SM |
257 | ;; |
258 | ;; Function or variable names. | |
9e4b54a0 | 259 | ("^\\( +\\|\t\\)(\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" |
fe735a8d | 260 | (2 'change-log-list) |
171c707b | 261 | ("\\=, *\\([^(),\n]+\\|(\\(setf\\|SETF\\) [^() ,\n]+)\\)" nil nil |
fe735a8d | 262 | (1 'change-log-list))) |
5572c1d1 SM |
263 | ;; |
264 | ;; Conditionals. | |
fe735a8d | 265 | ("\\[!?\\([^]\n]+\\)\\]\\(:\\| (\\)" (1 'change-log-conditionals)) |
5572c1d1 | 266 | ;; |
a8d693d8 | 267 | ;; Function of change. |
fe735a8d | 268 | ("<\\([^>\n]+\\)>\\(:\\| (\\)" (1 'change-log-function)) |
a8d693d8 | 269 | ;; |
a3cab9f0 | 270 | ;; Acknowledgements. |
a8e10524 RS |
271 | ;; Don't include plain "From" because that is vague; |
272 | ;; we want to encourage people to say something more specific. | |
c50089c9 RS |
273 | ;; Note that the FSF does not use "Patches by"; our convention |
274 | ;; is to put the name of the author of the changes at the top | |
275 | ;; of the change log entry. | |
9e4b54a0 | 276 | ("\\(^\\( +\\|\t\\)\\| \\)\\(Patch\\(es\\)? by\\|Report\\(ed by\\| from\\)\\|Suggest\\(ed by\\|ion from\\)\\)" |
fe735a8d | 277 | 3 'change-log-acknowledgement)) |
5f562719 RS |
278 | "Additional expressions to highlight in Change Log mode.") |
279 | ||
3066d4ad SM |
280 | (defvar change-log-mode-map |
281 | (let ((map (make-sparse-keymap))) | |
282 | (define-key map [?\C-c ?\C-p] 'add-log-edit-prev-comment) | |
283 | (define-key map [?\C-c ?\C-n] 'add-log-edit-next-comment) | |
284 | map) | |
45c50c5d | 285 | "Keymap for Change Log major mode.") |
45c50c5d | 286 | |
51bd1843 EN |
287 | (defvar change-log-time-zone-rule nil |
288 | "Time zone used for calculating change log time stamps. | |
289 | It takes the same format as the TZ argument of `set-time-zone-rule'. | |
290 | If nil, use local time.") | |
291 | ||
df63ae66 | 292 | (defun add-log-iso8601-time-zone (time) |
51bd1843 EN |
293 | (let* ((utc-offset (or (car (current-time-zone time)) 0)) |
294 | (sign (if (< utc-offset 0) ?- ?+)) | |
295 | (sec (abs utc-offset)) | |
296 | (ss (% sec 60)) | |
297 | (min (/ sec 60)) | |
298 | (mm (% min 60)) | |
299 | (hh (/ min 60))) | |
300 | (format (cond ((not (zerop ss)) "%c%02d:%02d:%02d") | |
301 | ((not (zerop mm)) "%c%02d:%02d") | |
302 | (t "%c%02d")) | |
303 | sign hh mm ss))) | |
304 | ||
df63ae66 RS |
305 | (defun add-log-iso8601-time-string () |
306 | (if change-log-time-zone-rule | |
307 | (let ((tz (getenv "TZ")) | |
308 | (now (current-time))) | |
309 | (unwind-protect | |
310 | (progn | |
918f4ac3 | 311 | (set-time-zone-rule change-log-time-zone-rule) |
df63ae66 RS |
312 | (concat |
313 | (format-time-string "%Y-%m-%d " now) | |
314 | (add-log-iso8601-time-zone now))) | |
315 | (set-time-zone-rule tz))) | |
316 | (format-time-string "%Y-%m-%d"))) | |
317 | ||
84fc2cfa | 318 | (defun change-log-name () |
075a6629 | 319 | "Return (system-dependent) default name for a change log file." |
84fc2cfa | 320 | (or change-log-default-name |
513063cf | 321 | (if (eq system-type 'vax-vms) |
4a047d23 RS |
322 | "$CHANGE_LOG$.TXT" |
323 | "ChangeLog"))) | |
84fc2cfa | 324 | |
3066d4ad SM |
325 | (defun add-log-edit-prev-comment (arg) |
326 | "Cycle backward through Log-Edit mode comment history. | |
327 | With a numeric prefix ARG, go back ARG comments." | |
328 | (interactive "*p") | |
329 | (save-restriction | |
330 | (narrow-to-region (point) | |
331 | (if (memq last-command '(add-log-edit-prev-comment | |
332 | add-log-edit-next-comment)) | |
333 | (mark) (point))) | |
334 | (when (fboundp 'log-edit-previous-comment) | |
335 | (log-edit-previous-comment arg) | |
336 | (indent-region (point-min) (point-max)) | |
337 | (goto-char (point-min)) | |
338 | (unless (save-restriction (widen) (bolp)) | |
339 | (delete-region (point) (progn (skip-chars-forward " \t\n") (point)))) | |
340 | (set-mark (point-min)) | |
341 | (goto-char (point-max)) | |
342 | (delete-region (point) (progn (skip-chars-backward " \t\n") (point)))))) | |
343 | ||
344 | (defun add-log-edit-next-comment (arg) | |
345 | "Cycle forward through Log-Edit mode comment history. | |
346 | With a numeric prefix ARG, go back ARG comments." | |
347 | (interactive "*p") | |
348 | (add-log-edit-prev-comment (- arg))) | |
349 | ||
287d149f | 350 | ;;;###autoload |
84fc2cfa ER |
351 | (defun prompt-for-change-log-name () |
352 | "Prompt for a change log name." | |
117aaf60 KH |
353 | (let* ((default (change-log-name)) |
354 | (name (expand-file-name | |
355 | (read-file-name (format "Log file (default %s): " default) | |
356 | nil default)))) | |
357 | ;; Handle something that is syntactically a directory name. | |
358 | ;; Look for ChangeLog or whatever in that directory. | |
359 | (if (string= (file-name-nondirectory name) "") | |
360 | (expand-file-name (file-name-nondirectory default) | |
361 | name) | |
362 | ;; Handle specifying a file that is a directory. | |
363 | (if (file-directory-p name) | |
364 | (expand-file-name (file-name-nondirectory default) | |
365 | (file-name-as-directory name)) | |
366 | name)))) | |
84fc2cfa | 367 | |
776d8e16 | 368 | (defun change-log-version-number-search () |
5960adc7 DL |
369 | "Return version number of current buffer's file. |
370 | This is the value returned by `vc-workfile-version' or, if that is | |
371 | nil, by matching `change-log-version-number-regexp-list'." | |
776d8e16 | 372 | (let* ((size (buffer-size)) |
fc9b0554 | 373 | (limit |
5960adc7 DL |
374 | ;; The version number can be anywhere in the file, but |
375 | ;; restrict search to the file beginning: 10% should be | |
376 | ;; enough to prevent some mishits. | |
776d8e16 | 377 | ;; |
5960adc7 DL |
378 | ;; Apply percentage only if buffer size is bigger than |
379 | ;; approx 100 lines. | |
fc9b0554 | 380 | (if (> size (* 100 80)) (+ (point) (/ size 10))))) |
d68f7f1b | 381 | (or (and buffer-file-name (vc-workfile-version buffer-file-name)) |
5960adc7 DL |
382 | (save-restriction |
383 | (widen) | |
fc9b0554 SM |
384 | (let ((regexps change-log-version-number-regexp-list) |
385 | version) | |
5960adc7 DL |
386 | (while regexps |
387 | (save-excursion | |
388 | (goto-char (point-min)) | |
fc9b0554 | 389 | (when (re-search-forward (pop regexps) limit t) |
5960adc7 | 390 | (setq version (match-string 1) |
fc9b0554 SM |
391 | regexps nil)))) |
392 | version))))) | |
776d8e16 GM |
393 | |
394 | ||
45a13f0d | 395 | ;;;###autoload |
d68f7f1b | 396 | (defun find-change-log (&optional file-name buffer-file) |
45a13f0d | 397 | "Find a change log file for \\[add-change-log-entry] and return the name. |
a82e2ed5 RS |
398 | |
399 | Optional arg FILE-NAME specifies the file to use. | |
de98fcaf | 400 | If FILE-NAME is nil, use the value of `change-log-default-name'. |
218cf475 | 401 | If `change-log-default-name' is nil, behave as though it were 'ChangeLog' |
de98fcaf RS |
402 | \(or whatever we use on this operating system). |
403 | ||
218cf475 | 404 | If `change-log-default-name' contains a leading directory component, then |
513063cf | 405 | simply find it in the current directory. Otherwise, search in the current |
de98fcaf | 406 | directory and its successive parents for a file so named. |
45a13f0d RM |
407 | |
408 | Once a file is found, `change-log-default-name' is set locally in the | |
d68f7f1b SM |
409 | current buffer to the complete file name. |
410 | Optional arg BUFFER-FILE overrides `buffer-file-name'." | |
a82e2ed5 RS |
411 | ;; If user specified a file name or if this buffer knows which one to use, |
412 | ;; just use that. | |
45a13f0d | 413 | (or file-name |
de98fcaf RS |
414 | (setq file-name (and change-log-default-name |
415 | (file-name-directory change-log-default-name) | |
416 | change-log-default-name)) | |
a82e2ed5 RS |
417 | (progn |
418 | ;; Chase links in the source file | |
419 | ;; and use the change log in the dir where it points. | |
d68f7f1b | 420 | (setq file-name (or (and (or buffer-file buffer-file-name) |
a82e2ed5 | 421 | (file-name-directory |
d68f7f1b SM |
422 | (file-chase-links |
423 | (or buffer-file buffer-file-name)))) | |
a82e2ed5 RS |
424 | default-directory)) |
425 | (if (file-directory-p file-name) | |
426 | (setq file-name (expand-file-name (change-log-name) file-name))) | |
427 | ;; Chase links before visiting the file. | |
428 | ;; This makes it easier to use a single change log file | |
429 | ;; for several related directories. | |
430 | (setq file-name (file-chase-links file-name)) | |
431 | (setq file-name (expand-file-name file-name)) | |
432 | ;; Move up in the dir hierarchy till we find a change log file. | |
433 | (let ((file1 file-name) | |
434 | parent-dir) | |
435 | (while (and (not (or (get-file-buffer file1) (file-exists-p file1))) | |
436 | (progn (setq parent-dir | |
437 | (file-name-directory | |
438 | (directory-file-name | |
439 | (file-name-directory file1)))) | |
440 | ;; Give up if we are already at the root dir. | |
441 | (not (string= (file-name-directory file1) | |
442 | parent-dir)))) | |
443 | ;; Move up to the parent dir and try again. | |
513063cf | 444 | (setq file1 (expand-file-name |
a82e2ed5 RS |
445 | (file-name-nondirectory (change-log-name)) |
446 | parent-dir))) | |
447 | ;; If we found a change log in a parent, use that. | |
448 | (if (or (get-file-buffer file1) (file-exists-p file1)) | |
449 | (setq file-name file1))))) | |
450 | ;; Make a local variable in this buffer so we needn't search again. | |
451 | (set (make-local-variable 'change-log-default-name) file-name) | |
452 | file-name) | |
45a13f0d | 453 | |
2eb7ccf4 SM |
454 | (defun add-log-file-name (buffer-file log-file) |
455 | ;; Never want to add a change log entry for the ChangeLog file itself. | |
456 | (unless (or (null buffer-file) (string= buffer-file log-file)) | |
d68f7f1b SM |
457 | (if add-log-file-name-function |
458 | (funcall add-log-file-name-function buffer-file) | |
459 | (setq buffer-file | |
460 | (if (string-match | |
461 | (concat "^" (regexp-quote (file-name-directory log-file))) | |
462 | buffer-file) | |
463 | (substring buffer-file (match-end 0)) | |
464 | (file-name-nondirectory buffer-file))) | |
465 | ;; If we have a backup file, it's presumably because we're | |
466 | ;; comparing old and new versions (e.g. for deleted | |
467 | ;; functions) and we'll want to use the original name. | |
468 | (if (backup-file-name-p buffer-file) | |
469 | (file-name-sans-versions buffer-file) | |
470 | buffer-file)))) | |
2eb7ccf4 | 471 | |
84fc2cfa | 472 | ;;;###autoload |
287d149f | 473 | (defun add-change-log-entry (&optional whoami file-name other-window new-entry) |
d882f144 | 474 | "Find change log file, and add an entry for today and an item for this file. |
83afd62c | 475 | Optional arg WHOAMI (interactive prefix) non-nil means prompt for user |
d9ee5172 | 476 | name and email (stored in `add-log-full-name' and `add-log-mailing-address'). |
83afd62c | 477 | |
d882f144 RS |
478 | Second arg FILE-NAME is file name of the change log. |
479 | If nil, use the value of `change-log-default-name'. | |
480 | ||
287d149f | 481 | Third arg OTHER-WINDOW non-nil means visit in other window. |
d882f144 | 482 | |
287d149f | 483 | Fourth arg NEW-ENTRY non-nil means always create a new entry at the front; |
3697b807 DL |
484 | never append to an existing entry. Option `add-log-keep-changes-together' |
485 | otherwise affects whether a new entry is created. | |
486 | ||
598f34fa SS |
487 | Option `add-log-always-start-new-record' non-nil means always create a |
488 | new record, even when the last record was made on the same date and by | |
489 | the same person. | |
490 | ||
d882f144 RS |
491 | The change log file can start with a copyright notice and a copying |
492 | permission notice. The first blank line indicates the end of these | |
493 | notices. | |
494 | ||
3697b807 DL |
495 | Today's date is calculated according to `change-log-time-zone-rule' if |
496 | non-nil, otherwise in local time." | |
84fc2cfa ER |
497 | (interactive (list current-prefix-arg |
498 | (prompt-for-change-log-name))) | |
d68f7f1b SM |
499 | (let* ((defun (add-log-current-defun)) |
500 | (version (and change-log-version-info-enabled | |
501 | (change-log-version-number-search))) | |
8f530b95 SM |
502 | (buf-file-name (if add-log-buffer-file-name-function |
503 | (funcall add-log-buffer-file-name-function) | |
504 | buffer-file-name)) | |
505 | (buffer-file (if buf-file-name (expand-file-name buf-file-name))) | |
dd816e0d | 506 | (file-name (expand-file-name (find-change-log file-name buffer-file))) |
d882f144 RS |
507 | ;; Set ITEM to the file name to use in the new item. |
508 | (item (add-log-file-name buffer-file file-name)) | |
563631fe RS |
509 | bound |
510 | (full-name (or add-log-full-name (user-full-name))) | |
511 | (mailing-address (or add-log-mailing-address user-mail-address))) | |
512 | ||
513 | (if whoami | |
514 | (progn | |
3981e5b5 | 515 | (setq full-name (read-string "Full name: " full-name)) |
563631fe RS |
516 | ;; Note that some sites have room and phone number fields in |
517 | ;; full name which look silly when inserted. Rather than do | |
518 | ;; anything about that here, let user give prefix argument so that | |
519 | ;; s/he can edit the full name field in prompter if s/he wants. | |
520 | (setq mailing-address | |
3981e5b5 | 521 | (read-string "Mailing address: " mailing-address)))) |
2eb7ccf4 | 522 | |
7cd017ba SM |
523 | (unless (equal file-name buffer-file-name) |
524 | (if (or other-window (window-dedicated-p (selected-window))) | |
525 | (find-file-other-window file-name) | |
526 | (find-file file-name))) | |
675a998f RS |
527 | (or (eq major-mode 'change-log-mode) |
528 | (change-log-mode)) | |
84fc2cfa ER |
529 | (undo-boundary) |
530 | (goto-char (point-min)) | |
d882f144 RS |
531 | |
532 | ;; If file starts with a copyright and permission notice, skip them. | |
533 | ;; Assume they end at first blank line. | |
534 | (when (looking-at "Copyright") | |
535 | (search-forward "\n\n") | |
536 | (skip-chars-forward "\n")) | |
537 | ||
538 | ;; Advance into first entry if it is usable; else make new one. | |
c1b3ae42 CW |
539 | (let ((new-entries (mapcar (lambda (addr) |
540 | (concat (funcall add-log-time-format) | |
563631fe | 541 | " " full-name |
c1b3ae42 | 542 | " <" addr ">")) |
563631fe RS |
543 | (if (consp mailing-address) |
544 | mailing-address | |
545 | (list mailing-address))))) | |
598f34fa | 546 | (if (and (not add-log-always-start-new-record) |
c1b3ae42 CW |
547 | (let ((hit nil)) |
548 | (dolist (entry new-entries hit) | |
549 | (when (looking-at (regexp-quote entry)) | |
550 | (setq hit t))))) | |
51bd1843 | 551 | (forward-line 1) |
c1b3ae42 CW |
552 | (insert (nth (random (length new-entries)) |
553 | new-entries) | |
554 | "\n\n") | |
d882f144 | 555 | (forward-line -1))) |
82f4acaf | 556 | |
d882f144 RS |
557 | ;; Determine where we should stop searching for a usable |
558 | ;; item to add to, within this entry. | |
3697b807 | 559 | (setq bound |
d882f144 | 560 | (save-excursion |
3697b807 DL |
561 | (if (looking-at "\n*[^\n* \t]") |
562 | (skip-chars-forward "\n") | |
563 | (if add-log-keep-changes-together | |
564 | (forward-page) ; page delimits entries for date | |
565 | (forward-paragraph))) ; paragraph delimits entries for file | |
566 | (point))) | |
d882f144 RS |
567 | |
568 | ;; Now insert the new line for this item. | |
3697b807 | 569 | (cond ((re-search-forward "^\\s *\\*\\s *$" bound t) |
d882f144 RS |
570 | ;; Put this file name into the existing empty item. |
571 | (if item | |
572 | (insert item))) | |
287d149f | 573 | ((and (not new-entry) |
e172f546 KH |
574 | (let (case-fold-search) |
575 | (re-search-forward | |
d882f144 | 576 | (concat (regexp-quote (concat "* " item)) |
e172f546 KH |
577 | ;; Don't accept `foo.bar' when |
578 | ;; looking for `foo': | |
579 | "\\(\\s \\|[(),:]\\)") | |
3697b807 | 580 | bound t))) |
d882f144 | 581 | ;; Add to the existing item for the same file. |
82f4acaf | 582 | (re-search-forward "^\\s *$\\|^\\s \\*") |
e172f546 | 583 | (goto-char (match-beginning 0)) |
3697b807 DL |
584 | ;; Delete excess empty lines; make just 2. |
585 | (while (and (not (eobp)) (looking-at "^\\s *$")) | |
5960adc7 | 586 | (delete-region (point) (line-beginning-position 2))) |
918f4ac3 | 587 | (insert-char ?\n 2) |
3697b807 DL |
588 | (forward-line -2) |
589 | (indent-relative-maybe)) | |
21d7e080 | 590 | (t |
d882f144 | 591 | ;; Make a new item. |
dd309224 RM |
592 | (while (looking-at "\\sW") |
593 | (forward-line 1)) | |
09b389d0 | 594 | (while (and (not (eobp)) (looking-at "^\\s *$")) |
5960adc7 | 595 | (delete-region (point) (line-beginning-position 2))) |
918f4ac3 | 596 | (insert-char ?\n 3) |
1832dbd1 | 597 | (forward-line -2) |
dd309224 | 598 | (indent-to left-margin) |
918f4ac3 | 599 | (insert "* ") |
d882f144 | 600 | (if item (insert item)))) |
1832dbd1 | 601 | ;; Now insert the function name, if we have one. |
d882f144 | 602 | ;; Point is at the item for this file, |
21d7e080 | 603 | ;; either at the end of the line or at the first blank line. |
5a9ac14b SM |
604 | (if (not defun) |
605 | ;; No function name, so put in a colon unless we have just a star. | |
606 | (unless (save-excursion | |
607 | (beginning-of-line 1) | |
608 | (looking-at "\\s *\\(\\*\\s *\\)?$")) | |
609 | (insert ": ") | |
7b297602 | 610 | (if version (insert version ?\s))) |
5a9ac14b SM |
611 | ;; Make it easy to get rid of the function name. |
612 | (undo-boundary) | |
5960adc7 DL |
613 | (unless (save-excursion |
614 | (beginning-of-line 1) | |
5a9ac14b | 615 | (looking-at "\\s *$")) |
7b297602 | 616 | (insert ?\s)) |
5a9ac14b SM |
617 | ;; See if the prev function name has a message yet or not. |
618 | ;; If not, merge the two items. | |
619 | (let ((pos (point-marker))) | |
620 | (skip-syntax-backward " ") | |
621 | (skip-chars-backward "):") | |
622 | (if (and (looking-at "):") | |
fc9b0554 SM |
623 | (let ((pos (save-excursion (backward-sexp 1) (point)))) |
624 | (when (equal (buffer-substring pos (point)) defun) | |
625 | (delete-region pos (point))) | |
626 | (> fill-column (+ (current-column) (length defun) 4)))) | |
627 | (progn (skip-chars-backward ", ") | |
628 | (delete-region (point) pos) | |
629 | (unless (memq (char-before) '(?\()) (insert ", "))) | |
5a9ac14b SM |
630 | (if (looking-at "):") |
631 | (delete-region (+ 1 (point)) (line-end-position))) | |
632 | (goto-char pos) | |
633 | (insert "(")) | |
634 | (set-marker pos nil)) | |
635 | (insert defun "): ") | |
7b297602 | 636 | (if version (insert version ?\s))))) |
84fc2cfa | 637 | |
84fc2cfa ER |
638 | ;;;###autoload |
639 | (defun add-change-log-entry-other-window (&optional whoami file-name) | |
d882f144 RS |
640 | "Find change log file in other window and add entry and item. |
641 | This is just like `add-change-log-entry' except that it displays | |
642 | the change log file in another window." | |
84fc2cfa ER |
643 | (interactive (if current-prefix-arg |
644 | (list current-prefix-arg | |
645 | (prompt-for-change-log-name)))) | |
646 | (add-change-log-entry whoami file-name t)) | |
82f4acaf | 647 | ;;;###autoload (define-key ctl-x-4-map "a" 'add-change-log-entry-other-window) |
84fc2cfa | 648 | |
fc9b0554 SM |
649 | (defvar add-log-indent-text 0) |
650 | ||
651 | (defun add-log-indent () | |
652 | (let* ((indent | |
653 | (save-excursion | |
654 | (beginning-of-line) | |
655 | (skip-chars-forward " \t") | |
656 | (cond | |
657 | ((and (looking-at "\\(.*\\) [^ \n].*[^ \n] <.*>$") | |
658 | ;; Matching the output of add-log-time-format is difficult, | |
659 | ;; but I'll get it has at least two adjacent digits. | |
660 | (string-match "[[:digit:]][[:digit:]]" (match-string 1))) | |
661 | 0) | |
662 | ((looking-at "[^*(]") | |
663 | (+ (current-left-margin) add-log-indent-text)) | |
664 | (t (current-left-margin))))) | |
665 | (pos (save-excursion (indent-line-to indent) (point)))) | |
666 | (if (> pos (point)) (goto-char pos)))) | |
667 | ||
668 | ||
3066d4ad SM |
669 | (defvar smerge-resolve-function) |
670 | ||
1da56800 | 671 | ;;;###autoload |
7cd017ba | 672 | (define-derived-mode change-log-mode text-mode "Change Log" |
eb8c3be9 | 673 | "Major mode for editing change logs; like Indented Text Mode. |
09b389d0 | 674 | Prevents numeric backups and sets `left-margin' to 8 and `fill-column' to 74. |
3697b807 | 675 | New log entries are usually made with \\[add-change-log-entry] or \\[add-change-log-entry-other-window]. |
5516387c | 676 | Each entry behaves as a paragraph, and the entries for one day as a page. |
3066d4ad SM |
677 | Runs `change-log-mode-hook'. |
678 | \\{change-log-mode-map}" | |
7cd017ba | 679 | (setq left-margin 8 |
4f675a8c | 680 | fill-column 74 |
60f10a06 KH |
681 | indent-tabs-mode t |
682 | tab-width 8) | |
683 | (set (make-local-variable 'fill-paragraph-function) | |
684 | 'change-log-fill-paragraph) | |
fc9b0554 SM |
685 | (set (make-local-variable 'indent-line-function) 'add-log-indent) |
686 | (set (make-local-variable 'tab-always-indent) nil) | |
4b7d4d0d DL |
687 | ;; We really do want "^" in paragraph-start below: it is only the |
688 | ;; lines that begin at column 0 (despite the left-margin of 8) that | |
689 | ;; we are looking for. Adding `* ' allows eliding the blank line | |
690 | ;; between entries for different files. | |
c9cfb0f2 | 691 | (set (make-local-variable 'paragraph-start) "\\s *$\\|\f\\|^\\<") |
4b7d4d0d | 692 | (set (make-local-variable 'paragraph-separate) paragraph-start) |
2c91c85c RS |
693 | ;; Match null string on the date-line so that the date-line |
694 | ;; is grouped with what follows. | |
964141f2 | 695 | (set (make-local-variable 'page-delimiter) "^\\<\\|^\f") |
dd309224 | 696 | (set (make-local-variable 'version-control) 'never) |
7cd017ba SM |
697 | (set (make-local-variable 'smerge-resolve-function) |
698 | 'change-log-resolve-conflict) | |
dd309224 | 699 | (set (make-local-variable 'adaptive-fill-regexp) "\\s *") |
4b286eca | 700 | (set (make-local-variable 'font-lock-defaults) |
7cd017ba | 701 | '(change-log-font-lock-keywords t nil nil backward-paragraph))) |
21d7e080 | 702 | |
287d149f RM |
703 | ;; It might be nice to have a general feature to replace this. The idea I |
704 | ;; have is a variable giving a regexp matching text which should not be | |
705 | ;; moved from bol by filling. change-log-mode would set this to "^\\s *\\s(". | |
706 | ;; But I don't feel up to implementing that today. | |
707 | (defun change-log-fill-paragraph (&optional justify) | |
708 | "Fill the paragraph, but preserve open parentheses at beginning of lines. | |
709 | Prefix arg means justify as well." | |
710 | (interactive "P") | |
8d6467a4 RS |
711 | (let ((end (progn (forward-paragraph) (point))) |
712 | (beg (progn (backward-paragraph) (point))) | |
14ee1953 | 713 | (paragraph-start (concat paragraph-start "\\|\\s *\\s("))) |
8d6467a4 RS |
714 | (fill-region beg end justify) |
715 | t)) | |
287d149f | 716 | \f |
fcad5199 | 717 | (defcustom add-log-current-defun-header-regexp |
a8d693d8 | 718 | "^\\([[:upper:]][[:upper:]_ ]*[[:upper:]_]\\|[-_[:alpha:]]+\\)[ \t]*[:=]" |
fcad5199 RS |
719 | "*Heuristic regexp used by `add-log-current-defun' for unknown major modes." |
720 | :type 'regexp | |
721 | :group 'change-log) | |
21d7e080 | 722 | |
fb644f48 EN |
723 | ;;;###autoload |
724 | (defvar add-log-lisp-like-modes | |
3697b807 | 725 | '(emacs-lisp-mode lisp-mode scheme-mode dsssl-mode lisp-interaction-mode) |
fb644f48 EN |
726 | "*Modes that look like Lisp to `add-log-current-defun'.") |
727 | ||
728 | ;;;###autoload | |
729 | (defvar add-log-c-like-modes | |
730 | '(c-mode c++-mode c++-c-mode objc-mode) | |
731 | "*Modes that look like C to `add-log-current-defun'.") | |
732 | ||
733 | ;;;###autoload | |
734 | (defvar add-log-tex-like-modes | |
735 | '(TeX-mode plain-TeX-mode LaTeX-mode plain-tex-mode latex-mode) | |
736 | "*Modes that look like TeX to `add-log-current-defun'.") | |
737 | ||
e332f80b | 738 | ;;;###autoload |
21d7e080 ER |
739 | (defun add-log-current-defun () |
740 | "Return name of function definition point is in, or nil. | |
741 | ||
63314951 | 742 | Understands C, Lisp, LaTeX (\"functions\" are chapters, sections, ...), |
5960adc7 | 743 | Texinfo (@node titles) and Perl. |
21d7e080 ER |
744 | |
745 | Other modes are handled by a heuristic that looks in the 10K before | |
746 | point for uppercase headings starting in the first column or | |
5960adc7 | 747 | identifiers followed by `:' or `='. See variables |
c1356086 | 748 | `add-log-current-defun-header-regexp' and |
5ef08021 | 749 | `add-log-current-defun-function'. |
21d7e080 ER |
750 | |
751 | Has a preference of looking backwards." | |
2cc0b765 RS |
752 | (condition-case nil |
753 | (save-excursion | |
754 | (let ((location (point))) | |
5960adc7 DL |
755 | (cond (add-log-current-defun-function |
756 | (funcall add-log-current-defun-function)) | |
c1356086 | 757 | ((memq major-mode add-log-lisp-like-modes) |
a0151877 | 758 | ;; If we are now precisely at the beginning of a defun, |
2cc0b765 RS |
759 | ;; make sure beginning-of-defun finds that one |
760 | ;; rather than the previous one. | |
761 | (or (eobp) (forward-char 1)) | |
762 | (beginning-of-defun) | |
5960adc7 DL |
763 | ;; Make sure we are really inside the defun found, |
764 | ;; not after it. | |
42b1fc29 RS |
765 | (when (and (looking-at "\\s(") |
766 | (progn (end-of-defun) | |
767 | (< location (point))) | |
768 | (progn (forward-sexp -1) | |
769 | (>= location (point)))) | |
770 | (if (looking-at "\\s(") | |
771 | (forward-char 1)) | |
772 | ;; Skip the defining construct name, typically "defun" | |
773 | ;; or "defvar". | |
774 | (forward-sexp 1) | |
775 | ;; The second element is usually a symbol being defined. | |
776 | ;; If it is not, use the first symbol in it. | |
63c7727f | 777 | (skip-chars-forward " \t\n'(") |
5960adc7 DL |
778 | (buffer-substring-no-properties (point) |
779 | (progn (forward-sexp 1) | |
780 | (point))))) | |
fb644f48 EN |
781 | ((and (memq major-mode add-log-c-like-modes) |
782 | (save-excursion | |
783 | (beginning-of-line) | |
784 | ;; Use eq instead of = here to avoid | |
785 | ;; error when at bob and char-after | |
786 | ;; returns nil. | |
787 | (while (eq (char-after (- (point) 2)) ?\\) | |
788 | (forward-line -1)) | |
789 | (looking-at "[ \t]*#[ \t]*define[ \t]"))) | |
2cc0b765 RS |
790 | ;; Handle a C macro definition. |
791 | (beginning-of-line) | |
792 | (while (eq (char-after (- (point) 2)) ?\\) ;not =; note above | |
793 | (forward-line -1)) | |
794 | (search-forward "define") | |
795 | (skip-chars-forward " \t") | |
5960adc7 DL |
796 | (buffer-substring-no-properties (point) |
797 | (progn (forward-sexp 1) | |
798 | (point)))) | |
fb644f48 | 799 | ((memq major-mode add-log-c-like-modes) |
2cc0b765 RS |
800 | (beginning-of-line) |
801 | ;; See if we are in the beginning part of a function, | |
802 | ;; before the open brace. If so, advance forward. | |
803 | (while (not (looking-at "{\\|\\(\\s *$\\)")) | |
804 | (forward-line 1)) | |
805 | (or (eobp) | |
806 | (forward-char 1)) | |
8eca85ef RS |
807 | (let (maybe-beg) |
808 | ;; Try to find the containing defun. | |
809 | (beginning-of-defun) | |
810 | (end-of-defun) | |
811 | ;; If the defun we found ends before the desired position, | |
812 | ;; see if there's a DEFUN construct | |
813 | ;; between that end and the desired position. | |
814 | (when (save-excursion | |
815 | (and (> location (point)) | |
bf247b6e | 816 | (re-search-forward "^DEFUN" |
8eca85ef RS |
817 | (save-excursion |
818 | (goto-char location) | |
819 | (line-end-position)) | |
820 | t) | |
821 | (re-search-forward "^{" nil t) | |
822 | (setq maybe-beg (point)))) | |
823 | ;; If so, go to the end of that instead. | |
824 | (goto-char maybe-beg) | |
825 | (end-of-defun))) | |
826 | ;; If the desired position is within the defun we found, | |
827 | ;; find the function name. | |
828 | (when (< location (point)) | |
5960adc7 DL |
829 | (backward-sexp 1) |
830 | (let (beg tem) | |
831 | ||
832 | (forward-line -1) | |
833 | ;; Skip back over typedefs of arglist. | |
834 | (while (and (not (bobp)) | |
835 | (looking-at "[ \t\n]")) | |
836 | (forward-line -1)) | |
837 | ;; See if this is using the DEFUN macro used in Emacs, | |
568ec09d | 838 | ;; or the DEFUN macro used by the C library: |
5960adc7 DL |
839 | (if (condition-case nil |
840 | (and (save-excursion | |
841 | (end-of-line) | |
842 | (while (= (preceding-char) ?\\) | |
843 | (end-of-line 2)) | |
844 | (backward-sexp 1) | |
845 | (beginning-of-line) | |
846 | (setq tem (point)) | |
847 | (looking-at "DEFUN\\b")) | |
848 | (>= location tem)) | |
849 | (error nil)) | |
568ec09d SS |
850 | ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory |
851 | ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK | |
5960adc7 DL |
852 | (progn |
853 | (goto-char tem) | |
854 | (down-list 1) | |
568ec09d SS |
855 | (when (= (char-after (point)) ?\") |
856 | (forward-sexp 1) | |
857 | (search-forward ",")) | |
858 | (skip-syntax-forward " ") | |
5960adc7 DL |
859 | (buffer-substring-no-properties |
860 | (point) | |
568ec09d SS |
861 | (progn (search-forward ",") |
862 | (forward-char -1) | |
863 | (skip-syntax-backward " ") | |
5960adc7 DL |
864 | (point)))) |
865 | (if (looking-at "^[+-]") | |
866 | (change-log-get-method-definition) | |
867 | ;; Ordinary C function syntax. | |
868 | (setq beg (point)) | |
869 | (if (and | |
870 | ;; Protect against "Unbalanced parens" error. | |
871 | (condition-case nil | |
872 | (progn | |
873 | (down-list 1) ; into arglist | |
874 | (backward-up-list 1) | |
875 | (skip-chars-backward " \t") | |
876 | t) | |
877 | (error nil)) | |
878 | ;; Verify initial pos was after | |
879 | ;; real start of function. | |
880 | (save-excursion | |
881 | (goto-char beg) | |
882 | ;; For this purpose, include the line | |
883 | ;; that has the decl keywords. This | |
884 | ;; may also include some of the | |
885 | ;; comments before the function. | |
886 | (while (and (not (bobp)) | |
887 | (save-excursion | |
888 | (forward-line -1) | |
889 | (looking-at "[^\n\f]"))) | |
890 | (forward-line -1)) | |
891 | (>= location (point))) | |
892 | ;; Consistency check: going down and up | |
893 | ;; shouldn't take us back before BEG. | |
894 | (> (point) beg)) | |
895 | (let (end middle) | |
896 | ;; Don't include any final whitespace | |
897 | ;; in the name we use. | |
898 | (skip-chars-backward " \t\n") | |
899 | (setq end (point)) | |
900 | (backward-sexp 1) | |
901 | ;; Now find the right beginning of the name. | |
902 | ;; Include certain keywords if they | |
903 | ;; precede the name. | |
904 | (setq middle (point)) | |
905 | (forward-word -1) | |
906 | ;; Ignore these subparts of a class decl | |
907 | ;; and move back to the class name itself. | |
908 | (while (looking-at "public \\|private ") | |
909 | (skip-chars-backward " \t:") | |
910 | (setq end (point)) | |
911 | (backward-sexp 1) | |
912 | (setq middle (point)) | |
913 | (forward-word -1)) | |
914 | (and (bolp) | |
915 | (looking-at | |
916 | "enum \\|struct \\|union \\|class ") | |
917 | (setq middle (point))) | |
918 | (goto-char end) | |
919 | (when (eq (preceding-char) ?=) | |
920 | (forward-char -1) | |
921 | (skip-chars-backward " \t") | |
922 | (setq end (point))) | |
923 | (buffer-substring-no-properties | |
924 | middle end)))))))) | |
fb644f48 | 925 | ((memq major-mode add-log-tex-like-modes) |
2cc0b765 | 926 | (if (re-search-backward |
5960adc7 DL |
927 | "\\\\\\(sub\\)*\\(section\\|paragraph\\|chapter\\)" |
928 | nil t) | |
2cc0b765 RS |
929 | (progn |
930 | (goto-char (match-beginning 0)) | |
5960adc7 DL |
931 | (buffer-substring-no-properties |
932 | (1+ (point)) ; without initial backslash | |
933 | (line-end-position))))) | |
2cc0b765 | 934 | ((eq major-mode 'texinfo-mode) |
3071ee28 | 935 | (if (re-search-backward "^@node[ \t]+\\([^,\n]+\\)" nil t) |
5960adc7 | 936 | (match-string-no-properties 1))) |
c06e9d8b | 937 | ((memq major-mode '(perl-mode cperl-mode)) |
5a9ac14b | 938 | (if (re-search-backward "^sub[ \t]+\\([^({ \t\n]+\\)" nil t) |
5960adc7 DL |
939 | (match-string-no-properties 1))) |
940 | ;; Emacs's autoconf-mode installs its own | |
941 | ;; `add-log-current-defun-function'. This applies to | |
942 | ;; a different mode apparently for editing .m4 | |
943 | ;; autoconf source. | |
f654865f | 944 | ((eq major-mode 'autoconf-mode) |
5960adc7 DL |
945 | (if (re-search-backward |
946 | "^\\(\\(m4_\\)?define\\|A._DEFUN\\)(\\[?\\([A-Za-z0-9_]+\\)" nil t) | |
947 | (match-string-no-properties 3))) | |
2cc0b765 RS |
948 | (t |
949 | ;; If all else fails, try heuristics | |
c1356086 GM |
950 | (let (case-fold-search |
951 | result) | |
2cc0b765 | 952 | (end-of-line) |
c1356086 GM |
953 | (when (re-search-backward |
954 | add-log-current-defun-header-regexp | |
955 | (- (point) 10000) | |
956 | t) | |
5960adc7 DL |
957 | (setq result (or (match-string-no-properties 1) |
958 | (match-string-no-properties 0))) | |
c1356086 GM |
959 | ;; Strip whitespace away |
960 | (when (string-match "\\([^ \t\n\r\f].*[^ \t\n\r\f]\\)" | |
961 | result) | |
5960adc7 | 962 | (setq result (match-string-no-properties 1 result))) |
c1356086 | 963 | result)))))) |
2cc0b765 | 964 | (error nil))) |
ef15f270 | 965 | |
83afd62c | 966 | (defvar change-log-get-method-definition-md) |
15319a8f | 967 | |
83afd62c | 968 | ;; Subroutine used within change-log-get-method-definition. |
59c1a7de RS |
969 | ;; Add the last match in the buffer to the end of `md', |
970 | ;; followed by the string END; move to the end of that match. | |
83afd62c KH |
971 | (defun change-log-get-method-definition-1 (end) |
972 | (setq change-log-get-method-definition-md | |
973 | (concat change-log-get-method-definition-md | |
5960adc7 | 974 | (match-string 1) |
15319a8f | 975 | end)) |
59c1a7de RS |
976 | (goto-char (match-end 0))) |
977 | ||
83afd62c | 978 | (defun change-log-get-method-definition () |
eba72fc1 | 979 | "For Objective C, return the method name if we are in a method." |
83afd62c | 980 | (let ((change-log-get-method-definition-md "[")) |
59c1a7de | 981 | (save-excursion |
f27f16ed | 982 | (if (re-search-backward "^@implementation\\s-*\\([A-Za-z_]*\\)" nil t) |
83afd62c | 983 | (change-log-get-method-definition-1 " "))) |
59c1a7de RS |
984 | (save-excursion |
985 | (cond | |
f27f16ed | 986 | ((re-search-forward "^\\([-+]\\)[ \t\n\f\r]*\\(([^)]*)\\)?\\s-*" nil t) |
83afd62c | 987 | (change-log-get-method-definition-1 "") |
59c1a7de RS |
988 | (while (not (looking-at "[{;]")) |
989 | (looking-at | |
f27f16ed | 990 | "\\([A-Za-z_]*:?\\)\\s-*\\(([^)]*)\\)?[A-Za-z_]*[ \t\n\f\r]*") |
83afd62c KH |
991 | (change-log-get-method-definition-1 "")) |
992 | (concat change-log-get-method-definition-md "]")))))) | |
075a6629 DL |
993 | \f |
994 | (defun change-log-sortable-date-at () | |
995 | "Return date of log entry in a consistent form for sorting. | |
996 | Point is assumed to be at the start of the entry." | |
997 | (require 'timezone) | |
998 | (if (looking-at "^\\sw.........[0-9:+ ]*") | |
999 | (let ((date (match-string-no-properties 0))) | |
1000 | (if date | |
1001 | (if (string-match "\\(....\\)-\\(..\\)-\\(..\\)\\s-+" date) | |
1002 | (concat (match-string 1 date) (match-string 2 date) | |
1003 | (match-string 3 date)) | |
1004 | (condition-case nil | |
1005 | (timezone-make-date-sortable date) | |
1006 | (error nil))))) | |
1007 | (error "Bad date"))) | |
59c1a7de | 1008 | |
7cd017ba SM |
1009 | (defun change-log-resolve-conflict () |
1010 | "Function to be used in `smerge-resolve-function'." | |
1011 | (let ((buf (current-buffer))) | |
1012 | (with-temp-buffer | |
1013 | (insert-buffer-substring buf (match-beginning 1) (match-end 1)) | |
1014 | (save-match-data (change-log-mode)) | |
1015 | (let ((other-buf (current-buffer))) | |
1016 | (with-current-buffer buf | |
1017 | (save-excursion | |
1018 | (save-restriction | |
1019 | (narrow-to-region (match-beginning 0) (match-end 0)) | |
1020 | (replace-match (match-string 3) t t) | |
1021 | (change-log-merge other-buf)))))))) | |
1022 | ||
075a6629 DL |
1023 | ;;;###autoload |
1024 | (defun change-log-merge (other-log) | |
eba72fc1 | 1025 | "Merge the contents of change log file OTHER-LOG with this buffer. |
075a6629 | 1026 | Both must be found in Change Log mode (since the merging depends on |
7cd017ba SM |
1027 | the appropriate motion commands). OTHER-LOG can be either a file name |
1028 | or a buffer. | |
075a6629 | 1029 | |
918f4ac3 DL |
1030 | Entries are inserted in chronological order. Both the current and |
1031 | old-style time formats for entries are supported." | |
075a6629 DL |
1032 | (interactive "*fLog file name to merge: ") |
1033 | (if (not (eq major-mode 'change-log-mode)) | |
1034 | (error "Not in Change Log mode")) | |
7cd017ba SM |
1035 | (let ((other-buf (if (bufferp other-log) other-log |
1036 | (find-file-noselect other-log))) | |
075a6629 DL |
1037 | (buf (current-buffer)) |
1038 | date1 start end) | |
1039 | (save-excursion | |
1040 | (goto-char (point-min)) | |
1041 | (set-buffer other-buf) | |
1042 | (goto-char (point-min)) | |
1043 | (if (not (eq major-mode 'change-log-mode)) | |
1044 | (error "%s not found in Change Log mode" other-log)) | |
1045 | ;; Loop through all the entries in OTHER-LOG. | |
1046 | (while (not (eobp)) | |
1047 | (setq date1 (change-log-sortable-date-at)) | |
1048 | (setq start (point) | |
1049 | end (progn (forward-page) (point))) | |
1050 | ;; Look for an entry in original buffer that isn't later. | |
1051 | (with-current-buffer buf | |
1052 | (while (and (not (eobp)) | |
1053 | (string< date1 (change-log-sortable-date-at))) | |
1054 | (forward-page)) | |
1055 | (if (not (eobp)) | |
1056 | (insert-buffer-substring other-buf start end) | |
1057 | ;; At the end of the original buffer, insert a newline to | |
1058 | ;; separate entries and then the rest of the file being | |
7cd017ba SM |
1059 | ;; merged. |
1060 | (unless (or (bobp) | |
1061 | (and (= ?\n (char-before)) | |
1062 | (or (<= (1- (point)) (point-min)) | |
1063 | (= ?\n (char-before (1- (point))))))) | |
1064 | (insert "\n")) | |
1065 | ;; Move to the end of it to terminate outer loop. | |
1066 | (with-current-buffer other-buf | |
1067 | (goto-char (point-max))) | |
1068 | (insert-buffer-substring other-buf start))))))) | |
ef15f270 | 1069 | |
918f4ac3 DL |
1070 | ;;;###autoload |
1071 | (defun change-log-redate () | |
1072 | "Fix any old-style date entries in the current log file to default format." | |
1073 | (interactive) | |
1074 | (require 'timezone) | |
1075 | (save-excursion | |
1076 | (goto-char (point-min)) | |
1077 | (while (re-search-forward "^\\sw.........[0-9:+ ]*" nil t) | |
1078 | (unless (= 12 (- (match-end 0) (match-beginning 0))) | |
1079 | (let* ((date (save-match-data | |
1080 | (timezone-fix-time (match-string 0) nil nil))) | |
1081 | (zone (if (consp (aref date 6)) | |
1082 | (nth 1 (aref date 6))))) | |
1083 | (replace-match (format-time-string | |
1084 | "%Y-%m-%d " | |
1085 | (encode-time (aref date 5) | |
1086 | (aref date 4) | |
1087 | (aref date 3) | |
1088 | (aref date 2) | |
1089 | (aref date 1) | |
1090 | (aref date 0) | |
1091 | zone)))))))) | |
1092 | ||
1da56800 RS |
1093 | (provide 'add-log) |
1094 | ||
ab5796a9 | 1095 | ;;; arch-tag: 81eee6fc-088f-4372-a37f-80ad9620e762 |
fd7fa35a | 1096 | ;;; add-log.el ends here |