Author Topic: Alternate for getkword  (Read 7827 times)

0 Members and 1 Guest are viewing this topic.

Rabbit

  • Guest
Re: Alternate for getkword
« Reply #15 on: January 09, 2013, 04:50:18 PM »
Since this thread was originally about KWORDs, I got to thinking, there's got to be a better way.

A long time ago I got a hold of this little routine:
(Works best if Dynamic Input is turned on, then it's just a click and go thing)

Code - Auto/Visual Lisp: [Select]
  1. ;;;------------------------------------------------------------------------------------------------
  2. ;;;Tony Tanzillo - Gets the default return string for user input
  3. (defun xgetstring (msg StringInput / s)
  4.    (setq s
  5.       (getstring
  6.          (strcat "\n" msg
  7.             (cond (StringInput (strcat " <" StringInput ">: "))
  8.                   (t ": ")))))
  9.    (cond ((eq s "") StringInput) (t s))
  10. )
  11.  

And I made some modifications and came up with this:

Code - Auto/Visual Lisp: [Select]
  1. ;;;------------------------------------------------------------------------------------------------
  2. ;;;Written by Jamie Myers
  3. ;;;Loose extrapilation from Tony Tanzillo's XGETSTRING sub-routine
  4. (defun xgetkword (msg StringInput / s)
  5.    (setq s
  6.           (getkword
  7.             (strcat "\n" msg
  8.                     (cond (StringInput (strcat " <" StringInput ">: "))
  9.                           (t ": ")))))
  10.    (cond ((eq s nil) StringInput) (t s))
  11. )
  12.  

It requires a default variable bet set and an initget call before the routine is run.
Code - Auto/Visual Lisp: [Select]
  1. (setq rpt "Yes")
  2. (initget "Yes No")
  3. (setq rpt (xgetkword "\nContinue? [Yes/No]" rpt))
  4.  

And that has worked well for me for a long time.  But, then again, I'm somewhat lazy.  I wanted to create a better getkword function.  So I came up with this:

Code - Auto/Visual Lisp: [Select]
  1. ;;;------------------------------------------------------------------------------------------------
  2. ;;;Written by Jamie Myers 01-09-2013
  3. ;;;Inspiration fromTony Tanzillo's XGETSTRING sub-routine,
  4. ;;;and help from irneb, CAB and Lee Mac
  5. ;;;Use:      (setq <Variable> (GetKwordSub <message> <list of strings> <Variable>))
  6. ;;;Example:  (setq ReturnString (GetKwordSub "Selection" (list "Yes" "No" "Maybe" "A bc" "A-bc" "X_Y-z") ReturnString))
  7.  
  8. (defun GetKwordSub (Msg StringList Default / Msg Default Answer StringList ModList Cnt Setinitget Setmessage)
  9.   (if (not Default) (setq Answer (nth 0 StringList)) (setq Answer Default));set default if there is none
  10.   (setq ModList (mapcar '(lambda ( x ) (vl-string-translate " " "-" (vl-string-translate "_" "-" x))) StringList));Use hyphens instead of underscores and spaces
  11.   (setq Cnt (1- (length ModList)))
  12.   (while (> Cnt -1);iterate through the list to create 2 strings, one for the initget call and one for the message
  13.     (setq Temp (nth Cnt ModList))
  14.     (if (not Setinitget)
  15.       (setq Setinitget Temp)
  16.       (setq Setinitget (strcat Temp " " Setinitget)))
  17.     (if (not Setmessage)
  18.       (setq Setmessage Temp)
  19.       (setq Setmessage (strcat Temp "/" Setmessage)))
  20.     (setq Cnt (1- Cnt))
  21.   );while
  22.   (initget Setinitget)
  23.   (setq Answer
  24.          (getkword
  25.             (strcat "\n" Msg "[" Setmessage "]"
  26.                     (cond (Answer (strcat " <" Answer ">: "))
  27.                           (t ": ")))))
  28.   (cond ((eq Answer nil) Answer) (t Answer))
  29.   (nth (vl-position Answer ModList) StringList)
  30. );defun
  31.  
  32.  

It has issues.  Using the example call, it gets confused between "A bc" and "A-bc".  My thinking is that there more than likely will never be an issue.
I've added a few comments to get and idea of what's going on.
Anyways, what do the experts say.  And of course, I want to see if it will even work for someone else.

Rabbit

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Alternate for getkword
« Reply #16 on: January 09, 2013, 11:51:13 PM »
Try this one:
Code - Auto/Visual Lisp: [Select]
  1. (defun getkword+  (msg options default / Lst->Opt values)
  2.   (defun Lst->Opt  (lst sep old new /)
  3.     (substr
  4.       (apply 'strcat
  5.              (mapcar
  6.                '(lambda (s) (strcat sep s))
  7.                (setq values (mapcar '(lambda (s) (strcase (vl-string-translate old new s))) lst))))
  8.       (1+ (strlen sep))))
  9.   (initget (Lst->Opt options " " " _" "--"))
  10.   (if (setq msg (vl-position
  11.                   (cond (default
  12.                          (cond ((getkword (strcat msg
  13.                                                   " ["
  14.                                                   (Lst->Opt options "/" " _" "--")
  15.                                                   "] <"
  16.                                                   (setq default (strcase default))
  17.                                                   ">: ")))
  18.                                (default)))
  19.                         (t (getkword (strcat msg " [" (Lst->Opt options "/" " _" "--") "]: "))))
  20.                   values))
  21.     (nth msg options)))

You use it thus:
Code: [Select]
Command: (getkword+ "Testing" '("Yes" "No") nil)
Testing [YES/NO]:
nil
Command: (getkword+ "Testing" '("Yes" "No") nil)
Testing [YES/NO]: y
"Yes"
Command: (getkword+ "Testing" '("Yes" "No") nil)
Testing [YES/NO]: n
"No"
Command: (getkword+ "Testing" '("Yes" "No") "Yes")
Testing [YES/NO] <YES>:
"Yes"
Command: (getkword+ "Testing" '("Yes" "No") "Yes")
Testing [YES/NO] <YES>:
"Yes"
Command: (getkword+ "Testing" '("Yes" "No") "Yes")
Testing [YES/NO] <YES>: n
"No"
It works with the pick a keyword also. It would allow for spaces in the keywords as well.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Rabbit

  • Guest
Re: Alternate for getkword
« Reply #17 on: January 10, 2013, 11:55:02 AM »
Wow.  I mean WOW!  I expected suggestions like "Change this part to this", or, "Change this other stuff to that", but what you came up with is a totally new thing.  Very impressive.  My eyes have been opened to a whole new way of looking at some situations.

Thank you for sharing your knowledge.

Rabbit

CAB

  • Global Moderator
  • Seagull
  • Posts: 10401
Re: Alternate for getkword
« Reply #18 on: January 10, 2013, 12:43:12 PM »
This is an old post but may give some ideas for keyword routines.
http://www.theswamp.org/index.php?topic=6992.msg93574#msg93574
I've reached the age where the happy hour is a nap. (°¿°)
Windows 10 core i7 4790k 4Ghz 32GB GTX 970
Please support this web site.

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Alternate for getkword
« Reply #19 on: January 10, 2013, 11:51:36 PM »
Thank you for sharing your knowledge.
Pleasure! Glad I could help someone. Though my code is not much more than an amalgamation of all the ideas in this thread.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.