File: nedit.c

package info (click to toggle)
nedit 1%3A5.5-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 7,124 kB
  • ctags: 6,957
  • sloc: ansic: 92,920; xml: 1,427; yacc: 621; makefile: 342; awk: 40; sh: 10
file content (1222 lines) | stat: -rw-r--r-- 46,728 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
static const char CVSID[] = "$Id: nedit.c,v 1.83 2004/09/02 08:49:56 edg Exp $";
/*******************************************************************************
*									       *
* nedit.c -- Nirvana Editor main program				       *
*									       *
* Copyright (C) 1999 Mark Edel						       *
*									       *
* This is free software; you can redistribute it and/or modify it under the    *
* terms of the GNU General Public License as published by the Free Software    *
* Foundation; either version 2 of the License, or (at your option) any later   *
* version. In addition, you may distribute version of this program linked to   *
* Motif or Open Motif. See README for details.                                 *
*                                                                              *
* This software is distributed in the hope that it will be useful, but WITHOUT *
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        *
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License        *
* for more details.							       *
* 									       *
* You should have received a copy of the GNU General Public License along with *
* software; if not, write to the Free Software Foundation, Inc., 59 Temple     *
* Place, Suite 330, Boston, MA  02111-1307 USA		                       *
*									       *
* Nirvana Text Editor	    						       *
* May 10, 1991								       *
*									       *
* Written by Mark Edel							       *
*									       *
* Modifications:							       *
*									       *
*	8/18/93 - Mark Edel & Joy Kyriakopulos - Ported to VMS		       *
*									       *
*******************************************************************************/

#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#include "nedit.h"
/* #include "textBuf.h" */
#include "file.h"
#include "preferences.h"
#include "regularExp.h"
#include "selection.h"
#include "tags.h"
#include "menu.h"
#include "macro.h"
#include "server.h"
#include "interpret.h"
#include "parse.h"
#include "help.h"
#include "../util/misc.h"
#include "../util/printUtils.h"
#include "../util/fileUtils.h"
#include "../util/getfiles.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <limits.h>

#ifndef NO_XMIM
#include <X11/Xlocale.h>
#else
#include <locale.h>
#endif
#include <X11/Intrinsic.h>
#include <Xm/Xm.h>
#include <Xm/XmP.h>
#if XmVersion >= 1002
#include <Xm/RepType.h>
#endif
#ifdef VMS
#include <rmsdef.h>
#include "../util/VMSparam.h"
#include "../util/VMSUtils.h"
#else
#ifndef __MVS__
#include <sys/param.h>
#endif
#endif /*VMS*/


#ifdef HAVE_DEBUG_H
#include "../debug.h"
#endif

static void nextArg(int argc, char **argv, int *argIndex);
static int checkDoMacroArg(const char *macro);
static String neditLanguageProc(Display *dpy, String xnl, XtPointer closure);
static void maskArgvKeywords(int argc, char **argv, const char **maskArgs);
static void unmaskArgvKeywords(int argc, char **argv, const char **maskArgs);
static void patchResourcesForVisual(void);
static void patchResourcesForKDEbug(void);
static void patchLocaleForMotif(void);
static unsigned char* sanitizeVirtualKeyBindings();
static int sortAlphabetical(const void* k1, const void* k2);
static int virtKeyBindingsAreInvalid(const unsigned char* bindings);
static void restoreInsaneVirtualKeyBindings(unsigned char* bindings);
#ifdef LESSTIF_VERSION
static void bogusWarningFilter(String);
#endif

WindowInfo *WindowList = NULL;
Display *TheDisplay = NULL;
char *ArgV0 = NULL;
Boolean IsServer = False;
Widget TheAppShell;

/* Reasons for choice of default font qualifications:

   iso8859 appears to be necessary for newer versions of XFree86 that
   default to Unicode encoding, which doesn't quite work with Motif.
   Otherwise Motif puts up garbage (square blocks).

   (This of course, is a stupid default because there are far more iso8859
   apps than Unicode apps.  But the X folks insist it's a client bug.  Hah.)

   RedHat 7.3 won't default to '-1' for an encoding, if left with a *,
   and so reverts to "fixed".  Yech. */

#define NEDIT_DEFAULT_FONT      "-*-helvetica-medium-r-normal-*-*-120-*-*-*-iso8859-1," \
                                "-*-helvetica-bold-r-normal-*-*-120-*-*-*-iso8859-1=BOLD," \
                                "-*-helvetica-medium-o-normal-*-*-120-*-*-*-iso8859-1=ITALIC"

#define NEDIT_FIXED_FONT        "-*-courier-medium-r-normal-*-*-120-*-*-*-iso8859-1," \
                                "-*-courier-bold-r-normal-*-*-120-*-*-*-iso8859-1=BOLD," \
                                "-*-courier-medium-o-normal-*-*-120-*-*-*-iso8859-1=ITALIC"

#define NEDIT_DEFAULT_BG        "#b3b3b3"

