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