Author Topic: GetKeysFromDCLstring  (Read 274 times)

0 Members and 1 Guest are viewing this topic.

Grrr1337

  • Bull Frog
  • Posts: 265
GetKeysFromDCLstring
« on: November 27, 2016, 06:30:04 am »
Hi guys,
I just wanted to share with you, this subfunction I wrote yesterday:

Code: [Select]
(defun GetKeysFromDCLstring ( DCLstring / ShiftListRightRepeat DCLstring Lst Key LstKeys flg)

(defun ShiftListRightRepeat ( n Lst / nLst )
(if (and (= 'INT (type n)) (listp Lst))
(progn
(setq nLst Lst)
(repeat n (setq nLst (append (cdr nLst) (list (car nLst)))))
)
)
nLst
); defun ShiftListRightRepeat
(if
(or
(eq 'STR (type DCLstring)) ; is a string
(and
(vl-consp DCLstring) ; is a list
(vl-every (function (lambda (x) (eq 'STR (type x)))) DCLstring) ; where each item is a string
(setq DCLstring (apply 'strcat DCLstring)) ; make it a string
); and
); or
(progn
(setq Lst (mapcar 'chr (vl-string->list DCLstring))) ; break down the string into a list of letters
(mapcar
(function
(lambda ( a b c d e f g )
(if (= "key = \"" (apply 'strcat (list a b c d e f g))) ; find the unique combination that can create "key = \"" string
(setq flg 0)
); if
(if (= flg 1)
(if (/= g "\""); start collecting the letters between \" and \" while (lambda) is iterating
(setq Key (cons g Key)) ; collect the letters into a list
(progn
(setq LstKeys (cons (apply 'strcat (reverse Key)) LstKeys)) ; collect the key into the list
(setq flg nil)
(setq Key nil)
); progn
)
); if flg
(and flg (not Key) (setq flg (1+ flg)))
); lambda
); function
Lst ; a 1
(ShiftListRightRepeat 1 Lst) ; (cdr Lst) ; b 2
(ShiftListRightRepeat 2 Lst) ; (cddr Lst) ; c 3
(ShiftListRightRepeat 3 Lst) ; (cdddr Lst) ; d 4
(ShiftListRightRepeat 4 Lst) ; (cddddr Lst) ; e 5
(ShiftListRightRepeat 5 Lst) ; (cdddddr Lst) ; f 6 ???
(ShiftListRightRepeat 6 Lst) ; (cddddddr Lst) ; g 7 ???
); mapcar
); progn
); if
(reverse LstKeys)
); defun GetKeysFromDCLstring
Example:
Code: [Select]
_$ (setq LstDCL ; keys = "case" "SelButton" "SelPrompt" "CtC" "KO" "Text"
(list
" TextTest : dialog // The folowing dialog utilises 1 toggle, 1 image_button, 2 radio_button(s), 1 edit_box"
" {"
" label = \"Edit Text\";"
" spacer;"
" : row"
" {"
" : toggle { label = \"UPPERCASE\"; key = \"case\"; alignment = left; value = 1; }"
" : image_button { key = \"SelButton\"; width = 5; color = 1; } // this color must turn to green when selection is made"
" : text { label = \"< Select object(s)\"; key = \"SelPrompt\"; fixed_width = true; }"
" }"
" : boxed_radio_row"
" {"
" label = \"Layer options\";"
" : radio_button { label = \"Change to Current\"; key = \"CtC\"; value = 1; }"
" : radio_button { label = \"Keep Original\"; key = \"KO\"; }"
" }"
" : edit_box"
" { label = \"Text\"; key = \"Text\"; value =  \"Sample Text\"; width = 12; }"
" spacer;"
" ok_cancel;"
" }"
); list
); setq LstDCL
("\tTextTest : dialog // The folowing dialog utilises 1 toggle, 1 image_button, 2 radio_button(s), 1 edit_box" "\t{" "\t\tlabel = \"Edit Text\";" "\t\tspacer;" "\t\t: row" "\t\t{" "\t\t\t: toggle { label = \"UPPERCASE\"; key = \"case\"; alignment = left; value = 1; }" "\t\t\t: image_button { key = \"SelButton\"; width = 5; color = 1; } // this color must turn to green when selection is made" "\t\t\t: text { label = \"< Select object(s)\"; key = \"SelPrompt\"; fixed_width = true; }" "\t\t}" "\t\t: boxed_radio_row" "\t\t{" "\t\t\tlabel = \"Layer options\";" "\t\t\t: radio_button { label = \"Change to Current\"; key = \"CtC\"; value = 1; }" "\t\t\t: radio_button { label = \"Keep Original\"; key = \"KO\"; }" "\t\t}" "\t\t: edit_box" "\t\t{ label = \"Text\"; key = \"Text\"; value =  \"Sample Text\"; width = 12; }" "\t\tspacer;" "\t\tok_cancel;" "\t\t}")
_$ (GetKeysFromDCLstring LstDCL)
("case" "SelButton" "SelPrompt" "CtC" "KO" "Text")
It expects string, or list of strings that contains the DCL code, and returns all the keys from it.
Hopefully it helps at your daily work. :)
« Last Edit: November 27, 2016, 06:33:10 am by Grrr1337 »