static char *fallbackResources[] = {
    /* Try to avoid Motif's horrificly ugly default colors and fonts,
       if the user's environment provides no usable defaults.  We try
       to choose a Windows-y default color setting here.  Editable text 
       fields are forced to a fixed-pitch font for usability.

       By using the VendorShell fontList resources, Motif automatically
       groups the fonts into the right classes.  It's then easier for
       the user or environment to override this sensibly:

       nedit -xrm '*textFontList: myfont'
      */

#if (defined(LESSTIF_VERSION) && defined(__CYGWIN__))
    "*FontList: "               NEDIT_DEFAULT_FONT,
    "*XmText.FontList: "        NEDIT_FIXED_FONT,
    "*XmTextField.FontList: "   NEDIT_FIXED_FONT,
    "*XmList.FontList: "        NEDIT_FIXED_FONT,
    "*XmFileSelectionBox*XmList.FontList: " 	 NEDIT_FIXED_FONT,
#else
    "*buttonFontList: "         NEDIT_DEFAULT_FONT,
    "*labelFontList: "          NEDIT_DEFAULT_FONT,
    "*textFontList: "           NEDIT_FIXED_FONT,
#endif

    "*background: "             NEDIT_DEFAULT_BG,
    "*foreground: "             NEDIT_DEFAULT_FG,
    "*XmText.foreground: "      NEDIT_DEFAULT_FG,
    "*XmText.background: "      NEDIT_DEFAULT_TEXT_BG,
    "*XmList.foreground: "      NEDIT_DEFAULT_FG,
    "*XmList.background: "      NEDIT_DEFAULT_TEXT_BG,
    "*XmTextField.foreground: " NEDIT_DEFAULT_FG,
    "*XmTextField.background: " NEDIT_DEFAULT_TEXT_BG,

    "*XmText.translations: #override\\n"
        "Ctrl~Alt~Meta<KeyPress>v: paste-clipboard()\\n"
        "Ctrl~Alt~Meta<KeyPress>c: copy-clipboard()\\n"
        "Ctrl~Alt~Meta<KeyPress>x: cut-clipboard()\\n"
        "Ctrl~Alt~Meta<KeyPress>u: delete-to-start-of-line()\\n",
    "*XmTextField.translations: #override\\n"
        "Ctrl~Alt~Meta<KeyPress>v: paste-clipboard()\\n"
        "Ctrl~Alt~Meta<KeyPress>c: copy-clipboard()\\n"
        "Ctrl~Alt~Meta<KeyPress>x: cut-clipboard()\\n"
        "Ctrl~Alt~Meta<KeyPress>u: delete-to-start-of-line()\\n",

    "*XmLFolder.highlightThickness: 0",
    "*XmLFolder.shadowThickness:    1",
    "*XmLFolder.maxTabWidth:        150",
    "*XmLFolder.traversalOn:        False",
    "*XmLFolder.inactiveForeground: #666" ,
    "*tab.alignment: XmALIGNMENT_BEGINNING",
    "*tab.marginWidth: 0",
    "*tab.marginHeight: 1",

    /* Prevent the file selection box from acting stupid. */
    "*XmFileSelectionBox.resizePolicy: XmRESIZE_NONE",
    "*XmFileSelectionBox.textAccelerators:",

    /* NEdit-specific widgets.  Theses things should probably be manually
       jammed into the database, rather than fallbacks.  We really want
       the accelerators to be there even if someone creates an app-defaults
       file against our wishes. */

    "*text.lineNumForeground: " NEDIT_DEFAULT_LINENO_FG,
    "*text.background: " NEDIT_DEFAULT_TEXT_BG,
    "*text.foreground: " NEDIT_DEFAULT_FG,
    "*text.highlightForeground: " NEDIT_DEFAULT_HI_FG,
    "*text.highlightBackground: " NEDIT_DEFAULT_HI_BG,
    "*textFrame.shadowThickness: 1",
    "*menuBar.marginHeight: 0",
    "*menuBar.shadowThickness: 1",
    "*pane.sashHeight: 11",
    "*pane.sashWidth: 11",
    "*pane.marginWidth: 0",
    "*pane.marginHeight: 0",
    "*scrolledW*spacing: 0",
    "*text.selectionArrayCount: 3",
    "*helpText.background: " NEDIT_DEFAULT_HELP_BG,
    "*helpText.foreground: " NEDIT_DEFAULT_HELP_FG,
    "*helpText.selectBackground: " NEDIT_DEFAULT_BG,
    "*statsLine.background: " NEDIT_DEFAULT_BG,
    "*statsLine.FontList: " NEDIT_DEFAULT_FONT,
    "*calltip.background: LemonChiffon1",
    "*calltip.foreground: black",
    "*iSearchForm*highlightThickness: 1",
    "*fileMenu.tearOffModel: XmTEAR_OFF_ENABLED",
    "*editMenu.tearOffModel: XmTEAR_OFF_ENABLED",
    "*searchMenu.tearOffModel: XmTEAR_OFF_ENABLED",
    "*preferencesMenu.tearOffModel: XmTEAR_OFF_ENABLED",
    "*windowsMenu.tearOffModel: XmTEAR_OFF_ENABLED",
    "*shellMenu.tearOffModel: XmTEAR_OFF_ENABLED",
    "*macroMenu.tearOffModel: XmTEAR_OFF_ENABLED",
    "*helpMenu.tearOffModel: XmTEAR_OFF_ENABLED",
    "*fileMenu.mnemonic: F",
    "*fileMenu.new.accelerator: Ctrl<Key>n",
    "*fileMenu.new.acceleratorText: Ctrl+N",
    "*fileMenu.newOpposite.accelerator: Shift Ctrl<Key>n",
    "*fileMenu.newOpposite.acceleratorText: Shift+Ctrl+N",
    "*fileMenu.open.accelerator: Ctrl<Key>o",
    "*fileMenu.open.acceleratorText: Ctrl+O",
    "*fileMenu.openSelected.accelerator: Ctrl<Key>y",
    "*fileMenu.openSelected.acceleratorText: Ctrl+Y",
    "*fileMenu.close.accelerator: Ctrl<Key>w",
    "*fileMenu.close.acceleratorText: Ctrl+W",
    "*fileMenu.save.accelerator: Ctrl<Key>s",
    "*fileMenu.save.acceleratorText: Ctrl+S",
    "*fileMenu.includeFile.accelerator: Alt<Key>i",
    "*fileMenu.includeFile.acceleratorText: Alt+I",
    "*fileMenu.print.accelerator: Ctrl<Key>p",
    "*fileMenu.print.acceleratorText: Ctrl+P",
    "*fileMenu.exit.accelerator: Ctrl<Key>q",
    "*fileMenu.exit.acceleratorText: Ctrl+Q",
    "*editMenu.mnemonic: E",
    "*editMenu.undo.accelerator: Ctrl<Key>z",
    "*editMenu.undo.acceleratorText: Ctrl+Z",
    "*editMenu.redo.accelerator: Shift Ctrl<Key>z",
    "*editMenu.redo.acceleratorText: Shift+Ctrl+Z",
    "*editMenu.cut.accelerator: Ctrl<Key>x",
    "*editMenu.cut.acceleratorText: Ctrl+X",
    "*editMenu.copy.accelerator: Ctrl<Key>c",
    "*editMenu.copy.acceleratorText: Ctrl+C",
    "*editMenu.paste.accelerator: Ctrl<Key>v",
    "*editMenu.paste.acceleratorText: Ctrl+V",
    "*editMenu.pasteColumn.accelerator: Shift Ctrl<Key>v",
    "*editMenu.pasteColumn.acceleratorText: Ctrl+Shift+V",
    "*editMenu.delete.acceleratorText: Del",
    "*editMenu.selectAll.accelerator: Ctrl<Key>a",
    "*editMenu.selectAll.acceleratorText: Ctrl+A",
    "*editMenu.shiftLeft.accelerator: Ctrl<Key>9",
    "*editMenu.shiftLeft.acceleratorText: [Shift]Ctrl+9",
    "*editMenu.shiftLeftShift.accelerator: Shift Ctrl<Key>9",
    "*editMenu.shiftRight.accelerator: Ctrl<Key>0",
    "*editMenu.shiftRight.acceleratorText: [Shift]Ctrl+0",
    "*editMenu.shiftRightShift.accelerator: Shift Ctrl<Key>0",
    "*editMenu.upperCase.accelerator: Ctrl<Key>6",
    "*editMenu.upperCase.acceleratorText: Ctrl+6",
    "*editMenu.lowerCase.accelerator: Shift Ctrl<Key>6",
    "*editMenu.lowerCase.acceleratorText: Shift+Ctrl+6",
    "*editMenu.fillParagraph.accelerator: Ctrl<Key>j",
    "*editMenu.fillParagraph.acceleratorText: Ctrl+J",
    "*editMenu.insertFormFeed.accelerator: Alt Ctrl<Key>l",
    "*editMenu.insertFormFeed.acceleratorText: Alt+Ctrl+L",
    "*editMenu.insertCtrlCode.accelerator: Alt Ctrl<Key>i",
    "*editMenu.insertCtrlCode.acceleratorText: Alt+Ctrl+I",
    "*searchMenu.mnemonic: S",
    "*searchMenu.find.accelerator: Ctrl<Key>f",
    "*searchMenu.find.acceleratorText: [Shift]Ctrl+F",
    "*searchMenu.findShift.accelerator: Shift Ctrl<Key>f",
    "*searchMenu.findAgain.accelerator: Ctrl<Key>g",
    "*searchMenu.findAgain.acceleratorText: [Shift]Ctrl+G",
    "*searchMenu.findAgainShift.accelerator: Shift Ctrl<Key>g",
    "*searchMenu.findSelection.accelerator: Ctrl<Key>h",
    "*searchMenu.findSelection.acceleratorText: [Shift]Ctrl+H",
    "*searchMenu.findSelectionShift.accelerator: Shift Ctrl<Key>h",
    "*searchMenu.findIncremental.accelerator: Ctrl<Key>i",
    "*searchMenu.findIncrementalShift.accelerator: Shift Ctrl<Key>i",
    "*searchMenu.findIncremental.acceleratorText: [Shift]Ctrl+I",
    "*searchMenu.replace.accelerator: Ctrl<Key>r",
    "*searchMenu.replace.acceleratorText: [Shift]Ctrl+R",
    "*searchMenu.replaceShift.accelerator: Shift Ctrl<Key>r",
    "*searchMenu.findReplace.accelerator: Ctrl<Key>r",
    "*searchMenu.findReplace.acceleratorText: [Shift]Ctrl+R",
    "*searchMenu.findReplaceShift.accelerator: Shift Ctrl<Key>r",
    "*searchMenu.replaceFindAgain.accelerator: Ctrl<Key>t",
    "*searchMenu.replaceFindAgain.acceleratorText: [Shift]Ctrl+T",
    "*searchMenu.replaceFindAgainShift.accelerator: Shift Ctrl<Key>t",
    "*searchMenu.replaceAgain.accelerator: Alt<Key>t",
    "*searchMenu.replaceAgain.acceleratorText: [Shift]Alt+T",
    "*searchMenu.replaceAgainShift.accelerator: Shift Alt<Key>t",
    "*searchMenu.gotoLineNumber.accelerator: Ctrl<Key>l",
    "*searchMenu.gotoLineNumber.acceleratorText: Ctrl+L",
    "*searchMenu.gotoSelected.accelerator: Ctrl<Key>e",
    "*searchMenu.gotoSelected.acceleratorText: Ctrl+E",
    "*searchMenu.mark.accelerator: Alt<Key>m",
    "*searchMenu.mark.acceleratorText: Alt+M a-z",
    "*searchMenu.gotoMark.accelerator: Alt<Key>g",
    "*searchMenu.gotoMark.acceleratorText: [Shift]Alt+G a-z",
    "*searchMenu.gotoMarkShift.accelerator: Shift Alt<Key>g",
    "*searchMenu.gotoMatching.accelerator: Ctrl<Key>m",
    "*searchMenu.gotoMatching.acceleratorText: [Shift]Ctrl+M",
    "*searchMenu.gotoMatchingShift.accelerator: Shift Ctrl<Key>m",
    "*searchMenu.findDefinition.accelerator: Ctrl<Key>d",
    "*searchMenu.findDefinition.acceleratorText: Ctrl+D",
    "*searchMenu.showCalltip.accelerator: Ctrl<Key>apostrophe",
    "*searchMenu.showCalltip.acceleratorText: Ctrl+'",    
    "*preferencesMenu.mnemonic: P",
    "*preferencesMenu.statisticsLine.accelerator: Alt<Key>a",
    "*preferencesMenu.statisticsLine.acceleratorText: Alt+A",
    "*preferencesMenu.overtype.acceleratorText: Insert",
    "*shellMenu.mnemonic: l",
    "*shellMenu.filterSelection.accelerator: Alt<Key>r",
    "*shellMenu.filterSelection.acceleratorText: Alt+R",
    "*shellMenu.executeCommand.accelerator: Alt<Key>x",
    "*shellMenu.executeCommand.acceleratorText: Alt+X",
    "*shellMenu.executeCommandLine.accelerator: Ctrl<Key>KP_Enter",
    "*shellMenu.executeCommandLine.acceleratorText: Ctrl+KP Enter",
    "*shellMenu.cancelShellCommand.accelerator: Ctrl<Key>period",
    "*shellMenu.cancelShellCommand.acceleratorText: Ctrl+.",
    "*macroMenu.mnemonic: c",
    "*macroMenu.learnKeystrokes.accelerator: Alt<Key>k",
    "*macroMenu.learnKeystrokes.acceleratorText: Alt+K",
    "*macroMenu.finishLearn.accelerator: Alt<Key>k",
    "*macroMenu.finishLearn.acceleratorText: Alt+K",
    "*macroMenu.cancelLearn.accelerator: Ctrl<Key>period",
    "*macroMenu.cancelLearn.acceleratorText: Ctrl+.",
    "*macroMenu.replayKeystrokes.accelerator: Ctrl<Key>k",
    "*macroMenu.replayKeystrokes.acceleratorText: Ctrl+K",
    "*macroMenu.repeat.accelerator: Ctrl<Key>comma",
    "*macroMenu.repeat.acceleratorText: Ctrl+,",
    "*windowsMenu.mnemonic: W",
    "*windowsMenu.splitPane.accelerator: Ctrl<Key>2",
    "*windowsMenu.splitPane.acceleratorText: Ctrl+2",
    "*windowsMenu.closePane.accelerator: Ctrl<Key>1",
    "*windowsMenu.closePane.acceleratorText: Ctrl+1",
    "*helpMenu.mnemonic: H",
    "nedit.help.helpForm.sw.helpText*translations: #override\
<Key>Tab:help-focus-buttons()\\n\
<Key>Return:help-button-action(\"close\")\\n\
<KeyPress>osfCancel:help-button-action(\"close\")\\n\
~Meta~Ctrl~Shift<Btn1Down>:\
    grab-focus() help-hyperlink()\\n\
~Meta~Ctrl~Shift<Btn1Up>:\
    help-hyperlink(\"current\", \"process-cancel\", \"extend-end\")\\n\
~Meta~Ctrl~Shift<Btn2Down>:\
    process-bdrag() help-hyperlink()\\n\
~Meta~Ctrl~Shift<Btn2Up>:\
    help-hyperlink(\"new\", \"process-cancel\", \"copy-to\")",
    NULL
};

