Minor leak fix.
[nedit.git] / source / nedit.c
blob3e998bcf9e769654dac1f7929ee1d7b03a30cf01
1 static const char CVSID[] = "$Id: nedit.c,v 1.88 2005/02/02 09:08:09 edg Exp $";
2 /*******************************************************************************
3 * *
4 * nedit.c -- Nirvana Editor main program *
5 * *
6 * Copyright (C) 1999 Mark Edel *
7 * *
8 * This is free software; you can redistribute it and/or modify it under the *
9 * terms of the GNU General Public License as published by the Free Software *
10 * Foundation; either version 2 of the License, or (at your option) any later *
11 * version. In addition, you may distribute versions of this program linked to *
12 * Motif or Open Motif. See README for details. *
13 * *
14 * This software is distributed in the hope that it will be useful, but WITHOUT *
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or *
16 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License *
17 * for more details. *
18 * *
19 * You should have received a copy of the GNU General Public License along with *
20 * software; if not, write to the Free Software Foundation, Inc., 59 Temple *
21 * Place, Suite 330, Boston, MA 02111-1307 USA *
22 * *
23 * Nirvana Text Editor *
24 * May 10, 1991 *
25 * *
26 * Written by Mark Edel *
27 * *
28 * Modifications: *
29 * *
30 * 8/18/93 - Mark Edel & Joy Kyriakopulos - Ported to VMS *
31 * *
32 *******************************************************************************/
34 #ifdef HAVE_CONFIG_H
35 #include "../config.h"
36 #endif
38 #include "nedit.h"
39 /* #include "textBuf.h" */
40 #include "file.h"
41 #include "preferences.h"
42 #include "regularExp.h"
43 #include "selection.h"
44 #include "tags.h"
45 #include "menu.h"
46 #include "macro.h"
47 #include "server.h"
48 #include "interpret.h"
49 #include "parse.h"
50 #include "help.h"
51 #include "../util/misc.h"
52 #include "../util/printUtils.h"
53 #include "../util/fileUtils.h"
54 #include "../util/getfiles.h"
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <ctype.h>
60 #include <limits.h>
62 #ifndef NO_XMIM
63 #include <X11/Xlocale.h>
64 #else
65 #include <locale.h>
66 #endif
67 #include <X11/Intrinsic.h>
68 #include <Xm/Xm.h>
69 #include <Xm/XmP.h>
70 #if XmVersion >= 1002
71 #include <Xm/RepType.h>
72 #endif
73 #ifdef VMS
74 #include <rmsdef.h>
75 #include "../util/VMSparam.h"
76 #include "../util/VMSUtils.h"
77 #else
78 #ifndef __MVS__
79 #include <sys/param.h>
80 #endif
81 #endif /*VMS*/
84 #ifdef HAVE_DEBUG_H
85 #include "../debug.h"
86 #endif
88 static void nextArg(int argc, char **argv, int *argIndex);
89 static int checkDoMacroArg(const char *macro);
90 static String neditLanguageProc(Display *dpy, String xnl, XtPointer closure);
91 static void maskArgvKeywords(int argc, char **argv, const char **maskArgs);
92 static void unmaskArgvKeywords(int argc, char **argv, const char **maskArgs);
93 static void patchResourcesForVisual(void);
94 static void patchResourcesForKDEbug(void);
95 static void patchLocaleForMotif(void);
96 static unsigned char* sanitizeVirtualKeyBindings(void);
97 static int sortAlphabetical(const void* k1, const void* k2);
98 static int virtKeyBindingsAreInvalid(const unsigned char* bindings);
99 static void restoreInsaneVirtualKeyBindings(unsigned char* bindings);
100 #ifdef LESSTIF_VERSION
101 static void bogusWarningFilter(String);
102 #endif
104 WindowInfo *WindowList = NULL;
105 Display *TheDisplay = NULL;
106 char *ArgV0 = NULL;
107 Boolean IsServer = False;
108 Widget TheAppShell;
110 /* Reasons for choice of default font qualifications:
112 iso8859 appears to be necessary for newer versions of XFree86 that
113 default to Unicode encoding, which doesn't quite work with Motif.
114 Otherwise Motif puts up garbage (square blocks).
116 (This of course, is a stupid default because there are far more iso8859
117 apps than Unicode apps. But the X folks insist it's a client bug. Hah.)
119 RedHat 7.3 won't default to '-1' for an encoding, if left with a *,
120 and so reverts to "fixed". Yech. */
122 #define NEDIT_DEFAULT_FONT "-*-helvetica-medium-r-normal-*-*-120-*-*-*-iso8859-1," \
123 "-*-helvetica-bold-r-normal-*-*-120-*-*-*-iso8859-1=BOLD," \
124 "-*-helvetica-medium-o-normal-*-*-120-*-*-*-iso8859-1=ITALIC"
126 #define NEDIT_FIXED_FONT "-*-courier-medium-r-normal-*-*-120-*-*-*-iso8859-1," \
127 "-*-courier-bold-r-normal-*-*-120-*-*-*-iso8859-1=BOLD," \
128 "-*-courier-medium-o-normal-*-*-120-*-*-*-iso8859-1=ITALIC"
130 #define NEDIT_DEFAULT_BG "#b3b3b3"
132 static char *fallbackResources[] = {
133 /* Try to avoid Motif's horrificly ugly default colors and fonts,
134 if the user's environment provides no usable defaults. We try
135 to choose a Windows-y default color setting here. Editable text
136 fields are forced to a fixed-pitch font for usability.
138 By using the VendorShell fontList resources, Motif automatically
139 groups the fonts into the right classes. It's then easier for
140 the user or environment to override this sensibly:
142 nedit -xrm '*textFontList: myfont'
145 #if (defined(LESSTIF_VERSION) && defined(__CYGWIN__))
146 "*FontList: " NEDIT_DEFAULT_FONT,
147 "*XmText.FontList: " NEDIT_FIXED_FONT,
148 "*XmTextField.FontList: " NEDIT_FIXED_FONT,
149 "*XmList.FontList: " NEDIT_FIXED_FONT,
150 "*XmFileSelectionBox*XmList.FontList: " NEDIT_FIXED_FONT,
151 #else
152 "*buttonFontList: " NEDIT_DEFAULT_FONT,
153 "*labelFontList: " NEDIT_DEFAULT_FONT,
154 "*textFontList: " NEDIT_FIXED_FONT,
155 #endif
157 "*background: " NEDIT_DEFAULT_BG,
158 "*foreground: " NEDIT_DEFAULT_FG,
159 "*XmText.foreground: " NEDIT_DEFAULT_FG,
160 "*XmText.background: " NEDIT_DEFAULT_TEXT_BG,
161 "*XmList.foreground: " NEDIT_DEFAULT_FG,
162 "*XmList.background: " NEDIT_DEFAULT_TEXT_BG,
163 "*XmTextField.foreground: " NEDIT_DEFAULT_FG,
164 "*XmTextField.background: " NEDIT_DEFAULT_TEXT_BG,
166 "*XmText.translations: #override\\n"
167 "Ctrl~Alt~Meta<KeyPress>v: paste-clipboard()\\n"
168 "Ctrl~Alt~Meta<KeyPress>c: copy-clipboard()\\n"
169 "Ctrl~Alt~Meta<KeyPress>x: cut-clipboard()\\n"
170 "Ctrl~Alt~Meta<KeyPress>u: delete-to-start-of-line()\\n",
171 "*XmTextField.translations: #override\\n"
172 "Ctrl~Alt~Meta<KeyPress>v: paste-clipboard()\\n"
173 "Ctrl~Alt~Meta<KeyPress>c: copy-clipboard()\\n"
174 "Ctrl~Alt~Meta<KeyPress>x: cut-clipboard()\\n"
175 "Ctrl~Alt~Meta<KeyPress>u: delete-to-start-of-line()\\n",
177 "*XmLFolder.highlightThickness: 0",
178 "*XmLFolder.shadowThickness: 1",
179 "*XmLFolder.maxTabWidth: 150",
180 "*XmLFolder.traversalOn: False",
181 "*XmLFolder.inactiveForeground: #666" ,
182 "*tab.alignment: XmALIGNMENT_BEGINNING",
183 "*tab.marginWidth: 0",
184 "*tab.marginHeight: 1",
186 /* Prevent the file selection box from acting stupid. */
187 "*XmFileSelectionBox.resizePolicy: XmRESIZE_NONE",
188 "*XmFileSelectionBox.textAccelerators:",
190 /* NEdit-specific widgets. Theses things should probably be manually
191 jammed into the database, rather than fallbacks. We really want
192 the accelerators to be there even if someone creates an app-defaults
193 file against our wishes. */
195 "*text.lineNumForeground: " NEDIT_DEFAULT_LINENO_FG,
196 "*text.background: " NEDIT_DEFAULT_TEXT_BG,
197 "*text.foreground: " NEDIT_DEFAULT_FG,
198 "*text.highlightForeground: " NEDIT_DEFAULT_HI_FG,
199 "*text.highlightBackground: " NEDIT_DEFAULT_HI_BG,
200 "*textFrame.shadowThickness: 1",
201 "*menuBar.marginHeight: 0",
202 "*menuBar.shadowThickness: 1",
203 "*pane.sashHeight: 11",
204 "*pane.sashWidth: 11",
205 "*pane.marginWidth: 0",
206 "*pane.marginHeight: 0",
207 "*scrolledW*spacing: 0",
208 "*text.selectionArrayCount: 3",
209 "*helpText.background: " NEDIT_DEFAULT_HELP_BG,
210 "*helpText.foreground: " NEDIT_DEFAULT_HELP_FG,
211 "*helpText.selectBackground: " NEDIT_DEFAULT_BG,
212 "*statsLine.background: " NEDIT_DEFAULT_BG,
213 "*statsLine.FontList: " NEDIT_DEFAULT_FONT,
214 "*calltip.background: LemonChiffon1",
215 "*calltip.foreground: black",
216 "*iSearchForm*highlightThickness: 1",
217 "*fileMenu.tearOffModel: XmTEAR_OFF_ENABLED",
218 "*editMenu.tearOffModel: XmTEAR_OFF_ENABLED",
219 "*searchMenu.tearOffModel: XmTEAR_OFF_ENABLED",
220 "*preferencesMenu.tearOffModel: XmTEAR_OFF_ENABLED",
221 "*windowsMenu.tearOffModel: XmTEAR_OFF_ENABLED",
222 "*shellMenu.tearOffModel: XmTEAR_OFF_ENABLED",
223 "*macroMenu.tearOffModel: XmTEAR_OFF_ENABLED",
224 "*helpMenu.tearOffModel: XmTEAR_OFF_ENABLED",
225 "*fileMenu.mnemonic: F",
226 "*fileMenu.new.accelerator: Ctrl<Key>n",
227 "*fileMenu.new.acceleratorText: Ctrl+N",
228 "*fileMenu.newOpposite.accelerator: Shift Ctrl<Key>n",
229 "*fileMenu.newOpposite.acceleratorText: Shift+Ctrl+N",
230 "*fileMenu.open.accelerator: Ctrl<Key>o",
231 "*fileMenu.open.acceleratorText: Ctrl+O",
232 "*fileMenu.openSelected.accelerator: Ctrl<Key>y",
233 "*fileMenu.openSelected.acceleratorText: Ctrl+Y",
234 "*fileMenu.close.accelerator: Ctrl<Key>w",
235 "*fileMenu.close.acceleratorText: Ctrl+W",
236 "*fileMenu.save.accelerator: Ctrl<Key>s",
237 "*fileMenu.save.acceleratorText: Ctrl+S",
238 "*fileMenu.includeFile.accelerator: Alt<Key>i",
239 "*fileMenu.includeFile.acceleratorText: Alt+I",
240 "*fileMenu.print.accelerator: Ctrl<Key>p",
241 "*fileMenu.print.acceleratorText: Ctrl+P",
242 "*fileMenu.exit.accelerator: Ctrl<Key>q",
243 "*fileMenu.exit.acceleratorText: Ctrl+Q",
244 "*editMenu.mnemonic: E",
245 "*editMenu.undo.accelerator: Ctrl<Key>z",
246 "*editMenu.undo.acceleratorText: Ctrl+Z",
247 "*editMenu.redo.accelerator: Shift Ctrl<Key>z",
248 "*editMenu.redo.acceleratorText: Shift+Ctrl+Z",
249 "*editMenu.cut.accelerator: Ctrl<Key>x",
250 "*editMenu.cut.acceleratorText: Ctrl+X",
251 "*editMenu.copy.accelerator: Ctrl<Key>c",
252 "*editMenu.copy.acceleratorText: Ctrl+C",
253 "*editMenu.paste.accelerator: Ctrl<Key>v",
254 "*editMenu.paste.acceleratorText: Ctrl+V",
255 "*editMenu.pasteColumn.accelerator: Shift Ctrl<Key>v",
256 "*editMenu.pasteColumn.acceleratorText: Ctrl+Shift+V",
257 "*editMenu.delete.acceleratorText: Del",
258 "*editMenu.selectAll.accelerator: Ctrl<Key>a",
259 "*editMenu.selectAll.acceleratorText: Ctrl+A",
260 "*editMenu.shiftLeft.accelerator: Ctrl<Key>9",
261 "*editMenu.shiftLeft.acceleratorText: [Shift]Ctrl+9",
262 "*editMenu.shiftLeftShift.accelerator: Shift Ctrl<Key>9",
263 "*editMenu.shiftRight.accelerator: Ctrl<Key>0",
264 "*editMenu.shiftRight.acceleratorText: [Shift]Ctrl+0",
265 "*editMenu.shiftRightShift.accelerator: Shift Ctrl<Key>0",
266 "*editMenu.upperCase.accelerator: Ctrl<Key>6",
267 "*editMenu.upperCase.acceleratorText: Ctrl+6",
268 "*editMenu.lowerCase.accelerator: Shift Ctrl<Key>6",
269 "*editMenu.lowerCase.acceleratorText: Shift+Ctrl+6",
270 "*editMenu.fillParagraph.accelerator: Ctrl<Key>j",
271 "*editMenu.fillParagraph.acceleratorText: Ctrl+J",
272 "*editMenu.insertFormFeed.accelerator: Alt Ctrl<Key>l",
273 "*editMenu.insertFormFeed.acceleratorText: Alt+Ctrl+L",
274 "*editMenu.insertCtrlCode.accelerator: Alt Ctrl<Key>i",
275 "*editMenu.insertCtrlCode.acceleratorText: Alt+Ctrl+I",
276 "*searchMenu.mnemonic: S",
277 "*searchMenu.find.accelerator: Ctrl<Key>f",
278 "*searchMenu.find.acceleratorText: [Shift]Ctrl+F",
279 "*searchMenu.findShift.accelerator: Shift Ctrl<Key>f",
280 "*searchMenu.findAgain.accelerator: Ctrl<Key>g",
281 "*searchMenu.findAgain.acceleratorText: [Shift]Ctrl+G",
282 "*searchMenu.findAgainShift.accelerator: Shift Ctrl<Key>g",
283 "*searchMenu.findSelection.accelerator: Ctrl<Key>h",
284 "*searchMenu.findSelection.acceleratorText: [Shift]Ctrl+H",
285 "*searchMenu.findSelectionShift.accelerator: Shift Ctrl<Key>h",
286 "*searchMenu.findIncremental.accelerator: Ctrl<Key>i",
287 "*searchMenu.findIncrementalShift.accelerator: Shift Ctrl<Key>i",
288 "*searchMenu.findIncremental.acceleratorText: [Shift]Ctrl+I",
289 "*searchMenu.replace.accelerator: Ctrl<Key>r",
290 "*searchMenu.replace.acceleratorText: [Shift]Ctrl+R",
291 "*searchMenu.replaceShift.accelerator: Shift Ctrl<Key>r",
292 "*searchMenu.findReplace.accelerator: Ctrl<Key>r",
293 "*searchMenu.findReplace.acceleratorText: [Shift]Ctrl+R",
294 "*searchMenu.findReplaceShift.accelerator: Shift Ctrl<Key>r",
295 "*searchMenu.replaceFindAgain.accelerator: Ctrl<Key>t",
296 "*searchMenu.replaceFindAgain.acceleratorText: [Shift]Ctrl+T",
297 "*searchMenu.replaceFindAgainShift.accelerator: Shift Ctrl<Key>t",
298 "*searchMenu.replaceAgain.accelerator: Alt<Key>t",
299 "*searchMenu.replaceAgain.acceleratorText: [Shift]Alt+T",
300 "*searchMenu.replaceAgainShift.accelerator: Shift Alt<Key>t",
301 "*searchMenu.gotoLineNumber.accelerator: Ctrl<Key>l",
302 "*searchMenu.gotoLineNumber.acceleratorText: Ctrl+L",
303 "*searchMenu.gotoSelected.accelerator: Ctrl<Key>e",
304 "*searchMenu.gotoSelected.acceleratorText: Ctrl+E",
305 "*searchMenu.mark.accelerator: Alt<Key>m",
306 "*searchMenu.mark.acceleratorText: Alt+M a-z",
307 "*searchMenu.gotoMark.accelerator: Alt<Key>g",
308 "*searchMenu.gotoMark.acceleratorText: [Shift]Alt+G a-z",
309 "*searchMenu.gotoMarkShift.accelerator: Shift Alt<Key>g",
310 "*searchMenu.gotoMatching.accelerator: Ctrl<Key>m",
311 "*searchMenu.gotoMatching.acceleratorText: [Shift]Ctrl+M",
312 "*searchMenu.gotoMatchingShift.accelerator: Shift Ctrl<Key>m",
313 "*searchMenu.findDefinition.accelerator: Ctrl<Key>d",
314 "*searchMenu.findDefinition.acceleratorText: Ctrl+D",
315 "*searchMenu.showCalltip.accelerator: Ctrl<Key>apostrophe",
316 "*searchMenu.showCalltip.acceleratorText: Ctrl+'",
317 "*preferencesMenu.mnemonic: P",
318 "*preferencesMenu.statisticsLine.accelerator: Alt<Key>a",
319 "*preferencesMenu.statisticsLine.acceleratorText: Alt+A",
320 "*preferencesMenu.overtype.acceleratorText: Insert",
321 "*shellMenu.mnemonic: l",
322 "*shellMenu.filterSelection.accelerator: Alt<Key>r",
323 "*shellMenu.filterSelection.acceleratorText: Alt+R",
324 "*shellMenu.executeCommand.accelerator: Alt<Key>x",
325 "*shellMenu.executeCommand.acceleratorText: Alt+X",
326 "*shellMenu.executeCommandLine.accelerator: Ctrl<Key>KP_Enter",
327 "*shellMenu.executeCommandLine.acceleratorText: Ctrl+KP Enter",
328 "*shellMenu.cancelShellCommand.accelerator: Ctrl<Key>period",
329 "*shellMenu.cancelShellCommand.acceleratorText: Ctrl+.",
330 "*macroMenu.mnemonic: c",
331 "*macroMenu.learnKeystrokes.accelerator: Alt<Key>k",
332 "*macroMenu.learnKeystrokes.acceleratorText: Alt+K",
333 "*macroMenu.finishLearn.accelerator: Alt<Key>k",
334 "*macroMenu.finishLearn.acceleratorText: Alt+K",
335 "*macroMenu.cancelLearn.accelerator: Ctrl<Key>period",
336 "*macroMenu.cancelLearn.acceleratorText: Ctrl+.",
337 "*macroMenu.replayKeystrokes.accelerator: Ctrl<Key>k",
338 "*macroMenu.replayKeystrokes.acceleratorText: Ctrl+K",
339 "*macroMenu.repeat.accelerator: Ctrl<Key>comma",
340 "*macroMenu.repeat.acceleratorText: Ctrl+,",
341 "*windowsMenu.mnemonic: W",
342 "*windowsMenu.splitPane.accelerator: Ctrl<Key>2",
343 "*windowsMenu.splitPane.acceleratorText: Ctrl+2",
344 "*windowsMenu.closePane.accelerator: Ctrl<Key>1",
345 "*windowsMenu.closePane.acceleratorText: Ctrl+1",
346 "*helpMenu.mnemonic: H",
347 "nedit.help.helpForm.sw.helpText*translations: #override\
348 <Key>Tab:help-focus-buttons()\\n\
349 <Key>Return:help-button-action(\"close\")\\n\
350 Ctrl<Key>F:help-button-action(\"find\")\\n\
351 Ctrl<Key>G:help-button-action(\"findAgain\")\\n\
352 <KeyPress>osfCancel:help-button-action(\"close\")\\n\
353 ~Meta~Ctrl~Shift<Btn1Down>:\
354 grab-focus() help-hyperlink()\\n\
355 ~Meta~Ctrl~Shift<Btn1Up>:\
356 help-hyperlink(\"current\", \"process-cancel\", \"extend-end\")\\n\
357 ~Meta~Ctrl~Shift<Btn2Down>:\
358 process-bdrag() help-hyperlink()\\n\
359 ~Meta~Ctrl~Shift<Btn2Up>:\
360 help-hyperlink(\"new\", \"process-cancel\", \"copy-to\")",
361 NULL
364 static const char cmdLineHelp[] =
365 #ifndef VMS
366 "Usage: nedit [-read] [-create] [-line n | +n] [-server] [-do command]\n\
367 [-tags file] [-tabs n] [-wrap] [-nowrap] [-autowrap]\n\
368 [-autoindent] [-noautoindent] [-autosave] [-noautosave]\n\
369 [-lm languagemode] [-rows n] [-columns n] [-font font]\n\
370 [-geometry geometry] [-iconic] [-noiconic] [-svrname name]\n\
371 [-display [host]:server[.screen] [-xrm resourcestring]\n\
372 [-import file] [-background color] [-foreground color]\n\
373 [-tabbed] [-untabbed] [-group] [-V|-version]\n\
374 [--] [file...]\n";
375 #else
377 #endif /*VMS*/
379 int main(int argc, char **argv)
381 int i, lineNum, nRead, fileSpecified = FALSE, editFlags = CREATE;
382 int gotoLine = False, macroFileRead = False, opts = True;
383 int iconic=False, tabbed = -1, group = 0, isTabbed;
384 char *toDoCommand = NULL, *geometry = NULL, *langMode = NULL;
385 char filename[MAXPATHLEN], pathname[MAXPATHLEN];
386 XtAppContext context;
387 XrmDatabase prefDB;
388 WindowInfo *window = NULL, *lastFile = NULL;
389 static const char *protectedKeywords[] = {"-iconic", "-icon", "-geometry",
390 "-g", "-rv", "-reverse", "-bd", "-bordercolor", "-borderwidth",
391 "-bw", "-title", NULL};
392 unsigned char* invalidBindings = NULL;
394 /* Save the command which was used to invoke nedit for restart command */
395 ArgV0 = argv[0];
397 /* Set locale for C library, X, and Motif input functions.
398 Reverts to "C" if requested locale not available. */
399 XtSetLanguageProc(NULL, neditLanguageProc, NULL);
401 /* Initialize X toolkit (does not open display yet) */
402 XtToolkitInitialize();
403 context = XtCreateApplicationContext();
405 /* Set up a warning handler to trap obnoxious Xt grab warnings */
406 SuppressPassiveGrabWarnings();
408 #ifdef LESSTIF_VERSION
409 /* Set up a handler to suppress bogus Lesstif warning messages */
410 XtAppSetWarningHandler(context, bogusWarningFilter);
411 #endif
413 /* Set up default resources if no app-defaults file is found */
414 XtAppSetFallbackResources(context, fallbackResources);
416 #if XmVersion >= 1002
417 /* Allow users to change tear off menus with X resources */
418 XmRepTypeInstallTearOffModelConverter();
419 #endif
421 #ifdef VMS
422 /* Convert the command line to Unix style (This is not an ideal solution) */
423 ConvertVMSCommandLine(&argc, &argv);
424 #endif /*VMS*/
425 #ifdef __EMX__
426 /* expand wildcards if necessary */
427 _wildcard(&argc, &argv);
428 #endif
430 /* Read the preferences file and command line into a database */
431 prefDB = CreateNEditPrefDB(&argc, argv);
433 /* Open the display and read X database and remaining command line args.
434 XtOpenDisplay must be allowed to process some of the resource arguments
435 with its inaccessible internal option table, but others, like -geometry
436 and -iconic are per-window and it should not be allowed to consume them,
437 so we temporarily masked them out. */
438 maskArgvKeywords(argc, argv, protectedKeywords);
439 TheDisplay = XtOpenDisplay (context, NULL, APP_NAME, APP_CLASS,
440 NULL, 0, &argc, argv);
441 unmaskArgvKeywords(argc, argv, protectedKeywords);
442 if (!TheDisplay) {
443 /* Respond to -V or -version even if there is no display */
444 for (i = 1; i < argc && strcmp(argv[i], "--"); i++)
446 if (0 == strcmp(argv[i], "-V") || 0 == strcmp(argv[i], "-version"))
448 PrintVersion();
449 exit(EXIT_SUCCESS);
452 XtWarning ("NEdit: Can't open display\n");
453 exit(EXIT_FAILURE);
456 /* Must be done before creating widgets */
457 patchResourcesForVisual();
458 patchResourcesForKDEbug();
460 /* Initialize global symbols and subroutines used in the macro language */
461 InitMacroGlobals();
462 RegisterMacroSubroutines();
464 /* Store preferences from the command line and .nedit file,
465 and set the appropriate preferences */
466 RestoreNEditPrefs(prefDB, XtDatabase(TheDisplay));
468 /* Intercept syntactically invalid virtual key bindings BEFORE we
469 create any shells. */
470 invalidBindings = sanitizeVirtualKeyBindings();
472 /* Create a hidden application shell that is the parent of all the
473 main editor windows. Realize it so it the window can act as
474 group leader. */
475 TheAppShell = CreateShellWithBestVis(APP_NAME,
476 APP_CLASS,
477 applicationShellWidgetClass,
478 TheDisplay,
479 NULL,
482 /* Restore the original bindings ASAP such that other apps are not affected. */
483 restoreInsaneVirtualKeyBindings(invalidBindings);
485 XtSetMappedWhenManaged(TheAppShell, False);
486 XtRealizeWidget(TheAppShell);
488 #ifndef NO_SESSION_RESTART
489 AttachSessionMgrHandler(TheAppShell);
490 #endif
492 /* More preference stuff */
493 LoadPrintPreferences(XtDatabase(TheDisplay), APP_NAME, APP_CLASS, True);
494 SetDeleteRemap(GetPrefMapDelete());
495 SetPointerCenteredDialogs(GetPrefRepositionDialogs());
496 SetGetEFTextFieldRemoval(!GetPrefStdOpenDialog());
498 /* Set up action procedures for menu item commands */
499 InstallMenuActions(context);
501 /* Add Actions for following hyperlinks in the help window */
502 InstallHelpLinkActions(context);
503 /* Add actions for mouse wheel support in scrolled windows (except text
504 area) */
505 InstallMouseWheelActions(context);
507 /* Install word delimiters for regular expression matching */
508 SetREDefaultWordDelimiters(GetPrefDelimiters());
510 /* Read the nedit dynamic database of files for the Open Previous
511 command (and eventually other information as well) */
512 ReadNEditDB();
514 /* Process -import command line argument before others which might
515 open windows (loading preferences doesn't update menu settings,
516 which would then be out of sync with the real preference settings) */
517 for (i=1; i<argc; i++) {
518 if(!strcmp(argv[i], "--")) {
519 break; /* treat all remaining arguments as filenames */
520 } else if (!strcmp(argv[i], "-import")) {
521 nextArg(argc, argv, &i);
522 ImportPrefFile(argv[i], False);
523 } else if (!strcmp(argv[i], "-importold")) {
524 nextArg(argc, argv, &i);
525 ImportPrefFile(argv[i], True);
529 /* Load the default tags file. Don't complain if it doesn't load, the tag
530 file resource is intended to be set and forgotten. Running nedit in a
531 directory without a tags should not cause it to spew out errors. */
532 if (*GetPrefTagFile() != '\0') {
533 AddTagsFile(GetPrefTagFile(), TAG);
536 if (strcmp(GetPrefServerName(), "") != 0) {
537 IsServer = True;
540 /* Process any command line arguments (-tags, -do, -read, -create,
541 +<line_number>, -line, -server, and files to edit) not already
542 processed by RestoreNEditPrefs. */
543 fileSpecified = FALSE;
544 for (i=1; i<argc; i++) {
545 if (opts && !strcmp(argv[i], "--")) {
546 opts = False; /* treat all remaining arguments as filenames */
547 continue;
548 } else if (opts && !strcmp(argv[i], "-tags")) {
549 nextArg(argc, argv, &i);
550 if (!AddTagsFile(argv[i], TAG))
551 fprintf(stderr, "NEdit: Unable to load tags file\n");
552 } else if (opts && !strcmp(argv[i], "-do")) {
553 nextArg(argc, argv, &i);
554 if (checkDoMacroArg(argv[i]))
555 toDoCommand = argv[i];
556 } else if (opts && !strcmp(argv[i], "-read")) {
557 editFlags |= PREF_READ_ONLY;
558 } else if (opts && !strcmp(argv[i], "-create")) {
559 editFlags |= SUPPRESS_CREATE_WARN;
560 } else if (opts && !strcmp(argv[i], "-tabbed")) {
561 tabbed = 1;
562 group = 0; /* override -group option */
563 } else if (opts && !strcmp(argv[i], "-untabbed")) {
564 tabbed = 0;
565 group = 0; /* override -group option */
566 } else if (opts && !strcmp(argv[i], "-group")) {
567 group = 2; /* 2: start new group, 1: in group */
568 } else if (opts && !strcmp(argv[i], "-line")) {
569 nextArg(argc, argv, &i);
570 nRead = sscanf(argv[i], "%d", &lineNum);
571 if (nRead != 1)
572 fprintf(stderr, "NEdit: argument to line should be a number\n");
573 else
574 gotoLine = True;
575 } else if (opts && (*argv[i] == '+')) {
576 nRead = sscanf((argv[i]+1), "%d", &lineNum);
577 if (nRead != 1)
578 fprintf(stderr, "NEdit: argument to + should be a number\n");
579 else
580 gotoLine = True;
581 } else if (opts && !strcmp(argv[i], "-server")) {
582 IsServer = True;
583 } else if (opts && (!strcmp(argv[i], "-iconic") ||
584 !strcmp(argv[i], "-icon"))) {
585 iconic = True;
586 } else if (opts && !strcmp(argv[i], "-noiconic")) {
587 iconic = False;
588 } else if (opts && (!strcmp(argv[i], "-geometry") ||
589 !strcmp(argv[i], "-g"))) {
590 nextArg(argc, argv, &i);
591 geometry = argv[i];
592 } else if (opts && !strcmp(argv[i], "-lm")) {
593 nextArg(argc, argv, &i);
594 langMode = argv[i];
595 } else if (opts && !strcmp(argv[i], "-import")) {
596 nextArg(argc, argv, &i); /* already processed, skip */
597 } else if (opts && (!strcmp(argv[i], "-V") ||
598 !strcmp(argv[i], "-version"))) {
599 PrintVersion();
600 exit(EXIT_SUCCESS);
601 } else if (opts && (*argv[i] == '-')) {
602 #ifdef VMS
603 *argv[i] = '/';
604 #endif /*VMS*/
605 fprintf(stderr, "nedit: Unrecognized option %s\n%s", argv[i],
606 cmdLineHelp);
607 exit(EXIT_FAILURE);
608 } else {
609 #ifdef VMS
610 int numFiles, j;
611 char **nameList = NULL;
612 /* Use VMS's LIB$FILESCAN for filename in argv[i] to process */
613 /* wildcards and to obtain a full VMS file specification */
614 numFiles = VMSFileScan(argv[i], &nameList, NULL, INCLUDE_FNF);
615 /* for each expanded file name do: */
616 for (j = 0; j < numFiles; ++j) {
617 if (ParseFilename(nameList[j], filename, pathname) == 0) {
618 /* determine if file is to be openned in new tab, by
619 factoring the options -group, -tabbed & -untabbed */
620 if (group == 2) {
621 isTabbed = 0; /* start a new window for new group */
622 group = 1; /* next file will be within group */
623 } else if (group == 1) {
624 isTabbed = 1; /* new tab for file in group */
625 } else { /* not in group */
626 isTabbed = tabbed==-1? GetPrefOpenInTab() : tabbed;
629 /* Files are opened in background to improve opening speed
630 by defering certain time consuiming task such as syntax
631 highlighting. At the end of the file-opening loop, the
632 last file opened will be raised to restore those deferred
633 items. The current file may also be raised if there're
634 macros to execute on. */
635 window = EditExistingFile(WindowList, filename, pathname,
636 editFlags, geometry, iconic, langMode, isTabbed,
637 True);
638 fileSpecified = TRUE;
640 if (window) {
641 CleanUpTabBarExposeQueue(window);
643 /* raise the last file of previous window */
644 if (lastFile && window->shell != lastFile->shell) {
645 CleanUpTabBarExposeQueue(lastFile);
646 RaiseDocument(lastFile);
649 if (!macroFileRead) {
650 ReadMacroInitFile(WindowList);
651 macroFileRead = True;
653 if (gotoLine)
654 SelectNumberedLine(window, lineNum);
655 if (toDoCommand != NULL) {
656 DoMacro(window, toDoCommand, "-do macro");
657 toDoCommand = NULL;
658 if (!IsValidWindow(window))
659 window = NULL; /* window closed by macro */
660 if (lastFile && !IsValidWindow(lastFile))
661 lastFile = NULL; /* window closed by macro */
665 /* register last opened file for later use */
666 if (window)
667 lastFile = window;
668 } else {
669 fprintf(stderr, "nedit: file name too long: %s\n", nameList[j]);
671 free(nameList[j]);
673 if (nameList != NULL)
674 free(nameList);
675 #else
676 if (ParseFilename(argv[i], filename, pathname) == 0 ) {
677 /* determine if file is to be openned in new tab, by
678 factoring the options -group, -tabbed & -untabbed */
679 if (group == 2) {
680 isTabbed = 0; /* start a new window for new group */
681 group = 1; /* next file will be within group */
682 } else if (group == 1) {
683 isTabbed = 1; /* new tab for file in group */
684 } else { /* not in group */
685 isTabbed = tabbed==-1? GetPrefOpenInTab() : tabbed;
688 /* Files are opened in background to improve opening speed
689 by defering certain time consuiming task such as syntax
690 highlighting. At the end of the file-opening loop, the
691 last file opened will be raised to restore those deferred
692 items. The current file may also be raised if there're
693 macros to execute on. */
694 window = EditExistingFile(WindowList, filename, pathname,
695 editFlags, geometry, iconic, langMode, isTabbed, True);
696 fileSpecified = TRUE;
697 if (window) {
698 CleanUpTabBarExposeQueue(window);
700 /* raise the last tab of previous window */
701 if (lastFile && window->shell != lastFile->shell) {
702 CleanUpTabBarExposeQueue(lastFile);
703 RaiseDocument(lastFile);
706 if (!macroFileRead) {
707 ReadMacroInitFile(WindowList);
708 macroFileRead = True;
710 if (gotoLine)
711 SelectNumberedLine(window, lineNum);
712 if (toDoCommand != NULL) {
713 DoMacro(window, toDoCommand, "-do macro");
714 toDoCommand = NULL;
715 if (!IsValidWindow(window))
716 window = NULL; /* window closed by macro */
717 if (lastFile && !IsValidWindow(lastFile))
718 lastFile = NULL; /* window closed by macro */
722 /* register last opened file for later use */
723 if (window)
724 lastFile = window;
725 } else {
726 fprintf(stderr, "nedit: file name too long: %s\n", argv[i]);
728 #endif /*VMS*/
731 #ifdef VMS
732 VMSFileScanDone();
733 #endif /*VMS*/
735 /* Raise the last file opened */
736 if (lastFile) {
737 CleanUpTabBarExposeQueue(lastFile);
738 RaiseDocument(lastFile);
740 CheckCloseDim();
742 /* If no file to edit was specified, open a window to edit "Untitled" */
743 if (!fileSpecified) {
744 EditNewFile(NULL, geometry, iconic, langMode, NULL);
745 ReadMacroInitFile(WindowList);
746 CheckCloseDim();
747 if (toDoCommand != NULL)
748 DoMacro(WindowList, toDoCommand, "-do macro");
751 /* Begin remembering last command invoked for "Repeat" menu item */
752 AddLastCommandActionHook(context);
754 /* Set up communication port and write ~/.nedit_server_process file */
755 if (IsServer)
756 InitServerCommunication();
758 /* Process events. */
759 if (IsServer)
760 ServerMainLoop(context);
761 else
762 XtAppMainLoop(context);
764 /* Not reached but this keeps some picky compilers happy */
765 return EXIT_SUCCESS;
768 static void nextArg(int argc, char **argv, int *argIndex)
770 if (*argIndex + 1 >= argc) {
771 #ifdef VMS
772 *argv[*argIndex] = '/';
773 #endif /*VMS*/
774 fprintf(stderr, "NEdit: %s requires an argument\n%s", argv[*argIndex],
775 cmdLineHelp);
776 exit(EXIT_FAILURE);
778 (*argIndex)++;
782 ** Return True if -do macro is valid, otherwise write an error on stderr
784 static int checkDoMacroArg(const char *macro)
786 Program *prog;
787 char *errMsg, *stoppedAt, *tMacro;
788 int macroLen;
790 /* Add a terminating newline (which command line users are likely to omit
791 since they are typically invoking a single routine) */
792 macroLen = strlen(macro);
793 tMacro = XtMalloc(strlen(macro)+2);
794 strncpy(tMacro, macro, macroLen);
795 tMacro[macroLen] = '\n';
796 tMacro[macroLen+1] = '\0';
798 /* Do a test parse */
799 prog = ParseMacro(tMacro, &errMsg, &stoppedAt);
800 XtFree(tMacro);
801 if (prog == NULL) {
802 ParseError(NULL, tMacro, stoppedAt, "argument to -do", errMsg);
803 return False;
805 FreeProgram(prog);
806 return True;
810 ** maskArgvKeywords and unmaskArgvKeywords mangle selected keywords by
811 ** replacing the '-' with a space, for the purpose of hiding them from
812 ** XtOpenDisplay's option processing. Why this silly scheme? XtOpenDisplay
813 ** really needs to see command line arguments, particularly -display, but
814 ** there's no way to change the option processing table it uses, to keep
815 ** it from consuming arguments which are meant to apply per-window, like
816 ** -geometry and -iconic.
818 static void maskArgvKeywords(int argc, char **argv, const char **maskArgs)
820 int i, k;
822 for (i=1; i<argc; i++)
823 for (k=0; maskArgs[k]!=NULL; k++)
824 if (!strcmp(argv[i], maskArgs[k]))
825 argv[i][0] = ' ';
829 static void unmaskArgvKeywords(int argc, char **argv, const char **maskArgs)
831 int i, k;
833 for (i=1; i<argc; i++)
834 for (k=0; maskArgs[k]!=NULL; k++)
835 if (argv[i][0]==' ' && !strcmp(&argv[i][1], &maskArgs[k][1]))
836 argv[i][0] = '-';
840 ** If we're not using the default visual, then some default resources in
841 ** the database (colors) are not valid, because they are indexes into the
842 ** default colormap. If we used them blindly, then we'd get "random"
843 ** unreadable colors. So we inspect the resource list, and use the
844 ** fallback "grey" color instead if this is the case.
846 static void patchResourcesForVisual(void)
848 Cardinal i;
849 Visual *visual;
850 int depth;
851 Colormap map;
852 Boolean usingDefaultVisual;
853 XrmDatabase db;
855 if (!TheDisplay)
856 return;
858 db = XtDatabase(TheDisplay);
860 usingDefaultVisual = FindBestVisual(TheDisplay,
861 APP_NAME,
862 APP_CLASS,
863 &visual,
864 &depth,
865 &map);
867 if (!usingDefaultVisual)
869 #ifndef LESSTIF_VERSION
871 For non-Lesstif versions, we have to put non-default visuals etc.
872 in the resource data base to make sure that all (shell) widgets
873 inherit them, especially Motif's own shells (eg, drag icons).
875 For Lesstif, this doesn't work, but luckily, Lesstif handles
876 non-default visuals etc. properly for its own shells and
877 we can take care of things for our shells (eg, call tips) through
878 our shell creation wrappers in misc.c.
881 XrmValue value;
882 value.addr = (XPointer)&visual;
883 value.size = sizeof(visual);
884 XrmPutResource(&db, "*visual", "Visual", &value);
885 value.addr = (XPointer)&map;
886 value.size = sizeof(map);
887 XrmPutResource(&db, "*colormap", "Colormap", &value);
888 value.addr = (XPointer)&depth;
889 value.size = sizeof(depth);
890 XrmPutResource(&db, "*depth", "Int", &value);
892 /* Drag-and-drop visuals do not work well when using a different
893 visual. One some systems, you'll just get a funny-looking icon
894 (maybe all-black) but on other systems it crashes with a BadMatch
895 error. This appears to be an OSF Motif bug. It would be nicer
896 to just disable the visual itself, instead of the entire drag
897 operation.
899 Update: this is no longer necessary since all problems with
900 non-default visuals should now be solved.
902 XrmPutStringResource(&db, "*dragInitiatorProtocolStyle", "DRAG_NONE");
904 #endif
906 for (i = 1; i < XtNumber(fallbackResources); ++i)
908 Cardinal resIndex = i - 1;
910 if (strstr(fallbackResources[resIndex], "*background:") ||
911 strstr(fallbackResources[resIndex], "*foreground:"))
913 /* Qualify by application name to prevent them from being
914 converted against the wrong colormap. */
915 char buf[1024] = "*" APP_NAME;
916 strcat(buf, fallbackResources[resIndex]);
917 XrmPutLineResource(&db, buf);
924 ** Several KDE version (2.x and 3.x) ship with a template application-default
925 ** file for NEdit in which several strings have to be substituted in order to
926 ** make it a valid .ad file. However, for some reason (a KDE bug?), the
927 ** template sometimes ends up in the resource db unmodified, such that several
928 ** invalid entries are present. This function checks for the presence of such
929 ** invalid entries and silently replaces them with NEdit's default values where
930 ** necessary. Without this, NEdit will typically write several warnings to
931 ** the terminal (Cannot convert string "FONTLIST" to type FontStruct etc) and
932 ** fall back on some really ugly colors and fonts.
934 static void patchResourcesForKDEbug(void)
937 * These are the resources found the Nedit.ad template shipped with KDE 3.0.
939 static const char* buggyResources[][3] = {
940 { "*fontList", "FONTLIST", NEDIT_DEFAULT_FONT },
941 { "*XmText.background", "BACKGROUND", NEDIT_DEFAULT_TEXT_BG },
942 { "*XmText.foreground", "FOREGROUND", NEDIT_DEFAULT_FG },
943 { "*XmTextField.background", "BACKGROUND", NEDIT_DEFAULT_TEXT_BG },
944 { "*XmTextField.foreground", "FOREGROUND", NEDIT_DEFAULT_FG },
945 { "*XmList.background", "BACKGROUND", NEDIT_DEFAULT_TEXT_BG },
946 { "*XmList.foreground", "FOREGROUND", NEDIT_DEFAULT_FG },
947 { "*helpText.background", "BACKGROUND", NEDIT_DEFAULT_HELP_BG },
948 { "*helpText.foreground", "FOREGROUND", NEDIT_DEFAULT_HELP_FG },
949 { "*background", "BACKGROUND", NEDIT_DEFAULT_BG },
950 { "*foreground", "FOREGROUND", NEDIT_DEFAULT_FG, },
951 { "*selectColor", "BACKGROUND", NEDIT_DEFAULT_SEL_BG },
952 { "*highlightColor", "BACKGROUND", NEDIT_DEFAULT_HI_BG },
953 { "*text.background", "WINDOW_BACKGROUND", NEDIT_DEFAULT_TEXT_BG },
954 { "*text.foreground", "WINDOW_FOREGROUND", NEDIT_DEFAULT_FG },
955 { "*text.selectBackground", "SELECT_BACKGROUND", NEDIT_DEFAULT_SEL_BG },
956 { "*text.selectForeground", "SELECT_FOREGROUND", NEDIT_DEFAULT_SEL_FG },
957 { "*text.cursorForeground", "WINDOW_FOREGROUND", NEDIT_DEFAULT_CURSOR_FG},
958 /* { "*remapDeleteKey", "False", }, OK */
959 /* { "!*text.heavyCursor", "True" }, OK */
960 /* { "!*BlinkRate", "0" }, OK */
961 /* { "*shell", "/bin/sh" }, OK */
962 { "*statsLine.background", "BACKGROUND", NEDIT_DEFAULT_BG },
963 { "*statsLine.foreground", "FOREGROUND", NEDIT_DEFAULT_FG },
964 { NULL, NULL, NULL } };
965 XrmDatabase db;
966 int i;
968 if (!TheDisplay)
969 return;
971 db = XtDatabase(TheDisplay);
973 i = 0;
974 while (buggyResources[i][0])
976 const char* resource = buggyResources[i][0];
977 const char* buggyValue = buggyResources[i][1];
978 const char* defaultValue = buggyResources[i][2];
979 char name[128] = APP_NAME;
980 char class[128] = APP_CLASS;
981 char* type;
982 XrmValue resValue;
984 strcat(name, resource);
985 strcat(class, resource); /* Is this ok ? */
987 if (XrmGetResource(db, name, class, &type, &resValue) &&
988 !strcmp(type, XmRString))
990 /* Buggy value? Replace by the default. */
991 if (!strcmp(resValue.addr, buggyValue))
993 XrmPutStringResource(&db, &name[0], (char*)defaultValue);
996 ++i;
1001 ** It seems OSF Motif cannot handle locales with UTF-8 at the end, crashing
1002 ** in various places. The easiest one to find is to open the File Open
1003 ** dialog box. So we lop off UTF-8 if it's there and continue. Newer
1004 ** versions of Linux distros (e.g., RedHat 8) set the default language to
1005 ** to have "UTF-8" at the end, so users were seeing these crashes.
1007 static void patchLocaleForMotif()
1009 #ifndef LESSTIF_VERSION
1010 const char *ctype;
1011 char ctypebuf[1024];
1012 char *utf_start;
1014 /* We have to check LC_CTYPE specifically here, because the system
1015 might specify different locales for different categories (why
1016 anyone would want to do this is beyond me). As far as I can
1017 tell, only LC_CTYPE causes OSF Motif to crash. If it turns out
1018 others do, we'll have to iterate over a list of locale cateogries
1019 and patch every one of them. */
1021 ctype = setlocale(LC_CTYPE, NULL);
1023 if (!ctype)
1024 return;
1026 strncpy(ctypebuf, ctype, sizeof ctypebuf);
1028 if ((utf_start = strstr(ctypebuf, ".utf8")) ||
1029 (utf_start = strstr(ctypebuf, ".UTF-8")))
1031 *utf_start = '\0'; /* Samurai chop */
1032 XtWarning("UTF8 locale not supported.");
1033 setlocale(LC_CTYPE, ctypebuf);
1035 #endif
1040 ** Same as the default X language procedure, except we check if Motif can
1041 ** handle the locale as well.
1044 static String neditLanguageProc(Display *dpy, String xnl, XtPointer closure)
1046 /* "xnl" will be set if the user passes in a new language via the
1047 "-xnllanguage" flag. If it's empty, then setlocale will get
1048 the default locale by some system-dependent means (usually,
1049 reading some environment variables). */
1051 if (! setlocale(LC_ALL, xnl)) {
1052 XtWarning("locale not supported by C library, locale unchanged");
1055 patchLocaleForMotif();
1057 if (! XSupportsLocale()) {
1058 XtWarning("locale not supported by Xlib, locale set to C");
1059 setlocale(LC_ALL, "C");
1061 if (! XSetLocaleModifiers(""))
1062 XtWarning("X locale modifiers not supported, using default");
1064 return setlocale(LC_ALL, NULL); /* re-query in case overwritten */
1067 static int sortAlphabetical(const void* k1, const void* k2)
1069 const char* key1 = *(const char**)k1;
1070 const char* key2 = *(const char**)k2;
1071 return strcmp(key1, key2);
1075 * Checks whether a given virtual key binding string is invalid.
1076 * A binding is considered invalid if there are duplicate key entries.
1078 static int virtKeyBindingsAreInvalid(const unsigned char* bindings)
1080 int maxCount = 1, i, count;
1081 const char *pos = (const char*)bindings;
1082 char *copy;
1083 char *pos2, *pos3;
1084 char **keys;
1086 /* First count the number of bindings; bindings are separated by \n
1087 strings. The number of bindings equals the number of \n + 1.
1088 Beware of leading and trailing \n; the number is actually an
1089 upper bound on the number of entries. */
1090 while ((pos = strstr(pos, "\n")))
1092 ++pos;
1093 ++maxCount;
1096 if (maxCount == 1)
1097 return False; /* One binding is always ok */
1099 keys = (char**)malloc(maxCount*sizeof(char*));
1100 copy = XtNewString((const char*)bindings);
1101 i = 0;
1102 pos2 = copy;
1104 count = 0;
1105 while (i<maxCount && pos2 && *pos2)
1107 while (isspace((int) *pos2) || *pos2 == '\n') ++pos2;
1109 if (*pos2 == '!') /* Ignore comment lines */
1111 pos2 = strstr(pos2, "\n");
1112 continue; /* Go to the next line */
1115 if (*pos2)
1117 keys[i++] = pos2;
1118 ++count;
1119 pos3 = strstr(pos2, ":");
1120 if (pos3)
1122 *pos3++ = 0; /* Cut the string and jump to the next entry */
1123 pos2 = pos3;
1125 pos2 = strstr(pos2, "\n");
1129 if (count <= 1)
1131 free(keys);
1132 XtFree(copy);
1133 return False; /* No conflict */
1136 /* Sort the keys and look for duplicates */
1137 qsort((void*)keys, count, sizeof(const char*), sortAlphabetical);
1138 for (i=1; i<count; ++i)
1140 if (!strcmp(keys[i-1], keys[i]))
1142 /* Duplicate detected */
1143 free(keys);
1144 XtFree(copy);
1145 return True;
1148 free(keys);
1149 XtFree(copy);
1150 return False;
1154 * Optionally sanitizes the Motif default virtual key bindings.
1155 * Some applications install invalid bindings (attached to the root window),
1156 * which cause certain keys to malfunction in NEdit.
1157 * Through an X-resource, users can choose whether they want
1158 * - to always keep the existing bindings
1159 * - to override the bindings only if they are invalid
1160 * - to always override the existing bindings.
1163 static Atom virtKeyAtom;
1165 static unsigned char* sanitizeVirtualKeyBindings(void)
1167 int overrideBindings = GetPrefOverrideVirtKeyBindings();
1168 Window rootWindow;
1169 const char *virtKeyPropName = "_MOTIF_DEFAULT_BINDINGS";
1170 Atom dummyAtom;
1171 int getFmt;
1172 unsigned long dummyULong, nItems;
1173 unsigned char *insaneVirtKeyBindings = NULL;
1175 if (overrideBindings == VIRT_KEY_OVERRIDE_NEVER) return NULL;
1177 virtKeyAtom = XInternAtom(TheDisplay, virtKeyPropName, False);
1178 rootWindow = RootWindow(TheDisplay, DefaultScreen(TheDisplay));
1180 /* Remove the property, if it exists; we'll restore it later again */
1181 if (XGetWindowProperty(TheDisplay, rootWindow, virtKeyAtom, 0, INT_MAX,
1182 True, XA_STRING, &dummyAtom, &getFmt, &nItems,
1183 &dummyULong, &insaneVirtKeyBindings) != Success
1184 || nItems == 0)
1186 return NULL; /* No binding yet; nothing to do */
1189 if (overrideBindings == VIRT_KEY_OVERRIDE_AUTO)
1191 if (!virtKeyBindingsAreInvalid(insaneVirtKeyBindings))
1193 /* Restore the property immediately; it seems valid */
1194 XChangeProperty(TheDisplay, rootWindow, virtKeyAtom, XA_STRING, 8,
1195 PropModeReplace, insaneVirtKeyBindings,
1196 strlen((const char*)insaneVirtKeyBindings));
1197 XFree((char*)insaneVirtKeyBindings);
1198 return NULL; /* Prevent restoration */
1201 return insaneVirtKeyBindings;
1205 * NEdit should not mess with the bindings installed by other apps, so we
1206 * just restore whatever was installed, if necessary
1208 static void restoreInsaneVirtualKeyBindings(unsigned char *insaneVirtKeyBindings)
1210 if (insaneVirtKeyBindings)
1212 Window rootWindow = RootWindow(TheDisplay, DefaultScreen(TheDisplay));
1213 /* Restore the root window atom, such that we don't affect
1214 other apps. */
1215 XChangeProperty(TheDisplay, rootWindow, virtKeyAtom, XA_STRING, 8,
1216 PropModeReplace, insaneVirtKeyBindings,
1217 strlen((const char*)insaneVirtKeyBindings));
1218 XFree((char*)insaneVirtKeyBindings);
1222 #ifdef LESSTIF_VERSION
1224 ** Warning handler that suppresses harmless but annoying warnings generated
1225 ** by non-production Lesstif versions.
1227 static void bogusWarningFilter(String message)
1229 const char* bogusMessages[] = {
1230 "XmFontListCreate() is an obsolete function!",
1231 "No type converter registered for 'String' to 'PathMode' conversion.",
1232 "XtRemoveGrab asked to remove a widget not on the list",
1233 NULL };
1234 const char **bogusMessage = &bogusMessages[0];
1236 while (*bogusMessage) {
1237 size_t bogusLen = strlen(*bogusMessage);
1238 if (strncmp(message, *bogusMessage, bogusLen) == 0) {
1239 #ifdef DEBUG_LESSTIF_WARNINGS
1240 /* Developers may want to see which messages are suppressed. */
1241 fprintf(stderr, "[SUPPRESSED] %s\n", message);
1242 #endif
1243 return;
1245 ++bogusMessage;
1248 /* An unknown message. Keep it. */
1249 fprintf(stderr, "%s\n", message);
1251 #endif /* LESSTIF_VERSION */