| 1 | /* ScriptEditor.cpp |
| 2 | * |
| 3 | * Copyright (C) 1997-2005,2007-2018,2020-2026 Paul Boersma |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 | * See the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include "ScriptEditor.h" |
| 20 | #include "../kar/longchar.h" |
| 21 | #include "praatP.h" |
| 22 | #include "EditorM.h" |
| 23 | #include "UiPause.h" |
| 24 | |
| 25 | Thing_implement (ScriptEditor, TextEditor, 0); |
| 26 | |
| 27 | CollectionOf <structScriptEditor> theReferencesToAllOpenScriptEditors; |
| 28 | |
| 29 | bool ScriptEditors_dirty () { |
| 30 | for (integer i = 1; i <= theReferencesToAllOpenScriptEditors.size; i ++) { |
| 31 | const ScriptEditor me = theReferencesToAllOpenScriptEditors.at [i]; |
| 32 | if (my dirty) |
| 33 | return true; |
| 34 | } |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | void structScriptEditor :: v9_destroy () noexcept { |
| 39 | our argsDialog. reset(); // don't delay till delete |
| 40 | theReferencesToAllOpenScriptEditors. undangleItem (this); |
| 41 | ScriptEditor_Parent :: v9_destroy (); |
| 42 | } |
| 43 | |
| 44 | void structScriptEditor :: v_nameChanged () { |
| 45 | /* |
| 46 | As TextEditor does, we totally ignore the name that our boss wants to give us. |
| 47 | Instead, we compose the window title from five ingredients, i.e. two more than TextEditor: |
| 48 | |
| 49 | (1) whether we are already associated with a file or not; |
| 50 | (2) if so, the full file path; |
| 51 | (3) whether our text has been modified (i.e. whether we are "dirty"); |
| 52 | (4) whether we have a boss, i.e. whether or not we were created from a data editor; |
| 53 | (5) if so, the name of our boss, i.e. of the data editor that we were created from. |
| 54 | |
| 55 | (last checked 2023-02-25) |
| 56 | */ |
| 57 | const bool dirtinessAlreadyShown = GuiWindow_setDirty (our windowForm, our dirty); // (3) on the Mac (last checked 2023-02-25) |
| 58 | static MelderString buffer; |
| 59 | MelderString_copy (& buffer, MelderFile_isNull (& our file) ? U"untitled script" : U"Script"); // (1) |
| 60 | if (our wasCreatedInAnEditor()) { |
| 61 | if (our optionalReferenceToOwningEditor) |
| 62 | MelderString_append (& buffer, U" [editor “", Thing_getName (our optionalReferenceToOwningEditor), U"”]"); // (4), (5) |
| 63 | else |
| 64 | MelderString_append (& buffer, U" [closed ", our optionalOwningEditorClassName.get(), U"]"); // (4) |
| 65 | } |
| 66 | if (! MelderFile_isNull (& our file)) |
| 67 | MelderString_append (& buffer, U" ", MelderFile_messageName (& our file)); // (2) |
| 68 | if (our dirty && ! dirtinessAlreadyShown) |
| 69 | MelderString_append (& buffer, U" (modified)"); // (3) on Windows and Linux (last checked 2023-02-25) |
| 70 | GuiShell_setTitle (windowForm, buffer.string); |
| 71 | /* |
| 72 | Finally, remember the name of this script. |
| 73 | */ |
| 74 | if (! MelderFile_isNull (& our file)) { |
| 75 | autoScript script = Script_createFromFile (& our file); |
| 76 | Script_rememberDuringThisAppSession_move (script.move()); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | static void common_args_ok (UiForm sendingForm, void *void_me, Editor optionalEditor, const bool fullTrust) { |
| 81 | iam (ScriptEditor); |
| 82 | autostring32 text = GuiText_getString (my textWidget); |
| 83 | if (! MelderFile_isNull (& my file)) |
| 84 | MelderFile_setDefaultDir (& my file); |
| 85 | Melder_includeIncludeFiles (& text); |
| 86 | |
| 87 | Melder_assert (my optionalReferenceToOwningEditor == optionalEditor); // TODO: if never fires, then redundancy |
| 88 | Melder_assert (my interpreterStack); |
| 89 | Melder_assert (my interpreterStack -> interpreters [1]); |
| 90 | //Melder_assert (interpreter); // TODO: figure out why this is usually null |
| 91 | Interpreter_getArgumentsFromDialog (my interpreterStack -> interpreters [1].get(), sendingForm); |
| 92 | |
| 93 | autoPraatBackground background; |
| 94 | if (! MelderFile_isNull (& my file)) |
| 95 | MelderFile_setDefaultDir (& my file); |
| 96 | Interpreter_rememberScript (my interpreterStack -> interpreters [1].get(), & my file, fullTrust); // right before running! (not at interpreter creation, because the name may change) |
| 97 | my interpreterStack -> runDown (autoInterpreter(), text.move(), false); |
| 98 | } |
| 99 | static void args_ok (UiForm sendingForm, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */, |
| 100 | Interpreter /* interpreter */, conststring32 /* invokingButtonTitle */, bool /* modified */, void *void_me, Editor optionalEditor) |
| 101 | { |
| 102 | common_args_ok (sendingForm, void_me, optionalEditor, false); |
| 103 | } |
| 104 | static void args_ok_FULL_TRUST (UiForm sendingForm, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */, |
| 105 | Interpreter /* interpreter */, conststring32 /* invokingButtonTitle */, bool /* modified */, void *void_me, Editor optionalEditor) |
| 106 | { |
| 107 | common_args_ok (sendingForm, void_me, optionalEditor, true); |
| 108 | } |
| 109 | |
| 110 | static void common_args_ok_selectionOnly (UiForm sendingForm, void *void_me, Editor optionalEditor, const bool fullTrust) |
| 111 | { |
| 112 | iam (ScriptEditor); |
| 113 | autostring32 text = GuiText_getSelection (my textWidget); |
| 114 | if (! text) |
| 115 | Melder_throw (U"No text is selected any longer.\nPlease reselect or click Cancel."); |
| 116 | if (! MelderFile_isNull (& my file)) |
| 117 | MelderFile_setDefaultDir (& my file); |
| 118 | Melder_includeIncludeFiles (& text); |
| 119 | |
| 120 | Melder_assert (my optionalReferenceToOwningEditor == optionalEditor); // TODO: if never fires, then redundancy |
| 121 | Melder_assert (my interpreterStack); |
| 122 | Melder_assert (my interpreterStack -> interpreters [1]); |
| 123 | //Melder_assert (interpreter); // TODO: figure out why this is usually null |
| 124 | Interpreter_getArgumentsFromDialog (my interpreterStack -> interpreters [1].get(), sendingForm); |
| 125 | |
| 126 | autoPraatBackground background; |
| 127 | if (! MelderFile_isNull (& my file)) |
| 128 | MelderFile_setDefaultDir (& my file); |
| 129 | Interpreter_rememberScript (my interpreterStack -> interpreters [1].get(), & my file, fullTrust); |
| 130 | my interpreterStack -> runDown (autoInterpreter(), text.move(), false); |
| 131 | } |
| 132 | static void args_ok_selectionOnly (UiForm sendingForm, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */, |
| 133 | Interpreter /* interpreter */, conststring32 /* invokingButtonTitle */, bool /* modified */, void *void_me, Editor optionalEditor) |
| 134 | { |
| 135 | common_args_ok_selectionOnly (sendingForm, void_me, optionalEditor, false); |
| 136 | } |
| 137 | static void args_ok_selectionOnly_FULL_TRUST (UiForm sendingForm, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */, |
| 138 | Interpreter /* interpreter */, conststring32 /* invokingButtonTitle */, bool /* modified */, void *void_me, Editor optionalEditor) |
| 139 | { |
| 140 | common_args_ok_selectionOnly (sendingForm, void_me, optionalEditor, true); |
| 141 | } |
| 142 | |
| 143 | static void common_menu_cb_run (ScriptEditor me, const bool fullTrust) { |
| 144 | try { |
| 145 | Melder_assert (my interpreterStack); |
| 146 | my interpreterStack -> emptyAll (); |
| 147 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 148 | my interpreterStack.get(), |
| 149 | my optionalReferenceToOwningEditor, |
| 150 | & my file |
| 151 | ); |
| 152 | autostring32 text = GuiText_getString (my textWidget); |
| 153 | if (! MelderFile_isNull (& my file)) |
| 154 | MelderFile_setDefaultDir (& my file); // TODO: can be wrong |
| 155 | Melder_includeIncludeFiles (& text); |
| 156 | const integer npar = Interpreter_readParameters (interpreter.get(), text.get()); |
| 157 | if (npar != 0) { |
| 158 | /* |
| 159 | Pop up a dialog box for querying the arguments. |
| 160 | */ |
| 161 | my argsDialog = Interpreter_createForm (interpreter.get(), my windowForm, my optionalReferenceToOwningEditor, nullptr, |
| 162 | fullTrust ? args_ok_FULL_TRUST : args_ok, me, false); // trust setting cannot be done earlier |
| 163 | my interpreterStack -> interpreters [1] = interpreter.move(); |
| 164 | UiForm_do (my argsDialog.get(), false); |
| 165 | } else { |
| 166 | autoPraatBackground background; |
| 167 | if (! MelderFile_isNull (& my file)) |
| 168 | MelderFile_setDefaultDir (& my file); |
| 169 | Interpreter_rememberScript (interpreter.get(), & my file, fullTrust); |
| 170 | Melder_assert (interpreter -> owningInterpreterStack); |
| 171 | interpreter -> owningInterpreterStack -> emptyAll (); // TODO: should we create a new InterpreterStack instead, owned by the script editor? |
| 172 | interpreter -> owningInterpreterStack -> runDown (interpreter.move(), text.move(), false); |
| 173 | } |
| 174 | } catch (MelderError) { |
| 175 | Melder_flushError (U"The script didn’t run to its completion."); |
| 176 | } |
| 177 | } |
| 178 | static void menu_cb_run (ScriptEditor me, EDITOR_ARGS) { |
| 179 | common_menu_cb_run (me, false); |
| 180 | } |
| 181 | static void menu_cb_runWithFullTrust (ScriptEditor me, EDITOR_ARGS) { |
| 182 | common_menu_cb_run (me, true); |
| 183 | } |
| 184 | |
| 185 | static void common_menu_cb_runSelection (ScriptEditor me, const bool fullTrust) { |
| 186 | try { |
| 187 | Melder_assert (my interpreterStack); |
| 188 | my interpreterStack -> emptyAll (); |
| 189 | autoInterpreter interpreter = Interpreter_createFromEnvironment ( |
| 190 | my interpreterStack.get(), |
| 191 | my optionalReferenceToOwningEditor, |
| 192 | & my file |
| 193 | ); |
| 194 | autostring32 selectedText = GuiText_getSelection (my textWidget); |
| 195 | if (! selectedText) |
| 196 | Melder_throw (U"No text selected."); |
| 197 | if (! MelderFile_isNull (& my file)) |
| 198 | MelderFile_setDefaultDir (& my file); |
| 199 | Melder_includeIncludeFiles (& selectedText); |
| 200 | const integer npar = Interpreter_readParameters (interpreter.get(), selectedText.get()); |
| 201 | /* |
| 202 | TODO: check that no two procedures have the same name |
| 203 | */ |
| 204 | |
| 205 | /* |
| 206 | Add all the procedures to the selected text. |
| 207 | A procedure is counted as any text that honours the following conditions: |
| 208 | - it starts with a line starting with the text "procedure" |
| 209 | optionally preceded by whitespace and obligatorily followed by whitespace; |
| 210 | - it ends with a line starting with the text "endproc" |
| 211 | optionally preceded by whitespace and obligatorily followed by null or whitespace. |
| 212 | */ |
| 213 | autoMelderString textPlusProcedures; |
| 214 | MelderString_copy (& textPlusProcedures, selectedText.get()); |
| 215 | {// scope |
| 216 | autostring32 wholeText = GuiText_getString (my textWidget); |
| 217 | autoMelderReadText textReader = MelderReadText_createFromText (wholeText.move()); |
| 218 | int procedureDepth = 0; |
| 219 | for (;;) { |
| 220 | const conststring32 line = MelderReadText_readLine (textReader.get()); |
| 221 | if (! line) |
| 222 | break; |
| 223 | const conststring32 startOfCode = Melder_findEndOfHorizontalSpace (line); |
| 224 | if (Melder_startsWith (startOfCode, U"procedure") && Melder_isHorizontalSpace (startOfCode [9])) |
| 225 | procedureDepth += 1; |
| 226 | if (procedureDepth > 0) |
| 227 | MelderString_append (& textPlusProcedures, U"\n", line); |
| 228 | if (Melder_startsWith (startOfCode, U"endproc") && (startOfCode [7] == U'\0' || Melder_isHorizontalSpace (startOfCode [7]))) |
| 229 | procedureDepth -= 1; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | if (npar != 0) { |
| 234 | /* |
| 235 | Pop up a dialog box for querying the arguments. |
| 236 | */ |
| 237 | my argsDialog = Interpreter_createForm (interpreter.get(), my windowForm, my optionalReferenceToOwningEditor, nullptr, args_ok_selectionOnly, me, true); |
| 238 | my interpreterStack -> interpreters [1] = interpreter.move(); |
| 239 | UiForm_do (my argsDialog.get(), false); |
| 240 | } else { |
| 241 | autoPraatBackground background; |
| 242 | if (! MelderFile_isNull (& my file)) |
| 243 | MelderFile_setDefaultDir (& my file); |
| 244 | Interpreter_rememberScript (interpreter.get(), & my file, fullTrust); |
| 245 | Melder_assert (interpreter -> owningInterpreterStack); |
| 246 | interpreter -> owningInterpreterStack -> emptyAll (); // TODO: should we create a new InterpreterStack instead, owned by the script editor? |
| 247 | interpreter -> owningInterpreterStack -> runDown (interpreter.move(), Melder_dup (textPlusProcedures.string), false); |
| 248 | } |
| 249 | } catch (MelderError) { |
| 250 | Melder_flushError (U"The script selection didn’t run to its completion."); |
| 251 | } |
| 252 | } |
| 253 | static void menu_cb_runSelection (ScriptEditor me, EDITOR_ARGS) { |
| 254 | common_menu_cb_runSelection (me, false); |
| 255 | } |
| 256 | static void menu_cb_runSelectionWithFullTrust (ScriptEditor me, EDITOR_ARGS) { |
| 257 | common_menu_cb_runSelection (me, true); |
| 258 | } |
| 259 | |
| 260 | static void menu_cb_addToMenu (ScriptEditor me, EDITOR_ARGS) { |
| 261 | EDITOR_FORM (U"Add to menu", U"Add to fixed menu...") |
| 262 | WORD (window, U"Window", U"?") |
| 263 | SENTENCE (menu, U"Menu", U"File") |
| 264 | SENTENCE (command, U"Command", U"Do it...") |
| 265 | SENTENCE (afterCommand, U"After command", U"") |
| 266 | INTEGER (depth, U"Depth", U"0") |
| 267 | INFILE (scriptFile, U"Script file", U"") |
| 268 | EDITOR_OK |
| 269 | if (my wasCreatedInAnEditor()) |
| 270 | SET_STRING (window, my optionalOwningEditorClassName.get()) |
| 271 | if (MelderFile_isNull (& my file)) |
| 272 | SET_STRING (scriptFile, U"(please save your script first)") |
| 273 | else |
| 274 | SET_STRING (scriptFile, MelderFile_peekPath (& my file)); |
| 275 | EDITOR_DO |
| 276 | praat_addMenuCommandScript (window, menu, command, afterCommand, depth, scriptFile); |
| 277 | praat_show (); |
| 278 | EDITOR_END |
| 279 | } |
| 280 | |
| 281 | static void menu_cb_addToFixedMenu (ScriptEditor me, EDITOR_ARGS) { |
| 282 | EDITOR_FORM (U"Add to fixed menu", U"Add to fixed menu..."); |
| 283 | CHOICESTR (window, U"Window", 1) |
| 284 | OPTION (U"Objects") |
| 285 | OPTION (U"Picture") |
| 286 | SENTENCE (menu, U"Menu", U"New") |
| 287 | SENTENCE (command, U"Command", U"Do it...") |
| 288 | SENTENCE (afterCommand, U"After command", U"") |
| 289 | INTEGER (depth, U"Depth", U"0") |
| 290 | INFILE (scriptFile, U"Script file", U"") |
| 291 | EDITOR_OK |
| 292 | if (MelderFile_isNull (& my file)) |
| 293 | SET_STRING (scriptFile, U"(please save your script first)") |
| 294 | else |
| 295 | SET_STRING (scriptFile, MelderFile_peekPath (& my file)) |
| 296 | EDITOR_DO |
| 297 | praat_addMenuCommandScript (window, menu, command, afterCommand, depth, scriptFile); |
| 298 | praat_show (); |
| 299 | EDITOR_END |
| 300 | } |
| 301 | |
| 302 | static void menu_cb_addToDynamicMenu (ScriptEditor me, EDITOR_ARGS) { |
| 303 | EDITOR_FORM (U"Add to dynamic menu", U"Add to dynamic menu...") |
| 304 | WORD (class1, U"Class 1", U"Sound") |
| 305 | INTEGER (number1, U"Number 1", U"0") |
| 306 | WORD (class2, U"Class 2", U"") |
| 307 | INTEGER (number2, U"Number 2", U"0") |
| 308 | WORD (class3, U"Class 3", U"") |
| 309 | INTEGER (number3, U"Number 3", U"0") |
| 310 | SENTENCE (command, U"Command", U"Do it...") |
| 311 | SENTENCE (afterCommand, U"After command", U"") |
| 312 | INTEGER (depth, U"Depth", U"0") |
| 313 | INFILE (scriptFile, U"Script file", U"") |
| 314 | EDITOR_OK |
| 315 | if (MelderFile_isNull (& my file)) |
| 316 | SET_STRING (scriptFile, U"(please save your script first)") |
| 317 | else |
| 318 | SET_STRING (scriptFile, MelderFile_peekPath (& my file)) |
| 319 | EDITOR_DO |
| 320 | praat_addActionScript (class1, number1, class2, number2, class3, number3, command, afterCommand, depth, scriptFile); |
| 321 | praat_show (); |
| 322 | EDITOR_END |
| 323 | } |
| 324 | |
| 325 | static void menu_cb_clearHistory (ScriptEditor /* me */, EDITOR_ARGS) { |
| 326 | UiHistory_clear (); |
| 327 | } |
| 328 | |
| 329 | static void menu_cb_pasteHistory (ScriptEditor me, EDITOR_ARGS) { |
| 330 | char32 *history = UiHistory_get (); |
| 331 | if (! history || history [0] == U'\0') |
| 332 | Melder_throw (U"No history."); |
| 333 | integer length = Melder_length (history); |
| 334 | if (history [length - 1] != U'\n') { |
| 335 | UiHistory_write (U"\n"); |
| 336 | history = UiHistory_get (); |
| 337 | length = Melder_length (history); |
| 338 | } |
| 339 | if (history [0] == U'\n') { |
| 340 | history ++; |
| 341 | length --; |
| 342 | } |
| 343 | integer first = 0, last = 0; |
| 344 | autostring32 text = GuiText_getStringAndSelectionPosition (my textWidget, & first, & last); |
| 345 | GuiText_replace (my textWidget, first, last, history); |
| 346 | GuiText_setSelection (my textWidget, first, first + length); |
| 347 | GuiText_scrollToSelection (my textWidget); |
| 348 | } |
| 349 | |
| 350 | static void menu_cb_expandIncludeFiles (ScriptEditor me, EDITOR_ARGS) { |
| 351 | autostring32 text = GuiText_getString (my textWidget); |
| 352 | if (! MelderFile_isNull (& my file)) |
| 353 | MelderFile_setDefaultDir (& my file); |
| 354 | Melder_includeIncludeFiles (& text); |
| 355 | GuiText_setString (my textWidget, text.get()); |
| 356 | } |
| 357 | |
| 358 | static void menu_cb_AboutScriptEditor (ScriptEditor, EDITOR_ARGS) { Melder_help (U"ScriptEditor"); } |
| 359 | static void menu_cb_ScriptingTutorial (ScriptEditor, EDITOR_ARGS) { Melder_help (U"Scripting"); } |
| 360 | static void menu_cb_ScriptingExamples (ScriptEditor, EDITOR_ARGS) { Melder_help (U"Scripting examples"); } |
| 361 | static void menu_cb_PraatScript (ScriptEditor, EDITOR_ARGS) { Melder_help (U"Praat script"); } |
| 362 | static void menu_cb_FormulasTutorial (ScriptEditor, EDITOR_ARGS) { Melder_help (U"Formulas"); } |
| 363 | static void menu_cb_Functions (ScriptEditor, EDITOR_ARGS) { Melder_help (U"Functions"); } |
| 364 | static void menu_cb_DemoWindow (ScriptEditor, EDITOR_ARGS) { Melder_help (U"Demo window"); } |
| 365 | static void menu_cb_TheHistoryMechanism (ScriptEditor, EDITOR_ARGS) { Melder_help (U"History mechanism"); } |
| 366 | static void menu_cb_InitializationScripts (ScriptEditor, EDITOR_ARGS) { Melder_help (U"Initialization script"); } |
| 367 | static void menu_cb_AddingToAFixedMenu (ScriptEditor, EDITOR_ARGS) { Melder_help (U"Add to fixed menu..."); } |
| 368 | static void menu_cb_AddingToADynamicMenu (ScriptEditor, EDITOR_ARGS) { Melder_help (U"Add to dynamic menu..."); } |
| 369 | |
| 370 | void structScriptEditor :: v_createMenus () { |
| 371 | ScriptEditor_Parent :: v_createMenus (); |
| 372 | if (our wasCreatedInAnEditor()) { |
| 373 | Editor_addCommand (this, U"File", U"Add to menu...", 0, menu_cb_addToMenu); |
| 374 | } else { |
| 375 | Editor_addCommand (this, U"File", U"Add to fixed menu...", 0, menu_cb_addToFixedMenu); |
| 376 | Editor_addCommand (this, U"File", U"Add to dynamic menu...", 0, menu_cb_addToDynamicMenu); |
| 377 | } |
| 378 | Editor_addCommand (this, U"File", U"-- close --", 0, nullptr); |
| 379 | Editor_addCommand (this, U"Edit", U"-- history --", 0, nullptr); |
| 380 | Editor_addCommand (this, U"Edit", U"Clear history", 0, menu_cb_clearHistory); |
| 381 | Editor_addCommand (this, U"Edit", U"Paste history", 'H', menu_cb_pasteHistory); |
| 382 | Editor_addCommand (this, U"Convert", U"-- expand --", 0, nullptr); |
| 383 | Editor_addCommand (this, U"Convert", U"Expand include files", 0, menu_cb_expandIncludeFiles); |
| 384 | Editor_addMenu (this, U"Run", 0); |
| 385 | Editor_addCommand (this, U"Run", U"Run", 'R', menu_cb_run); |
| 386 | Editor_addCommand (this, U"Run", U"Run selection", 'T', menu_cb_runSelection); |
| 387 | Editor_addCommand (this, U"Run", U"-- run with full trust --", 0, nullptr); |
| 388 | Editor_addCommand (this, U"Run", U"Run with FULL TRUST", 0, menu_cb_runWithFullTrust); |
| 389 | Editor_addCommand (this, U"Run", U"Run selection with FULL TRUST", 0, menu_cb_runSelectionWithFullTrust); |
| 390 | } |
| 391 | |
| 392 | void structScriptEditor :: v_createMenuItems_help (EditorMenu menu) { |
| 393 | ScriptEditor_Parent :: v_createMenuItems_help (menu); |
| 394 | EditorMenu_addCommand (menu, U"About ScriptEditor", '?', menu_cb_AboutScriptEditor); |
| 395 | EditorMenu_addCommand (menu, U"Scripting tutorial", 0, menu_cb_ScriptingTutorial); |
| 396 | EditorMenu_addCommand (menu, U"Scripting examples", 0, menu_cb_ScriptingExamples); |
| 397 | EditorMenu_addCommand (menu, U"Praat script", 0, menu_cb_PraatScript); |
| 398 | EditorMenu_addCommand (menu, U"Formulas tutorial", 0, menu_cb_FormulasTutorial); |
| 399 | EditorMenu_addCommand (menu, U"Functions", 0, menu_cb_Functions); |
| 400 | EditorMenu_addCommand (menu, U"Demo window", 0, menu_cb_DemoWindow); |
| 401 | EditorMenu_addCommand (menu, U"-- help history --", 0, nullptr); |
| 402 | EditorMenu_addCommand (menu, U"The History mechanism", 0, menu_cb_TheHistoryMechanism); |
| 403 | EditorMenu_addCommand (menu, U"Initialization scripts", 0, menu_cb_InitializationScripts); |
| 404 | EditorMenu_addCommand (menu, U"-- help add --", 0, nullptr); |
| 405 | EditorMenu_addCommand (menu, U"Adding to a fixed menu", 0, menu_cb_AddingToAFixedMenu); |
| 406 | EditorMenu_addCommand (menu, U"Adding to a dynamic menu", 0, menu_cb_AddingToADynamicMenu); |
| 407 | } |
| 408 | |
| 409 | void ScriptEditor_init (ScriptEditor me, Editor optionalOwningEditor, conststring32 initialText) { |
| 410 | if (optionalOwningEditor) { |
| 411 | my optionalOwningEditorClassName = Melder_dup (Thing_className (optionalOwningEditor)); // BUG: no copying needed |
| 412 | my optionalReferenceToOwningEditor = optionalOwningEditor; |
| 413 | } |
| 414 | TextEditor_init (me, initialText); |
| 415 | my interpreterStack = InterpreterStack_create (optionalOwningEditor); |
| 416 | theReferencesToAllOpenScriptEditors. addItem_ref (me); |
| 417 | } |
| 418 | |
| 419 | autoScriptEditor ScriptEditor_createFromText (Editor optionalOwningEditor, conststring32 initialText) { |
| 420 | try { |
| 421 | autoScriptEditor me = Thing_new (ScriptEditor); |
| 422 | ScriptEditor_init (me.get(), optionalOwningEditor, initialText); |
| 423 | return me; |
| 424 | } catch (MelderError) { |
| 425 | Melder_throw (U"Script window not created."); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | autoScriptEditor ScriptEditor_createFromScript_canBeNull (Editor optionalOwningEditor, autoScript script) { |
| 430 | try { |
| 431 | structMelderFile scriptFile { }; |
| 432 | for (integer ieditor = 1; ieditor <= theReferencesToAllOpenScriptEditors.size; ieditor ++) { |
| 433 | ScriptEditor editor = theReferencesToAllOpenScriptEditors.at [ieditor]; |
| 434 | if (Melder_equ (script -> string.get(), MelderFile_peekPath (& editor -> file))) { |
| 435 | Editor_raise (editor); |
| 436 | Melder_pathToFile (script -> string.get(), & scriptFile); // ensure correct messaging format |
| 437 | Melder_appendError (U"The script ", & scriptFile, U" is already open and has been moved to the front."); |
| 438 | if (editor -> dirty) |
| 439 | Melder_appendError (U"Choose “Reopen from disk” if you want to revert to the old version."); |
| 440 | Melder_flushError (); |
| 441 | return autoScriptEditor(); // safe null, and `script` will be deleted |
| 442 | } |
| 443 | } |
| 444 | Melder_pathToFile (script -> string.get(), & scriptFile); |
| 445 | autostring32 text = MelderFile_readText (& scriptFile); |
| 446 | autoScriptEditor me = ScriptEditor_createFromText (optionalOwningEditor, text.get()); |
| 447 | MelderFile_copy (& scriptFile, & my file); |
| 448 | Script_rememberDuringThisAppSession_move (script.move()); |
| 449 | Thing_setName (me.get(), nullptr); |
| 450 | return me; |
| 451 | } catch (MelderError) { |
| 452 | Melder_throw (U"Script window not created."); |
| 453 | } |
| 454 | } |
| 455 | |
| 456 | void ScriptEditor_debug_printAllOpenScriptEditors () { |
| 457 | for (integer ieditor = 1; ieditor <= theReferencesToAllOpenScriptEditors.size; ieditor ++) { |
| 458 | ScriptEditor editor = theReferencesToAllOpenScriptEditors.at [ieditor]; |
| 459 | Melder_casual (U"Open script editor #", ieditor, U": <<", & editor -> file, U">>"); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /* End of file ScriptEditor.cpp */ |