ScriptEditor.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* ScriptEditor.cpp
  2. *
  3. * Copyright (C) 1997-2005,2007-2018 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 2 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. #include "ScriptEditor.h"
  19. #include "../kar/longchar.h"
  20. #include "praatP.h"
  21. #include "EditorM.h"
  22. Thing_implement (ScriptEditor, TextEditor, 0);
  23. static CollectionOf <structScriptEditor> theReferencesToAllOpenScriptEditors;
  24. bool ScriptEditors_dirty () {
  25. for (integer i = 1; i <= theReferencesToAllOpenScriptEditors.size; i ++) {
  26. ScriptEditor me = theReferencesToAllOpenScriptEditors.at [i];
  27. if (my dirty) return true;
  28. }
  29. return false;
  30. }
  31. void structScriptEditor :: v_destroy () noexcept {
  32. our argsDialog.reset(); // don't delay till delete
  33. theReferencesToAllOpenScriptEditors. undangleItem (this);
  34. ScriptEditor_Parent :: v_destroy ();
  35. }
  36. void structScriptEditor :: v_nameChanged () {
  37. bool dirtinessAlreadyShown = GuiWindow_setDirty (our windowForm, dirty);
  38. static MelderString buffer { };
  39. MelderString_copy (& buffer, name [0] ? U"Script" : U"untitled script");
  40. if (editorClass)
  41. MelderString_append (& buffer, U" [", environmentName.get(), U"]");
  42. if (name [0])
  43. MelderString_append (& buffer, U" ", MelderFile_messageName (& file));
  44. if (dirty && ! dirtinessAlreadyShown)
  45. MelderString_append (& buffer, U" (modified)");
  46. GuiShell_setTitle (windowForm, buffer.string);
  47. }
  48. void structScriptEditor :: v_goAway () {
  49. if (interpreter -> running) {
  50. Melder_flushError (U"Cannot close the script window while the script is running or paused. Please close or continue the pause or demo window.");
  51. } else {
  52. ScriptEditor_Parent :: v_goAway ();
  53. }
  54. }
  55. static void args_ok (UiForm sendingForm, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */,
  56. Interpreter /* interpreter */, conststring32 /* invokingButtonTitle */, bool /* modified */, void *void_me)
  57. {
  58. iam (ScriptEditor);
  59. autostring32 text = GuiText_getString (my textWidget);
  60. structMelderFile file { };
  61. if (my name [0]) {
  62. Melder_pathToFile (my name.get(), & file);
  63. MelderFile_setDefaultDir (& file);
  64. }
  65. Melder_includeIncludeFiles (& text);
  66. Interpreter_getArgumentsFromDialog (my interpreter.get(), sendingForm);
  67. autoPraatBackground background;
  68. if (my name [0]) MelderFile_setDefaultDir (& file);
  69. Interpreter_run (my interpreter.get(), text.get());
  70. }
  71. static void args_ok_selectionOnly (UiForm sendingForm, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */,
  72. Interpreter /* interpreter */, conststring32 /* invokingButtonTitle */, bool /* modified */, void *void_me)
  73. {
  74. iam (ScriptEditor);
  75. autostring32 text = GuiText_getSelection (my textWidget);
  76. if (! text)
  77. Melder_throw (U"No text is selected any longer.\nPlease reselect or click Cancel.");
  78. structMelderFile file { };
  79. if (my name [0]) {
  80. Melder_pathToFile (my name.get(), & file);
  81. MelderFile_setDefaultDir (& file);
  82. }
  83. Melder_includeIncludeFiles (& text);
  84. Interpreter_getArgumentsFromDialog (my interpreter.get(), sendingForm);
  85. autoPraatBackground background;
  86. if (my name [0]) MelderFile_setDefaultDir (& file);
  87. Interpreter_run (my interpreter.get(), text.get());
  88. }
  89. static void menu_cb_run (ScriptEditor me, EDITOR_ARGS_DIRECT) {
  90. if (my interpreter -> running)
  91. Melder_throw (U"The script is already running (paused). Please close or continue the pause or demo window.");
  92. autostring32 text = GuiText_getString (my textWidget);
  93. trace (U"Running the following script (1):\n", text.get());
  94. structMelderFile file { };
  95. if (my name [0]) {
  96. Melder_pathToFile (my name.get(), & file);
  97. MelderFile_setDefaultDir (& file);
  98. }
  99. Melder_includeIncludeFiles (& text);
  100. integer npar = Interpreter_readParameters (my interpreter.get(), text.get());
  101. if (npar) {
  102. /*
  103. * Pop up a dialog box for querying the arguments.
  104. */
  105. my argsDialog = Interpreter_createForm (my interpreter.get(), my windowForm, nullptr, args_ok, me, false);
  106. UiForm_do (my argsDialog.get(), false);
  107. } else {
  108. autoPraatBackground background;
  109. if (my name [0]) MelderFile_setDefaultDir (& file);
  110. trace (U"Running the following script (2):\n", text.get());
  111. Interpreter_run (my interpreter.get(), text.get());
  112. }
  113. }
  114. static void menu_cb_runSelection (ScriptEditor me, EDITOR_ARGS_DIRECT) {
  115. if (my interpreter -> running)
  116. Melder_throw (U"The script is already running (paused). Please close or continue the pause or demo window.");
  117. autostring32 text = GuiText_getSelection (my textWidget);
  118. if (! text)
  119. Melder_throw (U"No text selected.");
  120. structMelderFile file { };
  121. if (my name [0]) {
  122. Melder_pathToFile (my name.get(), & file);
  123. MelderFile_setDefaultDir (& file);
  124. }
  125. Melder_includeIncludeFiles (& text);
  126. integer npar = Interpreter_readParameters (my interpreter.get(), text.get());
  127. if (npar) {
  128. /*
  129. * Pop up a dialog box for querying the arguments.
  130. */
  131. my argsDialog = Interpreter_createForm (my interpreter.get(), my windowForm, nullptr, args_ok_selectionOnly, me, true);
  132. UiForm_do (my argsDialog.get(), false);
  133. } else {
  134. autoPraatBackground background;
  135. if (my name [0]) MelderFile_setDefaultDir (& file);
  136. Interpreter_run (my interpreter.get(), text.get());
  137. }
  138. }
  139. static void menu_cb_addToMenu (ScriptEditor me, EDITOR_ARGS_FORM) {
  140. EDITOR_FORM (U"Add to menu", U"Add to fixed menu...")
  141. WORD (window, U"Window", U"?")
  142. SENTENCE (menu, U"Menu", U"File")
  143. SENTENCE (command, U"Command", U"Do it...")
  144. SENTENCE (afterCommand, U"After command", U"")
  145. INTEGER (depth, U"Depth", U"0")
  146. TEXTFIELD (scriptFile, U"Script file:", U"")
  147. EDITOR_OK
  148. if (my editorClass) SET_STRING (window, my editorClass -> className)
  149. if (my name [0])
  150. SET_STRING (scriptFile, my name.get())
  151. else
  152. SET_STRING (scriptFile, U"(please save your script first)")
  153. EDITOR_DO
  154. praat_addMenuCommandScript (window, menu, command, afterCommand, depth, scriptFile);
  155. praat_show ();
  156. EDITOR_END
  157. }
  158. static void menu_cb_addToFixedMenu (ScriptEditor me, EDITOR_ARGS_FORM) {
  159. EDITOR_FORM (U"Add to fixed menu", U"Add to fixed menu...");
  160. RADIOSTR (window, U"Window", 1)
  161. RADIOBUTTON (U"Objects")
  162. RADIOBUTTON (U"Picture")
  163. SENTENCE (menu, U"Menu", U"New")
  164. SENTENCE (command, U"Command", U"Do it...")
  165. SENTENCE (afterCommand, U"After command", U"")
  166. INTEGER (depth, U"Depth", U"0")
  167. TEXTFIELD (scriptFile, U"Script file:", U"")
  168. EDITOR_OK
  169. if (my name [0])
  170. SET_STRING (scriptFile, my name.get())
  171. else
  172. SET_STRING (scriptFile, U"(please save your script first)")
  173. EDITOR_DO
  174. praat_addMenuCommandScript (window, menu, command, afterCommand, depth, scriptFile);
  175. praat_show ();
  176. EDITOR_END
  177. }
  178. static void menu_cb_addToDynamicMenu (ScriptEditor me, EDITOR_ARGS_FORM) {
  179. EDITOR_FORM (U"Add to dynamic menu", U"Add to dynamic menu...")
  180. WORD (class1, U"Class 1", U"Sound")
  181. INTEGER (number1, U"Number 1", U"0")
  182. WORD (class2, U"Class 2", U"")
  183. INTEGER (number2, U"Number 2", U"0")
  184. WORD (class3, U"Class 3", U"")
  185. INTEGER (number3, U"Number 3", U"0")
  186. SENTENCE (command, U"Command", U"Do it...")
  187. SENTENCE (afterCommand, U"After command", U"")
  188. INTEGER (depth, U"Depth", U"0")
  189. TEXTFIELD (scriptFile, U"Script file:", U"")
  190. EDITOR_OK
  191. if (my name [0])
  192. SET_STRING (scriptFile, my name.get())
  193. else
  194. SET_STRING (scriptFile, U"(please save your script first)")
  195. EDITOR_DO
  196. praat_addActionScript (class1, number1, class2, number2, class3, number3, command, afterCommand, depth, scriptFile);
  197. praat_show ();
  198. EDITOR_END
  199. }
  200. static void menu_cb_clearHistory (ScriptEditor /* me */, EDITOR_ARGS_DIRECT) {
  201. UiHistory_clear ();
  202. }
  203. static void menu_cb_pasteHistory (ScriptEditor me, EDITOR_ARGS_DIRECT) {
  204. char32 *history = UiHistory_get ();
  205. if (! history || history [0] == U'\0')
  206. Melder_throw (U"No history.");
  207. integer length = str32len (history);
  208. if (history [length - 1] != U'\n') {
  209. UiHistory_write (U"\n");
  210. history = UiHistory_get ();
  211. length = str32len (history);
  212. }
  213. if (history [0] == U'\n') {
  214. history ++;
  215. length --;
  216. }
  217. integer first = 0, last = 0;
  218. autostring32 text = GuiText_getStringAndSelectionPosition (my textWidget, & first, & last);
  219. GuiText_replace (my textWidget, first, last, history);
  220. GuiText_setSelection (my textWidget, first, first + length);
  221. GuiText_scrollToSelection (my textWidget);
  222. }
  223. static void menu_cb_expandIncludeFiles (ScriptEditor me, EDITOR_ARGS_DIRECT) {
  224. structMelderFile file { };
  225. autostring32 text = GuiText_getString (my textWidget);
  226. if (my name [0]) {
  227. Melder_pathToFile (my name.get(), & file);
  228. MelderFile_setDefaultDir (& file);
  229. }
  230. Melder_includeIncludeFiles (& text);
  231. GuiText_setString (my textWidget, text.get());
  232. }
  233. static void menu_cb_AboutScriptEditor (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"ScriptEditor"); }
  234. static void menu_cb_ScriptingTutorial (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"Scripting"); }
  235. static void menu_cb_ScriptingExamples (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"Scripting examples"); }
  236. static void menu_cb_PraatScript (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"Praat script"); }
  237. static void menu_cb_FormulasTutorial (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"Formulas"); }
  238. static void menu_cb_DemoWindow (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"Demo window"); }
  239. static void menu_cb_TheHistoryMechanism (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"History mechanism"); }
  240. static void menu_cb_InitializationScripts (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"Initialization script"); }
  241. static void menu_cb_AddingToAFixedMenu (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"Add to fixed menu..."); }
  242. static void menu_cb_AddingToADynamicMenu (ScriptEditor, EDITOR_ARGS_DIRECT) { Melder_help (U"Add to dynamic menu..."); }
  243. void structScriptEditor :: v_createMenus () {
  244. ScriptEditor_Parent :: v_createMenus ();
  245. if (editorClass) {
  246. Editor_addCommand (this, U"File", U"Add to menu...", 0, menu_cb_addToMenu);
  247. } else {
  248. Editor_addCommand (this, U"File", U"Add to fixed menu...", 0, menu_cb_addToFixedMenu);
  249. Editor_addCommand (this, U"File", U"Add to dynamic menu...", 0, menu_cb_addToDynamicMenu);
  250. }
  251. Editor_addCommand (this, U"File", U"-- close --", 0, nullptr);
  252. Editor_addCommand (this, U"Edit", U"-- history --", 0, nullptr);
  253. Editor_addCommand (this, U"Edit", U"Clear history", 0, menu_cb_clearHistory);
  254. Editor_addCommand (this, U"Edit", U"Paste history", 'H', menu_cb_pasteHistory);
  255. Editor_addCommand (this, U"Convert", U"-- expand --", 0, nullptr);
  256. Editor_addCommand (this, U"Convert", U"Expand include files", 0, menu_cb_expandIncludeFiles);
  257. Editor_addMenu (this, U"Run", 0);
  258. Editor_addCommand (this, U"Run", U"Run", 'R', menu_cb_run);
  259. Editor_addCommand (this, U"Run", U"Run selection", 'T', menu_cb_runSelection);
  260. }
  261. void structScriptEditor :: v_createHelpMenuItems (EditorMenu menu) {
  262. ScriptEditor_Parent :: v_createHelpMenuItems (menu);
  263. EditorMenu_addCommand (menu, U"About ScriptEditor", '?', menu_cb_AboutScriptEditor);
  264. EditorMenu_addCommand (menu, U"Scripting tutorial", 0, menu_cb_ScriptingTutorial);
  265. EditorMenu_addCommand (menu, U"Scripting examples", 0, menu_cb_ScriptingExamples);
  266. EditorMenu_addCommand (menu, U"Praat script", 0, menu_cb_PraatScript);
  267. EditorMenu_addCommand (menu, U"Formulas tutorial", 0, menu_cb_FormulasTutorial);
  268. EditorMenu_addCommand (menu, U"Demo window", 0, menu_cb_DemoWindow);
  269. EditorMenu_addCommand (menu, U"-- help history --", 0, nullptr);
  270. EditorMenu_addCommand (menu, U"The History mechanism", 0, menu_cb_TheHistoryMechanism);
  271. EditorMenu_addCommand (menu, U"Initialization scripts", 0, menu_cb_InitializationScripts);
  272. EditorMenu_addCommand (menu, U"-- help add --", 0, nullptr);
  273. EditorMenu_addCommand (menu, U"Adding to a fixed menu", 0, menu_cb_AddingToAFixedMenu);
  274. EditorMenu_addCommand (menu, U"Adding to a dynamic menu", 0, menu_cb_AddingToADynamicMenu);
  275. }
  276. void ScriptEditor_init (ScriptEditor me, Editor environment, conststring32 initialText) {
  277. if (environment) {
  278. my environmentName = Melder_dup (environment -> name.get());
  279. my editorClass = environment -> classInfo;
  280. }
  281. TextEditor_init (me, initialText);
  282. my interpreter = Interpreter_createFromEnvironment (environment);
  283. theReferencesToAllOpenScriptEditors. addItem_ref (me);
  284. }
  285. autoScriptEditor ScriptEditor_createFromText (Editor environment, conststring32 initialText) {
  286. try {
  287. autoScriptEditor me = Thing_new (ScriptEditor);
  288. ScriptEditor_init (me.get(), environment, initialText);
  289. return me;
  290. } catch (MelderError) {
  291. Melder_throw (U"Script window not created.");
  292. }
  293. }
  294. autoScriptEditor ScriptEditor_createFromScript_canBeNull (Editor environment, Script script) {
  295. try {
  296. for (integer ieditor = 1; ieditor <= theReferencesToAllOpenScriptEditors.size; ieditor ++) {
  297. ScriptEditor editor = theReferencesToAllOpenScriptEditors.at [ieditor];
  298. if (MelderFile_equal (& script -> file, & editor -> file)) {
  299. Editor_raise (editor);
  300. Melder_appendError (U"The script ", & script -> file, U" is already open and has been moved to the front.");
  301. if (editor -> dirty)
  302. Melder_appendError (U"Choose \"Reopen from disk\" if you want to revert to the old version.");
  303. Melder_flushError ();
  304. return autoScriptEditor(); // safe null
  305. }
  306. }
  307. autostring32 text = MelderFile_readText (& script -> file);
  308. autoScriptEditor me = ScriptEditor_createFromText (environment, text.get());
  309. MelderFile_copy (& script -> file, & my file);
  310. Thing_setName (me.get(), Melder_fileToPath (& script -> file));
  311. return me;
  312. } catch (MelderError) {
  313. Melder_throw (U"Script window not created.");
  314. }
  315. }
  316. void ScriptEditor_debug_printAllOpenScriptEditors () {
  317. for (integer ieditor = 1; ieditor <= theReferencesToAllOpenScriptEditors.size; ieditor ++) {
  318. ScriptEditor editor = theReferencesToAllOpenScriptEditors.at [ieditor];
  319. Melder_casual (U"Open script editor #", ieditor, U": <<", & editor -> file, U">>");
  320. }
  321. }
  322. /* End of file ScriptEditor.cpp */