eieio-opt.el 25 KB
Newer Older
Chong Yidong's avatar
Chong Yidong committed
1 2
;;; eieio-opt.el -- eieio optional functions (debug, printing, speedbar)

3
;; Copyright (C) 1996, 1998-2003, 2005, 2008-2012
4
;;   Free Software Foundation, Inc.
Chong Yidong's avatar
Chong Yidong committed
5

Glenn Morris's avatar
Glenn Morris committed
6
;; Author: Eric M. Ludlam <zappo@gnu.org>
Chong Yidong's avatar
Chong Yidong committed
7
;; Keywords: OO, lisp
8
;; Package: eieio
Chong Yidong's avatar
Chong Yidong committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:
;;
;;   This contains support functions to eieio.  These functions contain
;; some small class browser and class printing functions.
;;

(require 'eieio)
Chong Yidong's avatar
Chong Yidong committed
32 33 34
(require 'button)
(require 'help-mode)
(require 'find-func)
Chong Yidong's avatar
Chong Yidong committed
35 36

;;; Code:
37
;;;###autoload
Chong Yidong's avatar
Chong Yidong committed
38 39 40 41 42 43 44 45 46 47 48 49
(defun eieio-browse (&optional root-class)
  "Create an object browser window to show all objects.
If optional ROOT-CLASS, then start with that, otherwise start with
variable `eieio-default-superclass'."
  (interactive (if current-prefix-arg
		   (list (read (completing-read "Class: "
						(eieio-build-class-alist)
						nil t)))
		 nil))
  (if (not root-class) (setq root-class 'eieio-default-superclass))
  (if (not (class-p root-class)) (signal 'wrong-type-argument (list 'class-p root-class)))
  (display-buffer (get-buffer-create "*EIEIO OBJECT BROWSE*") t)
50
  (with-current-buffer (get-buffer "*EIEIO OBJECT BROWSE*")
Chong Yidong's avatar
Chong Yidong committed
51 52 53 54 55 56
    (erase-buffer)
    (goto-char 0)
    (eieio-browse-tree root-class "" "")
    ))

(defun eieio-browse-tree (this-root prefix ch-prefix)
Juanma Barranquero's avatar
Juanma Barranquero committed
57
  "Recursively draw the children of the given class on the screen.
Chong Yidong's avatar
Chong Yidong committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
Argument THIS-ROOT is the local root of the tree.
Argument PREFIX is the character prefix to use.
Argument CH-PREFIX is another character prefix to display."
  (if (not (class-p (eval this-root))) (signal 'wrong-type-argument (list 'class-p this-root)))
  (let ((myname (symbol-name this-root))
	(chl (aref (class-v this-root) class-children))
	(fprefix (concat ch-prefix "  +--"))
	(mprefix (concat ch-prefix "  |  "))
	(lprefix (concat ch-prefix "     ")))
    (insert prefix myname "\n")
    (while (cdr chl)
      (eieio-browse-tree (car chl) fprefix mprefix)
      (setq chl (cdr chl)))
    (if chl
	(eieio-browse-tree (car chl) fprefix lprefix))
    ))

;;; CLASS COMPLETION / DOCUMENTATION

77
;;;###autoload(defalias 'describe-class 'eieio-describe-class)
Chong Yidong's avatar
Chong Yidong committed
78

79
;;;###autoload
Chong Yidong's avatar
Chong Yidong committed
80 81
(defun eieio-describe-class (class &optional headerfcn)
  "Describe a CLASS defined by a string or symbol.
Glenn Morris's avatar
Glenn Morris committed
82
If CLASS is actually an object, then also display current values of that object.
Chong Yidong's avatar
Chong Yidong committed
83 84 85 86
Optional HEADERFCN should be called to insert a few bits of info first."
  (interactive (list (eieio-read-class "Class: ")))
  (with-output-to-temp-buffer (help-buffer) ;"*Help*"
    (help-setup-xref (list #'eieio-describe-class class headerfcn)
87
		     (called-interactively-p 'interactive))
Chong Yidong's avatar
Chong Yidong committed
88 89 90

    (when headerfcn (funcall headerfcn))
    (prin1 class)
Chong Yidong's avatar
Chong Yidong committed
91 92 93 94 95 96 97 98 99
    (princ " is a")
    (if (class-option class :abstract)
	(princ "n abstract"))
    (princ " class")
    ;; Print file location
    (when (get class 'class-location)
      (princ " in `")
      (princ (file-name-nondirectory (get class 'class-location)))
      (princ "'"))
Chong Yidong's avatar
Chong Yidong committed
100
    (terpri)
Paul Eggert's avatar
Paul Eggert committed
101
    ;; Inheritance tree information
Chong Yidong's avatar
Chong Yidong committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    (let ((pl (class-parents class)))
      (when pl
	(princ " Inherits from ")
	(while pl
	  (princ "`") (prin1 (car pl)) (princ "'")
	  (setq pl (cdr pl))
	  (if pl (princ ", ")))
	(terpri)))
    (let ((ch (class-children class)))
      (when ch
	(princ " Children ")
	(while ch
	  (princ "`") (prin1 (car ch)) (princ "'")
	  (setq ch (cdr ch))
	  (if ch (princ ", ")))
	(terpri)))
    (terpri)
    ;; System documentation
    (let ((doc (documentation-property class 'variable-documentation)))
      (when doc
	(princ "Documentation:")
	(terpri)
	(princ doc)
	(terpri)
	(terpri)))
    ;; Describe all the slots in this class
    (eieio-describe-class-slots class)
    ;; Describe all the methods specific to this class.
    (let ((methods (eieio-all-generic-functions class))
	  (doc nil))
      (if (not methods) nil
	(princ "Specialized Methods:")
	(terpri)
	(terpri)
	(while methods
	  (setq doc (eieio-method-documentation (car methods) class))
	  (princ "`")
	  (prin1 (car methods))
	  (princ "'")
	  (if (not doc)
	      (princ "  Undocumented")
	    (if (car doc)
		(progn
		  (princ "  :STATIC ")
		  (prin1 (car (car doc)))
		  (terpri)
		  (princ (cdr (car doc)))))
	    (setq doc (cdr doc))
	    (if (car doc)
		(progn
		  (princ "  :BEFORE ")
		  (prin1 (car (car doc)))
		  (terpri)
		  (princ (cdr (car doc)))))
	    (setq doc (cdr doc))
	    (if (car doc)
		(progn
		  (princ "  :PRIMARY ")
		  (prin1 (car (car doc)))
		  (terpri)
		  (princ (cdr (car doc)))))
	    (setq doc (cdr doc))
	    (if (car doc)
		(progn
		  (princ "  :AFTER ")
		  (prin1 (car (car doc)))
		  (terpri)
		  (princ (cdr (car doc)))))
	    (terpri)
	    (terpri))
	  (setq methods (cdr methods))))))
173
  (with-current-buffer (help-buffer)
Chong Yidong's avatar
Chong Yidong committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
    (buffer-string)))

(defun eieio-describe-class-slots (class)
  "Describe the slots in CLASS.
Outputs to the standard output."
  (let* ((cv (class-v class))
	 (docs   (aref cv class-public-doc))
	 (names  (aref cv class-public-a))
	 (deflt  (aref cv class-public-d))
	 (types  (aref cv class-public-type))
	 (publp (aref cv class-public-printer))
	 (i      0)
	 (prot   (aref cv class-protection))
	 )
    (princ "Instance Allocated Slots:")
    (terpri)
    (terpri)
    (while names
      (if (car prot) (princ "Private "))
      (princ "Slot: ")
      (prin1 (car names))
      (when (not (eq (aref types i) t))
	(princ "    type = ")
	(prin1 (aref types i)))
      (unless (eq (car deflt) eieio-unbound)
	(princ "    default = ")
	(prin1 (car deflt)))
      (when (car publp)
	(princ "    printer = ")
	(prin1 (car publp)))
      (when (car docs)
	(terpri)
	(princ "  ")
	(princ (car docs))
	(terpri))
      (terpri)
      (setq names (cdr names)
	    docs (cdr docs)
	    deflt (cdr deflt)
	    publp (cdr publp)
	    prot (cdr prot)
	    i (1+ i)))
    (setq docs  (aref cv class-class-allocation-doc)
	  names (aref cv class-class-allocation-a)
	  types (aref cv class-class-allocation-type)
	  i     0
	  prot  (aref cv class-class-allocation-protection))
    (when names
	(terpri)
	(princ "Class Allocated Slots:"))
	(terpri)
	(terpri)
    (while names
      (when (car prot)
	(princ "Private "))
      (princ "Slot: ")
      (prin1 (car names))
      (unless (eq (aref types i) t)
	(princ "    type = ")
	(prin1 (aref types i)))
      (condition-case nil
	  (let ((value (eieio-oref class (car names))))
	    (princ "   value = ")
	    (prin1 value))
	  (error nil))
      (when (car docs)
	(terpri)
	(princ "  ")
	(princ (car docs))
	(terpri))
      (terpri)
      (setq names (cdr names)
	    docs (cdr docs)
	    prot (cdr prot)
	    i (1+ i)))))

250
;;;###autoload
Chong Yidong's avatar
Chong Yidong committed
251 252 253 254 255 256 257 258 259 260 261
(defun eieio-describe-constructor (fcn)
  "Describe the constructor function FCN.
Uses `eieio-describe-class' to describe the class being constructed."
  (interactive
   ;; Use eieio-read-class since all constructors have the same name as
   ;; the class they create.
   (list (eieio-read-class "Class: ")))
  (eieio-describe-class
   fcn (lambda ()
	 ;; Describe the constructor part.
	 (prin1 fcn)
Chong Yidong's avatar
Chong Yidong committed
262 263 264 265 266 267
	 (princ " is an object constructor function")
	 ;; Print file location
	 (when (get fcn 'class-location)
	   (princ " in `")
	   (princ (file-name-nondirectory (get fcn 'class-location)))
	   (princ "'"))
Chong Yidong's avatar
Chong Yidong committed
268 269 270 271 272 273 274 275 276
	 (terpri)
	 (princ "Creates an object of class ")
	 (prin1 fcn)
	 (princ ".")
	 (terpri)
	 (terpri)
	 ))
  )

Chong Yidong's avatar
Chong Yidong committed
277 278 279 280 281 282 283 284 285 286
(defun eieio-build-class-list (class)
  "Return a list of all classes that inherit from CLASS."
  (if (class-p class)
      (apply #'append
	     (mapcar
	      (lambda (c)
		(append (list c) (eieio-build-class-list c)))
	      (class-children-fast class)))
    (list class)))

Chong Yidong's avatar
Chong Yidong committed
287 288 289 290 291 292 293 294
(defun eieio-build-class-alist (&optional class instantiable-only buildlist)
  "Return an alist of all currently active classes for completion purposes.
Optional argument CLASS is the class to start with.
If INSTANTIABLE-ONLY is non nil, only allow names of classes which
are not abstract, otherwise allow all classes.
Optional argument BUILDLIST is more list to attach and is used internally."
  (let* ((cc (or class eieio-default-superclass))
	 (sublst (aref (class-v cc) class-children)))
Chong Yidong's avatar
Chong Yidong committed
295 296 297
    (unless (assoc (symbol-name cc) buildlist)
      (when (or (not instantiable-only) (not (class-abstract-p cc)))
	(setq buildlist (cons (cons (symbol-name cc) 1) buildlist))))
Chong Yidong's avatar
Chong Yidong committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
    (while sublst
      (setq buildlist (eieio-build-class-alist
		       (car sublst) instantiable-only buildlist))
      (setq sublst (cdr sublst)))
    buildlist))

(defvar eieio-read-class nil
  "History of the function `eieio-read-class' prompt.")

(defun eieio-read-class (prompt &optional histvar instantiable-only)
  "Return a class chosen by the user using PROMPT.
Optional argument HISTVAR is a variable to use as history.
If INSTANTIABLE-ONLY is non nil, only allow names of classes which
are not abstract."
  (intern (completing-read prompt (eieio-build-class-alist nil instantiable-only)
			   nil t nil
			   (or histvar 'eieio-read-class))))

(defun eieio-read-subclass (prompt class &optional histvar instantiable-only)
  "Return a class chosen by the user using PROMPT.
CLASS is the base class, and completion occurs across all subclasses.
Optional argument HISTVAR is a variable to use as history.
If INSTANTIABLE-ONLY is non nil, only allow names of classes which
are not abstract."
  (intern (completing-read prompt
			   (eieio-build-class-alist class instantiable-only)
			   nil t nil
			   (or histvar 'eieio-read-class))))

;;; METHOD COMPLETION / DOC

(defalias 'describe-method 'eieio-describe-generic)
330
;;;###autoload(defalias 'describe-generic 'eieio-describe-generic)
Chong Yidong's avatar
Chong Yidong committed
331 332
(defalias 'eieio-describe-method 'eieio-describe-generic)

333
;;;###autoload
Chong Yidong's avatar
Chong Yidong committed
334 335 336 337 338 339 340
(defun eieio-describe-generic (generic)
  "Describe the generic function GENERIC.
Also extracts information about all methods specific to this generic."
  (interactive (list (eieio-read-generic "Generic Method: ")))
  (if (not (generic-p generic))
      (signal 'wrong-type-argument '(generic-p generic)))
  (with-output-to-temp-buffer (help-buffer) ; "*Help*"
341 342
    (help-setup-xref (list #'eieio-describe-generic generic)
		     (called-interactively-p 'interactive))
Chong Yidong's avatar
Chong Yidong committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367

    (prin1 generic)
    (princ " is a generic function")
    (when (generic-primary-only-p generic)
      (princ " with only ")
      (when (generic-primary-only-one-p generic)
	(princ "one "))
      (princ "primary method")
      (when (not (generic-primary-only-one-p generic))
	(princ "s"))
      )
    (princ ".")
    (terpri)
    (terpri)
    (let ((d (documentation generic)))
      (if (not d)
	  (princ "The generic is not documented.\n")
	(princ "Documentation:")
	(terpri)
	(princ d)
	(terpri)
	(terpri)))
    (princ "Implementations:")
    (terpri)
    (terpri)
Chong Yidong's avatar
Chong Yidong committed
368
    (let ((i 4)
Chong Yidong's avatar
Chong Yidong committed
369 370
	  (prefix [ ":STATIC" ":BEFORE" ":PRIMARY" ":AFTER" ] ))
      ;; Loop over fanciful generics
Chong Yidong's avatar
Chong Yidong committed
371
      (while (< i 7)
Chong Yidong's avatar
Chong Yidong committed
372 373 374 375 376 377 378 379 380 381 382
	(let ((gm (aref (get generic 'eieio-method-tree) i)))
	  (when gm
	    (princ "Generic ")
	    (princ (aref prefix (- i 3)))
	    (terpri)
	    (princ (or (nth 2 gm) "Undocumented"))
	    (terpri)
	    (terpri)))
	(setq i (1+ i)))
      (setq i 0)
      ;; Loop over defined class-specific methods
Chong Yidong's avatar
Chong Yidong committed
383 384 385
      (while (< i 4)
	(let ((gm (reverse (aref (get generic 'eieio-method-tree) i)))
	      location)
Chong Yidong's avatar
Chong Yidong committed
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	  (while gm
	    (princ "`")
	    (prin1 (car (car gm)))
	    (princ "'")
	    ;; prefix type
	    (princ " ")
	    (princ (aref prefix i))
	    (princ " ")
	    ;; argument list
	    (let* ((func (cdr (car gm)))
		   (arglst (eieio-lambda-arglist func)))
	      (prin1 arglst))
	    (terpri)
	    ;; 3 because of cdr
	    (princ (or (documentation (cdr (car gm)))
		       "Undocumented"))
Chong Yidong's avatar
Chong Yidong committed
402 403 404 405 406 407 408
	    ;; Print file location if available
	    (when (and (setq location (get generic 'method-locations))
		       (setq location (assoc (caar gm) location)))
	      (setq location (cadr location))
	      (princ "\n\nDefined in `")
	      (princ (file-name-nondirectory location))
	      (princ "'\n"))
Chong Yidong's avatar
Chong Yidong committed
409 410 411 412
	    (setq gm (cdr gm))
	    (terpri)
	    (terpri)))
	(setq i (1+ i)))))
413
  (with-current-buffer (help-buffer)
Chong Yidong's avatar
Chong Yidong committed
414 415 416 417 418 419 420 421 422 423 424
    (buffer-string)))

(defun eieio-lambda-arglist (func)
  "Return the argument list of FUNC, a function body."
  (if (symbolp func) (setq func (symbol-function func)))
  (if (byte-code-function-p func)
      (eieio-compiled-function-arglist func)
    (car (cdr func))))

(defun eieio-all-generic-functions (&optional class)
  "Return a list of all generic functions.
Juanma Barranquero's avatar
Juanma Barranquero committed
425 426
Optional CLASS argument returns only those functions that contain
methods for CLASS."
Chong Yidong's avatar
Chong Yidong committed
427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
  (let ((l nil) tree (cn (if class (symbol-name class) nil)))
    (mapatoms
     (lambda (symbol)
       (setq tree (get symbol 'eieio-method-obarray))
       (if tree
	   (progn
	     ;; A symbol might be interned for that class in one of
	     ;; these three slots in the method-obarray.
	     (if (or (not class)
		     (fboundp (intern-soft cn (aref tree 0)))
		     (fboundp (intern-soft cn (aref tree 1)))
		     (fboundp (intern-soft cn (aref tree 2))))
		 (setq l (cons symbol l)))))))
    l))

(defun eieio-method-documentation (generic class)
  "Return a list of the specific documentation of GENERIC for CLASS.
If there is not an explicit method for CLASS in GENERIC, or if that
function has no documentation, then return nil."
  (let ((tree (get generic 'eieio-method-obarray))
	(cn (symbol-name class))
	before primary after)
    (if (not tree)
	nil
      ;; A symbol might be interned for that class in one of
      ;; these three slots in the method-obarray.
      (setq before (intern-soft cn (aref tree 0))
	    primary (intern-soft cn (aref tree 1))
	    after (intern-soft cn (aref tree 2)))
      (if (not (or (fboundp before)
		   (fboundp primary)
		   (fboundp after)))
	  nil
	(list (if (fboundp before)
		  (cons (eieio-lambda-arglist before)
			(documentation before))
		nil)
	      (if (fboundp primary)
		  (cons (eieio-lambda-arglist primary)
			(documentation primary))
		nil)
	      (if (fboundp after)
		  (cons (eieio-lambda-arglist after)
			(documentation after))
		nil))))))

(defvar eieio-read-generic nil
  "History of the `eieio-read-generic' prompt.")

(defun eieio-read-generic-p (fn)
  "Function used in function `eieio-read-generic'.
This is because `generic-p' is a macro.
Argument FN is the function to test."
  (generic-p fn))

(defun eieio-read-generic (prompt &optional historyvar)
  "Read a generic function from the minibuffer with PROMPT.
Optional argument HISTORYVAR is the variable to use as history."
  (intern (completing-read prompt obarray 'eieio-read-generic-p
			   t nil (or historyvar 'eieio-read-generic))))

;;; METHOD STATS
;;
;; Dump out statistics about all the active methods in a session.
(defun eieio-display-method-list ()
  "Display a list of all the methods and what features are used."
  (interactive)
  (let* ((meth1 (eieio-all-generic-functions))
	 (meth (sort meth1 (lambda (a b)
			     (string< (symbol-name a)
				      (symbol-name b)))))
	 (buff (get-buffer-create "*EIEIO Method List*"))
	 (methidx 0)
	 (standard-output buff)
	 (slots '(method-static
		  method-before
		  method-primary
		  method-after
		  method-generic-before
		  method-generic-primary
		  method-generic-after))
	 (slotn '("static"
		  "before"
		  "primary"
		  "after"
		  "G bef"
		  "G prim"
		  "G aft"))
	 (idxarray (make-vector (length slots) 0))
	 (primaryonly 0)
	 (oneprimary 0)
	 )
    (switch-to-buffer-other-window buff)
    (erase-buffer)
    (dolist (S slotn)
      (princ S)
      (princ "\t")
      )
    (princ "Method Name")
    (terpri)
    (princ "--------------------------------------------------------------------")
    (terpri)
    (dolist (M meth)
      (let ((mtree (get M 'eieio-method-tree))
	    (P nil) (numP)
	    (!P nil))
	(dolist (S slots)
	  (let ((num (length (aref mtree (symbol-value S)))))
	    (aset idxarray (symbol-value S)
		  (+ num (aref idxarray (symbol-value S))))
	    (prin1 num)
	    (princ "\t")
	    (when (< 0 num)
	      (if (eq S 'method-primary)
		  (setq P t numP num)
		(setq !P t)))
	    ))
	;; Is this a primary-only impl method?
	(when (and P (not !P))
	  (setq primaryonly (1+ primaryonly))
	  (when (= numP 1)
	    (setq oneprimary (1+ oneprimary))
	    (princ "*"))
	  (princ "* ")
	  )
	(prin1 M)
	(terpri)
	(setq methidx (1+ methidx))
	)
      )
    (princ "--------------------------------------------------------------------")
    (terpri)
    (dolist (S slots)
      (prin1 (aref idxarray (symbol-value S)))
      (princ "\t")
      )
    (prin1 methidx)
    (princ " Total symbols")
    (terpri)
    (dolist (S slotn)
      (princ S)
      (princ "\t")
      )
    (terpri)
    (terpri)
    (princ "Methods Primary Only: ")
    (prin1 primaryonly)
    (princ "\t")
    (princ (format "%d" (* (/ (float primaryonly) (float methidx)) 100)))
    (princ "% of total methods")
    (terpri)
    (princ "Only One Primary Impl: ")
    (prin1 oneprimary)
    (princ "\t")
    (princ (format "%d" (* (/ (float oneprimary) (float primaryonly)) 100)))
    (princ "% of total primary methods")
    (terpri)
    ))

;;; HELP AUGMENTATION
;;
Chong Yidong's avatar
Chong Yidong committed
588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
(define-button-type 'eieio-method-def
  :supertype 'help-xref
  'help-function (lambda (class method file)
		   (eieio-help-find-method-definition class method file))
  'help-echo (purecopy "mouse-2, RET: find method's definition"))

(define-button-type 'eieio-class-def
  :supertype 'help-xref
  'help-function (lambda (class file)
		   (eieio-help-find-class-definition class file))
  'help-echo (purecopy "mouse-2, RET: find class definition"))

(defun eieio-help-find-method-definition (class method file)
  (let ((filename (find-library-name file))
	location buf)
    (when (null filename)
      (error "Cannot find library %s" file))
    (setq buf (find-file-noselect filename))
    (with-current-buffer buf
      (goto-char (point-min))
      (when
	  (re-search-forward
	   ;; Regexp for searching methods.
	   (concat "(defmethod[ \t\r\n]+" method
		   "\\([ \t\r\n]+:[a-zA-Z]+\\)?"
		   "[ \t\r\n]+(\\s-*(\\(\\sw\\|\\s_\\)+\\s-+"
		   class
		   "\\s-*)")
	   nil t)
	(setq location (match-beginning 0))))
    (if (null location)
	(message "Unable to find location in file")
      (pop-to-buffer buf)
      (goto-char location)
      (recenter)
      (beginning-of-line))))

(defun eieio-help-find-class-definition (class file)
  (let ((filename (find-library-name file))
	location buf)
    (when (null filename)
      (error "Cannot find library %s" file))
    (setq buf (find-file-noselect filename))
    (with-current-buffer buf
      (goto-char (point-min))
      (when
	  (re-search-forward
	   ;; Regexp for searching a class.
	   (concat "(defclass[ \t\r\n]+" class "[ \t\r\n]+")
	   nil t)
	(setq location (match-beginning 0))))
    (if (null location)
	(message "Unable to find location in file")
      (pop-to-buffer buf)
      (goto-char location)
      (recenter)
      (beginning-of-line))))


Chong Yidong's avatar
Chong Yidong committed
647
(defun eieio-help-mode-augmentation-maybee (&rest unused)
Juanma Barranquero's avatar
Juanma Barranquero committed
648
  "For buffers thrown into help mode, augment for EIEIO.
Chong Yidong's avatar
Chong Yidong committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
Arguments UNUSED are not used."
  ;; Scan created buttons so far if we are in help mode.
  (when (eq major-mode 'help-mode)
    (save-excursion
      (goto-char (point-min))
      (let ((pos t) (inhibit-read-only t))
	(while pos
	  (if (get-text-property (point) 'help-xref) ; move off reference
	      (goto-char
	       (or (next-single-property-change (point) 'help-xref)
		   (point))))
	  (setq pos (next-single-property-change (point) 'help-xref))
	  (when pos
	    (goto-char pos)
	    (let* ((help-data (get-text-property (point) 'help-xref))
		   ;(method (car help-data))
		   (args (cdr help-data)))
	      (when (symbolp (car args))
		(cond ((class-p (car args))
		       (setcar help-data 'eieio-describe-class))
		      ((generic-p (car args))
		       (setcar help-data 'eieio-describe-generic))
		      (t nil))
		))))
	;; start back at the beginning, and highlight some sections
	(goto-char (point-min))
	(while (re-search-forward "^\\(Documentation\\|Implementations\\):$" nil t)
	    (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
	(goto-char (point-min))
	(if (re-search-forward "^Specialized Methods:$" nil t)
	    (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
	(goto-char (point-min))
	(while (re-search-forward "^\\(Instance\\|Class\\) Allocated Slots:$" nil t)
	    (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
	(goto-char (point-min))
	(while (re-search-forward ":\\(STATIC\\|BEFORE\\|AFTER\\|PRIMARY\\)" nil t)
	    (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
	(goto-char (point-min))
	(while (re-search-forward "^\\(Private \\)?Slot:" nil t)
	    (put-text-property (match-beginning 0) (match-end 0) 'face 'bold))
Chong Yidong's avatar
Chong Yidong committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
	(goto-char (point-min))
	(cond
	 ((looking-at "\\(.+\\) is a generic function")
	  (let ((mname (match-string 1))
		cname)
	    (while (re-search-forward "^`\\(.+\\)'[^\0]+?Defined in `\\(.+\\)'" nil t)
	      (setq cname (match-string-no-properties 1))
	      (help-xref-button 2 'eieio-method-def cname
				mname
				(cadr (assoc (intern cname)
					     (get (intern mname)
						  'method-locations)))))))
	 ((looking-at "\\(.+\\) is an object constructor function in `\\(.+\\)'")
	  (let ((cname (match-string-no-properties 1)))
	    (help-xref-button 2 'eieio-class-def cname
			      (get (intern cname) 'class-location))))
	 ((looking-at "\\(.+\\) is a\\(n abstract\\)? class in `\\(.+\\)'")
	  (let ((cname (match-string-no-properties 1)))
	    (help-xref-button 3 'eieio-class-def cname
			      (get (intern cname) 'class-location)))))
Chong Yidong's avatar
Chong Yidong committed
709 710 711 712 713 714 715
	))))

;;; SPEEDBAR SUPPORT
;;
(eval-when-compile
  (condition-case nil
      (require 'speedbar)
Juanma Barranquero's avatar
Juanma Barranquero committed
716
    (error (message "Error loading speedbar... ignored"))))
Chong Yidong's avatar
Chong Yidong committed
717 718 719 720 721

(defvar eieio-class-speedbar-key-map nil
  "Keymap used when working with a project in speedbar.")

(defun eieio-class-speedbar-make-map ()
Juanma Barranquero's avatar
Juanma Barranquero committed
722
  "Make a keymap for EIEIO under speedbar."
Chong Yidong's avatar
Chong Yidong committed
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
  (setq eieio-class-speedbar-key-map (speedbar-make-specialized-keymap))

  ;; General viewing stuff
  (define-key eieio-class-speedbar-key-map "\C-m" 'speedbar-edit-line)
  (define-key eieio-class-speedbar-key-map "+" 'speedbar-expand-line)
  (define-key eieio-class-speedbar-key-map "-" 'speedbar-contract-line)
  )

(if eieio-class-speedbar-key-map
    nil
  (if (not (featurep 'speedbar))
      (add-hook 'speedbar-load-hook (lambda ()
				      (eieio-class-speedbar-make-map)
				      (speedbar-add-expansion-list
				       '("EIEIO"
					 eieio-class-speedbar-menu
					 eieio-class-speedbar-key-map
					 eieio-class-speedbar))))
    (eieio-class-speedbar-make-map)
    (speedbar-add-expansion-list '("EIEIO"
				   eieio-class-speedbar-menu
				   eieio-class-speedbar-key-map
				   eieio-class-speedbar))))

(defvar eieio-class-speedbar-menu
  ()
  "Menu part in easymenu format used in speedbar while in `eieio' mode.")

(defun eieio-class-speedbar (dir-or-object depth)
  "Create buttons in speedbar that represents the current project.
Juanma Barranquero's avatar
Juanma Barranquero committed
753 754
DIR-OR-OBJECT is the object to expand, or nil, and DEPTH is the
current expansion depth."
Chong Yidong's avatar
Chong Yidong committed
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810
  (when (eq (point-min) (point-max))
    ;; This function is only called once, to start the whole deal.
    ;; Ceate, and expand the default object.
    (eieio-class-button eieio-default-superclass 0)
    (forward-line -1)
    (speedbar-expand-line)))

(defun eieio-class-button (class depth)
  "Draw a speedbar button at the current point for CLASS at DEPTH."
  (if (not (class-p class))
      (signal 'wrong-type-argument (list 'class-p class)))
  (let ((subclasses (aref (class-v class) class-children)))
    (if subclasses
	(speedbar-make-tag-line 'angle ?+
				'eieio-sb-expand
				class
				(symbol-name class)
				'eieio-describe-class-sb
				class
				'speedbar-directory-face
				depth)
      (speedbar-make-tag-line 'angle ?  nil nil
			      (symbol-name class)
			      'eieio-describe-class-sb
			      class
			      'speedbar-directory-face
			      depth))))

(defun eieio-sb-expand (text class indent)
  "For button TEXT, expand CLASS at the current location.
Argument INDENT is the depth of indentation."
  (cond ((string-match "+" text)	;we have to expand this file
	 (speedbar-change-expand-button-char ?-)
	 (speedbar-with-writable
	   (save-excursion
	     (end-of-line) (forward-char 1)
	     (let ((subclasses (aref (class-v class) class-children)))
	       (while subclasses
		 (eieio-class-button (car subclasses) (1+ indent))
		 (setq subclasses (cdr subclasses)))))))
	((string-match "-" text)	;we have to contract this node
	 (speedbar-change-expand-button-char ?+)
	 (speedbar-delete-subblock indent))
	(t (error "Ooops...  not sure what to do")))
  (speedbar-center-buffer-smartly))

(defun eieio-describe-class-sb (text token indent)
  "Describe the class TEXT in TOKEN.
INDENT is the current indentation level."
  (speedbar-with-attached-buffer
   (eieio-describe-class token))
  (speedbar-maybee-jump-to-attached-frame))

(provide 'eieio-opt)

;;; eieio-opt.el ends here