aboutsummaryrefslogtreecommitdiffstats
path: root/tiddlers/plugin-org-gtd-codemirror.tid
blob: 09b3e6c0d45c27c7175e1551e5664b9ba8487e6b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
title: $:/plugins/local/org-gtd/codemirror.js
type: application/javascript
module-type: codemirror

/*\
title: $:/plugins/local/org-gtd/codemirror.js
type: application/javascript
module-type: codemirror
\*/
(function() {
"use strict";

var CodeMirror = require("$:/plugins/tiddlywiki/codemirror/lib/codemirror.js"),
	org = require("$:/plugins/local/org-gtd/library.js"),
	JUMP_STATE_TITLE = "$:/state/org-gtd/jump",
	KEY_MAP_TITLE = "$:/config/org-gtd/extraKeys";

function isOrgEditor(cm) {
	return cm.getMode().name === "org-gtd";
}

function readOrgGtdKeyMap(cm) {
	var widget = cm.getOption("editWidget"),
		text,
		keyMap;
	if(!widget || !widget.wiki) {
		return null;
	}
	text = widget.wiki.getTiddlerText(KEY_MAP_TITLE,"{}");
	try {
		keyMap = JSON.parse(text);
	} catch(error) {
		console.error("Org GTD shortcut configuration is invalid JSON",error);
		return null;
	}
	if(!keyMap || typeof keyMap !== "object" || Array.isArray(keyMap) || Object.keys(keyMap).length === 0) {
		return null;
	}
	try {
		return CodeMirror.normalizeKeyMap(keyMap);
	} catch(error) {
		console.error("Org GTD shortcut configuration contains an invalid key name",error);
		return null;
	}
}

function syncOrgGtdKeyMap(cm) {
	var keyMap;
	if(cm.state.orgGtdKeyMap) {
		cm.removeKeyMap(cm.state.orgGtdKeyMap);
		cm.state.orgGtdKeyMap = null;
	}
	if(!isOrgEditor(cm)) {
		return;
	}
	keyMap = readOrgGtdKeyMap(cm);
	if(keyMap) {
		cm.addKeyMap(keyMap);
		cm.state.orgGtdKeyMap = keyMap;
	}
}

function clearFolds(cm) {
	var marks = cm.state.orgGtdFoldMarks || [];
	marks.forEach(function(mark) {
		if(mark.find()) {
			mark.clear();
		}
	});
	cm.state.orgGtdFoldMarks = [];
}

function replaceDocument(cm,lines,cursorLine,cursorCh) {
	var lastLine = cm.lastLine(),
		lastCh = cm.getLine(lastLine).length;
	clearFolds(cm);
	cm.operation(function() {
		cm.replaceRange(lines.join("\n"),CodeMirror.Pos(0,0),CodeMirror.Pos(lastLine,lastCh),"+org-gtd");
		cm.setCursor(CodeMirror.Pos(cursorLine,cursorCh || 0));
	});
}

function currentHeading(cm) {
	var parsed = org.parse(cm.getValue()),
		heading = org.headingAtLine(parsed,cm.getCursor().line);
	return {parsed: parsed,heading: heading};
}

CodeMirror.defineMode("org-gtd",function() {
	return {
		startState: function() {
			return {heading: false,planning: false};
		},
		token: function(stream,state) {
			if(stream.sol()) {
				state.heading = /^\*+\s+/.test(stream.string);
				state.planning = /^\s*(?:SCHEDULED|DEADLINE):/.test(stream.string);
			}
			if(state.heading) {
				if(stream.match(/^\*+\s+/)) {
					return "header";
				}
				if(stream.match(/^(?:TODO|NEXT|WAIT|DONE)\b/)) {
					return "keyword";
				}
				if(stream.match(/^\s+:[A-Za-z0-9_@#%+.-]+(?::[A-Za-z0-9_@#%+.-]+)*:\s*$/)) {
					return "tag";
				}
			}
			if(state.planning) {
				if(stream.match(/^\s*(?:SCHEDULED|DEADLINE):/)) {
					return "def";
				}
				if(stream.match(/^\s*<[0-9]{4}-[0-9]{2}-[0-9]{2}(?:\s+[^>]*)?>/)) {
					return "atom";
				}
			}
			if(stream.sol() && stream.match(/^\s*#.*$/)) {
				return "comment";
			}
			stream.next();
			return null;
		},
		lineComment: "# "
	};
});

CodeMirror.defineMIME("text/x-org","org-gtd");

CodeMirror.commands.orgGtdCycleState = function(cm) {
	var cursor,
		line,
		result;
	if(!isOrgEditor(cm)) {
		return CodeMirror.Pass;
	}
	cursor = cm.getCursor();
	line = cm.getLine(cursor.line);
	result = org.cycleState(line);
	if(!result) {
		return CodeMirror.Pass;
	}
	cm.replaceRange(result.line,CodeMirror.Pos(cursor.line,0),CodeMirror.Pos(cursor.line,line.length),"+org-gtd");
	cm.setCursor(CodeMirror.Pos(cursor.line,Math.min(cursor.ch + (result.line.length - line.length),result.line.length)));
};

function changeLevel(cm,direction) {
	var context,
		lines;
	if(!isOrgEditor(cm)) {
		return CodeMirror.Pass;
	}
	context = currentHeading(cm);
	if(!context.heading || (direction < 0 && context.heading.level === 1)) {
		return CodeMirror.Pass;
	}
	lines = context.parsed.lines.slice(0);
	for(var index = context.heading.line; index <= context.heading.subtreeEndLine; index++) {
		if(/^\*+\s+/.test(lines[index])) {
			lines[index] = direction > 0 ? "*" + lines[index] : lines[index].substring(1);
		}
	}
	replaceDocument(cm,lines,context.heading.line,Math.max(0,cm.getCursor().ch + direction));
}

CodeMirror.commands.orgGtdPromote = function(cm) {
	return changeLevel(cm,-1);
};

CodeMirror.commands.orgGtdDemote = function(cm) {
	return changeLevel(cm,1);
};

function moveSubtree(cm,direction) {
	var context,
		heading,
		headings,
		index,
		other,
		lines,
		before,
		current,
		after,
		newLine;
	if(!isOrgEditor(cm)) {
		return CodeMirror.Pass;
	}
	context = currentHeading(cm);
	heading = context.heading;
	if(!heading) {
		return CodeMirror.Pass;
	}
	headings = context.parsed.headings;
	index = headings.indexOf(heading);
	if(direction < 0) {
		for(index = index - 1; index >= 0; index--) {
			if(headings[index].level < heading.level) {
				return CodeMirror.Pass;
			}
			if(headings[index].level === heading.level) {
				other = headings[index];
				break;
			}
		}
		if(!other) {
			return CodeMirror.Pass;
		}
		lines = context.parsed.lines;
		before = lines.slice(0,other.line);
		current = lines.slice(heading.line,heading.subtreeEndLine + 1);
		after = lines.slice(other.line,heading.line).concat(lines.slice(heading.subtreeEndLine + 1));
		newLine = other.line;
		replaceDocument(cm,before.concat(current,after),newLine,cm.getCursor().ch);
	} else {
		for(index = index + 1; index < headings.length; index++) {
			if(headings[index].line <= heading.subtreeEndLine) {
				continue;
			}
			if(headings[index].level < heading.level) {
				return CodeMirror.Pass;
			}
			if(headings[index].level === heading.level) {
				other = headings[index];
				break;
			}
		}
		if(!other) {
			return CodeMirror.Pass;
		}
		lines = context.parsed.lines;
		before = lines.slice(0,heading.line);
		current = lines.slice(heading.line,heading.subtreeEndLine + 1);
		after = lines.slice(other.line,other.subtreeEndLine + 1);
		newLine = heading.line + after.length;
		replaceDocument(cm,before.concat(after,current,lines.slice(other.subtreeEndLine + 1)),newLine,cm.getCursor().ch);
	}
}

CodeMirror.commands.orgGtdMoveUp = function(cm) {
	return moveSubtree(cm,-1);
};

CodeMirror.commands.orgGtdMoveDown = function(cm) {
	return moveSubtree(cm,1);
};

CodeMirror.commands.orgGtdToggleFold = function(cm) {
	var context,
		heading,
		marks,
		existing,
		marker,
		mark;
	if(!isOrgEditor(cm)) {
		return CodeMirror.Pass;
	}
	context = currentHeading(cm);
	heading = context.heading;
	if(!heading || cm.getCursor().line !== heading.line || heading.subtreeEndLine === heading.line) {
		return CodeMirror.Pass;
	}
	marks = cm.state.orgGtdFoldMarks = cm.state.orgGtdFoldMarks || [];
	existing = marks.filter(function(item) {
		var range = item.find();
		return range && range.from.line === heading.line;
	})[0];
	if(existing) {
		existing.clear();
		cm.state.orgGtdFoldMarks = marks.filter(function(item) {return item !== existing;});
		return;
	}
	marker = cm.getWrapperElement().ownerDocument.createElement("span");
	marker.className = "cm-org-gtd-fold-marker";
	marker.textContent = " ...";
	mark = cm.markText(
		CodeMirror.Pos(heading.line,cm.getLine(heading.line).length),
		CodeMirror.Pos(heading.subtreeEndLine,cm.getLine(heading.subtreeEndLine).length),
		{collapsed: true,replacedWith: marker,atomic: true}
	);
	marks.push(mark);
	marker.addEventListener("click",function() {
		mark.clear();
		cm.state.orgGtdFoldMarks = marks.filter(function(item) {return item !== mark;});
	});
};

CodeMirror.commands.orgGtdInsertSibling = function(cm) {
	var context,
		heading,
		lines,
		newLine;
	if(!isOrgEditor(cm)) {
		return CodeMirror.Pass;
	}
	context = currentHeading(cm);
	heading = context.heading;
	if(!heading) {
		return CodeMirror.Pass;
	}
	lines = context.parsed.lines.slice(0);
	newLine = heading.subtreeEndLine + 1;
	lines.splice(newLine,0,new Array(heading.level + 1).join("*") + " TODO ");
	replaceDocument(cm,lines,newLine,heading.level + 6);
};

CodeMirror.defineInitHook(function(cm) {
	var widget = cm.getOption("editWidget"),
		request,
		currentTitle,
		tiddler,
		originalTitle;
	syncOrgGtdKeyMap(cm);
	cm.on("optionChange",function(instance,option) {
		if(option === "mode") {
			syncOrgGtdKeyMap(instance);
		}
	});
	if(!widget || !widget.wiki) {
		return;
	}
	request = $tw.utils.parseJSONSafe(widget.wiki.getTiddlerText(JUMP_STATE_TITLE,""),null);
	if(!request) {
		return;
	}
	currentTitle = widget.editTitle || widget.getVariable("currentTiddler","");
	tiddler = widget.wiki.getTiddler(currentTitle);
	originalTitle = tiddler && tiddler.fields["draft.of"] ? tiddler.fields["draft.of"] : currentTitle;
	if(originalTitle !== request.title) {
		return;
	}
	setTimeout(function() {
		var line = Math.max(0,Math.min(Number(request.line || 1) - 1,cm.lastLine())),
			position = CodeMirror.Pos(line,0);
		cm.setCursor(position);
		cm.scrollIntoView(position,100);
		cm.focus();
		widget.wiki.deleteTiddler(JUMP_STATE_TITLE);
	},0);
});

})();