static const char cmdLineHelp[] =
#ifndef VMS
"Usage:  nedit [-read] [-create] [-line n | +n] [-server] [-do command]\n\
	      [-tags file] [-tabs n] [-wrap] [-nowrap] [-autowrap]\n\
	      [-autoindent] [-noautoindent] [-autosave] [-noautosave]\n\
	      [-lm languagemode] [-rows n] [-columns n] [-font font]\n\
	      [-geometry geometry] [-iconic] [-noiconic] [-svrname name]\n\
	      [-display [host]:server[.screen] [-xrm resourcestring]\n\
	      [-import file] [-background color] [-foreground color]\n\
	      [-tabbed] [-untabbed] [-group] [-V|-version]\n\
	      [--] [file...]\n";
#else
"";
#endif /*VMS*/

int main(int argc, char **argv)
{
    int i, lineNum, nRead, fileSpecified = FALSE, editFlags = CREATE;
    int gotoLine = False, macroFileRead = False, opts = True;
    int iconic=False, tabbed = -1, group = 0, isTabbed;
    char *toDoCommand = NULL, *geometry = NULL, *langMode = NULL;
    char filename[MAXPATHLEN], pathname[MAXPATHLEN];
    XtAppContext context;
    XrmDatabase prefDB;
    WindowInfo *window = NULL, *lastFile = NULL;
    static const char *protectedKeywords[] = {"-iconic", "-icon", "-geometry",
            "-g", "-rv", "-reverse", "-bd", "-bordercolor", "-borderwidth",
	    "-bw", "-title", NULL};
    unsigned char* invalidBindings = NULL;
    
    /* Save the command which was used to invoke nedit for restart command */
    ArgV0 = argv[0];

    /* Set locale for C library, X, and Motif input functions. 
       Reverts to "C" if requested locale not available. */
    XtSetLanguageProc(NULL, neditLanguageProc, NULL);
 
    /* Initialize X toolkit (does not open display yet) */
    XtToolkitInitialize();
    context = XtCreateApplicationContext();
    
    /* Set up a warning handler to trap obnoxious Xt grab warnings */
    SuppressPassiveGrabWarnings();

#ifdef LESSTIF_VERSION
    /* Set up a handler to suppress bogus Lesstif warning messages */
    XtAppSetWarningHandler(context, bogusWarningFilter);
#endif
    
    /* Set up default resources if no app-defaults file is found */
    XtAppSetFallbackResources(context, fallbackResources);
    
#if XmVersion >= 1002
    /* Allow users to change tear off menus with X resources */
    XmRepTypeInstallTearOffModelConverter();
#endif
    
#ifdef VMS
    /* Convert the command line to Unix style (This is not an ideal solution) */
    ConvertVMSCommandLine(&argc, &argv);
#endif /*VMS*/
#ifdef __EMX__
    /* expand wildcards if necessary */
    _wildcard(&argc, &argv);
#endif
    
    /* Read the preferences file and command line into a database */
    prefDB = CreateNEditPrefDB(&argc, argv);

    /* Open the display and read X database and remaining command line args.
       XtOpenDisplay must be allowed to process some of the resource arguments
       with its inaccessible internal option table, but others, like -geometry
       and -iconic are per-window and it should not be allowed to consume them,
       so we temporarily masked them out. */
    maskArgvKeywords(argc, argv, protectedKeywords);
    TheDisplay = XtOpenDisplay (context, NULL, APP_NAME, APP_CLASS,
	    NULL, 0, &argc, argv);
    unmaskArgvKeywords(argc, argv, protectedKeywords);
    if (!TheDisplay) {
        /* Respond to -V or -version even if there is no display */
        for (i = 1; i < argc && strcmp(argv[i], "--"); i++)
        {
            if (0 == strcmp(argv[i], "-V") || 0 == strcmp(argv[i], "-version"))
            {
                PrintVersion();
                exit(EXIT_SUCCESS);
            }
        }
	XtWarning ("NEdit: Can't open display\n");
	exit(EXIT_FAILURE);
    }
    
    /* Must be done before creating widgets */
    patchResourcesForVisual();
    patchResourcesForKDEbug();
    
    /* Initialize global symbols and subroutines used in the macro language */
    InitMacroGlobals();
    RegisterMacroSubroutines();

    /* Store preferences from the command line and .nedit file, 
       and set the appropriate preferences */
    RestoreNEditPrefs(prefDB, XtDatabase(TheDisplay));

    /* Intercept syntactically invalid virtual key bindings BEFORE we 
       create any shells. */
    invalidBindings = sanitizeVirtualKeyBindings();
    
    /* Create a hidden application shell that is the parent of all the
       main editor windows.  Realize it so it the window can act as 
       group leader. */
    TheAppShell = CreateShellWithBestVis(APP_NAME, 
                                         APP_CLASS,
                                         applicationShellWidgetClass,
                                         TheDisplay,
                                         NULL,
                                         0);
    
    /* Restore the original bindings ASAP such that other apps are not affected. */
    restoreInsaneVirtualKeyBindings(invalidBindings);

    XtSetMappedWhenManaged(TheAppShell, False);
    XtRealizeWidget(TheAppShell);

#ifndef NO_SESSION_RESTART
    AttachSessionMgrHandler(TheAppShell);
#endif

    /* More preference stuff */
    LoadPrintPreferences(XtDatabase(TheDisplay), APP_NAME, APP_CLASS, True);
    SetDeleteRemap(GetPrefMapDelete());
    SetPointerCenteredDialogs(GetPrefRepositionDialogs());
    SetGetEFTextFieldRemoval(!GetPrefStdOpenDialog());
    
    /* Set up action procedures for menu item commands */
    InstallMenuActions(context);
    
    /* Add Actions for following hyperlinks in the help window */
    InstallHelpLinkActions(context);
    /* Add actions for mouse wheel support in scrolled windows (except text
       area) */
    InstallMouseWheelActions(context);
    
    /* Install word delimiters for regular expression matching */
    SetREDefaultWordDelimiters(GetPrefDelimiters());
    
    /* Read the nedit dynamic database of files for the Open Previous
       command (and eventually other information as well) */
    ReadNEditDB();
    
    /* Process -import command line argument before others which might
       open windows (loading preferences doesn't update menu settings,
       which would then be out of sync with the real preference settings) */
    for (i=1; i<argc; i++) {
      	if(!strcmp(argv[i], "--")) {
	    break; /* treat all remaining arguments as filenames */
	} else if (!strcmp(argv[i], "-import")) {
    	    nextArg(argc, argv, &i);
    	    ImportPrefFile(argv[i], False);
	} else if (!strcmp(argv[i], "-importold")) {
    	    nextArg(argc, argv, &i);
    	    ImportPrefFile(argv[i], True);
	}
    }
    
    /* Load the default tags file. Don't complain if it doesn't load, the tag
       file resource is intended to be set and forgotten.  Running nedit in a
       directory without a tags should not cause it to spew out errors. */
    if (*GetPrefTagFile() != '\0') {
        AddTagsFile(GetPrefTagFile(), TAG);
    }

    if (strcmp(GetPrefServerName(), "") != 0) {
        IsServer = True;
    }

    /* Process any command line arguments (-tags, -do, -read, -create,
       +<line_number>, -line, -server, and files to edit) not already
       processed by RestoreNEditPrefs. */
    fileSpecified = FALSE;
    for (i=1; i<argc; i++) {
        if (opts && !strcmp(argv[i], "--")) { 
    	    opts = False; /* treat all remaining arguments as filenames */
	    continue;
	} else if (opts && !strcmp(argv[i], "-tags")) {
    	    nextArg(argc, argv, &i);
    	    if (!AddTagsFile(argv[i], TAG))
    	    	fprintf(stderr, "NEdit: Unable to load tags file\n");
    	} else if (opts && !strcmp(argv[i], "-do")) {
    	    nextArg(argc, argv, &i);
	    if (checkDoMacroArg(argv[i]))
    	    	toDoCommand = argv[i];
    	} else if (opts && !strcmp(argv[i], "-read")) {
    	    editFlags |= PREF_READ_ONLY;
    	} else if (opts && !strcmp(argv[i], "-create")) {
    	    editFlags |= SUPPRESS_CREATE_WARN;
    	} else if (opts && !strcmp(argv[i], "-tabbed")) {
    	    tabbed = 1;
    	    group = 0;	/* override -group option */
    	} else if (opts && !strcmp(argv[i], "-untabbed")) {
    	    tabbed = 0;
    	    group = 0;	/* override -group option */
    	} else if (opts && !strcmp(argv[i], "-group")) {
    	    group = 2; /* 2: start new group, 1: in group */
    	} else if (opts && !strcmp(argv[i], "-line")) {
    	    nextArg(argc, argv, &i);
	    nRead = sscanf(argv[i], "%d", &lineNum);
	    if (nRead != 1)
    		fprintf(stderr, "NEdit: argument to line should be a number\n");
    	    else
    	    	gotoLine = True;
    	} else if (opts && (*argv[i] == '+')) {
    	    nRead = sscanf((argv[i]+1), "%d", &lineNum);
	    if (nRead != 1)
    		fprintf(stderr, "NEdit: argument to + should be a number\n");
    	    else
    	    	gotoLine = True;
    	} else if (opts && !strcmp(argv[i], "-server")) {
    	    IsServer = True;
	} else if (opts && (!strcmp(argv[i], "-iconic") || 
	                    !strcmp(argv[i], "-icon"))) {
    	    iconic = True;
	} else if (opts && !strcmp(argv[i], "-noiconic")) {
    	    iconic = False;
	} else if (opts && (!strcmp(argv[i], "-geometry") || 
	                    !strcmp(argv[i], "-g"))) {
	    nextArg(argc, argv, &i);
    	    geometry = argv[i];
	} else if (opts && !strcmp(argv[i], "-lm")) {
	    nextArg(argc, argv, &i);
    	    langMode = argv[i];
	} else if (opts && !strcmp(argv[i], "-import")) {
	    nextArg(argc, argv, &i); /* already processed, skip */
	} else if (opts && (!strcmp(argv[i], "-V") || 
	                    !strcmp(argv[i], "-version"))) {
	    PrintVersion();
	    exit(EXIT_SUCCESS);
	} else if (opts && (*argv[i] == '-')) {
#ifdef VMS
	    *argv[i] = '/';
#endif /*VMS*/
    	    fprintf(stderr, "NEdit: Unrecognized option %s\n%s", argv[i],
    	    	    cmdLineHelp);
    	    exit(EXIT_FAILURE);
    	} else {
#ifdef VMS
	    int numFiles, j;
	    char **nameList = NULL;
	    /* Use VMS's LIB$FILESCAN for filename in argv[i] to process */
	    /* wildcards and to obtain a full VMS file specification     */
	    numFiles = VMSFileScan(argv[i], &nameList, NULL, INCLUDE_FNF);
	    /* for each expanded file name do: */
	    for (j = 0; j < numFiles; ++j) {
	    	if (ParseFilename(nameList[j], filename, pathname) == 0) {
		    /* determine if file is to be openned in new tab, by
		       factoring the options -group, -tabbed & -untabbed */
    		    if (group == 2) {
	        	isTabbed = 0;  /* start a new window for new group */
			group = 1;     /* next file will be within group */
		    } else if (group == 1) {
	    		isTabbed = 1;  /* new tab for file in group */
		    } else {           /* not in group */
	    		isTabbed = tabbed==-1? GetPrefOpenInTab() : tabbed; 
		    }

		    /* Files are opened in background to improve opening speed
		       by defering certain time  consuiming task such as syntax
		       highlighting. At the end of the file-opening loop, the 
		       last file opened will be raised to restore those deferred
		       items. The current file may also be raised if there're
		       macros to execute on. */
		    window = EditExistingFile(WindowList, filename, pathname, 
		    	    editFlags, geometry, iconic, langMode, isTabbed, 
			    True);
		    fileSpecified = TRUE;

		    if (window) {
			CleanUpTabBarExposeQueue(window);

			/* raise the last file of previous window */
			if (lastFile && window->shell != lastFile->shell) {
			    CleanUpTabBarExposeQueue(lastFile);
			    RaiseDocument(lastFile);
			}
			
			if (!macroFileRead) {
		            ReadMacroInitFile(WindowList);
		            macroFileRead = True;
			}
	    		if (gotoLine)
	    	            SelectNumberedLine(window, lineNum);
			if (toDoCommand != NULL) {
			    DoMacro(window, toDoCommand, "-do macro");
			    toDoCommand = NULL;
			    if (!IsValidWindow(window))
		    		window = NULL; /* window closed by macro */
			    if (lastFile && !IsValidWindow(lastFile))
		    		lastFile = NULL; /* window closed by macro */
			}
		    }
		    
		    /* register last opened file for later use */
		    if (window)
    	    		lastFile = window;
                } else {
		    fprintf(stderr, "nedit: file name too long: %s\n", nameList[j]);
                }
		free(nameList[j]);
	    }
	    if (nameList != NULL)
	    	free(nameList);
#else
	    if (ParseFilename(argv[i], filename, pathname) == 0 ) {
		/* determine if file is to be openned in new tab, by
		   factoring the options -group, -tabbed & -untabbed */
    		if (group == 2) {
	            isTabbed = 0;  /* start a new window for new group */
		    group = 1;     /* next file will be within group */
		} else if (group == 1) {
	    	    isTabbed = 1;  /* new tab for file in group */
		} else {           /* not in group */
	    	    isTabbed = tabbed==-1? GetPrefOpenInTab() : tabbed; 
		}
		
		/* Files are opened in background to improve opening speed
		   by defering certain time  consuiming task such as syntax
		   highlighting. At the end of the file-opening loop, the 
		   last file opened will be raised to restore those deferred
		   items. The current file may also be raised if there're
		   macros to execute on. */
		window = EditExistingFile(WindowList, filename, pathname, 
		    	editFlags, geometry, iconic, langMode, isTabbed, True);
    	    	fileSpecified = TRUE;
		if (window) {
		    CleanUpTabBarExposeQueue(window);

		    /* raise the last tab of previous window */
		    if (lastFile && window->shell != lastFile->shell) {
			CleanUpTabBarExposeQueue(lastFile);
			RaiseDocument(lastFile);
		    }

		    if (!macroFileRead) {
			ReadMacroInitFile(WindowList);
			macroFileRead = True;
		    }
		    if (gotoLine)
			SelectNumberedLine(window, lineNum);
		    if (toDoCommand != NULL) {
			DoMacro(window, toDoCommand, "-do macro");
	    	    	toDoCommand = NULL;
			if (!IsValidWindow(window))
		    	    window = NULL; /* window closed by macro */
			if (lastFile && !IsValidWindow(lastFile))
		    	    lastFile = NULL; /* window closed by macro */
		    }
		}
		
		/* register last opened file for later use */
		if (window)
    	    	    lastFile = window;
	    } else {
		fprintf(stderr, "nedit: file name too long: %s\n", argv[i]);
	    }
#endif /*VMS*/
	}
    }
#ifdef VMS
    VMSFileScanDone();
#endif /*VMS*/
    
    /* Raise the last file opened */
    if (lastFile) {
	CleanUpTabBarExposeQueue(lastFile);
	RaiseDocument(lastFile);
    }
    CheckCloseDim();

    /* If no file to edit was specified, open a window to edit "Untitled" */
    if (!fileSpecified) {
    	EditNewFile(NULL, geometry, iconic, langMode, NULL);
	ReadMacroInitFile(WindowList);
	CheckCloseDim();
	if (toDoCommand != NULL)
	    DoMacro(WindowList, toDoCommand, "-do macro");
    }
    
    /* Begin remembering last command invoked for "Repeat" menu item */
    AddLastCommandActionHook(context);

    /* Set up communication port and write ~/.nedit_server_process file */
    if (IsServer)
    	InitServerCommunication();

    /* Process events. */
    if (IsServer)
    	ServerMainLoop(context);
    else
    	XtAppMainLoop(context);

    /* Not reached but this keeps some picky compilers happy */
    return EXIT_SUCCESS;
}

