summaryrefslogtreecommitdiff
path: root/elisp/geiser-debug.el
blob: 20db436b341ae2e62d0bd4d32cd6581958f09192 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
;; geiser-debug.el -- displaying debug information

;; Copyright (C) 2009 Jose Antonio Ortega Ruiz

;; Author: Jose Antonio Ortega Ruiz <jao@gnu.org>
;; Start date: Mon Feb 23, 2009 22:34

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3 of the License, or
;; (at your option) any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Comentary:

;; Buffer and associated mode for displaying results of evaluations
;; and compilations.

;;; Code:

(require 'geiser-eval)
(require 'geiser-popup)
(require 'geiser-base)


;;; Debug buffer mode:

(defconst geiser-debug--error-alist
  '(("^In file \\([^ \n]+\\):\\([0-9]+\\):\\([0-9]+\\)" 1 2 3)
    ("^Error.+$" nil nil nil 0)))

(define-derived-mode geiser-debug-mode compilation-mode "Geiser Dbg"
  "A major mode for displaying Scheme compilation and evaluation results.
\\{geiser-debug-mode-map}"
  (set (make-local-variable 'compilation-error-regexp-alist)
       geiser-debug--error-alist))


;;; Buffer for displaying evaluation results:

(geiser-popup--define debug "*Geiser dbg*" geiser-debug-mode)


;;; Displaying retorts

(defun geiser-debug--display-retort (what ret)
  (let* ((err (geiser-eval--retort-error ret))
         (output (geiser-eval--retort-output ret))
         (stack (geiser-eval--retort-stack ret)))
    (when err
      (geiser-debug--with-buffer
        (erase-buffer)
        (insert what)
        (newline 2)
        (insert (geiser-eval--error-str err) "\n\n")
        (when output (insert output "\n\n"))
        (geiser-debug--display-stack stack)
        (goto-char (point-min)))
      (geiser-debug--pop-to-buffer))))

(defsubst geiser-debug--frame-proc (frame) (cdr (assoc 'procedure frame)))
(defsubst geiser-debug--frame-desc (frame) (cdr (assoc 'description frame)))
(defsubst geiser-debug--frame-source (frame) (cdr (assoc 'source frame)))
(defsubst geiser-debug--frame-source-file (src) (car src))
(defsubst geiser-debug--frame-source-line (src) (or (cadr src) 1))
(defsubst geiser-debug--frame-source-column (src) (or (caddr src) 0))

(defun geiser-debug--display-stack (stack)
  (let* ((frames (cdr stack))
         (step 2)
         (indent (* (length frames) step)))
    (dolist (f frames)
      (geiser-debug--display-stack-frame f indent)
      (setq indent (- indent step)))))

(defun geiser-debug--display-stack-frame (frame offset)
  (let ((procedure (geiser-debug--frame-proc frame))
        (source (geiser-debug--frame-source frame))
        (description (geiser-debug--frame-desc frame)))
    (if source
        (insert (format "In file %s:%s:%s\n"
                        (geiser-debug--frame-source-file source)
                        (geiser-debug--frame-source-line source)
                        (1+ (geiser-debug--frame-source-column source))))
      (insert "In expression:\n"))
    (insert (format "%s%s\n" (make-string offset ?\ ) description))))

(defun geiser-debug--send-region (compile start end and-go)
  (let* ((ret (geiser-eval--send-region compile start end and-go))
         (err (geiser-eval--retort-error ret)))
    (if (not err)
        (message (format "=> %s" (geiser-eval--retort-result ret)))
      (geiser-debug--display-retort str ret))))


(provide 'geiser-debug)
;;; geiser-debug.el ends here