1 ;;; emchat-track.el --- Event tracking code for EMchat.
3 ;; Copyright (C) 2005 - 2011 Steve Youngs, Alexey Mikhailov
5 ;; Author: Alexey Mikhailov <karma@sxemacs.org>
6 ;; Maintainer: Alexey Mikhailov <karma@sxemacs.org>
8 ;; Homepage: http://www.emchat.org/
11 ;; This file is part of EMchat.
13 ;; Redistribution and use in source and binary forms, with or without
14 ;; modification, are permitted provided that the following conditions
17 ;; 1. Redistributions of source code must retain the above copyright
18 ;; notice, this list of conditions and the following disclaimer.
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.
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.
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.
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:
45 ;; (require 'emchat-track)
46 ;; (emchat-track-mode 1)
50 ;; (setq emchat-track-enable t)
52 ;; if you dont want emchat-track to be loaded
54 ;; You may want to customize `emchat-track-events-type' and
55 ;; `emchat-track-indicator-type'
60 (defvar emchat-log-buffer))
62 (defgroup emchat-track nil
63 "Track EMchat events and show activity in the modeline."
66 (defcustom emchat-track-enable nil
71 (defcustom emchat-track-events-type 'msg
72 "*What event types to track.
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
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)))
85 (defcustom emchat-track-indicator-type 'name
86 "*What type of indicator is displayed in the modeline.
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))
97 (defcustom emchat-track-shorten-to 4
98 "*All nicks will be shortened to this number of chars."
100 :group 'emchat-track)
102 (defcustom emchat-track-activity-hook nil
103 "*Hooks run when `emchat-track-add-nick' is called."
105 :group 'emchat-track)
107 (defcustom emchat-track-clear-hook nil
108 "*Hooks run when `emchat-track-clear-modeline' is called."
110 :group 'emchat-track)
112 ;;; Internal variables
113 (defvar emchat-track-events-string "")
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)
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
135 (substring emchat-track-events-string 0
136 (1- (length emchat-track-events-string)))
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)))
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)
150 (defun emchat-track-check-log-buffer ()
151 (and (get-buffer-window emchat-log-buffer)
152 (emchat-track-clear-modeline)))
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))
164 (defun emchat-track-mode (&optional arg)
165 "Toggle emchat track mode."
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)))
172 (defun emchat-track-enable ()
174 (emchat-track-mode 1))
176 (defun emchat-track-disable ()
178 (emchat-track-mode -1))
180 (provide 'emchat-track)
181 ;;; emchat-track.el ends here