static void nextArg(int argc, char **argv, int *argIndex)
{
    if (*argIndex + 1 >= argc) {
#ifdef VMS
	    *argv[*argIndex] = '/';
#endif /*VMS*/
    	fprintf(stderr, "NEdit: %s requires an argument\n%s", argv[*argIndex],
    	        cmdLineHelp);
    	exit(EXIT_FAILURE);
    }
    (*argIndex)++;
}

/*
** Return True if -do macro is valid, otherwise write an error on stderr
*/
static int checkDoMacroArg(const char *macro)
{
    Program *prog;
    char *errMsg, *stoppedAt, *tMacro;
    int macroLen;
    
    /* Add a terminating newline (which command line users are likely to omit
       since they are typically invoking a single routine) */
    macroLen = strlen(macro);
    tMacro = XtMalloc(strlen(macro)+2);
    strncpy(tMacro, macro, macroLen);
    tMacro[macroLen] = '\n';
    tMacro[macroLen+1] = '\0';
    
    /* Do a test parse */
    prog = ParseMacro(tMacro, &errMsg, &stoppedAt);
    if (prog == NULL) {
    	ParseError(NULL, tMacro, stoppedAt, "argument to -do", errMsg);
	return False;
    }
    FreeProgram(prog);
    return True;
}

