diff options
Diffstat (limited to 'plugins/LocalFilesEditor/codemirror/mode/clike')
3 files changed, 288 insertions, 0 deletions
diff --git a/plugins/LocalFilesEditor/codemirror/mode/clike/clike.css b/plugins/LocalFilesEditor/codemirror/mode/clike/clike.css new file mode 100644 index 000000000..21016d7b1 --- /dev/null +++ b/plugins/LocalFilesEditor/codemirror/mode/clike/clike.css @@ -0,0 +1,6 @@ +span.c-like-keyword {color: #90b;} +span.c-like-number {color: #291;} +span.c-like-comment {color: #a70;} +span.c-like-string {color: #a22;} +span.c-like-preprocessor {color: #049;} +span.c-like-var {color: #22b;} diff --git a/plugins/LocalFilesEditor/codemirror/mode/clike/clike.js b/plugins/LocalFilesEditor/codemirror/mode/clike/clike.js new file mode 100644 index 000000000..25bd79144 --- /dev/null +++ b/plugins/LocalFilesEditor/codemirror/mode/clike/clike.js @@ -0,0 +1,181 @@ +CodeMirror.defineMode("clike", function(config, parserConfig) { + var indentUnit = config.indentUnit, keywords = parserConfig.keywords, + cpp = parserConfig.useCPP, multiLineStrings = parserConfig.multiLineStrings, $vars = parserConfig.$vars; + var isOperatorChar = /[+\-*&%=<>!?|]/; + + function chain(stream, state, f) { + state.tokenize = f; + return f(stream, state); + } + + var type; + function ret(tp, style) { + type = tp; + return style; + } + + function tokenBase(stream, state) { + var ch = stream.next(); + if (ch == '"' || ch == "'") + return chain(stream, state, tokenString(ch)); + else if (/[\[\]{}\(\),;\:\.]/.test(ch)) + return ret(ch); + else if (ch == "#" && cpp && state.startOfLine) { + stream.skipToEnd(); + return ret("directive", "c-like-preprocessor"); + } + else if (/\d/.test(ch)) { + stream.eatWhile(/[\w\.]/) + return ret("number", "c-like-number"); + } + else if (ch == "/") { + if (stream.eat("*")) { + return chain(stream, state, tokenComment); + } + else if (stream.eat("/")) { + stream.skipToEnd(); + return ret("comment", "c-like-comment"); + } + else { + stream.eatWhile(isOperatorChar); + return ret("operator"); + } + } + else if (isOperatorChar.test(ch)) { + stream.eatWhile(isOperatorChar); + return ret("operator"); + } + else if ($vars && ch == "$") { + stream.eatWhile(/[\w\$_]/); + return ret("word", "c-like-var"); + } + else { + stream.eatWhile(/[\w\$_]/); + if (keywords && keywords.propertyIsEnumerable(stream.current())) return ret("keyword", "c-like-keyword"); + return ret("word", "c-like-word"); + } + } + + function tokenString(quote) { + return function(stream, state) { + var escaped = false, next, end = false; + while ((next = stream.next()) != null) { + if (next == quote && !escaped) {end = true; break;} + escaped = !escaped && next == "\\"; + } + if (end || !(escaped || multiLineStrings)) + state.tokenize = tokenBase; + return ret("string", "c-like-string"); + }; + } + + function tokenComment(stream, state) { + var maybeEnd = false, ch; + while (ch = stream.next()) { + if (ch == "/" && maybeEnd) { + state.tokenize = tokenBase; + break; + } + maybeEnd = (ch == "*"); + } + return ret("comment", "c-like-comment"); + } + + function Context(indented, column, type, align, prev) { + this.indented = indented; + this.column = column; + this.type = type; + this.align = align; + this.prev = prev; + } + + function pushContext(state, col, type) { + return state.context = new Context(state.indented, col, type, null, state.context); + } + function popContext(state) { + return state.context = state.context.prev; + } + + // Interface + + return { + startState: function(basecolumn) { + return { + tokenize: tokenBase, + context: new Context((basecolumn || 0) - indentUnit, 0, "top", false), + indented: 0, + startOfLine: true + }; + }, + + token: function(stream, state) { + var ctx = state.context; + if (stream.sol()) { + if (ctx.align == null) ctx.align = false; + state.indented = stream.indentation(); + state.startOfLine = true; + } + if (stream.eatSpace()) return null; + var style = state.tokenize(stream, state); + if (type == "comment") return style; + if (ctx.align == null) ctx.align = true; + + if ((type == ";" || type == ":") && ctx.type == "statement") popContext(state); + else if (type == "{") pushContext(state, stream.column(), "}"); + else if (type == "[") pushContext(state, stream.column(), "]"); + else if (type == "(") pushContext(state, stream.column(), ")"); + else if (type == "}") { + if (ctx.type == "statement") ctx = popContext(state); + if (ctx.type == "}") ctx = popContext(state); + if (ctx.type == "statement") ctx = popContext(state); + } + else if (type == ctx.type) popContext(state); + else if (ctx.type == "}") pushContext(state, stream.column(), "statement"); + state.startOfLine = false; + return style; + }, + + indent: function(state, textAfter) { + if (state.tokenize != tokenBase) return 0; + var firstChar = textAfter && textAfter.charAt(0), ctx = state.context, closing = firstChar == ctx.type; + if (ctx.type == "statement") return ctx.indented + (firstChar == "{" ? 0 : indentUnit); + else if (ctx.align) return ctx.column + (closing ? 0 : 1); + else return ctx.indented + (closing ? 0 : indentUnit); + }, + + electricChars: "{}" + }; +}); + +(function() { + function keywords(str) { + var obj = {}, words = str.split(" "); + for (var i = 0; i < words.length; ++i) obj[words[i]] = true; + return obj; + } + var cKeywords = "auto if break int case long char register continue return default short do sizeof " + + "double static else struct entry switch extern typedef float union for unsigned " + + "goto while enum void const signed volatile"; + + CodeMirror.defineMIME("text/x-csrc", { + name: "clike", + useCPP: true, + keywords: keywords(cKeywords) + }); + CodeMirror.defineMIME("text/x-c++src", { + name: "clike", + useCPP: true, + keywords: keywords(cKeywords + " asm dynamic_cast namespace reinterpret_cast try bool explicit new " + + "static_cast typeid catch false operator template typename class friend private " + + "this using const_cast inline public throw virtual delete mutable protected true " + + "wchar_t") + }); + CodeMirror.defineMIME("text/x-java", { + name: "clike", + keywords: keywords("abstract assert boolean break byte case catch char class const continue default " + + "do double else enum extends false final finally float for goto if implements import " + + "instanceof int interface long native new null package private protected public " + + "return short static strictfp super switch synchronized this throw throws transient " + + "true try void volatile while") + }); +}()); diff --git a/plugins/LocalFilesEditor/codemirror/mode/clike/index.html b/plugins/LocalFilesEditor/codemirror/mode/clike/index.html new file mode 100644 index 000000000..0836535d2 --- /dev/null +++ b/plugins/LocalFilesEditor/codemirror/mode/clike/index.html @@ -0,0 +1,101 @@ +<!doctype html> +<html> + <head> + <title>CodeMirror 2: C-like mode</title> + <link rel="stylesheet" href="../../lib/codemirror.css"> + <script src="../../lib/codemirror.js"></script> + <script src="clike.js"></script> + <link rel="stylesheet" href="clike.css"> + <link rel="stylesheet" href="../../css/docs.css"> + <style>.CodeMirror {border: 2px inset #dee;}</style> + </head> + <body> + <h1>CodeMirror 2: C-like mode</h1> + +<form><textarea id="code" name="code"> +/* C demo code */ + +#include <zmq.h> +#include <pthread.h> +#include <semaphore.h> +#include <time.h> +#include <stdio.h> +#include <fcntl.h> +#include <malloc.h> + +typedef struct { + void* arg_socket; + zmq_msg_t* arg_msg; + char* arg_string; + unsigned long arg_len; + int arg_int, arg_command; + + int signal_fd; + int pad; + void* context; + sem_t sem; +} acl_zmq_context; + +#define p(X) (context->arg_##X) + +void* zmq_thread(void* context_pointer) { + acl_zmq_context* context = (acl_zmq_context*)context_pointer; + char ok = 'K', err = 'X'; + int res; + + while (1) { + while ((res = sem_wait(&context->sem)) == EINTR); + if (res) {write(context->signal_fd, &err, 1); goto cleanup;} + switch(p(command)) { + case 0: goto cleanup; + case 1: p(socket) = zmq_socket(context->context, p(int)); break; + case 2: p(int) = zmq_close(p(socket)); break; + case 3: p(int) = zmq_bind(p(socket), p(string)); break; + case 4: p(int) = zmq_connect(p(socket), p(string)); break; + case 5: p(int) = zmq_getsockopt(p(socket), p(int), (void*)p(string), &p(len)); break; + case 6: p(int) = zmq_setsockopt(p(socket), p(int), (void*)p(string), p(len)); break; + case 7: p(int) = zmq_send(p(socket), p(msg), p(int)); break; + case 8: p(int) = zmq_recv(p(socket), p(msg), p(int)); break; + case 9: p(int) = zmq_poll(p(socket), p(int), p(len)); break; + } + p(command) = errno; + write(context->signal_fd, &ok, 1); + } + cleanup: + close(context->signal_fd); + free(context_pointer); + return 0; +} + +void* zmq_thread_init(void* zmq_context, int signal_fd) { + acl_zmq_context* context = malloc(sizeof(acl_zmq_context)); + pthread_t thread; + + context->context = zmq_context; + context->signal_fd = signal_fd; + sem_init(&context->sem, 1, 0); + pthread_create(&thread, 0, &zmq_thread, context); + pthread_detach(thread); + return context; +} +</textarea></form> + + <script> + var editor = CodeMirror.fromTextArea(document.getElementById("code"), { + lineNumbers: true, + matchBrackets: true, + mode: "text/x-csrc" + }); + </script> + + <p>Simple mode that tries to handle C-like languages as well as it + can. Takes two configuration parameters: <code>keywords</code>, an + object whose property names are the keywords in the language, + and <code>useCPP</code>, which determines whether C preprocessor + directives are recognized.</p> + + <p><strong>MIME types defined:</strong> <code>text/x-csrc</code> + (C code), <code>text/x-c++src</code> (C++ + code), <code>text/x-java</code> (Java code).</p> + </body> +</html> |