2
* CodePress - Real Time Syntax Highlighting Editor written in JavaScript - http://codepress.org/
4
* Copyright (C) 2007 Fernando M.A.d.S. <fermads@gmail.com>
7
* Fernando M.A.d.S. <fermads@gmail.com>
8
* Michael Hurni <michael.hurni@gmail.com>
12
* This program is free software; you can redistribute it and/or modify it under the terms of the
13
* GNU Lesser General Public License as published by the Free Software Foundation.
15
* Read the full licence: http://www.opensource.org/licenses/lgpl-license.php
22
// set initial vars and start sh
23
initialize : function() {
24
if(typeof(editor)=='undefined' && !arguments[0]) return;
25
body = document.getElementsByTagName('body')[0];
26
body.innerHTML = body.innerHTML.replace(/\n/g,"");
27
chars = '|32|46|62|8|'; // charcodes that trigger syntax highlighting
28
cc = '\u2009'; // carret char
29
editor = document.getElementsByTagName('pre')[0];
30
document.designMode = 'on';
31
document.addEventListener('keypress', this.keyHandler, true);
32
window.addEventListener('scroll', function() { if(!CodePress.scrolling) CodePress.syntaxHighlight('scroll') }, false);
33
completeChars = this.getCompleteChars();
34
completeEndingChars = this.getCompleteEndingChars();
35
document.changehandlers = Array()
39
keyHandler : function(evt) {
40
keyCode = evt.keyCode;
41
charCode = evt.charCode;
42
fromChar = String.fromCharCode(charCode);
43
for (var handler in this.changehandlers)
45
document.changehandlers[handler]();
48
if((evt.ctrlKey || evt.metaKey) && evt.shiftKey && charCode!=90) { // shortcuts = ctrl||appleKey+shift+key!=z(undo)
49
CodePress.shortcuts(charCode?charCode:keyCode);
51
else if( (completeEndingChars.indexOf('|'+fromChar+'|')!= -1 || completeChars.indexOf('|'+fromChar+'|')!=-1) && CodePress.autocomplete) { // auto complete
52
if(!CodePress.completeEnding(fromChar))
53
CodePress.complete(fromChar);
55
else if(chars.indexOf('|'+charCode+'|')!=-1||keyCode==13) { // syntax highlighting
56
top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
58
else if(keyCode==9 || evt.tabKey) { // snippets activation (tab)
59
CodePress.snippets(evt);
61
else if(keyCode==46||keyCode==8) { // save to history when delete or backspace pressed
62
CodePress.actions.history[CodePress.actions.next()] = editor.innerHTML;
64
else if((charCode==122||charCode==121||charCode==90) && evt.ctrlKey) { // undo and redo
65
(charCode==121||evt.shiftKey) ? CodePress.actions.redo() : CodePress.actions.undo();
68
else if(charCode==118 && evt.ctrlKey) { // handle paste
69
top.setTimeout(function(){CodePress.syntaxHighlight('generic');},100);
71
else if(charCode==99 && evt.ctrlKey) { // handle cut
72
//alert(window.getSelection().getRangeAt(0).toString().replace(/\t/g,'FFF'));
77
// put cursor back to its original position after every parsing
78
findString : function() {
80
window.getSelection().getRangeAt(0).deleteContents();
83
// split big files, highlighting parts of it
84
split : function(code,flag) {
86
this.scrolling = true;
90
this.scrolling = false;
91
mid = code.indexOf(cc);
92
if(mid-2000<0) {ini=0;end=4000;}
93
else if(mid+2000>code.length) {ini=code.length-4000;end=code.length;}
94
else {ini=mid-2000;end=mid+2000;}
95
code = code.substring(ini,end);
100
getEditor : function() {
101
if(!document.getElementsByTagName('pre')[0]) {
102
body = document.getElementsByTagName('body')[0];
103
if(!body.innerHTML) return body;
104
if(body.innerHTML=="<br>") body.innerHTML = "<pre> </pre>";
105
else body.innerHTML = "<pre>"+body.innerHTML+"</pre>";
107
return document.getElementsByTagName('pre')[0];
110
// syntax highlighting parser
111
syntaxHighlight : function(flag) {
112
//if(document.designMode=='off') document.designMode='on'
113
if(flag != 'init') { window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));}
114
editor = CodePress.getEditor();
115
o = editor.innerHTML;
116
o = o.replace(/<br>/g,'\n');
117
o = o.replace(/<.*?>/g,'');
118
x = z = this.split(o,flag);
119
x = x.replace(/\n/g,'<br>');
121
if(arguments[1]&&arguments[2]) x = x.replace(arguments[1],arguments[2]);
123
for(i=0;i<Language.syntax.length;i++)
124
x = x.replace(Language.syntax[i].input,Language.syntax[i].output);
126
editor.innerHTML = this.actions.history[this.actions.next()] = (flag=='scroll') ? x : o.split(z).join(x);
127
if(flag!='init') this.findString();
130
getLastWord : function() {
131
var rangeAndCaret = CodePress.getRangeAndCaret();
132
words = rangeAndCaret[0].substring(rangeAndCaret[1]-40,rangeAndCaret[1]);
133
words = words.replace(/[\s\n\r\);\W]/g,'\n').split('\n');
134
return words[words.length-1].replace(/[\W]/gi,'').toLowerCase();
137
snippets : function(evt) {
138
var snippets = Language.snippets;
139
var trigger = this.getLastWord();
140
for (var i=0; i<snippets.length; i++) {
141
if(snippets[i].input == trigger) {
142
var content = snippets[i].output.replace(/</g,'<');
143
content = content.replace(/>/g,'>');
144
if(content.indexOf('$0')<0) content += cc;
145
else content = content.replace(/\$0/,cc);
146
content = content.replace(/\n/g,'<br>');
147
var pattern = new RegExp(trigger+cc,'gi');
148
evt.preventDefault(); // prevent the tab key from being added
149
this.syntaxHighlight('snippets',pattern,content);
154
readOnly : function() {
155
document.designMode = (arguments[0]) ? 'off' : 'on';
158
complete : function(trigger) {
159
window.getSelection().getRangeAt(0).deleteContents();
160
var complete = Language.complete;
161
for (var i=0; i<complete.length; i++) {
162
if(complete[i].input == trigger) {
163
var pattern = new RegExp('\\'+trigger+cc);
164
var content = complete[i].output.replace(/\$0/g,cc);
165
parent.setTimeout(function () { CodePress.syntaxHighlight('complete',pattern,content)},0); // wait for char to appear on screen
170
getCompleteChars : function() {
172
for(var i=0;i<Language.complete.length;i++)
173
cChars += '|'+Language.complete[i].input;
177
getCompleteEndingChars : function() {
179
for(var i=0;i<Language.complete.length;i++)
180
cChars += '|'+Language.complete[i].output.charAt(Language.complete[i].output.length-1);
184
completeEnding : function(trigger) {
185
var range = window.getSelection().getRangeAt(0);
187
range.setEnd(range.endContainer, range.endOffset+1)
192
var next_character = range.toString()
193
range.setEnd(range.endContainer, range.endOffset-1)
194
if(next_character != trigger) return false;
196
range.setEnd(range.endContainer, range.endOffset+1)
197
range.deleteContents();
202
shortcuts : function() {
203
var cCode = arguments[0];
204
if(cCode==13) cCode = '[enter]';
205
else if(cCode==32) cCode = '[space]';
206
else cCode = '['+String.fromCharCode(charCode).toLowerCase()+']';
207
for(var i=0;i<Language.shortcuts.length;i++)
208
if(Language.shortcuts[i].input == cCode)
209
this.insertCode(Language.shortcuts[i].output,false);
212
getRangeAndCaret : function() {
213
var range = window.getSelection().getRangeAt(0);
214
var range2 = range.cloneRange();
215
var node = range.endContainer;
216
var caret = range.endOffset;
217
range2.selectNode(node);
218
return [range2.toString(),caret];
221
insertCode : function(code,replaceCursorBefore) {
222
var range = window.getSelection().getRangeAt(0);
223
var node = window.document.createTextNode(code);
224
var selct = window.getSelection();
225
var range2 = range.cloneRange();
226
// Insert text at cursor position
227
selct.removeAllRanges();
228
range.deleteContents();
229
range.insertNode(node);
230
// Move the cursor to the end of text
231
range2.selectNode(node);
232
range2.collapse(replaceCursorBefore);
233
selct.removeAllRanges();
234
selct.addRange(range2);
237
// get code from editor
238
getCode : function() {
239
if(!document.getElementsByTagName('pre')[0] || editor.innerHTML == '')
240
editor = CodePress.getEditor();
241
var code = editor.innerHTML;
242
code = code.replace(/<br>/g,'\n');
243
code = code.replace(/\u2009/g,'');
244
code = code.replace(/<.*?>/g,'');
245
code = code.replace(/</g,'<');
246
code = code.replace(/>/g,'>');
247
code = code.replace(/&/gi,'&');
251
// put code inside editor
252
setCode : function() {
253
var code = arguments[0];
254
code = code.replace(/\u2009/gi,'');
255
code = code.replace(/&/gi,'&');
256
code = code.replace(/</g,'<');
257
code = code.replace(/>/g,'>');
258
editor.innerHTML = code;
260
document.getElementsByTagName('body')[0].innerHTML = '';
263
addChangeHandler : function(handler) {
264
document.changehandlers.push(handler);
267
// undo and redo methods
269
pos : -1, // actual history position
270
history : [], // history vector
273
editor = CodePress.getEditor();
274
if(editor.innerHTML.indexOf(cc)==-1){
275
if(editor.innerHTML != " ")
276
window.getSelection().getRangeAt(0).insertNode(document.createTextNode(cc));
277
this.history[this.pos] = editor.innerHTML;
280
if(typeof(this.history[this.pos])=='undefined') this.pos ++;
281
editor.innerHTML = this.history[this.pos];
282
if(editor.innerHTML.indexOf(cc)>-1) editor.innerHTML+=cc;
283
CodePress.findString();
287
// editor = CodePress.getEditor();
289
if(typeof(this.history[this.pos])=='undefined') this.pos--;
290
editor.innerHTML = this.history[this.pos];
291
CodePress.findString();
294
next : function() { // get next vector position and clean old ones
295
if(this.pos>20) this.history[this.pos-21] = undefined;
302
window.addEventListener('load', function() { CodePress.initialize('new'); }, true);