* riece-button.el (riece-identity-button-click): New function.
[riece] / lisp / riece-button.el
index 6a112c2..ff8c0e5 100644 (file)
     ["Allow To Speak" riece-user-button-set-speakers])
   "Menu for user buttons.")
 
     ["Allow To Speak" riece-user-button-set-speakers])
   "Menu for user buttons.")
 
+(defvar riece-button-enabled nil)
+
+(defconst riece-button-description
+  "Display useful buttons in IRC buffers")
+
 (defvar help-echo-owns-message)
 (define-widget 'riece-identity-button 'push-button
   "A channel button."
 (defvar help-echo-owns-message)
 (define-widget 'riece-identity-button 'push-button
   "A channel button."
@@ -78,9 +83,30 @@ This function is used as a callback for a channel button."
       (message "%s" (substitute-command-keys
                     "Type \\[riece-command-join] to join the channel")))))
 
       (message "%s" (substitute-command-keys
                     "Type \\[riece-command-join] to join the channel")))))
 
+(defun riece-identity-button-click (event)
+  "Call widget-button-click and select the last selected window."
+  (interactive "e")                    ;widget-button-click has
+                                       ;interactive spec "@e"
+  (let ((buffer (current-buffer))
+       (point (point))
+       window)
+    (unwind-protect
+       (save-excursion
+         (set-buffer (riece-event-buffer event))
+         (goto-char (riece-event-point event))
+         (widget-button-click event))
+      ;; riece-button-switch-to-identity changes window-configuration
+      ;; so we must select the last selected window by _buffer_.
+      (if (setq window (get-buffer-window buffer))
+         (progn
+           (select-window window)
+           (set-window-point window point))
+       (if riece-debug
+           (message "buffer %s not visible" (buffer-name buffer)))))))
+
 (defun riece-identity-button-popup-menu (event)
   "Popup the menu for identity buttons."
 (defun riece-identity-button-popup-menu (event)
   "Popup the menu for identity buttons."
-  (interactive "@e")
+  (interactive "e")
   (save-excursion
     (set-buffer (riece-event-buffer event))
     (goto-char (riece-event-point event))
   (save-excursion
     (set-buffer (riece-event-buffer event))
     (goto-char (riece-event-point event))
@@ -113,7 +139,7 @@ This function is used as a callback for a channel button."
 
 (defun riece-user-button-set-operators ()
   (interactive)
 
 (defun riece-user-button-set-operators ()
   (interactive)
-  (let (group)
+  (let (group users)
     (if (riece-region-active-p)
        (save-excursion
          (riece-scan-property-region
     (if (riece-region-active-p)
        (save-excursion
          (riece-scan-property-region
@@ -123,24 +149,25 @@ This function is used as a callback for a channel button."
             (setq group (cons (get-text-property start 'riece-identity)
                               group)))))
       (setq group (list (get-text-property (point) 'riece-identity))))
             (setq group (cons (get-text-property start 'riece-identity)
                               group)))))
       (setq group (list (get-text-property (point) 'riece-identity))))
+    (setq users (riece-with-server-buffer
+                   (riece-identity-server riece-current-channel)
+                 (riece-channel-get-users (riece-identity-prefix
+                                           riece-current-channel))))
     (if (setq group
              (delq nil
                    (mapcar
                     (lambda (identity)
     (if (setq group
              (delq nil
                    (mapcar
                     (lambda (identity)
-                      (unless (riece-channel-operator-p
-                               (riece-with-server-buffer
-                                   (riece-identity-server
-                                    riece-current-channel)
-                                 (riece-get-channel (riece-identity-prefix
-                                                     riece-current-channel)))
-                               (riece-identity-prefix identity))
+                      (unless (memq ?o (cdr (riece-identity-assoc
+                                             (riece-identity-prefix identity)
+                                             users
+                                             t)))
                         identity))
                     group)))
        (riece-command-set-operators (mapcar #'riece-identity-prefix group)))))
 
 (defun riece-user-button-set-speakers ()
   (interactive)
                         identity))
                     group)))
        (riece-command-set-operators (mapcar #'riece-identity-prefix group)))))
 
 (defun riece-user-button-set-speakers ()
   (interactive)
-  (let (group)
+  (let (group users)
     (if (riece-region-active-p)
        (save-excursion
          (riece-scan-property-region
     (if (riece-region-active-p)
        (save-excursion
          (riece-scan-property-region
@@ -150,26 +177,18 @@ This function is used as a callback for a channel button."
             (setq group (cons (get-text-property start 'riece-identity)
                               group)))))
       (setq group (list (get-text-property (point) 'riece-identity))))
             (setq group (cons (get-text-property start 'riece-identity)
                               group)))))
       (setq group (list (get-text-property (point) 'riece-identity))))
+    (setq users (riece-with-server-buffer
+                   (riece-identity-server riece-current-channel)
+                 (riece-channel-get-users (riece-identity-prefix
+                                           riece-current-channel))))
     (if (setq group
              (delq nil
                    (mapcar
                     (lambda (identity)
     (if (setq group
              (delq nil
                    (mapcar
                     (lambda (identity)
-                      (unless (or (riece-channel-operator-p
-                                   (riece-with-server-buffer
-                                       (riece-identity-server
-                                        riece-current-channel)
-                                     (riece-get-channel
-                                      (riece-identity-prefix
-                                       riece-current-channel)))
-                                   (riece-identity-prefix identity))
-                                  (riece-channel-speaker-p
-                                   (riece-with-server-buffer
-                                       (riece-identity-server
-                                        riece-current-channel)
-                                     (riece-get-channel
-                                      (riece-identity-prefix
-                                       riece-current-channel)))
-                                   (riece-identity-prefix identity)))
+                      (unless (memq ?v (cdr (riece-identity-assoc
+                                             (riece-identity-prefix identity)
+                                             users
+                                             t)))
                         identity))
                     group)))
        (riece-command-set-speakers (mapcar #'riece-identity-prefix group)))))
                         identity))
                     group)))
        (riece-command-set-speakers (mapcar #'riece-identity-prefix group)))))
@@ -181,29 +200,29 @@ This function is used as a callback for a channel button."
 (defun riece-make-identity-button-map ()
   (let ((map (make-sparse-keymap)))
     (set-keymap-parent map (current-local-map))
 (defun riece-make-identity-button-map ()
   (let ((map (make-sparse-keymap)))
     (set-keymap-parent map (current-local-map))
+    (define-key map [down-mouse-2] 'riece-identity-button-click)
     (define-key map [down-mouse-3] 'riece-identity-button-popup-menu)
     map))
 
 (defvar riece-identity-button-map)
 (defun riece-button-add-identity-button (start end)
     (define-key map [down-mouse-3] 'riece-identity-button-popup-menu)
     map))
 
 (defvar riece-identity-button-map)
 (defun riece-button-add-identity-button (start end)
-  (riece-scan-property-region
-   'riece-identity
-   start end
-   (lambda (start end)
-     (let ((inhibit-read-only t)
-          buffer-read-only)
-       (widget-convert-button 'riece-identity-button start end
-                             (get-text-property start 'riece-identity))
-       (add-text-properties start end
-                           (list 'local-map riece-identity-button-map
-                                 'keymap riece-identity-button-map))))))
+  (if riece-button-enabled
+      (riece-scan-property-region
+       'riece-identity
+       start end
+       (lambda (start end)
+        (let ((inhibit-read-only t)
+              buffer-read-only)
+          (widget-convert-button 'riece-identity-button start end
+                                 (get-text-property start 'riece-identity))
+          (add-text-properties
+           start end
+           (list 'local-map riece-identity-button-map
+                 'keymap riece-identity-button-map)))))))
 
 (defun riece-button-update-buffer ()
   (riece-button-add-identity-button (point-min) (point-max)))
 
 
 (defun riece-button-update-buffer ()
   (riece-button-add-identity-button (point-min) (point-max)))
 
-(defun riece-button-requires ()
-  '(riece-highlight))
-
 (defvar riece-channel-list-mode-map)
 (defvar riece-user-list-mode-map)
 (defvar riece-dialogue-mode-map)
 (defvar riece-channel-list-mode-map)
 (defvar riece-user-list-mode-map)
 (defvar riece-dialogue-mode-map)
@@ -229,6 +248,31 @@ This function is used as a callback for a channel button."
                   (riece-make-identity-button-map))))
   (add-hook 'riece-after-insert-functions 'riece-button-add-identity-button))
 
                   (riece-make-identity-button-map))))
   (add-hook 'riece-after-insert-functions 'riece-button-add-identity-button))
 
+(defun riece-button-enable ()
+  (setq riece-button-enabled t)
+  (let ((pointer riece-buffer-list))
+    (while pointer
+      (with-current-buffer (car pointer)
+       (if (eq (derived-mode-class major-mode)
+               'riece-dialogue-mode)
+           (riece-button-update-buffer)))
+      (setq pointer (cdr pointer)))
+    (if riece-current-channel
+       (riece-emit-signal 'user-list-changed riece-current-channel))
+    (riece-emit-signal 'channel-list-changed)))
+
+(defun riece-button-disable ()
+  (setq riece-button-enabled nil)
+  (save-excursion
+    (let ((pointer riece-buffer-list))
+      (while pointer
+       ;; On XEmacs, BUFFER arg of widget-map-buttons is ignored.
+       (set-buffer (car pointer))
+       (widget-map-buttons
+        (lambda (widget maparg)
+          (widget-leave-text widget)))
+       (setq pointer (cdr pointer))))))
+
 (provide 'riece-button)
 
 ;;; riece-button.el ends here
 (provide 'riece-button)
 
 ;;; riece-button.el ends here