shell.js 3.5 KB

12345678910111213141516171819202122
  1. (function(mod){if(typeof exports=="object"&&typeof module=="object")
  2. mod(require("../../lib/codemirror"));else if(typeof define=="function"&&define.amd)
  3. define(["../../lib/codemirror"],mod);else
  4. mod(CodeMirror);})(function(CodeMirror){"use strict";CodeMirror.defineMode('shell',function(){var words={};function define(style,dict){for(var i=0;i<dict.length;i++){words[dict[i]]=style;}};var commonAtoms=["true","false"];var commonKeywords=["if","then","do","else","elif","while","until","for","in","esac","fi","fin","fil","done","exit","set","unset","export","function"];var commonCommands=["ab","awk","bash","beep","cat","cc","cd","chown","chmod","chroot","clear","cp","curl","cut","diff","echo","find","gawk","gcc","get","git","grep","hg","kill","killall","ln","ls","make","mkdir","openssl","mv","nc","nl","node","npm","ping","ps","restart","rm","rmdir","sed","service","sh","shopt","shred","source","sort","sleep","ssh","start","stop","su","sudo","svn","tee","telnet","top","touch","vi","vim","wall","wc","wget","who","write","yes","zsh"];CodeMirror.registerHelper("hintWords","shell",commonAtoms.concat(commonKeywords,commonCommands));define('atom',commonAtoms);define('keyword',commonKeywords);define('builtin',commonCommands);function tokenBase(stream,state){if(stream.eatSpace())return null;var sol=stream.sol();var ch=stream.next();if(ch==='\\'){stream.next();return null;}
  5. if(ch==='\''||ch==='"'||ch==='`'){state.tokens.unshift(tokenString(ch,ch==="`"?"quote":"string"));return tokenize(stream,state);}
  6. if(ch==='#'){if(sol&&stream.eat('!')){stream.skipToEnd();return'meta';}
  7. stream.skipToEnd();return'comment';}
  8. if(ch==='$'){state.tokens.unshift(tokenDollar);return tokenize(stream,state);}
  9. if(ch==='+'||ch==='='){return'operator';}
  10. if(ch==='-'){stream.eat('-');stream.eatWhile(/\w/);return'attribute';}
  11. if(/\d/.test(ch)){stream.eatWhile(/\d/);if(stream.eol()||!/\w/.test(stream.peek())){return'number';}}
  12. stream.eatWhile(/[\w-]/);var cur=stream.current();if(stream.peek()==='='&&/\w+/.test(cur))return'def';return words.hasOwnProperty(cur)?words[cur]:null;}
  13. function tokenString(quote,style){var close=quote=="("?")":quote=="{"?"}":quote
  14. return function(stream,state){var next,escaped=false;while((next=stream.next())!=null){if(next===close&&!escaped){state.tokens.shift();break;}else if(next==='$'&&!escaped&&quote!=="'"&&stream.peek()!=close){escaped=true;stream.backUp(1);state.tokens.unshift(tokenDollar);break;}else if(!escaped&&quote!==close&&next===quote){state.tokens.unshift(tokenString(quote,style))
  15. return tokenize(stream,state)}else if(!escaped&&/['"]/.test(next)&&!/['"]/.test(quote)){state.tokens.unshift(tokenStringStart(next,"string"));stream.backUp(1);break;}
  16. escaped=!escaped&&next==='\\';}
  17. return style;};};function tokenStringStart(quote,style){return function(stream,state){state.tokens[0]=tokenString(quote,style)
  18. stream.next()
  19. return tokenize(stream,state)}}
  20. var tokenDollar=function(stream,state){if(state.tokens.length>1)stream.eat('$');var ch=stream.next()
  21. if(/['"({]/.test(ch)){state.tokens[0]=tokenString(ch,ch=="("?"quote":ch=="{"?"def":"string");return tokenize(stream,state);}
  22. if(!/\d/.test(ch))stream.eatWhile(/\w/);state.tokens.shift();return'def';};function tokenize(stream,state){return(state.tokens[0]||tokenBase)(stream,state);};return{startState:function(){return{tokens:[]};},token:function(stream,state){return tokenize(stream,state);},closeBrackets:"()[]{}''\"\"``",lineComment:'#',fold:"brace"};});CodeMirror.defineMIME('text/x-sh','shell');CodeMirror.defineMIME('application/x-sh','shell');});