Author Topic: when error, I want Restore the "textstyle"  (Read 6115 times)

0 Members and 1 Guest are viewing this topic.

reltro

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #15 on: June 06, 2014, 11:16:27 AM »
@ Reltro:
Interesting code. I have a question about this portion:
Code: [Select]
(if (not (wcmatch msg "*EXIT*,*BREAK*,*CANCEL*"))
  (progn
    (princ "\n*** error ***\n")
    (princ (strcat "\t" name ": " (vl-princ-to-string msg) "\n"))

    (*error* msg)
    (princ)
  )
)

If an EXIT/BREAK/CANCEL error occurs the restored *error* function is not called meaning only a single level is undone.

If a different error occurs the restored *error* function is called meaning all levels are undone. And there will also be a superfluous call to the 'default' *error* function.

I may be missing something but this does not seem logical.

Oh shit...
roy_043 u r totally right... this is a mistake...

Found an other issue...
should be (wcmatch (strcase msg) ...)

corrected version
Code - Auto/Visual Lisp: [Select]
  1. ;Note:
  2. ;    *error* should not be localized!!!
  3.  
  4.  
  5. (defun ini*error* (name sysvars / *doc*)
  6.     (eval
  7.         (list 'defun '*error* '(msg / )
  8.             (list
  9.                 (lambda (name prevErrorHandler SysVars *doc* currentcmdecho / )
  10.                     (mapcar
  11.                         'setvar
  12.                         (mapcar 'car SysVars)
  13.                         (mapcar 'cdr SysVars)
  14.                     )
  15.                     (vla-endundomark *doc*)
  16.                     (setq *error* prevErrorHandler)
  17.                     (if msg
  18.                         (progn
  19.                             (setvar 'cmdecho 0)
  20.                             (command-s "_.undo" 1 "")
  21.                             (setvar 'cmdecho currentcmdecho)
  22.  
  23.                             (if (not (wcmatch (strcase msg) "*EXIT*,*BREAK*,*CANCEL*"))
  24.                                 (progn
  25.                                     (princ "\n*** error ***\n")
  26.                                     (princ (strcat "\t" name ": " (vl-princ-to-string msg) "\n"))
  27.  
  28.                                     (princ)
  29.                                 )
  30.                             )
  31.                                                        
  32.                             (*error* msg)
  33.                         )
  34.                     )
  35.                 )
  36.                 (vl-princ-to-string name)
  37.                 *error*
  38.                 (list 'quote
  39.                     (mapcar
  40.                         '(lambda (a / ) (cons a (getvar a)))
  41.                         sysvars
  42.                     )
  43.                 )
  44.                 *doc*
  45.                 (getvar 'cmdecho)
  46.             )
  47.         )
  48.     )    
  49. )
  50.  
  51. (ini*error* ;initialize error-handler
  52.     "String to display if an error occurs to identify wich function fails: "
  53.     '("CLAYER" "OSMODE" "CMDECHO" "DIMZIN") ;LIST of Sysvars to save/restore; String or Sym
  54. )
  55.  
  56. ;other stuff to do....
  57.  
  58. (*error* nil) ;restore Sys-Vars, and a previous-error-handler
  59.  

reltro

reltro

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #16 on: June 06, 2014, 12:04:12 PM »
DOES NOT WORK

Code - Auto/Visual Lisp: [Select]
  1. (defun initializeError (name /  *doc*)
  2.    
  3.     (eval
  4.         (list 'defun-q '*error* '(msg / )
  5.             (list 'setq 'setvar setvar)
  6.             nil
  7.             (list
  8.                 (lambda (name *doc* prevErr / )
  9.                     (vla-endundomark *doc*)
  10.                    
  11.                     (setq *error* prevErr)
  12.                    
  13.                     (if msg
  14.                         (progn
  15.                             (setvar 'cmdecho 0)
  16.                             (command-s "_.undo" 1 "")
  17.                             (setvar 'cmdecho currentcmdecho)
  18.  
  19.                             (if (not (wcmatch (strcase msg) "*EXIT*,*BREAK*,*CANCEL*"))
  20.                                 (progn
  21.                                     (princ "\n*** error ***\n")
  22.                                     (princ (strcat "\t" name ": " msg "\n"))
  23.  
  24.                                     (princ)
  25.                                 )
  26.                             )
  27.                            
  28.                             (*error* msg)
  29.                         )
  30.                     )
  31.                 )
  32.                 (vl-princ-to-string name)
  33.                 *doc*
  34.                 (list 'quote *error*)
  35.             )
  36.         )
  37.     )
  38.    
  39.     (eval
  40.         (list 'defun 'setvar '(n v / )
  41.             (list
  42.                 (lambda (S store / )
  43.                     (eval
  44.                         (vl-list*
  45.                             'defun-q '*error* '(msg / )
  46.                             (cadr *error*)
  47.                             (list 'setvar (vl-princ-to-string n) store)
  48.                             (cddr *error*)
  49.                         )
  50.                     )
  51.                     (S n v)
  52.                 )
  53.                 setvar
  54.                 (list getvar 'n)
  55.             )
  56.         )
  57.     )
  58. )
  59.  

This one would be nice too, I think, but overwriting 'setvar does not work...  :-(

reltro
« Last Edit: June 06, 2014, 01:20:50 PM by reltro »

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #17 on: June 10, 2014, 02:35:47 AM »
Thanks ,reltro

reltro

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #18 on: June 10, 2014, 01:01:38 PM »
I'm not sure if this is good stuff or not...
Im overwriting setvar to catch all changes and restore changed sysvars if an error occurs or the errorHandler is called with nil...

Code - Auto/Visual Lisp: [Select]
  1. (defun INIerror (name /  *doc*)
  2.  
  3.     (eval
  4.         (list 'defun-q '*error* '(msg / )
  5.             (list
  6.                 (lambda (s / )
  7.                     (pragma '((unprotect-assign setvar)))
  8.                     (setq setvar s)
  9.                     (pragma '((protect-assign setvar)))
  10.                 )
  11.                 setvar
  12.             )
  13.             (list
  14.                 (lambda (store / )
  15.                     (mapcar
  16.                         '(lambda (a / )
  17.                             (setvar (car a) (cdr a))
  18.                         )
  19.                         store
  20.                     )
  21.                 )
  22.                 nil
  23.             )
  24.             (list
  25.                 (lambda (name *doc* prevErr  / currentcmdecho)
  26.                     (vla-endundomark *doc*)
  27.                     (setq *error* prevErr)
  28.  
  29.                     (if msg
  30.                         (progn
  31.                             (setq currentcmdecho (getvar 'cmdecho))
  32.                             (setvar 'cmdecho 0)
  33.                             (command-s "_.undo" 1 "")
  34.                             (setvar 'cmdecho currentcmdecho)
  35.  
  36.                             (if (not (wcmatch (strcase msg) "*EXIT*,*BREAK*,*CANCEL*"))
  37.                                 (progn
  38.                                     (princ "\n*** error ***\n")
  39.                                     (princ (strcat "\t" name ": " msg "\n"))
  40.  
  41.                                     (princ)
  42.                                 )
  43.                             )
  44.  
  45.                             (*error* msg)
  46.                         )
  47.                     )
  48.                 )
  49.                 (vl-princ-to-string name)
  50.                 *doc*
  51.                 (list 'quote *error*)
  52.             )
  53.         )
  54.     )
  55.  
  56.     (pragma '((unprotect-assign setvar)))
  57.    
  58.     (eval
  59.         (list 'defun 'setvar '(n v / )
  60.             (list
  61.                 (lambda (S store / )
  62.                     (setq n (strcase (vl-princ-to-string n)))
  63.                     (eval
  64.                         (vl-list*
  65.                             'defun-q '*error* '(msg / )
  66.                             (cadr *error*)
  67.                             (    (lambda (values / )
  68.                                     (if (assoc n (eval (cadr values)))
  69.                                         values
  70.                                         (list
  71.                                             (car values)
  72.                                             (list 'quote (cons (cons n store) (eval (cadr values))))
  73.                                         )
  74.                                     )
  75.                                 )
  76.                                 (caddr *error*)
  77.                             )
  78.                             (cddr *error*)
  79.                         )
  80.                     )
  81.                     (S n v)
  82.                 )
  83.                 setvar
  84.                 (list getvar 'n)
  85.             )
  86.         )
  87.     )
  88.    
  89.     (pragma '((protect-assign setvar)))
  90. )
  91.  

USAGE:
Code: [Select]
(INIerror "This is a function ...")

(setvar "osmode" 0)
(setvar "cmdecho 0)

.
.
.
.

(*error* nil)

reltro

liuhaixin88

  • Guest
Re: when error, I want Restore the "textstyle"
« Reply #19 on: June 16, 2014, 09:49:15 PM »
I'm not sure if this is good stuff or not...
Im overwriting setvar to catch all changes and restore changed sysvars if an error occurs or the errorHandler is called with nil...

Code - Auto/Visual Lisp: [Select]
  1. (defun INIerror (name /  *doc*)
  2.  
  3.     (eval
  4.         (list 'defun-q '*error* '(msg / )
  5.             (list
  6.                 (lambda (s / )
  7.                     (pragma '((unprotect-assign setvar)))
  8.                     (setq setvar s)
  9.                     (pragma '((protect-assign setvar)))
  10.                 )
  11.                 setvar
  12.             )
  13.             (list
  14.                 (lambda (store / )
  15.                     (mapcar
  16.                         '(lambda (a / )
  17.                             (setvar (car a) (cdr a))
  18.                         )
  19.                         store
  20.                     )
  21.                 )
  22.                 nil
  23.             )
  24.             (list
  25.                 (lambda (name *doc* prevErr  / currentcmdecho)
  26.                     (vla-endundomark *doc*)
  27.                     (setq *error* prevErr)
  28.  
  29.                     (if msg
  30.                         (progn
  31.                             (setq currentcmdecho (getvar 'cmdecho))
  32.                             (setvar 'cmdecho 0)
  33.                             (command-s "_.undo" 1 "")
  34.                             (setvar 'cmdecho currentcmdecho)
  35.  
  36.                             (if (not (wcmatch (strcase msg) "*EXIT*,*BREAK*,*CANCEL*"))
  37.                                 (progn
  38.                                     (princ "\n*** error ***\n")
  39.                                     (princ (strcat "\t" name ": " msg "\n"))
  40.  
  41.                                     (princ)
  42.                                 )
  43.                             )
  44.  
  45.                             (*error* msg)
  46.                         )
  47.                     )
  48.                 )
  49.                 (vl-princ-to-string name)
  50.                 *doc*
  51.                 (list 'quote *error*)
  52.             )
  53.         )
  54.     )
  55.  
  56.     (pragma '((unprotect-assign setvar)))
  57.    
  58.     (eval
  59.         (list 'defun 'setvar '(n v / )
  60.             (list
  61.                 (lambda (S store / )
  62.                     (setq n (strcase (vl-princ-to-string n)))
  63.                     (eval
  64.                         (vl-list*
  65.                             'defun-q '*error* '(msg / )
  66.                             (cadr *error*)
  67.                             (    (lambda (values / )
  68.                                     (if (assoc n (eval (cadr values)))
  69.                                         values
  70.                                         (list
  71.                                             (car values)
  72.                                             (list 'quote (cons (cons n store) (eval (cadr values))))
  73.                                         )
  74.                                     )
  75.                                 )
  76.                                 (caddr *error*)
  77.                             )
  78.                             (cddr *error*)
  79.                         )
  80.                     )
  81.                     (S n v)
  82.                 )
  83.                 setvar
  84.                 (list getvar 'n)
  85.             )
  86.         )
  87.     )
  88.    
  89.     (pragma '((protect-assign setvar)))
  90. )
  91.  

USAGE:
Code: [Select]
(INIerror "This is a function ...")

(setvar "osmode" 0)
(setvar "cmdecho 0)

.
.
.
.

(*error* nil)

reltro

Thanks  reltro, you are great !
your routine  is complex. I can't understand completely.

When errors happen, this routine can do what ?


« Last Edit: June 16, 2014, 09:52:32 PM by liuhaixin88 »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: when error, I want Restore the "textstyle"
« Reply #20 on: June 16, 2014, 11:24:21 PM »
My experience has shown that the *error* function is best defined as local to the main application function.

eg
Quote
(defun c:test ( / *error* ... )
 
    (defun *error* ( msg )
        ;; stuff here
    )
    ;; stuff here

    ; force a clean-up
    (*error* nil )
    (princ)
)

If you define *error as global it will stay in memory and be re-used for any following errors. ... something that may not be required if you are making specific restorations or changes to the environment.

A general guideline is "Leave the environment as you found it". I've seen cases where a user defined global *error* routine has caused real debugging problems in other routines.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.