]> code.delx.au - gnu-emacs-elpa/blob - packages/epoch-view/epoch-view.el
Add f90-interface-browser.
[gnu-emacs-elpa] / packages / epoch-view / epoch-view.el
1 ;;; epoch-view.el --- Minor mode to visualize epoch timestamps
2
3 ;; Copyright (C) 2010
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
7 ;; Keywords: data, timestamp, epoch, unix
8 ;; Version: 0.0.1
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
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.
30
31 ;;; TODO:
32
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.
36
37 ;;; Code:
38
39 \f
40 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
41 ;;;; User Variables:
42
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"))
48 :group 'epoch-view)
49
50 (defvar epoch-view-font-lock-keywords
51 '(("\\<[0-9]\\{8,11\\}\\>"
52 (0 (epoch-view-render))))
53 "Font-lock keywords of epoch timestamps.")
54
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))))
60
61 (defun epoch-view--render-time (text)
62 "Render the time portion of an epoch match from TEXT."
63 (format-time-string
64 epoch-view-time-format
65 (seconds-to-time (car (read-from-string (concat text ".0"))))))
66
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))
70
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))
75
76 (font-lock-add-keywords nil epoch-view-font-lock-keywords))
77
78 (defun epoch-view-turn-off ()
79 "Turn off epoch-view-mode."
80 (font-lock-remove-keywords
81 nil
82 `(,@epoch-view-font-lock-keywords)))
83
84 ;;;###autoload
85 (define-minor-mode
86 epoch-view-mode
87 "Visualize epoch (Unix) timestamps."
88 :lighter " EpochVw"
89 (progn
90 (if epoch-view-mode
91 (epoch-view-turn-on)
92 (epoch-view-turn-off))
93 ;; Turn on font lock
94 (font-lock-mode 1)))
95
96 (provide 'epoch-view)
97
98 (run-hooks 'epoch-view-load-hook)
99
100 ;;; epoch-view.el ends here