Fixed.
[riece] / lisp / riece-debug.el
1 ;;; riece-debug.el --- debug support
2 ;; Copyright (C) 1998-2005 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 ;;; Code:
26
27 (require 'riece-globals)
28
29 (defun riece-debug-reset-standard-output ()
30   "Reset `riece-temp-buffer' to be used as `standard-output'."
31   (save-excursion
32     (set-buffer riece-temp-buffer)
33     (buffer-disable-undo)
34     (erase-buffer)))
35
36 (defmacro riece-debug-with-backtrace (&rest body)
37   "Execute BODY and send a backtrace to `riece-temp-buffer'."
38   `(unwind-protect
39        (progn ,@body)
40      (riece-debug-reset-standard-output)
41      (let ((standard-output riece-temp-buffer))
42        (backtrace))))
43
44 (put 'riece-debug-with-backtrace 'lisp-indent-function 0)
45 (put 'riece-debug-with-backtrace 'edebug-form-spec '(form body))
46
47 (defmacro riece-ignore-errors (location &rest body)
48   "Execute BODY; if an error occurs, return nil.
49 Otherwise, return result of last FORM.
50 If `riece-debug' is non-nil and an error occurred, it sends a
51 backtrace to standard-output."
52   `(condition-case error
53        (if riece-debug
54            (riece-debug-with-backtrace ,@body)
55          ,@body)
56      (error
57       (if riece-debug
58           (save-excursion
59             (set-buffer riece-temp-buffer)
60             (goto-char (point-min))
61             (if (re-search-forward "^  signal(" nil t)
62                 (delete-region (point-min) (match-beginning 0)))
63             (message "Error in `%s': %S\n%s" ,location error (buffer-string))))
64       nil)))
65
66 (put 'riece-ignore-errors 'lisp-indent-function 1)
67 (put 'riece-ignore-errors 'edebug-form-spec '(form body))
68
69 (defun riece-funcall-ignore-errors (location function &rest args)
70   "Call FUNCTION with ARGS; if an error occurs, return nil.
71 Otherwise, return result of the function.
72 If `riece-debug' is non-nil and an error occurred, it sends a
73 backtrace to standard-output."
74   (riece-ignore-errors location
75     (apply function args)))
76
77 (provide 'riece-debug)
78
79 ;;; riece-debug.el ends here