1 ;;; erc-match.el --- Highlight messages matching certain regexps
3 ;; Copyright (C) 2002-2013 Free Software Foundation, Inc.
5 ;; Author: Andreas Fuchs <asf@void.at>
7 ;; Keywords: comm, faces
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcMatch
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file includes stuff to work with pattern matching in ERC. If
28 ;; you were used to customizing erc-fools, erc-keywords, erc-pals,
29 ;; erc-dangerous-hosts and the like, this file contains these
30 ;; customizable variables.
33 ;; Put (erc-match-mode 1) into your init file.
38 (eval-when-compile (require 'cl))
42 (defgroup erc-match nil
43 "Keyword and Friend/Foe/... recognition.
44 Group containing all things concerning pattern matching in ERC
48 ;;;###autoload (autoload 'erc-match-mode "erc-match")
49 (define-erc-module match nil
50 "This mode checks whether messages match certain patterns. If so,
51 they are hidden or highlighted. This is controlled via the variables
52 `erc-pals', `erc-fools', `erc-keywords', `erc-dangerous-hosts', and
53 `erc-current-nick-highlight-type'. For all these highlighting types,
54 you can decide whether the entire message or only the sending nick is
56 ((add-hook 'erc-insert-modify-hook 'erc-match-message 'append))
57 ((remove-hook 'erc-insert-modify-hook 'erc-match-message)))
59 ;; Remaining customizations
61 (defcustom erc-pals nil
62 "List of pals on IRC."
64 :type '(repeat regexp))
66 (defcustom erc-fools nil
67 "List of fools on IRC."
69 :type '(repeat regexp))
71 (defcustom erc-keywords nil
72 "List of keywords to highlight in all incoming messages.
73 Each entry in the list is either a regexp, or a cons cell with the
74 regexp in the car and the face to use in the cdr. If no face is
75 specified, `erc-keyword-face' is used."
77 :type '(repeat (choice regexp
80 (defcustom erc-dangerous-hosts nil
81 "List of regexps for hosts to highlight.
82 Useful to mark nicks from dangerous hosts."
84 :type '(repeat regexp))
86 (defcustom erc-current-nick-highlight-type 'keyword
87 "Determines how to highlight text in which your current nickname appears
88 \(does not apply to text sent by you\).
90 The following values are allowed:
92 nil - do not highlight the message at all
93 'keyword - highlight all instances of current nickname in message
94 'nick - highlight the nick of the user who typed your nickname
95 'nick-or-keyword - highlight the nick of the user who typed your nickname,
96 or all instances of the current nickname if there was
98 'all - highlight the entire message where current nickname occurs
100 Any other value disables highlighting of current nickname altogether."
102 :type '(choice (const nil)
105 (const nick-or-keyword)
108 (defcustom erc-pal-highlight-type 'nick
109 "Determines how to highlight messages by pals.
112 The following values are allowed:
114 nil - do not highlight the message at all
115 'nick - highlight pal's nickname only
116 'all - highlight the entire message from pal
118 Any other value disables pal highlighting altogether."
120 :type '(choice (const nil)
124 (defcustom erc-fool-highlight-type 'nick
125 "Determines how to highlight messages by fools.
128 The following values are allowed:
130 nil - do not highlight the message at all
131 'nick - highlight fool's nickname only
132 'all - highlight the entire message from fool
134 Any other value disables fool highlighting altogether."
136 :type '(choice (const nil)
140 (defcustom erc-keyword-highlight-type 'keyword
141 "Determines how to highlight messages containing keywords.
142 See variable `erc-keywords'.
144 The following values are allowed:
146 'keyword - highlight keyword only
147 'all - highlight the entire message containing keyword
149 Any other value disables keyword highlighting altogether."
151 :type '(choice (const nil)
155 (defcustom erc-dangerous-host-highlight-type 'nick
156 "Determines how to highlight messages by nicks from dangerous-hosts.
157 See `erc-dangerous-hosts'.
159 The following values are allowed:
161 'nick - highlight nick from dangerous-host only
162 'all - highlight the entire message from dangerous-host
164 Any other value disables dangerous-host highlighting altogether."
166 :type '(choice (const nil)
171 (defcustom erc-log-matches-types-alist '((keyword . "ERC Keywords"))
172 "Alist telling ERC where to log which match types.
173 Valid match type keys are:
180 The other element of each cons pair in this list is the buffer name to
181 use for the logged message."
183 :type '(repeat (cons (choice :tag "Key"
186 (const dangerous-host)
188 (const current-nick))
189 (string :tag "Buffer name"))))
191 (defcustom erc-log-matches-flag 'away
192 "Flag specifying when matched message logging should happen.
193 When nil, don't log any matched messages.
194 When t, log messages.
195 When 'away, log messages only when away."
197 :type '(choice (const nil)
201 (defcustom erc-log-match-format "%t<%n:%c> %m"
202 "Format for matched Messages.
203 This variable specifies how messages in the corresponding log buffers will
204 be formatted. The various format specs are:
206 %t Timestamp (uses `erc-timestamp-format' if non-nil or \"[%Y-%m-%d %H:%M] \")
207 %n Nickname of sender
208 %u Nickname!user@host of sender
209 %c Channel in which this was received
214 (defcustom erc-beep-match-types '(current-nick)
215 "Types of matches to beep for when a match occurs.
216 The function `erc-beep-on-match' needs to be added to `erc-text-matched-hook'
217 for beeping to work."
219 :type '(choice (repeat :tag "Beep on match" (choice
223 (const dangerous-host)
225 (const :tag "Don't beep" nil)))
227 (defcustom erc-text-matched-hook '(erc-log-matches)
228 "Hook run when text matches a given match-type.
229 Functions in this hook are passed as arguments:
230 \(match-type nick!user@host message) where MATCH-TYPE is a symbol of:
231 current-nick, keyword, pal, dangerous-host, fool"
232 :options '(erc-log-matches erc-hide-fools erc-beep-on-match)
236 (defcustom erc-match-exclude-server-buffer nil
237 "If true, don't perform match on the server buffer; this is
238 useful for excluding all the things like MOTDs from the server
239 and other miscellaneous functions."
244 ;; Internal variables:
246 ;; This is exactly the same as erc-button-syntax-table. Should we
247 ;; just put it in erc.el
248 (defvar erc-match-syntax-table
249 (let ((table (make-syntax-table)))
250 (modify-syntax-entry ?\( "w" table)
251 (modify-syntax-entry ?\) "w" table)
252 (modify-syntax-entry ?\[ "w" table)
253 (modify-syntax-entry ?\] "w" table)
254 (modify-syntax-entry ?\{ "w" table)
255 (modify-syntax-entry ?\} "w" table)
256 (modify-syntax-entry ?` "w" table)
257 (modify-syntax-entry ?' "w" table)
258 (modify-syntax-entry ?^ "w" table)
259 (modify-syntax-entry ?- "w" table)
260 (modify-syntax-entry ?_ "w" table)
261 (modify-syntax-entry ?| "w" table)
262 (modify-syntax-entry ?\\ "w" table)
264 "Syntax table used when highlighting messages.
265 This syntax table should make all the valid nick characters word
270 (defface erc-current-nick-face '((t :weight bold :foreground "DarkTurquoise"))
271 "ERC face for occurrences of your current nickname."
274 (defface erc-dangerous-host-face '((t :foreground "red"))
275 "ERC face for people on dangerous hosts.
276 See `erc-dangerous-hosts'."
279 (defface erc-pal-face '((t :weight bold :foreground "Magenta"))
280 "ERC face for your pals.
284 (defface erc-fool-face '((t :foreground "dim gray"))
285 "ERC face for fools on the channel.
289 (defface erc-keyword-face '((t :weight bold :foreground "pale green"))
290 "ERC face for your keywords.
291 Note that this is the default face to use if
292 `erc-keywords' does not specify another."
297 (defun erc-add-entry-to-list (list prompt &optional completions)
298 "Add an entry interactively to a list.
299 LIST must be passed as a symbol
300 The query happens using PROMPT.
301 Completion is performed on the optional alist COMPLETIONS."
302 (let ((entry (completing-read
306 (not (erc-member-ignore-case (car x) (symbol-value list)))))))
307 (if (erc-member-ignore-case entry (symbol-value list))
308 (error "\"%s\" is already on the list" entry)
309 (set list (cons entry (symbol-value list))))))
311 (defun erc-remove-entry-from-list (list prompt)
312 "Remove an entry interactively from a list.
313 LIST must be passed as a symbol.
314 The elements of LIST can be strings, or cons cells where the
316 (let* ((alist (mapcar (lambda (x)
320 (symbol-value list)))
321 (entry (completing-read
326 (if (erc-member-ignore-case entry (symbol-value list))
328 (set list (delete entry (symbol-value list)))
330 (set list (delete (assoc entry (symbol-value list))
331 (symbol-value list))))))
334 (defun erc-add-pal ()
335 "Add pal interactively to `erc-pals'."
337 (erc-add-entry-to-list 'erc-pals "Add pal: " (erc-get-server-nickname-alist)))
340 (defun erc-delete-pal ()
341 "Delete pal interactively to `erc-pals'."
343 (erc-remove-entry-from-list 'erc-pals "Delete pal: "))
346 (defun erc-add-fool ()
347 "Add fool interactively to `erc-fools'."
349 (erc-add-entry-to-list 'erc-fools "Add fool: "
350 (erc-get-server-nickname-alist)))
353 (defun erc-delete-fool ()
354 "Delete fool interactively to `erc-fools'."
356 (erc-remove-entry-from-list 'erc-fools "Delete fool: "))
359 (defun erc-add-keyword ()
360 "Add keyword interactively to `erc-keywords'."
362 (erc-add-entry-to-list 'erc-keywords "Add keyword: "))
365 (defun erc-delete-keyword ()
366 "Delete keyword interactively to `erc-keywords'."
368 (erc-remove-entry-from-list 'erc-keywords "Delete keyword: "))
371 (defun erc-add-dangerous-host ()
372 "Add dangerous-host interactively to `erc-dangerous-hosts'."
374 (erc-add-entry-to-list 'erc-dangerous-hosts "Add dangerous-host: "))
377 (defun erc-delete-dangerous-host ()
378 "Delete dangerous-host interactively to `erc-dangerous-hosts'."
380 (erc-remove-entry-from-list 'erc-dangerous-hosts "Delete dangerous-host: "))
382 (defun erc-match-current-nick-p (nickuserhost msg)
383 "Check whether the current nickname is in MSG.
384 NICKUSERHOST will be ignored."
385 (with-syntax-table erc-match-syntax-table
387 (string-match (concat "\\b"
388 (regexp-quote (erc-current-nick))
392 (defun erc-match-pal-p (nickuserhost msg)
393 "Check whether NICKUSERHOST is in `erc-pals'.
394 MSG will be ignored."
396 (erc-list-match erc-pals nickuserhost)))
398 (defun erc-match-fool-p (nickuserhost msg)
399 "Check whether NICKUSERHOST is in `erc-fools' or MSG is directed at a fool."
400 (and msg nickuserhost
401 (or (erc-list-match erc-fools nickuserhost)
402 (erc-match-directed-at-fool-p msg))))
404 (defun erc-match-keyword-p (nickuserhost msg)
405 "Check whether any keyword of `erc-keywords' matches for MSG.
406 NICKUSERHOST will be ignored."
416 (defun erc-match-dangerous-host-p (nickuserhost msg)
417 "Check whether NICKUSERHOST is in `erc-dangerous-hosts'.
418 MSG will be ignored."
420 (erc-list-match erc-dangerous-hosts nickuserhost)))
422 (defun erc-match-directed-at-fool-p (msg)
423 "Check whether MSG is directed at a fool.
424 In order to do this, every entry in `erc-fools' will be used.
425 In any of the following situations, MSG is directed at an entry FOOL:
427 - MSG starts with \"FOOL: \" or \"FOO, \"
428 - MSG contains \", FOOL.\" (actually, \"\\s. FOOL\\s.\")"
429 (let ((fools-beg (mapcar (lambda (entry)
430 (concat "^" entry "[:,] "))
432 (fools-end (mapcar (lambda (entry)
433 (concat "\\s. " entry "\\s."))
435 (or (erc-list-match fools-beg msg)
436 (erc-list-match fools-end msg))))
438 (defun erc-match-message ()
439 "Mark certain keywords in a region.
440 Use this defun with `erc-insert-modify-hook'."
441 ;; This needs some refactoring.
442 (goto-char (point-min))
443 (let* ((to-match-nick-dep '("pal" "fool" "dangerous-host"))
444 (to-match-nick-indep '("keyword" "current-nick"))
445 (vector (erc-get-parsed-vector (point-min)))
446 (nickuserhost (erc-get-parsed-vector-nick vector))
447 (nickname (and nickuserhost
448 (nth 0 (erc-parse-user nickuserhost))))
450 (nick-beg (and nickname
451 (re-search-forward (regexp-quote nickname)
453 (match-beginning 0)))
454 (nick-end (when nick-beg
456 (message (buffer-substring (if (and nick-end
457 (<= (+ 2 nick-end) (point-max)))
462 (not (and erc-match-exclude-server-buffer
463 (erc-server-buffer-p))))
466 (goto-char (point-min))
467 (let* ((match-prefix (concat "erc-" match-type))
468 (match-pred (intern (concat "erc-match-" match-type "-p")))
469 (match-htype (eval (intern (concat match-prefix
470 "-highlight-type"))))
471 (match-regex (if (string= match-type "current-nick")
472 (regexp-quote (erc-current-nick))
473 (eval (intern (concat match-prefix "s")))))
474 (match-face (intern (concat match-prefix "-face"))))
475 (when (funcall match-pred nickuserhost message)
477 ;; Highlight the nick of the message
478 ((and (eq match-htype 'nick)
480 (erc-put-text-property
482 'face match-face (current-buffer)))
483 ;; Highlight the nick of the message, or the current
484 ;; nick if there's no nick in the message (e.g. /NAMES
486 ((and (string= match-type "current-nick")
487 (eq match-htype 'nick-or-keyword))
489 (erc-put-text-property
491 'face match-face (current-buffer))
492 (goto-char (+ 2 (or nick-end
494 (while (re-search-forward match-regex nil t)
495 (erc-put-text-property (match-beginning 0) (match-end 0)
497 ;; Highlight the whole message
498 ((eq match-htype 'all)
499 (erc-put-text-property
500 (point-min) (point-max)
501 'face match-face (current-buffer)))
502 ;; Highlight all occurrences of the word to be
504 ((and (string= match-type "keyword")
505 (eq match-htype 'keyword))
510 (setq regex (car elt)
512 (goto-char (+ 2 (or nick-end
514 (while (re-search-forward regex nil t)
515 (erc-put-text-property
516 (match-beginning 0) (match-end 0)
519 ;; Highlight all occurrences of our nick.
520 ((and (string= match-type "current-nick")
521 (eq match-htype 'keyword))
522 (goto-char (+ 2 (or nick-end
524 (while (re-search-forward match-regex nil t)
525 (erc-put-text-property (match-beginning 0) (match-end 0)
527 ;; Else twiddle your thumbs.
530 'erc-text-matched-hook
533 (concat "Server:" (erc-get-parsed-vector-type vector)))
536 (append to-match-nick-dep to-match-nick-indep)
537 to-match-nick-indep)))))
539 (defun erc-log-matches (match-type nickuserhost message)
540 "Log matches in a separate buffer, determined by MATCH-TYPE.
541 The behavior of this function is controlled by the variables
542 `erc-log-matches-types-alist' and `erc-log-matches-flag'.
543 Specify the match types which should be logged in the former,
544 and deactivate/activate match logging in the latter.
545 See `erc-log-match-format'."
546 (let ((match-buffer-name (cdr (assq match-type
547 erc-log-matches-types-alist)))
548 (nick (nth 0 (erc-parse-user nickuserhost))))
550 (or (eq erc-log-matches-flag t)
551 (and (eq erc-log-matches-flag 'away)
554 (let ((line (format-spec erc-log-match-format
557 ?t (format-time-string
558 (or (and (boundp 'erc-timestamp-format)
559 erc-timestamp-format)
560 "[%Y-%m-%d %H:%M] "))
561 ?c (or (erc-default-target) "")
564 (with-current-buffer (erc-log-matches-make-buffer match-buffer-name)
565 (let ((inhibit-read-only t))
566 (goto-char (point-max))
569 (defun erc-log-matches-make-buffer (name)
570 "Create or get a log-matches buffer named NAME and return it."
571 (let* ((buffer-already (get-buffer name))
572 (buffer (or buffer-already
573 (get-buffer-create name))))
574 (with-current-buffer buffer
575 (unless buffer-already
576 (insert " == Type \"q\" to dismiss messages ==\n")
577 (erc-view-mode-enter nil (lambda (buffer)
578 (when (y-or-n-p "Discard messages? ")
579 (kill-buffer buffer)))))
582 (defun erc-log-matches-come-back (proc parsed)
583 "Display a notice that messages were logged while away."
584 (when (and (erc-away-time)
585 (eq erc-log-matches-flag 'away))
588 (let ((buffer (get-buffer (cdr match-type)))
589 (buffer-name (cdr match-type)))
591 (let* ((last-msg-time (erc-emacs-time-to-erc-time
592 (with-current-buffer buffer
593 (get-text-property (1- (point-max))
595 (away-time (erc-emacs-time-to-erc-time (erc-away-time))))
596 (when (and away-time last-msg-time
597 (erc-time-gt last-msg-time away-time))
600 (format "You have logged messages waiting in \"%s\"."
604 (format "Type \"C-c C-k %s RET\" to view them."
606 erc-log-matches-types-alist))
609 ; This handler must be run _before_ erc-process-away is.
610 (add-hook 'erc-server-305-functions 'erc-log-matches-come-back nil)
612 (defun erc-go-to-log-matches-buffer ()
613 "Interactively open an erc-log-matches buffer."
615 (let ((buffer-name (completing-read "Switch to ERC Log buffer: "
618 erc-log-matches-types-alist)
619 (lambda (buffer-cons)
620 (get-buffer (car buffer-cons))))))
621 (switch-to-buffer buffer-name)))
623 (define-key erc-mode-map "\C-c\C-k" 'erc-go-to-log-matches-buffer)
625 (defun erc-hide-fools (match-type nickuserhost message)
626 "Hide foolish comments.
627 This function should be called from `erc-text-matched-hook'."
628 (when (eq match-type 'fool)
629 (erc-put-text-properties (point-min) (point-max)
630 '(invisible intangible)
633 (defun erc-beep-on-match (match-type nickuserhost message)
634 "Beep when text matches.
635 This function is meant to be called from `erc-text-matched-hook'."
636 (when (member match-type erc-beep-match-types)
641 ;;; erc-match.el ends here
644 ;; indent-tabs-mode: t