;;AUTOLOAD: PV
;;AUTODOC: PV "prints value of variables"
;;(PV exprs...)              special form, quotes args
;;
;; Prints in a user-friendly way the value of variables in the form
;;     [foo] = <foo value>
;; If arguments are strings, they are written as is, and if numbers
;; sets the indent level. Other are evaluated and printed.
;; Prints on stderr, prefixing data with >>, word-wrapping data at end of line
;; (via the print-margin-item package)
;; e.g.:
;;     (PV foo bar (+ x y))  ==> prints:
;;     >> [foo] = "toto", [bar] = "tata", [(+ x y)] = 47
;;SEE_ALSO: print-margin-item PVD debug-print debug?


print-margin-item			;autoloads it

(defunq PV (&rest PV:args &aux
    (print-margin-item:prompts (copy 
	'(">> " ">> " "" ">> " ",  " "\\" "     ")))
  )
  (with (*standard-output* *standard-error*)
    (print-margin-item :begin)
    (dolist (PV:arg PV:args)
      (if (= (type-of PV:arg) String)
	(print-margin-item PV:arg)
	
	(typep PV:arg Number)
	(with (prompt (+ ">> " (make-string (* 2 PV:arg) #\ )))
	  (put print-margin-item:prompts 0 prompt)
	  (put print-margin-item:prompts 1 prompt)
	  (put print-margin-item:prompts 3 prompt)
	  (print-margin-item :break)
	)
	
	(with (PV:line (print-format String "[%l0] = %l1" PV:arg 
	      (PV:value-of PV:arg)))
	  (print-margin-item PV:line)
	)
    ))
    (print-margin-item :end)
))

(defvar debug ())			;define debug so we are sure it exists

;;AUTOLOAD: PVD
;;AUTODOC: PVD "PV if debug"
;;PVD is like PV but is inactived while the debug variable is nil or undefined
;;SEE_ALSO: PV debug? debug-print

(defunq PVD (&rest PVD:args)
  (if debug (apply PV PVD:args))
)

;; returns a variable value  without failing it if is undefined

(defun PV:value-of (var)
  (if (or (not (typep var Symbol)) (boundp var))
    (eval var)
    (intern "*undefined* ")		;note the ending blank :-)
))

;;AUTOLOAD: debug-print
;;AUTODOC: debug-print "prints if debug"
;;(debug-print exprs...)
;;
;;debug-print is like print-format, except that it prints on *standard-error* 
;;only if the global varibale "debug" is set
;;e.g: 
;;    (debug-print "x = %0\n" x)
;;SEE_ALSO: PV PVD debug?

(defun debug-print (&rest args)
  (if debug (progn
      (apply print-format (+ (list *standard-error*) args))
      (print-format *standard-error* "\n")
      (flush *standard-error*)
)))

;;AUTOLOAD: debug?
;;AUTODOC: debug? "execute code in debug mode"
;;(debug? forms...)                      special form, quotes args
;;
;;debug? executes the forms given in arguments only when the global variable 
;;debug is set
;;SEE_ALSO: PV PVD debug-print

(defunq debug? (&rest debug:args)
  (if debug (eval `(progn ,debug:args)))
)
