Source

erc / erc-track.el

Diff from to

File erc-track.el

 ;;; erc-track.el --- Track modified channel buffers
 
-;; Copyright (C) 2002,2003,2004,2005 Free Software Foundation, Inc.
+;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
 
 ;; Author: Mario Lang <mlang@delysid.org>
 ;; Keywords: comm, faces
 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcChannelTracking
 
-;; This file is free software; you can redistribute it and/or modify
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software; you can redistribute it and/or modify
 ;; it under the terms of the GNU General Public License as published by
 ;; the Free Software Foundation; either version 2, or (at your option)
 ;; any later version.
 
-;; This file is distributed in the hope that it will be useful,
+;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 ;; GNU General Public License for more details.
 
 ;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
-;; Boston, MA 02110-1301 USA
+;; along with GNU Emacs; see the file COPYING.  If not, write to the
+;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
 
 ;;; Commentary:
 
 ;; * Add extensibility so that custom functions can track
 ;;   custom modification types.
 
+(eval-when-compile (require 'cl))
 (require 'erc)
 (require 'erc-compat)
 (require 'erc-match)
 		 function))
 
 (defcustom erc-track-use-faces t
-  "*If non-nil, use faces to indicate current nick/pal/fool/keyword/dangerous
-host activities in the mode-line.
+  "*Use faces in the mode-line.
 The faces used are the same as used for text in the buffers.
 \(e.g. `erc-pal-face' is used if a pal sent a message to that channel.)"
   :group 'erc-track
 
 (defcustom erc-track-faces-priority-list
   '(erc-error-face erc-current-nick-face erc-keyword-face erc-pal-face
-    erc-nick-msg-face erc-direct-msg-face erc-dangerous-host-face
+    erc-nick-msg-face erc-direct-msg-face erc-button erc-dangerous-host-face
     erc-default-face erc-action-face erc-nick-default-face erc-fool-face
     erc-notice-face erc-input-face erc-prompt-face)
   "A list of faces used to highlight active buffer names in the modeline.
   :type '(repeat face))
 
 (defcustom erc-track-priority-faces-only nil
-  "If you would like to ignore changes in certain channels where there
-are no faces corresponding to your erc-track-faces-priority-list, set
+  "Only track text highlighted with a priority face.
+If you would like to ignore changes in certain channels where there
+are no faces corresponding to your `erc-track-faces-priority-list', set
 this variable.  You can set a list of channel name strings, so those
 will be ignored while all other channels will be tracked as normal.
 Other options are 'all, to apply this to all channels or nil, to disable
 this feature.
-Note: If you have a lot of faces listed in erc-track-faces-priority-list,
+Note: If you have a lot of faces listed in `erc-track-faces-priority-list',
 setting this variable might not be very useful."
   :group 'erc-track
   :type '(choice (const nil)
 Entries in this list should only happen for buffers where activity occurred
 while the buffer was not visible.")
 
+(defcustom erc-track-showcount nil
+  "If non-nil, count of unseen messages will be shown for each channel."
+  :type 'boolean
+  :group 'erc-track)
+
+(defcustom erc-track-showcount-string ":"
+  "The string to display between buffer name and the count in the mode line.
+The default is a colon, resulting in \"#emacs:9\"."
+  :type 'string
+  :group 'erc-track)
+
+(defcustom erc-track-switch-from-erc t
+  "If non-nil, `erc-track-switch-buffer' will return to the last non-erc buffer
+when there are no more active channels."
+  :type 'boolean
+  :group 'erc-track)
+
+(defcustom erc-track-switch-direction 'oldest
+  "Direction `erc-track-switch-buffer' should switch.
+
+  oldest      -  find oldest active buffer
+  newest      -  find newest active buffer
+  leastactive -  find buffer with least unseen messages
+  mostactive  -  find buffer with most unseen messages."
+  :group 'erc-track
+  :type '(choice (const oldest)
+		 (const newest)
+		 (const leastactive)
+		 (const mostactive)))
+
 
 (defun erc-track-remove-from-mode-line ()
   "Remove `erc-track-modified-channels' from the mode-line"
   (when (boundp 'mode-line-modes)
     (setq mode-line-modes
 	  (remove '(t erc-modified-channels-object) mode-line-modes)))
-  (setq global-mode-string
-	(delq 'erc-modified-channels-object global-mode-string)))
+  (when (consp global-mode-string)
+    (setq global-mode-string
+	  (delq 'erc-modified-channels-object global-mode-string))))
 
 (defun erc-track-add-to-mode-line (position)
   "Add `erc-track-modified-channels' to POSITION in the mode-line.
 
 ;;; Test:
 
-(assert
+(erc-assert
  (and
   ;; verify examples from the doc strings
   (equal (let ((erc-track-shorten-aggressively nil))
 	  (unless (and
 		   (or (eq erc-track-priority-faces-only 'all)
 		       (member this-channel erc-track-priority-faces-only))
-		   (not (dolist (f faces)
-			  (when (member f erc-track-faces-priority-list)
-			    (return t)))))
+		   (not (catch 'found
+			  (dolist (f faces)
+			    (when (member f erc-track-faces-priority-list)
+			      (throw 'found t))))))
 	    (if (not (assq (current-buffer) erc-modified-channels-alist))
 		;; Add buffer, faces and counts
 		(setq erc-modified-channels-alist
 				  (if old-face
 				      (cons old-face faces)
 				    faces))))
-		  (setcdr cell (cons (1+ (second cell)) new-face)))))
+		  (setcdr cell (cons (1+ (cadr cell)) new-face)))))
 	    ;; And display it
 	    (erc-modified-channels-display)))
       ;; Else if the active buffer is the current buffer, remove it
 	(add-to-list 'faces face)))
     faces))
 
-(assert
+(erc-assert
  (let ((str "is bold"))
    (put-text-property 3 (length str)
 		      'face '(bold erc-current-nick-face)
 
 (defun erc-find-parsed-property ()
   "Find the next occurrence of the `erc-parsed' text property."
-  (let ((pos (point-min)))
-    (while (and pos (not (get-text-property pos 'erc-parsed)))
-      (setq pos (next-single-property-change pos 'erc-parsed)))
-    pos))
+  (text-property-not-all (point-min) (point-max) 'erc-parsed nil))
 
 ;;; Buffer switching
 
-(defcustom erc-track-switch-from-erc t
-  "If non-nil, `erc-track-switch-buffer' will return to the last non-erc buffer
-when there are no more active channels."
-  :type 'boolean
-  :group 'erc-track)
-
-(defcustom erc-track-showcount nil
-  "If non-nil, count of unseen messages will be shown for each channel."
-  :type 'boolean
-  :group 'erc-track)
-
-(defcustom erc-track-showcount-string ":"
-  "The string to display between buffer name and the count in the mode line.
-The default is a colon, resulting in \"#emacs:9\"."
-  :type 'string
-  :group 'erc-track)
-
-(defcustom erc-track-switch-direction 'oldest
-  "Direction `erc-track-switch-buffer' should switch.
-'oldest will find the oldest active buffer.
-'newest finds the latest, 'leastactive finds buffer with least unseen messages,
-'mostactive - with most unseen messages."
-  :group 'erc-track
-  :type '(choice (const oldest) (const newest) (const leastactive)
-		 (const mostactive)))
-
 (defvar erc-track-last-non-erc-buffer nil
   "Stores the name of the last buffer you were in before activating
 `erc-track-switch-buffers'")
 
 (defun erc-track-sort-by-activest ()
-  "Sorts erc-modified-channels-alist by 'activeness'
-\(count of not seen messages) of channel"
+  "Sort erc-modified-channels-alist by activity.
+That means the number of unseen messages in a channel."
   (setq erc-modified-channels-alist
 	(sort erc-modified-channels-alist
 	      (lambda (a b) (> (nth 1 a) (nth 1 b))))))
 relative to `erc-track-switch-direction'"
   (let ((dir erc-track-switch-direction)
 	offset)
-    (if (< arg 0)
-	(progn
-	  (cond
-	   ((eq 'oldest dir) (setq dir 'newest))
-	   ((eq 'newest dir) (setq dir 'oldest))
-	   ((eq 'mostactive dir) (setq dir 'leastactive))
-	   ((eq 'leastactive dir) (setq dir 'mostacive)))
-	  (setq arg (* -1 arg))))
-    (setq arg (- arg 1))
-    (setq offset (cond
-		  ((or (eq 'oldest dir) (eq 'leastactive dir))
-		   (- (- (length erc-modified-channels-alist) 1) arg))
-		  (t 0)))
+    (when (< arg 0)
+      (setq dir (case dir
+		  (oldest      'newest)
+		  (newest      'oldest)
+		  (mostactive  'leastactive)
+		  (leastactive 'mostactive)))
+      (setq arg (- arg)))
+    (setq offset (case dir
+		   ((oldest leastactive)
+		    (- (length erc-modified-channels-alist) arg))
+		   (t (1- arg))))
     ;; normalise out of range user input
-    (if (>= offset (length erc-modified-channels-alist))
-	(setq offset (- (length erc-modified-channels-alist) 1))
-      (if (< offset 0)
-	  (setq offset 0))
-      (car (nth offset erc-modified-channels-alist)))))
+    (cond ((>= offset (length erc-modified-channels-alist))
+	   (setq offset (1- (length erc-modified-channels-alist))))
+	  ((< offset 0)
+	   (setq offset 0)))
+    (car (nth offset erc-modified-channels-alist))))
 
 (defun erc-track-switch-buffer (arg)
   "Switch to the next active ERC buffer, or if there are no active buffers,
 `erc-track-switch-direction', a negative argument will reverse this."
   (interactive "p")
   (when erc-track-mode
-    (let ((dir erc-track-switch-direction))
-      (if erc-modified-channels-alist
-	  (progn
-	    ;; if we're not in erc-mode, set this buffer to return to
-	    (unless (eq major-mode 'erc-mode)
-	      (setq erc-track-last-non-erc-buffer (current-buffer)))
-	    ;; and jump to the next active channel
-	    (switch-to-buffer (erc-track-get-active-buffer arg)))
-	;; if no active channels, switch back to what we were doing before
-	(when (and erc-track-last-non-erc-buffer
-		   erc-track-switch-from-erc
-		   (buffer-live-p erc-track-last-non-erc-buffer))
-	  (switch-to-buffer erc-track-last-non-erc-buffer))))))
+    (cond (erc-modified-channels-alist
+	   ;; if we're not in erc-mode, set this buffer to return to
+	   (unless (eq major-mode 'erc-mode)
+	     (setq erc-track-last-non-erc-buffer (current-buffer)))
+	   ;; and jump to the next active channel
+	   (switch-to-buffer (erc-track-get-active-buffer arg)))
+	  ;; if no active channels, switch back to what we were doing before
+	  ((and erc-track-last-non-erc-buffer
+		erc-track-switch-from-erc
+		(buffer-live-p erc-track-last-non-erc-buffer))
+	   (switch-to-buffer erc-track-last-non-erc-buffer)))))
 
 ;; These bindings are global, because they pop us from any other
 ;; buffer to an active ERC buffer!
 ;; indent-tabs-mode: t
 ;; tab-width: 8
 ;; End:
+
+;; arch-tag: 11b439f5-e5d7-4c6c-bb3f-eda98f9b0ac1