Commit | Line | Data |
---|---|---|
9565745a | 1 | ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs |
9565745a | 2 | |
22453b51 | 3 | ;; Copyright 1989, 1993, 1994, 1995, 1997, 2000, 2001 |
3b5e21df | 4 | ;;; Free Software Foundation, Inc. |
b578f267 | 5 | |
e8af40ee PJ |
6 | ;; This file is part of GNU Emacs. |
7 | ||
8 | ;; Maintainer's Time-stamp: <2001-07-15 12:35:58 pavel> | |
e602e779 | 9 | ;; Maintainer: Stephen Gildea <gildea@stop.mail-abuse.org> |
9565745a RS |
10 | ;; Keywords: tools |
11 | ||
12 | ;; This file is free software; you can redistribute it and/or modify | |
13 | ;; it under the terms of the GNU General Public License as published by | |
14 | ;; the Free Software Foundation; either version 2, or (at your option) | |
15 | ;; any later version. | |
16 | ||
17 | ;; This file is distributed in the hope that it will be useful, | |
18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
20 | ;; GNU General Public License for more details. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
25 | ;; Boston, MA 02111-1307, USA. | |
9565745a RS |
26 | |
27 | ;;; Commentary: | |
28 | ||
03d7856a KH |
29 | ;; A template in a file can be updated with a new time stamp when |
30 | ;; you save the file. For example: | |
31 | ;; static char *ts = "sdmain.c Time-stamp: <1996-08-13 10:20:51 gildea>"; | |
32 | ;; See the top of `time-stamp.el' for another example. | |
33 | ||
34 | ;; To use time-stamping, add this line to your .emacs file: | |
35 | ;; (add-hook 'write-file-hooks 'time-stamp) | |
36 | ;; Now any time-stamp templates in your files will be updated automatically. | |
37 | ||
38 | ;; See the documentation for the functions `time-stamp' | |
39 | ;; and `time-stamp-toggle-active' for details. | |
b578f267 | 40 | |
9565745a RS |
41 | ;;; Code: |
42 | ||
cb35a83c RS |
43 | (defgroup time-stamp nil |
44 | "Maintain last change time stamps in files edited by Emacs." | |
45 | :group 'data | |
46 | :group 'extensions) | |
47 | ||
fd72ddf6 | 48 | (defcustom time-stamp-format "%:y-%02m-%02d %02H:%02M:%02S %u" |
03d7856a | 49 | "*Format of the string inserted by \\[time-stamp]. |
c9c0e4bb RS |
50 | The value may be a string or a list. Lists are supported only for |
51 | backward compatibility; see variable `time-stamp-old-format-warn'. | |
f8d35bf3 | 52 | |
22453b51 DL |
53 | A string is used verbatim except for character sequences beginning |
54 | with %, as follows. The values of non-numeric formatted items depend | |
ef080b16 EZ |
55 | on the locale setting recorded in `system-time-locale' and |
56 | `locale-coding-system'. The examples here are for the default | |
57 | (`C') locale. | |
fd72ddf6 RS |
58 | |
59 | %:a weekday name: `Monday'. %#A gives uppercase: `MONDAY' | |
60 | %3a abbreviated weekday: `Mon'. %3A gives uppercase: `MON' | |
61 | %:b month name: `January'. %#B gives uppercase: `JANUARY' | |
62 | %3b abbreviated month: `Jan'. %3B gives uppercase: `JAN' | |
63 | %02d day of month | |
64 | %02H 24-hour clock hour | |
65 | %02I 12-hour clock hour | |
66 | %02m month number | |
67 | %02M minute | |
68 | %#p `am' or `pm'. %P gives uppercase: `AM' or `PM' | |
69 | %02S seconds | |
70 | %w day number of week, Sunday is 0 | |
71 | %02y 2-digit year: `97' %:y 4-digit year: `1997' | |
72 | %z time zone name: `est'. %Z gives uppercase: `EST' | |
73 | ||
74 | Non-date items: | |
75 | %% a literal percent character: `%' | |
76 | %f file name without directory %F gives absolute pathname | |
77 | %s system name | |
f7769aa5 | 78 | %u user's login name %U user's full name |
fd72ddf6 RS |
79 | %h mail host name |
80 | ||
81 | Decimal digits between the % and the type character specify the | |
82 | field width. Strings are truncated on the right; years on the left. | |
55ac4d11 | 83 | A leading zero in the field width zero-fills a number. |
622b7ede RS |
84 | |
85 | For example, to get the format used by the `date' command, | |
fd72ddf6 RS |
86 | use \"%3a %3b %2d %02H:%02M:%02S %Z %:y\". |
87 | ||
55ac4d11 | 88 | In the future these formats will be aligned more with `format-time-string'. |
fd72ddf6 RS |
89 | Because of this transition, the default padding for numeric formats will |
90 | change in a future version. Therefore either a padding width should be | |
91 | specified, or the : modifier should be used to explicitly request the | |
92 | historical default." | |
cb35a83c RS |
93 | :type 'string |
94 | :group 'time-stamp) | |
03d7856a | 95 | |
95772e85 RS |
96 | (defcustom time-stamp-active t |
97 | "*Non-nil to enable time-stamping of buffers by \\[time-stamp]. | |
98 | Can be toggled by \\[time-stamp-toggle-active]. | |
99 | See also the variable `time-stamp-warn-inactive'." | |
100 | :type 'boolean | |
101 | :group 'time-stamp) | |
102 | ||
103 | (defcustom time-stamp-warn-inactive t | |
3b5e21df GM |
104 | "Have \\[time-stamp] warn if a buffer did not get time-stamped. |
105 | If non-nil, a warning is displayed if `time-stamp-active' disables time | |
106 | stamping and the buffer contains a template that would otherwise have | |
107 | been updated." | |
95772e85 RS |
108 | :type 'boolean |
109 | :group 'time-stamp) | |
110 | ||
111 | (defcustom time-stamp-old-format-warn 'ask | |
3b5e21df | 112 | "Action if `time-stamp-format' is an old-style list. |
95772e85 RS |
113 | If `error', the format is not used. If `ask', the user is queried about |
114 | using the time-stamp-format. If `warn', a warning is displayed. | |
115 | If nil, no notification is given." | |
0a8052bd GM |
116 | :type '(choice (const :tag "Don't use the format" error) |
117 | (const ask) | |
118 | (const warn) | |
119 | (const :tag "No notification" nil)) | |
95772e85 RS |
120 | :group 'time-stamp) |
121 | ||
122 | (defcustom time-stamp-time-zone nil | |
123 | "If non-nil, a string naming the timezone to be used by \\[time-stamp]. | |
124 | Format is the same as that used by the environment variable TZ on your system." | |
7967f8ab | 125 | :type '(choice (const nil) string) |
95772e85 | 126 | :group 'time-stamp) |
622b7ede RS |
127 | |
128 | ||
b4b33e01 | 129 | ;;; Do not change time-stamp-line-limit, time-stamp-start, |
55ac4d11 KH |
130 | ;;; time-stamp-end, time-stamp-pattern, time-stamp-inserts-lines, |
131 | ;;; or time-stamp-count in your .emacs or you will be incompatible | |
132 | ;;; with other people's files! If you must change them, do so only | |
133 | ;;; in the local variables section of the file itself. | |
9565745a | 134 | |
03d7856a | 135 | |
e1f40b28 | 136 | (defvar time-stamp-line-limit 8 ;Do not change! |
c7c4ddd4 | 137 | "Lines of a file searched; positive counts from start, negative from end. |
55ac4d11 KH |
138 | The patterns `time-stamp-start' and `time-stamp-end' must be found in |
139 | the first (last) `time-stamp-line-limit' lines of the file for the | |
f7769aa5 RS |
140 | file to be time-stamped by \\[time-stamp]. A value of 0 searches the |
141 | entire buffer (use with care). | |
e1f40b28 RS |
142 | |
143 | Do not change `time-stamp-line-limit', `time-stamp-start', or | |
144 | `time-stamp-end' for yourself or you will be incompatible | |
145 | with other people's files! If you must change them for some application, | |
146 | do so in the local variables section of the time-stamped file itself.") | |
147 | ||
9565745a | 148 | |
36081614 | 149 | (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change! |
9565745a | 150 | "Regexp after which the time stamp is written by \\[time-stamp]. |
e1f40b28 | 151 | See also the variables `time-stamp-end' and `time-stamp-line-limit'. |
9565745a | 152 | |
e1f40b28 RS |
153 | Do not change `time-stamp-line-limit', `time-stamp-start', or |
154 | `time-stamp-end' for yourself or you will be incompatible | |
9565745a RS |
155 | with other people's files! If you must change them for some application, |
156 | do so in the local variables section of the time-stamped file itself.") | |
157 | ||
158 | ||
e1f40b28 | 159 | (defvar time-stamp-end "\\\\?[\">]" ;Do not change! |
9565745a | 160 | "Regexp marking the text after the time stamp. |
e1f40b28 | 161 | \\[time-stamp] deletes the text between the first match of `time-stamp-start' |
55ac4d11 KH |
162 | and the following match of `time-stamp-end', then writes the |
163 | time stamp specified by `time-stamp-format' between them. | |
e1f40b28 | 164 | |
55ac4d11 KH |
165 | The end text normally starts on the same line as the start text ends, |
166 | but if there are any newlines in `time-stamp-format', the same number | |
167 | of newlines must separate the start and end. \\[time-stamp] tries | |
168 | to not change the number of lines in the buffer. `time-stamp-inserts-lines' | |
169 | controls this behavior. | |
170 | ||
171 | Do not change `time-stamp-line-limit', `time-stamp-start', `time-stamp-end', | |
172 | or `time-stamp-inserts-lines' for yourself or you will be incompatible | |
e1f40b28 RS |
173 | with other people's files! If you must change them for some application, |
174 | do so in the local variables section of the time-stamped file itself.") | |
175 | ||
9565745a | 176 | |
55ac4d11 KH |
177 | (defvar time-stamp-inserts-lines nil ;Do not change! |
178 | "Whether time-stamp can change the number of lines in a file. | |
179 | If nil, \\[time-stamp] skips as many lines as there are newlines in | |
180 | `time-stamp-format' before looking for the `time-stamp-end' pattern, | |
181 | thus it tries not to change the number of lines in the buffer. | |
182 | If non-nil, \\[time-stamp] starts looking for the end pattern | |
183 | immediately after the start pattern. This behavior can cause | |
184 | unexpected changes in the buffer if used carelessly, but it is useful | |
185 | for generating repeated time stamps. | |
186 | ||
187 | Do not change `time-stamp-end' or `time-stamp-inserts-lines' for | |
188 | yourself or you will be incompatible with other people's files! | |
189 | If you must change them for some application, do so in the local | |
190 | variables section of the time-stamped file itself.") | |
191 | ||
192 | ||
193 | (defvar time-stamp-count 1 ;Do not change! | |
3b5e21df | 194 | "How many templates \\[time-stamp] will look for in a buffer. |
55ac4d11 KH |
195 | The same time-stamp will be written in each case. |
196 | ||
197 | Do not change `time-stamp-count' for yourself or you will be | |
198 | incompatible with other people's files! If you must change it for | |
199 | some application, do so in the local variables section of the | |
200 | time-stamped file itself.") | |
201 | ||
202 | ||
b4b33e01 | 203 | (defvar time-stamp-pattern "%%" ;Do not change! |
55ac4d11 | 204 | "Convenience variable setting all `time-stamp' location and format values. |
b4b33e01 | 205 | This string has four parts, each of which is optional. |
55ac4d11 KH |
206 | These four parts set `time-stamp-line-limit', `time-stamp-start', |
207 | `time-stamp-format', and `time-stamp-end'. See the documentation | |
b4b33e01 RS |
208 | for each of these variables for details. |
209 | ||
210 | The first part is a number followed by a slash; the number sets the number | |
211 | of lines at the beginning (negative counts from end) of the file searched | |
212 | for the time-stamp. The number and the slash may be omitted to use the | |
213 | normal value. | |
214 | ||
215 | The second part is a regexp identifying the pattern preceding the time stamp. | |
216 | This part may be omitted to use the normal pattern. | |
217 | ||
218 | The third part specifies the format of the time-stamp inserted. See | |
55ac4d11 | 219 | the documentation for `time-stamp-format' for details. Specify this |
b4b33e01 RS |
220 | part as \"%%\" to use the normal format. |
221 | ||
222 | The fourth part is a regexp identifying the pattern following the time stamp. | |
223 | This part may be omitted to use the normal pattern. | |
224 | ||
225 | As an example, the default behavior can be specified something like this: | |
226 | \"8/Time-stamp: [\\\"<]%:y-%02m-%02d %02H:%02M:%02S %u[\\\">]\" | |
227 | ||
228 | Do not change `time-stamp-pattern' for yourself or you will be incompatible | |
229 | with other people's files! Set it only in the local variables section | |
230 | of the time-stamped file itself.") | |
231 | ||
232 | ||
03d7856a | 233 | |
59b644e8 | 234 | ;;;###autoload |
9565745a | 235 | (defun time-stamp () |
55ac4d11 | 236 | "Update the time stamp string(s) in the buffer. |
03d7856a KH |
237 | A template in a file can be automatically updated with a new time stamp |
238 | every time you save the file. Add this line to your .emacs file: | |
239 | (add-hook 'write-file-hooks 'time-stamp) | |
240 | Normally the template must appear in the first 8 lines of a file and | |
241 | look like one of the following: | |
242 | Time-stamp: <> | |
243 | Time-stamp: \" \" | |
244 | The time stamp is written between the brackets or quotes: | |
b4b33e01 | 245 | Time-stamp: <1998-02-18 10:20:51 gildea> |
622b7ede | 246 | The time stamp is updated only if the variable `time-stamp-active' is non-nil. |
03d7856a | 247 | The format of the time stamp is set by the variable `time-stamp-format'. |
55ac4d11 KH |
248 | The variables `time-stamp-line-limit', `time-stamp-start', `time-stamp-end', |
249 | `time-stamp-count', and `time-stamp-inserts-lines' control finding the | |
250 | template." | |
9565745a | 251 | (interactive) |
55ac4d11 | 252 | (let ((line-limit time-stamp-line-limit) |
b4b33e01 RS |
253 | (ts-start time-stamp-start) |
254 | (ts-format time-stamp-format) | |
55ac4d11 KH |
255 | (ts-end time-stamp-end) |
256 | (ts-count time-stamp-count) | |
257 | (format-lines 0) | |
258 | (end-lines 1) | |
259 | (start nil) | |
260 | search-limit) | |
b4b33e01 RS |
261 | (if (stringp time-stamp-pattern) |
262 | (progn | |
15ceaba4 | 263 | (string-match "\\`\\(\\(-?[0-9]+\\)/\\)?\\([^%]+\\)?\\(\\(.\\|\n\\)*%[-.,:@+_ #^()0-9]*[A-Za-z%]\\)?\\([^%]+\\)?\\'" time-stamp-pattern) |
b4b33e01 RS |
264 | (and (match-beginning 2) |
265 | (setq line-limit | |
266 | (string-to-int (match-string 2 time-stamp-pattern)))) | |
267 | (and (match-beginning 3) | |
268 | (setq ts-start (match-string 3 time-stamp-pattern))) | |
269 | (and (match-beginning 4) | |
270 | (not (string-equal (match-string 4 time-stamp-pattern) "%%")) | |
271 | (setq ts-format (match-string 4 time-stamp-pattern))) | |
15ceaba4 KH |
272 | (and (match-beginning 6) |
273 | (setq ts-end (match-string 6 time-stamp-pattern))))) | |
622b7ede RS |
274 | (cond ((not (integerp line-limit)) |
275 | (setq line-limit 8) | |
b4b33e01 | 276 | (message "time-stamp-line-limit is not an integer") |
622b7ede | 277 | (sit-for 1))) |
55ac4d11 KH |
278 | (cond ((not (integerp ts-count)) |
279 | (setq ts-count 1) | |
280 | (message "time-stamp-count is not an integer") | |
281 | (sit-for 1)) | |
282 | ((< ts-count 1) | |
283 | ;; We need to call time-stamp-once at least once | |
284 | ;; to output any warnings about time-stamp not being active. | |
285 | (setq ts-count 1))) | |
286 | ;; Figure out what lines the end should be on. | |
3b5e21df GM |
287 | (if (stringp ts-format) |
288 | (let ((nl-start 0)) | |
289 | (while (string-match "\n" ts-format nl-start) | |
290 | (setq format-lines (1+ format-lines) nl-start (match-end 0))))) | |
55ac4d11 KH |
291 | (let ((nl-start 0)) |
292 | (while (string-match "\n" ts-end nl-start) | |
293 | (setq end-lines (1+ end-lines) nl-start (match-end 0)))) | |
294 | ;; Find overall what lines to look at | |
622b7ede RS |
295 | (save-excursion |
296 | (save-restriction | |
297 | (widen) | |
298 | (cond ((> line-limit 0) | |
299 | (goto-char (setq start (point-min))) | |
300 | (forward-line line-limit) | |
301 | (setq search-limit (point))) | |
f7769aa5 | 302 | ((< line-limit 0) |
622b7ede RS |
303 | (goto-char (setq search-limit (point-max))) |
304 | (forward-line line-limit) | |
f7769aa5 RS |
305 | (setq start (point))) |
306 | (t ;0 => no limit (use with care!) | |
307 | (setq start (point-min)) | |
55ac4d11 KH |
308 | (setq search-limit (point-max)))))) |
309 | (while (and start | |
310 | (< start search-limit) | |
311 | (> ts-count 0)) | |
312 | (setq start (time-stamp-once start search-limit ts-start ts-end | |
313 | ts-format format-lines end-lines)) | |
314 | (setq ts-count (1- ts-count)))) | |
315 | ;; be sure to return nil so can be used on write-file-hooks | |
316 | nil) | |
317 | ||
318 | (defun time-stamp-once (start search-limit ts-start ts-end | |
319 | ts-format format-lines end-lines) | |
320 | "Update one time-stamp. Internal routine called by \\[time-stamp]. | |
321 | Returns the end point, which is where `time-stamp' begins the next search." | |
322 | (let ((case-fold-search nil) | |
323 | (end nil) | |
324 | end-search-start | |
325 | (end-length nil)) | |
326 | (save-excursion | |
327 | (save-restriction | |
328 | (widen) | |
329 | ;; Find the location of the time stamp. | |
330 | (while (and (< (goto-char start) search-limit) | |
622b7ede | 331 | (not end) |
b4b33e01 | 332 | (re-search-forward ts-start search-limit 'move)) |
622b7ede | 333 | (setq start (point)) |
55ac4d11 KH |
334 | (if (not time-stamp-inserts-lines) |
335 | (forward-line format-lines)) | |
336 | (setq end-search-start (max start (point))) | |
337 | (if (= (forward-line end-lines) 0) | |
338 | (progn | |
339 | (and (bolp) (backward-char)) | |
340 | (let ((line-end (min (point) search-limit))) | |
341 | (if (>= line-end end-search-start) | |
342 | (progn | |
343 | (goto-char end-search-start) | |
344 | (if (re-search-forward ts-end line-end t) | |
345 | (progn | |
346 | (setq end (match-beginning 0)) | |
347 | (setq end-length (- (match-end 0) end)))))))))))) | |
b4b33e01 RS |
348 | (if end |
349 | (progn | |
350 | ;; do all warnings outside save-excursion | |
351 | (cond | |
352 | ((not time-stamp-active) | |
353 | (if time-stamp-warn-inactive | |
354 | ;; don't signal an error in a write-file-hook | |
355 | (progn | |
356 | (message "Warning: time-stamp-active is off; did not time-stamp buffer.") | |
357 | (sit-for 1)))) | |
358 | ((not (and (stringp ts-start) | |
359 | (stringp ts-end))) | |
360 | (message "time-stamp-start or time-stamp-end is not a string") | |
361 | (sit-for 1)) | |
362 | (t | |
363 | (let ((new-time-stamp (time-stamp-string ts-format))) | |
f7769aa5 RS |
364 | (if (and (stringp new-time-stamp) |
365 | (not (string-equal (buffer-substring start end) | |
366 | new-time-stamp))) | |
b4b33e01 RS |
367 | (save-excursion |
368 | (save-restriction | |
369 | (widen) | |
370 | (delete-region start end) | |
371 | (goto-char start) | |
372 | (insert-and-inherit new-time-stamp) | |
373 | (setq end (point)) | |
374 | ;; remove any tabs used to format time stamp | |
55ac4d11 KH |
375 | (if (search-backward "\t" start t) |
376 | (progn | |
377 | (untabify start end) | |
378 | (setq end (point)))))))))))) | |
379 | ;; return the location after this time stamp, if there was one | |
380 | (and end end-length | |
381 | (+ end end-length)))) | |
382 | ||
9565745a | 383 | |
b1defad2 RS |
384 | ;;;###autoload |
385 | (defun time-stamp-toggle-active (&optional arg) | |
03d7856a | 386 | "Toggle `time-stamp-active', setting whether \\[time-stamp] updates a buffer. |
b1defad2 RS |
387 | With arg, turn time stamping on if and only if arg is positive." |
388 | (interactive "P") | |
389 | (setq time-stamp-active | |
390 | (if (null arg) | |
391 | (not time-stamp-active) | |
392 | (> (prefix-numeric-value arg) 0))) | |
55ac4d11 KH |
393 | (message "time-stamp is now %s." (if time-stamp-active "active" "off"))) |
394 | ||
395 | ||
396 | (defun time-stamp-string (&optional ts-format) | |
397 | "Generate the new string to be inserted by \\[time-stamp]. | |
e7b0deaf EZ |
398 | Optionally use format TS-FORMAT instead of `time-stamp-format' to |
399 | format the string." | |
55ac4d11 KH |
400 | (or ts-format |
401 | (setq ts-format time-stamp-format)) | |
402 | (if (stringp ts-format) | |
403 | (if (stringp time-stamp-time-zone) | |
404 | (let ((ts-real-time-zone (getenv "TZ"))) | |
405 | (unwind-protect | |
406 | (progn | |
407 | (setenv "TZ" time-stamp-time-zone) | |
408 | (format-time-string | |
409 | (time-stamp-string-preprocess ts-format))) | |
410 | (setenv "TZ" ts-real-time-zone))) | |
411 | (format-time-string | |
412 | (time-stamp-string-preprocess ts-format))) | |
413 | ;; handle version 1 compatibility | |
414 | (cond ((or (eq time-stamp-old-format-warn 'error) | |
415 | (and (eq time-stamp-old-format-warn 'ask) | |
416 | (not (y-or-n-p "Use non-string time-stamp-format? ")))) | |
417 | (message "Warning: no time-stamp: time-stamp-format not a string") | |
418 | (sit-for 1) | |
419 | nil) | |
420 | (t | |
421 | (cond ((eq time-stamp-old-format-warn 'warn) | |
422 | (message "Obsolescent time-stamp-format type; should be string") | |
423 | (sit-for 1))) | |
424 | (time-stamp-fconcat ts-format " "))))) | |
03d7856a | 425 | |
6946129c RS |
426 | (defconst time-stamp-no-file "(no file)" |
427 | "String to use when the buffer is not associated with a file.") | |
428 | ||
fd72ddf6 RS |
429 | ;;; time-stamp is transitioning to using the new, expanded capabilities |
430 | ;;; of format-time-string. During the process, this function implements | |
431 | ;;; intermediate, compatible formats and complains about old, soon to | |
432 | ;;; be unsupported, formats. This function will get a lot (a LOT) shorter | |
433 | ;;; when the transition is complete and we can just pass most things | |
434 | ;;; straight through to format-time-string. | |
435 | ;;; At all times, all the formats recommended in the doc string | |
436 | ;;; of time-stamp-format will work not only in the current version of | |
437 | ;;; Emacs, but in all versions that have been released within the past | |
438 | ;;; two years. | |
439 | ;;; The : modifier is a temporary conversion feature used to resolve | |
440 | ;;; ambiguous formats--formats that are changing (over time) incompatibly. | |
441 | (defun time-stamp-string-preprocess (format &optional time) | |
22453b51 DL |
442 | "Use a FORMAT to format date, time, file, and user information. |
443 | Optional second argument TIME is only for testing. | |
444 | Implements non-time extensions to `format-time-string' | |
445 | and all time-stamp-format compatibility." | |
fd72ddf6 RS |
446 | (let ((fmt-len (length format)) |
447 | (ind 0) | |
448 | cur-char | |
449 | (prev-char nil) | |
450 | (result "") | |
fd72ddf6 RS |
451 | field-width |
452 | field-result | |
453 | alt-form change-case require-padding | |
454 | (paren-level 0)) | |
455 | (while (< ind fmt-len) | |
456 | (setq cur-char (aref format ind)) | |
457 | (setq | |
458 | result | |
459 | (concat result | |
460 | (cond | |
461 | ((eq cur-char ?%) | |
462 | ;; eat any additional args to allow for future expansion | |
15ceaba4 | 463 | (setq alt-form nil change-case nil require-padding nil field-width "") |
fd72ddf6 RS |
464 | (while (progn |
465 | (setq ind (1+ ind)) | |
466 | (setq cur-char (if (< ind fmt-len) | |
467 | (aref format ind) | |
468 | ?\0)) | |
469 | (or (eq ?. cur-char) | |
470 | (eq ?, cur-char) (eq ?: cur-char) (eq ?@ cur-char) | |
22453b51 | 471 | (eq ?- cur-char) (eq ?+ cur-char) (eq ?_ cur-char) |
fd72ddf6 RS |
472 | (eq ?\ cur-char) (eq ?# cur-char) (eq ?^ cur-char) |
473 | (and (eq ?\( cur-char) | |
474 | (not (eq prev-char ?\\)) | |
475 | (setq paren-level (1+ paren-level))) | |
476 | (if (and (eq ?\) cur-char) | |
477 | (not (eq prev-char ?\\)) | |
478 | (> paren-level 0)) | |
479 | (setq paren-level (1- paren-level)) | |
480 | (and (> paren-level 0) | |
15ceaba4 KH |
481 | (< ind fmt-len))) |
482 | (if (and (<= ?0 cur-char) (>= ?9 cur-char)) | |
483 | ;; get format width | |
484 | (let ((field-index ind)) | |
485 | (while (progn | |
486 | (setq ind (1+ ind)) | |
487 | (setq cur-char (if (< ind fmt-len) | |
488 | (aref format ind) | |
489 | ?\0)) | |
490 | (and (<= ?0 cur-char) (>= ?9 cur-char)))) | |
491 | (setq field-width (substring format field-index ind)) | |
492 | (setq ind (1- ind)) | |
493 | t)))) | |
fd72ddf6 RS |
494 | (setq prev-char cur-char) |
495 | ;; some characters we actually use | |
496 | (cond ((eq cur-char ?:) | |
497 | (setq alt-form t)) | |
498 | ((eq cur-char ?#) | |
499 | (setq change-case t)))) | |
fd72ddf6 RS |
500 | (setq field-result |
501 | (cond | |
502 | ((eq cur-char ?%) | |
55ac4d11 | 503 | "%%") |
fd72ddf6 RS |
504 | ((eq cur-char ?a) ;day of week |
505 | (if change-case | |
506 | (format-time-string "%#A" time) | |
507 | (or alt-form (not (string-equal field-width "")) | |
508 | (time-stamp-conv-warn "%a" "%:a")) | |
509 | (if (and alt-form (not (string-equal field-width ""))) | |
510 | "" ;discourage "%:3a" | |
511 | (format-time-string "%A" time)))) | |
512 | ((eq cur-char ?A) | |
513 | (if alt-form | |
514 | (format-time-string "%A" time) | |
515 | (or change-case (not (string-equal field-width "")) | |
516 | (time-stamp-conv-warn "%A" "%#A")) | |
517 | (format-time-string "%#A" time))) | |
518 | ((eq cur-char ?b) ;month name | |
519 | (if change-case | |
520 | (format-time-string "%#B" time) | |
521 | (or alt-form (not (string-equal field-width "")) | |
522 | (time-stamp-conv-warn "%b" "%:b")) | |
523 | (if (and alt-form (not (string-equal field-width ""))) | |
524 | "" ;discourage "%:3b" | |
525 | (format-time-string "%B" time)))) | |
526 | ((eq cur-char ?B) | |
527 | (if alt-form | |
528 | (format-time-string "%B" time) | |
529 | (or change-case (not (string-equal field-width "")) | |
530 | (time-stamp-conv-warn "%B" "%#B")) | |
531 | (format-time-string "%#B" time))) | |
532 | ((eq cur-char ?d) ;day of month, 1-31 | |
fdac7fba | 533 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 | 534 | ((eq cur-char ?H) ;hour, 0-23 |
fdac7fba | 535 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 | 536 | ((eq cur-char ?I) ;hour, 1-12 |
fdac7fba | 537 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 | 538 | ((eq cur-char ?m) ;month number, 1-12 |
fdac7fba | 539 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 | 540 | ((eq cur-char ?M) ;minute, 0-59 |
fdac7fba | 541 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 RS |
542 | ((eq cur-char ?p) ;am or pm |
543 | (or change-case | |
544 | (time-stamp-conv-warn "%p" "%#p")) | |
545 | (format-time-string "%#p" time)) | |
546 | ((eq cur-char ?P) ;AM or PM | |
547 | (format-time-string "%p" time)) | |
548 | ((eq cur-char ?S) ;seconds, 00-60 | |
fdac7fba | 549 | (time-stamp-do-number cur-char alt-form field-width time)) |
fd72ddf6 RS |
550 | ((eq cur-char ?w) ;weekday number, Sunday is 0 |
551 | (format-time-string "%w" time)) | |
552 | ((eq cur-char ?y) ;year | |
553 | (or alt-form (not (string-equal field-width "")) | |
554 | (time-stamp-conv-warn "%y" "%:y")) | |
d09b9dbd | 555 | (string-to-int (format-time-string "%Y" time))) |
fd72ddf6 | 556 | ((eq cur-char ?Y) ;4-digit year, new style |
d09b9dbd | 557 | (string-to-int (format-time-string "%Y" time))) |
fd72ddf6 RS |
558 | ((eq cur-char ?z) ;time zone lower case |
559 | (if change-case | |
560 | "" ;discourage %z variations | |
561 | (format-time-string "%#Z" time))) | |
562 | ((eq cur-char ?Z) | |
563 | (if change-case | |
564 | (format-time-string "%#Z" time) | |
565 | (format-time-string "%Z" time))) | |
566 | ((eq cur-char ?f) ;buffer-file-name, base name only | |
567 | (if buffer-file-name | |
568 | (file-name-nondirectory buffer-file-name) | |
569 | time-stamp-no-file)) | |
570 | ((eq cur-char ?F) ;buffer-file-name, full path | |
571 | (or buffer-file-name | |
572 | time-stamp-no-file)) | |
573 | ((eq cur-char ?s) ;system name | |
574 | (system-name)) | |
575 | ((eq cur-char ?u) ;user name | |
576 | (user-login-name)) | |
2c8d5749 RS |
577 | ((eq cur-char ?U) ;user full name |
578 | (user-full-name)) | |
55ac4d11 KH |
579 | ((eq cur-char ?l) ;logname (undocumented user name alt) |
580 | (user-login-name)) | |
581 | ((eq cur-char ?L) ;(undocumented alt user full name) | |
582 | (user-full-name)) | |
fd72ddf6 RS |
583 | ((eq cur-char ?h) ;mail host name |
584 | (time-stamp-mail-host-name)) | |
585 | )) | |
b6735035 GM |
586 | (let ((padded-result |
587 | (format (format "%%%s%c" | |
588 | field-width | |
589 | (if (numberp field-result) ?d ?s)) | |
590 | (or field-result "")))) | |
591 | (let* ((initial-length (length padded-result)) | |
592 | (desired-length (if (string-equal field-width "") | |
593 | initial-length | |
594 | (string-to-int field-width)))) | |
595 | (if (> initial-length desired-length) | |
596 | ;; truncate strings on right, years on left | |
597 | (if (stringp field-result) | |
598 | (substring padded-result 0 desired-length) | |
599 | (if (eq cur-char ?y) | |
600 | (substring padded-result (- desired-length)) | |
601 | padded-result)) ;non-year numbers don't truncate | |
602 | padded-result)))) | |
fd72ddf6 RS |
603 | (t |
604 | (char-to-string cur-char))))) | |
605 | (setq ind (1+ ind))) | |
606 | result)) | |
607 | ||
fdac7fba | 608 | (defun time-stamp-do-number (format-char alt-form field-width time) |
22453b51 DL |
609 | "Handle compatible FORMAT-CHAR where only default width/padding will change. |
610 | ALT-FORM is whether `#' specified. FIELD-WIDTH is the string | |
611 | width specification or \"\". TIME is the time to convert." | |
fd72ddf6 RS |
612 | (let ((format-string (concat "%" (char-to-string format-char)))) |
613 | (and (not alt-form) (string-equal field-width "") | |
614 | (time-stamp-conv-warn format-string | |
615 | (format "%%:%c" format-char))) | |
616 | (if (and alt-form (not (string-equal field-width ""))) | |
617 | "" ;discourage "%:2d" and the like | |
618 | (string-to-int (format-time-string format-string time))))) | |
619 | ||
620 | (defvar time-stamp-conversion-warn t | |
3b5e21df GM |
621 | "Warn about soon-to-be-unsupported forms in `time-stamp-format'. |
622 | If nil, these warnings are disabled, which would be a bad idea! | |
fd72ddf6 RS |
623 | You really need to update your files instead. |
624 | ||
625 | The new formats will work with old versions of Emacs. | |
55ac4d11 KH |
626 | New formats are being recommended now to allow `time-stamp-format' |
627 | to change in the future to be compatible with `format-time-string'. | |
fd72ddf6 RS |
628 | The new forms being recommended now will continue to work then.") |
629 | ||
630 | ||
631 | (defun time-stamp-conv-warn (old-form new-form) | |
22453b51 | 632 | "Display a warning about a soon-to-be-obsolete format." |
fd72ddf6 RS |
633 | (cond |
634 | (time-stamp-conversion-warn | |
635 | (save-excursion | |
636 | (set-buffer (get-buffer-create "*Time-stamp-compatibility*")) | |
637 | (goto-char (point-max)) | |
638 | (if (bobp) | |
639 | (progn | |
640 | (insert | |
641 | "The formats recognized in time-stamp-format will change in a future release\n" | |
642 | "to be compatible with the new, expanded format-time-string function.\n\n" | |
643 | "The following obsolescent time-stamp-format construct(s) were found:\n\n"))) | |
644 | (insert "\"" old-form "\" -- use " new-form "\n")) | |
645 | (display-buffer "*Time-stamp-compatibility*")))) | |
95772e85 | 646 | |
fd72ddf6 | 647 | |
b1defad2 | 648 | |
b1defad2 RS |
649 | (defun time-stamp-mail-host-name () |
650 | "Return the name of the host where the user receives mail. | |
651 | This is the value of `mail-host-address' if bound and a string, | |
55ac4d11 | 652 | otherwise the value of the function `system-name'." |
b1defad2 RS |
653 | (or (and (boundp 'mail-host-address) |
654 | (stringp mail-host-address) | |
655 | mail-host-address) | |
b1defad2 RS |
656 | (system-name))) |
657 | ||
658 | ;;; the rest of this file is for version 1 compatibility | |
9565745a RS |
659 | |
660 | (defun time-stamp-fconcat (list sep) | |
e1f40b28 | 661 | "Similar to (mapconcat 'funcall LIST SEP) but LIST allows literals. |
9565745a RS |
662 | If an element of LIST is a symbol, it is funcalled to get the string to use; |
663 | the separator SEP is used between two strings obtained by funcalling a | |
664 | symbol. Otherwise the element itself is inserted; no separator is used | |
665 | around literals." | |
666 | (let ((return-string "") | |
667 | (insert-sep-p nil)) | |
668 | (while list | |
669 | (cond ((symbolp (car list)) | |
670 | (if insert-sep-p | |
671 | (setq return-string (concat return-string sep))) | |
672 | (setq return-string (concat return-string (funcall (car list)))) | |
673 | (setq insert-sep-p t)) | |
674 | (t | |
675 | (setq return-string (concat return-string (car list))) | |
676 | (setq insert-sep-p nil))) | |
677 | (setq list (cdr list))) | |
678 | return-string)) | |
679 | ||
03d7856a | 680 | ;;; Some functions used in time-stamp-format |
9565745a RS |
681 | |
682 | ;;; Could generate most of a message-id with | |
e1f40b28 | 683 | ;;; '(time-stamp-yymmdd "" time-stamp-hhmm "@" time-stamp-mail-host-name) |
9565745a | 684 | |
9565745a RS |
685 | ;;; pretty form, suitable for a title page |
686 | ||
687 | (defun time-stamp-month-dd-yyyy () | |
e1f40b28 | 688 | "Return the current date as a string in \"Month DD, YYYY\" form." |
b8944601 | 689 | (format-time-string "%B %e, %Y")) |
9565745a | 690 | |
b24173f7 RS |
691 | (defun time-stamp-dd/mm/yyyy () |
692 | "Return the current date as a string in \"DD/MM/YYYY\" form." | |
b8944601 | 693 | (format-time-string "%d/%m/%Y")) |
b24173f7 | 694 | |
9565745a RS |
695 | ;;; same as __DATE__ in ANSI C |
696 | ||
697 | (defun time-stamp-mon-dd-yyyy () | |
e1f40b28 RS |
698 | "Return the current date as a string in \"Mon DD YYYY\" form. |
699 | The first character of DD is space if the value is less than 10." | |
b8944601 | 700 | (format-time-string "%b %d %Y")) |
9565745a RS |
701 | |
702 | ;;; RFC 822 date | |
703 | ||
704 | (defun time-stamp-dd-mon-yy () | |
e1f40b28 | 705 | "Return the current date as a string in \"DD Mon YY\" form." |
b8944601 | 706 | (format-time-string "%d %b %y")) |
9565745a RS |
707 | |
708 | ;;; RCS 3 date | |
709 | ||
710 | (defun time-stamp-yy/mm/dd () | |
e1f40b28 | 711 | "Return the current date as a string in \"YY/MM/DD\" form." |
b8944601 | 712 | (format-time-string "%y/%m/%d")) |
9565745a RS |
713 | |
714 | ;;; RCS 5 date | |
715 | ||
716 | (defun time-stamp-yyyy/mm/dd () | |
e1f40b28 | 717 | "Return the current date as a string in \"YYYY/MM/DD\" form." |
b8944601 | 718 | (format-time-string "%Y/%m/%d")) |
9565745a | 719 | |
e1f40b28 | 720 | ;;; ISO 8601 date |
9565745a | 721 | |
e1f40b28 RS |
722 | (defun time-stamp-yyyy-mm-dd () |
723 | "Return the current date as a string in \"YYYY-MM-DD\" form." | |
b8944601 | 724 | (format-time-string "%Y-%m-%d")) |
9565745a | 725 | |
e1f40b28 RS |
726 | (defun time-stamp-yymmdd () |
727 | "Return the current date as a string in \"YYMMDD\" form." | |
b8944601 | 728 | (format-time-string "%y%m%d")) |
9565745a RS |
729 | |
730 | (defun time-stamp-hh:mm:ss () | |
e1f40b28 | 731 | "Return the current time as a string in \"HH:MM:SS\" form." |
b8944601 | 732 | (format-time-string "%T")) |
9565745a | 733 | |
9565745a | 734 | (defun time-stamp-hhmm () |
e1f40b28 | 735 | "Return the current time as a string in \"HHMM\" form." |
b8944601 | 736 | (format-time-string "%H%M")) |
9565745a RS |
737 | |
738 | (provide 'time-stamp) | |
739 | ||
740 | ;;; time-stamp.el ends here |