Mercurial > emacs
comparison lisp/time-stamp.el @ 88155:d7ddb3e565de
sync with trunk
author | Henrik Enberg <henrik.enberg@telia.com> |
---|---|
date | Mon, 16 Jan 2006 00:03:54 +0000 |
parents | fcde0602da01 |
children |
comparison
equal
deleted
inserted
replaced
88154:8ce476d3ba36 | 88155:d7ddb3e565de |
---|---|
1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs | 1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs |
2 | 2 |
3 ;; Copyright 1989, 1993, 1994, 1995, 1997, 2000, 2001 | 3 ;; Copyright (C) 1989, 1993, 1994, 1995, 1997, 2000, 2001, 2002, 2003, |
4 ;;; Free Software Foundation, Inc. | 4 ;; 2004, 2005 Free Software Foundation, Inc. |
5 | 5 |
6 ;; This file is part of GNU Emacs. | 6 ;; This file is part of GNU Emacs. |
7 | 7 |
8 ;; Maintainer's Time-stamp: <2003-02-01 09:26:25 gildea> | 8 ;; Maintainer's Time-stamp: <2004-09-25 20:55:35 gildea> |
9 ;; Maintainer: Stephen Gildea <gildea@stop.mail-abuse.org> | 9 ;; Maintainer: Stephen Gildea <gildea@stop.mail-abuse.org> |
10 ;; Keywords: tools | 10 ;; Keywords: tools |
11 | 11 |
12 ;; GNU Emacs is free software; you can redistribute it and/or modify | 12 ;; GNU Emacs is free software; you can redistribute it and/or modify |
13 ;; it under the terms of the GNU General Public License as published by | 13 ;; it under the terms of the GNU General Public License as published by |
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | 19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
20 ;; GNU General Public License for more details. | 20 ;; GNU General Public License for more details. |
21 | 21 |
22 ;; You should have received a copy of the GNU General Public License | 22 ;; You should have received a copy of the GNU General Public License |
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the | 23 ;; along with GNU Emacs; see the file COPYING. If not, write to the |
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 ;; Boston, MA 02111-1307, USA. | 25 ;; Boston, MA 02110-1301, USA. |
26 | 26 |
27 ;;; Commentary: | 27 ;;; Commentary: |
28 | 28 |
29 ;; A template in a file can be updated with a new time stamp when | 29 ;; A template in a file can be updated with a new time stamp when |
30 ;; you save the file. For example: | 30 ;; you save the file. For example: |
31 ;; static char *ts = "sdmain.c Time-stamp: <2001-08-13 10:20:51 gildea>"; | 31 ;; static char *ts = "sdmain.c Time-stamp: <2001-08-13 10:20:51 gildea>"; |
32 ;; See the top of `time-stamp.el' for another example. | 32 ;; See the top of `time-stamp.el' for another example. |
33 | 33 |
34 ;; To use time-stamping, add this line to your .emacs file: | 34 ;; To use time-stamping, add this line to your .emacs file: |
35 ;; (add-hook 'write-file-hooks 'time-stamp) | 35 ;; (add-hook 'before-save-hook 'time-stamp) |
36 ;; Now any time-stamp templates in your files will be updated automatically. | 36 ;; Now any time-stamp templates in your files will be updated automatically. |
37 | 37 |
38 ;; See the documentation for the functions `time-stamp' | 38 ;; See the documentation for the functions `time-stamp' |
39 ;; and `time-stamp-toggle-active' for details. | 39 ;; and `time-stamp-toggle-active' for details. |
40 | 40 |
141 The patterns `time-stamp-start' and `time-stamp-end' must be found in | 141 The patterns `time-stamp-start' and `time-stamp-end' must be found in |
142 the first (last) `time-stamp-line-limit' lines of the file for the | 142 the first (last) `time-stamp-line-limit' lines of the file for the |
143 file to be time-stamped by \\[time-stamp]. A value of 0 searches the | 143 file to be time-stamped by \\[time-stamp]. A value of 0 searches the |
144 entire buffer (use with care). | 144 entire buffer (use with care). |
145 | 145 |
146 Do not change `time-stamp-line-limit', `time-stamp-start', or | 146 This value can also be set with the variable `time-stamp-pattern'. |
147 `time-stamp-end' for yourself or you will be incompatible | 147 |
148 with other people's files! If you must change them for some application, | 148 Do not change `time-stamp-line-limit', `time-stamp-start', |
149 do so in the local variables section of the time-stamped file itself.") | 149 `time-stamp-end', or `time-stamp-pattern' for yourself or you will be |
150 incompatible with other people's files! If you must change them for some | |
151 application, do so in the local variables section of the time-stamped file | |
152 itself.") | |
150 | 153 |
151 | 154 |
152 (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change! | 155 (defvar time-stamp-start "Time-stamp:[ \t]+\\\\?[\"<]+" ;Do not change! |
153 "Regexp after which the time stamp is written by \\[time-stamp]. | 156 "Regexp after which the time stamp is written by \\[time-stamp]. |
154 See also the variables `time-stamp-end' and `time-stamp-line-limit'. | 157 See also the variables `time-stamp-end' and `time-stamp-line-limit'. |
155 | 158 |
156 Do not change `time-stamp-line-limit', `time-stamp-start', or | 159 This value can also be set with the variable `time-stamp-pattern'. |
157 `time-stamp-end' for yourself or you will be incompatible | 160 |
158 with other people's files! If you must change them for some application, | 161 Do not change `time-stamp-line-limit', `time-stamp-start', |
159 do so in the local variables section of the time-stamped file itself.") | 162 `time-stamp-end', or `time-stamp-pattern' for yourself or you will be |
163 incompatible with other people's files! If you must change them for some | |
164 application, do so in the local variables section of the time-stamped file | |
165 itself.") | |
160 | 166 |
161 | 167 |
162 (defvar time-stamp-end "\\\\?[\">]" ;Do not change! | 168 (defvar time-stamp-end "\\\\?[\">]" ;Do not change! |
163 "Regexp marking the text after the time stamp. | 169 "Regexp marking the text after the time stamp. |
164 \\[time-stamp] deletes the text between the first match of `time-stamp-start' | 170 \\[time-stamp] deletes the text between the first match of `time-stamp-start' |
165 and the following match of `time-stamp-end', then writes the | 171 and the following match of `time-stamp-end', then writes the |
166 time stamp specified by `time-stamp-format' between them. | 172 time stamp specified by `time-stamp-format' between them. |
167 | 173 |
174 This value can also be set with the variable `time-stamp-pattern'. | |
175 | |
168 The end text normally starts on the same line as the start text ends, | 176 The end text normally starts on the same line as the start text ends, |
169 but if there are any newlines in `time-stamp-format', the same number | 177 but if there are any newlines in `time-stamp-format', the same number |
170 of newlines must separate the start and end. \\[time-stamp] tries | 178 of newlines must separate the start and end. \\[time-stamp] tries |
171 to not change the number of lines in the buffer. `time-stamp-inserts-lines' | 179 to not change the number of lines in the buffer. `time-stamp-inserts-lines' |
172 controls this behavior. | 180 controls this behavior. |
173 | 181 |
174 Do not change `time-stamp-line-limit', `time-stamp-start', `time-stamp-end', | 182 Do not change `time-stamp-start', `time-stamp-end', `time-stamp-pattern', |
175 or `time-stamp-inserts-lines' for yourself or you will be incompatible | 183 or `time-stamp-inserts-lines' for yourself or you will be incompatible |
176 with other people's files! If you must change them for some application, | 184 with other people's files! If you must change them for some application, |
177 do so in the local variables section of the time-stamped file itself.") | 185 do so in the local variables section of the time-stamped file itself.") |
178 | 186 |
179 | 187 |
229 \"-10/\" | 237 \"-10/\" |
230 \"-9/^Last modified: %%$\" | 238 \"-9/^Last modified: %%$\" |
231 \"@set Time-stamp: %:b %:d, %:y$\" | 239 \"@set Time-stamp: %:b %:d, %:y$\" |
232 \"newcommand{\\\\\\\\timestamp}{%%}\" | 240 \"newcommand{\\\\\\\\timestamp}{%%}\" |
233 | 241 |
234 Do not change `time-stamp-pattern' for yourself or you will be incompatible | 242 Do not change `time-stamp-pattern' `time-stamp-line-limit', |
235 with other people's files! Set it only in the local variables section | 243 `time-stamp-start', or `time-stamp-end' for yourself or you will be |
236 of the time-stamped file itself.") | 244 incompatible with other people's files! If you must change them for |
245 some application, do so only in the local variables section of the | |
246 time-stamped file itself.") | |
237 | 247 |
238 | 248 |
239 | 249 |
240 ;;;###autoload | 250 ;;;###autoload |
241 (defun time-stamp () | 251 (defun time-stamp () |
242 "Update the time stamp string(s) in the buffer. | 252 "Update the time stamp string(s) in the buffer. |
243 A template in a file can be automatically updated with a new time stamp | 253 A template in a file can be automatically updated with a new time stamp |
244 every time you save the file. Add this line to your .emacs file: | 254 every time you save the file. Add this line to your .emacs file: |
245 (add-hook 'write-file-hooks 'time-stamp) | 255 (add-hook 'before-save-hook 'time-stamp) |
256 or customize `before-save-hook' through Custom. | |
246 Normally the template must appear in the first 8 lines of a file and | 257 Normally the template must appear in the first 8 lines of a file and |
247 look like one of the following: | 258 look like one of the following: |
248 Time-stamp: <> | 259 Time-stamp: <> |
249 Time-stamp: \" \" | 260 Time-stamp: \" \" |
250 The time stamp is written between the brackets or quotes: | 261 The time stamp is written between the brackets or quotes: |
251 Time-stamp: <2001-02-18 10:20:51 gildea> | 262 Time-stamp: <2001-02-18 10:20:51 gildea> |
252 The time stamp is updated only if the variable `time-stamp-active' is non-nil. | 263 The time stamp is updated only if the variable `time-stamp-active' is non-nil. |
253 The format of the time stamp is set by the variable `time-stamp-format'. | 264 The format of the time stamp is set by the variable `time-stamp-pattern' or |
254 The variables `time-stamp-line-limit', `time-stamp-start', `time-stamp-end', | 265 `time-stamp-format'. The variables `time-stamp-pattern', |
255 `time-stamp-count', and `time-stamp-inserts-lines' control finding the | 266 `time-stamp-line-limit', `time-stamp-start', `time-stamp-end', |
256 template." | 267 `time-stamp-count', and `time-stamp-inserts-lines' control finding |
268 the template." | |
257 (interactive) | 269 (interactive) |
258 (let ((line-limit time-stamp-line-limit) | 270 (let ((line-limit time-stamp-line-limit) |
259 (ts-start time-stamp-start) | 271 (ts-start time-stamp-start) |
260 (ts-format time-stamp-format) | 272 (ts-format time-stamp-format) |
261 (ts-end time-stamp-end) | 273 (ts-end time-stamp-end) |
267 (if (stringp time-stamp-pattern) | 279 (if (stringp time-stamp-pattern) |
268 (progn | 280 (progn |
269 (string-match "\\`\\(\\(-?[0-9]+\\)/\\)?\\([^%]+\\)?\\(\\(%[-.,:@+_ #^()0-9]*[A-Za-z%][^%]*\\)*%[-.,:@+_ #^()0-9]*[A-Za-z%]\\)?\\([^%]+\\)?\\'" time-stamp-pattern) | 281 (string-match "\\`\\(\\(-?[0-9]+\\)/\\)?\\([^%]+\\)?\\(\\(%[-.,:@+_ #^()0-9]*[A-Za-z%][^%]*\\)*%[-.,:@+_ #^()0-9]*[A-Za-z%]\\)?\\([^%]+\\)?\\'" time-stamp-pattern) |
270 (and (match-beginning 2) | 282 (and (match-beginning 2) |
271 (setq line-limit | 283 (setq line-limit |
272 (string-to-int (match-string 2 time-stamp-pattern)))) | 284 (string-to-number (match-string 2 time-stamp-pattern)))) |
273 (and (match-beginning 3) | 285 (and (match-beginning 3) |
274 (setq ts-start (match-string 3 time-stamp-pattern))) | 286 (setq ts-start (match-string 3 time-stamp-pattern))) |
275 (and (match-beginning 4) | 287 (and (match-beginning 4) |
276 (not (string-equal (match-string 4 time-stamp-pattern) "%%")) | 288 (not (string-equal (match-string 4 time-stamp-pattern) "%%")) |
277 (setq ts-format (match-string 4 time-stamp-pattern))) | 289 (setq ts-format (match-string 4 time-stamp-pattern))) |
316 (< start search-limit) | 328 (< start search-limit) |
317 (> ts-count 0)) | 329 (> ts-count 0)) |
318 (setq start (time-stamp-once start search-limit ts-start ts-end | 330 (setq start (time-stamp-once start search-limit ts-start ts-end |
319 ts-format format-lines end-lines)) | 331 ts-format format-lines end-lines)) |
320 (setq ts-count (1- ts-count)))) | 332 (setq ts-count (1- ts-count)))) |
321 ;; be sure to return nil so can be used on write-file-hooks | |
322 nil) | 333 nil) |
323 | 334 |
324 (defun time-stamp-once (start search-limit ts-start ts-end | 335 (defun time-stamp-once (start search-limit ts-start ts-end |
325 ts-format format-lines end-lines) | 336 ts-format format-lines end-lines) |
326 "Update one time stamp. Internal routine called by \\[time-stamp]. | 337 "Update one time stamp. Internal routine called by \\[time-stamp]. |
556 ((eq cur-char ?w) ;weekday number, Sunday is 0 | 567 ((eq cur-char ?w) ;weekday number, Sunday is 0 |
557 (format-time-string "%w" time)) | 568 (format-time-string "%w" time)) |
558 ((eq cur-char ?y) ;year | 569 ((eq cur-char ?y) ;year |
559 (or alt-form (not (string-equal field-width "")) | 570 (or alt-form (not (string-equal field-width "")) |
560 (time-stamp-conv-warn "%y" "%:y")) | 571 (time-stamp-conv-warn "%y" "%:y")) |
561 (string-to-int (format-time-string "%Y" time))) | 572 (string-to-number (format-time-string "%Y" time))) |
562 ((eq cur-char ?Y) ;4-digit year, new style | 573 ((eq cur-char ?Y) ;4-digit year, new style |
563 (string-to-int (format-time-string "%Y" time))) | 574 (string-to-number (format-time-string "%Y" time))) |
564 ((eq cur-char ?z) ;time zone lower case | 575 ((eq cur-char ?z) ;time zone lower case |
565 (if change-case | 576 (if change-case |
566 "" ;discourage %z variations | 577 "" ;discourage %z variations |
567 (format-time-string "%#Z" time))) | 578 (format-time-string "%#Z" time))) |
568 ((eq cur-char ?Z) | 579 ((eq cur-char ?Z) |
586 (user-login-name)) | 597 (user-login-name)) |
587 ((eq cur-char ?L) ;(undocumented alt user full name) | 598 ((eq cur-char ?L) ;(undocumented alt user full name) |
588 (user-full-name)) | 599 (user-full-name)) |
589 ((eq cur-char ?h) ;mail host name | 600 ((eq cur-char ?h) ;mail host name |
590 (time-stamp-mail-host-name)) | 601 (time-stamp-mail-host-name)) |
602 ((eq cur-char ?q) ;(undocumented unqual hostname) | |
603 (let ((qualname (system-name))) | |
604 (if (string-match "\\." qualname) | |
605 (substring qualname 0 (match-beginning 0)) | |
606 qualname))) | |
607 ((eq cur-char ?Q) ;(undocumented fully-qualified host) | |
608 (system-name)) | |
591 )) | 609 )) |
592 (let ((padded-result | 610 (let ((padded-result |
593 (format (format "%%%s%c" | 611 (format (format "%%%s%c" |
594 field-width | 612 field-width |
595 (if (numberp field-result) ?d ?s)) | 613 (if (numberp field-result) ?d ?s)) |
596 (or field-result "")))) | 614 (or field-result "")))) |
597 (let* ((initial-length (length padded-result)) | 615 (let* ((initial-length (length padded-result)) |
598 (desired-length (if (string-equal field-width "") | 616 (desired-length (if (string-equal field-width "") |
599 initial-length | 617 initial-length |
600 (string-to-int field-width)))) | 618 (string-to-number field-width)))) |
601 (if (> initial-length desired-length) | 619 (if (> initial-length desired-length) |
602 ;; truncate strings on right, years on left | 620 ;; truncate strings on right, years on left |
603 (if (stringp field-result) | 621 (if (stringp field-result) |
604 (substring padded-result 0 desired-length) | 622 (substring padded-result 0 desired-length) |
605 (if (eq cur-char ?y) | 623 (if (eq cur-char ?y) |
619 (and (not alt-form) (string-equal field-width "") | 637 (and (not alt-form) (string-equal field-width "") |
620 (time-stamp-conv-warn format-string | 638 (time-stamp-conv-warn format-string |
621 (format "%%:%c" format-char))) | 639 (format "%%:%c" format-char))) |
622 (if (and alt-form (not (string-equal field-width ""))) | 640 (if (and alt-form (not (string-equal field-width ""))) |
623 "" ;discourage "%:2d" and the like | 641 "" ;discourage "%:2d" and the like |
624 (string-to-int (format-time-string format-string time))))) | 642 (string-to-number (format-time-string format-string time))))) |
625 | 643 |
626 (defvar time-stamp-conversion-warn t | 644 (defvar time-stamp-conversion-warn t |
627 "Warn about soon-to-be-unsupported forms in `time-stamp-format'. | 645 "Warn about soon-to-be-unsupported forms in `time-stamp-format'. |
628 If nil, these warnings are disabled, which would be a bad idea! | 646 If nil, these warnings are disabled, which would be a bad idea! |
629 You really need to update your files instead. | 647 You really need to update your files instead. |
758 "Return the current time as a string in \"HHMM\" form." | 776 "Return the current time as a string in \"HHMM\" form." |
759 (format-time-string "%H%M")) | 777 (format-time-string "%H%M")) |
760 | 778 |
761 (provide 'time-stamp) | 779 (provide 'time-stamp) |
762 | 780 |
781 ;;; arch-tag: 8a12c5c3-25d6-4a71-adc5-24b0e025a1e7 | |
763 ;;; time-stamp.el ends here | 782 ;;; time-stamp.el ends here |