Patch #3380 2012-11-26 04:43

alpha0010

Highlight Lang: parse #! and recognize standard headers
Download
3380-Highlight_Lang.patch (2.1 KB)
Category
Application::FeatureAdd
Status
Accepted
Close date
2012-12-13 14:50
Assigned to
mortenmacfly
Index: src/sdk/editorcolourset.cpp
===================================================================
--- src/sdk/editorcolourset.cpp    (revision 8628)
+++ src/sdk/editorcolourset.cpp    (working copy)
@@ -380,6 +380,49 @@
                 return it->first;
         }
     }
+    // parse #! directive
+    if (wxFileExists(filename))
+    {
+        wxFileInputStream input(filename);
+        wxTextInputStream text(input);
+        wxString line;
+        if (input.IsOk() && !input.Eof() )
+            line = text.ReadLine();
+        if (!line.IsEmpty())
+        {
+            wxRegEx reSheBang(wxT("#![ \t]*([a-zA-Z/]+)[ \t]*([a-zA-Z/]*)"));
+            if (reSheBang.Matches(line))
+            {
+                wxString prog = reSheBang.GetMatch(line, 1);
+                if (prog.EndsWith(wxT("env")))
+                    prog = reSheBang.GetMatch(line, 2);
+                if (prog.Find(wxT('/')) != wxNOT_FOUND)
+                    prog = prog.AfterLast(wxT('/'));
+                if (prog == wxT("sh"))
+                    prog = wxT("bash");
+                HighlightLanguage lang = GetHighlightLanguage(prog);
+                if (lang !=  HL_NONE)
+                    return lang;
+            }
+            else if (line.Trim().StartsWith(wxT("<?xml")))
+                return GetHighlightLanguage(wxT("XML"));
+        }
+    }
+    // standard headers
+    const wxString cppNames = wxT("|"
+            "algorithm|" "bitset|"    "complex|"    "deque|"
+            "exception|" "fstream|"   "functional|" "hash_map|"
+            "hash_set|"  "iomanip|"   "iostream|"   "iterator|"
+            "limits|"    "list|"      "locale|"     "map|"
+            "memory|"    "numeric|"   "queue|"      "set|"
+            "sstream|"   "stack|"     "stdexcept|"  "streambuf|"
+            "string|"    "strstream|" "utility|"    "valarray|"
+            "vector|"
+
+            "cassert|" "cctype|"  "clocale|" "cstdio|"
+            "cstdlib|" "cstring|" "ctime|"             );
+    if (cppNames.Find(wxT("|") + lfname + wxT("|")) != wxNOT_FOUND)
+        return GetHighlightLanguage(wxT("C/C++"));
     return HL_NONE;
 }