X-Git-Url: https://cgit.sxemacs.org/?p=riece;a=blobdiff_plain;f=lisp%2Friece-keyword.el;h=33a479b02a6b53797cb6cc2d3b087ac764bc6fe7;hp=37ae4afc3ec302df2cb8290d419bfb7c6e333b9d;hb=39e9974840411db591958263f41f420f436f10fb;hpb=5db60d6053bc53f4f90cfd1afa3c1c40b8b6d762 diff --git a/lisp/riece-keyword.el b/lisp/riece-keyword.el index 37ae4af..33a479b 100644 --- a/lisp/riece-keyword.el +++ b/lisp/riece-keyword.el @@ -29,13 +29,17 @@ ;;; Code: +(require 'riece-message) + (defgroup riece-keyword nil "Highlight keyword in IRC buffer." - :group 'riece-vars) + :group 'riece) (defcustom riece-keywords nil "Keywords to be highlightened." - :type '(repeat string) + :type '(repeat (choice (string :tag "Keyword") + (cons (string :tag "Regexp") + (integer :tag "Match")))) :group 'riece-keyword) (defcustom riece-notify-keyword-functions nil @@ -43,63 +47,79 @@ :type '(list function) :group 'riece-keyword) +(make-obsolete-variable 'riece-notify-keyword-functions + 'riece-keyword-notify-functions) + +(defcustom riece-keyword-notify-functions nil + "Functions used to notify keyword match. +Two arguments are passed to each function: the keyword used to match +and the matched message object." + :type '(list function) + :group 'riece-keyword) + (defface riece-keyword-face '((((class color)) (:foreground "red" :underline t)) (t - ())) + (:underline t))) "Face used for highlightening matching keyword." :group 'riece-highlight-faces) (defvar riece-keyword-face 'riece-keyword-face) +(defvar riece-keyword-enabled nil) + +(defconst riece-keyword-description + "Highlight keywords in IRC buffers") + ;;; The old XEmacs package doesn't have autoload setting for regexp-opt. (autoload 'regexp-opt "regexp-opt") (defun riece-keyword-message-filter (message) - (if riece-keywords - (let ((regexp (regexp-opt riece-keywords)) - (index 0)) - (while (string-match regexp (riece-message-text message) index) - (if (memq 'riece-highlight riece-addons) - (put-text-property (match-beginning 0) (match-end 0) - 'riece-keyword t - (riece-message-text message))) - (save-match-data + (if (and riece-keyword-enabled + riece-keywords + ;; Ignore messages which belongs to myself. + (not (riece-message-own-p message))) + (let* (keywords + (alist + (nconc + (delq nil (mapcar + (lambda (matcher) + (if (stringp matcher) + (ignore + (setq keywords (cons matcher keywords))) + matcher)) + riece-keywords)) + (list (cons (regexp-opt keywords) 0)))) + index) + (while alist + (setq index 0) + (while (string-match (car (car alist)) + (riece-message-text message) index) + (put-text-property (match-beginning (cdr (car alist))) + (match-end (cdr (car alist))) + 'riece-overlay-face riece-keyword-face + (riece-message-text message)) (run-hook-with-args 'riece-notify-keyword-functions - (match-string 0 (riece-message-text message)))) - (setq index (match-end 0))))) + (match-string (cdr (car alist)) + (riece-message-text message))) + (run-hook-with-args 'riece-keyword-notify-functions + (cdr (car alist)) + message) + (setq index (match-end (cdr (car alist))))) + (setq alist (cdr alist))))) message) -(defun riece-keyword-map-region (start end function) - (catch 'done - (while t - ;; Search for the beginning of the button region. - (unless (get-text-property start 'riece-keyword) - (setq start (next-single-property-change start 'riece-keyword - nil end))) - (if (= start end) - (throw 'done nil)) - ;; Search for the end of the button region. - (let ((button-end (next-single-property-change start 'riece-keyword - nil end))) - (if (= button-end end) - (throw 'done nil)) - (funcall function start button-end) - (setq start button-end))))) - -(defun riece-keyword-scan-region (start end) - (riece-keyword-map-region - start end - (lambda (start end) - (riece-overlay-put (riece-make-overlay start end) - 'face riece-keyword-face)))) - (defun riece-keyword-requires () (if (memq 'riece-highlight riece-addons) '(riece-highlight))) (defun riece-keyword-insinuate () - (add-hook 'riece-message-filter-functions 'riece-keyword-message-filter) - (add-hook 'riece-after-insert-functions 'riece-keyword-scan-region)) + (add-hook 'riece-message-filter-functions 'riece-keyword-message-filter)) + +(defun riece-keyword-enable () + (setq riece-keyword-enabled t)) + +(defun riece-keyword-disable () + (setq riece-keyword-enabled nil)) (provide 'riece-keyword)