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