* riece-identity.el (riece-identity-member): Take an optional 3rd
[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                        (unless (riece-channel-operator-p
131                                 (riece-with-server-buffer
132                                     (riece-identity-server
133                                      riece-current-channel)
134                                   (riece-get-channel (riece-identity-prefix
135                                                       riece-current-channel)))
136                                 (riece-identity-prefix identity))
137                          identity))
138                      group)))
139         (riece-command-set-operators (mapcar #'riece-identity-prefix group)))))
140
141 (defun riece-user-button-set-speakers ()
142   (interactive)
143   (let (group)
144     (if (riece-region-active-p)
145         (save-excursion
146           (riece-scan-property-region
147            'riece-identity
148            (region-beginning) (region-end)
149            (lambda (start end)
150              (setq group (cons (get-text-property start 'riece-identity)
151                                group)))))
152       (setq group (list (get-text-property (point) 'riece-identity))))
153     (if (setq group
154               (delq nil
155                     (mapcar
156                      (lambda (identity)
157                        (unless (or (riece-channel-operator-p
158                                     (riece-with-server-buffer
159                                         (riece-identity-server
160                                          riece-current-channel)
161                                       (riece-get-channel
162                                        (riece-identity-prefix
163                                         riece-current-channel)))
164                                     (riece-identity-prefix identity))
165                                    (riece-channel-speaker-p
166                                     (riece-with-server-buffer
167                                         (riece-identity-server
168                                          riece-current-channel)
169                                       (riece-get-channel
170                                        (riece-identity-prefix
171                                         riece-current-channel)))
172                                     (riece-identity-prefix identity)))
173                          identity))
174                      group)))
175         (riece-command-set-speakers (mapcar #'riece-identity-prefix group)))))
176
177 (defun riece-user-button-finger ()
178   (interactive)
179   (riece-command-finger (get-text-property (point) 'riece-identity)))
180
181 (defun riece-make-identity-button-map ()
182   (let ((map (make-sparse-keymap)))
183     (set-keymap-parent map (current-local-map))
184     (define-key map [down-mouse-3] 'riece-identity-button-popup-menu)
185     map))
186
187 (defvar riece-identity-button-map)
188 (defun riece-button-add-identity-button (start end)
189   (riece-scan-property-region
190    'riece-identity
191    start end
192    (lambda (start end)
193      (let ((inhibit-read-only t)
194            buffer-read-only)
195        (widget-convert-button 'riece-identity-button start end
196                               (get-text-property start 'riece-identity))
197        (add-text-properties start end
198                             (list 'local-map riece-identity-button-map
199                                   'keymap riece-identity-button-map))))))
200
201 (defun riece-button-update-buffer ()
202   (riece-button-add-identity-button (point-min) (point-max)))
203
204 (defun riece-button-requires ()
205   '(riece-highlight))
206
207 (defvar riece-channel-list-mode-map)
208 (defvar riece-user-list-mode-map)
209 (defvar riece-dialogue-mode-map)
210 (defun riece-button-insinuate ()
211   (add-hook 'riece-channel-list-mode-hook
212             (lambda ()
213               (set-keymap-parent riece-channel-list-mode-map widget-keymap)
214               (set (make-local-variable 'riece-identity-button-map)
215                    (riece-make-identity-button-map))
216               (add-hook 'riece-update-buffer-functions
217                         'riece-button-update-buffer t t)))
218   (add-hook 'riece-user-list-mode-hook
219             (lambda ()
220               (set-keymap-parent riece-user-list-mode-map widget-keymap)
221               (set (make-local-variable 'riece-identity-button-map)
222                    (riece-make-identity-button-map))
223               (add-hook 'riece-update-buffer-functions
224                         'riece-button-update-buffer t t)))
225   (add-hook 'riece-dialogue-mode-hook
226             (lambda ()
227               (set-keymap-parent riece-dialogue-mode-map widget-keymap)
228               (set (make-local-variable 'riece-identity-button-map)
229                    (riece-make-identity-button-map))))
230   (add-hook 'riece-after-insert-functions 'riece-button-add-identity-button))
231
232 (provide 'riece-button)
233
234 ;;; riece-button.el ends here