* riece-options.el (riece-addons): Enable riece-keyword by default.
[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 To Channel" riece-channel-button-switch-to-channel]
40     ["Part Channel" riece-channel-button-part]
41     ["List Channel" riece-channel-button-list])
42   "Menu for channel buttons.")
43
44 (defconst riece-user-button-popup-menu
45   '("User"
46     ["Finger (WHOIS)" riece-user-button-finger]
47     ["Start Private Conversation" riece-user-button-join-partner]
48     ["Give Channel Operator Privileges" riece-user-button-set-operators]
49     ["Allow To Speak" riece-user-button-set-speakers])
50   "Menu for user buttons.")
51
52 (defvar riece-button-enabled nil)
53
54 (defconst riece-button-description
55   "Display useful buttons in IRC buffers")
56
57 (defvar help-echo-owns-message)
58 (define-widget 'riece-identity-button 'push-button
59   "A channel button."
60   :action 'riece-button-switch-to-identity
61   :help-echo
62   (lambda (widget/window &optional overlay pos)
63     ;; Needed to properly clear the message due to a bug in
64     ;; wid-edit (XEmacs only).
65     (if (boundp 'help-echo-owns-message)
66         (setq help-echo-owns-message t))
67     (format "%S: switch to %s; down-mouse-3: more options"
68             (aref riece-mouse-2 0)
69             ;; XEmacs will get a single widget arg; Emacs 21 will get
70             ;; window, overlay, position.
71             (riece-format-identity
72              (if overlay
73                  (with-current-buffer (riece-overlay-buffer overlay)
74                    (widget-value (widget-at (riece-overlay-start overlay))))
75                (widget-value widget/window))))))
76
77 (defun riece-button-switch-to-identity (widget &optional event)
78   "Switch to identity stored in WIDGET.
79 This function is used as a callback for a channel button."
80   (let ((channel (widget-value widget)))
81     (if (riece-identity-member channel riece-current-channels)
82         (riece-command-switch-to-channel channel)
83       (message "%s" (substitute-command-keys
84                      "Type \\[riece-command-join] to join the channel")))))
85
86 (defun riece-identity-button-click (event)
87   "Call widget-button-click and select the last selected window."
88   (interactive "e")                     ;widget-button-click has
89                                         ;interactive spec "@e"
90   (let ((buffer (current-buffer))
91         (point (point))
92         window)
93     (unwind-protect
94         (save-excursion
95           (set-buffer (riece-event-buffer event))
96           (goto-char (riece-event-point event))
97           (widget-button-click event))
98       ;; riece-button-switch-to-identity changes window-configuration
99       ;; so we must select the last selected window by _buffer_.
100       (if (setq window (get-buffer-window buffer))
101           (progn
102             (select-window window)
103             (set-window-point window point))
104         (if riece-debug
105             (message "buffer %s not visible" (buffer-name buffer)))))))
106
107 (defun riece-identity-button-popup-menu (event)
108   "Popup the menu for identity buttons."
109   (interactive "e")
110   (save-excursion
111     (set-buffer (riece-event-buffer event))
112     (goto-char (riece-event-point event))
113     (riece-popup-menu-popup
114      (if (riece-channel-p (riece-identity-prefix
115                            (get-text-property (point) 'riece-identity)))
116          riece-channel-button-popup-menu
117        riece-user-button-popup-menu)
118      event)))
119
120 (defun riece-channel-button-switch-to-channel ()
121   (interactive)
122   (riece-command-switch-to-channel
123    (get-text-property (point) 'riece-identity)))
124
125 (defun riece-channel-button-part ()
126   (interactive)
127   (riece-command-part
128    (get-text-property (point) 'riece-identity)))
129
130 (defun riece-channel-button-list ()
131   (interactive)
132   (riece-command-list
133    (riece-identity-prefix (get-text-property (point) 'riece-identity))))
134
135 (defun riece-user-button-join-partner ()
136   (interactive)
137   (riece-command-join-partner
138    (get-text-property (point) 'riece-identity)))
139
140 (defun riece-user-button-set-operators ()
141   (interactive)
142   (let (group users)
143     (if (riece-region-active-p)
144         (save-excursion
145           (riece-scan-property-region
146            'riece-identity
147            (region-beginning) (region-end)
148            (lambda (start end)
149              (setq group (cons (get-text-property start 'riece-identity)
150                                group)))))
151       (setq group (list (get-text-property (point) 'riece-identity))))
152     (setq users (riece-with-server-buffer
153                     (riece-identity-server riece-current-channel)
154                   (riece-channel-get-users (riece-identity-prefix
155                                             riece-current-channel))))
156     (if (setq group
157               (delq nil
158                     (mapcar
159                      (lambda (identity)
160                        (unless (memq ?o (cdr (riece-identity-assoc
161                                               (riece-identity-prefix identity)
162                                               users
163                                               t)))
164                          identity))
165                      group)))
166         (riece-command-set-operators (mapcar #'riece-identity-prefix group)))))
167
168 (defun riece-user-button-set-speakers ()
169   (interactive)
170   (let (group users)
171     (if (riece-region-active-p)
172         (save-excursion
173           (riece-scan-property-region
174            'riece-identity
175            (region-beginning) (region-end)
176            (lambda (start end)
177              (setq group (cons (get-text-property start 'riece-identity)
178                                group)))))
179       (setq group (list (get-text-property (point) 'riece-identity))))
180     (setq users (riece-with-server-buffer
181                     (riece-identity-server riece-current-channel)
182                   (riece-channel-get-users (riece-identity-prefix
183                                             riece-current-channel))))
184     (if (setq group
185               (delq nil
186                     (mapcar
187                      (lambda (identity)
188                        (unless (memq ?v (cdr (riece-identity-assoc
189                                               (riece-identity-prefix identity)
190                                               users
191                                               t)))
192                          identity))
193                      group)))
194         (riece-command-set-speakers (mapcar #'riece-identity-prefix group)))))
195
196 (defun riece-user-button-finger ()
197   (interactive)
198   (riece-command-finger (get-text-property (point) 'riece-identity)))
199
200 (defun riece-make-identity-button-map ()
201   (let ((map (make-sparse-keymap)))
202     (set-keymap-parent map (current-local-map))
203     (define-key map [down-mouse-2] 'riece-identity-button-click)
204     (define-key map [down-mouse-3] 'riece-identity-button-popup-menu)
205     map))
206
207 (defvar riece-identity-button-map)
208 (defun riece-button-add-identity-button (start end)
209   (if riece-button-enabled
210       (riece-scan-property-region
211        'riece-identity
212        start end
213        (lambda (start end)
214          (let ((inhibit-read-only t)
215                buffer-read-only)
216            (widget-convert-button 'riece-identity-button start end
217                                   (get-text-property start 'riece-identity))
218            (add-text-properties
219             start end
220             (list 'local-map riece-identity-button-map
221                   'keymap riece-identity-button-map)))))))
222
223 (defun riece-button-update-buffer ()
224   (riece-button-add-identity-button (point-min) (point-max)))
225
226 (defvar riece-channel-list-mode-map)
227 (defvar riece-user-list-mode-map)
228 (defvar riece-dialogue-mode-map)
229 (defun riece-button-insinuate ()
230   (add-hook 'riece-channel-list-mode-hook
231             (lambda ()
232               (set-keymap-parent riece-channel-list-mode-map widget-keymap)
233               (set (make-local-variable 'riece-identity-button-map)
234                    (riece-make-identity-button-map))
235               (add-hook 'riece-update-buffer-functions
236                         'riece-button-update-buffer t t)))
237   (add-hook 'riece-user-list-mode-hook
238             (lambda ()
239               (set-keymap-parent riece-user-list-mode-map widget-keymap)
240               (set (make-local-variable 'riece-identity-button-map)
241                    (riece-make-identity-button-map))
242               (add-hook 'riece-update-buffer-functions
243                         'riece-button-update-buffer t t)))
244   (add-hook 'riece-dialogue-mode-hook
245             (lambda ()
246               (set-keymap-parent riece-dialogue-mode-map widget-keymap)
247               (set (make-local-variable 'riece-identity-button-map)
248                    (riece-make-identity-button-map))))
249   (add-hook 'riece-after-insert-functions 'riece-button-add-identity-button))
250
251 (defun riece-button-enable ()
252   (setq riece-button-enabled t)
253   (let ((pointer riece-buffer-list))
254     (while pointer
255       (with-current-buffer (car pointer)
256         (if (eq (derived-mode-class major-mode)
257                 'riece-dialogue-mode)
258             (riece-button-update-buffer)))
259       (setq pointer (cdr pointer)))
260     (if riece-current-channel
261         (riece-emit-signal 'user-list-changed riece-current-channel))
262     (riece-emit-signal 'channel-list-changed)))
263
264 (defun riece-button-disable ()
265   (setq riece-button-enabled nil)
266   (save-excursion
267     (let ((pointer riece-buffer-list))
268       (while pointer
269         ;; On XEmacs, BUFFER arg of widget-map-buttons is ignored.
270         (set-buffer (car pointer))
271         (widget-map-buttons
272          (lambda (widget maparg)
273            (widget-leave-text widget)))
274         (setq pointer (cdr pointer))))))
275
276 (provide 'riece-button)
277
278 ;;; riece-button.el ends here