Commit | Line | Data |
---|---|---|
9565745a RS |
1 | ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs |
2 | ||
3 | ;;; Copyright 1989, 1993 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu> | |
6 | ;; Time-stamp: <93/06/20 17:36:04 gildea> | |
7 | ;; Keywords: tools | |
8 | ||
9 | ;; This file is free software; you can redistribute it and/or modify | |
10 | ;; it under the terms of the GNU General Public License as published by | |
11 | ;; the Free Software Foundation; either version 2, or (at your option) | |
12 | ;; any later version. | |
13 | ||
14 | ;; This file is distributed in the hope that it will be useful, | |
15 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 | ;; GNU General Public License for more details. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
20 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | ||
23 | ;;; Commentary: | |
24 | ||
25 | ;;; If you put a time stamp template anywhere in the first 8 lines of a file, | |
26 | ;;; it can be updated every time you save the file. See the top of | |
27 | ;;; time-stamp.el for a sample. The template looks like one of the following: | |
28 | ;;; Time-stamp: <> | |
29 | ;;; Time-stamp: "" | |
30 | ;;; The time stamp is written between the brackets or quotes, resulting in | |
31 | ;;; Time-stamp: <93/06/18 10:26:51 gildea> | |
32 | ;;; Here is an example which puts the file name and time stamp in the binary: | |
33 | ;;; static char *time_stamp = "sdmain.c Time-stamp: <>"; | |
34 | ||
35 | ;;; To activate automatic time stamping, add this code to your .emacs file: | |
36 | ;;; | |
37 | ;;; (autoload 'time-stamp "time-stamp" "Update the time stamp in a buffer." t) | |
38 | ;;; (if (not (memq 'time-stamp write-file-hooks)) | |
39 | ;;; (setq write-file-hooks | |
40 | ;;; (cons 'time-stamp write-file-hooks))) | |
41 | ||
42 | ;;; Change Log: | |
43 | ||
44 | ;;; Originally based on the 19 Dec 88 version of | |
45 | ;;; date.el by John Sturdy <mcvax!harlqn.co.uk!jcgs@uunet.uu.net> | |
46 | ||
47 | ;;; Code: | |
48 | ||
49 | (defvar time-stamp-active t | |
50 | "*Non-nil to enable time-stamping of files. See the function time-stamp.") | |
51 | ||
52 | (defvar time-stamp-format | |
53 | '(time-stamp-yy/mm/dd time-stamp-hh:mm:ss user-login-name) | |
54 | "*A list of functions to call to generate the time stamp string. | |
55 | Each element of the list is called as a function and the results are | |
56 | concatenated together separated by spaces. Elements may also be strings, | |
57 | which are included verbatim. Spaces are not inserted around literal strings.") | |
58 | ||
59 | ;;; Do not change time-stamp-line-limit, time-stamp-start, or | |
60 | ;;; time-stamp-end in your .emacs or you will be incompatible | |
61 | ;;; with other people's files! If you must change them, | |
62 | ;;; do so only in the local variables section of the file itself. | |
63 | ||
64 | (defvar time-stamp-line-limit 8 ;Do not change! See comment above. | |
65 | "Number of lines at the beginning of a file that are searched. | |
66 | The patterns time-stamp-start and time-stamp-end must be found on one | |
67 | of the first time-stamp-line-limit lines of the file for the file to | |
68 | be time-stamped.") | |
69 | ||
70 | (defvar time-stamp-start "Time-stamp: \\\\?[\"<]+" ;Do not change! | |
71 | "Regexp after which the time stamp is written by \\[time-stamp]. | |
72 | See also the variables time-stamp-end and time-stamp-line-limit. | |
73 | ||
74 | Do not change time-stamp-line-limit, time-stamp-start, or | |
75 | time-stamp-end for yourself or you will be incompatible | |
76 | with other people's files! If you must change them for some application, | |
77 | do so in the local variables section of the time-stamped file itself.") | |
78 | ||
79 | ||
80 | (defvar time-stamp-end "\\\\?[\">]" ;Do not change! See comment above. | |
81 | "Regexp marking the text after the time stamp. | |
82 | \\[time-stamp] deletes the text between the first match of time-stamp-start | |
83 | \(which see) and the following match of time-stamp-end on the same line, | |
84 | then writes the time stamp specified by time-stamp-format between them.") | |
85 | ||
86 | (defun time-stamp () | |
87 | "Update the time stamp string in the buffer. | |
88 | Only does its thing if the variable time-stamp-active is non-nil. | |
89 | Typically used on write-file-hooks for automatic time-stamping. | |
90 | The format of the time stamp is determined by the variable | |
91 | time-stamp-format. The first time-stamp-line-limit lines of the | |
92 | buffer (normally 8) are searched for the time stamp template, | |
93 | and if it is found, a new time stamp is written into it." | |
94 | (interactive) | |
95 | (if time-stamp-active | |
96 | (let ((case-fold-search nil)) | |
97 | (if (and (stringp time-stamp-start) | |
98 | (stringp time-stamp-end)) | |
99 | (save-excursion | |
100 | (goto-char (point-min)) | |
101 | (if (re-search-forward time-stamp-start | |
102 | (save-excursion | |
103 | (forward-line time-stamp-line-limit) | |
104 | (point)) | |
105 | t) | |
106 | (let ((start (point))) | |
107 | (if (re-search-forward time-stamp-end | |
108 | (save-excursion (end-of-line) (point)) | |
109 | t) | |
110 | (let ((end (match-beginning 0))) | |
111 | (delete-region start end) | |
112 | (goto-char start) | |
113 | (insert (time-stamp-string)) | |
114 | (setq end (point)) | |
115 | ;; remove any tabs used to format the time stamp | |
116 | (goto-char start) | |
117 | (if (search-forward "\t" end t) | |
118 | (untabify start end))))))) | |
119 | ;; don't signal an error in a write-file-hook | |
120 | (message "time-stamp-start or time-stamp-end is not a string")))) | |
121 | ;; be sure to return nil so can be used on write-file-hooks | |
122 | nil) | |
123 | ||
124 | (defun time-stamp-string () | |
125 | "Generate the new string to be inserted by \\[time-stamp]." | |
126 | (time-stamp-fconcat time-stamp-format " ")) | |
127 | ||
128 | (defun time-stamp-fconcat (list sep) | |
129 | "Similar to (mapconcat 'funcall LIST SEP) but LIST can have literals. | |
130 | If an element of LIST is a symbol, it is funcalled to get the string to use; | |
131 | the separator SEP is used between two strings obtained by funcalling a | |
132 | symbol. Otherwise the element itself is inserted; no separator is used | |
133 | around literals." | |
134 | (let ((return-string "") | |
135 | (insert-sep-p nil)) | |
136 | (while list | |
137 | (cond ((symbolp (car list)) | |
138 | (if insert-sep-p | |
139 | (setq return-string (concat return-string sep))) | |
140 | (setq return-string (concat return-string (funcall (car list)))) | |
141 | (setq insert-sep-p t)) | |
142 | (t | |
143 | (setq return-string (concat return-string (car list))) | |
144 | (setq insert-sep-p nil))) | |
145 | (setq list (cdr list))) | |
146 | return-string)) | |
147 | ||
148 | ||
149 | (defconst time-stamp-month-numbers | |
150 | '(("Jan" . 1) ("Feb" . 2) ("Mar" . 3) ("Apr" . 4) ("May" . 5) ("Jun" . 6) | |
151 | ("Jul" . 7) ("Aug" . 8) ("Sep" . 9) ("Oct" . 10) ("Nov" . 11) ("Dec" . 12)) | |
152 | "Assoc list of months and their number.") | |
153 | ||
154 | (defconst time-stamp-month-full-names | |
155 | ["(zero)" "January" "February" "March" "April" "May" "June" | |
156 | "July" "August" "September" "October" "November" "December"]) | |
157 | ||
158 | (defvar time-stamp-mail-host nil | |
159 | "Name of the host where the user receives mail. | |
160 | See the function time-stamp-mail-host-name.") | |
161 | ||
162 | ;;; Some useful functions to use in time-stamp-format | |
163 | ||
164 | ;;; Could generate most of a message-id with | |
165 | ;;; '(yymmdd "" hhmm "@" mail-host-name) | |
166 | ||
167 | (defun time-stamp-mail-host-name () | |
168 | "Return the name of the host where the user receives mail. | |
169 | This is the value of time-stamp-mail-host if bound and a string, | |
170 | otherwise the value of the function system-name." | |
171 | (or (and (boundp 'time-stamp-mail-host) | |
172 | (stringp time-stamp-mail-host) | |
173 | time-stamp-mail-host) | |
174 | (system-name))) | |
175 | ||
176 | (defun time-stamp-current-year () | |
177 | "Return the current year as a four-character string." | |
178 | (substring (current-time-string) -4)) | |
179 | ||
180 | ;;; pretty form, suitable for a title page | |
181 | ||
182 | (defun time-stamp-month-dd-yyyy () | |
183 | "Return the current date as a string in \"Month dd, yyyy\" form." | |
184 | (let ((date (current-time-string))) | |
185 | (format "%s %02d, %s" | |
186 | (aref time-stamp-month-full-names | |
187 | (cdr (assoc (substring date 4 7) time-stamp-month-numbers))) | |
188 | (string-to-int (substring date 8 10)) | |
189 | (substring date -4)))) | |
190 | ||
191 | ;;; same as __DATE__ in ANSI C | |
192 | ||
193 | (defun time-stamp-mon-dd-yyyy () | |
194 | "Return the current date as a string in \"Mon dd yyyy\" form. | |
195 | The first character of dd is Space if the value is less than 10." | |
196 | (let ((date (current-time-string))) | |
197 | (format "%s %2d %s" | |
198 | (substring date 4 7) | |
199 | (string-to-int (substring date 8 10)) | |
200 | (substring date -4)))) | |
201 | ||
202 | ;;; RFC 822 date | |
203 | ||
204 | (defun time-stamp-dd-mon-yy () | |
205 | "Return the current date as a string in \"dd Mon yy\" form." | |
206 | (let ((date (current-time-string))) | |
207 | (format "%02d %s %s" | |
208 | (string-to-int (substring date 8 10)) | |
209 | (substring date 4 7) | |
210 | (substring date -2)))) | |
211 | ||
212 | ;;; RCS 3 date | |
213 | ||
214 | (defun time-stamp-yy/mm/dd () | |
215 | "Return the current date as a string in \"yy/mm/dd\" form." | |
216 | (let ((date (current-time-string))) | |
217 | (format "%s/%02d/%02d" | |
218 | (substring date -2) | |
219 | (cdr (assoc (substring date 4 7) time-stamp-month-numbers)) | |
220 | (string-to-int (substring date 8 10))))) | |
221 | ||
222 | ;;; RCS 5 date | |
223 | ||
224 | (defun time-stamp-yyyy/mm/dd () | |
225 | "Return the current date as a string in \"yyyy/mm/dd\" form." | |
226 | (let ((date (current-time-string))) | |
227 | (format "%s/%02d/%02d" | |
228 | (substring date -4) | |
229 | (cdr (assoc (substring date 4 7) time-stamp-month-numbers)) | |
230 | (string-to-int (substring date 8 10))))) | |
231 | ||
232 | (defun time-stamp-yymmdd () | |
233 | "Return the current date as a string in \"yymmdd\" form." | |
234 | (let ((date (current-time-string))) | |
235 | (format "%s%02d%02d" | |
236 | (substring date -2) | |
237 | (cdr (assoc (substring date 4 7) time-stamp-month-numbers)) | |
238 | (string-to-int (substring date 8 10))))) | |
239 | ||
240 | (defun time-stamp-dd/mm/yy () | |
241 | "Return the current date as a string in \"dd/mm/yy\" form." | |
242 | (let ((date (current-time-string))) | |
243 | (format "%02d/%02d/%s" | |
244 | (string-to-int (substring date 8 10)) | |
245 | (cdr (assoc (substring date 4 7) time-stamp-month-numbers)) | |
246 | (substring date -2)))) | |
247 | ||
248 | (defun time-stamp-mm/dd/yy () | |
249 | "Return the current date as a string in \"mm/dd/yy\" form." | |
250 | (let ((date (current-time-string))) | |
251 | (format "%02d/%02d/%s" | |
252 | (cdr (assoc (substring date 4 7) time-stamp-month-numbers)) | |
253 | (string-to-int (substring date 8 10)) | |
254 | (substring date -2)))) | |
255 | ||
256 | (defun time-stamp-hh:mm:ss () | |
257 | "Return the current time as a string in \"hh:mm:ss\" form." | |
258 | (substring (current-time-string) 11 19)) | |
259 | ||
260 | (defun time-stamp-hh:mm () | |
261 | "Return the current time as a string in \"hh:mm\" form." | |
262 | (substring (current-time-string) 11 16)) | |
263 | ||
264 | (defun time-stamp-hhmm () | |
265 | "Return the current time as a string in \"hhmm\" form." | |
266 | (let ((date (current-time-string))) | |
267 | (concat (substring date 11 13) | |
268 | (substring date 14 16)))) | |
269 | ||
270 | (provide 'time-stamp) | |
271 | ||
272 | ;;; time-stamp.el ends here |