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