eieio-datadebug.el 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. ;;; eieio-datadebug.el --- EIEIO extensions to the data debugger.
  2. ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric M. Ludlam <zappo@gnu.org>
  4. ;; Keywords: OO, lisp
  5. ;; Package: eieio
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; Extensions to data-debug for EIEIO objects.
  20. ;;
  21. (require 'eieio)
  22. (require 'data-debug)
  23. ;;; Code:
  24. (defun data-debug-insert-object-slots (object prefix)
  25. "Insert all the slots of OBJECT.
  26. PREFIX specifies what to insert at the start of each line."
  27. (let ((attrprefix (concat (make-string (length prefix) ? ) "] ")))
  28. (data-debug/eieio-insert-slots object attrprefix)))
  29. (defun data-debug-insert-object-slots-from-point (point)
  30. "Insert the object slots found at the object button at POINT."
  31. (let ((object (get-text-property point 'ddebug))
  32. (indent (get-text-property point 'ddebug-indent))
  33. start)
  34. (end-of-line)
  35. (setq start (point))
  36. (forward-char 1)
  37. (data-debug-insert-object-slots object
  38. (concat (make-string indent ? )
  39. "~ "))
  40. (goto-char start)))
  41. (defun data-debug-insert-object-button (object prefix prebuttontext)
  42. "Insert a button representing OBJECT.
  43. PREFIX is the text that precedes the button.
  44. PREBUTTONTEXT is some text between PREFIX and the object button."
  45. (let ((start (point))
  46. (end nil)
  47. (str (object-print object))
  48. (tip (format "Object %s\nClass: %S\nParent(s): %S\n%d slots"
  49. (object-name-string object)
  50. (object-class object)
  51. (class-parents (object-class object))
  52. (length (object-slots object))
  53. ))
  54. )
  55. (insert prefix prebuttontext str)
  56. (setq end (point))
  57. (put-text-property (- end (length str)) end 'face 'font-lock-keyword-face)
  58. (put-text-property start end 'ddebug object)
  59. (put-text-property start end 'ddebug-indent(length prefix))
  60. (put-text-property start end 'ddebug-prefix prefix)
  61. (put-text-property start end 'help-echo tip)
  62. (put-text-property start end 'ddebug-function
  63. 'data-debug-insert-object-slots-from-point)
  64. (insert "\n")))
  65. ;;; METHODS
  66. ;;
  67. ;; Each object should have an opportunity to show stuff about itself.
  68. (defmethod data-debug/eieio-insert-slots ((obj eieio-default-superclass)
  69. prefix)
  70. "Insert the slots of OBJ into the current DDEBUG buffer."
  71. (data-debug-insert-thing (object-name-string obj)
  72. prefix
  73. "Name: ")
  74. (let* ((cl (object-class obj))
  75. (cv (class-v cl)))
  76. (data-debug-insert-thing (class-constructor cl)
  77. prefix
  78. "Class: ")
  79. ;; Loop over all the public slots
  80. (let ((publa (aref cv class-public-a))
  81. (publd (aref cv class-public-d))
  82. )
  83. (while publa
  84. (if (slot-boundp obj (car publa))
  85. (let ((i (class-slot-initarg cl (car publa)))
  86. (v (eieio-oref obj (car publa))))
  87. (data-debug-insert-thing
  88. v prefix (concat
  89. (if i (symbol-name i)
  90. (symbol-name (car publa)))
  91. " ")))
  92. ;; Unbound case
  93. (let ((i (class-slot-initarg cl (car publa))))
  94. (data-debug-insert-custom
  95. "#unbound" prefix
  96. (concat (if i (symbol-name i)
  97. (symbol-name (car publa)))
  98. " ")
  99. 'font-lock-keyword-face))
  100. )
  101. (setq publa (cdr publa) publd (cdr publd))))))
  102. ;;; Augment the Data debug thing display list.
  103. (data-debug-add-specialized-thing (lambda (thing) (object-p thing))
  104. #'data-debug-insert-object-button)
  105. ;;; DEBUG METHODS
  106. ;;
  107. ;; A generic function to run DDEBUG on an object and popup a new buffer.
  108. ;;
  109. (defmethod data-debug-show ((obj eieio-default-superclass))
  110. "Run ddebug against any EIEIO object OBJ."
  111. (data-debug-new-buffer (format "*%s DDEBUG*" (object-name obj)))
  112. (data-debug-insert-object-slots obj "]"))
  113. ;;; DEBUG FUNCTIONS
  114. ;;
  115. (defun eieio-debug-methodinvoke (method class)
  116. "Show the method invocation order for METHOD with CLASS object."
  117. (interactive "aMethod: \nXClass Expression: ")
  118. (let* ((eieio-pre-method-execution-hooks
  119. (lambda (l) (throw 'moose l) ))
  120. (data
  121. (catch 'moose (eieio-generic-call
  122. method (list class))))
  123. (buf (data-debug-new-buffer "*Method Invocation*"))
  124. (data2 (mapcar (lambda (sym)
  125. (symbol-function (car sym)))
  126. data)))
  127. (data-debug-insert-thing data2 ">" "")))
  128. (provide 'eieio-datadebug)
  129. ;;; eieio-datadebug.el ends here