* riece-commands.el (riece-command-join): Extract target
[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 nil
36   "Functions which returns a list of channels the user wants to switch."
37   :type '(repeat function)
38   :group 'riece-guess)
39
40 (defvar riece-current-channels)
41
42 (defun riece-guess-candidates ()
43   "Build candidate list.
44 This function calls \\[riece-guess-channel-try-functions] in turn and
45 merge the results."
46   (let ((functions riece-guess-channel-try-functions)
47         candidates)
48     (while functions
49       (setq candidates
50             (nconc candidates
51                    (delq nil (mapcar
52                               (lambda (channel)
53                                 (unless (riece-identity-member
54                                          channel candidates)
55                                   channel))
56                               (funcall (car functions)))))
57             functions (cdr functions)))
58     ;; Merge the default.
59     (setq candidates
60           (nconc candidates
61                  (delq nil (mapcar
62                             (lambda (channel)
63                               (if (and channel
64                                        (not (riece-identity-member
65                                              channel candidates)))
66                                   channel))
67                             riece-current-channels))))
68     candidates))
69
70 (defvar riece-guess-candidates nil)
71
72 (defun riece-command-guess-switch-to-channel ()
73   "Try to switch to the channel where the user is interested in."
74   (interactive)
75   (unless (and (eq last-command this-command)
76                riece-guess-candidates)
77     (setq riece-guess-candidates (riece-guess-candidates)))
78   (unless riece-guess-candidates
79     (error "No channel"))
80   (riece-command-switch-to-channel
81    (prog1 (car riece-guess-candidates)
82      (setq riece-guess-candidates (cdr riece-guess-candidates)))))
83
84 (defvar riece-command-mode-map)
85 (defvar riece-dialogue-mode-map)
86 (defvar riece-channel-list-mode-map)
87
88 (defun riece-guess-insinuate ()
89   (define-key riece-command-mode-map
90     "\C-cg" 'riece-command-guess-switch-to-channel)
91   (define-key riece-dialogue-mode-map
92     "g" 'riece-command-guess-switch-to-channel)
93   (define-key riece-channel-list-mode-map
94     "g" 'riece-command-guess-switch-to-channel))
95         
96 (provide 'riece-guess)
97
98 ;;; riece-guess.el ends here