1 ;;; epoch-view.el --- Minor mode to visualize epoch timestamps
4 ;; Free Software Foundation, Inc.
6 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: data, timestamp, epoch, unix
10 ;; This file is part of GNU Emacs.
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
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Use like any other minor mode. You'll see tooltips with dates
28 ;; instead of Unix epoch times. This mode turns on font-lock and
29 ;; leaves it on forever. You may or may not like that.
33 ;; Instead of letting font-lock-mode manage the `display' property,
34 ;; manage it ourselves so when multiple modes specify `display' it
35 ;; won't get wiped out when this mode doesn't need it anymore.
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 (defcustom epoch-view-time-format "%F %T"
44 "Format for time view. Same as `format-time-string'."
45 :type '(choice :tag "Time format"
46 (string :tag "Choose your own `format-time-string' format")
47 (const :tag "YYYY-MM-DD HH:MM:SS" "%F %T"))
50 (defvar epoch-view-font-lock-keywords
51 '(("\\<[0-9]\\{8,11\\}\\>"
52 (0 (epoch-view-render))))
53 "Font-lock keywords of epoch timestamps.")
55 (defun epoch-view-render ()
56 "Render a epoch match."
57 (let ((text (match-string-no-properties 0)))
58 `(face font-lock-warning-face
59 display ,(epoch-view--render text))))
61 (defun epoch-view--render-time (text)
62 "Render the time portion of an epoch match from TEXT."
64 epoch-view-time-format
65 (seconds-to-time (car (read-from-string (concat text ".0"))))))
67 (defun epoch-view--render (text)
68 "Render a epoch match from a number in TEXT, ending with TEXT."
69 (format "[%s] %s" (epoch-view--render-time text) text))
71 (defun epoch-view-turn-on ()
72 "Turn on epoch-view-mode."
73 (let ((props (make-local-variable 'font-lock-extra-managed-props)))
74 (add-to-list props 'display))
76 (font-lock-add-keywords nil epoch-view-font-lock-keywords))
78 (defun epoch-view-turn-off ()
79 "Turn off epoch-view-mode."
80 (font-lock-remove-keywords
82 `(,@epoch-view-font-lock-keywords)))
87 "Visualize epoch (Unix) timestamps."
92 (epoch-view-turn-off))
98 (run-hooks 'epoch-view-load-hook)
100 ;;; epoch-view.el ends here