fix macro_hooks patch
[nedit-bw.git] / langmoderegex.patch
blob2b164e63b691bf23d29ace22c3de946f1776af84
1 From: Arne Foerlie <arne@forlie.com>
2 Subject: regex to detect language mode
4 The following patch extends the language mode detection mechanism to allow
5 use of regular expressions. It provides backward compatibility with the old
6 format by requiring that the regex is enclosed in single quotes.
8 E.g.:
10 .mk '[m|M]akefile*'
12 will match common ways of naming make files.
14 ---
16 source/preferences.c | 64 +++++++++++++++++++++++++++++++--------------------
17 1 file changed, 39 insertions(+), 25 deletions(-)
19 diff --quilt old/source/preferences.c new/source/preferences.c
20 --- old/source/preferences.c
21 +++ new/source/preferences.c
22 @@ -3213,7 +3213,9 @@ the list on the left. To add a new lang
24 extLbl = XtVaCreateManagedWidget("extLbl", xmLabelGadgetClass, form,
25 XmNlabelString,
26 - s1=XmStringCreateSimple("File extensions (separate w/ space)"),
27 + s1=MKSTRING(
28 +"File extensions or regexes (in single quotes)\n\
29 +(separate w/ space)"),
30 XmNmnemonic, 'F',
31 XmNalignment, XmALIGNMENT_BEGINNING,
32 XmNleftAttachment, XmATTACH_POSITION,
33 @@ -4815,47 +4817,59 @@ static int matchLanguageMode(WindowInfo
35 /*... look for an explicit mode statement first */
37 - /* Do a regular expression search on for recognition pattern */
38 - first200 = BufGetRange(window->buffer, 0, 200);
39 - for (i=0; i<NLanguageModes; i++) {
40 - if (LanguageModes[i]->recognitionExpr != NULL) {
41 - if (SearchString(first200, LanguageModes[i]->recognitionExpr,
42 - SEARCH_FORWARD, SEARCH_REGEX, False, 0, &beginPos,
43 - &endPos, NULL, NULL, NULL))
44 - {
45 - XtFree(first200);
46 - return i;
47 - }
48 - }
49 - }
50 - XtFree(first200);
52 /* Look at file extension ("@@/" starts a ClearCase version extended path,
53 which gets appended after the file extension, and therefore must be
54 stripped off to recognize the extension to make ClearCase users happy) */
55 fileNameLen = strlen(window->filename);
56 #ifdef VMS
57 if (strchr(window->filename, ';') != NULL)
58 - fileNameLen = strchr(window->filename, ';') - window->filename;
59 + fileNameLen = strchr(window->filename, ';') - window->filename;
60 #else
61 if ((versionExtendedPath = GetClearCaseVersionExtendedPath(window->filename)) != NULL)
62 fileNameLen = versionExtendedPath - window->filename;
63 #endif
64 for (i=0; i<NLanguageModes; i++) {
65 - for (j=0; j<LanguageModes[i]->nExtensions; j++) {
66 - ext = LanguageModes[i]->extensions[j];
67 - extLen = strlen(ext);
68 - start = fileNameLen - extLen;
69 -#if defined(__VMS) && (__VMS_VER >= 70200000)
70 - /* VMS v7.2 has case-preserving filenames */
71 + for (j=0; j<LanguageModes[i]->nExtensions; j++) {
72 + ext = LanguageModes[i]->extensions[j];
73 + extLen = strlen(ext);
74 + if (ext[0] == '\'' && ext[extLen - 1] == '\'') {
75 + char* regex = XtMalloc(extLen);
76 + /* copy everything except the single quotes into regex */
77 + strcpy(regex, &ext[1]);
78 + regex[extLen - 2] = '\0';
79 + if (regexFind(window->filename, regex)) {
80 + XtFree(regex);
81 + return i;
82 + } else {
83 + XtFree(regex);
84 + }
85 + } else {
86 + start = fileNameLen - extLen;
87 +#if defined(__VMS) && (__VMS_VER >= 70200000)
88 + /* VMS v7.2 has case-preserving filenames */
89 if (start >= 0 && !strncasecmp(&window->filename[start], ext, extLen))
90 return i;
91 #else
92 - if (start >= 0 && !strncmp(&window->filename[start], ext, extLen))
93 + if (start >= 0 && !strncmp(&window->filename[start], ext, extLen))
94 return i;
95 #endif
96 - }
97 + }
98 + }
99 + }
101 + /* Do a regular expression search on for recognition pattern */
102 + first200 = BufGetRange(window->buffer, 0, 200);
103 + for (i=0; i<NLanguageModes; i++) {
104 + if (LanguageModes[i]->recognitionExpr != NULL) {
105 + if (SearchString(first200, LanguageModes[i]->recognitionExpr,
106 + SEARCH_FORWARD, SEARCH_REGEX, False, 0, &beginPos,
107 + &endPos, NULL, NULL, NULL)) {
108 + XtFree(first200);
109 + return i;
113 + XtFree(first200);
115 /* no appropriate mode was found */
116 return PLAIN_LANGUAGE_MODE;