Please do not take offence to this < ... >. But in an effort to help teach, here is my first pass at the function.
Again, please, no offence. Just trying to help give direction.
No offense taken Se7en, I really appreciate the constructive feedback. This is where I can really learn, I thank you. If you don't mind, I'll try to explain my actions & thoughts here as well. Please correct me where I may be incorrect as I would really like to get some good programming practices in my work instead of the bad habits we can easily inherit.
(defun C:ReplaceText (/ *error* NewTLen ; Local
OldText NewText OldTLen SearchStr NewTLen AllText) ; Global
[color=red] ;; 7: there isnt anything GLOBAL about the localization "( / <blah> )" process so remove that comment.
[/color]
Perhaps I am not yet understanding the difference between the Local and Global variables yet. These variables, although they are used by this main function, are also used in the other functions. As this is where the program begins, I figured this is where it would be best to declare the variables. I know they can't be declared in the other functions as they will be rendered useless in this function. That is what I was understanding Global to be. I know declaring them here makes them Local, but they are (to my limited understanding) Global in that they are accessed by the other functions in this routine.
(defun *error* (msg /)
(on-error msg)
(princ)
) ; _EoF
[color=red] ;; 7: what is "on-error"? ...either use "*error*" or "on-error"
;; 7: this is not the END OF FILE (that tag is typical used to mean END OF FILE not END OF FUNCTION)
;; use the typical syntax instead. Like: "...);_ end defun"[/color]
Oops. I apologize, I forgot I needed the 'on-error' to fully understand it all. Here it is.
(defun on-error (msg / ) ; borrowed from Kerry - theSwamp
(while
(> (getvar "cmdactive") 0)
(command)
)
(setvar "cmdecho" 1)
(cond ( (not msg)) ; no error, do nothing
( (member
(strcase msg t) ; invoke a cancel
'("console break" "function cancelled" "quit / exit abort")
)
(prompt "\nFunction Cancelled\nFunction Cancelled")
)
( (princ (strcat "\nApplication Error: " ; else, an error message
(itoa (getvar "errno"))
" :- "
msg "\n"
)
)
)
)
(setq *error* nil)
)
... ; EoF = End of File ... Got it, thanks.
(setvar "cmdecho" 0)
[color=red] ;; 7: you're not even going to save the users current settings?
[/color]
Well umm, ... no I wasn't. Most of the users don't even read their command line yet, I'm hoping to get them trained to look at it. I want to force the echo on at the end but I don't see a reason for the echo to be on during the function, so I'm hard coding it in. I did forget about my little (quit) if there's no text though.
I need to fix that.
(prompt "\nUse quotes for multiple words.")
(setq OldText (strcase (getstring "\nEnter text being replaced (Press 'Enter' for entire string): "))
NewText (strcase (getstring "\nEnter new text to replace existing: ")))
(if (or (= OldText nil) (= OldText "")) ; if OldText is empty
[color=red] ;; 7: (or (null Oldtext) ...?
[/color] (setq OldText "String") ; Then replace entire string
(setq OldTLen (strlen OldText) ; Else get Length of OldText
SearchStr (strcat "*" OldText "*")) ; create search string
) ; _if
(if (or (= NewText nil) (= NewText "")) ; if NewText IS empty
[color=red] ;; 7: (or (null NewText) ...?
[/color] (quit)
[color=red] ;; 7: You have already altered the users CMDECHO var, are you setting it back?
;; try and think of a way to NOT use this method.[/color]
Null ... I forgot about that. Null does help, I don't like the way I currently have it.
You are correct, (quit) does not work like this. Instead of using (quit), I can simply send it to the 'on-error' to clean everything up, yes?.
Thank you again Se7en for your eye's & constructive feedback.
Hangman,
FYI, First thing you do when debugging is to disable the local error handler.
I ran the routine with out error but it did not replace the text.
Correct CAB, that's why I came here. I had it disabled early on. I also had all the declared variables commented out so I could go through them and see if any one was not being used as they should. As it does run without the error handler enabled, I re-enabled it and put a bunch of prompts in there to see how far it was going before erroring as well. Like I mentioned earlier, I was using VLIDE too, but I don't think I was using it correctly. I need to go re-read the help files on it.
Looking at your code I did not see a clear picture of what you are trying to do with (if (= OldText "")
What does your pseudo code look like?
Well, ... I don't actually have any pseudo code for that part. It was a random thought as I was putting this together and I just ... threw it in there. Basically all it does is; If there is no specific text to be replaced, it replaces every piece of text string selected. So the function forces a specific selection rather than the global (ssget "_X" ... ) selection.
I thank you guys for your help and your instruction. I love coming to the Swamp just because of people like you guys here. Thank you.