qemacs-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemacs-commit] qemacs clang.c forth.c perl.c qe.h util.c extra...


From: Charlie Gordon
Subject: [Qemacs-commit] qemacs clang.c forth.c perl.c qe.h util.c extra...
Date: Sun, 08 Jun 2014 10:56:59 +0000

CVSROOT:        /sources/qemacs
Module name:    qemacs
Changes by:     Charlie Gordon <chqrlie>        14/06/08 10:56:59

Modified files:
        .              : clang.c forth.c perl.c qe.h util.c 
                         extra-modes.c lisp.c qe.c script.c 

Log message:
        add match_shell_handler function and mode field
        
        * add function match_shell_handler to probe shell #! dispatcher line
        * add mode field shell_handlers for generic syntax probe function
        * add memfind to find keywords from buf+length
        * add shell handlers for ruby, perl and python flavors, lua, haskell, 
ocaml
        * remove perl_mode_probe
        * add C++ keywords, include namespace in identifiers
        * add cpp_mode_probe to detect C++ header files
        * add shell handler for tcc scripts
        * remove calc_mode_probe
        * modify scoring between shell-script-mode and simpler sharp-mode

CVSWeb URLs:
http://cvs.savannah.gnu.org/viewcvs/qemacs/clang.c?cvsroot=qemacs&r1=1.77&r2=1.78
http://cvs.savannah.gnu.org/viewcvs/qemacs/forth.c?cvsroot=qemacs&r1=1.2&r2=1.3
http://cvs.savannah.gnu.org/viewcvs/qemacs/perl.c?cvsroot=qemacs&r1=1.21&r2=1.22
http://cvs.savannah.gnu.org/viewcvs/qemacs/qe.h?cvsroot=qemacs&r1=1.182&r2=1.183
http://cvs.savannah.gnu.org/viewcvs/qemacs/util.c?cvsroot=qemacs&r1=1.67&r2=1.68
http://cvs.savannah.gnu.org/viewcvs/qemacs/extra-modes.c?cvsroot=qemacs&r1=1.29&r2=1.30
http://cvs.savannah.gnu.org/viewcvs/qemacs/lisp.c?cvsroot=qemacs&r1=1.21&r2=1.22
http://cvs.savannah.gnu.org/viewcvs/qemacs/qe.c?cvsroot=qemacs&r1=1.183&r2=1.184
http://cvs.savannah.gnu.org/viewcvs/qemacs/script.c?cvsroot=qemacs&r1=1.16&r2=1.17

Patches:
Index: clang.c
===================================================================
RCS file: /sources/qemacs/qemacs/clang.c,v
retrieving revision 1.77
retrieving revision 1.78
diff -u -b -r1.77 -r1.78
--- clang.c     4 Jun 2014 23:59:09 -0000       1.77
+++ clang.c     8 Jun 2014 10:56:58 -0000       1.78
@@ -73,13 +73,18 @@
 };
 
 static const char cpp_keywords[] = {
-    "asm|catch|class|delete|friend|inline|new|operator|"
+    "asm|catch|class|delete|friend|inline|namespace|new|operator|"
     "private|protected|public|template|try|this|virtual|throw|"
+    "explicit|override|mutable|using|assert|true|false|nullptr|"
     // XXX: many missing keywords
 };
 
 static const char cpp_types[] = {
-    ""
+    "bool|exception|istream|ostream|ofstream|string|vector|map|set|stack|"
+    "std::istream|std::ostream|std::ofstream|std::string|"
+    "std::vector|std::unique_ptr|std::map|std::set|std::stack|"
+    "std::hash|std::unordered_set|std::unordered_map|std::exception|"
+    "std::string::iterator|std::stringstream|std::ostringstream|"
 };
 
 static const char objc_keywords[] = {
@@ -453,7 +458,19 @@
             c = p[i] & CHAR_MASK;
             if (c == '-' && flavor == CLANG_CSS)
                 continue;
-            if (!qe_isalnum_(c))
+            if (qe_isalnum_(c))
+                continue;
+            if (c == ':' && (p[i + 1] & CHAR_MASK) == ':'
+            &&  flavor == CLANG_CPP
+            &&  qe_isalpha_(p[i + 2] & CHAR_MASK)) {
+                if (j < buf_size - 2) {
+                    buf[j++] = c;
+                    buf[j++] = c;
+                }
+                i += 2;
+                c = p[i] & CHAR_MASK;
+                continue;
+            }
                 break;
         }
     }