/*
** maskArgvKeywords and unmaskArgvKeywords mangle selected keywords by
** replacing the '-' with a space, for the purpose of hiding them from
** XtOpenDisplay's option processing.  Why this silly scheme?  XtOpenDisplay
** really needs to see command line arguments, particularly -display, but
** there's no way to change the option processing table it uses, to keep
** it from consuming arguments which are meant to apply per-window, like
** -geometry and -iconic.
*/
static void maskArgvKeywords(int argc, char **argv, const char **maskArgs)
{
    int i, k;

    for (i=1; i<argc; i++)
	for (k=0; maskArgs[k]!=NULL; k++)
	    if (!strcmp(argv[i], maskArgs[k]))
    		argv[i][0] = ' ';
}


static void unmaskArgvKeywords(int argc, char **argv, const char **maskArgs)
{
    int i, k;

    for (i=1; i<argc; i++)
	for (k=0; maskArgs[k]!=NULL; k++)
	    if (argv[i][0]==' ' && !strcmp(&argv[i][1], &maskArgs[k][1]))
    		argv[i][0] = '-';
}

/*
** If we're not using the default visual, then some default resources in
** the database (colors) are not valid, because they are indexes into the
** default colormap.  If we used them blindly, then we'd get "random"
** unreadable colors.  So we inspect the resource list, and use the
** fallback "grey" color instead if this is the case.
*/
static void patchResourcesForVisual(void)
{
    Cardinal    i;
    Visual     *visual;
    int         depth;
    Colormap    map;
    Boolean     usingDefaultVisual;
    XrmDatabase db;
    
    if (!TheDisplay)
        return;

    db = XtDatabase(TheDisplay);
    
    usingDefaultVisual = FindBestVisual(TheDisplay,
                                        APP_NAME,
                                        APP_CLASS,
                                        &visual,
                                        &depth,
                                        &map);

    if (!usingDefaultVisual)
    {
#ifndef LESSTIF_VERSION
        /* Drag-and-drop visuals do not work well when using a different
           visual.  One some systems, you'll just get a funny-looking icon
           (maybe all-black) but on other systems it crashes with a BadMatch
           error.  This appears to be an OSF Motif bug.  It would be nicer 
           to just disable the visual itself, instead of the entire drag
           operation. */

        XrmPutStringResource(&db, "*dragInitiatorProtocolStyle", "DRAG_NONE");
#endif

        for (i = 1; i < XtNumber(fallbackResources); ++i)
        {
            Cardinal resIndex = i - 1;
            
            if (strstr(fallbackResources[resIndex], "*background:") ||
                strstr(fallbackResources[resIndex], "*foreground:"))
            {
                /* Qualify by application name to prevent them from being
                   converted against the wrong colormap. */
                char buf[1024] = "*" APP_NAME;
                strcat(buf, fallbackResources[resIndex]);
                XrmPutLineResource(&db, buf);
            }
        }
    }
}

