/vi/vi.js
JavaScript | 3799 lines | 3619 code | 93 blank | 87 comment | 740 complexity | 775a4d065f9b0a74ceac66f1a6152e71 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, LGPL-2.0, MIT, Unlicense
Large files files are truncated, but you can click here to view the full file
- /*
- JSVI - VI in JavaScript.
- Copyright (C) 2006-2008 Internet Connection, Inc.
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- */
- var emacsen = false;
- /* TODO
- sortof bugs:
- the toolbar is kind of ugly- possibly replace with an image-version-
- or more likely- replace with styled buttons
- ideas:
- uneditable sections?
- a ruler at the 72 character mark? possibly word-wrapping?
- want to switch between vi/emacs modes?
- */
- var term_cols;
- var term_rows;
- var term_win_width;
- var term_win_height;
- var term_cur_width;
- var tools;
- var suggest;
- var backing;
- var tagstyle = 0;
- var line_height = 0;
- var cclick = undefined;
- var mode = 0;
- var accum = 0;
- var lastaccum = 0;
- var marks = new Object();
- var registers = new Object();
- var lastreg = '';
- var lastcommand;
- var lastmotion;
- var cursoriv;
- var drawiv;
- var printer;
- var term;
- var base = 0;
- var left = 0;
- var vselm = 0;
- var vselx = undefined;
- var vsely = undefined;
- var vseld = false;
- var lastkey = undefined;
- var statustext = '';
- var command = '';
- var oldcommand = '';
- var commandleft = 0;
- var savex, savey;
- var once = true;
- var cursorx, cursory;
- var file = new Array();
- var tags = new Array();
- var palette;
- var cursor;
- var yank_buffer = undefined;
- var term_save_h = new Array();
- var term_save_ss;
- var term_save_mc;
- var term_save_kd;
- var term_save_kp;
- var term_save_rs;
- var term_save_op;
- var lastsearch;
- var lastsubst;
- var lastflags;
- var undoset = undefined;
- var undoline = undefined;
- var undoy = -1;
- var term_ex_motion;
- var vi_ft_reg;
- var viflags = '';
- var lastinsert = '';
- var spelling = false;
- var spellcheck = new Object();
- var safewords = new Object();
- var brokenwords = new Object();
- var suggestions = new Object();
- safewords['jsvi']=true;
- safewords['javascript']=true;
- safewords['URL']=true;
- safewords['hyperlinks']=true;
- safewords['HTML']=true;
- safewords['UNIX']=true;
- safewords['Firefox']=true;
- safewords['MSIE']=true;
- safewords['Ctrl']=true;
- safewords['vi']=true;
- safewords['vi-keys']=true;
- safewords[':hardcopy']=true;
- safewords['unicode-aware']=true;
- safewords['developer-centric']=true;
- var doing_backing_paste = false;
- function _true() { return true; }
- function _false() { return false; }
- function _cbrestore() {
- var i;
- for (i = 0; i < term_save_h.length; i++) {
- var z = term_save_h[i];
- z();
- }
- term_save_h = new Array();
- }
- function _cbw(nx,ny) {
- var nz = window['on'+nx];
- (function(x,y,z) {
- term_save_h[term_save_h.length] = function() {
- window['on'+x] = z;
- };
- })(nx,ny,nz);
- window['on'+nx]=ny;
- }
- function _cbd(nx,ny) {
- var nz = document['on'+nx];
- (function(x,y,z) {
- term_save_h[term_save_h.length] = function() {
- document['on'+x] = z;
- };
- })(nx, ny, nz);
- document['on'+nx]=ny;
- }
- function _rer(re,t,aa) {
- if (RegExp.rightContext != undefined) return RegExp.rightContext;
- // emulate rightContext
- var s = re.toString();
- if ((s.substr(0,2) == '/^' && s.substr(s.length-1,1) == '/') || s.substr(0,1) == '^') {
- // bound at beginning
- return t.substr(aa[0].length, t.length - aa[0].length);
- }
- if ((s.substr(0,1) == '/' && s.substr(s.length-2,2) == '$/') || s.substr(s.length-1,1) == '$') {
- // bound at end
- return "";
- }
- var j = t.lastIndexOf(aa[0]);
- if (j != -1) return t.substr(j.t.length-j);
- return t;
- }
- function _rel(re,t,aa) {
- if (RegExp.leftContext != undefined) return RegExp.leftContext;
- // emulate leftContext
- var s = re.toString();
- if ((s.substr(0,2) == '/^' && s.substr(s.length-1,1) == '/') || s.substr(0,1) == '^') {
- // bound at beginning
- return "";
- }
- if ((s.substr(0,1) == '/' && s.substr(s.length-2,2) == '$/') || s.substr(s.length-1,1) == '$') {
- // bound at end
- return t.substr(0, t.length - aa[0].length);
- }
- var j = t.indexOf(aa[0]);
- if (j != -1) return t.substr(0,j);
- return "";
- }
- function _hra(x) {
- var i;
- var cx = 0;
- var t = '';
- var g = '';
- for (i = 0; i < x.length; i++) {
- var x3 = x.substr(i,3);
- var gx = String.fromCharCode(cx);
- if (x3 == '<b>') {
- cx = cx | 1;
- i += 2;
- } else if (x3 == '</b') {
- cx = (cx | 1) ^ 1;
- i += 3;
- } else if (x3 == '<u>') {
- cx = cx | 2;
- i += 2;
- } else if (x3 == '</u') {
- cx = (cx | 2) ^ 2;
- i += 3;
- } else if (x3 == '<i>') {
- cx = cx | 16;
- i += 2;
- } else if (x3 == '</i') {
- cx = (cx | 16) ^ 16;
- i += 3;
- } else if (x3 == '<sp') { // <span class="rv">
- cx = cx | 4;
- i += 16;
- } else if (x3 == '</s') { // </span>
- cx = (cx | 4) ^ 4;
- i += 6;
- } else if (x3 == '&am') { // &
- t += '&';
- g += gx;
- i += 4;
- } else if (x3 == '<') { // <
- t += '<';
- g += gx;
- i += 3;
- } else {
- t += x.substr(i,1);
- g += gx;
- }
- }
- var aa = new Array();
- aa[0] = t;
- aa[1] = g;
- return aa;
- }
- function _rtf(t,g) {
- var cx = 0;
- var i;
- var o = '';
- if (t == undefined) {
- t = '';
- g = '';
- }
- for (i = 0; i < t.length; i++) {
- var gx = g.substr(i, 1).charCodeAt(0);
- var tx = t.substr(i, 1);
- if (tx == "<") tx = "<";
- else if (tx == '&') tx = '&';
- if (gx != cx) {
- if ((gx & 1) && !(cx & 1)) {
- o += "<b>";
- } else if (!(gx & 1) && (cx & 1)) {
- o += "</b>";
- }
- if ((gx & 2) && !(cx & 2)) {
- o += "<u>";
- } else if (!(gx & 2) && (cx & 2)) {
- o += "</u>";
- }
- if ((gx & 4) && !(cx & 4)) {
- o += "<span class=\"rv\">";
- } else if (!(gx & 4) && (cx & 4)) {
- o += "</span>";
- }
- if ((gx & 16) && !(cx & 16)) {
- o += "<i>";
- } else if (!(gx & 16) && (cx & 16)) {
- o += "</i>";
- }
- cx = gx;
- }
- o += tx;
- }
- return o;
- }
- function _rtfl(y) {
- return _rtf(file[y],tags[y]);
- }
- function _dfx(q) {
- /*@cc_on @*/
- /*@if (1) return
- @end @*/
- q.style.position = 'fixed';
- }
- function term_freeze() {
- var i;
- var o = '';
- for (i = 0; i < file.length; i++) {
- o += _rtfl(i)+"\n";
- }
- return o;
- }
- function _term_update_printer() {
- var i;
- var o = '';
- for (i = 0; i < file.length; i++) {
- o += _rtfl(i)+"<br/>";
- }
- printer.innerHTML = o;
- }
- function term_thaw(s) {
- var a = s.split("\n");
- var i;
- var aa;
- file = new Array();
- tags = new Array();
- var o = '';
- for (i = 0; i < a.length; i++) {
- o += a[i] + "<br/>";
- aa = _hra(a[i]);
- file[i] = aa[0];
- tags[i] = aa[1];
- }
- printer.innerHTML = o;
- }
- function _mxo(z,y) {
- var i;
- var o = '';
- for (i = 0; i < z.length; i++) {
- o += String.fromCharCode(z.substr(i, 1).charCodeAt(0) | y);
- }
- return o;
- }
- function _mxs(n,y) {
- var z = String.fromCharCode(y);
- var i;
- var o = '';
- for (i = 0; i < n; i++) {
- o += z;
- }
- return o;
- }
- function _zeros(n) {
- return _mxs(n,0);
- }
- function _fauc() {
- var d = document.getElementsByTagName('A');
- var i;
- for (i = 0; i < d.length; i++) {
- var j = d[i];
- if (j._len && j._term) {
- if (j._row == (base+cursory) && (left+cursorx) >= j._col && (left+cursorx) <= (j._col+j._len)) {
- return j;
- }
- }
- }
- return undefined;
- }
- function _pass_click(e) {
- var z = _fauc();
- if (z && z.onclick) return z.onclick();
- return false;
- }
- function _pass_dblclick(e) {
- var z = _fauc();
- if (z && z.ondblclick) return z.ondblclick();
- return false;
- }
- function _cancel_ev(e) {
- if (!e) e = window.event;
- if (!e) return false;
- if (e.preventDefault) e.preventDefault();
- if (e.stopPropagation) e.stopPropagation();
- return false;
- }
- function _willclick(e) {
- if (window.event) {
- if (!e) e = window.event;
- }
- if (!e) return true;
- if (cclick != undefined) window.clearTimeout(cclick);
- var x = e.clientX;
- var y = e.clientY;
- cclick = window.setTimeout(function() {
- cclick=undefined;
- _cursortoxy(x,y);
- }, 200);
- return false;
- }
- function _subclick(e) {
- return _willclick(e);
- }
- function _srep(e) {
- if (!e) e = window.event;
- if (e.preventDefault) e.preventDefault();
- if (e.stopPropagation) e.stopPropagation();
- e.cancelBubble=true;
- var y = this._row;
- var x = this._col;
- var len = this._len;
- var rep = this._word;
- var t = (file[y]);
- var g = (tags[y]);
- var w = (t.substr(x, len));
- var st = (g.substr(x, len));
- while (st.length < rep.length) {
- st = st + st;
- }
- if (st.length > rep.length) {
- st = st.substr(0, rep.length);
- }
- term_save_undo(); // save undo!
- file[y] = t.substr(0, x) + rep + t.substr(x+len, t.length-(x+len));
- tags[y] = g.substr(0, x) + st + g.substr(x+len, t.length-(x+len));
- suggest.style.display = 'none';
- suggest._visible = false;
- while (suggest.firstChild) suggest.removeChild(suggest.firstChild);
- if (w == rep) {
- statustext = '';
- } else {
- statustext = 'Replaced "' + w + '" with "' + rep + '"';
- }
- term_redraw();
- return false;
- }
- function _rl(w,h) {
- var x = document.createElement('DIV');
- x.style.overflow = 'hidden';
- x.style.height = h + 'px';
- x.style.marginLeft = w + 'px';
- x.style.marginRight = w + 'px';
- x.style.backgroundColor = palette[0];
- x.style.display = 'block';
- x.style.innerHTML = ' ';
- x.style.fontFamily = 'monospace';
- return x;
- }
- function _ruo(t) {
- this.style.color = palette[0];
- this.style.backgroundColor = palette[1];
- }
- function _rux(t) {
- this.style.color = palette[1];
- this.style.backgroundColor = palette[0];
- }
- function _openurl(e) {
- var u = this._term;
- window.open(u,'_new');
- return true;
- }
- function _suggest(e) {
- var z = this;
- if (window.event) {
- if (!e) e = window.event;
- }
- if (e) {
- if (e.preventDefault) e.preventDefault();
- if (e.stopPropagation) e.stopPropagation();
- e.cancelBubble=true;
- }
- (function(q) {
- window.setTimeout(function(){
- _dosuggest(q);
- }, 10);
- })(z);
- return false;
- }
- function _dosuggest(z) {
- var x = 0;
- var y = 0;
- var xt = z._term;
- var wrow = z._row;
- var wcol = z._col;
- var wlen = z._len;
- while (z && z != document.body) {
- x += z.offsetLeft;
- y += z.offsetTop;
- z = z.offsetParent;
- }
- suggest._visible = true;
- suggest.style.top = y + 'px';
- suggest.style.left = x + 'px';
- suggest.style.display = 'block';
- suggest.style.zIndex = '3';
- suggest.style.padding = '2px';
- while (suggest.firstChild) suggest.removeChild(suggest.firstChild);
- var sg = document.createElement('DIV');
- sg.style.backgroundColor = palette[0];
- sg.style.color = palette[1];
- sg.style.fontSize = '100%';
- sg.style.padding = '2px';
- sg.style.textAlign = 'center';
- sg.style.cursor = 'default';
- var sa = suggestions[xt];
- var i;
- var fs = 200;
- var fd = parseInt((100 / sa.length) * 1.5);
- var bf;
- for (i = 0; i < sa.length; i++) {
- var da = document.createElement('A');
- da.href = 'javascript:void(0)';
- da.onclick = _srep;
- da._word = sa[i];
- da._row = wrow;
- da._col = wcol;
- da._len = wlen;
- da.onmouseover = _ruo;
- da.onmouseout = _rux;
- da.style.margin = '4px';
- da.style.textDecoration = 'none';
- da.style.display = 'block';
- da.style.color = palette[1];
- da.style.backgroundColor = palette[0];
- da.style.fontSize = fs + '%';
- fs -= fd;
- if (fs <= 100) fs = 100;
- da.appendChild(document.createTextNode(sa[i]));
- // err...
- da.appendChild(document.createElement('BR'));
- if ((wrow-base) > term_rows-(sa.length+1)) {
- sg.insertBefore(da, sg.firstChild);
- bf = true;
- } else if (((i % 2) == 0) || (wrow < (sa.length+1))) {
- sg.appendChild(da);
- } else {
- sg.insertBefore(da, sg.firstChild);
- }
- da = undefined; // break
- }
- if (sa.length == 0) {
- sg.appendChild(document.createTextNode('No matches'));
- }
- var da = document.createElement('A');
- da.href = 'javascript:void(0)';
- da.onclick = _srep;
- da._word = xt;
- da._row = wrow;
- da._col = wcol;
- da._len = wlen;
- da.style.margin= '4px';
- da.style.textDecoration = 'none';
- da.style.color = palette[1];
- da.style.backgroundColor = palette[0];
- da.style.borderBottom = '1px dashed red';
- da.style.fontSize = '100%';
- da.onmouseover = _ruo;
- da.onmouseout = _rux;
- da.appendChild(document.createTextNode(xt));
- if (wrow > term_rows-(sa.length+1)) {
- sg.insertBefore(da, sg.firstChild);
- } else {
- sg.appendChild(da);
- }
- // rounded top and bottom
- suggest.appendChild(_rl(3,1));
- suggest.appendChild(_rl(2,1));
- suggest.appendChild(_rl(1,2));
- suggest.appendChild(sg);
- suggest.appendChild(_rl(1,2));
- suggest.appendChild(_rl(2,1));
- suggest.appendChild(_rl(3,1));
- // msie needs to recalculate these things manually... grr...
- // this doesn't work because msie doesn't calculate offsetwidth (thtphtpht)
- var zq;
- var mw = 11;
- if (mw < xt.length) mw = xt.length;
- for (i = 0; i < sa.length; i++) {
- if (mw < sa[i].length) mw = sa[i].length;
- }
- if (mw) {
- mw *= (term_cur_width*2);
- mw += 16;
- suggest.style.width = mw + 'px';
- }
- var sx = parseInt(sg.offsetWidth / 4);
- if (x < sx) {
- x = 0;
- } else {
- x -= sx;
- suggest.style.left = x + 'px';
- }
- var sy = parseInt(sg.offsetHeight / 4);
- if (bf) {
- suggest.style.top = '';
- suggest.style.bottom = '0px';
- } else if (y < sy) {
- y = 0;
- suggest.style.top = '0px';
- suggest.style.bottom = '';
- } else {
- y -= sy;
- suggest.style.top = y + 'px';
- suggest.style.bottom = '';
- }
- statustext = 'Suggestions for: ' + xt;
- term_redraw();
- da = undefined; // break
- sg = undefined; // break
- }
- function _backing_paste_real() {
- doing_backing_paste = false;
- term_redraw();
- if (!backing.value) return;
- if (backing._lastvalue == backing.value) {
- return;
- }
- backing._lastvalue = backing.value;
- term_paste(false, backing.value);
- term_redraw();
- }
- function _msie_paste() {
- var chunk = "new content associated with this object";
- event.returnValue = false;
- term_paste(false, window.clipboardData.getData("Text", chunk));
- }
- function _backing_paste() {
- _update_backing();
- if (!doing_backing_paste) {
- doing_backing_paste = true;
- window.setTimeout(_backing_paste_real, 10);
- }
- }
- function _update_backing() {
- if (!backing) return;
- /*@cc_on @*/
- /*@if (1) return
- @end @*/
- backing.focus();
- backing.select();
- }
- function _yaty(y) {
- if (line_height) return parseInt(y/line_height);
- var zx;
- var qx = term.firstChild;
- var nh = 0;
- while (qx && qx != document.body) {
- nh += qx.offsetTop;
- qx = qx.offsetParent;
- }
- var ny = 0;
- var cy = 0;
- for (zx = term.firstChild; zx; zx = zx.nextSibling) {
- nh += (zx.offsetHeight + 4);
- if (y <= nh) {
- cy = ny;
- break;
- }
- ny++;
- }
- return cy;
- }
- function _cursortoxy(x,y) {
- // this is a little gross...
- var sx = cursorx;
- cursorx = parseInt(x / term_cur_width);
- term_redraw();
- var sy = cursory;
- cursory = _yaty(y);
- if (cursory >= (term_rows-1)) {
- cursory = sy;
- cursorx = sx;
- sy = 0;
- }
- term_scrollto();
- term_calcx();
- if (cursory != sy) {
- term_redraw();
- } else {
- term_calcy();
- }
- _update_backing();
- return true;
- }
- function _mousescroll(e) {
- if (!e) e = window.event;
- var d = 0;
- if (e.wheelDelta) {
- d = e.wheelDelta;
- if (d < 0) d = 1; else d = -1;
- } else if (e.detail) {
- d = e.detail;
- } else {
- return true;
- }
- if (d < 0) {
- if (base > 0) base--;
- } else if (d > 0) {
- if (base < (file.length - (term_rows-1))) base++;
- }
- term_redraw();
- return false;
- }
- function _mousedown(e) {
- if (suggest._visible) return true;
- if (!e) e = window.event;
- var y = _yaty(e.clientY);
- if (y >= (term_rows-1)) return true;
- _willclick(e);
- vseld = true;
- vselm = 0;
- vselx = undefined;
- vsely = undefined;
- return false;
- }
- function _mousemove(e) {
- if (!e) e = window.event;
- if (e) {
- if (e.preventDefault) e.preventDefault();
- if (e.stopPropagation) e.stopPropagation();
- e.cancelBubble=true;
- }
- if (window.getSelection) {
- var s = window.getSelection();
- if (s.removeAllRanges) s.removeAllRanges();
- }
- if (document.selection && document.selection.empty) {
- eval('try{document.selection.empty();}catch(e){}');
- }
- // fixup selection
- _update_backing();
- if (suggest._visible || !vseld) return true;
- if (vseld) {
- if (vselx == undefined && vsely == undefined) {
- // turn on v here
- vselm = 1;
- vselx = cursorx+left;
- vsely = cursory+base;
- }
- _willclick(e);
- return true;
- }
- return true;
- }
- function _mouseup(e) {
- if (suggest._visible || !vseld) return true;
- vseld = false;
- if (vselm && vselx != undefined && vsely != undefined) {
- // okay, we HAVE selection
- var rr = lastreg;
- lastreg = '*';
- term_vi_set('y');
- term_vi_unset('d');
- term_select();
- term_operate();
- lastreg = rr;
- } else {
- vselm = 0;
- }
- _willclick(e);
- return false;
- }
- function _mouseclick(e) {
- if (!e) e = window.event;
- var y = _yaty(e.clientY);
- if (y >= (term_rows-1)) return true;
- vselm = 0;
- _cursorto(e);
- return true;
- }
- function _cursorto(e) {
- if (!e) e = window.event;
- var x = e.clientX;
- var y = e.clientY;
- if (suggest._visible) {
- suggest.style.display = 'none';
- suggest._visible = false;
- statustext = '';
- }
- return _cursortoxy(x,y);
- }
- function _word(s) {
- var t = s.replace(/[.?!,:]*$/,"");
- if (t) s = t;
- t = s.replace(/[ \r\n\t]/,"");
- if (t) s = t;
- return s;
- }
- function _safe(s) {
- if (s.match(/^[ \r\n\t]*<.*>[ \r\n\t]*$/)) return true;
- return false;
- }
- function _xhttp() {
- var xmlhttp=false;
- /*@cc_on @*/
- /*@if (@_jscript_version >= 5)
- try {
- xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
- } catch (e) {
- try {
- xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
- } catch (E) {
- xmlhttp = false;
- }
- }
- @end @*/
- if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
- xmlhttp = new XMLHttpRequest();
- }
- if (!xmlhttp) return new Object(); // fake out caller
- return xmlhttp;
- }
- function term_setmode(n) {
- mode = n;
- lastinsert = '';
- }
- function term_roll_yank() {
- registers["9"] = registers["8"];
- registers["8"] = registers["7"];
- registers["7"] = registers["6"];
- registers["6"] = registers["5"];
- registers["5"] = registers["4"];
- registers["4"] = registers["3"];
- registers["3"] = registers["2"];
- registers["2"] = registers["1"];
- registers["1"] = registers["0"];
- if (lastreg == '0') {
- registers[""] = yank_buffer;
- }
- if (lastreg == "" || lastreg == "*") {
- backing._lastvalue = yank_buffer;
- backing.value = yank_buffer;
- }
- registers["0"] = yank_buffer;
- }
- function term_justify() {
- var y = cursory+base;
- var i;
- if (!accum) accum = 1;
- var t = (file[y]);
- file[p] = t.replace(/[ ][ ]*$/,"");
- tags[p] = tags[p].substr(0, file[p].length);
- for (i = 0; i < accum; i++) {
- var t = file[i+y+1];
- var g = tags[i+y+1];
- term_delete(i+y+1); // ignore return
- t = t.replace(/^[ ][ ]*/,"");
- g = g.substr(g.length - t.length, t.length);
- if (t != '') {
- file[y] = file[y] + " " + t;
- tags[y] = tags[y] + "\0" + g;
- }
- }
- file[y] = file[y].replace(/^[ ][ ]*/,"");
- tags[y] = tags[y].substr(tags[y].length - file[y].length, file[y].length);
- accum = 0;
- }
- function term_vi_bb() {
- return term_skipbackward(/[ ][^ ][^ ]*[ ]*$/);
- }
- function term_vi_b() {
- return term_skipbackward(/[^a-zA-Z0-9_][a-zA-Z0-9_][a-zA-Z0-9_]*[^a-zA-Z0-9_]*$/);
- }
- function term_vi_tt() {
- var t = (file[cursory+base]);
- var i;
- var w = term_cols;
- for (i = (cursorx+left)-1; i >= 0; i--) {
- var c = (t.substr(i, 1));
- if (c == vi_ft_reg) {
- cursorx = (i - left)+1;
- if (cursorx >= w) {
- left = 0;
- cursorx = i+1;
- }
- return true;
- }
- }
- return false;
- }
- function term_vi_t() {
- var t = (file[cursory+base]);
- var i;
- for (i = (cursorx+left)+1; i < t.length; i++) {
- var c = (t.substr(i, 1));
- if (c == vi_ft_reg) {
- cursorx = (i - left)-1;
- if (term_vi_flag('d') || term_vi_flag('c') || term_vi_flag('y')) cursorx++;
- if (cursorx < 0) {
- left = 0;
- cursorx = i-1;
- }
- return true;
- }
- }
- return false;
- }
- function term_vi_ff() {
- var t = (file[cursory+base]);
- var i;
- for (i = (cursorx+left)-1; i >= 0; i--) {
- var c = (t.substr(i, 1));
- if (c == vi_ft_reg) {
- cursorx = i - left;
- if (cursorx < 0) {
- left = 0;
- cursorx = i;
- }
- return true;
- }
- }
- return false;
- }
- function term_vi_f() {
- var t = (file[cursory+base]);
- var i;
- var w = term_cols;
- for (i = (cursorx+left)+1; i < t.length; i++) {
- var c = (t.substr(i, 1));
- if (c == vi_ft_reg) {
- cursorx = i - left;
- if (term_vi_flag('d') || term_vi_flag('c') || term_vi_flag('y')) cursorx++;
- if (cursorx >= w) {
- left = 0;
- cursorx = i;
- }
- return true;
- }
- }
- return false;
- }
- function term_vi_eof() {
- cursorx = 0;
- left = 0;
- base = 0;
- if (accum) {
- cursory = accum-1;
- return false;
- } else {
- base = file.length - (term_rows-1);
- cursory = term_rows-1;
- if (base < 0) {
- base = 0;
- cursory = file.length-1;
- }
- }
- term_redraw();
- return true;
- }
- function term_vi_top() {
- cursorx = 0;
- base = 0;
- left = 0;
- if (accum) {
- cursory = accum-1;
- return false;
- } else {
- cursory = 0;
- }
- return true;
- }
- function term_vi_h() { cursorx--; term_scrollto(); return true; }
- function term_vi_j() { cursory++; term_scrollto(); return true; }
- function term_vi_k() { cursory--; term_scrollto(); return true; }
- function term_vi_l() {
- cursorx++;
- // hack
- var a = mode;
- mode = 1;
- term_scrollto();
- mode = a;
- return true;
- }
- function term_vi_ll() {
- cursory = term_rows-2;
- return true;
- }
- function term_vi_mm() {
- cursory = parseInt((term_rows-2) / 2);
- cursorx = 0; left = 0;
- return true;
- }
- function term_vi_hh() {
- cursory = 0;
- return true;
- }
- function term_vi_ob() {
- cursory--;
- return term_skipreverse2(/^[ ]*$/, 0);
- }
- function term_vi_cb() {
- cursory++;
- return term_skipforward(/^[ ]*$/, 0);
- }
- function term_vi_ww() {
- return term_skipforward(/[ ][^ ]/, 0);
- }
- function term_vi_w() {
- return term_skipforward(/[^a-zA-Z0-9_(){}<>][a-zA-Z0-9_(){}<>]/, 0);
- }
- function term_vi_flag(f) {
- return (viflags.indexOf(f) == -1) ? false : true;
- }
- function term_vi_unset(f) {
- var j = viflags.indexOf(f);
- if (j == -1) return;
- viflags = viflags.substr(0, j) + viflags.substr(j+1, (viflags.length-j)-1);
- }
- function term_vi_set(f) {
- if (viflags.indexOf(f) == -1) viflags += ''+f;
- }
- function term_vi_bounce() {
- var y = cursory+base;
- var t = (file[y]);
- var x = cursorx+left;
- var z1 = '[{()}]';
- var z2 = ']})({[';
- while (x < t.length) {
- var z = z1.indexOf(t.substr(x,1));
- if (z == -1) {
- x++;
- continue;
- }
- var d = (z > 2) ? -1 : 1;
- var c=z2.substr(z,1);
- while (y >= 0 && y < file.length) {
- while (x > 0 && x < t.length) {
- if (t.substr(x,1) == c) {
- cursorx = x-left;
- cursory = y;
- base = 0;
- term_scrollto();
- return true;
- }
- x += d;
- }
- if (d == -1) {
- y--;
- t = file[y];
- x = (t.length-1)
- } else {
- y++;
- t = file[y];
- x = 0;
- }
- }
- }
- return false;
- }
- function term_vi_eol() {
- var t = (file[cursory+base]);
- cursorx = (t.length)-left;
- if (cursorx < 0) {
- left = 0;
- cursorx = (t.length);
- }
- return true;
- }
- function term_vi_line() {
- var t = (file[cursory+base]);
- left = 0;
- cursorx = t.length+1;
- return true;
- }
- function term_vi_ee() {
- return term_skipforward(/[^ ][ ]/, 1);
- }
- function term_vi_e() {
- if (term_skipforward(/[a-zA-Z0-9_(){}<>][^a-zA-Z0-9_(){}<>]/, 1)) {
- if (term_vi_flag('d') || term_vi_flag('c') || term_vi_flag('y')) cursorx++;
- return true;
- }
- return false;
- }
- function term_vi_v() {
- cursorx = vselx - left;
- if (cursorx < 0) {
- left = 0;
- cursorx = vselx;
- }
- cursory = vsely - base;
- if (cursory < 0) {
- base = 0;
- cursory = vsely;
- }
- }
- function term_vi_vv() {
- term_vi_v();
- term_vi_line();
- }
- function term_select() {
- if (vselm == 1) {
- term_ex_motion = term_vi_v;
- } else if (vselm == 2) {
- term_ex_motion = term_vi_vv;
- cursorx = 0;
- left = 0;
- }
- }
- function term_indent(y,amount) {
- if (file[y] == undefined) {
- file[y] = '';
- tags[y] = '';
- }
- file[y] = _mxs(amount*4,32)+file[y];
- tags[y] = _mxs(amount*4,tagstyle)+tags[y];
- }
- function term_unindent(y,amount) {
- amount*=4;
- while (amount > 0 && file[y].substr(0, 1) == ' ') {
- file[y] = file[y].substr(1, file[y].length-1);
- amount--;
- }
- }
- function term_operate() {
- term_save_undo();
- var fa = accum;
- if (!fa) fa = 1;
- var sx = cursorx+left;
- var sy = cursory+base;
- while (fa > 0) {
- var t = file[base+cursory];
- if (t == undefined) break;
- if (fa > 1 && ((cursorx+left) >= t.length)) {
- cursory++;
- cursorx = 0;
- left = 0;
- }
- term_ex_motion();
- fa--;
- }
- fa = accum;
- if (!fa) fa = 1;
- accum = 0;
- var ex = cursorx+left;
- var ey = cursory+base;
- var i;
- if (ey < sy) {
- i = ey;
- ey = sy;
- sy = i;
- }
- if (ex < sx) {
- i = ex;
- ex = sx;
- sx = i;
- }
- var t = file[ey];
- var g = tags[ey];
- var restore = false;
- if (term_vi_flag('c')) term_vi_set('d');
- if (vselm != 2 && ey == sy && ex <= t.length) {
- if (vselm) ex++;
- if (ex != sx) {
- if (term_vi_flag('F')) {
- // styling
- tags[ey] = (g.substr(0, sx)) + _mxs(ex-sx, tagstyle) + (g.substr(ex, g.length-ex));
- } else if (term_vi_flag('d') || term_vi_flag('y')) {
- yank_buffer = _rtf(t.substr(sx, ex-sx),
- g.substr(sx,ex-sx));
- }
- if (term_vi_flag('d')) {
- file[ey] = (t.substr(0, sx)) + (t.substr(ex, t.length-ex));
- tags[ey] = (g.substr(0, sx)) + (g.substr(ex, g.length-ex));
- if (lastreg != '_') {
- registers[lastreg] = yank_buffer;
- term_roll_yank();
- }
- } else if (term_vi_flag('y')) {
- registers[lastreg] = yank_buffer;
- term_roll_yank();
- }
- if (term_vi_flag('>')) {
- term_indent(ey,fa);
- } else if (term_vi_flag('<')) {
- term_unindent(ey,fa);
- }
- }
- } else if (vselm == 1) {
- if (term_vi_flag('d') || term_vi_flag('y') || term_vi_flag('F')) {
- yank_buffer = '';
- var al, bl;
- for (i = sy; i <= ey; i++) {
- t = file[i];
- g = tags[i];
- if (i == sy) {
- if ((sy == vsely && sx == vselx) || (sy != vsely && sx != vselx)) {
- al = sx;
- } else {
- al = ex;
- }
- bl = t.length;
- } else if (i == ey) {
- al = 0;
- if ((sy == vsely && sx == vselx) || (sy != vsely && sx != vselx)) {
- al = ex;
- } else {
- al = sx;
- }
- } else {
- al = 0;
- bl = t.length;
- }
- yank_buffer += _rtf(t.substr(al, bl-al),
- g.substr(al,bl-al));
- if (sy != ey || vselm == 2) yank_buffer += "\n";
- if (term_vi_flag('d')) {
- file[i] = t.substr(0, al) + t.substr(bl, t.length-bl);
- tags[i] = g.substr(0, al) + g.substr(bl, g.length-bl);
- } else if (term_vi_flag('F')) {
- tags[i] = g.substr(0, al) + _mxs(bl-al, tagstyle)
- + g.substr(bl, g.length-bl);
- }
- if (term_vi_flag('>')) {
- term_indent(i,fa);
- } else if (term_vi_flag('<')) {
- term_unindent(i,fa);
- }
- }
- if (lastreg != '_' && !term_vi_flag('F')) {
- registers[lastreg] = yank_buffer;
- term_roll_yank();
- }
- }
- } else {
- if (term_vi_flag('F')) {
- for (i = sy; i <= ey; i++) {
- tags[i] = _mxs(file[i].length, tagstyle);
- }
- } else if (term_vi_flag('d')) {
- yank_buffer = '';
- for (i = sy; i <= ey; i++) {
- yank_buffer += term_delete(sy);
- }
- if (lastreg != '_') {
- registers[lastreg] = yank_buffer;
- term_roll_yank();
- }
- } else if (term_vi_flag('y')) {
- yank_buffer = '';
- for (i = sy; i <= ey; i++) {
- yank_buffer += _rtfl(sy) + "\n";
- }
- registers[lastreg] = yank_buffer;
- term_roll_yank();
- } else if (term_vi_flag('>')) {
- for (i = sy; i <= ey; i++) {
- term_indent(i,fa);
- }
- } else if (term_vi_flag('<')) {
- for (i = sy; i <= ey; i++) {
- term_unindent(i,fa);
- }
- }
- }
- if (term_vi_flag('d') || term_vi_flag('F') || term_vi_flag('c')) {
- _term_update_printer();
- }
- if (term_vi_flag('d')) {
- term_vi_unset('d');
- lastcommand = 'd';
- lastmotion = term_ex_motion;
- restore = true;
- }
- if (term_vi_flag('y')) {
- term_vi_unset('y');
- restore = true;
- }
- if (term_vi_flag('F')) {
- term_vi_unset('F');
- restore = true;
- }
- lastaccum = accum;
- if (restore) {
- cursorx = sx - left;
- if (cursorx < 0) {
- cursorx = sx;
- left = 0;
- }
- cursory = sy - base;
- if (cursory < 0) {
- cursory = sy;
- base = 0;
- }
- term_scrollto();
- }
- if (term_vi_flag('c')) {
- term_vi_unset('c');
- term_setmode(1);
- }
- accum = 0;
- }
- function term_save_undo_line() {
- if ((base+cursory) != undoy) {
- undoy = base+cursory;
- undoline = _rtfl(undoy);
- }
- }
- function term_save_undo() {
- undoset = term_freeze();
- }
- function term_delete(i) {
- if (i > file.length) return '';
- var j;
- var z = file[i];
- var g = tags[i];
- for (j = i+1; j < file.length; j++) {
- file[j-1] = file[j];
- tags[j-1] = tags[j];
- }
- file=_pop(file);
- return _rtf(z,g) + "\n";
- }
- function term_skipreverse2(re, fuzz) {
- var y = cursory+base;
- var x = (cursorx+left)+(1+fuzz);
- for (;;) {
- var t = file[y];
- if (t == undefined) {
- // beep
- cursory = y - base;
- return false;
- }
- if (fuzz) t += " "; else t = " " + t;
- t = (t.substr(x, t.length-x));
- var aa = re.exec(t);
- if (!aa) {
- y--;
- x = 0;
- } else {
- x += _rel(re,t,aa).length;
- cursorx = x - left;
- cursory = y - base;
- return true;
- }
- }
- return false;
- }
- function term_skipforward(re, fuzz) {
- var y = cursory+base;
- var x = (cursorx+left)+(1+fuzz);
- for (;;) {
- var t = file[y];
- if (t == undefined) {
- // beep
- cursory = y - base;
- return false;
- }
- if (fuzz) t += " "; else t = " " + t;
- t = (t.substr(x, t.length-x));
- var aa = re.exec(t);
- if (!aa) {
- y++;
- x = 0;
- } else {
- x += _rel(re,t,aa).length;
- cursorx = x - left;
- cursory = y - base;
- return true;
- }
- }
- return false;
- }
- function term_skipbackward(re) {
- var y = cursory+base;
- var x = (cursorx+left)+1;
- for (;;) {
- var t = file[y];
- if (t == undefined) {
- // beep
- cursory = 0;
- cursorx = 0;
- return false;
- }
- t = " " + t.substr(0, x-1);
- if (t == '') {
- y--;
- t = file[y];
- if (t == undefined) continue;
- x = (t.length);
- continue;
- }
- var aa = re.exec(t);
- if (!aa) {
- left = 0;
- cursorx = 0;
- cursory = y - base;
- return true;
- } else {
- x = _rel(re,t,aa).length;
- cursorx = x - left;
- cursory = y - base;
- return true;
- }
- }
- return false;
- }
- function term_search(s, top, start, bottom) {
- var re = new RegExp(s.substr(1));
- var i;
- if (s.substr(0, 1) == '/') {
- statustext='';
- re.lastIndex = cursorx+left+1;
- for (i = start; i < bottom; i++) {
- var t = (file[i]);
- if (i == start) {
- t = t.substr(cursorx+left+1, t.length-(cursorx+left+1));
- }
- aa = re.exec(t);
- if (!aa) continue;
- var tx = _rel(re,t,aa).length;
- if (i == start) {
- tx += cursorx+left+1;
- }
- left = 0;
- base = 0;
- cursorx = tx;
- cursory = i;
- term_scrollto();
- return true;
- }
- statustext = 'search hit BOTTOM, continuing at TOP';
- for (i = top; i <= start; i++) {
- var aa = re.exec(file[i]);
- if (!aa) continue;
- left = 0;
- base = 0;
- cursorx = _rel(re,file[i],aa).length;
- cursory = i;
- term_scrollto();
- return true;
- }
- statustext = 'Pattern not found: ' + s.substr(1);
- } else {
- statustext='';
- for (i = start; i >= top; i--) {
- var t = file[i];
- if (t == undefined) continue;
- var tail = 0;
- if (i == start) {
- tail = (cursorx+left);
- } else {
- tail = t.length;
- }
- var right = tail;
- while (tail > 0) {
- tail--;
- var xj = t.substr(tail, right-tail);
- var aa = re.exec(xj);
- if (!aa) continue;
- var tx = tail+_rel(re,xj,aa).length;
- left = 0;
- base = 0;
- cursorx = tx;
- cursory = i;
- term_scrollto();
- return true;
- }
- }
- statustext = 'search hit TOP, continuing at BOTTOM';
- for (i = bottom; i >= start; i--) {
- var t = file[i];
- if (t == undefined) continue;
- var tail = t.length;
- while (tail > 0) {
- tail--;
- var xj = t.substr(tail, t.length-tail);
- var aa = re.exec(xj);
- if (!aa) continue;
- cursorx = tail+_rel(re,xj,aa).length;
- cursory = i;
- left = 0;
- base = 0;
- term_scrollto();
- return true;
- }
- }
- statustext = 'Pattern not found: ' + s.substr(1);
- }
- return false;
- }
- function term_rsearch(s, top, start, bottom) {
- var cx = s.substr(0, 1);
- cx = (cx == '/') ? '?' : "/";
- return term_search(cx+s.substr(1,s.length-1), top, start, bottom);
- }
- function _pop(q) {
- var a = new Array();
- var i;
- for (i = 0; i < q.length-1; i++) {
- a[i] = q[i];
- }
- return a;
- }
- function _addr(q) {
- if (q == '.') {
- return cursory+base;
- }
- if (q == '$') {
- return file.length-1;
- }
- if (q.substr(0, 1) == "'") {
- return marks[ q.substr(1, 1) ];
- }
- if (q == "\\/" || q == "\\&") {
- var a=cursory;
- var b=base;
- term_search(lastsearch, 0, cursory+base, file.length);
- var c=cursory+base;
- cursory=a;
- base=b;
- return c;
- }
- if (q == "\\?") {
- var a=cursory;
- var b=base;
- term_rsearch(lastsearch, 0, cursory+base, file.length);
- var c=cursory+base;
- cursory=a;
- base=b;
- return c;
- }
- if (q.substr(0, 1) == "/" || q.substr(0,1) == "?") {
- var a=cursory;
- var b=base;
- term_search(q, 0, cursory+base, file.length);
- var c=cursory+base;
- cursory=a;
- base=b;
- return c;
- }
- q=parseInt(q)-1;
- if (q >= file.length-1) q=file.length-1;
- if (q < 0) q=0;
- return q;
- }
- function term_command(s) {
- var top, start,bottom;
- if (s && s.length > 0 && s.substr(0,1) == ':') {
- s = s.substr(1, s.length-1);
- top = cursory+base;
- start = top;
- bottom = top;
- } else {
- top = 0;
- start = cursory+base;
- bottom = file.length;
- }
- // okay, this is kind of tricky
- var i;
- var tok = '';
- var tc = 0;
- var ng = new Array();
- var lastre = undefined;
- var nf = false;
- /// xxx todo implement ! with nf
- for (i = 0; i < s.length; i++) {
- var c = s.substr(i, 1);
- if ((tc==0 || tc==1) && "0123456789".indexOf(c) > -1) {
- tc = 1;
- tok = ''+tok+''+c;
- continue;
- } else if (tc == 1) {
- tc = 0;
- ng[ng.length] = parseInt(tok)-1;
- tok = '';
- }
- if (c == '%') {
- top = 0;
- bottom = file.length;
- start = cursory+base;
- } else if (c == '!') {
- nf = !nf;
- } else if (c == ',') {
- // do nothing
- while (ng.length > 2) {
- ng=_pop(ng);
- }
- } else if (c == ';') {
- start = ng[ng.length-1];
- ng = _pop(ng);
- cursory=start;
- base=0;
- ng = _pop(ng);
- } else if (c == '$') {
- ng[ng.length] = file.length;
- } else if (c == '.') {
- ng[ng.length] = cursory+base;
- } else if (c == "'") {
- // mark
- ng[ng.length] = marks[s.substr(i+1,1)];
- i++;
- } else if (c == "\\") {
- i++;
- c = s.substr(i, 1);
- var qq;
- if (c == '?') {
- qq=term_rsearch;
- } else {
- qq=term_search;
- }
- if (ng.length == 1) {
- top = ng[0];
- ng = _pop(ng);
- } else if (ng.length >= 2) {
- top = ng[ng.length-2];
- bottom = ng[ng.length-1];
- ng = _pop(_pop(ng));
- }
- if (!qq(lastsearch,top,start,bottom)) {
- return;
- }
- ng[ng.length] = cursory+base;
- } else if (c == "/" || c == "?") {
- var j = i;
- for (i++; i < s.length; i++) {
- var tc = s.substr(i, 1);
- if (tc == "\\") i++;
- else if (tc == c) break;
- }
- lastre = s.substr(j, i-j);
- if (ng.length == 1) {
- top = ng[0];
- ng = _pop(ng);
- } else if (ng.length >= 2) {
- top = ng[ng.length-2];
- bottom = ng[ng.length-1];
- ng = _pop(_pop(ng));
- }
- if (!term_search(lastre,top,start,bottom)) {
- return;
- }
- ng[ng.length] = cursory+base;
- } else if (c == " " || c == "\t") {
- continue;
- } else {
- break;
- }
- }
- if (tc == 1) {
- ng[ng.length] = parseInt(tok)-1;
- tok = '';
- i++;
- }
- if (ng.length == 1) {
- top = ng[0];
- bottom = ng[0];
- } else if (ng.length >= 2) {
- top = ng[ng.length-2];
- bottom = ng[ng.length-1];
- }
- if (lastre != undefined) {
- lastsearch = lastre;
- registers["/"] = lastsearch.substr(1, lastsearch.length-1);
- }
- var cmd2 = s.substr(i,2);
- var cmd = s.substr(i, 1);
- if (cmd2 == 'wq' || cmd == 'x') {
- editor_disable(true);
- } else if (cmd == '=') {
- statustext = '' + bottom;
- return;
- } else if (cmd2 == 'ha') {
- window.print();
- return;
- } else if (cmd == 'w') {
- var zx = term_freeze();
- if (term._formelement) term._formelement.value=zx;
- statustext = '"/tmp/mess4XbCXM" ' + file.length + 'L, '
- + zx.length + 'C written';
- } else if (!emacsen && s.substr(i,(s.length-i)) == 'emacs') {
- statustext = 'EMACS mode enabled. Press M-x vi to use vi mode';
- emacsen = true;
- mode = 1;
- } else if (emacsen && cmd2 == 'vi') {
- statustext = 'VI mode enabled. Press ESC :emacs to use EMACS mode';
- mode = 0;
- emacsen = false;
- } else if (cmd == 'e' && term._formelement) {
- var zx = term_freeze();
- if (cmd2 != 'e!') {
- if (cmd2 == 'e?') {
- if (!confirm("Your changes will be lost\nAre you sure?")) {
- return;
- }
- } else if (term._formelement.value != zx) {
- statustext = 'No write since last change (use ! to override)';
- return;
- }
- }
- term_thaw(term._formelement.value);
- } else if (cmd == 'f') {
- var zx = term_freeze();
- statustext = '"/tmp/mess4XbCXM"';
- if (term._formelement.value != zx) {
- statustext += ' [Modified]';
- }
- statustext += ' line ' + (cursory+base+1) + ' of '
- + file.length + ' col ' + (cursorx+left+1);
- } else if (cmd == 'h' || s.substr(i,5) == 'about') {
- statustext = "jsvi \xa9 2006 Internet Connection, Inc";
- } else if (s.substr(i,4) == 'kwak') {
- term.style.backgroundImage = 'url(ducky.jpg)';
- statustext = 'kwak kwak kwak...';
- } else if (s.substr(i,3) == 'moo') {
- statustext = 'This editor does not have Super Cow Powers';
- } else if (cmd == 'b') {
- // only one buffer
- } else if (cmd == 'n' || cmd == 'N') {
- statustext = 'There is only one file to edit';
- } else if (cmd == 'q') {
- var zx = term_freeze();
- if (cmd2 != 'q!') {
- if (term._formelement.value != zx) {
- if (cmd2 == 'q?') {
- if (confirm("Your changes will be lost\nAre you sure?")) {
- editor_disable(false);
- return;
- } else {
- return;
- }
- }
- statustext = 'No write since last change (use ! to override)';
- return;
- }
- }
- editor_disable(false);
- } else if (cmd == 'd') {
- // delete lines
- yank_buffer = term_delete(top);
- while (top < bottom) {
- yank_buffer += term_delete(top);
- bottom--;
- }
- if (lastreg != '_') {
- registers[lastreg] = yank_buffer;
- term_roll_yank();
- }
- term_scrollto();
- } else if (cmd == 'u') {
- var z = term_freeze();
- term_thaw(undoset);
- undoset = z;
- } else if (cmd == 'y') {
- yank_buffer = '';
- for (i = top; i < bottom; i++) {
- yank_buffer += (file[i])+"\n";
- }
- registers[lastreg] = yank_buffer;
- term_roll_yank();
- } else if (cmd == 'F') {
- // styling!
- var y;
- var tadd = 0;
- var tsub = 0;
- var tset = undefined;
- var otc = tagstyle;
- var tg = '=';
- for (y = i+1; y < s.length; y++) {
- var cy = s.substr(y, 1);
- if (cy == '=' || cy == '+' || cy == '-' || cy == '!') {
- tg = cy;
- } else {
- var fl = 0;
- if (cy == 'b') {
- fl = 1;
- } else if (cy == 'i') {
- fl = 16;
- } else if (cy == 'u') {
- fl = 2;
- } else if (cy == 'o') {
- fl = 4;
- } else {
- statustext = 'Unrecognized formatting specifier: ' + cy;
- return;
- }
- if (tg == '!') {
- if (tset != undefined) tagstyle = tset;
- tagstyle = tagstyle ^ fl;
- } else if (tg == '=') {
- if (tset == undefined) tset = 0;
- tset = tset + fl;
- } else if (tg == '+') {
- tadd |= fl;
- } else if (tg == '-') {
- tsub |= fl;
- }
- }
- }
- if (tset == undefined) {
- tagstyle = ((tagstyle | tadd) | tsub) ^ tsub;
- } else {
- tagstyle = tset;
- }
- // okay, we've set tagstyle
- if (i != 0) {
- // if this happened, then we were given a range(!)
- for (y = top; y <= bottom; y++) {
- if (file[y] == undefined) continue;
- tags[y] = _mxs(file[y].length, tagstyle);
- }
- tagstyle = otc;
- } else if (vselm) {
- term_select();
- term_vi_unset('d');
- term_vi_unset('y');
- term_vi_unset('c');
- term_vi_set('F');
- term_operate();
- tagstyle = otc;
- }
- } else if (cmd == 'g' || cmd == 'v') {
- // okay then
- var y;
- var ng = s.substr(i+1,(s.length-i)-1);
- if (cmd == 'v') ng = '!' + ng;
- for (y = 0; y < file.length; y++) {
- base = 0;
- cursory = y;
- term_command(ng);
- }
- } else if (cmd == 't' || cmd2 == 'co') {
- // copy- need address
- var t = s.substr(i, s.length-i).replace(/^[a-z]*[ ]*/);
- var x;
- yank_buffer = "";
- for (x = top; x <= bottom; x++) {
- if (file[x] != undefined) {
- yank_buffer += (file[x]) + "\n";
- }
- }
- var a = cursory;
- var b = base;
- base = 0;
- cursorx = 0;
- left = 0;
- cursory = _addr(t);
- if (cursory < 0) cursory = 0;
- term_paste(false, yank_buffer);
- cursory = a;
- base = b;
- } else if (cmd == 'm') {
- // move- need address
- i++;
- if (s.substr(i, 3) == 'ove') {
- i += 3;
- }
- var t = s.substr(i, s.length-i);
- yank_buffer = term_delete(top);
- while (top < bottom) {
- yank_buffer += term_delete(top);
- bottom--;
- }
- var a = cursory;
- var b = base;
- base = 0;
- cursorx = 0;
- left = 0;
- cursory = _addr(t);
- if (cursory < 0) cursory = 0;
- term_paste(false, yank_buffer);
- cursory = a;
- base = b;
- } else if (cmd == 's') {
- // substitute
- i++;
- // extract regex
- var q = s.substr(i,1);
- while (q == ' ') {
- i++;
- q = s.substr(i,1);
- }
- var sep = s.substr(i,1);
- i++;
- var lr, ls, lf;
- if (sep == '') {
- if (!lastsearch || !lastsubst) {
- statustext = 'No previous substitute regular expression';
- return;
- }
- lr = lastsearch.substr(1, lastsearch.length-1);
- ls = lastsubst;
- lf = lastflags;
- } else {
- var jj = i;
- var zj;
- for (; i < s.length; i++) {
- zj = s.substr(i,1);
- if (zj == "\\") {
- i++;
- } else if (zj == sep) {
- break;
- }
- }
- lastsearch = "/" + s.substr(jj, i-jj);
- registers["/"] = lastsearch.substr(1, lastsearch.length-1);
- i++; // sep
- jj=i;
- for (; i < s.length; i++) {
- zj = s.substr(i,1);
- if (zj == "\\") {
- i++;
- } else if (zj == sep) {
- break;
- }
- }
- lastsubst = s.substr(jj, i-jj);
- lastflags = '';
- i++;
- var count = -1;
- for (; i < s.length; i++) {
- zj = s.substr(i,1);
- if (zj == 'i' || zj == 'g') {
- lastflags += zj;
- } else {
- count = parseInt(s.substr(i,s.length-i));
- if (count > 0) {
- break;
- } else {
- statustext = "Trailing characters";
- return;
- }
- }
- }
- var re = new RegExp;
- re.compile(lastsearch.substr(1, lastsearch.length-1),
- (lastflags.indexOf('i') > -1) ? 'i' : '');
- var hit = false;
- for (i = top; i < bottom; i++) {
- var t = file[i];
- var g = tags[i];
- var aa;
- var st = t;
- aa = re.exec(t);
- if (!aa || aa.length == 0) continue;
- file[i] = '';
- tags[i] = '';
- // okay got a hit, do this vi style
- for (;;) {
- t = _rer(re,st,aa);
- var lt = _rel(re,st,aa);
- var lg = _resubst(lastsubst,aa);
- file[i] += lt + lg;
- tags[i] += g.substr(0, lt.length) + _zeros(lg.length);
- if (lastflags.indexOf('g') > -1) {
- st = t;
- aa = re.exec(t);
- if (aa && aa.length > 0) continue;
- }
- file[i] += t;
- tags[i] += g.substr(g.length - t.length, t.length);
- break;
- }
- hit = true;
- if (count > -1) {
- count--;
- if (count == 0) break;
- }
- }
- if (!hit) {
- statustext = 'Pattern not found: ' + lastsearch.substr(1, lastsearch.length-1);
- }
- }
- } else if (cmd == '' && ng.length > 0) {
- base = 0;
- cursory = top;
- } else {
- statustext = "Not an editor command: " + s.substr(i,s.length-i);
- }
- }
- function _resubst(s, aa) {
- var i;
- var out = '';
- for (i = 0; i < s.length; i++) {
- var zq = s.substr(i, 1);
- if (zq == "\\") {
- zq = s.substr(i, 2);
- if (zq == "\\1") {
- zq = aa[1];
- } else if (zq == "\\2") {
- zq = aa[2];
- } else if (zq == "\\3") {
- zq = aa[3];
- } else if (zq == "\\4") {
- zq = aa[4];
- } else if (zq == "\\5") {
- zq = aa[5];
- } else if (zq == "\\6") {
- zq = aa[6];
- } else if (zq == "\\7") {
- zq = aa[7];
- } else if (zq == "\\8") {
- zq = aa[8];
- } else if (zq == "\\9") {
- zq = aa[9];
- } else {
- zq = s.substr(i+1, 1);
- }
- i++;
- } else if (zq == "&") {
- zq = aa[0];
- }
- out += zq;
- }
- return out;
- }
- function _calcy(zx, x, g) {
- if (zx && cursor._lasty != cursory) {
- cursor._lasty = cursory;
- var nh = 0;
- while (zx && zx != document.body) {
- nh += zx.offsetTop;
- zx = zx.offsetParent;
- }
- cursor.style.top = nh + 'px';
- }
- var z = x.substr(cursorx, 1);
- var q = g.substr(cursorx, 1).charCodeAt(0);
- if (cursorx >= x.length || z == undefined || z == "\240" || z == '')
- z = ' ';
-
- if (cursor._lastch != z || cursor._lastgh != q) {
- if (z == ' ') {
- z = "\240";
- q = 0;
- }
- while (cursor.firstChild)
- cursor.removeChild(cursor.firstChild);
- cursor.appendChild(document.createTextNode(z));
- cursor._lastch = z;
- cursor._lastgh = q;
- if (q & 1) {
- cursor.style.fontWeight = 'bold';
- } else {
- cursor.style.fontWeight = 'normal';
- }
- if (q & 2) {
- cursor.style.textDecoration = 'underline';
- } else {
- cursor.style.textDecoration = 'none';
- }
- if (q & 16) {
- cursor.style.fontStyle = 'italic';
- } else {
- cursor.style.fontStyle = 'normal';
- }
- }
- }
- function term_calcy() {
- // fixup character inside... burrr
- var xx = file[cursory+base];
- var xg = tags[cursory+base];
- var zleft = left;
- if (cursory == (term_rows-1)) {
- xx = command + " ";
- zleft = commandleft;
- xg = _mxs(xx.length+1,0);
- }
- var gg = term.childNodes;
- if (gg.length > cursory) {
- gg = term.childNodes[cursory];
- } else {
- gg = undefined;
- }
- _calcy(gg, xx.substr(zleft, xx.length-zleft), xg.substr(zleft, xg.length-zleft));
- }
- function term_calcx() {
- if (cursorx != cursor._lastx) {
- cursor.style.left = (cursorx * (term_cur_width)) + 'px';
- cursor._lastx = cursorx;
- term_calcy();
- }
- }
- function term_scrollto() {
- var h = term_rows;
- var w = term_cols;
- var x = cursorx+left;
- var y = cursory+base;
- var t = file[y];
- if (command == '') h--;
- var ex = parseInt((w/8));
- if (ex < 4) ex = 4;
- var ey = parseInt((h/8));
- if (ey < 4) ey = 4;
- while (t == undefined && y > 0) {
- y--;
- t = file[y];
- }
- if (t == undefined) t = '';
- if (x >= t.length) {
- if (mode) x = t.length;
- else x = t.length-1;
- }
- if (x < 0) x = 0;
- if (y < 0) y = 0;
- if (x < left) {
- left = (x - ex);
- cursorx = ex;
- if (left < 0) {
- left = 0;
- cursorx = 0;
- }
- } else if (x >= left+w) {
- left = (x - w) + ex;
- cursorx = w - ex;
- } else {
- cursorx = x - left;
- }
- if (y < base) {
- base = (y - ey);
- cursory = ey;
- if (base < 0) {
- base = 0;
- cursory = 0;
- }
- } else if (y >= base+h) {
- base = ((y - h) + ey);
- cursory = (h - ey);
- } else {
- cursory = y - base;
- }
- term_calcx();
- term_calcy();
- }
- function term_insert(y, str) {
- var z = file.length;
- file[z] = '';
- tags[z] = '';
- var i;
- for (i = z; i > y; i--) {
- file[i] = (file[i-1]);
- tags[i] = (tags[i-1]);
- }
- file[y] = '';
- tags[y] = '';
- }
- function term_paste(after, ign) {
- if (ign != undefined) {
- yank_buffer = ign;
- } else {
- registers['.'] = lastinsert;
- registers[':'] = oldcommand;
- registers['%'] = "/tmp/mess4XbCXM";
- registers['#'] = "";
- yank_buffer = registers[lastreg];
- }
- if (yank_buffer == undefined) return false;
- if (yank_buffer.indexOf("\n") > -1) {
- // insert after this line
- if (after) cursory++;
- var nq = yank_buffer.split("\n");
- var nj;
- for (nj = 0; nj < nq.length; nj++) {
- if (nj == (nq.length-1)) {
- if (nq[nj] == undefined) break;
- if (nq[nj] == '') break;
- // handled specially
- var aa = _hra(nq[nj]);
- file[cursory+base+nj] = aa[0] + (file[cursory+base+nj]);
- tags[cursory+base+nj] = aa[1] + (tags[cursory+base+nj]);
- break;
- }
- term_insert(cursory+base+nj);
- var aa = _hra(nq[nj]);
- file[cursory+base+nj] = aa[0];
- tags[cursory+base+nj] = aa[1];
- }
- } else {
- var t = (file[cursory+base]);
- var g = (tags[cursory+base]);
- var aa = _hra(yank_buffer);
- if (after) cursorx++;
- file[cursory+base] = t.substr(0, cursorx+left)
- + aa[0] + t.substr(cursorx+left, t.length-(cursorx+left));
- tags[cursory+base] = g.substr(0, cursorx+left)
- + aa[1] + g.substr(cursorx+left, g.length-(cursorx+left));
- }
- return true;
- }
- function term_keyfix(e) {
- if (!e) e = window.event;
- var ch = e.keyCode;
- if (!ch) ch = e.which;
- if (e.DOM_VK_UP) {
- if (e.DOM_VK_UP == ch) ch = 57373;
- else if (e.DOM_VK_DOWN == ch) ch = 57374;
- else if (e.DOM_VK_LEFT == ch) ch = 57375;
- else if (e.DOM_VK_RIGHT == ch) ch = 57376;
- }
- if (ch == 8 || ch == 9 || ch == 37 || ch == 39
- || ch == 38 || ch == 40 || ch == 127
- || ch == 33 || ch == 34 || ch == 36
- || ch == 35 || ch == 45 || ch == 46
- || ch == 57373
- || ch == 57374
- || ch == 57375
- || ch == 57376) {
- if (e.preventDefault) e.preventDefault();
- if (e.stopPropagation) e.stopPropagation();
- term_keypress_inner(e, true);
- e.cancelBubble=true;
- return false;
- } else {
- e.cancelBubble=false;
- return true;
- }
- }
- function term_keypress(e) {
- if (!e) e = window.event;
- if (e.preventDefault) e.preventDefault();
- if (e.stopPropagation) e.stopPropagation();
- if (suggest._visible) {
- suggest.style.display = 'none';
- suggest._visible = false;
- }
- e.cancelBubble=true;
- term_keypress_inner(e, false);
- return false;
- }
- function term_keypress_inner(e, synth) {
- var k = e.which;
- var kc;
- if (e.charCode) {
- if (e.charCode == 27 || e.charCode == 13 || e.charCode == 10
- || e.charCode == 8
- || e.charCode == 9) {
- k = e.charCode;
- kc = String.fromCharCode(e.charCode);
- } else if (e.charCode == 63232) {
- k = 57373;
- } else if (e.charCode == 63233) {
- k = 57374;
- } else if (e.charCode == 63234) {
- k = 57375;
- } else if (e.charCode == 63235) {
- k = 57376;
- } else {
- if (e.charCode == 191) return; // wtf?
- kc = String.fromCharCode(e.charCode);
- k = 0;
- }
- } else if (e.keyCode) {
- k = e.keyCode;
- if (e.DOM_VK_UP) {
- if (e.DOM_VK_UP == k) k = 57373;
- else if (e.DOM_VK_DOWN == k) k = 57374;
- else if (e.DOM_VK_LEFT == k) k = 57375;
- else if (e.DOM_VK_RIGHT == k) k = 57376;
- } else {
- kc = String.fromCharCode(e.keyCode);
- }
- if (k == 191) return; // unicode i think
- } else {
- if (!k || k == 191) return;
- kc = String.fromCharCode(k);
- var i;
- }
- var mod;
- if (e.modifiers) mod = e.modifiers; else mod = 0;
- var ctrl = e.ctrlKey | (mod & 2);
- var shift = e.shiftKey | (mod & 4);
- var meta = e.altKey | e.metaKey | (mod & 1);
- var lk = lastkey;…
Large files files are truncated, but you can click here to view the full file