comment reduxed MultipleAssignment
[nedit-bw.git] / newMacroFnRegistration.patch
blob41aa603c2da9a1c369fc119b7a9757a4f0605a3a
1 From: Tony Balinski <ajbj@free.fr>
2 Subject: Change the registration of macro built-ins to improve maintenance
4 The association of name and value (either a function pointer or an integer)
5 is made explicit here. It also allows for other tables of such associations
6 elsewhere, allowing for separate (theme-based) macro function definition
7 files. It makes macro definition more like the action routine naming too.
9 ---
11 source/macro.c | 280 +++++++++++++++++++++++++++++++++++++--------------------
12 source/macro.h | 22 ++++
13 2 files changed, 203 insertions(+), 99 deletions(-)
15 diff --quilt old/source/macro.c new/source/macro.c
16 --- old/source/macro.c
17 +++ new/source/macro.c
18 @@ -418,86 +418,136 @@ static int filenameDialogMS(WindowInfo*
19 DataValue* result, char** errMsg);
21 /* Built-in subroutines and variables for the macro language */
22 -static BuiltInSubr MacroSubrs[] = {lengthMS, getRangeMS, tPrintMS,
23 - dialogMS, stringDialogMS, replaceRangeMS, replaceSelectionMS,
24 - setCursorPosMS, getCharacterMS, minMS, maxMS, searchMS,
25 - searchStringMS, substringMS, replaceSubstringMS, readFileMS,
26 - writeFileMS, appendFileMS, beepMS, getSelectionMS, validNumberMS,
27 - replaceInStringMS, selectMS, selectRectangleMS, focusWindowMS,
28 - shellCmdMS, stringToClipboardMS, clipboardToStringMS, toupperMS,
29 - tolowerMS, listDialogMS, getenvMS,
30 - stringCompareMS, splitMS, calltipMS, killCalltipMS,
31 -/* DISABLED for 5.4 setBacklightStringMS,*/
32 - rangesetCreateMS, rangesetDestroyMS,
33 - rangesetAddMS, rangesetSubtractMS, rangesetInvertMS,
34 - rangesetInfoMS, rangesetRangeMS, rangesetIncludesPosMS,
35 - rangesetSetColorMS, rangesetSetNameMS, rangesetSetModeMS,
36 - rangesetGetByNameMS,
37 - getPatternByNameMS, getPatternAtPosMS,
38 - getStyleByNameMS, getStyleAtPosMS, filenameDialogMS
39 - };
40 -#define N_MACRO_SUBRS (sizeof MacroSubrs/sizeof *MacroSubrs)
41 -static const char *MacroSubrNames[N_MACRO_SUBRS] = {"length", "get_range", "t_print",
42 - "dialog", "string_dialog", "replace_range", "replace_selection",
43 - "set_cursor_pos", "get_character", "min", "max", "search",
44 - "search_string", "substring", "replace_substring", "read_file",
45 - "write_file", "append_file", "beep", "get_selection", "valid_number",
46 - "replace_in_string", "select", "select_rectangle", "focus_window",
47 - "shell_command", "string_to_clipboard", "clipboard_to_string",
48 - "toupper", "tolower", "list_dialog", "getenv",
49 - "string_compare", "split", "calltip", "kill_calltip",
50 -/* DISABLED for 5.4 "set_backlight_string", */
51 - "rangeset_create", "rangeset_destroy",
52 - "rangeset_add", "rangeset_subtract", "rangeset_invert",
53 - "rangeset_info", "rangeset_range", "rangeset_includes",
54 - "rangeset_set_color", "rangeset_set_name", "rangeset_set_mode",
55 - "rangeset_get_by_name",
56 - "get_pattern_by_name", "get_pattern_at_pos",
57 - "get_style_by_name", "get_style_at_pos", "filename_dialog"
58 - };
59 -static BuiltInSubr SpecialVars[] = {cursorMV, lineMV, columnMV,
60 - fileNameMV, filePathMV, lengthMV, selectionStartMV, selectionEndMV,
61 - selectionLeftMV, selectionRightMV, wrapMarginMV, tabDistMV,
62 - emTabDistMV, useTabsMV, languageModeMV, modifiedMV,
63 - statisticsLineMV, incSearchLineMV, showLineNumbersMV,
64 - autoIndentMV, wrapTextMV, highlightSyntaxMV,
65 - makeBackupCopyMV, incBackupMV, showMatchingMV, matchSyntaxBasedMV,
66 - overTypeModeMV, readOnlyMV, lockedMV, fileFormatMV,
67 - fontNameMV, fontNameItalicMV,
68 - fontNameBoldMV, fontNameBoldItalicMV, subscriptSepMV,
69 - minFontWidthMV, maxFontWidthMV, topLineMV, numDisplayLinesMV,
70 - displayWidthMV, activePaneMV, nPanesMV, emptyArrayMV,
71 - serverNameMV, calltipIDMV,
72 -/* DISABLED for 5.4 backlightStringMV, */
73 - rangesetListMV, versionMV
74 - };
75 -#define N_SPECIAL_VARS (sizeof SpecialVars/sizeof *SpecialVars)
76 -static const char *SpecialVarNames[N_SPECIAL_VARS] = {"$cursor", "$line", "$column",
77 - "$file_name", "$file_path", "$text_length", "$selection_start",
78 - "$selection_end", "$selection_left", "$selection_right",
79 - "$wrap_margin", "$tab_dist", "$em_tab_dist", "$use_tabs",
80 - "$language_mode", "$modified",
81 - "$statistics_line", "$incremental_search_line", "$show_line_numbers",
82 - "$auto_indent", "$wrap_text", "$highlight_syntax",
83 - "$make_backup_copy", "$incremental_backup", "$show_matching", "$match_syntax_based",
84 - "$overtype_mode", "$read_only", "$locked", "$file_format",
85 - "$font_name", "$font_name_italic",
86 - "$font_name_bold", "$font_name_bold_italic", "$sub_sep",
87 - "$min_font_width", "$max_font_width", "$top_line", "$n_display_lines",
88 - "$display_width", "$active_pane", "$n_panes", "$empty_array",
89 - "$server_name", "$calltip_ID",
90 -/* DISABLED for 5.4 "$backlight_string", */
91 - "$rangeset_list", "$VERSION"
92 - };
94 -/* Global symbols for returning values from built-in functions */
95 -#define N_RETURN_GLOBALS 5
96 -enum retGlobalSyms {STRING_DIALOG_BUTTON, SEARCH_END, READ_STATUS,
97 - SHELL_CMD_STATUS, LIST_DIALOG_BUTTON};
98 -static const char *ReturnGlobalNames[N_RETURN_GLOBALS] = {"$string_dialog_button",
99 - "$search_end", "$read_status", "$shell_cmd_status",
100 - "$list_dialog_button"};
101 -static Symbol *ReturnGlobals[N_RETURN_GLOBALS];
102 +static const BuiltInSubrName MacroSubrs[] = {
103 + { "length", lengthMS },
104 + { "get_range", getRangeMS },
105 + { "t_print", tPrintMS },
106 + { "dialog", dialogMS },
107 + { "string_dialog", stringDialogMS },
108 + { "replace_range", replaceRangeMS },
109 + { "replace_selection", replaceSelectionMS },
110 + { "set_cursor_pos", setCursorPosMS },
111 + { "get_character", getCharacterMS },
112 + { "min", minMS },
113 + { "max", maxMS },
114 + { "search", searchMS },
115 + { "search_string", searchStringMS },
116 + { "substring", substringMS },
117 + { "replace_substring", replaceSubstringMS },
118 + { "read_file", readFileMS },
119 + { "write_file", writeFileMS },
120 + { "append_file", appendFileMS },
121 + { "beep", beepMS },
122 + { "get_selection", getSelectionMS },
123 + { "valid_number", validNumberMS },
124 + { "replace_in_string", replaceInStringMS },
125 + { "select", selectMS },
126 + { "select_rectangle", selectRectangleMS },
127 + { "focus_window", focusWindowMS },
128 + { "shell_command", shellCmdMS },
129 + { "string_to_clipboard", stringToClipboardMS },
130 + { "clipboard_to_string", clipboardToStringMS },
131 + { "toupper", toupperMS },
132 + { "tolower", tolowerMS },
133 + { "list_dialog", listDialogMS },
134 + { "getenv", getenvMS },
135 + { "string_compare", stringCompareMS },
136 + { "split", splitMS },
137 + { "calltip", calltipMS },
138 + { "kill_calltip", killCalltipMS },
139 +/* DISABLED for 5.4
140 + { "set_backlight_string", setBacklightStringMS },
142 + { "rangeset_create", rangesetCreateMS },
143 + { "rangeset_destroy", rangesetDestroyMS },
144 + { "rangeset_add", rangesetAddMS },
145 + { "rangeset_subtract", rangesetSubtractMS },
146 + { "rangeset_invert", rangesetInvertMS },
147 + { "rangeset_info", rangesetInfoMS },
148 + { "rangeset_range", rangesetRangeMS },
149 + { "rangeset_includes", rangesetIncludesPosMS },
150 + { "rangeset_set_color", rangesetSetColorMS },
151 + { "rangeset_set_name", rangesetSetNameMS },
152 + { "rangeset_set_mode", rangesetSetModeMS },
153 + { "rangeset_get_by_name", rangesetGetByNameMS },
154 + { "get_pattern_by_name", getPatternByNameMS },
155 + { "get_pattern_at_pos", getPatternAtPosMS },
156 + { "get_style_by_name", getStyleByNameMS },
157 + { "get_style_at_pos", getStyleAtPosMS },
158 + { "filename_dialog", filenameDialogMS },
159 + { NULL, NULL } /* sentinel */
162 +static const BuiltInSubrName SpecialVars[] = {
163 + { "$cursor", cursorMV },
164 + { "$line", lineMV },
165 + { "$column", columnMV },
166 + { "$file_name", fileNameMV },
167 + { "$file_path", filePathMV },
168 + { "$text_length", lengthMV },
169 + { "$selection_start", selectionStartMV },
170 + { "$selection_end", selectionEndMV },
171 + { "$selection_left", selectionLeftMV },
172 + { "$selection_right", selectionRightMV },
173 + { "$wrap_margin", wrapMarginMV },
174 + { "$tab_dist", tabDistMV },
175 + { "$em_tab_dist", emTabDistMV },
176 + { "$use_tabs", useTabsMV },
177 + { "$language_mode", languageModeMV },
178 + { "$modified", modifiedMV },
179 + { "$statistics_line", statisticsLineMV },
180 + { "$incremental_search_line", incSearchLineMV },
181 + { "$show_line_numbers", showLineNumbersMV },
182 + { "$auto_indent", autoIndentMV },
183 + { "$wrap_text", wrapTextMV },
184 + { "$highlight_syntax", highlightSyntaxMV },
185 + { "$make_backup_copy", makeBackupCopyMV },
186 + { "$incremental_backup", incBackupMV },
187 + { "$show_matching", showMatchingMV },
188 + { "$match_syntax_based", matchSyntaxBasedMV },
189 + { "$overtype_mode", overTypeModeMV },
190 + { "$read_only", readOnlyMV },
191 + { "$locked", lockedMV },
192 + { "$file_format", fileFormatMV },
193 + { "$font_name", fontNameMV },
194 + { "$font_name_italic", fontNameItalicMV },
195 + { "$font_name_bold", fontNameBoldMV },
196 + { "$font_name_bold_italic", fontNameBoldItalicMV },
197 + { "$sub_sep", subscriptSepMV },
198 + { "$min_font_width", minFontWidthMV },
199 + { "$max_font_width", maxFontWidthMV },
200 + { "$top_line", topLineMV },
201 + { "$n_display_lines", numDisplayLinesMV },
202 + { "$display_width", displayWidthMV },
203 + { "$active_pane", activePaneMV },
204 + { "$n_panes", nPanesMV },
205 + { "$empty_array", emptyArrayMV },
206 + { "$server_name", serverNameMV },
207 + { "$calltip_ID", calltipIDMV },
208 +/* DISABLED for 5.4
209 + { "$backlight_string", backlightStringMV },
211 + { "$rangeset_list", rangesetListMV },
212 + { "$VERSION", versionMV },
213 + { NULL, NULL } /* sentinel */
216 +/* Global symbols for "returning" secondary values from built-in functions */
217 +static int STRING_DIALOG_BUTTON, SEARCH_END, READ_STATUS,
218 + SHELL_CMD_STATUS, LIST_DIALOG_BUTTON;
220 +static ReturnGlobalName ReturnGlobalNames[] = {
221 + /* name, pIndex: *pIndex will be set to the symbol's
222 + index in ReturnGlobals */
223 + { "$string_dialog_button", &STRING_DIALOG_BUTTON },
224 + { "$search_end", &SEARCH_END },
225 + { "$read_status", &READ_STATUS },
226 + { "$shell_cmd_status", &SHELL_CMD_STATUS },
227 + { "$list_dialog_button", &LIST_DIALOG_BUTTON },
228 + { NULL, NULL } /* sentinel */
231 +static Symbol **ReturnGlobals = NULL;
233 /* List of actions not useful when learning a macro sequence (also see below) */
234 static char* IgnoredActions[] = {"focusIn", "focusOut"};
235 @@ -539,30 +589,66 @@ static char ReplaceChars[] = "\\\"ntbrfa
236 static char EscapeChars[] = "\\\"\n\t\b\r\f\a\v";
239 +** Installs a set of built-in macro subroutines as symbols of given type.
241 +void RegisterMacroSubroutineSet(const BuiltInSubrName *set, enum symTypes type)
243 + static DataValue subrPtr = {NO_TAG, {0}};
244 + unsigned i;
246 + for (i = 0; set[i].name; i++) {
247 + subrPtr.val.subr = set[i].macroSubr;
248 + InstallSymbol(set[i].name, type, subrPtr);
253 +** Installs a set of built-in macro secondary return values as symbols of
254 +** given type.
256 +void RegisterGlobalReturnValuesSet(ReturnGlobalName *set, enum symTypes type)
258 + int n, i, oldn;
259 + Symbol **newRetGlobals;
260 + static DataValue noValue = {NO_TAG, {0}};
262 + n = oldn = 0;
264 + /* count the entries in the new table */
265 + while (set[n].name) n++;
266 + /* and existing entries if any */
267 + while (ReturnGlobals && ReturnGlobals[oldn]) oldn++;
269 + /* reallocate symbol reference table with space for a sentinel at the end */
270 + newRetGlobals = realloc(ReturnGlobals, (oldn + n + 1) * sizeof(Symbol *));
271 + if (!newRetGlobals) {
272 + fprintf(stderr, "nedit: failed to build macro return globals table\n");
273 + exit(EXIT_FAILURE);
276 + ReturnGlobals = newRetGlobals;
277 + /* now assign the (new) symbols at the newly allocated end of the table
278 + and store the indices in set */
279 + for (i = 0, n = oldn; set[i].name; i++, n++) {
280 + ReturnGlobals[n] = InstallSymbol(set[i].name, type, noValue);
281 + *set[i].pIndex = n;
283 + /* add the sentinel */
284 + ReturnGlobals[n] = NULL;
288 ** Install built-in macro subroutines and special variables for accessing
289 ** editor information
291 void RegisterMacroSubroutines(void)
293 - static DataValue subrPtr = {NO_TAG, {0}}, noValue = {NO_TAG, {0}};
294 - unsigned i;
296 /* Install symbols for built-in routines and variables, with pointers
297 to the appropriate c routines to do the work */
298 - for (i=0; i<N_MACRO_SUBRS; i++) {
299 - subrPtr.val.subr = MacroSubrs[i];
300 - InstallSymbol(MacroSubrNames[i], C_FUNCTION_SYM, subrPtr);
302 - for (i=0; i<N_SPECIAL_VARS; i++) {
303 - subrPtr.val.subr = SpecialVars[i];
304 - InstallSymbol(SpecialVarNames[i], PROC_VALUE_SYM, subrPtr);
307 - /* Define global variables used for return values, remember their
308 - locations so they can be set without a LookupSymbol call */
309 - for (i=0; i<N_RETURN_GLOBALS; i++)
310 - ReturnGlobals[i] = InstallSymbol(ReturnGlobalNames[i], GLOBAL_SYM,
311 - noValue);
312 + RegisterMacroSubroutineSet(MacroSubrs, C_FUNCTION_SYM);
313 + RegisterMacroSubroutineSet(SpecialVars, PROC_VALUE_SYM);
314 + /* Define global variables used for return values */
315 + RegisterGlobalReturnValuesSet(ReturnGlobalNames, GLOBAL_SYM);
318 #define MAX_LEARN_MSG_LEN ((2 * MAX_ACCEL_LEN) + 60)
319 diff --quilt old/source/macro.h new/source/macro.h
320 --- old/source/macro.h
321 +++ new/source/macro.h
322 @@ -28,13 +28,31 @@
323 #ifndef NEDIT_MACRO_H_INCLUDED
324 #define NEDIT_MACRO_H_INCLUDED
326 -#include "nedit.h"
327 +#include "interpret.h"
329 #include <X11/Intrinsic.h>
331 #define REPEAT_TO_END -1
332 #define REPEAT_IN_SEL -2
335 +typedef struct BuiltInSubrNameTag {
336 + const char *name; /* its macro identifier */
337 + BuiltInSubr macroSubr; /* pointer to the function to call */
338 +} BuiltInSubrName;
340 +/* Structure used to build an initialized static array, each entry describing a
341 +** macro built-in global result variable's symbol. The array should provide the
342 +** macro identifier eg "$search_end", and a value for pIndex, pointing to a
343 +** static integer variable to store its index. As the macro identifiers are
344 +** registered (creating symbols), by scanning the static array, each one is
345 +** given a unique index value to be able to retrieve the symbol using a direct
346 +** array lookup.
348 +typedef struct ReturnGlobalNameTag {
349 + const char *name; /* macro identifier eg "$search_end" */
350 + int *pIndex; /* a place to store its index */
351 +} ReturnGlobalName;
353 void RegisterMacroSubroutines(void);
354 void AddLastCommandActionHook(XtAppContext context);
355 void BeginLearn(WindowInfo *window);