erc-track.el 39 KB

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