* riece-unread.el (riece-unread-after-display-message-function):
[riece] / lisp / riece-button.el
1 ;;; riece-button.el --- adding buttons in channel buffers
2 ;; Copyright (C) 1998-2003 Daiki Ueno
3
4 ;; Author: Daiki Ueno <ueno@unixuser.org>
5 ;; Created: 1998-09-28
6 ;; Keywords: IRC, riece
7
8 ;; This file is part of Riece.
9
10 ;; This program is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; This program is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;; To use, add the following line to your ~/.riece/init.el:
28 ;; (add-to-list 'riece-addons 'riece-button)
29
30 ;;; Code:
31
32 (require 'riece-commands)
33 (require 'riece-identity)
34 (require 'riece-misc)
35 (require 'wid-edit)
36
37 (defvar help-echo-owns-message)
38 (define-widget 'riece-channel-button 'push-button
39   "A channel button."
40   :action 'riece-channel-button-action
41   :help-echo
42   (lambda (widget/window &optional overlay pos)
43     ;; Needed to properly clear the message due to a bug in
44     ;; wid-edit (XEmacs only).
45     (if (boundp 'help-echo-owns-message)
46         (setq help-echo-owns-message t))
47     (format "Switch to %s"
48             ;; XEmacs will get a single widget arg; Emacs 21 will get
49             ;; window, overlay, position.
50             (riece-format-identity
51              (if overlay
52                  (with-current-buffer (overlay-buffer overlay)
53                    (widget-value (widget-at (overlay-start overlay))))
54                (widget-value widget/window))))))
55
56 (defun riece-channel-button-action (widget &optional event)
57   (let ((channel (widget-value widget)))
58     (if (riece-identity-member channel riece-current-channels)
59         (riece-command-switch-to-channel channel)
60       (message "%s" (substitute-command-keys
61                      "Type \\[riece-command-join] to join the channel")))))
62
63 (defun riece-button-add-channel-buttons (start end length)
64   (save-excursion
65     (catch 'done
66       (while t
67         ;; Search for the beginning of the button region.
68         (unless (get-text-property start 'riece-identity)
69           (setq start (next-single-property-change start 'riece-identity
70                                                    nil end)))
71         ;; Search for the end of the button region.
72         (let* ((identity (get-text-property start 'riece-identity))
73                (button-end (next-single-property-change start 'riece-identity
74                                                         nil end)))
75           (if (= button-end end)
76               (throw 'done nil)
77             (if (riece-channel-p (riece-identity-prefix identity))
78                 (widget-convert-button
79                  'riece-channel-button start button-end identity))
80             (setq start button-end)))))))
81
82 (defun riece-button-update-channel-list-buffer ()
83   (if riece-channel-list-buffer-mode
84       (save-excursion
85         (set-buffer riece-channel-list-buffer)
86         (let ((inhibit-read-only t)
87               buffer-read-only)
88           (riece-button-add-channel-buttons (point-min) (point-max) nil)))))
89
90 (defun riece-button-requires ()
91   '(riece-highlight))
92
93 (defun riece-button-insinuate ()
94   (add-hook 'riece-channel-list-mode-hook
95             (lambda ()
96               (set-keymap-parent riece-channel-list-mode-map widget-keymap)
97               (add-hook 'riece-update-buffer-functions
98                         'riece-button-update-channel-list-buffer t))))
99
100 (provide 'riece-button)
101
102 ;;; riece-button.el ends here