Make sure not to move cursor, when the buffer is frozen and selected.
[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., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
24
25 ;;; Code:
26
27 (require 'riece-globals)
28 (require 'riece-options)
29
30 (defun riece-debug-1 (message detail)
31   (message "riece-debug: %s" message)
32   (with-current-buffer riece-debug-buffer
33     (goto-char (point-max))
34     (let ((time (format-time-string "%Y-%m-%d:%H:%M:%S")))
35       (if detail
36           (insert "*** " time ": " message "\n" detail "\n")
37         (insert "*** " time ": " message "\n")))))
38
39 (defun riece-debug (message &optional detail)
40   "Print a one-line debug MESSAGE at the bottom of the frame.
41 If the optional 2nd argument DETAIL is specified, it is stored into
42 `riece-debug-buffer'."
43   (ignore (riece-debug-1 message detail)))
44
45 (defun riece-debug-reset-standard-output ()
46   "Reset `riece-temp-buffer' to be used as `standard-output'."
47   (with-current-buffer riece-temp-buffer
48     (buffer-disable-undo)
49     (erase-buffer)))
50
51 (defmacro riece-debug-with-backtrace (&rest body)
52   "Execute BODY and send a backtrace to `riece-temp-buffer'."
53   `(unwind-protect
54        (progn ,@body)
55      (riece-debug-reset-standard-output)
56      (let ((standard-output riece-temp-buffer))
57        (backtrace))))
58
59 (put 'riece-debug-with-backtrace 'lisp-indent-function 0)
60 (put 'riece-debug-with-backtrace 'edebug-form-spec '(form body))
61
62 (defmacro riece-ignore-errors (location &rest body)
63   "Execute BODY; if an error occurs, return nil.
64 Otherwise, return result of last FORM.
65 If `riece-debug' is non-nil and an error occurred, it sends a
66 backtrace to standard-output."
67   `(condition-case error
68        (if riece-debug
69            (riece-debug-with-backtrace ,@body)
70          ,@body)
71      (error
72       (if riece-debug
73           (with-current-buffer riece-temp-buffer
74             (goto-char (point-min))
75             (if (re-search-forward "^  signal(" nil t)
76                 (delete-region (point-min) (match-beginning 0)))
77             (riece-debug (format "Error in `%s': %S" ,location error)
78                          (buffer-string))))
79       nil)))
80
81 (put 'riece-ignore-errors 'lisp-indent-function 1)
82 (put 'riece-ignore-errors 'edebug-form-spec '(form body))
83
84 (defun riece-funcall-ignore-errors (location function &rest args)
85   "Call FUNCTION with ARGS; if an error occurs, return nil.
86 Otherwise, return result of the function.
87 If `riece-debug' is non-nil and an error occurred, it sends a
88 backtrace to standard-output."
89   (riece-ignore-errors location
90     (apply function args)))
91
92 (provide 'riece-debug)
93
94 ;;; riece-debug.el ends here