(command-line-1): Display a message in the echo area.
[bpt/emacs.git] / lisp / time-stamp.el
1 ;;; time-stamp.el --- Maintain last change time stamps in files edited by Emacs
2 ;;; Copyright 1989, 1993 Free Software Foundation, Inc.
3
4 ;; Maintainer: Stephen Gildea <gildea@lcs.mit.edu>
5 ;; Time-stamp: <93/06/20 17:36:04 gildea>
6 ;; Keywords: tools
7
8 ;; This file is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21
22 ;;; Commentary:
23
24 ;;; If you put a time stamp template anywhere in the first 8 lines of a file,
25 ;;; it can be updated every time you save the file. See the top of
26 ;;; time-stamp.el for a sample. The template looks like one of the following:
27 ;;; Time-stamp: <>
28 ;;; Time-stamp: " "
29 ;;; The time stamp is written between the brackets or quotes, resulting in
30 ;;; Time-stamp: <93/06/18 10:26:51 gildea>
31 ;;; Here is an example which puts the file name and time stamp in the binary:
32 ;;; static char *time_stamp = "sdmain.c Time-stamp: <>";
33
34 ;;; To activate automatic time stamping, add this code to your .emacs file:
35 ;;;
36 ;;; (autoload 'time-stamp "time-stamp" "Update the time stamp in a buffer." t)
37 ;;; (if (not (memq 'time-stamp write-file-hooks))
38 ;;; (setq write-file-hooks
39 ;;; (cons 'time-stamp write-file-hooks)))
40
41 ;;; Change Log:
42
43 ;;; Originally based on the 19 Dec 88 version of
44 ;;; date.el by John Sturdy <mcvax!harlqn.co.uk!jcgs@uunet.uu.net>
45
46 ;;; Code:
47
48 (defvar time-stamp-active t
49 "*Non-nil to enable time-stamping of files. See the function time-stamp.")
50
51 (defvar time-stamp-format
52 '(time-stamp-yy/mm/dd time-stamp-hh:mm:ss user-login-name)
53 "*A list of functions to call to generate the time stamp string.
54 Each element of the list is called as a function and the results are
55 concatenated together separated by spaces. Elements may also be strings,
56 which are included verbatim. Spaces are not inserted around literal strings.")
57
58 ;;; Do not change time-stamp-line-limit, time-stamp-start, or
59 ;;; time-stamp-end in your .emacs or you will be incompatible
60 ;;; with other people's files! If you must change them,
61 ;;; do so only in the local variables section of the file itself.
62
63 (defvar time-stamp-line-limit 8 ;Do not change! See comment above.
64 "Number of lines at the beginning of a file that are searched.
65 The patterns time-stamp-start and time-stamp-end must be found on one
66 of the first time-stamp-line-limit lines of the file for the file to
67 be time-stamped.")
68
69 (defvar time-stamp-start "Time-stamp: \\\\?[\"<]+" ;Do not change!
70 "Regexp after which the time stamp is written by \\[time-stamp].
71 See also the variables time-stamp-end and time-stamp-line-limit.
72
73 Do not change time-stamp-line-limit, time-stamp-start, or
74 time-stamp-end for yourself or you will be incompatible
75 with other people's files! If you must change them for some application,
76 do so in the local variables section of the time-stamped file itself.")
77
78
79 (defvar time-stamp-end "\\\\?[\">]" ;Do not change! See comment above.
80 "Regexp marking the text after the time stamp.
81 \\[time-stamp] deletes the text between the first match of time-stamp-start
82 \(which see) and the following match of time-stamp-end on the same line,
83 then writes the time stamp specified by time-stamp-format between them.")
84
85 ;;;###autoload
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