* riece-xemacs.el (riece-mouse-2): New variable.
[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 (defconst riece-channel-button-popup-menu
38   '("Channel"
39     ["Switch" riece-channel-button-switch-to-channel]
40     ["Part" riece-channel-button-part]
41     ["List" riece-channel-button-list])
42   "Menu for channel buttons")
43
44 (defvar help-echo-owns-message)
45 (define-widget 'riece-channel-button 'push-button
46   "A channel button."
47   :action 'riece-channel-button-action
48   :help-echo
49   (lambda (widget/window &optional overlay pos)
50     ;; Needed to properly clear the message due to a bug in
51     ;; wid-edit (XEmacs only).
52     (if (boundp 'help-echo-owns-message)
53         (setq help-echo-owns-message t))
54     (format "%S: switch to %s; down-mouse-3: more options"
55             (aref riece-mouse-2 0)
56             ;; XEmacs will get a single widget arg; Emacs 21 will get
57             ;; window, overlay, position.
58             (riece-format-identity
59              (if overlay
60                  (with-current-buffer (overlay-buffer overlay)
61                    (widget-value (widget-at (overlay-start overlay))))
62                (widget-value widget/window))))))
63
64 (defun riece-channel-button-action (widget &optional event)
65   "Callback for channel buttons."
66   (let ((channel (widget-value widget)))
67     (if (riece-identity-member channel riece-current-channels)
68         (riece-command-switch-to-channel channel)
69       (message "%s" (substitute-command-keys
70                      "Type \\[riece-command-join] to join the channel")))))
71
72 (defun riece-channel-button-popup-menu (event)
73   "Popup the menu for channel buttons."
74   (interactive "@e")
75   (riece-popup-menu-popup riece-channel-button-popup-menu event))
76
77 (defun riece-channel-button-switch-to-channel ()
78   (interactive)
79   (riece-command-switch-to-channel
80    (get-text-property (point) 'riece-identity)))
81
82 (defun riece-channel-button-part ()
83   (interactive)
84   (riece-command-part
85    (get-text-property (point) 'riece-identity)))
86
87 (defun riece-channel-button-list ()
88   (interactive)
89   (riece-command-list
90    (riece-identity-prefix (get-text-property (point) 'riece-identity))))
91
92 (defvar riece-channel-button-map
93   (let ((map (make-sparse-keymap)))
94     (set-keymap-parent map riece-channel-list-mode-map)
95     (define-key map [down-mouse-3] 'riece-channel-button-popup-menu)
96     map))
97
98 (defun riece-button-add-channel-buttons (start end)
99   (save-excursion
100     (catch 'done
101       (while t
102         ;; Search for the beginning of the button region.
103         (unless (get-text-property start 'riece-identity)
104           (setq start (next-single-property-change start 'riece-identity
105                                                    nil end)))
106         ;; Search for the end of the button region.
107         (let* ((identity (get-text-property start 'riece-identity))
108                (button-end (next-single-property-change start 'riece-identity
109                                                         nil end)))
110           (if (= button-end end)
111               (throw 'done nil)
112             (if (riece-channel-p (riece-identity-prefix identity))
113                 (progn
114                   (widget-convert-button 'riece-channel-button start
115                                          button-end identity)
116                   (add-text-properties
117                    start button-end
118                    (list 'local-map riece-channel-button-map
119                          'keymap riece-channel-button-map))))
120             (setq start button-end)))))))
121
122 (defun riece-button-update-channel-list-buffer ()
123   (if riece-channel-list-buffer-mode
124       (save-excursion
125         (set-buffer riece-channel-list-buffer)
126         (let ((inhibit-read-only t)
127               buffer-read-only)
128           (riece-button-add-channel-buttons (point-min) (point-max))))))
129
130 (defun riece-button-requires ()
131   '(riece-highlight))
132
133 (defun riece-button-insinuate ()
134   (add-hook 'riece-channel-list-mode-hook
135             (lambda ()
136               (set-keymap-parent riece-channel-list-mode-map widget-keymap)
137               (add-hook 'riece-update-buffer-functions
138                         'riece-button-update-channel-list-buffer t)))
139   (add-hook 'riece-dialogue-mode-hook
140             (lambda ()
141               (set-keymap-parent riece-dialogue-mode-map widget-keymap)))
142   (add-hook 'riece-after-insert-functions 'riece-button-add-channel-buttons))
143
144 (provide 'riece-button)
145
146 ;;; riece-button.el ends here