Debian Codegraph @

sys/NotebookEditor.cpp

raw symbols SCIP
1 /* NotebookEditor.cpp
2 *
3 * Copyright (C) 2023,2024,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 "NotebookEditor.h"
20 #include "../kar/longchar.h"
21 #include "praatP.h"
22 #include "EditorM.h"
23
24 Thing_implement (NotebookEditor, TextEditor, 0);
25
26 CollectionOf <structNotebookEditor> theReferencesToAllOpenNotebookEditors;
27
28 bool NotebookEditors_dirty () {
29 for (integer i = 1; i <= theReferencesToAllOpenNotebookEditors.size; i ++) {
30 const NotebookEditor me = theReferencesToAllOpenNotebookEditors.at [i];
31 if (my dirty)
32 return true;
33 }
34 return false;
35 }
36
37 void structNotebookEditor :: v9_destroy () noexcept {
38 our argsDialog. reset(); // don't delay till delete
39 theReferencesToAllOpenNotebookEditors. undangleItem (this);
40 NotebookEditor_Parent :: v9_destroy ();
41 }
42
43 void structNotebookEditor :: v_nameChanged () {
44 /*
45 As TextEditor does, we totally ignore the name that our boss wants to give us.
46 Instead, we compose the window title from three ingredients:
47
48 (1) whether we are already associated with a file or not;
49 (2) if so, the full file path;
50 (3) whether our text has been modified (i.e. whether we are "dirty");
51
52 (last checked 2023-03-25)
53 */
54 const bool dirtinessAlreadyShown = GuiWindow_setDirty (our windowForm, our dirty); // (3) on the Mac (last checked 2023-03-25)
55 static MelderString buffer;
56 MelderString_copy (& buffer, MelderFile_isNull (& our file) ? U"untitled notebook" : U"Notebook"); // (1)
57 if (! MelderFile_isNull (& our file))
58 MelderString_append (& buffer, U" ", MelderFile_messageName (& our file)); // (2)
59 if (our dirty && ! dirtinessAlreadyShown)
60 MelderString_append (& buffer, U" (modified)"); // (3) on Windows and Linux (last checked 2023-02-25)
61 GuiShell_setTitle (windowForm, buffer.string);
62 }
63
64 void structNotebookEditor :: v_goAway () {
65 if (our interpreterStack -> interpreters [1] && our interpreterStack -> interpreters [1] -> running)
66 Melder_flushError (U"Cannot close the NotebookEditor while the notebook is running or paused.\n"
67 "Please close or continue the pause, trust or demo window.");
68 else
69 NotebookEditor_Parent :: v_goAway ();
70 }
71
72 static void args_ok (UiForm sendingForm, integer /* narg */, Stackel /* args */, conststring32 /* sendingString */,
73 Interpreter /* interpreter */, conststring32 /* invokingButtonTitle */, bool /* modified */, void *void_me, Editor optionalEditor)
74 {
75 iam (NotebookEditor);
76 autostring32 text = GuiText_getString (my textWidget);
77 if (! MelderFile_isNull (& my file))
78 MelderFile_setDefaultDir (& my file);
79 Melder_includeIncludeFiles (& text, true);
80
81 Melder_assert (my interpreterStack -> interpreters [1]);
82 Interpreter_getArgumentsFromDialog (my interpreterStack -> interpreters [1].get(), sendingForm);
83
84 autoPraatBackground background;
85 if (! MelderFile_isNull (& my file))
86 MelderFile_setDefaultDir (& my file);
87
88 autoMelderReadText readText = MelderReadText_createFromText (text.move());
89 autoManPages manPages = ManPages_createFromText (readText.get(), & my file); // readText can release, because manPages duplicates (last checked 2023-03-25)
90 ManPage firstPage = manPages -> pages.at [1];
91 autoManual manual = Manual_create (firstPage -> title.get(), my interpreterStack -> interpreters [1].get(), manPages.releaseToAmbiguousOwner(), true, true);
92 manual.releaseToUser ();
93 }
94
95 static void menu_cb_run (NotebookEditor me, EDITOR_ARGS) {
96 if (my interpreterStack -> interpreters [1] && my interpreterStack -> interpreters [1] -> running)
97 Melder_throw (U"The notebook is already running (paused). Please close or continue the pause, trust or demo window.");
98 //Melder_assert (! my interpreterStack -> interpreters [1]); // TRICKY
99 my interpreterStack -> interpreters [1] = Interpreter_createFromEnvironment (
100 my interpreterStack.get(),
101 Editor (nullptr),
102 & my file
103 );
104 TRACE
105 trace (U"File: ", & my file);
106 integer startOfSelection, endOfSelection;
107 autostring32 text = GuiText_getStringAndSelectionPosition (my textWidget, & startOfSelection, & endOfSelection);
108 if (Melder_startsWith (text.get(), U"\"")) {
109 if (! MelderFile_isNull (& my file))
110 MelderFile_setDefaultDir (& my file);
111 Melder_includeIncludeFiles (& text, true);
112 const integer npar = Interpreter_readParameters (my interpreterStack -> interpreters [1].get(), text.get());
113 if (npar != 0) {
114 /*
115 Pop up a dialog box for querying the arguments.
116 */
117 my argsDialog = Interpreter_createForm (my interpreterStack -> interpreters [1].get(), my windowForm, nullptr, nullptr, args_ok, me, false);
118 UiForm_do (my argsDialog.get(), false);
119 } else {
120 autoPraatBackground background;
121 if (! MelderFile_isNull (& my file))
122 MelderFile_setDefaultDir (& my file);
123
124 autoMelderReadText readText = MelderReadText_createFromText (text.move());
125 autoManPages manPages = ManPages_createFromText (readText.get(), & my file); // readText can release, because manPages duplicates (last checked 2023-03-25)
126 ManPage firstPage = manPages -> pages.at [1];
127 Interpreter_rememberNotebook (my interpreterStack -> interpreters [1].get(), & my file, false);
128 autoManual manual = Manual_create (firstPage -> title.get(), my interpreterStack -> interpreters [1].get(), manPages.releaseToAmbiguousOwner(), true, true);
129 manual.releaseToUser ();
130 }
131 } else if (str32str (text.get(), U"\n####################")) {
132 autoMelderReadText readText = MelderReadText_createFromText (text.move());
133 autoManPages you = Thing_new (ManPages);
134 your dynamic = false;
135 integer startingPage = ManPages_addPagesFromNotebookReader (you.get(), readText.get(), startOfSelection, endOfSelection);
136 Melder_clip (1_integer, & startingPage, your pages.size);
137 ManPage firstPage = your pages.at [startingPage];
138 autoManual manual = Manual_create (firstPage -> title.get(), my interpreterStack -> interpreters [1].get(), you.releaseToAmbiguousOwner(), true, true);
139 manual.releaseToUser ();
140 } else
141 Melder_throw (U"A Praat notebook should either start with a title between straight double quotes (\"\"), "
142 "or contain multiple such pieces separated by \"####################\" (or longer) lines on all sides.");
143 }
144
145 static void menu_cb_runChunk (NotebookEditor me, EDITOR_ARGS) {
146 if (my interpreterStack -> interpreters [1] && my interpreterStack -> interpreters [1] -> running)
147 Melder_throw (U"The notebook is already running (paused). Please close or continue the pause, trust or demo window.");
148 autostring32 text = GuiText_getSelection (my textWidget); // TODO: replace with chunk
149 if (! text)
150 Melder_throw (U"No text selected.");
151 if (! MelderFile_isNull (& my file))
152 MelderFile_setDefaultDir (& my file);
153 Melder_includeIncludeFiles (& text);
154 autoPraatBackground background;
155 if (! MelderFile_isNull (& my file))
156 MelderFile_setDefaultDir (& my file); // TODO: probably wrong
157 autoInterpreter interpreter = Interpreter_createFromEnvironment (
158 my interpreterStack.get(),
159 Editor (nullptr),
160 MelderFile (nullptr) // TODO: probably wrong
161 );
162 Interpreter_rememberNotebook (interpreter.get(), & my file, false);
163 my interpreterStack -> emptyAll ();
164 my interpreterStack -> runDown (interpreter.move(), text.move(), true);
165 }
166
167 static void menu_cb_expandIncludeFiles (NotebookEditor me, EDITOR_ARGS) {
168 autostring32 text = GuiText_getString (my textWidget);
169 if (! MelderFile_isNull (& my file))
170 MelderFile_setDefaultDir (& my file);
171 Melder_includeIncludeFiles (& text, true);
172 GuiText_setString (my textWidget, text.get());
173 }
174
175 static void menu_cb_AboutNotebookEditor (NotebookEditor, EDITOR_ARGS) { Melder_help (U"NotebookEditor"); }
176 static void menu_cb_ScriptingTutorial (NotebookEditor, EDITOR_ARGS) { Melder_help (U"Scripting"); }
177 static void menu_cb_ScriptingExamples (NotebookEditor, EDITOR_ARGS) { Melder_help (U"Scripting examples"); }
178 static void menu_cb_PraatScript (NotebookEditor, EDITOR_ARGS) { Melder_help (U"Praat script"); }
179 static void menu_cb_FormulasTutorial (NotebookEditor, EDITOR_ARGS) { Melder_help (U"Formulas"); }
180 static void menu_cb_Functions (NotebookEditor, EDITOR_ARGS) { Melder_help (U"Functions"); }
181
182 void structNotebookEditor :: v_createMenus () {
183 NotebookEditor_Parent :: v_createMenus ();
184 Editor_addCommand (this, U"File", U"-- close --", 0, nullptr);
185 Editor_addCommand (this, U"Convert", U"-- expand --", 0, nullptr);
186 Editor_addCommand (this, U"Convert", U"Expand include files", 0, menu_cb_expandIncludeFiles);
187 Editor_addMenu (this, U"Run", 0);
188 Editor_addCommand (this, U"Run", U"Run", 'R', menu_cb_run);
189 Editor_addCommand (this, U"Run", U"Run chunk", 'T', menu_cb_runChunk);
190 }
191
192 void structNotebookEditor :: v_createMenuItems_help (EditorMenu menu) {
193 NotebookEditor_Parent :: v_createMenuItems_help (menu);
194 EditorMenu_addCommand (menu, U"About NotebookEditor", '?', menu_cb_AboutNotebookEditor);
195 EditorMenu_addCommand (menu, U"Scripting tutorial", 0, menu_cb_ScriptingTutorial);
196 EditorMenu_addCommand (menu, U"Scripting examples", 0, menu_cb_ScriptingExamples);
197 EditorMenu_addCommand (menu, U"Praat script", 0, menu_cb_PraatScript);
198 EditorMenu_addCommand (menu, U"Formulas tutorial", 0, menu_cb_FormulasTutorial);
199 EditorMenu_addCommand (menu, U"Functions", 0, menu_cb_Functions);
200 }
201
202 void NotebookEditor_init (NotebookEditor me, conststring32 initialText) {
203 TextEditor_init (me, initialText);
204 my interpreterStack = InterpreterStack_create (Editor (nullptr));
205 theReferencesToAllOpenNotebookEditors. addItem_ref (me);
206 }
207
208 autoNotebookEditor NotebookEditor_createFromText (conststring32 initialText) {
209 try {
210 autoNotebookEditor me = Thing_new (NotebookEditor);
211 NotebookEditor_init (me.get(), initialText);
212 return me;
213 } catch (MelderError) {
214 Melder_throw (U"Notebook window not created.");
215 }
216 }
217
218 autoNotebookEditor NotebookEditor_createFromNotebook_canBeNull (Notebook notebook) {
219 try {
220 structMelderFile notebookFile { };
221 for (integer ieditor = 1; ieditor <= theReferencesToAllOpenNotebookEditors.size; ieditor ++) {
222 NotebookEditor editor = theReferencesToAllOpenNotebookEditors.at [ieditor];
223 if (Melder_equ (notebook -> string.get(), MelderFile_peekPath (& editor -> file))) {
224 Editor_raise (editor);
225 Melder_pathToFile (notebook -> string.get(), & notebookFile);
226 Melder_appendError (U"The notebook ", & notebookFile, U" is already open and has been moved to the front.");
227 if (editor -> dirty)
228 Melder_appendError (U"Choose “Reopen from disk” if you want to revert to the old version.");
229 Melder_flushError ();
230 return autoNotebookEditor(); // safe null
231 }
232 }
233 Melder_pathToFile (notebook -> string.get(), & notebookFile);
234 autostring32 text = MelderFile_readText (& notebookFile);
235 autoNotebookEditor me = NotebookEditor_createFromText (text.get());
236 MelderFile_copy (& notebookFile, & my file);
237 Thing_setName (me.get(), nullptr);
238 return me;
239 } catch (MelderError) {
240 Melder_throw (U"Notebook window not created.");
241 }
242 }
243
244 void NotebookEditor_debug_printAllOpenNotebookEditors () {
245 for (integer ieditor = 1; ieditor <= theReferencesToAllOpenNotebookEditors.size; ieditor ++) {
246 NotebookEditor editor = theReferencesToAllOpenNotebookEditors.at [ieditor];
247 Melder_casual (U"Open notebook editor #", ieditor, U": <<", & editor -> file, U">>");
248 }
249 }
250
251 /* End of file NotebookEditor.cpp */