* riece-foolproof.el (riece-foolproof-dmacro-override): New
[elisp/riece.git] / lisp / riece-foolproof.el
1 ;;; riece-foolproof.el --- channel miss killer
2 ;; Copyright (C) 2004 TAKAHASHI Kaoru
3
4 ;; Author: TAKAHASHI "beatmaria" Kaoru <kaoru@kaisei.org>
5 ;; Keywords: IRC, riece
6
7 ;; This file is part of Riece.
8
9 ;; This program is free software; you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation; either version 2, or (at
12 ;; your option) any later version.
13
14 ;; This program is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;; This add-on channel miss hold in check
27
28 ;; To use, add the following line to your ~/.riece/init.el:
29 ;; (add-to-list 'riece-addons 'riece-foolproof)
30
31 ;;; Code:
32
33 (eval-when-compile
34   (require 'riece-identity)
35   (require 'riece-display))
36
37 (defvar riece-foolproof-enabled nil)
38
39 (defconst riece-foolproof-description
40   "Channel miss killer")
41
42 (defun riece-foolproof-get-channel-window (identity)
43   (get-buffer-window
44    (cdr (riece-identity-assoc
45          identity riece-channel-buffer-alist))))
46
47 (eval-when-compile
48   (defvar *dmacro-key* nil))
49
50 (defun riece-foolproof-dmacro-override (&optional arg)
51   (when (and (fboundp 'dmacro-exec) (boundp '*dmacro-key*))
52     (with-current-buffer riece-command-buffer
53       (if arg
54           (when (eq (key-binding *dmacro-key*) 'dmacro-exec)
55             (local-set-key *dmacro-key* #'ignore))
56         (when (eq (key-binding *dmacro-key*) 'ignore)
57           (local-unset-key *dmacro-key*))))))
58
59 (defun riece-foolproof-insinuate ()
60   (defadvice riece-command-send-message (before riece-foolproof)
61     (unless (or (not riece-channel-buffer-mode)
62                 (riece-foolproof-get-channel-window
63                  riece-current-channel))
64       (error "%s is not displayed. (maybe channel miss)"
65              (riece-identity-prefix riece-current-channel)))
66     (unless (null executing-macro)
67       (error "Don't use `riece-command-send-message' in keyboard macro"))))
68
69 (defun riece-foolproof-enable ()
70   (riece-foolproof-dmacro-override t)
71   (ad-enable-advice 'riece-command-send-message 'before 'riece-foolproof)
72   (ad-activate 'riece-command-send-message)
73   (setq riece-foolproof-enabled t))
74
75 (defun riece-foolproof-disable ()
76   (riece-foolproof-dmacro-override nil)
77   (ad-disable-advice 'riece-command-send-message 'before 'riece-foolproof)
78   (ad-activate 'riece-command-send-message)
79   (setq riece-foolproof-enabled nil))
80
81 (provide 'riece-foolproof)
82
83 ;;; riece-foolproof.el ends here