@@ -844,7 +861,7 @@
                     continue;
                 }
 
-                if (str[i] == '(' || (str[i] == ' ' && str[i + 1] == '(')) {
+                if (str[i1] == '(') {
                     /* function call */
                     /* XXX: different styles for call and definition */
                     SET_COLOR(str, start, i, C_STYLE_FUNCTION);
@@ -1386,10 +1403,11 @@
 
 static int c_mode_probe(ModeDef *mode, ModeProbeData *p)
 {
-    /* trust the file extension */
-    if (match_extension(p->filename, mode->extensions))
+    /* trust the file extension and/or shell handler */
+    if (match_extension(p->filename, mode->extensions)
+    ||  match_shell_handler(cs8(p->buf), mode->shell_handlers)) {
         return 80;
-
+    }
     /* weaker match on C comment start */
     if (p->buf[0] == '/' && p->buf[1] == '*')
         return 60;
@@ -1411,6 +1429,7 @@
 ModeDef c_mode = {
     .name = "C",
     .extensions = c_extensions,
+    .shell_handlers = "tcc",
     .mode_probe = c_mode_probe,
     .colorize_func = c_colorize_line,
     .colorize_flags = CLANG_C | CLANG_CC,
@@ -1444,10 +1463,31 @@
     .fallback = &c_mode,
 };
 
+static int cpp_mode_probe(ModeDef *mode, ModeProbeData *p)
+{
+    int score;
+
+    /* trust the file extension */
+    if (match_extension(p->filename, mode->extensions))
+        return 80;
+
+    score = c_mode_probe(&c_mode, p);
+    if (score > 5) {
+        if (strstr(cs8(p->buf), "namespace")
+        ||  strstr(cs8(p->buf), "class")
+        ||  strstr(cs8(p->buf), "::")) {
+            return score + 5;
+        }
+        return score - 5;
+    }
+    return 1;
+}
+
 ModeDef cpp_mode = {
     .name = "C++",
     .mode_name = "cpp",
     .extensions = "cc|hh|cpp|hpp|cxx|hxx|CPP|CC|c++",
+    .mode_probe = cpp_mode_probe,
     .colorize_func = c_colorize_line,
     .colorize_flags = CLANG_CPP | CLANG_CC,
     .keywords = cpp_keywords,
@@ -1698,23 +1738,10 @@
     .fallback = &c_mode,
 };
 
-static int calc_mode_probe(ModeDef *mode, ModeProbeData *p)
-{
-    if (match_extension(p->filename, mode->extensions))
-        return 80;
-    
-    if (p->buf[0] == '#' && p->buf[1] == '!'
-    &&  memstr(p->buf, p->line_len, "/calc")) {
-        /* GNU Calc script */
-        return 80;
-    }
-    return 1;
-}
-
 ModeDef calc_mode = {
     .name = "calc", /* GNU Calc */
     .extensions = "cal|calc",
-    .mode_probe = calc_mode_probe,
+    .shell_handlers = "calc",
     .colorize_func = c_colorize_line,
     .colorize_flags = CLANG_CALC | CLANG_CC,
     .keywords = calc_keywords,
@@ -1738,9 +1765,10 @@
 
 static int qs_mode_probe(ModeDef *mode, ModeProbeData *p)
 {
-    if (match_extension(p->filename, mode->extensions))
+    if (match_extension(p->filename, mode->extensions)
+    ||  match_shell_handler(cs8(p->buf), mode->shell_handlers)) {
         return 80;
-    
+    }
     if (!strcmp(p->filename, ".qerc")
     ||  strstr(p->real_filename, "/.qe/config"))
         return 80;
@@ -1751,6 +1779,7 @@
 ModeDef qscript_mode = {
     .name = "QScript",
     .extensions = "qe|qs",
+    .shell_handlers = "qscript|qs",
     .mode_probe = qs_mode_probe,
     .colorize_func = c_colorize_line,
     .colorize_flags = CLANG_QSCRIPT | CLANG_REGEX,

Index: forth.c
===================================================================
RCS file: /sources/qemacs/qemacs/forth.c,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -b -r1.2 -r1.3
--- forth.c     1 Jun 2014 18:03:54 -0000       1.2
+++ forth.c     8 Jun 2014 10:56:58 -0000       1.3
@@ -295,20 +295,14 @@
     cp->colorize_state = colstate;
 }
 
-static int ff_probe(ModeDef *syn, ModeProbeData *pd)
+static int ff_probe(ModeDef *mode, ModeProbeData *pd)
 {
     const char *p = (const char *)pd->buf;
     const char *p1 = (const char *)pd->buf + pd->line_len;
 
-    if (match_extension(pd->filename, syn->extensions))
+    if (match_extension(pd->filename, mode->extensions)
+    ||  match_shell_handler(cs8(pd->buf), mode->shell_handlers)) {
         return 80;
-
-    if (p[0] == '#' && p[1] == '!') {
-        if (memstr(p, pd->line_len, "forth")
-        ||  memstr(p, pd->line_len, "fth")
-        ||  memstr(p, pd->line_len, "needs")) {
-            return 80;
-        }
     }
 
     if ((p[0] == ':' || p[0] == '\\') && p[1] == ' ')
@@ -323,6 +317,7 @@
 static ModeDef ff_mode = {
     .name = "Forth",
     .extensions = "ff|fth|fs|fr|4th",
+    .shell_handlers = "forth|fth",
     .mode_probe = ff_probe,
     .keywords = ff_keywords,
     .colorize_func = ff_colorize_line,

Index: perl.c
===================================================================
RCS file: /sources/qemacs/qemacs/perl.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -b -r1.21 -r1.22
--- perl.c      30 May 2014 17:18:14 -0000      1.21
+++ perl.c      8 Jun 2014 10:56:58 -0000       1.22
@@ -338,23 +338,10 @@
     cp->colorize_state = colstate;
 }
 
-static int perl_mode_probe(ModeDef *mode, ModeProbeData *p)
-{
-    /* just check file extension */
-    if (match_extension(p->filename, mode->extensions))
-        return 80;
-
-    if (p->buf[0] == '#' && p->buf[1] == '!'
-    &&  memstr(p->buf, p->line_len, "bin/perl"))
-        return 80;
-
-    return 1;
-}
-
 static ModeDef perl_mode = {
     .name = "Perl",
     .extensions = "pl|perl|pm",
-    .mode_probe = perl_mode_probe,
+    .shell_handlers = "perl|perl5",
     .colorize_func = perl_colorize_line,
 };
 

Index: qe.h
===================================================================
RCS file: /sources/qemacs/qemacs/qe.h,v
retrieving revision 1.182
retrieving revision 1.183
diff -u -b -r1.182 -r1.183
--- qe.h        4 Jun 2014 23:59:10 -0000       1.182
+++ qe.h        8 Jun 2014 10:56:58 -0000       1.183
@@ -224,6 +224,7 @@
 char *make_user_path(char *buf, int buf_size, const char *path);
 char *reduce_filename(char *dest, int size, const char *filename);
 int match_extension(const char *filename, const char *extlist);
+int match_shell_handler(const char *p, const char *list);
 int remove_slash(char *buf);
 int append_slash(char *buf, int buf_size);
 char *makepath(char *buf, int buf_size, const char *path, const char 
*filename);
@@ -319,6 +320,7 @@
     return !strcmp(s1, s2);
 }
 
+int memfind(const char *list, const char *p, int len);
 int strfind(const char *list, const char *s);
 int strxfind(const char *list, const char *s);
 const void *memstr(const void *buf, int size, const char *str);
@@ -1222,6 +1224,7 @@
     const char *name;
     const char *mode_name;
     const char *extensions;
+    const char *shell_handlers;
     const char *keywords;
     const char *types;
 

Index: util.c
===================================================================
RCS file: /sources/qemacs/qemacs/util.c,v
retrieving revision 1.67
retrieving revision 1.68
diff -u -b -r1.67 -r1.68
--- util.c      1 Jun 2014 18:02:31 -0000       1.67
+++ util.c      8 Jun 2014 10:56:58 -0000       1.68
@@ -402,6 +402,37 @@
     return 0;
 }
 
+int match_shell_handler(const char *p, const char *list)
+{
+    const char *base;
+
+    if (!list)
+        return 0;
+
+    if (p[0] == '#' && p[1] == '!') {
+        for (p += 2; qe_isblank(*p); p++)
+            continue;
+        for (base = p; *p && !qe_isspace(*p); p++) {
+            if (*p == '/')
+                base = p + 1;
+        }
+        if (memfind(list, base, p - base))
+            return 1;
+        if (p - base == 3 && !memcmp(base, "env", 3)) {
+            while (*p != '\0' && *p != '\n') {
+                for (; qe_isblank(*p); p++)
+                    continue;
+                base = p;
+                for (; *p && !qe_isspace(*p); p++)
+                    continue;
+                if (*base != '-')
+                    return memfind(list, base, p - base);
+            }
+        }
+    }
+    return 0;
+}
+
 /* Remove trailing slash from path, except for / directory */
 int remove_slash(char *buf)
 {
@@ -515,6 +546,40 @@
     *pp = p;
 }
 
+int memfind(const char *list, const char *s, int len)
+{
+    const char *q = list;
+
+    if (!q)
+        return 0;
+
+    for (;;) {
+        int i = 0;
+        for (;;) {
+            int c2 = q[i];
+            if (c2 == '|') {
+                if (i == len)
+                    return 1;
+                break;
+            }
+            if (c2 == '\0') {
+                /* match the empty string against || only */
+                return (len > 0 && i == len);
+            }
+            if (c2 == s[i++] && i <= len)
+                continue;
+            for (q += i;;) {
+                c2 = *q++;
+                if (c2 == '\0')
+                    return 0;
+                if (c2 == '|')
+                    break;
+            }
+            break;
+        }
+    }
+}
+
 #if 0
 /* find a word in a list using '|' as separator */
 int strfind(const char *keytable, const char *str)

Index: extra-modes.c
===================================================================
RCS file: /sources/qemacs/qemacs/extra-modes.c,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -b -r1.29 -r1.30
--- extra-modes.c       4 Jun 2014 08:32:17 -0000       1.29
+++ extra-modes.c       8 Jun 2014 10:56:59 -0000       1.30
@@ -1200,15 +1200,16 @@
 
 static int sharp_mode_probe(ModeDef *mode, ModeProbeData *pd)
 {
-    const char *p = (const char *)pd->buf;
+    const char *p = cs8(pd->buf);
 
-    if (match_extension(pd->filename, mode->extensions)) {
         while (qe_isspace(*p))
             p++;
-        if (*p == '#')
+
+    if (*p == '#') {
+        if (match_extension(pd->filename, mode->extensions))
             return 60;
+        return 30;
     }
-
     return 1;
 }
 
@@ -1698,6 +1699,7 @@
 ModeDef lua_mode = {
     .name = "Lua",
     .extensions = "lua",
+    .shell_handlers = "lua",
     .keywords = lua_keywords,
     .colorize_func = lua_colorize_line,
 };
@@ -2176,6 +2178,7 @@
 ModeDef haskell_mode = {
     .name = "Haskell",
     .extensions = "hs|haskell",
+    .shell_handlers = "haskell",
     .keywords = haskell_keywords,
     .colorize_func = haskell_colorize_line,
 };
@@ -2395,6 +2398,7 @@
 ModeDef python_mode = {
     .name = "Python",
     .extensions = "py|pyt",
+    .shell_handlers = "python|python2.6|python2.7",
     .keywords = python_keywords,
     .colorize_func = python_colorize_line,
 };
@@ -2865,9 +2869,10 @@
 static int ruby_mode_probe(ModeDef *mode, ModeProbeData *p)
 {
     if (match_extension(p->filename, mode->extensions)
-    ||  stristart(p->filename, "Rakefile", NULL))
+    ||  match_shell_handler(cs8(p->buf), mode->shell_handlers)
+    ||  stristart(p->filename, "Rakefile", NULL)) {
         return 80;
-
+    }
     return 1;
 }
 
@@ -2875,6 +2880,7 @@
     .name = "Ruby",
     .extensions = "rb|gemspec",
     .keywords = ruby_keywords,
+    .shell_handlers = "ruby",
     .mode_probe = ruby_mode_probe,
     .colorize_func = ruby_colorize_line,
 };
@@ -3094,6 +3100,7 @@
 static ModeDef ocaml_mode = {
     .name = "Ocaml",
     .extensions = "ml|mli|mll|mly",
+    .shell_handlers = "ocaml",
     .keywords = ocaml_keywords,
     .types = ocaml_types,
     .colorize_func = ocaml_colorize_line,

Index: lisp.c
===================================================================
RCS file: /sources/qemacs/qemacs/lisp.c,v
retrieving revision 1.21
retrieving revision 1.22
diff -u -b -r1.21 -r1.22
--- lisp.c      4 Jun 2014 08:24:55 -0000       1.21
+++ lisp.c      8 Jun 2014 10:56:59 -0000       1.22
@@ -341,11 +341,12 @@
     cp->colorize_state = colstate;
 }
 
-static int elisp_mode_probe(ModeDef *mode, ModeProbeData *mp)
+static int elisp_mode_probe(ModeDef *mode, ModeProbeData *p)
 {
     /* check file name or extension */
-    if (match_extension(mp->filename, mode->extensions)
-    ||  strstart(mp->filename, ".emacs", NULL))
+    if (match_extension(p->filename, mode->extensions)
+    ||  match_shell_handler(cs8(p->buf), mode->shell_handlers)
+    ||  strstart(p->filename, ".emacs", NULL))
         return 80;
 
     return 1;
@@ -355,6 +356,7 @@
     .name = "Lisp",
     .extensions = "ll|li|lh|lo|lm|lisp",
     .keywords = lisp_keywords,
+    .shell_handlers = "lisp",
     .types = lisp_types,
     .colorize_func = lisp_colorize_line,
     .colorize_flags = LISP_LANG_LISP,

Index: qe.c
===================================================================
RCS file: /sources/qemacs/qemacs/qe.c,v
retrieving revision 1.183
retrieving revision 1.184
diff -u -b -r1.183 -r1.184
--- qe.c        1 Jun 2014 14:18:17 -0000       1.183
+++ qe.c        8 Jun 2014 10:56:59 -0000       1.184
@@ -83,8 +83,8 @@
 
 static int generic_mode_probe(ModeDef *mode, ModeProbeData *p)
 {
-    if (mode->extensions) {
-        if (match_extension(p->filename, mode->extensions))
+    if (match_extension(p->filename, mode->extensions)
+    ||  match_shell_handler(cs8(p->buf), mode->shell_handlers)) {
             return 80;
     }
     return 1;

Index: script.c
===================================================================
RCS file: /sources/qemacs/qemacs/script.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -b -r1.16 -r1.17
--- script.c    30 May 2014 17:18:14 -0000      1.16
+++ script.c    8 Jun 2014 10:56:59 -0000       1.17
@@ -107,14 +107,16 @@
 
 static int script_mode_probe(ModeDef *mode, ModeProbeData *p)
 {
-    if (match_extension(p->filename, mode->extensions))
+    if (match_extension(p->filename, mode->extensions)
+    ||  match_shell_handler(cs8(p->buf), mode->shell_handlers)) {
         return 80;
+    }
 
     if (p->buf[0] == '#') {
         if (p->buf[1] == '!')
             return 60;
         if (p->buf[1] == ' ')
-            return 30;
+            return 25;
     }
     return 1;
 }
@@ -122,6 +124,7 @@
 static ModeDef script_mode = {
     .name = "Shell-script",
     .extensions = "sh|bash|csh|ksh|zsh",
+    .shell_handlers = "sh|bash|csh|ksh|zsh",
     .mode_probe = script_mode_probe,
     .colorize_func = script_colorize_line,
 };



reply via email to

[Prev in Thread] Current Thread [Next in Thread]