include wmdesktop.h in misc.h
[nedit-bw.git] / macro_hooks.patch
blob9815da8b4449ab64f93d7a4b34f8e37e7a14fda9
1 From: Thorsten Haude <yoo@vranx.de>
2 Subject: macro hooks
4 The following hooks are present:
6 * pre_open_hook
7 * post_open_hook
8 * pre_save_hook
9 * post_save_hook
10 * cursor_moved_hook
11 * modified_hook
12 * focus_hook
13 * losing_focus_hook
14 * language_mode_hook
16 ---
18 doc/help.etx | 46 ++++++++++++++++++++++++++++++
19 source/Makefile.dependencies | 6 +--
20 source/file.c | 18 +++++++++++
21 source/highlightData.c | 1
22 source/macro.c | 65 +++++++++++++++++++++++++++++++++++++++++--
23 source/macro.h | 3 +
24 source/menu.c | 28 +++++++++++++++++-
25 source/nedit.c | 24 +++++----------
26 source/nedit.h | 1
27 doc/help.etx | 50 ++++++++++++++++
28 source/Makefile.common | 1
29 source/Makefile.dependencies | 2
30 source/file.c | 19 ++++++
31 source/highlightData.c | 1
32 source/hooks.c | 133 +++++++++++++++++++++++++++++++++++++++++++
33 source/hooks.h | 15 ++++
34 source/macro.c | 65 ++++++++++++++++++++-
35 source/macro.h | 3
36 source/menu.c | 9 +-
37 source/nedit.c | 24 ++-----
38 source/nedit.h | 1
39 source/preferences.c | 3
40 source/selection.c | 11 ++-
41 source/server.c | 56 ++++++++----------
42 source/window.c | 21 ++++++
43 16 files changed, 358 insertions(+), 56 deletions(-)
45 diff --quilt old/doc/help.etx new/doc/help.etx
46 --- old/doc/help.etx
47 +++ new/doc/help.etx
48 @@ -3748,6 +3748,55 @@ Action Routines
49 To be attached to a key-press event, inserts the character
50 equivalent of the key pressed.
53 +Hooks
54 +-----
56 + Hooks are macro routines which are called at specific points in NEdit's
57 + execution. You can use hooks to tie in user-defined functionality at these
58 + points.
60 + No hooks are provided. To use a hook, simply define a macro function with
61 + the name of the hook. The next time the hook will catch, the macro function
62 + is called.
64 + You don't have to provide any hook. If a certain hook does not exist, it is
65 + simply skipped.
67 +**pre_open_hook(~filename~)**
68 + Called before NEdit opens a file using a certain name. The parameter is
69 + the filename NEdit intends to open.
71 + Return a string to use instead of the original filename. Return 0 to tell
72 + NEdit to use the original filename.
74 +**post_open_hook()**
75 + Called when an existing file is opened.
77 +**pre_save_hook()**
78 + Called before a file will be save saved.
80 + Return a string to use this as the file name.
82 +**post_save_hook()**
83 + Called after an file was saved.
85 +**language_mode_hook(~previous_mode~)**
86 + Called after a new language mode was applied with the previous mode as the
87 + first argument.
89 +**cursor_moved_hook()**
90 + Called when the was cursor moved.
92 +**modified_hook()**
93 + Called when the text buffer changed.
95 +**focus_hook()**
96 + Called when a document gets the input focus.
98 +**losing_focus_hook()**
99 + Called before a document lost the input focus.
101 ----------------------------------------------------------------------
103 Customizing
104 @@ -6036,6 +6085,7 @@ Problems/Defects
105 .. Menu: Rangesets # rangeset
106 .. Menu: Highlighting Information # hiliteInfo
107 .. Menu: Action Routines # actions
108 +.. Menu: H_o_oks # hooks
110 .. Menu: Customizing # customizing
111 .. Menu: Customizing NEdit # customize
112 diff --quilt old/source/Makefile.dependencies new/source/Makefile.dependencies
113 --- old/source/Makefile.dependencies
114 +++ new/source/Makefile.dependencies
115 @@ -85,3 +85,5 @@ windowTitle.o: windowTitle.c windowTitle
116 ../util/DialogF.h ../util/utils.h ../util/fileUtils.h \
117 ../util/clearcase.h
118 parse.c: parse.h textBuf.h nedit.h rbTree.h interpret.h ops.h
119 +server.o file.o preferences.o selection.o window.o: hooks.h
120 +hooks.o: hooks.c hooks.h macro.h
121 diff --quilt old/source/file.c new/source/file.c
122 --- old/source/file.c
123 +++ new/source/file.c
124 @@ -41,6 +41,7 @@ static const char CVSID[] = "$Id: file.c
125 #include "tags.h"
126 #include "server.h"
127 #include "interpret.h"
128 +#include "hooks.h"
129 #include "../util/misc.h"
130 #include "../util/DialogF.h"
131 #include "../util/fileUtils.h"
132 @@ -272,6 +273,8 @@ WindowInfo *EditExistingFile(WindowInfo
133 AddRelTagsFile(GetPrefTagFile(), path, TAG);
134 AddToPrevOpenMenu(fullname);
136 + PostOpenHook(window);
138 return window;
141 @@ -934,6 +937,19 @@ static int doSave(WindowInfo *window)
142 int fileLen, result;
143 int ret;
145 + /* call pre_save_hook, and set result as path/name */
146 + fileString = PreSaveHook(window);
147 + if (fileString &&
148 + ParseFilename(fileString, window->filename, window->path)) {
149 + result = DialogF(DF_ERR, window->shell, 2, "Invalid Filename",
150 + "The filename provided by the post_save_hook\n"
151 + "('%s')\n"
152 + "is not a valid filename.",
153 + "Cancel", "Keep Current", fileString);
154 + if (result == 1)
155 + return FALSE;
158 /* Get the full name of the file */
159 strcpy(fullname, window->path);
160 strcat(fullname, window->filename);
161 @@ -1085,6 +1101,9 @@ static int doSave(WindowInfo *window)
162 window->inode = 0;
165 + /* call "post_save_hook" */
166 + PostSaveHook(window);
168 return TRUE;
171 diff --quilt old/source/highlightData.c new/source/highlightData.c
172 --- old/source/highlightData.c
173 +++ new/source/highlightData.c
174 @@ -554,6 +554,7 @@ static char *DefaultPatternSets[] = {
175 Built-in Subrs:\"<(?:args|append_file|beep|call|calltip|clipboard_to_string|dialog|filename_dialog|focus_window|get_character|get_pattern_(by_name|at_pos)|get_range|get_selection|get_style_(by_name|at_pos)|getenv|highlight_calltip_line|kill_calltip|length|list_dialog|max|min|n_args|rangeset_(?:add|create|destroy|get_by_name|includes|info|invert|range|set_color|set_mode|set_name|subtract)|read_file|replace_in_string|replace_range|replace_selection|replace_substring|search|search_string|select|select_rectangle|set_cursor_pos|shell_command|split|string_compare|string_dialog|string_to_clipboard|substring|t_print|tolower|toupper|valid_number|write_file)(?=\\s*\\()\":::Subroutine::\n\
176 Menu Actions:\"<(?:new(?:_tab|_opposite)?|open|open-dialog|open_dialog|open-selected|open_selected|close|save|save-as|save_as|save-as-dialog|save_as_dialog|revert-to-saved|revert_to_saved|revert_to_saved_dialog|include-file|include_file|include-file-dialog|include_file_dialog|load-macro-file|load_macro_file|load-macro-file-dialog|load_macro_file_dialog|load-tags-file|load_tags_file|load-tags-file-dialog|load_tags_file_dialog|unload_tags_file|load_tips_file|load_tips_file_dialog|unload_tips_file|print|print-selection|print_selection|exit|undo|redo|delete|select-all|select_all|shift-left|shift_left|shift-left-by-tab|shift_left_by_tab|shift-right|shift_right|shift-right-by-tab|shift_right_by_tab|find|find-dialog|find_dialog|find-again|find_again|find-selection|find_selection|find_incremental|start_incremental_find|replace|replace-dialog|replace_dialog|replace-all|replace_all|replace-in-selection|replace_in_selection|replace-again|replace_again|replace_find|replace_find_same|replace_find_again|goto-line-number|goto_line_number|goto-line-number-dialog|goto_line_number_dialog|goto-selected|goto_selected|mark|mark-dialog|mark_dialog|goto-mark|goto_mark|goto-mark-dialog|goto_mark_dialog|match|select_to_matching|goto_matching|find-definition|find_definition|show_tip|split-pane|split_pane|close-pane|close_pane|detach_document(?:_dialog)?|move_document_dialog|(?:next|previous|last)_document|uppercase|lowercase|fill-paragraph|fill_paragraph|control-code-dialog|control_code_dialog|filter-selection-dialog|filter_selection_dialog|filter-selection|filter_selection|execute-command|execute_command|execute-command-dialog|execute_command_dialog|execute-command-line|execute_command_line|shell-menu-command|shell_menu_command|macro-menu-command|macro_menu_command|bg_menu_command|post_window_bg_menu|post_tab_context_menu|beginning-of-selection|beginning_of_selection|end-of-selection|end_of_selection|repeat_macro|repeat_dialog|raise_window|focus_pane|set_statistics_line|set_incremental_search_line|set_show_line_numbers|set_auto_indent|set_wrap_text|set_wrap_margin|set_highlight_syntax|set_make_backup_copy|set_incremental_backup|set_show_matching|set_match_syntax_based|set_overtype_mode|set_locked|set_tab_dist|set_em_tab_dist|set_use_tabs|set_fonts|set_language_mode)(?=\\s*\\()\":::Subroutine::\n\
177 Text Actions:\"<(?:self-insert|self_insert|grab-focus|grab_focus|extend-adjust|extend_adjust|extend-start|extend_start|extend-end|extend_end|secondary-adjust|secondary_adjust|secondary-or-drag-adjust|secondary_or_drag_adjust|secondary-start|secondary_start|secondary-or-drag-start|secondary_or_drag_start|process-bdrag|process_bdrag|move-destination|move_destination|move-to|move_to|move-to-or-end-drag|move_to_or_end_drag|end_drag|copy-to|copy_to|copy-to-or-end-drag|copy_to_or_end_drag|exchange|process-cancel|process_cancel|paste-clipboard|paste_clipboard|copy-clipboard|copy_clipboard|cut-clipboard|cut_clipboard|copy-primary|copy_primary|cut-primary|cut_primary|newline|newline-and-indent|newline_and_indent|newline-no-indent|newline_no_indent|delete-selection|delete_selection|delete-previous-character|delete_previous_character|delete-next-character|delete_next_character|delete-previous-word|delete_previous_word|delete-next-word|delete_next_word|delete-to-start-of-line|delete_to_start_of_line|delete-to-end-of-line|delete_to_end_of_line|forward-character|forward_character|backward-character|backward_character|key-select|key_select|process-up|process_up|process-down|process_down|process-shift-up|process_shift_up|process-shift-down|process_shift_down|process-home|process_home|forward-word|forward_word|backward-word|backward_word|forward-paragraph|forward_paragraph|backward-paragraph|backward_paragraph|beginning-of-line|beginning_of_line|end-of-line|end_of_line|beginning-of-file|beginning_of_file|end-of-file|end_of_file|next-page|next_page|previous-page|previous_page|page-left|page_left|page-right|page_right|toggle-overstrike|toggle_overstrike|scroll-up|scroll_up|scroll-down|scroll_down|scroll_left|scroll_right|scroll-to-line|scroll_to_line|select-all|select_all|deselect-all|deselect_all|focusIn|focusOut|process-return|process_return|process-tab|process_tab|insert-string|insert_string|mouse_pan)(?=\\s*\\()\":::Subroutine::\n\
178 + Macro Hooks:\"<(?:(?:pre|post)_(?:open|save)|cursor_moved|modified|(?:losing_)?focus|language_mode)_hook(?=\\s*\\()\":::Subroutine1::\n\
179 Keyword:\"<(?:break|continue|define|delete|do|else|for|if|in|return|while)>\":::Keyword::\n\
180 Braces:\"[{}\\[\\]()<>,.:;~!&|^%*/?=+-]\":::Keyword::\n\
181 Global Variable:\"\\$[A-Za-z0-9_]+\":::Identifier1::\n\
182 diff --quilt old/source/macro.c new/source/macro.c
183 --- old/source/macro.c
184 +++ new/source/macro.c
185 @@ -902,8 +902,10 @@ void SafeGC(void)
186 WindowInfo *win;
188 for (win=WindowList; win!=NULL; win=win->next)
189 - if (win->macroCmdData != NULL || InSmartIndentMacros(win))
190 - return;
191 + if (win->macroCmdData != NULL
192 + || InSmartIndentMacros(win)
193 + || win->inMacroHook)
194 + return;
195 GarbageCollectStrings();
198 @@ -5704,3 +5706,62 @@ static int readStringArg(DataValue dv, c
199 *errMsg = "%s called with unknown object";
200 return False;
204 +** call a macro function, if it exists, with the given arguments and store
205 +** the return value in resultDV.
207 +** Currently, only non-preemtable macros are supported.
209 +Boolean MacroApplyHook(WindowInfo *document, const char *hook, int argc,
210 + DataValue *argv, DataValue *resultDV)
212 + Symbol *hookSymbol;
213 + Boolean succ = False;
215 + hookSymbol = LookupSymbol(hook);
216 + if (document &&
217 + NULL != hookSymbol &&
218 + MACRO_FUNCTION_SYM == hookSymbol->type) {
219 + Program *hookProg = hookSymbol->value.val.prog;
220 + RestartData *restartData;
221 + DataValue dummyResultDV;
222 + DataValue *resultDVPtr = &dummyResultDV;
223 + int status;
224 + char *errMsg;
226 + /* ExecuteMacro() can't be called with a NULL resultDV, therefore the
227 + dummyResultDV */
228 + if (resultDV) {
229 + resultDVPtr = resultDV;
232 + /* prevent calling the GC */
233 + document->inMacroHook++;
235 + status = ExecuteMacro(document, hookProg, argc, argv,
236 + resultDVPtr, &restartData, &errMsg);
238 + while (MACRO_TIME_LIMIT == status) {
239 + status = ContinueMacro(restartData, resultDVPtr, &errMsg);
242 + document->inMacroHook--;
244 + /* call the GC only if the caller of this function is not interested
245 + in any result, else it may happen, that strings and arrays are swept
246 + away by the GC. */
247 + if (NULL == resultDV) {
248 + SafeGC();
251 + if (MACRO_PREEMPT == status || MACRO_ERROR == status) {
252 + fprintf(stderr, "nedit: \"%s\" error: %s\n", hook, (MACRO_ERROR == status) ? errMsg : "No dialogs");
253 + } else {
254 + /* Macro is done here without errors */
255 + succ = True;
259 + return succ;
261 diff --quilt old/source/macro.h new/source/macro.h
262 --- old/source/macro.h
263 +++ new/source/macro.h
264 @@ -73,5 +73,8 @@ int CheckMacroString(Widget dialogParent
265 char *GetReplayMacro(void);
266 void ReadMacroInitFile(WindowInfo *window);
267 void ReturnShellCommandOutput(WindowInfo *window, const char *outText, int status);
268 +struct DataValueTag;
269 +Boolean MacroApplyHook(WindowInfo *document, const char *hook, int argc,
270 + struct DataValueTag *argv, struct DataValueTag *resultDV);
272 #endif /* NEDIT_MACRO_H_INCLUDED */
273 diff --quilt old/source/menu.c new/source/menu.c
274 --- old/source/menu.c
275 +++ new/source/menu.c
276 @@ -53,6 +53,7 @@ static const char CVSID[] = "$Id: menu.c
277 #include "smartIndent.h"
278 #include "windowTitle.h"
279 #include "regularExp.h"
280 +#include "hooks.h"
281 #include "../util/getfiles.h"
282 #include "../util/DialogF.h"
283 #include "../util/misc.h"
284 @@ -2850,18 +2851,20 @@ static void openAP(Widget w, XEvent *eve
285 WindowInfo *window = WidgetToWindow(w);
286 char filename[MAXPATHLEN], pathname[MAXPATHLEN];
288 - if (*nArgs == 0) {
289 + if (*nArgs != 1) {
290 fprintf(stderr, "nedit: open action requires file argument\n");
291 return;
293 - if (0 != ParseFilename(args[0], filename, pathname)
294 - || strlen(filename) + strlen(pathname) > MAXPATHLEN - 1) {
296 + if (ParseFilename(PreOpenHook(window, args[0]), filename, pathname)) {
297 fprintf(stderr, "nedit: invalid file name for open action: %s\n",
298 args[0]);
299 return;
302 EditExistingFile(window, filename, pathname, 0, NULL, False,
303 NULL, GetPrefOpenInTab(), False);
305 CheckCloseDim();
308 diff --quilt old/source/nedit.c new/source/nedit.c
309 --- old/source/nedit.c
310 +++ new/source/nedit.c
311 @@ -389,7 +389,7 @@ static const char cmdLineHelp[] =
312 int main(int argc, char **argv)
314 int i, lineNum, nRead, fileSpecified = FALSE, editFlags = CREATE;
315 - int gotoLine = False, macroFileRead = False, opts = True;
316 + int gotoLine = False, opts = True;
317 int iconic=False, tabbed = -1, group = 0, isTabbed;
318 char *toDoCommand = NULL, *geometry = NULL, *langMode = NULL;
319 char filename[MAXPATHLEN], pathname[MAXPATHLEN];
320 @@ -578,6 +578,10 @@ int main(int argc, char **argv)
321 IsServer = True;
324 + EditNewFile(NULL, geometry, iconic, langMode, NULL);
325 + ReadMacroInitFile(WindowList);
326 + CheckCloseDim();
328 /* Process any command line arguments (-tags, -do, -read, -create,
329 +<line_number>, -line, -server, and files to edit) not already
330 processed by RestoreNEditPrefs. */
331 @@ -693,10 +697,6 @@ int main(int argc, char **argv)
332 RaiseDocument(lastFile);
335 - if (!macroFileRead) {
336 - ReadMacroInitFile(WindowList);
337 - macroFileRead = True;
339 if (gotoLine)
340 SelectNumberedLine(window, lineNum);
341 if (toDoCommand != NULL) {
342 @@ -750,10 +750,6 @@ int main(int argc, char **argv)
343 RaiseDocument(lastFile);
346 - if (!macroFileRead) {
347 - ReadMacroInitFile(WindowList);
348 - macroFileRead = True;
350 if (gotoLine)
351 SelectNumberedLine(window, lineNum);
352 if (toDoCommand != NULL) {
353 @@ -786,13 +782,9 @@ int main(int argc, char **argv)
355 CheckCloseDim();
357 - /* If no file to edit was specified, open a window to edit "Untitled" */
358 - if (!fileSpecified) {
359 - EditNewFile(NULL, geometry, iconic, langMode, NULL);
360 - ReadMacroInitFile(WindowList);
361 - CheckCloseDim();
362 - if (toDoCommand != NULL)
363 - DoMacro(WindowList, toDoCommand, "-do macro");
364 + /* If no file to edit was specified, do at least the -do macro */
365 + if (!fileSpecified && toDoCommand != NULL) {
366 + DoMacro(WindowList, toDoCommand, "-do macro");
369 /* Begin remembering last command invoked for "Repeat" menu item */
370 diff --quilt old/source/nedit.h new/source/nedit.h
371 --- old/source/nedit.h
372 +++ new/source/nedit.h
373 @@ -566,6 +566,7 @@ typedef struct _WindowInfo {
374 UserBGMenuCache userBGMenuCache; /* shell & macro menu are shared over all
375 "tabbed" documents, while each document
376 has its own background menu. */
377 + int inMacroHook; /* to protect GC in MacroApplyHook() */
378 } WindowInfo;
380 extern WindowInfo *WindowList;
381 diff --quilt old/source/selection.c new/source/selection.c
382 --- old/source/selection.c
383 +++ new/source/selection.c
384 @@ -38,6 +38,7 @@ static const char CVSID[] = "$Id: select
385 #include "menu.h"
386 #include "preferences.h"
387 #include "server.h"
388 +#include "hooks.h"
389 #include "../util/DialogF.h"
390 #include "../util/fileUtils.h"
392 @@ -321,7 +322,7 @@ static void fileCB(Widget widget, Window
393 guranteed to be available, but in practice is there and does work. */
394 #if defined(DONT_HAVE_GLOB) || defined(VMS)
395 /* Open the file */
396 - if (ParseFilename(nameText, filename, pathname) != 0) {
397 + if (ParseFilename(PreOpenHook(window, nameText), filename, pathname) != 0) {
398 XBell(TheDisplay, 0);
399 return;
401 @@ -331,14 +332,15 @@ static void fileCB(Widget widget, Window
402 { char **nameList = NULL;
403 int i, nFiles = 0, maxFiles = 30;
405 - if (ParseFilename(nameText, filename, pathname) != 0) {
406 + if (ParseFilename(PreOpenHook(window, nameText), filename, pathname)) {
407 XBell(TheDisplay, 0);
408 return;
410 _XmOSGetDirEntries(pathname, filename, XmFILE_ANY_TYPE, False, True,
411 &nameList, &nFiles, &maxFiles);
412 for (i=0; i<nFiles; i++) {
413 - if (ParseFilename(nameList[i], filename, pathname) != 0) {
414 + if (ParseFilename(PreOpenHook(window, nameList[i]),
415 + filename, pathname)) {
416 XBell(TheDisplay, 0);
418 else {
419 @@ -357,7 +359,8 @@ static void fileCB(Widget widget, Window
421 glob(nameText, GLOB_NOCHECK, NULL, &globbuf);
422 for (i=0; i<(int)globbuf.gl_pathc; i++) {
423 - if (ParseFilename(globbuf.gl_pathv[i], filename, pathname) != 0)
424 + if (ParseFilename(PreOpenHook(window, globbuf.gl_pathv[i]),
425 + filename, pathname))
426 XBell(TheDisplay, 0);
427 else
428 EditExistingFile(GetPrefOpenInTab()? window : NULL,
429 diff --quilt old/source/window.c new/source/window.c
430 --- old/source/window.c
431 +++ new/source/window.c
432 @@ -55,6 +55,7 @@ static const char CVSID[] = "$Id: window
433 #include "windowTitle.h"
434 #include "interpret.h"
435 #include "rangeset.h"
436 +#include "hooks.h"
437 #include "../util/clearcase.h"
438 #include "../util/misc.h"
439 #include "../util/fileUtils.h"
440 @@ -169,6 +170,7 @@ static void showStatsForm(WindowInfo *wi
441 static void addToWindowList(WindowInfo *window);
442 static void removeFromWindowList(WindowInfo *window);
443 static void focusCB(Widget w, WindowInfo *window, XtPointer callData);
444 +static void losingFocusCB(Widget w, WindowInfo *window, XtPointer callData);
445 static void modifiedCB(int pos, int nInserted, int nDeleted, int nRestyled,
446 const char *deletedText, void *cbArg);
447 static void movedCB(Widget w, WindowInfo *window, XtPointer callData);
448 @@ -325,6 +327,7 @@ WindowInfo *CreateWindow(const char *nam
449 window->tab = NULL;
450 window->device = 0;
451 window->inode = 0;
452 + window->inMacroHook = 0;
454 /* If window geometry was specified, split it apart into a window position
455 component and a window size component. Create a new geometry string
456 @@ -1029,6 +1032,10 @@ void CloseWindow(WindowInfo *window)
457 return;
460 + if (window->inMacroHook) {
461 + fprintf(stderr, "nedit: warning closing window while in MacroHook\n");
464 /* Free syntax highlighting patterns, if any. w/o redisplaying */
465 FreeHighlightingData(window);
467 @@ -2321,6 +2328,8 @@ static Widget createTextArea(Widget pare
469 /* add focus, drag, cursor tracking, and smart indent callbacks */
470 XtAddCallback(text, textNfocusCallback, (XtCallbackProc)focusCB, window);
471 + XtAddCallback(text, textNlosingFocusCallback,
472 + (XtCallbackProc)losingFocusCB, window);
473 XtAddCallback(text, textNcursorMovementCallback, (XtCallbackProc)movedCB,
474 window);
475 XtAddCallback(text, textNdragStartCallback, (XtCallbackProc)dragStartCB,
476 @@ -2373,6 +2382,8 @@ static void movedCB(Widget w, WindowInfo
477 /* Start blinking the caret again. */
478 ResetCursorBlink(textWidget, False);
481 + CursorMovedHook(window);
484 static void modifiedCB(int pos, int nInserted, int nDeleted, int nRestyled,
485 @@ -2444,6 +2455,8 @@ static void modifiedCB(int pos, int nIns
487 /* Check if external changes have been made to file and warn user */
488 CheckForChangesToFile(window);
490 + ModifiedHook(window);
493 static void focusCB(Widget w, WindowInfo *window, XtPointer callData)
494 @@ -2459,6 +2472,13 @@ static void focusCB(Widget w, WindowInfo
496 /* Check for changes to read-only status and/or file modifications */
497 CheckForChangesToFile(window);
499 + FocusHook(window);
502 +static void losingFocusCB(Widget w, WindowInfo *window, XtPointer callData)
504 + LosingFocusHook(window);
507 static void dragStartCB(Widget w, WindowInfo *window, XtPointer callData)
508 @@ -3466,6 +3486,7 @@ WindowInfo* CreateDocument(WindowInfo* s
509 window->bgMenuRedoItem = NULL;
510 window->device = 0;
511 window->inode = 0;
512 + window->inMacroHook = 0;
514 if (window->fontList == NULL)
515 XtVaGetValues(shellWindow->statsLine, XmNfontList,
516 diff --quilt /dev/null new/source/hooks.c
517 --- /dev/null
518 +++ new/source/hooks.c
519 @@ -0,0 +1,133 @@
520 +#ifdef HAVE_CONFIG_H
521 +#include "../config.h"
522 +#endif
524 +#include "preferences.h"
525 +#include "interpret.h"
526 +#include "macro.h"
528 +#include <errno.h>
529 +#include <limits.h>
530 +#include <stdio.h>
531 +#include <stdlib.h>
532 +#include <string.h>
533 +#include <unistd.h>
535 +#ifdef VMS
536 +#include "../util/VMSparam.h"
537 +#include <types.h>
538 +#include <stat.h>
539 +#include <unixio.h>
540 +#else
541 +#include <sys/types.h>
542 +#include <sys/stat.h>
543 +#include <utime.h>
544 +#ifndef __MVS__
545 +#include <sys/param.h>
546 +#endif
547 +#include <fcntl.h>
548 +#endif /*VMS*/
549 +#include <utime.h>
551 +#ifdef HAVE_DEBUG_H
552 +#include "../debug.h"
553 +#endif
555 +const char *
556 +PreOpenHook(WindowInfo *window, const char *fullname)
558 + DataValue fileNameArg;
559 + DataValue resultDV = {NO_TAG, {0}};
560 + Boolean succ = False;
562 + if (!window) {
563 + /* search a window which does not execute a macro currently */
564 + window = WindowList;
565 + while (window != NULL && window->macroCmdData != NULL) {
566 + window = window->next;
569 + if (!window) {
570 + XBell(TheDisplay, 0);
571 + fprintf(stderr, "nedit: warning: can't call pre_open_hook for "
572 + "file \"%s\"", fullname);
573 + return fullname;
576 + fileNameArg.tag = STRING_TAG;
577 + AllocNStringCpy(&fileNameArg.val.str, fullname);
578 + succ = MacroApplyHook(window, "pre_open_hook", 1, &fileNameArg, &resultDV);
580 + if (succ && resultDV.tag == STRING_TAG) {
581 + return resultDV.val.str.rep;
582 + } else {
583 + return fullname;
587 +void
588 +PostOpenHook(WindowInfo *window)
590 + MacroApplyHook(window, "post_open_hook", 0, NULL, NULL);
593 +const char *
594 +PreSaveHook(WindowInfo *window)
596 + DataValue resultDV = {NO_TAG,{0}};
597 + Boolean succ;
599 + /* call the "pre_save_hook", if the macro returns a string, interpret this
600 + as the new filename */
601 + succ = MacroApplyHook(window, "pre_save_hook", 0, NULL, &resultDV);
602 + if (succ && resultDV.tag == STRING_TAG) {
603 + return resultDV.val.str.rep;
606 + return NULL;
609 +void
610 +PostSaveHook(WindowInfo *window)
612 + MacroApplyHook(window, "post_save_hook", 0, NULL, NULL);
615 +void
616 +LanguageModeHook(WindowInfo *window, int oldMode)
618 + DataValue oldModeArg;
619 + const char *oldModeName = LanguageModeName(oldMode);
621 + if (oldModeName == NULL)
622 + oldModeName = "Plain";
624 + oldModeArg.tag = STRING_TAG;
625 + AllocNStringCpy(&oldModeArg.val.str, oldModeName);
626 + MacroApplyHook(window, "language_mode_hook", 1, &oldModeArg, NULL);
629 +void
630 +CursorMovedHook(WindowInfo *window)
632 + MacroApplyHook(window, "cursor_moved_hook", 0, NULL, NULL);
635 +void
636 +ModifiedHook(WindowInfo *window)
638 + MacroApplyHook(window, "modified_hook", 0, NULL, NULL);
641 +void
642 +FocusHook(WindowInfo *window)
644 + MacroApplyHook(window, "focus_hook", 0, NULL, NULL);
647 +void
648 +LosingFocusHook(WindowInfo *window)
650 + MacroApplyHook(window, "losing_focusHook", 0, NULL, NULL);
653 diff --quilt /dev/null new/source/hooks.h
654 --- /dev/null
655 +++ new/source/hooks.h
656 @@ -0,0 +1,15 @@
657 +#ifndef NEDIT_HOOKS_H_INCLUDED
658 +#define NEDIT_HOOKS_H_INCLUDED
660 +const char *PreOpenHook(WindowInfo *window, const char *fullname);
661 +void PostOpenHook(WindowInfo *window);
662 +const char *PreSaveHook(WindowInfo *window);
663 +void PostSaveHook(WindowInfo *window);
664 +void LanguageModeHook(WindowInfo *window, int oldMode);
666 +void CursorMovedHook(WindowInfo *window);
667 +void ModifiedHook(WindowInfo *window);
668 +void FocusHook(WindowInfo *window);
669 +void LosingFocusHook(WindowInfo *window);
671 +#endif /* NEDIT_HOOKS_H_INCLUDED */
672 diff --quilt old/source/server.c new/source/server.c
673 --- old/source/server.c
674 +++ new/source/server.c
675 @@ -41,6 +41,7 @@ static const char CVSID[] = "$Id: server
676 #include "menu.h"
677 #include "preferences.h"
678 #include "server_common.h"
679 +#include "hooks.h"
680 #include "../util/fileUtils.h"
681 #include "../util/utils.h"
682 #include "../util/misc.h"
683 @@ -76,7 +77,7 @@ static const char CVSID[] = "$Id: server
684 static void processServerCommand(void);
685 static void cleanUpServerCommunication(void);
686 static void processServerCommandString(char *string);
687 -static void getFileClosedProperty(WindowInfo *window);
688 +static void getFileClosedProperty(WindowInfo *window, const char *fulname);
689 static int isLocatedOnDesktop(WindowInfo *window, long currentDesktop);
690 static WindowInfo *findWindowOnDesktop(int tabbed, long currentDesktop);
692 @@ -213,62 +214,53 @@ Boolean ServerDispatchEvent(XEvent *even
695 /* Try to find existing 'FileOpen' property atom for path. */
696 -static Atom findFileOpenProperty(const char* filename,
697 - const char* pathname) {
698 - char path[MAXPATHLEN];
699 +static Atom findFileOpenProperty(const char *fullname)
701 Atom atom;
703 if (!IsServer) return(None);
705 - strcpy(path, pathname);
706 - strcat(path, filename);
707 - atom = CreateServerFileOpenAtom(GetPrefServerName(), path);
708 + atom = CreateServerFileOpenAtom(GetPrefServerName(), fullname);
709 return(atom);
712 /* Destroy the 'FileOpen' atom to inform nc that this file has
713 ** been opened.
715 -static void deleteFileOpenProperty(WindowInfo *window)
716 +static void deleteFileOpenProperty(WindowInfo *window, const char *fullname)
718 if (window->filenameSet) {
719 - Atom atom = findFileOpenProperty(window->filename, window->path);
720 + Atom atom = findFileOpenProperty(fullname);
721 deleteProperty(&atom);
725 -static void deleteFileOpenProperty2(const char* filename,
726 - const char* pathname)
727 +static void deleteFileOpenProperty2(const char *fullname)
729 - Atom atom = findFileOpenProperty(filename, pathname);
730 + Atom atom = findFileOpenProperty(fullname);
731 deleteProperty(&atom);
736 /* Try to find existing 'FileClosed' property atom for path. */
737 -static Atom findFileClosedProperty(const char* filename,
738 - const char* pathname)
739 +static Atom findFileClosedProperty(const char *fullname)
741 - char path[MAXPATHLEN];
742 Atom atom;
744 if (!IsServer) return(None);
746 - strcpy(path, pathname);
747 - strcat(path, filename);
748 atom = CreateServerFileClosedAtom(GetPrefServerName(),
749 - path,
750 + fullname,
751 True); /* don't create */
752 return(atom);
755 /* Get hold of the property to use when closing the file. */
756 -static void getFileClosedProperty(WindowInfo *window)
757 +static void getFileClosedProperty(WindowInfo *window, const char *fullname)
759 if (window->filenameSet) {
760 - window->fileClosedAtom = findFileClosedProperty(window->filename,
761 - window->path);
762 + window->fileClosedAtom = findFileClosedProperty(fullname);
766 @@ -282,10 +274,9 @@ void DeleteFileClosedProperty(WindowInfo
770 -static void deleteFileClosedProperty2(const char* filename,
771 - const char* pathname)
772 +static void deleteFileClosedProperty2(const char* fullname)
774 - Atom atom = findFileClosedProperty(filename, pathname);
775 + Atom atom = findFileClosedProperty(fullname);
776 deleteProperty(&atom);
779 @@ -338,7 +329,8 @@ static WindowInfo *findWindowOnDesktop(i
781 static void processServerCommandString(char *string)
783 - char *fullname, filename[MAXPATHLEN], pathname[MAXPATHLEN];
784 + const char *fullname, *requestname;
785 + char filename[MAXPATHLEN], pathname[MAXPATHLEN];
786 char *doCommand, *geometry, *langMode, *inPtr;
787 int editFlags, stringLen = strlen(string);
788 int lineNum, createFlag, readFlag, iconicFlag, lastIconic = 0, tabbed = -1;
789 @@ -388,7 +380,7 @@ static void processServerCommandString(c
790 inPtr += charsRead + 1;
791 if (inPtr - string + fileLen > stringLen)
792 goto readError;
793 - fullname = inPtr;
794 + requestname = inPtr;
795 inPtr += fileLen;
796 *inPtr++ = '\0';
797 if (inPtr - string + doLen > stringLen)
798 @@ -407,6 +399,8 @@ static void processServerCommandString(c
799 inPtr += geomLen;
800 *inPtr++ = '\0';
802 + fullname = PreOpenHook(NULL, requestname);
804 /* An empty file name means:
805 * put up an empty, Untitled window, or use an existing one
806 * choose a random window for executing the -do macro upon
807 @@ -456,7 +450,7 @@ static void processServerCommandString(c
808 (createFlag ? SUPPRESS_CREATE_WARN : 0);
809 if (ParseFilename(fullname, filename, pathname) != 0) {
810 fprintf(stderr, "NEdit: invalid file name\n");
811 - deleteFileClosedProperty2(filename, pathname);
812 + deleteFileClosedProperty2(requestname);
813 break;
816 @@ -486,8 +480,8 @@ static void processServerCommandString(c
817 /* Do the actions requested (note DoMacro is last, since the do
818 command can do anything, including closing the window!) */
819 if (window != NULL) {
820 - deleteFileOpenProperty(window);
821 - getFileClosedProperty(window);
822 + deleteFileOpenProperty(window, requestname);
823 + getFileClosedProperty(window, requestname);
825 if (lineNum > 0)
826 SelectNumberedLine(window, lineNum);
827 @@ -519,8 +513,8 @@ static void processServerCommandString(c
828 lastIconic = iconicFlag;
830 } else {
831 - deleteFileOpenProperty2(filename, pathname);
832 - deleteFileClosedProperty2(filename, pathname);
833 + deleteFileOpenProperty2(requestname);
834 + deleteFileClosedProperty2(requestname);
838 diff --quilt old/source/Makefile.common new/source/Makefile.common
839 --- old/source/Makefile.common
840 +++ new/source/Makefile.common
841 @@ -8,6 +8,7 @@ OBJS = nedit.o file.o menu.o window.o se
842 text.o textSel.o textDisp.o textBuf.o textDrag.o server.o highlight.o \
843 highlightData.o interpret.o parse.o smartIndent.o regexConvert.o \
844 rbTree.o windowTitle.o calltips.o server_common.o rangeset.o
845 +OBJS += hooks.o
847 XLTLIB = ../Xlt/libXlt.a
848 XMLLIB = ../Microline/XmL/libXmL.a
849 diff --quilt old/source/preferences.c new/source/preferences.c
850 --- old/source/preferences.c
851 +++ new/source/preferences.c
852 @@ -47,6 +47,7 @@ static const char CVSID[] = "$Id: prefer
853 #include "windowTitle.h"
854 #include "server.h"
855 #include "tags.h"
856 +#include "hooks.h"
857 #include "../util/prefFile.h"
858 #include "../util/misc.h"
859 #include "../util/DialogF.h"
860 @@ -4643,6 +4644,8 @@ static void reapplyLanguageMode(WindowIn
862 /* Add/remove language specific menu items */
863 UpdateUserMenus(window);
865 + LanguageModeHook(window, oldMode);