Merge changes from emacs-23 branch
[bpt/emacs.git] / lisp / erc / erc-track.el
CommitLineData
597993cf
MB
1;;; erc-track.el --- Track modified channel buffers
2
73b0cd50 3;; Copyright (C) 2002-2011 Free Software Foundation, Inc.
597993cf
MB
4
5;; Author: Mario Lang <mlang@delysid.org>
6;; Keywords: comm, faces
7;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcChannelTracking
8
9;; This file is part of GNU Emacs.
10
4ee57b2a 11;; GNU Emacs is free software: you can redistribute it and/or modify
597993cf 12;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
597993cf
MB
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
4ee57b2a 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
597993cf
MB
23
24;;; Commentary:
25
26;; Highlights keywords and pals (friends), and hides or highlights fools
27;; (using a dark color). Add to your ~/.emacs:
28
29;; (require 'erc-track)
30;; (erc-track-mode 1)
31
32;; Todo:
33;; * Add extensibility so that custom functions can track
34;; custom modification types.
35
36(eval-when-compile (require 'cl))
37(require 'erc)
38(require 'erc-compat)
39(require 'erc-match)
40
41;;; Code:
42
43(defgroup erc-track nil
44 "Track active buffers and show activity in the modeline."
45 :group 'erc)
46
ff59d266
MB
47(defcustom erc-track-enable-keybindings 'ask
48 "Whether to enable the ERC track keybindings, namely:
49`C-c C-SPC' and `C-c C-@', which both do the same thing.
50
51The default is to check to see whether these keys are used
52already: if not, then enable the ERC track minor mode, which
53provides these keys. Otherwise, do not touch the keys.
54
55This can alternatively be set to either t or nil, which indicate
56respectively always to enable ERC track minor mode or never to
57enable ERC track minor mode.
58
59The reason for using this default value is to both (1) adhere to
60the Emacs development guidelines which say not to touch keys of
61the form C-c C-<something> and also (2) to meet the expectations
62of long-time ERC users, many of whom rely on these keybindings."
63 :group 'erc-track
64 :type '(choice (const :tag "Ask, if used already" ask)
65 (const :tag "Enable" t)
66 (const :tag "Disable" nil)))
67
597993cf
MB
68(defcustom erc-track-visibility t
69 "Where do we look for buffers to determine their visibility?
70The value of this variable determines, when a buffer is considered
71visible or invisible. New messages in invisible buffers are tracked,
72while switching to visible buffers when they are tracked removes them
ff59d266 73from the list. See also `erc-track-when-inactive'.
597993cf
MB
74
75Possible values are:
76
77t - all frames
78visible - all visible frames
79nil - only the selected frame
80selected-visible - only the selected frame if it is visible
81
82Activity means that there was no user input in the last 10 seconds."
83 :group 'erc-track
84 :type '(choice (const :tag "All frames" t)
85 (const :tag "All visible frames" visible)
86 (const :tag "Only the selected frame" nil)
87 (const :tag "Only the selected frame if it was active"
88 active)))
89
90(defcustom erc-track-exclude nil
91 "A list targets (channel names or query targets) which should not be tracked."
92 :group 'erc-track
93 :type '(repeat string))
94
526dc846
MO
95(defcustom erc-track-remove-disconnected-buffers nil
96 "*If true, remove buffers associated with a server that is
97disconnected from `erc-modified-channels-alist'."
98 :group 'erc-track
99 :type 'boolean)
100
5e56b3fb 101(defcustom erc-track-exclude-types '("NICK" "333" "353")
597993cf 102 "*List of message types to be ignored.
5e56b3fb
MO
103This list could look like '(\"JOIN\" \"PART\").
104
105By default, exclude changes of nicknames (NICK), display of who
106set the channel topic (333), and listing of users on the current
107channel (353)."
597993cf
MB
108 :group 'erc-track
109 :type 'erc-message-type)
110
111(defcustom erc-track-exclude-server-buffer nil
112 "*If true, don't perform tracking on the server buffer; this is
113useful for excluding all the things like MOTDs from the server and
114other miscellaneous functions."
115 :group 'erc-track
116 :type 'boolean)
117
118(defcustom erc-track-shorten-start 1
119 "This number specifies the minimum number of characters a channel name in
120the mode-line should be reduced to."
121 :group 'erc-track
122 :type 'number)
123
124(defcustom erc-track-shorten-cutoff 4
125 "All channel names longer than this value will be shortened."
126 :group 'erc-track
127 :type 'number)
128
129(defcustom erc-track-shorten-aggressively nil
130 "*If non-nil, channel names will be shortened more aggressively.
131Usually, names are not shortened if this will save only one character.
132Example: If there are two channels, #linux-de and #linux-fr, then
133normally these will not be shortened. When shortening aggressively,
134however, these will be shortened to #linux-d and #linux-f.
135
136If this variable is set to `max', then channel names will be shortened
137to the max. Usually, shortened channel names will remain unique for a
138given set of existing channels. When shortening to the max, the shortened
139channel names will be unique for the set of active channels only.
0b6bb130 140Example: If there are two active channels #emacs and #vi, and two inactive
597993cf
MB
141channels #electronica and #folk, then usually the active channels are
142shortened to #em and #v. When shortening to the max, however, #emacs is
143not compared to #electronica -- only to #vi, therefore it can be shortened
144even more and the result is #e and #v.
145
146This setting is used by `erc-track-shorten-names'."
147 :group 'erc-track
148 :type '(choice (const :tag "No" nil)
149 (const :tag "Yes" t)
150 (const :tag "Max" max)))
151
152(defcustom erc-track-shorten-function 'erc-track-shorten-names
153 "*This function will be used to reduce the channel names before display.
154It takes one argument, CHANNEL-NAMES which is a list of strings.
155It should return a list of strings of the same number of elements.
156If nil instead of a function, shortening is disabled."
157 :group 'erc-track
158 :type '(choice (const :tag "Disabled")
159 function))
160
526dc846
MO
161(defcustom erc-track-list-changed-hook nil
162 "Hook that is run whenever the contents of
163`erc-modified-channels-alist' changes.
164
165This is useful for people that don't use the default mode-line
166notification but instead use a separate mechanism to provide
167notification of channel activity."
168 :group 'erc-track
169 :type 'hook)
170
597993cf
MB
171(defcustom erc-track-use-faces t
172 "*Use faces in the mode-line.
173The faces used are the same as used for text in the buffers.
174\(e.g. `erc-pal-face' is used if a pal sent a message to that channel.)"
175 :group 'erc-track
176 :type 'boolean)
177
178(defcustom erc-track-faces-priority-list
5e56b3fb
MO
179 '(erc-error-face
180 (erc-nick-default-face erc-current-nick-face)
181 erc-current-nick-face
182 erc-keyword-face
183 (erc-nick-default-face erc-pal-face)
184 erc-pal-face
185 erc-nick-msg-face
186 erc-direct-msg-face
187 (erc-button erc-default-face)
188 (erc-nick-default-face erc-dangerous-host-face)
189 erc-dangerous-host-face
190 erc-nick-default-face
191 (erc-nick-default-face erc-default-face)
192 erc-default-face
193 erc-action-face
194 (erc-nick-default-face erc-fool-face)
195 erc-fool-face
196 erc-notice-face
197 erc-input-face
198 erc-prompt-face)
597993cf
MB
199 "A list of faces used to highlight active buffer names in the modeline.
200If a message contains one of the faces in this list, the buffer name will
201be highlighted using that face. The first matching face is used."
202 :group 'erc-track
5e56b3fb
MO
203 :type '(repeat (choice face
204 (repeat :tag "Combination" face))))
597993cf
MB
205
206(defcustom erc-track-priority-faces-only nil
207 "Only track text highlighted with a priority face.
208If you would like to ignore changes in certain channels where there
209are no faces corresponding to your `erc-track-faces-priority-list', set
210this variable. You can set a list of channel name strings, so those
211will be ignored while all other channels will be tracked as normal.
212Other options are 'all, to apply this to all channels or nil, to disable
213this feature.
5e56b3fb 214
597993cf
MB
215Note: If you have a lot of faces listed in `erc-track-faces-priority-list',
216setting this variable might not be very useful."
217 :group 'erc-track
218 :type '(choice (const nil)
219 (repeat string)
220 (const all)))
221
5e56b3fb
MO
222(defcustom erc-track-faces-normal-list
223 '((erc-button erc-default-face)
224 (erc-nick-default-face erc-dangerous-host-face)
225 erc-dangerous-host-face
226 erc-nick-default-face
227 (erc-nick-default-face erc-default-face)
228 erc-default-face
229 erc-action-face)
230 "A list of faces considered to be part of normal conversations.
231This list is used to highlight active buffer names in the modeline.
232
233If a message contains one of the faces in this list, and the
234previous modeline face for this buffer is also in this list, then
235the buffer name will be highlighted using the face from the
236message. This gives a rough indication that active conversations
237are occurring in these channels.
238
239The effect may be disabled by setting this variable to nil."
240 :group 'erc-track
241 :type '(repeat (choice face
242 (repeat :tag "Combination" face))))
243
597993cf
MB
244(defcustom erc-track-position-in-mode-line 'before-modes
245 "Where to show modified channel information in the mode-line.
246
247Setting this variable only has effects in GNU Emacs versions above 21.3.
248
249Choices are:
5e56b3fb
MO
250'before-modes - add to the beginning of `mode-line-modes',
251'after-modes - add to the end of `mode-line-modes',
252t - add to the end of `global-mode-string',
253nil - don't add to mode line."
597993cf
MB
254 :group 'erc-track
255 :type '(choice (const :tag "Just before mode information" before-modes)
256 (const :tag "Just after mode information" after-modes)
526dc846
MO
257 (const :tag "After all other information" t)
258 (const :tag "Don't display in mode line" nil))
597993cf
MB
259 :set (lambda (sym val)
260 (set sym val)
261 (when (and (boundp 'erc-track-mode)
262 erc-track-mode)
263 (erc-track-remove-from-mode-line)
264 (erc-track-add-to-mode-line val))))
265
266(defun erc-modified-channels-object (strings)
267 "Generate a new `erc-modified-channels-object' based on STRINGS.
268If STRINGS is nil, we initialize `erc-modified-channels-object' to
269an appropriate initial value for this flavor of Emacs."
270 (if strings
271 (if (featurep 'xemacs)
272 (let ((e-m-c-s '("[")))
273 (push (cons (extent-at 0 (car strings)) (car strings))
274 e-m-c-s)
275 (dolist (string (cdr strings))
276 (push "," e-m-c-s)
277 (push (cons (extent-at 0 string) string)
278 e-m-c-s))
279 (push "] " e-m-c-s)
280 (reverse e-m-c-s))
281 (concat (if (eq erc-track-position-in-mode-line 'after-modes)
282 "[" " [")
283 (mapconcat 'identity (nreverse strings) ",")
284 (if (eq erc-track-position-in-mode-line 'before-modes)
285 "] " "]")))
286 (if (featurep 'xemacs) '() "")))
287
288(defvar erc-modified-channels-object (erc-modified-channels-object nil)
289 "Internal object used for displaying modified channels in the mode line.")
290
291(put 'erc-modified-channels-object 'risky-local-variable t); allow properties
292
293(defvar erc-modified-channels-alist nil
294 "An ALIST used for tracking channel modification activity.
295Each element looks like (BUFFER COUNT FACE) where BUFFER is a buffer
296object of the channel the entry corresponds to, COUNT is a number
297indicating how often activity was noticed, and FACE is the face to use
298when displaying the buffer's name. See `erc-track-faces-priority-list',
299and `erc-track-showcount'.
300
301Entries in this list should only happen for buffers where activity occurred
302while the buffer was not visible.")
303
304(defcustom erc-track-showcount nil
305 "If non-nil, count of unseen messages will be shown for each channel."
306 :type 'boolean
307 :group 'erc-track)
308
309(defcustom erc-track-showcount-string ":"
310 "The string to display between buffer name and the count in the mode line.
311The default is a colon, resulting in \"#emacs:9\"."
312 :type 'string
313 :group 'erc-track)
314
315(defcustom erc-track-switch-from-erc t
316 "If non-nil, `erc-track-switch-buffer' will return to the last non-erc buffer
317when there are no more active channels."
318 :type 'boolean
319 :group 'erc-track)
320
321(defcustom erc-track-switch-direction 'oldest
322 "Direction `erc-track-switch-buffer' should switch.
323
526dc846 324 importance - find buffer with the most important message
597993cf
MB
325 oldest - find oldest active buffer
326 newest - find newest active buffer
327 leastactive - find buffer with least unseen messages
d20cf916
MO
328 mostactive - find buffer with most unseen messages.
329
330If set to 'importance, the importance is determined by position
331in `erc-track-faces-priority-list', where first is most
332important."
597993cf 333 :group 'erc-track
526dc846
MO
334 :type '(choice (const importance)
335 (const oldest)
597993cf
MB
336 (const newest)
337 (const leastactive)
338 (const mostactive)))
339
340
341(defun erc-track-remove-from-mode-line ()
342 "Remove `erc-track-modified-channels' from the mode-line"
343 (when (boundp 'mode-line-modes)
344 (setq mode-line-modes
345 (remove '(t erc-modified-channels-object) mode-line-modes)))
346 (when (consp global-mode-string)
347 (setq global-mode-string
348 (delq 'erc-modified-channels-object global-mode-string))))
349
350(defun erc-track-add-to-mode-line (position)
351 "Add `erc-track-modified-channels' to POSITION in the mode-line.
352See `erc-track-position-in-mode-line' for possible values."
353 ;; CVS Emacs has a new format string, and global-mode-string
354 ;; is very far to the right.
355 (cond ((and (eq position 'before-modes)
356 (boundp 'mode-line-modes))
357 (add-to-list 'mode-line-modes
358 '(t erc-modified-channels-object)))
359 ((and (eq position 'after-modes)
360 (boundp 'mode-line-modes))
361 (add-to-list 'mode-line-modes
362 '(t erc-modified-channels-object) t))
526dc846 363 ((eq position t)
597993cf
MB
364 (when (not global-mode-string)
365 (setq global-mode-string '(""))) ; Padding for mode-line wart
366 (add-to-list 'global-mode-string
367 'erc-modified-channels-object
368 t))))
369
370;;; Shortening of names
371
372(defun erc-track-shorten-names (channel-names)
373 "Call `erc-unique-channel-names' with the correct parameters.
374This function is a good value for `erc-track-shorten-function'.
375The list of all channels is returned by `erc-all-buffer-names'.
376CHANNEL-NAMES is the list of active channel names.
377Only channel names longer than `erc-track-shorten-cutoff' are
378actually shortened, and they are only shortened to a minimum
379of `erc-track-shorten-start' characters."
380 (erc-unique-channel-names
381 (erc-all-buffer-names)
382 channel-names
383 (lambda (s)
384 (> (length s) erc-track-shorten-cutoff))
385 erc-track-shorten-start))
386
387(defvar erc-default-recipients)
388
389(defun erc-all-buffer-names ()
390 "Return all channel or query buffer names.
391Note that we cannot use `erc-channel-list' with a nil argument,
392because that does not return query buffers."
393 (save-excursion
394 (let (result)
395 (dolist (buf (buffer-list))
396 (set-buffer buf)
397 (when (or (eq major-mode 'erc-mode) (eq major-mode 'erc-dcc-chat-mode))
398 (setq result (cons (buffer-name) result))))
399 result)))
400
401(defun erc-unique-channel-names (all active &optional predicate start)
402 "Return a list of unique channel names.
403ALL is the list of all channel and query buffer names.
404ACTIVE is the list of active buffer names.
405PREDICATE is a predicate that should return non-nil if a name needs
406 no shortening.
407START is the minimum length of the name used."
408 (if (eq 'max erc-track-shorten-aggressively)
409 ;; Return the unique substrings of all active channels.
410 (erc-unique-substrings active predicate start)
411 ;; Otherwise, determine the unique substrings of all channels, and
412 ;; for every active channel, return the corresponding substring.
413 ;; Given the names of the active channels, we now need to find the
414 ;; corresponding short name from the list of all substrings. To
415 ;; avoid problems when there are two channels and one is a
416 ;; substring of the other (notorious examples are #hurd and
417 ;; #hurd-bunny), every candidate gets the longest possible
418 ;; substring.
419 (let ((all-substrings (sort
420 (erc-unique-substrings all predicate start)
421 (lambda (a b) (> (length a) (length b)))))
422 result)
423 (dolist (channel active)
424 (let ((substrings all-substrings)
425 candidate
426 winner)
427 (while (and substrings (not winner))
428 (setq candidate (car substrings)
429 substrings (cdr substrings))
430 (when (and (string= candidate
431 (substring channel
432 0
433 (min (length candidate)
434 (length channel))))
435 (not (member candidate result)))
436 (setq winner candidate)))
437 (setq result (cons winner result))))
438 (nreverse result))))
439
440(defun erc-unique-substrings (strings &optional predicate start)
441 "Return a list of unique substrings of STRINGS."
442 (if (or (not (numberp start))
443 (< start 0))
444 (setq start 2))
445 (mapcar
446 (lambda (str)
447 (let* ((others (delete str (copy-sequence strings)))
448 (maxlen (length str))
449 (i (min start
450 (length str)))
451 candidate
452 done)
453 (if (and (functionp predicate) (not (funcall predicate str)))
454 ;; do not shorten if a predicate exists and it returns nil
455 str
456 ;; Start with smallest substring candidate, ie. length 1.
457 ;; Then check all the others and see whether any of them starts
458 ;; with the same substring. While there is such another
459 ;; element in the list, increase the length of the candidate.
460 (while (not done)
461 (if (> i maxlen)
462 (setq done t)
463 (setq candidate (substring str 0 i)
464 done (not (erc-unique-substring-1 candidate others))))
465 (setq i (1+ i)))
466 (if (and (= (length candidate) (1- maxlen))
467 (not erc-track-shorten-aggressively))
468 str
469 candidate))))
470 strings))
471
472(defun erc-unique-substring-1 (candidate others)
473 "Return non-nil when any string in OTHERS starts with CANDIDATE."
474 (let (result other (maxlen (length candidate)))
475 (while (and others
476 (not result))
477 (setq other (car others)
478 others (cdr others))
479 (when (and (>= (length other) maxlen)
480 (string= candidate (substring other 0 maxlen)))
481 (setq result other)))
482 result))
483
484;;; Test:
485
5e56b3fb 486(assert
597993cf
MB
487 (and
488 ;; verify examples from the doc strings
489 (equal (let ((erc-track-shorten-aggressively nil))
490 (erc-unique-channel-names
491 '("#emacs" "#vi" "#electronica" "#folk")
492 '("#emacs" "#vi")))
493 '("#em" "#vi")) ; emacs is different from electronica
494 (equal (let ((erc-track-shorten-aggressively t))
495 (erc-unique-channel-names
496 '("#emacs" "#vi" "#electronica" "#folk")
497 '("#emacs" "#vi")))
498 '("#em" "#v")) ; vi is shortened by one letter
499 (equal (let ((erc-track-shorten-aggressively 'max))
500 (erc-unique-channel-names
501 '("#emacs" "#vi" "#electronica" "#folk")
502 '("#emacs" "#vi")))
503 '("#e" "#v")) ; emacs need not be different from electronica
504 (equal (let ((erc-track-shorten-aggressively nil))
505 (erc-unique-channel-names
506 '("#linux-de" "#linux-fr")
507 '("#linux-de" "#linux-fr")))
508 '("#linux-de" "#linux-fr")) ; shortening by one letter is too aggressive
509 (equal (let ((erc-track-shorten-aggressively t))
510 (erc-unique-channel-names
511 '("#linux-de" "#linux-fr")
512 '("#linux-de" "#linux-fr")))
513 '("#linux-d" "#linux-f")); now we want to be aggressive
514 ;; specific problems
515 (equal (let ((erc-track-shorten-aggressively nil))
516 (erc-unique-channel-names
517 '("#dunnet" "#lisp" "#sawfish" "#fsf" "#guile"
518 "#testgnome" "#gnu" "#fsbot" "#hurd" "#hurd-bunny"
519 "#emacs")
520 '("#hurd-bunny" "#hurd" "#sawfish" "#lisp")))
521 '("#hurd-" "#hurd" "#s" "#l"))
522 (equal (let ((erc-track-shorten-aggressively nil))
523 (erc-unique-substrings
524 '("#emacs" "#vi" "#electronica" "#folk")))
525 '("#em" "#vi" "#el" "#f"))
526 (equal (let ((erc-track-shorten-aggressively t))
527 (erc-unique-substrings
528 '("#emacs" "#vi" "#electronica" "#folk")))
529 '("#em" "#v" "#el" "#f"))
530 (equal (let ((erc-track-shorten-aggressively nil))
531 (erc-unique-channel-names
532 '("#emacs" "#burse" "+linux.de" "#starwars"
533 "#bitlbee" "+burse" "#ratpoison")
534 '("+linux.de" "#starwars" "#burse")))
535 '("+l" "#s" "#bu"))
536 (equal (let ((erc-track-shorten-aggressively nil))
537 (erc-unique-channel-names
538 '("fsbot" "#emacs" "deego")
539 '("fsbot")))
540 '("fs"))
541 (equal (let ((erc-track-shorten-aggressively nil))
542 (erc-unique-channel-names
543 '("fsbot" "#emacs" "deego")
544 '("fsbot")
545 (lambda (s)
546 (> (length s) 4))
547 1))
548 '("f"))
549 (equal (let ((erc-track-shorten-aggressively nil))
550 (erc-unique-channel-names
551 '("fsbot" "#emacs" "deego")
552 '("fsbot")
553 (lambda (s)
554 (> (length s) 4))
555 2))
556 '("fs"))
557 (let ((erc-track-shorten-aggressively nil))
558 (equal (erc-unique-channel-names '("deego" "#hurd" "#hurd-bunny" "#emacs")
559 '("#hurd" "#hurd-bunny"))
560 '("#hurd" "#hurd-")))
561 ;; general examples
562 (let ((erc-track-shorten-aggressively t))
563 (and (equal (erc-unique-substring-1 "abc" '("ab" "abcd")) "abcd")
564 (not (erc-unique-substring-1 "a" '("xyz" "xab")))
565 (equal (erc-unique-substrings '("abc" "xyz" "xab"))
566 '("ab" "xy" "xa"))
567 (equal (erc-unique-substrings '("abc" "abcdefg"))
568 '("abc" "abcd"))))
569 (let ((erc-track-shorten-aggressively nil))
570 (and (equal (erc-unique-substring-1 "abc" '("ab" "abcd")) "abcd")
571 (not (erc-unique-substring-1 "a" '("xyz" "xab")))
572 (equal (erc-unique-substrings '("abc" "xyz" "xab"))
573 '("abc" "xyz" "xab"))
574 (equal (erc-unique-substrings '("abc" "abcdefg"))
575 '("abc" "abcd"))))))
576
ff59d266
MB
577;;; Minor mode
578
579;; Play nice with other IRC clients (and Emacs development rules) by
580;; making this a minor mode
581
582(defvar erc-track-minor-mode-map (make-sparse-keymap)
583 "Keymap for rcirc track minor mode.")
584
585(define-key erc-track-minor-mode-map (kbd "C-c C-@") 'erc-track-switch-buffer)
586(define-key erc-track-minor-mode-map (kbd "C-c C-SPC")
587 'erc-track-switch-buffer)
588
589;;;###autoload
590(define-minor-mode erc-track-minor-mode
591 "Global minor mode for tracking ERC buffers and showing activity in the
592mode line.
593
594This exists for the sole purpose of providing the C-c C-SPC and
595C-c C-@ keybindings. Make sure that you have enabled the track
596module, otherwise the keybindings will not do anything useful."
597 :init-value nil
598 :lighter ""
599 :keymap erc-track-minor-mode-map
600 :global t
601 :group 'erc-track)
602
5e56b3fb 603(defun erc-track-minor-mode-maybe (&optional buffer)
ff59d266 604 "Enable `erc-track-minor-mode', depending on `erc-track-enable-keybindings'."
5e56b3fb
MO
605 (when (and (not erc-track-minor-mode)
606 ;; don't start the minor mode until we have an ERC
607 ;; process running, because we don't want to prompt the
608 ;; user while starting Emacs
609 (or (and (buffer-live-p buffer)
610 (with-current-buffer buffer (eq major-mode 'erc-mode)))
611 (erc-buffer-list)))
ff59d266
MB
612 (cond ((eq erc-track-enable-keybindings 'ask)
613 (let ((key (or (and (key-binding (kbd "C-c C-SPC")) "C-SPC")
614 (and (key-binding (kbd "C-c C-@")) "C-@"))))
615 (if key
616 (if (y-or-n-p
617 (concat "The C-c " key " binding is in use;"
618 " override it for tracking? "))
619 (progn
620 (message (concat "Will change it; set"
621 " `erc-track-enable-keybindings'"
622 " to disable this message"))
623 (sleep-for 3)
624 (erc-track-minor-mode 1))
625 (message (concat "Not changing it; set"
626 " `erc-track-enable-keybindings'"
627 " to disable this message"))
628 (sleep-for 3))
629 (erc-track-minor-mode 1))))
630 ((eq erc-track-enable-keybindings t)
631 (erc-track-minor-mode 1))
632 (t nil))))
633
597993cf
MB
634;;; Module
635
636;;;###autoload (autoload 'erc-track-mode "erc-track" nil t)
ff59d266 637(define-erc-module track nil
597993cf 638 "This mode tracks ERC channel buffers with activity."
ff59d266
MB
639 ;; Enable:
640 ((when (boundp 'erc-track-when-inactive)
641 (if erc-track-when-inactive
642 (progn
643 (if (featurep 'xemacs)
644 (defadvice switch-to-buffer (after erc-update-when-inactive
645 (&rest args) activate)
646 (erc-user-is-active))
647 (add-hook 'window-configuration-change-hook 'erc-user-is-active))
648 (add-hook 'erc-send-completed-hook 'erc-user-is-active)
649 (add-hook 'erc-server-001-functions 'erc-user-is-active))
650 (erc-track-add-to-mode-line erc-track-position-in-mode-line)
ff59d266
MB
651 (erc-update-mode-line)
652 (if (featurep 'xemacs)
653 (defadvice switch-to-buffer (after erc-update (&rest args) activate)
654 (erc-modified-channels-update))
655 (add-hook 'window-configuration-change-hook
656 'erc-modified-channels-update))
657 (add-hook 'erc-insert-post-hook 'erc-track-modified-channels)
658 (add-hook 'erc-disconnected-hook 'erc-modified-channels-update))
659 ;; enable the tracking keybindings
5e56b3fb 660 (add-hook 'erc-connect-pre-hook 'erc-track-minor-mode-maybe)
ff59d266
MB
661 (erc-track-minor-mode-maybe)))
662 ;; Disable:
663 ((when (boundp 'erc-track-when-inactive)
664 (erc-track-remove-from-mode-line)
665 (if erc-track-when-inactive
666 (progn
667 (if (featurep 'xemacs)
668 (ad-disable-advice 'switch-to-buffer 'after
669 'erc-update-when-inactive)
670 (remove-hook 'window-configuration-change-hook
671 'erc-user-is-active))
672 (remove-hook 'erc-send-completed-hook 'erc-user-is-active)
673 (remove-hook 'erc-server-001-functions 'erc-user-is-active)
674 (remove-hook 'erc-timer-hook 'erc-user-is-active))
675 (if (featurep 'xemacs)
676 (ad-disable-advice 'switch-to-buffer 'after 'erc-update)
677 (remove-hook 'window-configuration-change-hook
678 'erc-modified-channels-update))
679 (remove-hook 'erc-disconnected-hook 'erc-modified-channels-update)
680 (remove-hook 'erc-insert-post-hook 'erc-track-modified-channels))
681 ;; disable the tracking keybindings
5e56b3fb 682 (remove-hook 'erc-connect-pre-hook 'erc-track-minor-mode-maybe)
ff59d266
MB
683 (when erc-track-minor-mode
684 (erc-track-minor-mode -1)))))
685
686(defcustom erc-track-when-inactive nil
687 "Enable channel tracking even for visible buffers, if you are
688inactive."
689 :group 'erc-track
690 :type 'boolean
691 :set (lambda (sym val)
692 (if erc-track-mode
693 (progn
694 (erc-track-disable)
695 (set sym val)
696 (erc-track-enable))
697 (set sym val))))
597993cf
MB
698
699;;; Visibility
700
701(defvar erc-buffer-activity nil
702 "Last time the user sent something.")
703
704(defvar erc-buffer-activity-timeout 10
705 "How many seconds of inactivity by the user
706to consider when `erc-track-visibility' is set to
707only consider active buffers visible.")
708
709(defun erc-user-is-active (&rest ignore)
710 "Set `erc-buffer-activity'."
2131c501
MO
711 (when erc-server-connected
712 (setq erc-buffer-activity (erc-current-time))
713 (erc-track-modified-channels)))
597993cf 714
526dc846
MO
715(defun erc-track-get-buffer-window (buffer frame-param)
716 (if (eq frame-param 'selected-visible)
717 (if (eq (frame-visible-p (selected-frame)) t)
718 (get-buffer-window buffer nil)
719 nil)
720 (get-buffer-window buffer frame-param)))
721
597993cf
MB
722(defun erc-buffer-visible (buffer)
723 "Return non-nil when the buffer is visible."
ff59d266 724 (if erc-track-when-inactive
597993cf 725 (when erc-buffer-activity; could be nil
526dc846 726 (and (erc-track-get-buffer-window buffer erc-track-visibility)
597993cf
MB
727 (<= (erc-time-diff erc-buffer-activity (erc-current-time))
728 erc-buffer-activity-timeout)))
526dc846 729 (erc-track-get-buffer-window buffer erc-track-visibility)))
597993cf
MB
730
731;;; Tracking the channel modifications
732
733(defvar erc-modified-channels-update-inside nil
734 "Variable to prevent running `erc-modified-channels-update' multiple
735times. Without it, you cannot debug `erc-modified-channels-display',
736because the debugger also cases changes to the window-configuration.")
737
738(defun erc-modified-channels-update (&rest args)
739 "This function updates the information in `erc-modified-channels-alist'
740according to buffer visibility. It calls
741`erc-modified-channels-display' at the end. This should usually be
742called via `window-configuration-change-hook'.
743ARGS are ignored."
744 (interactive)
745 (unless erc-modified-channels-update-inside
526dc846
MO
746 (let ((erc-modified-channels-update-inside t)
747 (removed-channel nil))
e2cfa9af
GM
748 (mapc (lambda (elt)
749 (let ((buffer (car elt)))
750 (when (or (not (bufferp buffer))
751 (not (buffer-live-p buffer))
752 (erc-buffer-visible buffer)
753 (and erc-track-remove-disconnected-buffers
754 (not (with-current-buffer buffer
755 erc-server-connected))))
756 (setq removed-channel t)
757 (erc-modified-channels-remove-buffer buffer))))
758 erc-modified-channels-alist)
526dc846 759 (when removed-channel
597993cf 760 (erc-modified-channels-display)
526dc846 761 (force-mode-line-update t)))))
597993cf 762
83dc6995
MB
763(defvar erc-track-mouse-face (if (featurep 'xemacs)
764 'modeline-mousable
765 'mode-line-highlight)
766 "The face to use when mouse is over channel names in the mode line.")
767
597993cf
MB
768(defun erc-make-mode-line-buffer-name (string buffer &optional faces count)
769 "Return STRING as a button that switches to BUFFER when clicked.
770If FACES are provided, color STRING with them."
771 ;; We define a new sparse keymap every time, because 1. this data
772 ;; structure is very small, the alternative would require us to
773 ;; defvar a keymap, 2. the user is not interested in customizing it
774 ;; (really?), 3. the defun needs to switch to BUFFER, so we would
775 ;; need to save that value somewhere.
776 (let ((map (make-sparse-keymap))
777 (name (if erc-track-showcount
778 (concat string
779 erc-track-showcount-string
780 (int-to-string count))
781 (copy-sequence string))))
782 (define-key map (vector 'mode-line 'mouse-2)
783 `(lambda (e)
784 (interactive "e")
785 (save-selected-window
786 (select-window
787 (posn-window (event-start e)))
788 (switch-to-buffer ,buffer))))
789 (define-key map (vector 'mode-line 'mouse-3)
790 `(lambda (e)
791 (interactive "e")
792 (save-selected-window
793 (select-window
794 (posn-window (event-start e)))
795 (switch-to-buffer-other-window ,buffer))))
796 (put-text-property 0 (length name) 'local-map map name)
83dc6995
MB
797 (put-text-property
798 0 (length name)
799 'help-echo (concat "mouse-2: switch to buffer, "
800 "mouse-3: switch to buffer in other window")
801 name)
802 (put-text-property 0 (length name) 'mouse-face erc-track-mouse-face name)
597993cf
MB
803 (when (and faces erc-track-use-faces)
804 (put-text-property 0 (length name) 'face faces name))
805 name))
806
807(defun erc-modified-channels-display ()
808 "Set `erc-modified-channels-object'
809according to `erc-modified-channels-alist'.
810Use `erc-make-mode-line-buffer-name' to create buttons."
526dc846
MO
811 (cond ((or (eq 'mostactive erc-track-switch-direction)
812 (eq 'leastactive erc-track-switch-direction))
813 (erc-track-sort-by-activest))
814 ((eq 'importance erc-track-switch-direction)
815 (erc-track-sort-by-importance)))
816 (run-hooks 'erc-track-list-changed-hook)
817 (unless (eq erc-track-position-in-mode-line nil)
597993cf
MB
818 (if (null erc-modified-channels-alist)
819 (setq erc-modified-channels-object (erc-modified-channels-object nil))
820 ;; erc-modified-channels-alist contains all the data we need. To
821 ;; better understand what is going on, we split things up into
822 ;; four lists: BUFFERS, COUNTS, SHORT-NAMES, and FACES. These
823 ;; four lists we use to create a new
824 ;; `erc-modified-channels-object' using
825 ;; `erc-make-mode-line-buffer-name'.
826 (let* ((buffers (mapcar 'car erc-modified-channels-alist))
827 (counts (mapcar 'cadr erc-modified-channels-alist))
828 (faces (mapcar 'cddr erc-modified-channels-alist))
829 (long-names (mapcar #'(lambda (buf)
830 (or (buffer-name buf)
831 ""))
832 buffers))
833 (short-names (if (functionp erc-track-shorten-function)
834 (funcall erc-track-shorten-function
835 long-names)
836 long-names))
837 strings)
838 (while buffers
839 (when (car short-names)
840 (setq strings (cons (erc-make-mode-line-buffer-name
841 (car short-names)
842 (car buffers)
843 (car faces)
844 (car counts))
845 strings)))
846 (setq short-names (cdr short-names)
847 buffers (cdr buffers)
848 counts (cdr counts)
849 faces (cdr faces)))
850 (when (featurep 'xemacs)
851 (erc-modified-channels-object nil))
852 (setq erc-modified-channels-object
2a927019 853 (erc-modified-channels-object strings))))))
597993cf
MB
854
855(defun erc-modified-channels-remove-buffer (buffer)
856 "Remove BUFFER from `erc-modified-channels-alist'."
857 (interactive "bBuffer: ")
858 (setq erc-modified-channels-alist
859 (delete (assq buffer erc-modified-channels-alist)
860 erc-modified-channels-alist))
861 (when (interactive-p)
862 (erc-modified-channels-display)))
863
864(defun erc-track-find-face (faces)
865 "Return the face to use in the modeline from the faces in FACES.
2a927019
MO
866If `erc-track-faces-priority-list' is set, the one from FACES who
867is first in that list will be used. If nothing matches or if
868`erc-track-faces-priority-list' is not set, the default mode-line
869faces will be used.
5e56b3fb
MO
870
871If `erc-track-faces-normal-list' is non-nil, use it to produce a
872blinking effect that indicates channel activity when the first
873element in FACES and the highest-ranking face among the rest of
874FACES are both members of `erc-track-faces-normal-list'.
875
5e56b3fb
MO
876If one of the faces is a list, then it will be ranked according
877to its highest-tanking face member. A list of faces including
878that member will take priority over just the single member
879element."
880 (let ((choice (catch 'face
881 (dolist (candidate erc-track-faces-priority-list)
882 (when (member candidate faces)
883 (throw 'face candidate)))))
884 (no-first (and erc-track-faces-normal-list
885 (catch 'face
886 (dolist (candidate erc-track-faces-priority-list)
887 (when (member candidate (cdr faces))
888 (throw 'face candidate)))))))
889 (cond ((null choice)
2a927019 890 nil)
5e56b3fb
MO
891 ((and (member choice erc-track-faces-normal-list)
892 (member no-first erc-track-faces-normal-list))
893 no-first)
894 (t
895 choice))))
597993cf
MB
896
897(defun erc-track-modified-channels ()
898 "Hook function for `erc-insert-post-hook' to check if the current
899buffer should be added to the modeline as a hidden, modified
900channel. Assumes it will only be called when current-buffer
901is in `erc-mode'."
902 (let ((this-channel (or (erc-default-target)
903 (buffer-name (current-buffer)))))
904 (if (and (not (erc-buffer-visible (current-buffer)))
905 (not (member this-channel erc-track-exclude))
906 (not (and erc-track-exclude-server-buffer
526dc846 907 (erc-server-buffer-p)))
597993cf
MB
908 (not (erc-message-type-member
909 (or (erc-find-parsed-property)
910 (point-min))
911 erc-track-exclude-types)))
912 ;; If the active buffer is not visible (not shown in a
913 ;; window), and not to be excluded, determine the kinds of
914 ;; faces used in the current message, and unless the user
915 ;; wants to ignore changes in certain channels where there
916 ;; are no faces corresponding to `erc-track-faces-priority-list',
917 ;; and the faces in the current message are found in said
918 ;; priority list, add the buffer to the erc-modified-channels-alist,
919 ;; if it is not already there. If the buffer is already on the list
920 ;; (in the car), change its face attribute (in the cddr) if
921 ;; necessary. See `erc-modified-channels-alist' for the
922 ;; exact data structure used.
923 (let ((faces (erc-faces-in (buffer-string))))
924 (unless (and
925 (or (eq erc-track-priority-faces-only 'all)
926 (member this-channel erc-track-priority-faces-only))
927 (not (catch 'found
928 (dolist (f faces)
929 (when (member f erc-track-faces-priority-list)
930 (throw 'found t))))))
931 (if (not (assq (current-buffer) erc-modified-channels-alist))
932 ;; Add buffer, faces and counts
933 (setq erc-modified-channels-alist
934 (cons (cons (current-buffer)
935 (cons 1 (erc-track-find-face faces)))
936 erc-modified-channels-alist))
937 ;; Else modify the face for the buffer, if necessary.
938 (when faces
939 (let* ((cell (assq (current-buffer)
940 erc-modified-channels-alist))
941 (old-face (cddr cell))
942 (new-face (erc-track-find-face
943 (if old-face
944 (cons old-face faces)
945 faces))))
946 (setcdr cell (cons (1+ (cadr cell)) new-face)))))
947 ;; And display it
948 (erc-modified-channels-display)))
949 ;; Else if the active buffer is the current buffer, remove it
950 ;; from our list.
526dc846 951 (when (and (or (erc-buffer-visible (current-buffer))
597993cf 952 (and this-channel
597993cf 953 (member this-channel erc-track-exclude)))
526dc846 954 (assq (current-buffer) erc-modified-channels-alist))
597993cf
MB
955 ;; Remove it from mode-line if buffer is visible or
956 ;; channel was added to erc-track-exclude recently.
957 (erc-modified-channels-remove-buffer (current-buffer))
958 (erc-modified-channels-display)))))
959
960(defun erc-faces-in (str)
961 "Return a list of all faces used in STR."
962 (let ((i 0)
963 (m (length str))
5e56b3fb
MO
964 (faces (erc-list (get-text-property 0 'face str)))
965 cur)
597993cf
MB
966 (while (and (setq i (next-single-property-change i 'face str m))
967 (not (= i m)))
5e56b3fb
MO
968 (when (setq cur (get-text-property i 'face str))
969 (add-to-list 'faces cur)))
597993cf
MB
970 faces))
971
5e56b3fb 972(assert
597993cf
MB
973 (let ((str "is bold"))
974 (put-text-property 3 (length str)
975 'face '(bold erc-current-nick-face)
976 str)
977 (erc-faces-in str)))
978
597993cf
MB
979;;; Buffer switching
980
981(defvar erc-track-last-non-erc-buffer nil
982 "Stores the name of the last buffer you were in before activating
983`erc-track-switch-buffers'")
984
985(defun erc-track-sort-by-activest ()
986 "Sort erc-modified-channels-alist by activity.
987That means the number of unseen messages in a channel."
988 (setq erc-modified-channels-alist
989 (sort erc-modified-channels-alist
990 (lambda (a b) (> (nth 1 a) (nth 1 b))))))
991
526dc846
MO
992(defun erc-track-face-priority (face)
993 "Return a number indicating the priority of FACE in
994`erc-track-faces-priority-list'. Lower number means higher
995priority.
996
997If face is not in `erc-track-faces-priority-list', it will have a
998higher number than any other face in that list."
999 (let ((count 0))
1000 (catch 'done
1001 (dolist (item erc-track-faces-priority-list)
5e56b3fb 1002 (if (equal item face)
526dc846
MO
1003 (throw 'done t)
1004 (setq count (1+ count)))))
1005 count))
1006
1007(defun erc-track-sort-by-importance ()
1008 "Sort erc-modified-channels-alist by importance.
1009That means the position of the face in `erc-track-faces-priority-list'."
1010 (setq erc-modified-channels-alist
1011 (sort erc-modified-channels-alist
1012 (lambda (a b) (< (erc-track-face-priority (cddr a))
1013 (erc-track-face-priority (cddr b)))))))
1014
597993cf
MB
1015(defun erc-track-get-active-buffer (arg)
1016 "Return the buffer name of ARG in `erc-modified-channels-alist'.
1017Negative arguments index in the opposite direction. This direction is
1018relative to `erc-track-switch-direction'"
1019 (let ((dir erc-track-switch-direction)
1020 offset)
1021 (when (< arg 0)
1022 (setq dir (case dir
1023 (oldest 'newest)
1024 (newest 'oldest)
1025 (mostactive 'leastactive)
526dc846
MO
1026 (leastactive 'mostactive)
1027 (importance 'oldest)))
597993cf
MB
1028 (setq arg (- arg)))
1029 (setq offset (case dir
1030 ((oldest leastactive)
1031 (- (length erc-modified-channels-alist) arg))
1032 (t (1- arg))))
1033 ;; normalise out of range user input
1034 (cond ((>= offset (length erc-modified-channels-alist))
1035 (setq offset (1- (length erc-modified-channels-alist))))
1036 ((< offset 0)
1037 (setq offset 0)))
1038 (car (nth offset erc-modified-channels-alist))))
1039
1040(defun erc-track-switch-buffer (arg)
1041 "Switch to the next active ERC buffer, or if there are no active buffers,
1042switch back to the last non-ERC buffer visited. Next is defined by
1043`erc-track-switch-direction', a negative argument will reverse this."
1044 (interactive "p")
ff59d266
MB
1045 (if (not erc-track-mode)
1046 (message (concat "Enable the ERC track module if you want to use the"
1047 " tracking minor mode"))
597993cf
MB
1048 (cond (erc-modified-channels-alist
1049 ;; if we're not in erc-mode, set this buffer to return to
1050 (unless (eq major-mode 'erc-mode)
1051 (setq erc-track-last-non-erc-buffer (current-buffer)))
1052 ;; and jump to the next active channel
1053 (switch-to-buffer (erc-track-get-active-buffer arg)))
1054 ;; if no active channels, switch back to what we were doing before
1055 ((and erc-track-last-non-erc-buffer
1056 erc-track-switch-from-erc
1057 (buffer-live-p erc-track-last-non-erc-buffer))
1058 (switch-to-buffer erc-track-last-non-erc-buffer)))))
1059
597993cf
MB
1060(provide 'erc-track)
1061
1062;;; erc-track.el ends here
1063;;
1064;; Local Variables:
1065;; indent-tabs-mode: t
1066;; tab-width: 8
1067;; End:
1068