/*
** Several KDE version (2.x and 3.x) ship with a template application-default 
** file for NEdit in which several strings have to be substituted in order to 
** make it a valid .ad file. However, for some reason (a KDE bug?), the
** template sometimes ends up in the resource db unmodified, such that several
** invalid entries are present. This function checks for the presence of such
** invalid entries and silently replaces them with NEdit's default values where
** necessary. Without this, NEdit will typically write several warnings to 
** the terminal (Cannot convert string "FONTLIST" to type FontStruct etc) and
** fall back on some really ugly colors and fonts.
*/
static void patchResourcesForKDEbug(void)
{
    /*
     * These are the resources found the Nedit.ad template shipped with KDE 3.0.
     */
    static const char* buggyResources[][3] = { 
     { "*fontList",               "FONTLIST",          NEDIT_DEFAULT_FONT     }, 
     { "*XmText.background",      "BACKGROUND",        NEDIT_DEFAULT_TEXT_BG  }, 
     { "*XmText.foreground",      "FOREGROUND",        NEDIT_DEFAULT_FG       }, 
     { "*XmTextField.background", "BACKGROUND",        NEDIT_DEFAULT_TEXT_BG  }, 
     { "*XmTextField.foreground", "FOREGROUND",        NEDIT_DEFAULT_FG       }, 
     { "*XmList.background",      "BACKGROUND",        NEDIT_DEFAULT_TEXT_BG  }, 
     { "*XmList.foreground",      "FOREGROUND",        NEDIT_DEFAULT_FG       }, 
     { "*helpText.background",    "BACKGROUND",        NEDIT_DEFAULT_HELP_BG  },
     { "*helpText.foreground",    "FOREGROUND",        NEDIT_DEFAULT_HELP_FG  },
     { "*background",             "BACKGROUND",        NEDIT_DEFAULT_BG       },
     { "*foreground",             "FOREGROUND",        NEDIT_DEFAULT_FG,      },
     { "*selectColor",            "BACKGROUND",        NEDIT_DEFAULT_SEL_BG   },
     { "*highlightColor",         "BACKGROUND",        NEDIT_DEFAULT_HI_BG    },
     { "*text.background",        "WINDOW_BACKGROUND", NEDIT_DEFAULT_TEXT_BG  },
     { "*text.foreground",        "WINDOW_FOREGROUND", NEDIT_DEFAULT_FG       },
     { "*text.selectBackground",  "SELECT_BACKGROUND", NEDIT_DEFAULT_SEL_BG   },
     { "*text.selectForeground",  "SELECT_FOREGROUND", NEDIT_DEFAULT_SEL_FG   },
     { "*text.cursorForeground",  "WINDOW_FOREGROUND", NEDIT_DEFAULT_CURSOR_FG},
  /* { "*remapDeleteKey",         "False",                                    }, OK */
  /* { "!*text.heavyCursor",      "True"                                      }, OK */
  /* { "!*BlinkRate",             "0"                                         }, OK */
  /* { "*shell",                  "/bin/sh"                                   }, OK */
     { "*statsLine.background",   "BACKGROUND",        NEDIT_DEFAULT_BG       },
     { "*statsLine.foreground",   "FOREGROUND",        NEDIT_DEFAULT_FG       },
     { NULL,                      NULL,                NULL                   } };
    XrmDatabase db;
    int i;
    
    if (!TheDisplay)
        return;

    db = XtDatabase(TheDisplay);
    
    i = 0;
    while (buggyResources[i][0])
    {
        const char* resource = buggyResources[i][0];
        const char* buggyValue = buggyResources[i][1];
        const char* defaultValue = buggyResources[i][2];
        char name[128] = APP_NAME;
        char class[128] = APP_CLASS;
        char* type;
        XrmValue resValue;
        
        strcat(name, resource);
        strcat(class, resource); /* Is this ok ? */
        
        if (XrmGetResource(db, name, class, &type, &resValue) &&
            !strcmp(type, XmRString))
        {
            /* Buggy value? Replace by the default. */
            if (!strcmp(resValue.addr, buggyValue))
            {
                XrmPutStringResource(&db, &name[0], (char*)defaultValue);
            }
        }    
        ++i;
    }
}

