Explicit Variables again

Peter M. Brigham pmbrig at gmail.com
Fri Oct 19 17:41:57 CDT 2012


On Oct 19, 2012, at 2:00 PM, J. Landman Gay wrote:

> Typos are almost never a problem for me. I only type variable names once when I first write them. I have a custom frontscript that includes a whole lot of handy things, one of which is the ability to insert or replace the selection with whatever word I point at. When I need to use an existing variable, I just point at the original and hit a keyboard shortcut and the variable name goes into the script with no typing. If the handler is longer than the script window (very rare for me) then I use copy/paste or the replace dialog.


I've been using your frontscript handler for several years now and I love it -- it saves me so much time and prevents typos. For those who are interested here is my version (slightly altered from yours, Jacque). They are all control-shift keypresses that work only in the IDE script editor. The one Jacque referred to is the "scriptPaint" handler: control-shift-space inserts the mousetext into the selection.

Put this into a frontScript:

on controlkeydown whichKey
   put the long name of the target into tarName
   put the shiftkey is down into shK
   switch whichKey
      case "'" -- ctrl-sh-quote puts quotes around the selection
         if not shK then pass controlkeydown
         if "field" is not in tarName then pass controlkeydown
         if "revNewScriptEditor" is not in tarName then pass controlkeydown
         get the selection
         put q(it) into the selection
         if it = "" then
            put the selectedChunk into tSel
            put word 4 of tSel into word 2 of tSel
            put (word 4 of tSel) - 1 into word 4 of tSel
            select tSel
         end if
         doUndoSpace
         break
      case "9"
      case "0" -- ctrl(-sh)-paren puts parens around the selection
         if "field" is not in tarName then pass controlkeydown
         if not shK then pass controlkeydown
         if "revNewScriptEditor" is not in tarName then pass controlkeydown
         get the selection
         put "(" & it & ")" into the selection
         if it = "" then
            put the selectedChunk into tSel
            put word 4 of tSel into word 2 of tSel
            put (word 4 of tSel) - 1 into word 4 of tSel
            select tSel
         end if
         doUndoSpace
         break
      case "["
      case "]" -- ctrl-bracket puts brackets around the selection
         if not shK then pass controlkeydown
         if "field" is not in tarName then pass controlkeydown
         if "revNewScriptEditor" is not in tarName then pass controlkeydown
         get the selection
         put "[" & it & "]" into the selection
         if it = "" then
            put the selectedChunk into tSel
            put word 4 of tSel into word 2 of tSel
            put (word 4 of tSel) - 1 into word 4 of tSel
            select tSel
         end if
         doUndoSpace
         break
      case "-"
      case "_" -- ctrl-sh-dash comments out the line(s)
         if not shK then pass controlkeydown
         if "field" is not in tarName then pass controlkeydown
         if "revNewScriptEditor" is not in tarName then pass controlkeydown
         put the selectedLine into tLineCh
         put "-" & "-" into cmntChars
         if " to " is in tLineCh then
            put word 2 of tLineCh into stLineNbr
            put word 4 of tLineCh into endLineNbr
            repeat with n = stLineNbr to endLineNbr
               put "put cmntChars & space before word 1 of line" && n && "of" && tarName \
                      into theDo
               do theDo
            end repeat
            -- put scriptLinesText into line stLineNbr to endLineNbr of the target
            put endLineNbr into selectHere
         else
            put word 2 of tLineCh into lineNbr
            put the long name of the target into tLongName
            put sr(line lineNbr of the target) into tLineText
            do "put cmntChars & space before word 1 of" && tLineCh
            put lineNbr into selectHere
         end if
         doUndoSpace
         send "tabkey" to tarname -- to fix indenting
         select after line selectHere of the target
         break
      case "="
      case "+" -- ctr(-sh)-plus uncomments the line -- until Rev fixes the commandkey shortcut
         if not shK then pass controlkeydown
         if "field" is not in tarName then pass controlkeydown
         if "revNewScriptEditor" is not in tarName then pass controlkeydown
         put the selectedLine into tLineCh
         if " to " is in tLineCh then
            put word 2 of tLineCh into stLineNbr
            put word 4 of tLineCh into endLineNbr
            put line stLineNbr to endLineNbr of target into scriptLinesText
            repeat with n = 1 to the number of lines of scriptLinesText
               put sr(line n of scriptLinesText) into tLineText
               if char 1 to 2 of tLineText = "-" & "-"
               then delete char 1 to 2 of tLineText
               put tLineText into line n of scriptLinesText
            end repeat
            put scriptLinesText into line stLineNbr to endLineNbr of the target
         else
            put sr(value(tLineCh)) into tLineText
            if char 1 to 2 of tLineText = "-"&"-" then
               delete char 1 to 2 of tLineText
               do "put tLineText into" && tLineCh
            end if
         end if
         doUndoSpace
         send "tabkey" to tarname
         break
      case " " -- scriptPaint: crl-sh-space inserts the mousetext into the selection
         if not shK then pass controlkeydown
         if "field" is not in tarName then pass controlkeydown
         if "revNewScriptEditor" is not in tarName then pass controlkeydown
         put the mouseText into the selection
         doUndoSpace
         break
     default
         pass controlkeydown
   end switch
end controlkeydown

private command doUndoSpace
   -- this solves the problem that after the above scripted operations
   -- the script editor remains unaware that the script has changed,
   -- so an <enterkey> will close but not save the changed script
   -- thus we have to *type* at least one character
   lock screen
   put the selectedText into selTxt
   type numToChar(32) -- the important step
   put word 4 of the selectedChunk into charNbr
   put selTxt into char charNbr of the target
   unlock screen
end doUndoSpace

function q str
   return quote & str & quote
end q




More information about the use-livecode mailing list