]> code.delx.au - gnu-emacs-elpa/blob - packages/epoch-view/epoch-view.el
Merge commit '0cda39255827f283e7578cd469ae42daad9556a2' from js2-mode
[gnu-emacs-elpa] / packages / epoch-view / epoch-view.el
1 ;;; epoch-view.el --- Minor mode to visualize epoch timestamps
2
3 ;; Copyright (C) 2010, 2012 Free Software Foundation, Inc.
4
5 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
6 ;; Keywords: data, timestamp, epoch, unix
7 ;; Version: 0.0.1
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Use like any other minor mode. You'll see tooltips with dates
27 ;; instead of Unix epoch times. This mode turns on font-lock and
28 ;; leaves it on forever. You may or may not like that.
29
30 ;;; TODO:
31
32 ;; Instead of letting font-lock-mode manage the `display' property,
33 ;; manage it ourselves so when multiple modes specify `display' it
34 ;; won't get wiped out when this mode doesn't need it anymore.
35
36 ;;; Code:
37
38 \f
39 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
40 ;;;; User Variables:
41
42 (defcustom epoch-view-time-format "%F %T"
43 "Format for time view. Same as `format-time-string'."
44 :type '(choice :tag "Time format"
45 (string :tag "Choose your own `format-time-string' format")
46 (const :tag "YYYY-MM-DD HH:MM:SS" "%F %T"))
47 :group 'epoch-view)
48
49 (defvar epoch-view-font-lock-keywords
50 '(("\\<[0-9]\\{8,11\\}\\>"
51 (0 (epoch-view-render))))
52 "Font-lock keywords of epoch timestamps.")
53
54 (defun epoch-view-render ()
55 "Render a epoch match."
56 (let ((text (match-string-no-properties 0)))
57 `(face font-lock-warning-face
58 display ,(epoch-view--render text))))
59
60 (defun epoch-view--render-time (text)
61 "Render the time portion of an epoch match from TEXT."
62 (format-time-string
63 epoch-view-time-format
64 (seconds-to-time (car (read-from-string (concat text ".0"))))))
65
66 (defun epoch-view--render (text)
67 "Render a epoch match from a number in TEXT, ending with TEXT."
68 (format "[%s] %s" (epoch-view--render-time text) text))
69
70 (defun epoch-view-turn-on ()
71 "Turn on epoch-view-mode."
72 (let ((props (make-local-variable 'font-lock-extra-managed-props)))
73 (add-to-list props 'display))
74
75 (font-lock-add-keywords nil epoch-view-font-lock-keywords))
76
77 (defun epoch-view-turn-off ()
78 "Turn off epoch-view-mode."
79 (font-lock-remove-keywords
80 nil
81 `(,@epoch-view-font-lock-keywords)))
82
83 ;;;###autoload
84 (define-minor-mode
85 epoch-view-mode
86 "Visualize epoch (Unix) timestamps."
87 :lighter " EpochVw"
88 (progn
89 (if epoch-view-mode
90 (epoch-view-turn-on)
91 (epoch-view-turn-off))
92 ;; Turn on font lock
93 (font-lock-mode 1)))
94
95 (provide 'epoch-view)
96
97 (run-hooks 'epoch-view-load-hook)
98
99 ;;; epoch-view.el ends here