|
| 1 | +script "revSEFindMainCardBehavior" |
| 2 | +local sCancelled = "false" |
| 3 | + |
| 4 | +# Returns |
| 5 | +# The long id of the stack to use as a regular expression builder |
| 6 | +function regularExpressionBuilder |
| 7 | + local tPath |
| 8 | + put revEnvironmentPluginsPath() & slash & "RegExBuilder 1.6.rev" into tPath |
| 9 | + |
| 10 | + if there is no file tPath then |
| 11 | + return empty |
| 12 | + else |
| 13 | + return the long id of stack tPath |
| 14 | + end if |
| 15 | +end regularExpressionBuilder |
| 16 | + |
| 17 | +on preOpenCard |
| 18 | + send "revSEFindInitialize" to (getCaller()) |
| 19 | + --historyLoad |
| 20 | + optionsHide |
| 21 | + |
| 22 | + --set the label of button "Toggle Options" of me to "+" |
| 23 | + send "resetIcon" to button "Toggle Options" of me |
| 24 | + statusSet empty |
| 25 | + update |
| 26 | + updateButtonStates |
| 27 | +end preOpenCard |
| 28 | + |
| 29 | +on menuPick |
| 30 | + updateButtonStates |
| 31 | + pass menuPick |
| 32 | +end menuPick |
| 33 | + |
| 34 | +on closeCard |
| 35 | + historySave |
| 36 | + send "revSEFindFinalize" to (getCaller()) |
| 37 | + pass closeCard |
| 38 | +end closeCard |
| 39 | + |
| 40 | +on escapeKey |
| 41 | + close this stack |
| 42 | +end escapeKey |
| 43 | + |
| 44 | +on commandKeyDown pKey |
| 45 | + if pKey is not "v" then |
| 46 | + pass commandKeyDown |
| 47 | + end if |
| 48 | + |
| 49 | + # There are two possible pasting targets here, the find and replace combo boxes. In both these cases we have |
| 50 | + # to ensure that multiple lines and formatted text cannot be pasted in. |
| 51 | + local tTextToPaste |
| 52 | + put line 1 of the clipboardData["text"] into tTextToPaste |
| 53 | + |
| 54 | + set the clipboardData["text"] to empty |
| 55 | + set the clipboardData["html"] to empty |
| 56 | + set the clipboardData["text"] to tTextToPaste |
| 57 | + pass commandKeyDown |
| 58 | +end commandKeyDown |
| 59 | + |
| 60 | +# Description |
| 61 | +# Sets the properties of all controls on the card to their correct values according to the current context |
| 62 | +command update |
| 63 | + local tFindLabel |
| 64 | + put getFindLabel() into tFindLabel |
| 65 | + |
| 66 | + local tFindText |
| 67 | + put historyGet("find") into tFindText |
| 68 | + if tFindLabel is not empty and tFindLabel is not among the lines of tFindText then |
| 69 | + put tFindLabel & return & tFindText into tFindText |
| 70 | + end if |
| 71 | + |
| 72 | + set the text of button "Find" of me to tFindText |
| 73 | + set the label of button "Find" of me to "templabel" |
| 74 | + set the label of button "Find" of me to tFindLabel |
| 75 | + |
| 76 | + local tReplaceLabel |
| 77 | + put getReplaceLabel() into tReplaceLabel |
| 78 | + |
| 79 | + local tReplaceText |
| 80 | + put getReplaceLabel() into tReplaceLabel |
| 81 | + if tReplaceLabel is not empty and tReplaceLabel is not among the lines of tReplaceText then |
| 82 | + put tReplaceLabel & return & tReplaceText into tReplaceText |
| 83 | + end if |
| 84 | + |
| 85 | + set the text of button "Replace" of me to tReplaceText |
| 86 | + set the label of button "Replace" of me to "templabel" |
| 87 | + set the label of button "Replace" of me to tReplaceLabel |
| 88 | + |
| 89 | + set the hilite of button "Case Sensitive Search" of me to sePrefGet("find,casesensitive") |
| 90 | + set the hilite of button "Match Whole Words" of me to sePrefGet("find,wholematches") |
| 91 | + set the hilite of button "regEx" of me to sePrefGet("find,regularexpressions") |
| 92 | + if sePrefGet("find,wildcards") is true then |
| 93 | + set the hilite of button "wildcards" of me to true |
| 94 | + else |
| 95 | + set the hilite of button "wildcards" of me to false |
| 96 | + end if |
| 97 | + |
| 98 | + if sePrefGet("find,plaintext") is true then |
| 99 | + set the hilite of button "plaintext" of me to true |
| 100 | + else |
| 101 | + set the hilite of button "plaintext" of me to false |
| 102 | + end if |
| 103 | + |
| 104 | + ## Default |
| 105 | + if the hilite of button "regEx" of me is false and the hilite of button "wildcards" of me is false and the hilite of button "plaintext" of me is false then |
| 106 | + set the hilite of button "plaintext" of me to true |
| 107 | + sePrefSet "find,plaintext", true |
| 108 | + end if |
| 109 | +end update |
| 110 | + |
| 111 | +# Updates the disabled state of the search action buttons according to the label of the target menu. |
| 112 | +# This is needed because if searching all stacks, only replace all and find all are allowed. |
| 113 | +command updateButtonStates |
| 114 | + # If we are searching within objects in the script editor, we can do everything, otherwise, only |
| 115 | + # find all and replace all are allowed. |
| 116 | + if the label of button "Location" of me is "Current Tab" or the label of button "Location" of me is "All tabs" then |
| 117 | + enable button "Find Next" of me |
| 118 | + enable button "Replace one" of me |
| 119 | + enable button "Find all" of me |
| 120 | + enable button "Replace all" of me |
| 121 | + else |
| 122 | + enable button "Find all" of me |
| 123 | + |
| 124 | + # Disable this for now as this feature could cause data-loss if not used properly |
| 125 | + -- enable button "Replace all" of me |
| 126 | + disable button "Replace all" of me |
| 127 | + |
| 128 | + disable button "Find Next" of me |
| 129 | + disable button "Replace one" of me |
| 130 | + end if |
| 131 | +end updateButtonStates |
| 132 | + |
| 133 | +private function getCaller |
| 134 | + return the long id of the cCaller of the owner of me |
| 135 | +end getCaller |
| 136 | + |
| 137 | +# Returns |
| 138 | +# The label for the "Find" button. This is one of the following values if they exist, in order of preference: |
| 139 | +# 1. The last selected text of the editor field in the script editor that called this stack |
| 140 | +# 2. The last searched for term |
| 141 | +# 3. empty |
| 142 | +private function getFindLabel |
| 143 | + send "revSEGetLastSelectedWord" to (getCaller()) |
| 144 | + if the result is not empty then |
| 145 | + return line 1 of the result |
| 146 | + end if |
| 147 | + |
| 148 | + local tLastSearch |
| 149 | + put line 1 of historyGet("find") into tLastSearch |
| 150 | + if tLastSearch is not empty then |
| 151 | + return tLastSearch |
| 152 | + end if |
| 153 | + |
| 154 | + return empty |
| 155 | +end getFindLabel |
| 156 | + |
| 157 | +# Returns |
| 158 | +# The label for the "Replace" button. This is one of the following values if they exist, in order of preference: |
| 159 | +# 1. The last term used to replace something |
| 160 | +# 2. empty |
| 161 | +private function getReplaceLabel |
| 162 | + local tLastReplace |
| 163 | + put line 1 of historyGet("replace") into tLastReplace |
| 164 | + if tLastReplace is not empty then |
| 165 | + return tLastReplace |
| 166 | + end if |
| 167 | + |
| 168 | + return empty |
| 169 | +end getReplaceLabel |
| 170 | + |
| 171 | +local sFindHistory |
| 172 | +local sReplaceHistory |
| 173 | +constant kHistorySize = 20 |
| 174 | + |
| 175 | +# Parameters |
| 176 | +# pType : either "find" or "replace" |
| 177 | +# Returns |
| 178 | +# The history of the specified action type, one per line with most recent coming first. |
| 179 | +private function historyGet pType |
| 180 | + return seFindHistoryGet(pType) |
| 181 | +end historyGet |
| 182 | + |
| 183 | +# Parameters |
| 184 | +# pType : either "find" or "replace" |
| 185 | +# pTerm: the find / replace term to add |
| 186 | +# Description |
| 187 | +# Adds the specified term to the history. |
| 188 | +private command historyAdd pType, pTerm |
| 189 | + seFindHistoryAdd pType, pTerm |
| 190 | +end historyAdd |
| 191 | + |
| 192 | + |
| 193 | +# Description |
| 194 | +# Loads the history from the preferences |
| 195 | +private command historyLoad |
| 196 | + seFindHistoryLoad |
| 197 | +end historyLoad |
| 198 | + |
| 199 | +# Description |
| 200 | +# Saves the history to the preferences |
| 201 | +private command historySave |
| 202 | + seFindHistorySave |
| 203 | +end historySave |
| 204 | + |
| 205 | +# Description |
| 206 | +# Shows the find options |
| 207 | +command optionsShow |
| 208 | + lock screen |
| 209 | + local tTop |
| 210 | + put the top of the owner of me into tTop |
| 211 | + set the height of the owner of me to the bottom of group "Options" of me + 5 |
| 212 | + set the top of the owner of me to tTop |
| 213 | + unlock screen |
| 214 | +end optionsShow |
| 215 | + |
| 216 | +# Description |
| 217 | +# Hides the find options |
| 218 | +command optionsHide |
| 219 | + lock screen |
| 220 | + local tTop |
| 221 | + put the top of the owner of me into tTop |
| 222 | + set the height of the owner of me to the bottom of button "Find all" of me + 5 |
| 223 | + set the top of the owner of me to tTop |
| 224 | + unlock screen |
| 225 | +end optionsHide |
| 226 | + |
| 227 | +command statusSet pStatus |
| 228 | + set the text of field "Status" of me to pStatus |
| 229 | +end statusSet |
| 230 | + |
| 231 | +private command getSearchContext @rQuery, @rReplace, @rRegularExpression, @rWildcards, @rWholeMatches, @rIgnoreCase, @rTarget |
| 232 | + local tFindType |
| 233 | + |
| 234 | + put the label of button "Find" of me into rQuery |
| 235 | + put the label of button "Replace" of me into rReplace |
| 236 | + |
| 237 | + put sePrefGet("find,regularexpressions") into rRegularExpression |
| 238 | + put sePrefGet("find,wildcards") into rWildcards |
| 239 | + |
| 240 | + --put true into rWildcards |
| 241 | + local tWholeMatches |
| 242 | + put sePrefGet("find,wholematches") into rWholeMatches |
| 243 | + put not sePrefGet("find,casesensitive") into rIgnoreCase |
| 244 | + |
| 245 | + switch the label of button "Location" of me |
| 246 | + case "All tabs" |
| 247 | + put "all" into rTarget |
| 248 | + break |
| 249 | + case "Current Tab" |
| 250 | + put "current" into rTarget |
| 251 | + break |
| 252 | + case "Card" |
| 253 | + put "card" into rTarget |
| 254 | + break |
| 255 | + case "Stack" |
| 256 | + put "stack" into rTarget |
| 257 | + break |
| 258 | + case "Stack File" |
| 259 | + put "stack file" into rTarget |
| 260 | + break |
| 261 | + case "All Stack Files" |
| 262 | + put "all stacks" into rTarget |
| 263 | + break |
| 264 | + case "Stack File and its stackFiles" |
| 265 | + put "stack file and stackfiles" into rTarget |
| 266 | + break |
| 267 | + case "All available stacks" |
| 268 | + put "all available stacks" into rTarget |
| 269 | + break |
| 270 | + default |
| 271 | + put "current" into rTarget |
| 272 | + end switch |
| 273 | +end getSearchContext |
| 274 | + |
| 275 | +local sLastQuery |
| 276 | + |
| 277 | +# Parameters |
| 278 | +# pAction : either "findNext", "findAll", "replaceCurrent" or "replaceAll" |
| 279 | +# Description |
| 280 | +# Performs the specified find action, using the current values of the controls |
| 281 | +# on this card to obtain all the required parameters |
| 282 | +private command findAction pAction |
| 283 | + statusSet empty |
| 284 | + put false into sCancelled |
| 285 | + |
| 286 | + local tQuery, tReplace, tRegularExpression, tWildcards, tWholeMatches, tIgnoreCase, tTarget |
| 287 | + getSearchContext tQuery, tReplace, tRegularExpression, tWildcards, tWholeMatches, tIgnoreCase, tTarget |
| 288 | + |
| 289 | + # Don't allow empty search requests to be sent to the script editor because it will |
| 290 | + # assume that the user pressed a keyboard shortcut and will show the interactive find |
| 291 | + # group. |
| 292 | + if tQuery is empty then |
| 293 | + exit findAction |
| 294 | + end if |
| 295 | + |
| 296 | + switch pAction |
| 297 | + case "findNext" |
| 298 | + send "revSEFindNext tQuery, tRegularExpression, tWildcards, tWholeMatches, tIgnoreCase, tTarget" to (getCaller()) |
| 299 | + break |
| 300 | + case "findAll" |
| 301 | + local tCallback |
| 302 | + put "findAllCallback" into tCallback |
| 303 | + |
| 304 | + local tCallbackTarget |
| 305 | + put the long id of me into tCallbackTarget |
| 306 | + |
| 307 | + send "revSEFindAll tQuery, tRegularExpression, tWildcards, tWholeMatches, tIgnoreCase, tTarget, tCallback, tCallbackTarget" to (getCaller()) |
| 308 | + |
| 309 | + statusSet empty |
| 310 | + break |
| 311 | + case "replaceCurrent" |
| 312 | + send "revSEReplaceCurrent tQuery, tReplace, tRegularExpression, tWildcards, tWholeMatches, tIgnoreCase, tTarget" to (getCaller()) |
| 313 | + break |
| 314 | + case "replaceAll" |
| 315 | + put "replaceAllCallback" into tCallback |
| 316 | + put the long id of me into tCallbackTarget |
| 317 | + |
| 318 | + local tReplacementCount |
| 319 | + send "revSEReplaceAll tQuery, tReplace, tRegularExpression, tWildcards, tWholeMatches, tIgnoreCase, tTarget, tCallback, tCallbackTarget" to (getCaller()) |
| 320 | + put the result into tReplacementCount |
| 321 | + statusSet (tReplacementCount & " occurrences replaced") |
| 322 | + break |
| 323 | + end switch |
| 324 | +end findAction |
| 325 | + |
| 326 | +# Description |
| 327 | +# Finds the next ocurrence of the specified term and highlights it in the target script editor |
| 328 | +command findNext |
| 329 | + findAction "findNext" |
| 330 | +end findNext |
| 331 | + |
| 332 | +# Description |
| 333 | +# Replaces the currently highlighted ocurrence of the specified term with the contents of the replace button |
| 334 | +# in the target script editor. |
| 335 | +command replaceCurrent |
| 336 | + findAction "replaceCurrent" |
| 337 | +end replaceCurrent |
| 338 | + |
| 339 | +# Description |
| 340 | +# Finds all matches for the specified term and displays them in the "Search Results" pane of the target script editor |
| 341 | +command findAll |
| 342 | + findAction "findAll" |
| 343 | +end findAll |
| 344 | + |
| 345 | +# Description |
| 346 | +# Replaces all matches for the specified term with the contents of the replace button |
| 347 | +command replaceAll |
| 348 | + findAction "replaceAll" |
| 349 | +end replaceAll |
| 350 | + |
| 351 | +command findAllCallback pMessage |
| 352 | + statusSet pMessage |
| 353 | + |
| 354 | + # This line allows any pending mouseUps on the cancel button to execute, |
| 355 | + # allowing the search to be cancelled. |
| 356 | + wait 1 millisecond with messages |
| 357 | + |
| 358 | + if sCancelled then |
| 359 | + set the label of button "Find all" of me to empty |
| 360 | + unlock cursor |
| 361 | + put false into sCancelled |
| 362 | + return "cancel" |
| 363 | + else |
| 364 | + return empty |
| 365 | + end if |
| 366 | +end findAllCallback |
| 367 | + |
| 368 | +command replaceAllCallback pMessage |
| 369 | + statusSet pMessage |
| 370 | + |
| 371 | + # This line allows any pending mouseUps on the cancel button to execute, |
| 372 | + # allowing the replace to be cancelled. |
| 373 | + wait 1 millisecond with messages |
| 374 | + |
| 375 | + if sCancelled then |
| 376 | + set the label of button "Replace all" of me to empty |
| 377 | + unlock cursor |
| 378 | + put false into sCancelled |
| 379 | + return "cancel" |
| 380 | + else |
| 381 | + return empty |
| 382 | + end if |
| 383 | +end replaceAllCallback |
| 384 | + |
| 385 | +command cancelOperation |
| 386 | + put true into sCancelled |
| 387 | +end cancelOperation |
0 commit comments