all repos — NoPaste @ 3e3c433f026c49e04d0efa0f0f39fc688ed32bb1

Resurrected - The PussTheCat.org fork of NoPaste

scripts/CodeMirror/mode/shell/shell.js (view raw)

  1// CodeMirror, copyright (c) by Marijn Haverbeke and others
  2// Distributed under an MIT license: https://codemirror.net/LICENSE
  3
  4(function(mod) {
  5  if (typeof exports == "object" && typeof module == "object") // CommonJS
  6    mod(require("../../lib/codemirror"));
  7  else if (typeof define == "function" && define.amd) // AMD
  8    define(["../../lib/codemirror"], mod);
  9  else // Plain browser env
 10    mod(CodeMirror);
 11})(function(CodeMirror) {
 12"use strict";
 13
 14CodeMirror.defineMode('shell', function() {
 15
 16  var words = {};
 17  function define(style, dict) {
 18    for(var i = 0; i < dict.length; i++) {
 19      words[dict[i]] = style;
 20    }
 21  };
 22
 23  var commonAtoms = ["true", "false"];
 24  var commonKeywords = ["if", "then", "do", "else", "elif", "while", "until", "for", "in", "esac", "fi",
 25    "fin", "fil", "done", "exit", "set", "unset", "export", "function"];
 26  var commonCommands = ["ab", "awk", "bash", "beep", "cat", "cc", "cd", "chown", "chmod", "chroot", "clear",
 27    "cp", "curl", "cut", "diff", "echo", "find", "gawk", "gcc", "get", "git", "grep", "hg", "kill", "killall",
 28    "ln", "ls", "make", "mkdir", "openssl", "mv", "nc", "nl", "node", "npm", "ping", "ps", "restart", "rm",
 29    "rmdir", "sed", "service", "sh", "shopt", "shred", "source", "sort", "sleep", "ssh", "start", "stop",
 30    "su", "sudo", "svn", "tee", "telnet", "top", "touch", "vi", "vim", "wall", "wc", "wget", "who", "write",
 31    "yes", "zsh"];
 32
 33  CodeMirror.registerHelper("hintWords", "shell", commonAtoms.concat(commonKeywords, commonCommands));
 34
 35  define('atom', commonAtoms);
 36  define('keyword', commonKeywords);
 37  define('builtin', commonCommands);
 38
 39  function tokenBase(stream, state) {
 40    if (stream.eatSpace()) return null;
 41
 42    var sol = stream.sol();
 43    var ch = stream.next();
 44
 45    if (ch === '\\') {
 46      stream.next();
 47      return null;
 48    }
 49    if (ch === '\'' || ch === '"' || ch === '`') {
 50      state.tokens.unshift(tokenString(ch, ch === "`" ? "quote" : "string"));
 51      return tokenize(stream, state);
 52    }
 53    if (ch === '#') {
 54      if (sol && stream.eat('!')) {
 55        stream.skipToEnd();
 56        return 'meta'; // 'comment'?
 57      }
 58      stream.skipToEnd();
 59      return 'comment';
 60    }
 61    if (ch === '$') {
 62      state.tokens.unshift(tokenDollar);
 63      return tokenize(stream, state);
 64    }
 65    if (ch === '+' || ch === '=') {
 66      return 'operator';
 67    }
 68    if (ch === '-') {
 69      stream.eat('-');
 70      stream.eatWhile(/\w/);
 71      return 'attribute';
 72    }
 73    if (/\d/.test(ch)) {
 74      stream.eatWhile(/\d/);
 75      if(stream.eol() || !/\w/.test(stream.peek())) {
 76        return 'number';
 77      }
 78    }
 79    stream.eatWhile(/[\w-]/);
 80    var cur = stream.current();
 81    if (stream.peek() === '=' && /\w+/.test(cur)) return 'def';
 82    return words.hasOwnProperty(cur) ? words[cur] : null;
 83  }
 84
 85  function tokenString(quote, style) {
 86    var close = quote == "(" ? ")" : quote == "{" ? "}" : quote
 87    return function(stream, state) {
 88      var next, escaped = false;
 89      while ((next = stream.next()) != null) {
 90        if (next === close && !escaped) {
 91          state.tokens.shift();
 92          break;
 93        } else if (next === '$' && !escaped && quote !== "'" && stream.peek() != close) {
 94          escaped = true;
 95          stream.backUp(1);
 96          state.tokens.unshift(tokenDollar);
 97          break;
 98        } else if (!escaped && quote !== close && next === quote) {
 99          state.tokens.unshift(tokenString(quote, style))
100          return tokenize(stream, state)
101        } else if (!escaped && /['"]/.test(next) && !/['"]/.test(quote)) {
102          state.tokens.unshift(tokenStringStart(next, "string"));
103          stream.backUp(1);
104          break;
105        }
106        escaped = !escaped && next === '\\';
107      }
108      return style;
109    };
110  };
111
112  function tokenStringStart(quote, style) {
113    return function(stream, state) {
114      state.tokens[0] = tokenString(quote, style)
115      stream.next()
116      return tokenize(stream, state)
117    }
118  }
119
120  var tokenDollar = function(stream, state) {
121    if (state.tokens.length > 1) stream.eat('$');
122    var ch = stream.next()
123    if (/['"({]/.test(ch)) {
124      state.tokens[0] = tokenString(ch, ch == "(" ? "quote" : ch == "{" ? "def" : "string");
125      return tokenize(stream, state);
126    }
127    if (!/\d/.test(ch)) stream.eatWhile(/\w/);
128    state.tokens.shift();
129    return 'def';
130  };
131
132  function tokenize(stream, state) {
133    return (state.tokens[0] || tokenBase) (stream, state);
134  };
135
136  return {
137    startState: function() {return {tokens:[]};},
138    token: function(stream, state) {
139      return tokenize(stream, state);
140    },
141    closeBrackets: "()[]{}''\"\"``",
142    lineComment: '#',
143    fold: "brace"
144  };
145});
146
147CodeMirror.defineMIME('text/x-sh', 'shell');
148// Apache uses a slightly different Media Type for Shell scripts
149// http://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
150CodeMirror.defineMIME('application/x-sh', 'shell');
151
152});