/*
** It seems OSF Motif cannot handle locales with UTF-8 at the end, crashing
** in various places.  The easiest one to find is to open the File Open
** dialog box.  So we lop off UTF-8 if it's there and continue.  Newer 
** versions of Linux distros (e.g., RedHat 8) set the default language to
** to have "UTF-8" at the end, so users were seeing these crashes.
*/
static void patchLocaleForMotif()
{
#ifndef LESSTIF_VERSION
    const char *ctype;
    char ctypebuf[1024];
    char *utf_start;
    
    /* We have to check LC_CTYPE specifically here, because the system
       might specify different locales for different categories (why
       anyone would want to do this is beyond me).  As far as I can
       tell, only LC_CTYPE causes OSF Motif to crash.  If it turns out
       others do, we'll have to iterate over a list of locale cateogries
       and patch every one of them. */
       
    ctype = setlocale(LC_CTYPE, NULL);
    
    if (!ctype)
        return;

    strncpy(ctypebuf, ctype, sizeof ctypebuf);

    if ((utf_start = strstr(ctypebuf, ".utf8")) || 
        (utf_start = strstr(ctypebuf, ".UTF-8")))
    {
        *utf_start = '\0'; /* Samurai chop */
        XtWarning("UTF8 locale not supported.");
        setlocale(LC_CTYPE, ctypebuf);
    }
#endif
}


/*
** Same as the default X language procedure, except we check if Motif can
** handle the locale as well.
*/

