more Subject/From patch header stuff
[nedit-bw.git] / langmoderegex.patch
blob5b0f51f1e08c77729eda52caf81a7526d7b6945d
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 @@ -3215,11 +3215,13 @@ the list on the left. To add a new lang
23 RemapDeleteKey(LMDialog.nameW);
24 XtVaSetValues(nameLbl, XmNuserData, LMDialog.nameW, NULL);
26 extLbl = XtVaCreateManagedWidget("extLbl", xmLabelGadgetClass, form,
27 XmNlabelString,
28 - s1=XmStringCreateSimple("File extensions (separate w/ space)"),
29 + s1=MKSTRING(
30 +"File extensions or regexes (in single quotes)\n\
31 +(separate w/ space)"),
32 XmNmnemonic, 'F',
33 XmNalignment, XmALIGNMENT_BEGINNING,
34 XmNleftAttachment, XmATTACH_POSITION,
35 XmNleftPosition, LIST_RIGHT,
36 XmNtopAttachment, XmATTACH_WIDGET,
37 @@ -4815,51 +4817,63 @@ static int matchLanguageMode(WindowInfo
38 int i, j, fileNameLen, extLen, beginPos, endPos, start;
39 const char *versionExtendedPath;
41 /*... look for an explicit mode statement first */
43 - /* Do a regular expression search on for recognition pattern */
44 - first200 = BufGetRange(window->buffer, 0, 200);
45 - for (i=0; i<NLanguageModes; i++) {
46 - if (LanguageModes[i]->recognitionExpr != NULL) {
47 - if (SearchString(first200, LanguageModes[i]->recognitionExpr,
48 - SEARCH_FORWARD, SEARCH_REGEX, False, 0, &beginPos,
49 - &endPos, NULL, NULL, NULL))
50 - {
51 - XtFree(first200);
52 - return i;
53 - }
54 - }
55 - }
56 - XtFree(first200);
58 /* Look at file extension ("@@/" starts a ClearCase version extended path,
59 which gets appended after the file extension, and therefore must be
60 stripped off to recognize the extension to make ClearCase users happy) */
61 fileNameLen = strlen(window->filename);
62 #ifdef VMS
63 if (strchr(window->filename, ';') != NULL)
64 - fileNameLen = strchr(window->filename, ';') - window->filename;
65 + fileNameLen = strchr(window->filename, ';') - window->filename;
66 #else
67 if ((versionExtendedPath = GetClearCaseVersionExtendedPath(window->filename)) != NULL)
68 fileNameLen = versionExtendedPath - window->filename;
69 #endif
70 for (i=0; i<NLanguageModes; i++) {
71 - for (j=0; j<LanguageModes[i]->nExtensions; j++) {
72 - ext = LanguageModes[i]->extensions[j];
73 - extLen = strlen(ext);
74 - start = fileNameLen - extLen;
75 -#if defined(__VMS) && (__VMS_VER >= 70200000)
76 - /* VMS v7.2 has case-preserving filenames */
77 + for (j=0; j<LanguageModes[i]->nExtensions; j++) {
78 + ext = LanguageModes[i]->extensions[j];
79 + extLen = strlen(ext);
80 + if (ext[0] == '\'' && ext[extLen - 1] == '\'') {
81 + char* regex = XtMalloc(extLen);
82 + /* copy everything except the single quotes into regex */
83 + strcpy(regex, &ext[1]);
84 + regex[extLen - 2] = '\0';
85 + if (regexFind(window->filename, regex)) {
86 + XtFree(regex);
87 + return i;
88 + } else {
89 + XtFree(regex);
90 + }
91 + } else {
92 + start = fileNameLen - extLen;
93 +#if defined(__VMS) && (__VMS_VER >= 70200000)
94 + /* VMS v7.2 has case-preserving filenames */
95 if (start >= 0 && !strncasecmp(&window->filename[start], ext, extLen))
96 return i;
97 #else
98 - if (start >= 0 && !strncmp(&window->filename[start], ext, extLen))
99 + if (start >= 0 && !strncmp(&window->filename[start], ext, extLen))
100 return i;
101 #endif
107 + /* Do a regular expression search on for recognition pattern */
108 + first200 = BufGetRange(window->buffer, 0, 200);
109 + for (i=0; i<NLanguageModes; i++) {
110 + if (LanguageModes[i]->recognitionExpr != NULL) {
111 + if (SearchString(first200, LanguageModes[i]->recognitionExpr,
112 + SEARCH_FORWARD, SEARCH_REGEX, False, 0, &beginPos,
113 + &endPos, NULL, NULL, NULL)) {
114 + XtFree(first200);
115 + return i;
119 + XtFree(first200);
121 /* no appropriate mode was found */
122 return PLAIN_LANGUAGE_MODE;