Fix -- user list buffer not updating when parts are hidden.
[riece] / lisp / riece-eval.el
1 ;;; riece-eval.el --- evaluate input string as an elisp form -*- lexical-binding: t -*-
2 ;; Copyright (C) 2005 OHASHI Akira
3
4 ;; Author: OHASHI Akira <bg66@koka-in.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 modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; This program is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU 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., 51 Franklin Street, Fifth Floor,
22 ;; Boston, MA 02110-1301, USA.
23
24 ;;; Commentary:
25
26 ;; NOTE: This is an add-on module for Riece.
27
28 ;;; Code:
29
30 (require 'pp)
31 (require 'riece-message)
32
33 (defgroup riece-eval nil
34   "Evaluate an input string as an elisp form."
35   :prefix "riece-"
36   :group 'riece)
37
38 (defcustom riece-eval-regexp "^, \\(.+\\)"
39   "*Pattern of string evaluating."
40   :type 'string
41   :group 'riece-eval)
42
43 (defcustom riece-eval-ignore-error nil
44   "*If non-nil, an error is ignored."
45   :type 'boolean
46   :group 'riece-eval)
47
48 (defconst riece-eval-description
49   "Evaluate an input string as an elisp form.")
50
51 (defun riece-eval-display-message-function (message)
52   (when (and (get 'riece-eval 'riece-addon-enabled)
53              (riece-message-own-p message)
54              (string-match riece-eval-regexp (riece-message-text message)))
55     (let* ((form (match-string 1 (riece-message-text message)))
56            (string (riece-eval-form form)))
57       (unless (equal string "")
58         (riece-send-string
59          (format "NOTICE %s :%s\r\n"
60                  (riece-identity-prefix (riece-message-target message))
61                  string))
62         (riece-display-message
63          (riece-make-message (riece-current-nickname)
64                              (riece-message-target message)
65                              string 'notice))))))
66
67 (defun riece-eval-form (form)
68   (condition-case err
69       (let ((object (eval (read form))))
70         (cond
71          ((stringp object) object)
72          ((and (listp object)
73                (not (eq object nil)))
74           (let ((string (pp-to-string object)))
75             (substring string 0 (1- (length string)))))
76          ((numberp object)
77           (number-to-string object))
78          ((eq object nil) "")
79          (t (pp-to-string object))))
80     (error
81      (unless riece-eval-ignore-error
82        (format "Error evaluating %s: %s" form err)))))
83
84 (defun riece-eval-insinuate ()
85   (add-hook 'riece-after-display-message-functions
86             'riece-eval-display-message-function))
87
88 (defun riece-eval-uninstall ()
89   (remove-hook 'riece-after-display-message-functions
90                'riece-eval-display-message-function))
91
92 (provide 'riece-eval)
93
94 ;;; riece-eval.el ends here