static String neditLanguageProc(Display *dpy, String xnl, XtPointer closure)
{
    /* "xnl" will be set if the user passes in a new language via the
       "-xnllanguage" flag.  If it's empty, then setlocale will get
       the default locale by some system-dependent means (usually, 
       reading some environment variables). */

    if (! setlocale(LC_ALL, xnl)) {
        XtWarning("locale not supported by C library, locale unchanged");
    }

    patchLocaleForMotif();

    if (! XSupportsLocale()) {
        XtWarning("locale not supported by Xlib, locale set to C");
        setlocale(LC_ALL, "C");
    }
    if (! XSetLocaleModifiers(""))
        XtWarning("X locale modifiers not supported, using default");

    return setlocale(LC_ALL, NULL); /* re-query in case overwritten */
}

static int sortAlphabetical(const void* k1, const void* k2)
{
    const char* key1 = *(const char**)k1;
    const char* key2 = *(const char**)k2;
    return strcmp(key1, key2);
}

/*
 * Checks whether a given virtual key binding string is invalid. 
 * A binding is considered invalid if there are duplicate key entries.
 */
static int virtKeyBindingsAreInvalid(const unsigned char* bindings)
{
    int maxCount = 1, i, count;
    const char  *pos = (const char*)bindings;
    char *copy;
    char *pos2, *pos3;
    char **keys;

    /* First count the number of bindings; bindings are separated by \n
       strings. The number of bindings equals the number of \n + 1.
       Beware of leading and trailing \n; the number is actually an
       upper bound on the number of entries. */
    while ((pos = strstr(pos, "\n"))) 
    { 
        ++pos; 
        ++maxCount;
    }
    
    if (maxCount == 1) 
        return False; /* One binding is always ok */
    
    keys = (char**)malloc(maxCount*sizeof(char*));
    copy = XtNewString((const char*)bindings);
    i = 0;
    pos2 = copy;
    
    count = 0;
    while (i<maxCount && pos2 && *pos2)
    {
        while (isspace((int) *pos2) || *pos2 == '\n') ++pos2;
        
        if (*pos2 == '!') /* Ignore comment lines */
        {
            pos2 = strstr(pos2, "\n");
            continue; /* Go to the next line */
        }

        if (*pos2)
        {
            keys[i++] = pos2;
            ++count;
            pos3 = strstr(pos2, ":");
            if (pos3) 
            {
                *pos3++ = 0; /* Cut the string and jump to the next entry */
                pos2 = pos3;
            }
            pos2 = strstr(pos2, "\n");
        }
    }
    
    if (count <= 1)
    {
       free(keys);
       XtFree(copy);
       return False; /* No conflict */
    }
    
    /* Sort the keys and look for duplicates */
    qsort((void*)keys, count, sizeof(const char*), sortAlphabetical);
    for (i=1; i<count; ++i)
    {
        if (!strcmp(keys[i-1], keys[i]))
        {
            /* Duplicate detected */
            free(keys);
            XtFree(copy);
            return True;
        }
    }
    free(keys);
    XtFree(copy);
    return False;
}

/*
 * Optionally sanitizes the Motif default virtual key bindings. 
 * Some applications install invalid bindings (attached to the root window),
 * which cause certain keys to malfunction in NEdit.
 * Through an X-resource, users can choose whether they want
 *   - to always keep the existing bindings
 *   - to override the bindings only if they are invalid
 *   - to always override the existing bindings.
 */

static Atom virtKeyAtom;

static unsigned char* sanitizeVirtualKeyBindings()
{
    int overrideBindings = GetPrefOverrideVirtKeyBindings();
    Window rootWindow;
    const char *virtKeyPropName = "_MOTIF_DEFAULT_BINDINGS";
    Atom dummyAtom;
    int getFmt;
    unsigned long dummyULong, nItems;
    unsigned char *insaneVirtKeyBindings = NULL;
    
    if (overrideBindings == VIRT_KEY_OVERRIDE_NEVER) return NULL;
    
    virtKeyAtom =  XInternAtom(TheDisplay, virtKeyPropName, False);
    rootWindow = RootWindow(TheDisplay, DefaultScreen(TheDisplay));

    /* Remove the property, if it exists; we'll restore it later again */
    if (XGetWindowProperty(TheDisplay, rootWindow, virtKeyAtom, 0, INT_MAX, 
                           True, XA_STRING, &dummyAtom, &getFmt, &nItems, 
                           &dummyULong, &insaneVirtKeyBindings) != Success 
        || nItems == 0) 
    {
        return NULL; /* No binding yet; nothing to do */
    }
    
    if (overrideBindings == VIRT_KEY_OVERRIDE_AUTO)
    {   
        if (!virtKeyBindingsAreInvalid(insaneVirtKeyBindings))
        {
            /* Restore the property immediately; it seems valid */
            XChangeProperty(TheDisplay, rootWindow, virtKeyAtom, XA_STRING, 8,
                            PropModeReplace, insaneVirtKeyBindings, 
                            strlen((const char*)insaneVirtKeyBindings));
            XFree((char*)insaneVirtKeyBindings);
            return NULL; /* Prevent restoration */
        }
    }
    return insaneVirtKeyBindings;
}

/*
 * NEdit should not mess with the bindings installed by other apps, so we
 * just restore whatever was installed, if necessary
 */
static void restoreInsaneVirtualKeyBindings(unsigned char *insaneVirtKeyBindings)
{
   if (insaneVirtKeyBindings)
   {
      Window rootWindow = RootWindow(TheDisplay, DefaultScreen(TheDisplay));
      /* Restore the root window atom, such that we don't affect 
         other apps. */
      XChangeProperty(TheDisplay, rootWindow, virtKeyAtom, XA_STRING, 8,
                      PropModeReplace, insaneVirtKeyBindings, 
                      strlen((const char*)insaneVirtKeyBindings));
      XFree((char*)insaneVirtKeyBindings);
   }
}

#ifdef LESSTIF_VERSION
/*
** Warning handler that suppresses harmless but annoying warnings generated
** by non-production Lesstif versions.
*/
static void bogusWarningFilter(String message)
{
  const char* bogusMessages[] = {
    "XmFontListCreate() is an obsolete function!",
    "No type converter registered for 'String' to 'PathMode' conversion.",
    "XtRemoveGrab asked to remove a widget not on the list",
    NULL };
  const char **bogusMessage = &bogusMessages[0]; 
  
  while (*bogusMessage) {
      size_t bogusLen = strlen(*bogusMessage);
      if (strncmp(message, *bogusMessage, bogusLen) == 0) {
#ifdef DEBUG_LESSTIF_WARNINGS
         /* Developers may want to see which messages are suppressed. */
         fprintf(stderr, "[SUPPRESSED] %s\n", message);
#endif        
         return;
      }
      ++bogusMessage;
  }
  
  /* An unknown message. Keep it. */
  fprintf(stderr, "%s\n", message);
}
#endif /* LESSTIF_VERSION */