erc-track.el 39 KB

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