Use a nicer, cleaner syntax calling #'make-glyph
[emchat] / emchat-history.el
1 ;;; emchat-history.el --- Mode for viewing history in EMchat and its interface.
2
3 ;; Copyright (C) 2005 - 2011 Steve Youngs, Alexey Mikhailov
4
5 ;; Author:        Alexey Mikhailov <karma@sxemacs.org>
6 ;; Maintainer:    Alexey Mikhailov <karma@sxemacs.org>
7 ;; Created:       2005-04-19
8 ;; Homepage:      http://www.emchat.org/
9 ;; Keywords:      comm ICQ history
10
11 ;; This file is part of EMchat.
12
13 ;; Redistribution and use in source and binary forms, with or without
14 ;; modification, are permitted provided that the following conditions
15 ;; are met:
16 ;;
17 ;; 1. Redistributions of source code must retain the above copyright
18 ;;    notice, this list of conditions and the following disclaimer.
19 ;;
20 ;; 2. Redistributions in binary form must reproduce the above copyright
21 ;;    notice, this list of conditions and the following disclaimer in the
22 ;;    documentation and/or other materials provided with the distribution.
23 ;;
24 ;; 3. Neither the name of the author nor the names of any contributors
25 ;;    may be used to endorse or promote products derived from this
26 ;;    software without specific prior written permission.
27 ;;
28 ;; THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
29 ;; IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 ;; WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 ;; DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 ;; FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
33 ;; CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34 ;; SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
35 ;; BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
36 ;; WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
37 ;; OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
38 ;; IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39
40 ;;; Commentary:
41 ;;
42 ;; Provides mode for viewing person history and interface for EMchat.
43 ;; To start using it, add this to your ~/.sxemacs/init.el...
44 ;;
45 ;;    (setq emchat-history-enabled-flag t)
46
47 (eval-and-compile
48   (require 'outline)
49   (require 'emchat)
50   (require 'emchat-log)
51   (require 'font-lock))
52
53 (defvar emchat-history-old-window-config nil)
54
55 (defun emchat-history (&optional alias)
56   "Show history log for ALIAS person."
57   (interactive)
58   (let* ((alias (or alias
59                     (emchat-completing-read "Show history for: "
60                                           emchat-all-aliases nil t)))
61          (histf (emchat-world-getf alias 'history)))
62     (when histf
63       (find-file histf)
64       (emchat-history-mode))))
65
66 (defun emchat-history-around ()
67   "Show history around."
68   (interactive)
69   (emchat-history (emchat-alias-around)))
70
71
72 (defun emchat-history-here-other-window (&optional count)
73   "Show history around in other window."
74   (interactive)
75   (setq emchat-history-old-window-config (current-window-configuration))
76   (unless count (setq count 1))
77   (let ((alias (emchat-alias-here)))
78     (other-window count)
79     (emchat-history alias)))
80
81 (defvar emchat-history-outline-regexp "\\(^\\s-?[0-9]*[ ][A-Za-z]*[ ][A-Z]..\:..\\)"
82   "Regexp for history header.
83 See `outline-regexp'.")
84
85 (defvar emchat-history-event-regexp
86   (concat "\\(^\\s-?[0-9]*[ ][A-Za-z]*[ ][A-Z]..\:..\\)"
87           ".\\(\\[[A-Za-z0-9].*\\]\\).\\([\*><]*\\).\\(.*\\)")
88   "Regexp for history event.
89
90 First paren set matches date header, second -- person nickname, third
91 -- event type, fourth -- all other stuff.")
92
93 (defun emchat-history-mode ()
94   "Major mode for viewing history in emchat.
95 Commands: \\{emchat-history-mode-map}
96
97 Turning on `emchat-history-mode' runs the hook `emchat-history-mode-hook'."
98   (interactive)
99   (kill-all-local-variables)
100   (use-local-map emchat-history-mode-map)
101   (setq mode-name "emchat-history")
102   (setq major-mode 'emchat-history-mode)
103   (easy-menu-add emchat-history-menu)
104   (font-lock-mode t)
105   (toggle-read-only t)
106   (outline-minor-mode t)
107   (set (make-local-variable 'outline-regexp)
108        emchat-history-outline-regexp)
109   (goto-char (point-max))
110   (forward-line -1)
111   (run-hooks 'emchat-history-mode-hook))
112
113 (defun emchat-history-bury-buffer (&optional kill)
114   "Bury buffer in `emchat-history' mode.
115
116 With optional prefix arg, KILL, kill the buffer instead of burying
117 it."
118   (interactive "P")
119   ;; safeguard against calling this when the current buffer isn't in
120   ;; emchat-history-mode
121   (when (eq major-mode 'emchat-history-mode)
122     (if current-prefix-arg
123         (kill-buffer nil)
124       (bury-buffer nil)))
125   ;; Possibly restore the old window config
126   (when emchat-history-old-window-config
127     (set-window-configuration emchat-history-old-window-config)
128     (setq emchat-history-old-window-config nil)))
129
130 (defun emchat-history-save (&optional alias)
131   "Save all the open history files."
132   (interactive)
133   (if alias
134       (with-current-buffer (find-buffer-visiting
135                             (emchat-world-getf alias 'history))
136         (save-buffer))
137     (mapcar
138      #'(lambda (a)
139          (let ((buf (find-buffer-visiting
140                      (emchat-world-getf a 'history))))
141            (and buf
142                 (with-current-buffer buf
143                   (save-buffer)))))
144      emchat-all-aliases)))
145
146 (defalias 'emchat-history-next 'outline-forward-same-level)
147 (defalias 'emchat-history-prev 'outline-backward-same-level)
148
149 (defface emchat-history-timestamp-face
150   '((((background light))
151      (:foreground "red4" :bold t)))
152   "Face used to highlight timestamps in `emchat-history' mode."
153   :group 'emchat-log)
154
155 (defface emchat-history-event-type-face
156   '((((background light))
157      (:foreground "magenta3" :bold t)))
158   "Face used to highlight event types in `emchat-history' mode."
159   :group 'emchat-log)
160
161 (defface emchat-history-nick-face
162   '((((background light))
163      (:foreground "green4" :bold t)))
164   "Face used to highlight nicks in `emchat-history' mode."
165   :group 'emchat-log)
166
167 (defconst emchat-history-font-lock-keywords
168   (list
169    (cons emchat-history-event-regexp '(1 'emchat-history-timestamp-face))
170    (cons emchat-history-event-regexp '(2 'emchat-history-nick-face))
171    (cons emchat-history-event-regexp '(3 'emchat-history-event-type-face)))
172   "Highlighting rules for `emchat-history' buffers.")
173
174 (provide 'emchat-history)
175
176 ;;; emchat-history.el ends here