time-stamp.el 27 KB

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