5
if (typeof exports == "object" && typeof module == "object")
6
mod(require("../../lib/codemirror"));
7
else if (typeof define == "function" && define.amd)
8
define(["../../lib/codemirror"], mod);
11
})(function(CodeMirror) {
14
CodeMirror.registerHelper("wordChars", "r", /[\w.]/);
16
CodeMirror.defineMode("r", function(config) {
17
function wordObj(words) {
19
for (var i = 0; i < words.length; ++i) res[words[i]] = true;
22
var commonAtoms = ["NULL", "NA", "Inf", "NaN", "NA_integer_", "NA_real_", "NA_complex_", "NA_character_", "TRUE", "FALSE"];
23
var commonBuiltins = ["list", "quote", "bquote", "eval", "return", "call", "parse", "deparse"];
24
var commonKeywords = ["if", "else", "repeat", "while", "function", "for", "in", "next", "break"];
25
var commonBlockKeywords = ["if", "else", "repeat", "while", "function", "for"];
27
CodeMirror.registerHelper("hintWords", "r", commonAtoms.concat(commonBuiltins, commonKeywords));
29
var atoms = wordObj(commonAtoms);
30
var builtins = wordObj(commonBuiltins);
31
var keywords = wordObj(commonKeywords);
32
var blockkeywords = wordObj(commonBlockKeywords);
33
var opChars = /[+\-*\/^<>=!&|~$:]/;
36
function tokenBase(stream, state) {
38
var ch = stream.next();
42
} else if (ch == "0" && stream.eat("x")) {
43
stream.eatWhile(/[\da-f]/i);
45
} else if (ch == "." && stream.eat(/\d/)) {
46
stream.match(/\d*(?:e[+\-]?\d+)?/);
48
} else if (/\d/.test(ch)) {
49
stream.match(/\d*(?:\.\d+)?(?:e[+\-]\d+)?L?/);
51
} else if (ch == "'" || ch == '"') {
52
state.tokenize = tokenString(ch);
54
} else if (ch == "`") {
55
stream.match(/[^`]+`/);
57
} else if (ch == "." && stream.match(/.(?:[.]|\d+)/)) {
59
} else if (/[a-zA-Z\.]/.test(ch)) {
60
stream.eatWhile(/[\w\.]/);
61
var word = stream.current();
62
if (atoms.propertyIsEnumerable(word)) return "atom";
63
if (keywords.propertyIsEnumerable(word)) {
66
if (blockkeywords.propertyIsEnumerable(word) &&
67
!stream.match(/\s*if(\s+|$)/, false))
71
if (builtins.propertyIsEnumerable(word)) return "builtin";
73
} else if (ch == "%") {
74
if (stream.skipTo("%")) stream.next();
75
return "operator variable-2";
77
(ch == "<" && stream.eat("-")) ||
78
(ch == "<" && stream.match("<-")) ||
79
(ch == "-" && stream.match(/>>?/))
81
return "operator arrow";
82
} else if (ch == "=" && state.ctx.argList) {
84
} else if (opChars.test(ch)) {
85
if (ch == "$") return "operator dollar";
86
stream.eatWhile(opChars);
88
} else if (/[\(\){}\[\];]/.test(ch)) {
90
if (ch == ";") return "semi";
97
function tokenString(quote) {
98
return function(stream, state) {
99
if (stream.eat("\\")) {
100
var ch = stream.next();
101
if (ch == "x") stream.match(/^[a-f0-9]{2}/i);
102
else if ((ch == "u" || ch == "U") && stream.eat("{") && stream.skipTo("}")) stream.next();
103
else if (ch == "u") stream.match(/^[a-f0-9]{4}/i);
104
else if (ch == "U") stream.match(/^[a-f0-9]{8}/i);
105
else if (/[0-7]/.test(ch)) stream.match(/^[0-7]{1,2}/);
109
while ((next = stream.next()) != null) {
110
if (next == quote) { state.tokenize = tokenBase; break; }
111
if (next == "\\") { stream.backUp(1); break; }
118
var ALIGN_YES = 1, ALIGN_NO = 2, BRACELESS = 4
120
function push(state, type, stream) {
121
state.ctx = {type: type,
122
indent: state.indent,
124
column: stream.column(),
127
function setFlag(state, flag) {
129
state.ctx = {type: ctx.type,
131
flags: ctx.flags | flag,
135
function pop(state) {
136
state.indent = state.ctx.indent;
137
state.ctx = state.ctx.prev;
141
startState: function() {
142
return {tokenize: tokenBase,
144
indent: -config.indentUnit,
150
token: function(stream, state) {
152
if ((state.ctx.flags & 3) == 0) state.ctx.flags |= ALIGN_NO
153
if (state.ctx.flags & BRACELESS) pop(state)
154
state.indent = stream.indentation();
156
if (stream.eatSpace()) return null;
157
var style = state.tokenize(stream, state);
158
if (style != "comment" && (state.ctx.flags & ALIGN_NO) == 0) setFlag(state, ALIGN_YES)
160
if ((curPunc == ";" || curPunc == "{" || curPunc == "}") && state.ctx.type == "block") pop(state);
161
if (curPunc == "{") push(state, "}", stream);
162
else if (curPunc == "(") {
163
push(state, ")", stream);
164
if (state.afterIdent) state.ctx.argList = true;
166
else if (curPunc == "[") push(state, "]", stream);
167
else if (curPunc == "block") push(state, "block", stream);
168
else if (curPunc == state.ctx.type) pop(state);
169
else if (state.ctx.type == "block" && style != "comment") setFlag(state, BRACELESS)
170
state.afterIdent = style == "variable" || style == "keyword";
174
indent: function(state, textAfter) {
175
if (state.tokenize != tokenBase) return 0;
176
var firstChar = textAfter && textAfter.charAt(0), ctx = state.ctx,
177
closing = firstChar == ctx.type;
178
if (ctx.flags & BRACELESS) ctx = ctx.prev
179
if (ctx.type == "block") return ctx.indent + (firstChar == "{" ? 0 : config.indentUnit);
180
else if (ctx.flags & ALIGN_YES) return ctx.column + (closing ? 0 : 1);
181
else return ctx.indent + (closing ? 0 : config.indentUnit);
188
CodeMirror.defineMIME("text/x-rsrc", "r");