[Maxima-commits] [git] Maxima CAS branch, master, updated. branch-5_31-base-183-gf44d669



>   From: Robert Dodier <robert.dodier at gmail.com>
>   Date: Thu, 12 Dec 2013 19:53:19 +0000
>
>   On 2013-12-12, Leo Butler <l_butler at users.sourceforge.net> wrote:
>
>   > We can disagree on whether it is broken because the mfuncall is not
>   > wrapped in an unwind-protect. Does your home-owner's insurance cover
>   > your lighting your drapes on fire?
>
>   I'm generally in agreement with you, but specifically about wrapping the
>   call to the hook function in unwind-protect, I'm in favor of that.

Ok, below is a patch to commit d8dfa05d19 that wraps the call to the
hook in error protection. I tried as much as possible to use
pre-existing Maxima code to trap errors and to issue some intelligible
messages.

The patch also exposes the default format-prompt function to the
Maxima repl, as Rupert suggested in this thread.

It remains a question on how to handle jumps to the Lisp repl or
debugger. The easiest solution would be to leave that to the user (see
below).

Leo

Examples:

Bad input:

(%i1) alt_format_prompt:x-y$
Error in printing prompt; reverting to default.
apply: found false evaluates to x-y where a function was expected.

(%i2) alt_format_prompt:"==> "$
Error in printing prompt; reverting to default.
symbol


A simple prompt, and a more involved prompt:

(%i3) alt_format_prompt:lambda([[x]],"==> ")$
==> alt_format_prompt:lambda([a,b,c],printf(a,"~a~%",timedate()),block([simp:false],apply(default_format_prompt,[a,b,c])))$

2013-12-15 09:46:16-05:00
(%i4) alt_format_prompt:lambda([d,c,a],block([simp:false],apply(default_format_prompt,[d,"==> ",false])))$
==> to_lisp();
Type (to-maxima) to restart, ($quit) to quit Maxima.
==> (to-maxima)
Returning to Maxima

The debugger:

==> load("/tmp/foo.mac")$

==> bar(2,3);
Bkpt 0:(foo.mac 3)
^Z^Z/tmp/foo.mac:3::
==> :bt
#0: foo(y=5)(stdin line 0)
#1: bar(x=2,y=3)(foo.mac line 11)
==> :n
(foo.mac 4)
^Z^Z/tmp/foo.mac:4::
==> :r




===File ~/git.diff==========================================
diff --git a/src/macsys.lisp b/src/macsys.lisp
index 422a283..050ec1a 100644
--- a/src/macsys.lisp
+++ b/src/macsys.lisp
@@ -29,10 +29,24 @@
 ;; doc/implementation/external-interface.txt.
 (defvar *prompt-prefix* "")
 (defvar *prompt-suffix* "")
-
 (defvar *general-display-prefix* "")
+(defvar $alt_format_prompt nil "If NIL, use DEFAULT-FORMAT-PROMPT to print input prompt; if a function, use it to print input prompt.")
 
 (defun format-prompt (destination control-string &rest arguments)
+  "If $ALT_FORMAT_PROMPT is NIL, use DEFAULT-FORMAT-PROMPT to print
+prompt; otherwise MFUNCALL $ALT_FORMAT_PROMPT to print prompt."
+  (funcall (if $alt_format_prompt #'alt-format-prompt #'default-format-prompt)
+	   destination control-string arguments))
+
+(defun alt-format-prompt (destination control-string arguments)
+  "MFUNCALL $ALT_FORMAT_PROMPT with a heavy coating of error protection."
+  (handler-bind ((error (lambda(msg) (setq $alt_format_prompt nil)
+			       (format t (intl:gettext "Error in printing prompt; reverting to default.~%~a") msg)
+			       (throw 'macsyma-quit 'maxima-error))))
+    (with-$error (let ((prompt (mfuncall $alt_format_prompt destination control-string arguments)))
+		   (if (stringp prompt) prompt (merror "alt_format_prompt returned an object of type ~a, needed a string." (type-of prompt)))))))
+
+(defun default-format-prompt (destination control-string arguments)
   "Like AFORMAT, but add the prefix and suffix configured for a prompt. This
 function deals correctly with the ~M control character, but only when
 DESTINATION is an actual stream (rather than nil for a string)."
@@ -42,6 +56,8 @@ DESTINATION is an actual stream (rather than nil for a string)."
 		 (apply 'aformat destination control-string arguments)
 		 *prompt-suffix*)))
 
+(defvar $default_format_prompt (symbol-function 'default-format-prompt))
+
 ;;  "When time began" (or at least the start of version control history),
 ;;  the following comment was made at this point:
 ;;
============================================================