3112ce9388b3df95d425cc2e6eac7f4f15a98bb7
[riece] / lisp / riece-guess.el
1 ;;; riece-guess.el --- guess the next channel, using multiple methods
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is part of Riece.
8
9 ;; This program is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Code:
25
26 (require 'riece-identity)
27 (require 'riece-commands)
28
29 (defgroup riece-guess nil
30   "Guess the next channel"
31   :tag "Guess"
32   :prefix "riece-"
33   :group 'riece)
34
35 (defcustom riece-guess-channel-try-functions
36   '(riece-default-guess-channel)
37   "Functions which returns a list of channels the user wants to switch."
38   :type '(repeat function)
39   :group 'riece-guess)
40
41 (defvar riece-current-channels)
42 (defun riece-default-guess-channel ()
43   (delq nil (copy-sequence riece-current-channels)))
44
45 (defun riece-guess-candidates ()
46   "Call \\[riece-guess-channel-try-functions] in turn and merge the results."
47   (let ((functions riece-guess-channel-try-functions)
48         candidates)
49     (while functions
50       (setq candidates
51             (nconc candidates
52                    (delq nil (mapcar
53                               (lambda (channel)
54                                 (if (riece-identity-member channel candidates)
55                                     nil
56                                   channel))
57                               (funcall (car functions)))))
58             functions (cdr functions)))
59     candidates))
60
61 (defvar riece-guess-candidates nil)
62
63 (defun riece-command-guess-switch-to-channel ()
64   "Try to switch to the channel where the user is interested in."
65   (interactive)
66   (unless (eq last-command this-command)
67     (setq riece-guess-candidates (riece-guess-candidates)))
68   (unless riece-guess-candidates
69     (error "No channel"))
70   (riece-command-switch-to-channel
71    (prog1 (car riece-guess-candidates)
72      (setq riece-guess-candidates (cdr riece-guess-candidates)))))
73
74 (defvar riece-command-mode-map)
75 (defvar riece-dialogue-mode-map)
76 (defvar riece-channel-list-mode-map)
77
78 (defun riece-guess-insinuate ()
79   (define-key riece-command-mode-map
80     "\C-cg" 'riece-command-guess-switch-to-channel)
81   (define-key riece-dialogue-mode-map
82     "g" 'riece-command-guess-switch-to-channel)
83   (define-key riece-channel-list-mode-map
84     "g" 'riece-command-guess-switch-to-channel))
85         
86 (provide 'riece-guess)
87
88 ;;; riece-guess.el ends here