Use a nicer, cleaner syntax calling #'make-glyph
[emchat] / emchat-track.el
1 ;;; emchat-track.el --- Event tracking code for EMchat.
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-25
8 ;; Homepage:       http://www.emchat.org/
9 ;; Keywords:
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 ;; emchat-track.el tracks ICQ channel activity and notifies user in modeline.
43 ;; To start with emchat-track add to your ~/.sxemacs/init.el:
44 ;;
45 ;;     (require 'emchat-track)
46 ;;     (emchat-track-mode 1)
47 ;;
48 ;; or simple:
49 ;;
50 ;;     (setq emchat-track-enable t)
51 ;;
52 ;; if you dont want emchat-track to be loaded
53 ;;
54 ;; You may want to customize `emchat-track-events-type' and
55 ;; `emchat-track-indicator-type'
56
57 ;;; Code:
58 \f
59 (eval-when-compile
60   (defvar emchat-log-buffer))
61
62 (defgroup emchat-track nil
63   "Track EMchat events and show activity in the modeline."
64   :group 'emchat)
65
66 (defcustom emchat-track-enable nil
67   "*Enable tracking."
68   :group 'emchat-track
69   :type 'boolean)
70
71 (defcustom emchat-track-events-type 'msg
72   "*What event types to track.
73
74 Possible values are:
75     all      -- all events \(anything that gets logged will be tracked\)
76     incoming -- only incoming messages plus system and debug events
77     msg      -- incoming user messages only
78     nil      -- no events."
79   :group 'emchat-track
80   :type '(choice (item :tag "All Events" all)
81                  (item :tag "Incoming msg + sys + debug events" incoming)
82                  (item :tag "Incoming user messages" msg)
83                  (item :tag "Nothing" nil)))
84
85 (defcustom emchat-track-indicator-type 'name
86   "*What type of indicator is displayed in the modeline.
87
88 Possible values are:
89
90     name -- [nick,nick2,nick3] \(ERC style\).
91             \"nick\" is shortened to `emchat-track-shorten-to'.
92     char -- [E] \(Riece style\)"
93   :type '(radio (item :tag "Multi names \(ERC style\)" name)
94                 (item :tag "Single character \(Riece style\)" char))
95   :group 'emchat-track)
96
97 (defcustom emchat-track-shorten-to 4
98   "*All nicks will be shortened to this number of chars."
99   :type 'integer
100   :group 'emchat-track)
101
102 (defcustom emchat-track-activity-hook nil
103   "*Hooks run when `emchat-track-add-nick' is called."
104   :type 'hook
105   :group 'emchat-track)
106
107 (defcustom emchat-track-clear-hook nil
108   "*Hooks run when `emchat-track-clear-modeline' is called."
109   :type 'hook
110   :group 'emchat-track)
111
112 ;;; Internal variables
113 (defvar emchat-track-events-string "")
114
115 (defun emchat-track-truncate-nick (nick)
116   "Return shortened nick."
117   (if (> (length nick) emchat-track-shorten-to)
118       (substring nick 0 emchat-track-shorten-to)
119     nick))
120
121 (defun emchat-track-add-nick (nick)
122   "Add new nick to events strings."
123   (let ((snick (emchat-track-truncate-nick nick)))
124     (setq global-mode-string
125           (delq 'emchat-track-events-string global-mode-string))
126     (if (eq emchat-track-indicator-type 'char)
127         ;; Riece style indicator.
128         (setq emchat-track-events-string "[E]")
129       ;; ERC style indicator.
130       (if (equal emchat-track-events-string "")
131           (setq emchat-track-events-string (format "[%s]" snick))
132         (and (not (search snick emchat-track-events-string))
133              (setq emchat-track-events-string
134                    (format "%s,%s]"
135                            (substring emchat-track-events-string 0
136                                       (1- (length emchat-track-events-string)))
137                            snick)))))
138     (setq global-mode-string (append global-mode-string
139                                      '(emchat-track-events-string)))
140     (run-hooks 'emchat-track-activity-hook)
141     (redraw-modeline t)))
142
143 (defun emchat-track-clear-modeline ()
144   "Remove `emchat-track' messages from modeline."
145   (setq global-mode-string (delq 'emchat-track-events-string global-mode-string))
146   (setq emchat-track-events-string "")
147   (run-hooks 'emchat-track-clear-hook)
148   (redraw-modeline t))
149
150 (defun emchat-track-check-log-buffer ()
151   (and (get-buffer-window emchat-log-buffer)
152        (emchat-track-clear-modeline)))
153
154 (defun emchat-track-init ()
155   "Initialize `emchat-track', perform defadvices and hooks"
156   (defadvice switch-to-buffer (after emchat-update (&rest args) activate)
157     "After switching buffers, check to see if emchat-track should be cleared.
158 The emchat-track modeline indicator will only be cleared if
159 `emchat-log-buffer' is visible in the selected frame."
160     (emchat-track-check-log-buffer))
161   (add-hook 'select-frame-hook 'emchat-track-check-log-buffer)
162   (put 'emchat-track 'initialized t))
163
164 (defun emchat-track-mode (&optional arg)
165   "Toggle emchat track mode."
166   (interactive "P")
167   (if (or (and (numberp arg) (< arg 0))
168           (and (null arg) emchat-track-enable))
169       (setq emchat-track-enable nil)
170     (setq emchat-track-enable t)))
171
172 (defun emchat-track-enable ()
173   (interactive)
174   (emchat-track-mode 1))
175
176 (defun emchat-track-disable ()
177   (interactive)
178   (emchat-track-mode -1))
179
180 (provide 'emchat-track)
181 ;;; emchat-track.el ends here