f75ac2fc32632a9f1984f6bfb55dc0910c9faebe
[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 help-echo-owns-message)
53 (define-widget 'riece-identity-button 'push-button
54   "A channel button."
55   :action 'riece-button-switch-to-identity
56   :help-echo
57   (lambda (widget/window &optional overlay pos)
58     ;; Needed to properly clear the message due to a bug in
59     ;; wid-edit (XEmacs only).
60     (if (boundp 'help-echo-owns-message)
61         (setq help-echo-owns-message t))
62     (format "%S: switch to %s; down-mouse-3: more options"
63             (aref riece-mouse-2 0)
64             ;; XEmacs will get a single widget arg; Emacs 21 will get
65             ;; window, overlay, position.
66             (riece-format-identity
67              (if overlay
68                  (with-current-buffer (riece-overlay-buffer overlay)
69                    (widget-value (widget-at (riece-overlay-start overlay))))
70                (widget-value widget/window))))))
71
72 (defun riece-button-switch-to-identity (widget &optional event)
73   "Switch to identity stored in WIDGET.
74 This function is used as a callback for a channel button."
75   (let ((channel (widget-value widget)))
76     (if (riece-identity-member channel riece-current-channels)
77         (riece-command-switch-to-channel channel)
78       (message "%s" (substitute-command-keys
79                      "Type \\[riece-command-join] to join the channel")))))
80
81 (defun riece-identity-button-popup-menu (event)
82   "Popup the menu for identity buttons."
83   (interactive "@e")
84   (save-excursion
85     (set-buffer (riece-event-buffer event))
86     (goto-char (riece-event-point event))
87     (riece-popup-menu-popup
88      (if (riece-channel-p (riece-identity-prefix
89                            (get-text-property (point) 'riece-identity)))
90          riece-channel-button-popup-menu
91        riece-user-button-popup-menu)
92      event)))
93
94 (defun riece-channel-button-switch-to-channel ()
95   (interactive)
96   (riece-command-switch-to-channel
97    (get-text-property (point) 'riece-identity)))
98
99 (defun riece-channel-button-part ()
100   (interactive)
101   (riece-command-part
102    (get-text-property (point) 'riece-identity)))
103
104 (defun riece-channel-button-list ()
105   (interactive)
106   (riece-command-list
107    (riece-identity-prefix (get-text-property (point) 'riece-identity))))
108
109 (defun riece-user-button-join-partner ()
110   (interactive)
111   (riece-command-join-partner
112    (get-text-property (point) 'riece-identity)))
113
114 (defun riece-user-button-set-operators ()
115   (interactive)
116   (let (group)
117     (if (riece-region-active-p)
118         (save-excursion
119           (riece-scan-property-region
120            'riece-identity
121            (region-beginning) (region-end)
122            (lambda (start end)
123              (setq group (cons (get-text-property start 'riece-identity)
124                                group)))))
125       (setq group (list (get-text-property (point) 'riece-identity))))
126     (if (setq group
127               (delq nil
128                     (mapcar
129                      (lambda (identity)
130                        (riece-with-server-buffer (riece-identity-server
131                                                   riece-current-channel)
132                          (if (and (member
133                                    (riece-identity-prefix identity)
134                                    (riece-channel-get-users
135                                     (riece-identity-prefix
136                                      riece-current-channel)))
137                                   (not (member
138                                         (riece-identity-prefix identity)
139                                         (riece-channel-get-operators
140                                          (riece-identity-prefix
141                                           riece-current-channel)))))
142                              identity)))
143                      group)))
144         (riece-command-set-operators (mapcar #'riece-identity-prefix group)))))
145
146 (defun riece-user-button-set-speakers ()
147   (interactive)
148   (let (group)
149     (if (riece-region-active-p)
150         (save-excursion
151           (riece-scan-property-region
152            'riece-identity
153            (region-beginning) (region-end)
154            (lambda (start end)
155              (setq group (cons (get-text-property start 'riece-identity)
156                                group)))))
157       (setq group (list (get-text-property (point) 'riece-identity))))
158     (if (setq group
159               (delq nil
160                     (mapcar
161                      (lambda (identity)
162                        (riece-with-server-buffer (riece-identity-server
163                                                   riece-current-channel)
164                          (if (and (member
165                                    (riece-identity-prefix identity)
166                                    (riece-channel-get-users
167                                     (riece-identity-prefix
168                                      riece-current-channel)))
169                                   (not (member
170                                         (riece-identity-prefix identity)
171                                         (riece-channel-get-operators
172                                          (riece-identity-prefix
173                                           riece-current-channel))))
174                                   (not (member
175                                         (riece-identity-prefix identity)
176                                         (riece-channel-get-speakers
177                                          (riece-identity-prefix
178                                           riece-current-channel)))))
179                              identity)))
180                      group)))
181         (riece-command-set-speakers (mapcar #'riece-identity-prefix group)))))
182
183 (defun riece-user-button-finger ()
184   (interactive)
185   (riece-command-finger (get-text-property (point) 'riece-identity)))
186
187 (defun riece-make-identity-button-map ()
188   (let ((map (make-sparse-keymap)))
189     (set-keymap-parent map (current-local-map))
190     (define-key map [down-mouse-3] 'riece-identity-button-popup-menu)
191     map))
192
193 (defvar riece-identity-button-map)
194 (defun riece-button-add-identity-button (start end)
195   (riece-scan-property-region
196    'riece-identity
197    start end
198    (lambda (start end)
199      (let ((inhibit-read-only t)
200            buffer-read-only)
201        (widget-convert-button 'riece-identity-button start end
202                               (get-text-property start 'riece-identity))
203        (add-text-properties start end
204                             (list 'local-map riece-identity-button-map
205                                   'keymap riece-identity-button-map))))))
206
207 (defun riece-button-update-buffer ()
208   (riece-button-add-identity-button (point-min) (point-max)))
209
210 (defun riece-button-requires ()
211   '(riece-highlight))
212
213 (defvar riece-channel-list-mode-map)
214 (defvar riece-user-list-mode-map)
215 (defvar riece-dialogue-mode-map)
216 (defun riece-button-insinuate ()
217   (add-hook 'riece-channel-list-mode-hook
218             (lambda ()
219               (set-keymap-parent riece-channel-list-mode-map widget-keymap)
220               (set (make-local-variable 'riece-identity-button-map)
221                    (riece-make-identity-button-map))
222               (add-hook 'riece-update-buffer-functions
223                         'riece-button-update-buffer t t)))
224   (add-hook 'riece-user-list-mode-hook
225             (lambda ()
226               (set-keymap-parent riece-user-list-mode-map widget-keymap)
227               (set (make-local-variable 'riece-identity-button-map)
228                    (riece-make-identity-button-map))
229               (add-hook 'riece-update-buffer-functions
230                         'riece-button-update-buffer t t)))
231   (add-hook 'riece-dialogue-mode-hook
232             (lambda ()
233               (set-keymap-parent riece-dialogue-mode-map widget-keymap)
234               (set (make-local-variable 'riece-identity-button-map)
235                    (riece-make-identity-button-map))))
236   (add-hook 'riece-after-insert-functions 'riece-button-add-identity-button))
237
238 (provide 'riece-button)
239
240 ;;; riece-button.el ends here