2015-06-01 03:18:23 +00:00
|
|
|
/* jslint node: true */
|
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var View = require('./view.js').View;
|
|
|
|
var miscUtil = require('./misc_util.js');
|
|
|
|
var strUtil = require('./string_util.js');
|
|
|
|
var ansi = require('./ansi_term.js');
|
2015-09-11 04:20:11 +00:00
|
|
|
var colorCodes = require('./color_codes.js');
|
2015-09-16 03:55:10 +00:00
|
|
|
var wordWrapText = require('./word_wrap.js').wordWrapText;
|
2015-06-01 03:18:23 +00:00
|
|
|
|
|
|
|
var assert = require('assert');
|
|
|
|
var _ = require('lodash');
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
// :TODO: Determine CTRL-* keys for various things
|
|
|
|
// See http://www.bbsdocumentary.com/library/PROGRAMS/GRAPHICS/ANSI/bansi.txt
|
|
|
|
// http://wiki.synchro.net/howto:editor:slyedit#edit_mode
|
|
|
|
// http://sublime-text-unofficial-documentation.readthedocs.org/en/latest/reference/keyboard_shortcuts_win.html
|
|
|
|
|
|
|
|
/* Mystic
|
|
|
|
[^B] Reformat Paragraph [^O] Show this help file
|
|
|
|
[^I] Insert tab space [^Q] Enter quote mode
|
|
|
|
[^K] Cut current line of text [^V] Toggle insert/overwrite
|
|
|
|
[^U] Paste previously cut text [^Y] Delete current line
|
|
|
|
|
|
|
|
|
|
|
|
BASIC MOVEMENT COMMANDS
|
|
|
|
|
|
|
|
UP/^E LEFT/^S PGUP/^R HOME/^F
|
|
|
|
DOWN/^X RIGHT/^D PGDN/^C END/^G
|
|
|
|
*/
|
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
|
|
|
// Some other interesting implementations, resources, etc.
|
|
|
|
//
|
|
|
|
// Editors - BBS
|
|
|
|
// * https://github.com/M-griffin/Enthral/blob/master/src/msg_fse.cpp
|
2015-07-13 19:41:21 +00:00
|
|
|
//
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
|
|
|
// Editors - Other
|
|
|
|
// * http://joe-editor.sourceforge.net/
|
|
|
|
// * http://www.jbox.dk/downloads/edit.c
|
2015-07-13 19:41:21 +00:00
|
|
|
// * https://github.com/dominictarr/hipster
|
|
|
|
//
|
|
|
|
// Implementations - Word Wrap
|
|
|
|
// * https://github.com/protomouse/synchronet/blob/93b01c55b3102ebc3c4f4793c3a45b8c13d0dc2a/src/sbbs3/wordwrap.c
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
2015-07-02 02:35:03 +00:00
|
|
|
// Misc notes
|
2015-07-13 19:41:21 +00:00
|
|
|
// * https://github.com/dominictarr/hipster/issues/15 (Deleting lines/etc.)
|
2015-07-02 02:35:03 +00:00
|
|
|
//
|
|
|
|
// Blessed
|
|
|
|
// insertLine: CSR(top, bottom) + CUP(y, 0) + IL(1) + CSR(0, height)
|
|
|
|
// deleteLine: CSR(top, bottom) + CUP(y, 0) + DL(1) + CSR(0, height)
|
|
|
|
// Quick Ansi -- update only what was changed:
|
|
|
|
// https://github.com/dominictarr/quickansi
|
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
|
|
|
// To-Do
|
|
|
|
//
|
|
|
|
// * Index pos % for emit scroll events
|
2015-06-17 22:49:32 +00:00
|
|
|
// * Some of this shoudl be async'd where there is lots of processing (e.g. word wrap)
|
2015-07-02 02:35:03 +00:00
|
|
|
// * Fix backspace when col=0 (e.g. bs to prev line)
|
2015-07-09 04:07:25 +00:00
|
|
|
// * Add back word delete
|
|
|
|
// *
|
2015-07-02 02:35:03 +00:00
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
|
2015-06-03 04:18:00 +00:00
|
|
|
var SPECIAL_KEY_MAP_DEFAULT = {
|
2015-06-20 06:40:23 +00:00
|
|
|
'line feed' : [ 'return' ],
|
|
|
|
exit : [ 'esc' ],
|
|
|
|
backspace : [ 'backspace' ],
|
|
|
|
'delete' : [ 'del' ],
|
|
|
|
tab : [ 'tab' ],
|
|
|
|
up : [ 'up arrow' ],
|
|
|
|
down : [ 'down arrow' ],
|
|
|
|
end : [ 'end' ],
|
|
|
|
home : [ 'home' ],
|
|
|
|
left : [ 'left arrow' ],
|
|
|
|
right : [ 'right arrow' ],
|
|
|
|
'delete line' : [ 'ctrl + y' ],
|
|
|
|
'page up' : [ 'page up' ],
|
|
|
|
'page down' : [ 'page down' ],
|
|
|
|
insert : [ 'insert', 'ctrl + v' ],
|
2015-06-04 04:00:33 +00:00
|
|
|
};
|
2015-06-03 04:18:00 +00:00
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
exports.MultiLineEditTextView = MultiLineEditTextView;
|
2015-06-01 03:18:23 +00:00
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
function MultiLineEditTextView(options) {
|
2015-06-01 03:18:23 +00:00
|
|
|
if(!_.isBoolean(options.acceptsFocus)) {
|
|
|
|
options.acceptsFocus = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!_.isBoolean(this.acceptsInput)) {
|
|
|
|
options.acceptsInput = true;
|
|
|
|
}
|
|
|
|
|
2015-06-03 04:18:00 +00:00
|
|
|
if(!_.isObject(options.specialKeyMap)) {
|
|
|
|
options.specialKeyMap = SPECIAL_KEY_MAP_DEFAULT;
|
|
|
|
}
|
|
|
|
|
2015-06-01 03:18:23 +00:00
|
|
|
View.call(this, options);
|
|
|
|
|
|
|
|
var self = this;
|
|
|
|
|
|
|
|
//
|
2015-06-01 23:10:27 +00:00
|
|
|
// ANSI seems to want tabs to default to 8 characters. See the following:
|
2015-06-01 03:18:23 +00:00
|
|
|
// * http://www.ansi-bbs.org/ansi-bbs2/control_chars/
|
|
|
|
// * http://www.bbsdocumentary.com/library/PROGRAMS/GRAPHICS/ANSI/bansi.txt
|
|
|
|
//
|
2015-06-07 01:34:55 +00:00
|
|
|
// This seems overkill though, so let's default to 4 :)
|
2015-07-11 22:39:42 +00:00
|
|
|
// :TODO: what shoudl this really be? Maybe 8 is OK
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
2015-07-11 22:39:42 +00:00
|
|
|
this.tabWidth = _.isNumber(options.tabWidth) ? options.tabWidth : 4;
|
2015-06-01 03:18:23 +00:00
|
|
|
|
2015-08-13 22:05:17 +00:00
|
|
|
this.textLines = [ ];
|
2015-06-02 05:00:54 +00:00
|
|
|
this.topVisibleIndex = 0;
|
2015-07-11 22:39:42 +00:00
|
|
|
this.mode = options.mode || 'edit'; // edit | preview | read-only
|
2015-06-03 04:18:00 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// cursorPos represents zero-based row, col positions
|
|
|
|
// within the editor itself
|
|
|
|
//
|
2015-06-02 22:36:55 +00:00
|
|
|
this.cursorPos = { col : 0, row : 0 };
|
2015-06-01 03:18:23 +00:00
|
|
|
|
2015-06-08 22:51:27 +00:00
|
|
|
this.getSGRFor = function(sgrFor) {
|
|
|
|
return {
|
|
|
|
text : self.getSGR(),
|
|
|
|
}[sgrFor] || self.getSGR();
|
|
|
|
};
|
|
|
|
|
2015-09-03 05:11:17 +00:00
|
|
|
this.isEditMode = function() {
|
|
|
|
return 'edit' === self.mode;
|
|
|
|
};
|
|
|
|
|
|
|
|
this.isPreviewMode = function() {
|
|
|
|
return 'preview' === self.mode;
|
|
|
|
};
|
|
|
|
|
2015-06-04 04:00:33 +00:00
|
|
|
// :TODO: Most of the calls to this could be avoided via incrementRow(), decrementRow() that keeps track or such
|
2015-06-03 04:18:00 +00:00
|
|
|
this.getTextLinesIndex = function(row) {
|
2015-06-03 23:09:03 +00:00
|
|
|
if(!_.isNumber(row)) {
|
|
|
|
row = self.cursorPos.row;
|
|
|
|
}
|
|
|
|
var index = self.topVisibleIndex + row;
|
2015-06-03 04:18:00 +00:00
|
|
|
return index;
|
|
|
|
};
|
|
|
|
|
2015-06-06 06:33:59 +00:00
|
|
|
this.getRemainingLinesBelowRow = function(row) {
|
|
|
|
if(!_.isNumber(row)) {
|
|
|
|
row = self.cursorPos.row;
|
|
|
|
}
|
|
|
|
return self.textLines.length - (self.topVisibleIndex + row) - 1;
|
|
|
|
};
|
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
this.getNextEndOfLineIndex = function(startIndex) {
|
2015-06-08 03:32:47 +00:00
|
|
|
for(var i = startIndex; i < self.textLines.length; i++) {
|
2015-06-07 01:34:55 +00:00
|
|
|
if(self.textLines[i].eol) {
|
2015-06-08 03:32:47 +00:00
|
|
|
return i;
|
2015-06-07 01:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-08 03:32:47 +00:00
|
|
|
return self.textLines.length;
|
2015-06-07 01:34:55 +00:00
|
|
|
};
|
|
|
|
|
2015-06-07 06:04:09 +00:00
|
|
|
this.redrawRows = function(startRow, endRow) {
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(self.getSGRFor('text') + ansi.hideCursor());
|
2015-06-07 06:04:09 +00:00
|
|
|
|
|
|
|
var startIndex = self.getTextLinesIndex(startRow);
|
|
|
|
var endIndex = Math.min(self.getTextLinesIndex(endRow), self.textLines.length);
|
|
|
|
var absPos = self.getAbsolutePosition(startRow, 0);
|
|
|
|
|
|
|
|
for(var i = startIndex; i < endIndex; ++i) {
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.write(
|
|
|
|
ansi.goto(absPos.row++, absPos.col) +
|
|
|
|
self.getRenderText(i), false);
|
2015-06-07 06:04:09 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.showCursor());
|
2015-06-19 04:17:51 +00:00
|
|
|
|
|
|
|
return absPos.row - self.position.row; // row we ended on
|
|
|
|
};
|
|
|
|
|
|
|
|
this.eraseRows = function(startRow, endRow) {
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(self.getSGRFor('text') + ansi.hideCursor());
|
2015-06-19 04:17:51 +00:00
|
|
|
|
|
|
|
var absPos = self.getAbsolutePosition(startRow, 0);
|
|
|
|
var absPosEnd = self.getAbsolutePosition(endRow, 0);
|
|
|
|
var eraseFiller = new Array(self.dimens.width).join(' ');
|
|
|
|
|
|
|
|
while(absPos.row < absPosEnd.row) {
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.write(
|
|
|
|
ansi.goto(absPos.row++, absPos.col) +
|
|
|
|
eraseFiller, false);
|
2015-06-19 04:17:51 +00:00
|
|
|
}
|
|
|
|
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.showCursor());
|
2015-06-07 06:04:09 +00:00
|
|
|
};
|
|
|
|
|
2015-06-01 03:18:23 +00:00
|
|
|
this.redrawVisibleArea = function() {
|
2015-06-07 01:34:55 +00:00
|
|
|
assert(self.topVisibleIndex <= self.textLines.length);
|
2015-06-19 04:17:51 +00:00
|
|
|
var lastRow = self.redrawRows(0, self.dimens.height);
|
|
|
|
|
|
|
|
self.eraseRows(lastRow, self.dimens.height);
|
|
|
|
/*
|
|
|
|
|
|
|
|
// :TOOD: create eraseRows(startRow, endRow)
|
|
|
|
if(lastRow < self.dimens.height) {
|
|
|
|
var absPos = self.getAbsolutePosition(lastRow, 0);
|
|
|
|
var empty = new Array(self.dimens.width).join(' ');
|
|
|
|
while(lastRow++ < self.dimens.height) {
|
|
|
|
self.client.term.write(ansi.goto(absPos.row++, absPos.col));
|
|
|
|
self.client.term.write(empty);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
2015-06-01 23:10:27 +00:00
|
|
|
};
|
2015-06-02 05:00:54 +00:00
|
|
|
|
2015-06-02 22:36:55 +00:00
|
|
|
this.getVisibleText = function(index) {
|
2015-06-03 23:09:03 +00:00
|
|
|
if(!_.isNumber(index)) {
|
|
|
|
index = self.getTextLinesIndex();
|
|
|
|
}
|
2015-06-02 22:36:55 +00:00
|
|
|
return self.textLines[index].text.replace(/\t/g, ' ');
|
|
|
|
};
|
|
|
|
|
2015-06-03 23:09:03 +00:00
|
|
|
this.getText = function(index) {
|
|
|
|
if(!_.isNumber(index)) {
|
|
|
|
index = self.getTextLinesIndex();
|
|
|
|
}
|
2015-06-19 04:17:51 +00:00
|
|
|
return self.textLines.length > index ? self.textLines[index].text : '';
|
2015-06-03 23:09:03 +00:00
|
|
|
};
|
|
|
|
|
2015-06-19 22:47:04 +00:00
|
|
|
this.getTextLength = function(index) {
|
|
|
|
if(!_.isNumber(index)) {
|
|
|
|
index = self.getTextLinesIndex();
|
|
|
|
}
|
|
|
|
return self.textLines.length > index ? self.textLines[index].text.length : 0;
|
|
|
|
};
|
|
|
|
|
2015-06-12 05:09:13 +00:00
|
|
|
this.getCharacter = function(index, col) {
|
|
|
|
if(!_.isNumber(col)) {
|
|
|
|
col = self.cursorPos.col;
|
|
|
|
}
|
|
|
|
return self.getText(index).charAt(col);
|
2015-06-12 22:44:32 +00:00
|
|
|
};
|
2015-06-12 05:09:13 +00:00
|
|
|
|
2015-06-20 06:40:23 +00:00
|
|
|
this.isTab = function(index, col) {
|
|
|
|
return '\t' === self.getCharacter(index, col);
|
|
|
|
};
|
|
|
|
|
2015-06-03 23:09:03 +00:00
|
|
|
this.getTextEndOfLineColumn = function(index) {
|
2015-06-19 22:47:04 +00:00
|
|
|
return Math.max(0, self.getTextLength(index));
|
2015-06-03 23:09:03 +00:00
|
|
|
};
|
|
|
|
|
2015-06-02 22:36:55 +00:00
|
|
|
this.getRenderText = function(index) {
|
|
|
|
var text = self.getVisibleText(index);
|
|
|
|
var remain = self.dimens.width - text.length;
|
|
|
|
if(remain > 0) {
|
2015-06-20 21:15:37 +00:00
|
|
|
text += new Array(remain + 1).join(' ');
|
2015-06-02 22:36:55 +00:00
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2015-06-15 03:19:25 +00:00
|
|
|
this.getTextLines = function(startIndex, endIndex) {
|
|
|
|
var lines;
|
2015-06-07 01:34:55 +00:00
|
|
|
if(startIndex === endIndex) {
|
|
|
|
lines = [ self.textLines[startIndex] ];
|
|
|
|
} else {
|
2015-06-08 05:45:06 +00:00
|
|
|
lines = self.textLines.slice(startIndex, endIndex + 1); // "slice extracts up to but not including end."
|
2015-06-07 01:34:55 +00:00
|
|
|
}
|
2015-06-15 03:19:25 +00:00
|
|
|
return lines;
|
2015-06-16 22:30:23 +00:00
|
|
|
};
|
2016-03-23 04:24:00 +00:00
|
|
|
|
|
|
|
this.getOutputText = function(startIndex, endIndex, eolMarker) {
|
|
|
|
let lines = self.getTextLines(startIndex, endIndex);
|
|
|
|
let text = '';
|
|
|
|
var re = new RegExp('\\t{1,' + (self.tabWidth) + '}', 'g');
|
|
|
|
|
|
|
|
lines.forEach(line => {
|
|
|
|
text += line.text.replace(re, '\t');
|
|
|
|
if(eolMarker && line.eol) {
|
|
|
|
text += eolMarker;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return text;
|
|
|
|
}
|
2015-06-07 01:34:55 +00:00
|
|
|
|
2015-06-15 03:19:25 +00:00
|
|
|
this.getContiguousText = function(startIndex, endIndex, includeEol) {
|
|
|
|
var lines = self.getTextLines(startIndex, endIndex);
|
|
|
|
var text = '';
|
|
|
|
for(var i = 0; i < lines.length; ++i) {
|
|
|
|
text += lines[i].text;
|
|
|
|
if(includeEol && lines[i].eol) {
|
2015-06-16 22:30:23 +00:00
|
|
|
text += '\n';
|
2015-06-15 03:19:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2015-06-04 04:00:33 +00:00
|
|
|
this.replaceCharacterInText = function(c, index, col) {
|
|
|
|
self.textLines[index].text = strUtil.replaceAt(
|
|
|
|
self.textLines[index].text, col, c);
|
|
|
|
};
|
|
|
|
|
2015-06-15 21:54:33 +00:00
|
|
|
/*
|
2015-06-15 03:19:25 +00:00
|
|
|
this.editTextAtPosition = function(editAction, text, index, col) {
|
|
|
|
switch(editAction) {
|
|
|
|
case 'insert' :
|
|
|
|
self.insertCharactersInText(text, index, col);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'deleteForward' :
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'deleteBack' :
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'replace' :
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
2015-06-15 21:54:33 +00:00
|
|
|
*/
|
|
|
|
|
2015-06-16 06:27:04 +00:00
|
|
|
this.updateTextWordWrap = function(index) {
|
|
|
|
var nextEolIndex = self.getNextEndOfLineIndex(index);
|
|
|
|
var wrapped = self.wordWrapSingleLine(self.getContiguousText(index, nextEolIndex), 'tabsIntact');
|
|
|
|
var newLines = wrapped.wrapped;
|
|
|
|
|
|
|
|
for(var i = 0; i < newLines.length; ++i) {
|
|
|
|
newLines[i] = { text : newLines[i] };
|
|
|
|
}
|
|
|
|
newLines[newLines.length - 1].eol = true;
|
|
|
|
|
|
|
|
Array.prototype.splice.apply(
|
|
|
|
self.textLines,
|
|
|
|
[ index, (nextEolIndex - index) + 1 ].concat(newLines));
|
|
|
|
|
|
|
|
return wrapped.firstWrapRange;
|
|
|
|
};
|
|
|
|
|
2015-06-18 04:38:21 +00:00
|
|
|
this.removeCharactersFromText = function(index, col, operation, count) {
|
|
|
|
if('right' === operation) {
|
|
|
|
self.textLines[index].text =
|
|
|
|
self.textLines[index].text.slice(col, count) +
|
|
|
|
self.textLines[index].text.slice(col + count);
|
2015-06-16 04:53:49 +00:00
|
|
|
|
2015-06-18 04:38:21 +00:00
|
|
|
self.cursorPos.col -= count;
|
|
|
|
|
|
|
|
self.updateTextWordWrap(index);
|
|
|
|
self.redrawRows(self.cursorPos.row, self.dimens.height);
|
2015-06-16 04:53:49 +00:00
|
|
|
|
2015-06-18 04:38:21 +00:00
|
|
|
if(0 === self.textLines[index].text) {
|
2015-06-16 04:53:49 +00:00
|
|
|
|
|
|
|
} else {
|
|
|
|
self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
}
|
2015-06-20 06:40:23 +00:00
|
|
|
} else if ('backspace' === operation) {
|
|
|
|
// :TODO: method for splicing text
|
2015-06-16 06:27:04 +00:00
|
|
|
self.textLines[index].text =
|
2015-06-17 22:49:32 +00:00
|
|
|
self.textLines[index].text.slice(0, col - (count - 1)) +
|
2015-06-16 06:27:04 +00:00
|
|
|
self.textLines[index].text.slice(col + 1);
|
2015-06-16 04:53:49 +00:00
|
|
|
|
2015-06-17 22:49:32 +00:00
|
|
|
self.cursorPos.col -= (count - 1);
|
2015-06-20 06:40:23 +00:00
|
|
|
|
2015-06-16 06:27:04 +00:00
|
|
|
self.updateTextWordWrap(index);
|
2015-06-16 04:53:49 +00:00
|
|
|
self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
|
2015-09-20 04:55:09 +00:00
|
|
|
self.moveClientCursorToCursorPos();
|
2015-06-20 06:40:23 +00:00
|
|
|
} else if('delete line' === operation) {
|
2015-06-18 04:38:21 +00:00
|
|
|
//
|
2015-06-19 22:47:04 +00:00
|
|
|
// Delete a visible line. Note that this is *not* the "physical" line, or
|
|
|
|
// 1:n entries up to eol! This is to keep consistency with home/end, and
|
|
|
|
// some other text editors such as nano. Sublime for example want to
|
|
|
|
// treat all of these things using the physical approach, but this seems
|
|
|
|
// a bit odd in this context.
|
2015-06-18 04:38:21 +00:00
|
|
|
//
|
2015-07-07 04:52:07 +00:00
|
|
|
var isLastLine = (index === self.textLines.length - 1);
|
|
|
|
var hadEol = self.textLines[index].eol;
|
|
|
|
|
2015-06-19 22:47:04 +00:00
|
|
|
self.textLines.splice(index, 1);
|
|
|
|
if(hadEol && self.textLines.length > index && !self.textLines[index].eol) {
|
|
|
|
self.textLines[index].eol = true;
|
2015-06-18 04:38:21 +00:00
|
|
|
}
|
|
|
|
|
2015-06-19 22:47:04 +00:00
|
|
|
//
|
|
|
|
// Create a empty edit buffer if necessary
|
|
|
|
// :TODO: Make this a method
|
|
|
|
if(self.textLines.length < 1) {
|
|
|
|
self.textLines = [ { text : '', eol : true } ];
|
2015-07-07 04:52:07 +00:00
|
|
|
isLastLine = false; // resetting
|
2015-06-19 22:47:04 +00:00
|
|
|
}
|
2015-06-18 04:38:21 +00:00
|
|
|
|
|
|
|
self.cursorPos.col = 0;
|
|
|
|
|
2015-06-19 22:47:04 +00:00
|
|
|
var lastRow = self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
self.eraseRows(lastRow, self.dimens.height);
|
2015-07-07 04:52:07 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// If we just deleted the last line in the buffer, move up
|
|
|
|
//
|
|
|
|
if(isLastLine) {
|
|
|
|
self.cursorEndOfPreviousLine();
|
|
|
|
} else {
|
2015-09-20 04:55:09 +00:00
|
|
|
self.moveClientCursorToCursorPos();
|
2015-07-07 04:52:07 +00:00
|
|
|
}
|
2015-06-16 04:53:49 +00:00
|
|
|
}
|
2015-06-15 21:54:33 +00:00
|
|
|
};
|
2015-06-15 03:19:25 +00:00
|
|
|
|
|
|
|
this.insertCharactersInText = function(c, index, col) {
|
2015-06-07 01:34:55 +00:00
|
|
|
self.textLines[index].text = [
|
|
|
|
self.textLines[index].text.slice(0, col),
|
|
|
|
c,
|
|
|
|
self.textLines[index].text.slice(col)
|
|
|
|
].join('');
|
2015-06-14 03:49:12 +00:00
|
|
|
|
2015-06-15 03:19:25 +00:00
|
|
|
//self.cursorPos.col++;
|
|
|
|
self.cursorPos.col += c.length;
|
2015-06-14 03:49:12 +00:00
|
|
|
|
|
|
|
var cursorOffset;
|
|
|
|
var absPos;
|
|
|
|
|
2015-06-20 21:15:37 +00:00
|
|
|
if(self.getTextLength(index) > self.dimens.width) {
|
2015-06-16 06:30:46 +00:00
|
|
|
//
|
|
|
|
// Update word wrapping and |cursorOffset| if the cursor
|
|
|
|
// was within the bounds of the wrapped text
|
|
|
|
//
|
2015-06-16 06:27:04 +00:00
|
|
|
var lastCol = self.cursorPos.col - c.length;
|
|
|
|
var firstWrapRange = self.updateTextWordWrap(index);
|
|
|
|
if(lastCol >= firstWrapRange.start && lastCol <= firstWrapRange.end) {
|
|
|
|
cursorOffset = self.cursorPos.col - firstWrapRange.start;
|
|
|
|
}
|
2015-06-14 03:49:12 +00:00
|
|
|
|
|
|
|
// redraw from current row to end of visible area
|
|
|
|
self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
|
|
|
|
if(!_.isUndefined(cursorOffset)) {
|
|
|
|
self.cursorBeginOfNextLine();
|
|
|
|
self.cursorPos.col += cursorOffset;
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.right(cursorOffset));
|
2015-06-14 03:49:12 +00:00
|
|
|
} else {
|
2015-09-20 04:55:09 +00:00
|
|
|
self.moveClientCursorToCursorPos();
|
2015-06-14 03:49:12 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//
|
|
|
|
// We must only redraw from col -> end of current visible line
|
|
|
|
//
|
|
|
|
absPos = self.getAbsolutePosition(self.cursorPos.row, self.cursorPos.col);
|
|
|
|
self.client.term.write(
|
|
|
|
ansi.hideCursor() +
|
|
|
|
self.getSGRFor('text') +
|
2015-06-15 03:19:25 +00:00
|
|
|
self.getRenderText(index).slice(self.cursorPos.col - c.length) +
|
2015-06-14 03:49:12 +00:00
|
|
|
ansi.goto(absPos.row, absPos.col) +
|
2015-07-06 01:05:55 +00:00
|
|
|
ansi.showCursor(), false
|
2015-06-14 03:49:12 +00:00
|
|
|
);
|
|
|
|
}
|
2015-06-07 01:34:55 +00:00
|
|
|
};
|
|
|
|
|
2015-06-06 06:33:59 +00:00
|
|
|
this.getRemainingTabWidth = function(col) {
|
|
|
|
if(!_.isNumber(col)) {
|
|
|
|
col = self.cursorPos.col;
|
|
|
|
}
|
|
|
|
return self.tabWidth - (col % self.tabWidth);
|
|
|
|
};
|
|
|
|
|
2015-06-12 22:44:32 +00:00
|
|
|
this.calculateTabStops = function() {
|
2015-06-14 03:49:12 +00:00
|
|
|
self.tabStops = [ 0 ];
|
2015-06-12 22:44:32 +00:00
|
|
|
var col = 0;
|
|
|
|
while(col < self.dimens.width) {
|
|
|
|
col += self.getRemainingTabWidth(col);
|
|
|
|
self.tabStops.push(col);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-14 03:49:12 +00:00
|
|
|
this.getNextTabStop = function(col) {
|
|
|
|
var i = self.tabStops.length;
|
|
|
|
while(self.tabStops[--i] > col);
|
|
|
|
return self.tabStops[++i];
|
|
|
|
};
|
|
|
|
|
|
|
|
this.getPrevTabStop = function(col) {
|
|
|
|
var i = self.tabStops.length;
|
|
|
|
while(self.tabStops[--i] >= col);
|
|
|
|
return self.tabStops[i];
|
|
|
|
};
|
|
|
|
|
2015-06-01 23:10:27 +00:00
|
|
|
this.expandTab = function(col, expandChar) {
|
|
|
|
expandChar = expandChar || ' ';
|
2015-06-06 06:33:59 +00:00
|
|
|
return new Array(self.getRemainingTabWidth(col)).join(expandChar);
|
2015-06-01 23:10:27 +00:00
|
|
|
};
|
|
|
|
|
2015-09-11 04:20:11 +00:00
|
|
|
this.getStringLength = function(s) {
|
|
|
|
return self.isPreviewMode() ? colorCodes.enigmaStrLen(s) : s.length;
|
|
|
|
};
|
|
|
|
|
2015-06-15 03:19:25 +00:00
|
|
|
this.wordWrapSingleLine = function(s, tabHandling, width) {
|
|
|
|
if(!_.isNumber(width)) {
|
|
|
|
width = self.dimens.width;
|
|
|
|
}
|
|
|
|
|
2015-09-16 03:55:10 +00:00
|
|
|
return wordWrapText(
|
|
|
|
s, {
|
|
|
|
width : width,
|
|
|
|
tabHandling : tabHandling || 'expand',
|
|
|
|
tabWidth : self.tabWidth,
|
2015-09-20 07:29:07 +00:00
|
|
|
tabChar : '\t',
|
2015-06-06 06:33:59 +00:00
|
|
|
});
|
2015-06-01 23:10:27 +00:00
|
|
|
};
|
|
|
|
|
2015-06-15 03:19:25 +00:00
|
|
|
// :TODO: rename to insertRawText()
|
|
|
|
this.insertRawText = function(text, index, col) {
|
2015-06-01 23:10:27 +00:00
|
|
|
//
|
|
|
|
// Perform the following on |text|:
|
|
|
|
// * Normalize various line feed formats -> \n
|
|
|
|
// * Remove some control characters (e.g. \b)
|
|
|
|
// * Word wrap lines such that they fit in the visible workspace.
|
|
|
|
// Each actual line will then take 1:n elements in textLines[].
|
|
|
|
// * Each tab will be appropriately expanded and take 1:n \t
|
|
|
|
// characters. This allows us to know when we're in tab space
|
|
|
|
// when doing cursor movement/etc.
|
|
|
|
//
|
|
|
|
//
|
|
|
|
// Try to handle any possible newline that can be fed to us.
|
|
|
|
// See http://stackoverflow.com/questions/5034781/js-regex-to-split-by-line
|
|
|
|
//
|
2015-06-03 04:18:00 +00:00
|
|
|
// :TODO: support index/col insertion point
|
2015-06-02 05:00:54 +00:00
|
|
|
|
2015-06-03 04:18:00 +00:00
|
|
|
if(_.isNumber(index)) {
|
2015-06-02 05:00:54 +00:00
|
|
|
if(_.isNumber(col)) {
|
|
|
|
//
|
2015-06-03 04:18:00 +00:00
|
|
|
// Modify text to have information from index
|
2015-06-02 05:00:54 +00:00
|
|
|
// before and and after column
|
|
|
|
//
|
|
|
|
// :TODO: Need to clean this string (e.g. collapse tabs)
|
|
|
|
text = self.textLines
|
|
|
|
|
2015-06-03 04:18:00 +00:00
|
|
|
// :TODO: Remove original line @ index
|
2015-06-02 05:00:54 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-06-03 04:18:00 +00:00
|
|
|
index = self.textLines.length;
|
2015-06-02 05:00:54 +00:00
|
|
|
}
|
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
text = text
|
2015-06-01 23:10:27 +00:00
|
|
|
.replace(/\b/g, '')
|
|
|
|
.split(/\r\n|[\n\v\f\r\x85\u2028\u2029]/g);
|
|
|
|
|
|
|
|
var wrapped;
|
2015-06-02 05:00:54 +00:00
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
for(var i = 0; i < text.length; ++i) {
|
2015-09-20 07:29:07 +00:00
|
|
|
wrapped = self.wordWrapSingleLine(
|
|
|
|
text[i], // input
|
|
|
|
'expand', // tabHandling
|
|
|
|
self.dimens.width).wrapped;
|
2015-06-02 05:00:54 +00:00
|
|
|
|
|
|
|
for(var j = 0; j < wrapped.length - 1; ++j) {
|
2015-06-03 04:18:00 +00:00
|
|
|
self.textLines.splice(index++, 0, { text : wrapped[j] } );
|
2015-06-02 05:00:54 +00:00
|
|
|
}
|
2015-06-03 04:18:00 +00:00
|
|
|
self.textLines.splice(index++, 0, { text : wrapped[wrapped.length - 1], eol : true });
|
2015-06-01 23:10:27 +00:00
|
|
|
}
|
|
|
|
};
|
2015-06-02 05:00:54 +00:00
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
this.getAbsolutePosition = function(row, col) {
|
2015-06-07 06:04:09 +00:00
|
|
|
return {
|
|
|
|
row : self.position.row + row,
|
|
|
|
col : self.position.col + col,
|
|
|
|
};
|
2015-06-04 23:06:37 +00:00
|
|
|
};
|
|
|
|
|
2015-09-20 04:55:09 +00:00
|
|
|
this.moveClientCursorToCursorPos = function() {
|
2015-06-04 23:06:37 +00:00
|
|
|
var absPos = self.getAbsolutePosition(self.cursorPos.row, self.cursorPos.col);
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.goto(absPos.row, absPos.col));
|
2015-06-04 23:06:37 +00:00
|
|
|
};
|
|
|
|
|
2015-06-14 03:49:12 +00:00
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
this.keyPressCharacter = function(c) {
|
2015-07-07 05:26:16 +00:00
|
|
|
var index = self.getTextLinesIndex();
|
2015-06-04 04:00:33 +00:00
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
//
|
|
|
|
// :TODO: stuff that needs to happen
|
|
|
|
// * Break up into smaller methods
|
|
|
|
// * Even in overtype mode, word wrapping must apply if past bounds
|
|
|
|
// * A lot of this can be used for backspacing also
|
|
|
|
// * See how Sublime treats tabs in *non* overtype mode... just overwrite them?
|
|
|
|
//
|
2015-06-12 03:05:52 +00:00
|
|
|
//
|
2015-06-04 04:00:33 +00:00
|
|
|
|
|
|
|
if(self.overtypeMode) {
|
|
|
|
// :TODO: special handing for insert over eol mark?
|
2015-06-07 01:34:55 +00:00
|
|
|
self.replaceCharacterInText(c, index, self.cursorPos.col);
|
2015-06-04 04:00:33 +00:00
|
|
|
self.cursorPos.col++;
|
|
|
|
self.client.term.write(c);
|
|
|
|
} else {
|
2015-06-15 03:19:25 +00:00
|
|
|
self.insertCharactersInText(c, index, self.cursorPos.col);
|
2015-06-04 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-04 04:00:33 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
this.keyPressUp = function() {
|
2015-06-03 04:18:00 +00:00
|
|
|
if(self.cursorPos.row > 0) {
|
|
|
|
self.cursorPos.row--;
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.up());
|
2015-06-03 04:18:00 +00:00
|
|
|
|
2015-06-14 03:49:12 +00:00
|
|
|
if(!self.adjustCursorToNextTab('up')) {
|
|
|
|
self.adjustCursorIfPastEndOfLine(false);
|
|
|
|
}
|
2015-06-04 04:00:33 +00:00
|
|
|
} else {
|
|
|
|
self.scrollDocumentDown();
|
|
|
|
self.adjustCursorIfPastEndOfLine(true);
|
2015-06-03 04:18:00 +00:00
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-02 05:00:54 +00:00
|
|
|
};
|
2015-06-02 22:36:55 +00:00
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
this.keyPressDown = function() {
|
2015-06-22 04:36:07 +00:00
|
|
|
var lastVisibleRow = Math.min(
|
|
|
|
self.dimens.height,
|
|
|
|
(self.textLines.length - self.topVisibleIndex)) - 1;
|
|
|
|
|
2015-06-03 23:09:03 +00:00
|
|
|
if(self.cursorPos.row < lastVisibleRow) {
|
|
|
|
self.cursorPos.row++;
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.down());
|
2015-06-03 23:09:03 +00:00
|
|
|
|
2015-06-14 03:49:12 +00:00
|
|
|
if(!self.adjustCursorToNextTab('down')) {
|
|
|
|
self.adjustCursorIfPastEndOfLine(false);
|
|
|
|
}
|
2015-06-03 23:09:03 +00:00
|
|
|
} else {
|
2015-06-04 04:00:33 +00:00
|
|
|
self.scrollDocumentUp();
|
|
|
|
self.adjustCursorIfPastEndOfLine(true);
|
2015-06-03 23:09:03 +00:00
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-02 22:36:55 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
this.keyPressLeft = function() {
|
2015-06-03 04:18:00 +00:00
|
|
|
if(self.cursorPos.col > 0) {
|
2015-06-20 21:15:37 +00:00
|
|
|
var prevCharIsTab = self.isTab();
|
2015-06-12 05:09:13 +00:00
|
|
|
|
2015-06-03 04:18:00 +00:00
|
|
|
self.cursorPos.col--;
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.left());
|
2015-06-12 05:09:13 +00:00
|
|
|
|
2015-06-20 21:15:37 +00:00
|
|
|
if(prevCharIsTab) {
|
2015-06-12 05:09:13 +00:00
|
|
|
self.adjustCursorToNextTab('left');
|
|
|
|
}
|
2015-06-03 04:18:00 +00:00
|
|
|
} else {
|
2015-06-06 06:33:59 +00:00
|
|
|
self.cursorEndOfPreviousLine();
|
2015-06-03 04:18:00 +00:00
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-02 22:36:55 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
this.keyPressRight = function() {
|
2015-06-03 23:09:03 +00:00
|
|
|
var eolColumn = self.getTextEndOfLineColumn();
|
|
|
|
if(self.cursorPos.col < eolColumn) {
|
2015-06-20 21:15:37 +00:00
|
|
|
var prevCharIsTab = self.isTab();
|
2015-06-12 05:09:13 +00:00
|
|
|
|
2015-06-02 22:36:55 +00:00
|
|
|
self.cursorPos.col++;
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.right());
|
2015-06-03 04:18:00 +00:00
|
|
|
|
2015-06-20 21:15:37 +00:00
|
|
|
if(prevCharIsTab) {
|
2015-06-12 05:09:13 +00:00
|
|
|
self.adjustCursorToNextTab('right');
|
|
|
|
}
|
2015-06-02 22:36:55 +00:00
|
|
|
} else {
|
2015-06-06 06:33:59 +00:00
|
|
|
self.cursorBeginOfNextLine();
|
2015-06-02 22:36:55 +00:00
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-02 22:36:55 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
this.keyPressHome = function() {
|
2015-06-03 04:18:00 +00:00
|
|
|
var firstNonWhitespace = self.getVisibleText().search(/\S/);
|
|
|
|
if(-1 !== firstNonWhitespace) {
|
|
|
|
self.cursorPos.col = firstNonWhitespace;
|
|
|
|
} else {
|
|
|
|
self.cursorPos.col = 0;
|
|
|
|
}
|
2015-09-20 04:55:09 +00:00
|
|
|
self.moveClientCursorToCursorPos();
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-03 04:18:00 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
this.keyPressEnd = function() {
|
2015-06-03 23:09:03 +00:00
|
|
|
self.cursorPos.col = self.getTextEndOfLineColumn();
|
2015-09-20 04:55:09 +00:00
|
|
|
self.moveClientCursorToCursorPos();
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-03 04:18:00 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
this.keyPressPageUp = function() {
|
2015-06-20 21:15:37 +00:00
|
|
|
if(self.topVisibleIndex > 0) {
|
|
|
|
self.topVisibleIndex = Math.max(0, self.topVisibleIndex - self.dimens.height);
|
|
|
|
self.redraw();
|
|
|
|
self.adjustCursorIfPastEndOfLine(true);
|
|
|
|
} else {
|
|
|
|
self.cursorPos.row = 0;
|
2015-09-20 04:55:09 +00:00
|
|
|
self.moveClientCursorToCursorPos(); // :TODO: ajust if eol, etc.
|
2015-06-20 21:15:37 +00:00
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-04 04:00:33 +00:00
|
|
|
};
|
|
|
|
|
2015-06-04 23:06:37 +00:00
|
|
|
this.keyPressPageDown = function() {
|
2015-06-20 21:15:37 +00:00
|
|
|
var linesBelow = self.getRemainingLinesBelowRow();
|
|
|
|
if(linesBelow > 0) {
|
|
|
|
self.topVisibleIndex += Math.min(linesBelow, self.dimens.height);
|
|
|
|
self.redraw();
|
|
|
|
self.adjustCursorIfPastEndOfLine(true);
|
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-04 23:06:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.keyPressLineFeed = function() {
|
2015-06-14 03:49:12 +00:00
|
|
|
//
|
|
|
|
// Break up text from cursor position, redraw, and update cursor
|
|
|
|
// position to start of next line
|
|
|
|
//
|
|
|
|
var index = self.getTextLinesIndex();
|
|
|
|
var nextEolIndex = self.getNextEndOfLineIndex(index);
|
2015-06-15 05:18:21 +00:00
|
|
|
var text = self.getContiguousText(index, nextEolIndex);
|
|
|
|
var newLines = self.wordWrapSingleLine(text.slice(self.cursorPos.col), 'tabsIntact').wrapped;
|
2015-06-14 03:49:12 +00:00
|
|
|
|
|
|
|
newLines.unshift( { text : text.slice(0, self.cursorPos.col), eol : true } );
|
|
|
|
for(var i = 1; i < newLines.length; ++i) {
|
|
|
|
newLines[i] = { text : newLines[i] };
|
|
|
|
}
|
|
|
|
newLines[newLines.length - 1].eol = true;
|
|
|
|
|
|
|
|
Array.prototype.splice.apply(
|
|
|
|
self.textLines,
|
|
|
|
[ index, (nextEolIndex - index) + 1 ].concat(newLines));
|
2015-06-04 04:00:33 +00:00
|
|
|
|
2015-06-14 03:49:12 +00:00
|
|
|
// redraw from current row to end of visible area
|
|
|
|
self.redrawRows(self.cursorPos.row, self.dimens.height);
|
|
|
|
self.cursorBeginOfNextLine();
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-04 04:00:33 +00:00
|
|
|
};
|
|
|
|
|
2015-06-07 01:34:55 +00:00
|
|
|
this.keyPressInsert = function() {
|
2015-07-06 05:16:22 +00:00
|
|
|
self.toggleTextEditMode();
|
2015-06-07 01:34:55 +00:00
|
|
|
};
|
|
|
|
|
2015-06-14 03:49:12 +00:00
|
|
|
this.keyPressTab = function() {
|
|
|
|
var index = self.getTextLinesIndex();
|
2015-06-15 03:19:25 +00:00
|
|
|
self.insertCharactersInText(self.expandTab(self.cursorPos.col, '\t') + '\t', index, self.cursorPos.col);
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-14 03:49:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this.keyPressBackspace = function() {
|
2015-06-20 06:40:23 +00:00
|
|
|
if(self.cursorPos.col >= 1) {
|
2015-06-17 05:43:22 +00:00
|
|
|
//
|
|
|
|
// Don't want to delete character at cursor, but rather the character
|
|
|
|
// to the left of the cursor!
|
|
|
|
//
|
|
|
|
self.cursorPos.col -= 1;
|
2015-06-16 22:30:23 +00:00
|
|
|
|
2015-06-17 05:43:22 +00:00
|
|
|
var index = self.getTextLinesIndex();
|
|
|
|
var count;
|
|
|
|
|
2015-06-20 06:40:23 +00:00
|
|
|
if(self.isTab()) {
|
2015-06-17 22:49:32 +00:00
|
|
|
var col = self.cursorPos.col;
|
|
|
|
var prevTabStop = self.getPrevTabStop(self.cursorPos.col);
|
|
|
|
while(col >= prevTabStop) {
|
2015-06-20 21:15:37 +00:00
|
|
|
if(!self.isTab(index, col)) {
|
2015-06-17 22:49:32 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
--col;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = (self.cursorPos.col - col);
|
2015-06-17 05:43:22 +00:00
|
|
|
} else {
|
|
|
|
count = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
self.removeCharactersFromText(
|
|
|
|
index,
|
|
|
|
self.cursorPos.col,
|
2015-06-20 06:40:23 +00:00
|
|
|
'backspace',
|
2015-06-17 05:43:22 +00:00
|
|
|
count);
|
2015-06-20 06:40:23 +00:00
|
|
|
} else {
|
2015-06-22 04:36:07 +00:00
|
|
|
//
|
|
|
|
// Delete character at end of line previous.
|
|
|
|
// * This may be a eol marker
|
|
|
|
// * Word wrapping will need re-applied
|
|
|
|
//
|
2015-06-20 21:15:37 +00:00
|
|
|
// :TODO: apply word wrapping such that text can be re-adjusted if it can now fit on prev
|
2015-06-20 06:40:23 +00:00
|
|
|
self.keyPressLeft(); // same as hitting left - jump to previous line
|
2015-07-02 02:35:03 +00:00
|
|
|
//self.keyPressBackspace();
|
2015-06-17 05:43:22 +00:00
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-14 03:49:12 +00:00
|
|
|
};
|
|
|
|
|
2015-06-20 06:40:23 +00:00
|
|
|
this.keyPressDelete = function() {
|
2015-06-16 04:53:49 +00:00
|
|
|
self.removeCharactersFromText(
|
|
|
|
self.getTextLinesIndex(),
|
|
|
|
self.cursorPos.col,
|
|
|
|
'right',
|
|
|
|
1);
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-14 03:49:12 +00:00
|
|
|
};
|
|
|
|
|
2015-06-20 06:40:23 +00:00
|
|
|
//this.keyPressClearLine = function() {
|
|
|
|
this.keyPressDeleteLine = function() {
|
2015-06-19 22:47:04 +00:00
|
|
|
if(self.textLines.length > 0) {
|
|
|
|
self.removeCharactersFromText(
|
|
|
|
self.getTextLinesIndex(),
|
|
|
|
0,
|
2015-06-20 06:40:23 +00:00
|
|
|
'delete line');
|
2015-06-19 22:47:04 +00:00
|
|
|
}
|
2015-07-06 01:05:55 +00:00
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
self.emitEditPosition();
|
2015-06-18 04:38:21 +00:00
|
|
|
};
|
|
|
|
|
2015-06-06 06:33:59 +00:00
|
|
|
this.adjustCursorIfPastEndOfLine = function(forceUpdate) {
|
2015-06-03 23:09:03 +00:00
|
|
|
var eolColumn = self.getTextEndOfLineColumn();
|
|
|
|
if(self.cursorPos.col > eolColumn) {
|
|
|
|
self.cursorPos.col = eolColumn;
|
2015-06-06 06:33:59 +00:00
|
|
|
forceUpdate = true;
|
2015-06-04 04:00:33 +00:00
|
|
|
}
|
|
|
|
|
2015-06-06 06:33:59 +00:00
|
|
|
if(forceUpdate) {
|
2015-09-20 04:55:09 +00:00
|
|
|
self.moveClientCursorToCursorPos();
|
2015-06-03 23:09:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-06 06:33:59 +00:00
|
|
|
this.adjustCursorToNextTab = function(direction) {
|
2015-06-20 21:15:37 +00:00
|
|
|
if(self.isTab()) {
|
2015-06-12 22:44:32 +00:00
|
|
|
var move;
|
2015-06-12 05:09:13 +00:00
|
|
|
switch(direction) {
|
2015-06-16 04:53:49 +00:00
|
|
|
//
|
|
|
|
// Next tabstop to the right
|
|
|
|
//
|
2015-06-12 05:09:13 +00:00
|
|
|
case 'right' :
|
2015-06-14 03:49:12 +00:00
|
|
|
move = self.getNextTabStop(self.cursorPos.col) - self.cursorPos.col;
|
2015-06-12 05:09:13 +00:00
|
|
|
self.cursorPos.col += move;
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.right(move));
|
2015-06-12 05:09:13 +00:00
|
|
|
break;
|
|
|
|
|
2015-06-16 04:53:49 +00:00
|
|
|
//
|
|
|
|
// Next tabstop to the left
|
|
|
|
//
|
2015-06-12 05:09:13 +00:00
|
|
|
case 'left' :
|
2015-06-14 03:49:12 +00:00
|
|
|
move = self.cursorPos.col - self.getPrevTabStop(self.cursorPos.col);
|
2015-06-12 05:09:13 +00:00
|
|
|
self.cursorPos.col -= move;
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.left(move));
|
2015-06-12 05:09:13 +00:00
|
|
|
break;
|
2015-06-14 03:49:12 +00:00
|
|
|
|
|
|
|
case 'up' :
|
|
|
|
case 'down' :
|
|
|
|
//
|
2015-06-16 04:53:49 +00:00
|
|
|
// Jump to the tabstop nearest the cursor
|
2015-06-14 03:49:12 +00:00
|
|
|
//
|
|
|
|
var newCol = self.tabStops.reduce(function r(prev, curr) {
|
|
|
|
return (Math.abs(curr - self.cursorPos.col) < Math.abs(prev - self.cursorPos.col) ? curr : prev);
|
|
|
|
});
|
|
|
|
|
|
|
|
if(newCol > self.cursorPos.col) {
|
|
|
|
move = newCol - self.cursorPos.col;
|
|
|
|
self.cursorPos.col += move;
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.right(move));
|
2015-06-14 03:49:12 +00:00
|
|
|
} else if(newCol < self.cursorPos.col) {
|
|
|
|
move = self.cursorPos.col - newCol;
|
|
|
|
self.cursorPos.col -= move;
|
2015-07-06 01:05:55 +00:00
|
|
|
self.client.term.rawWrite(ansi.left(move));
|
2015-06-14 03:49:12 +00:00
|
|
|
}
|
|
|
|
break;
|
2015-06-06 06:33:59 +00:00
|
|
|
}
|
2015-06-14 03:49:12 +00:00
|
|
|
|
|
|
|
return true;
|
2015-06-06 06:33:59 +00:00
|
|
|
}
|
2015-06-14 03:49:12 +00:00
|
|
|
return false; // did not fall on a tab
|
2015-06-06 06:33:59 +00:00
|
|
|
};
|
|
|
|
|
2015-06-03 23:09:03 +00:00
|
|
|
this.cursorStartOfDocument = function() {
|
2015-06-02 22:36:55 +00:00
|
|
|
self.topVisibleIndex = 0;
|
|
|
|
self.cursorPos = { row : 0, col : 0 };
|
|
|
|
|
|
|
|
self.redraw();
|
2015-09-20 04:55:09 +00:00
|
|
|
self.moveClientCursorToCursorPos();
|
2015-06-02 22:36:55 +00:00
|
|
|
};
|
|
|
|
|
2015-06-03 23:09:03 +00:00
|
|
|
this.cursorEndOfDocument = function() {
|
2015-06-02 22:36:55 +00:00
|
|
|
self.topVisibleIndex = Math.max(self.textLines.length - self.dimens.height, 0);
|
2015-06-03 04:18:00 +00:00
|
|
|
self.cursorPos.row = (self.textLines.length - self.topVisibleIndex) - 1;
|
2015-06-03 23:09:03 +00:00
|
|
|
self.cursorPos.col = self.getTextEndOfLineColumn();
|
2015-06-02 22:36:55 +00:00
|
|
|
|
|
|
|
self.redraw();
|
2015-09-20 04:55:09 +00:00
|
|
|
self.moveClientCursorToCursorPos();
|
2015-06-02 22:36:55 +00:00
|
|
|
};
|
2015-06-03 04:18:00 +00:00
|
|
|
|
2015-06-06 06:33:59 +00:00
|
|
|
this.cursorBeginOfNextLine = function() {
|
|
|
|
// e.g. when scrolling right past eol
|
|
|
|
var linesBelow = self.getRemainingLinesBelowRow();
|
|
|
|
|
|
|
|
if(linesBelow > 0) {
|
|
|
|
var lastVisibleRow = Math.min(self.dimens.height, self.textLines.length) - 1;
|
|
|
|
if(self.cursorPos.row < lastVisibleRow) {
|
|
|
|
self.cursorPos.row++;
|
|
|
|
} else {
|
|
|
|
self.scrollDocumentUp();
|
|
|
|
}
|
|
|
|
self.keyPressHome(); // same as pressing 'home'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.cursorEndOfPreviousLine = function() {
|
2015-06-07 01:34:55 +00:00
|
|
|
// e.g. when scrolling left past start of line
|
|
|
|
var moveToEnd;
|
|
|
|
if(self.cursorPos.row > 0) {
|
|
|
|
self.cursorPos.row--;
|
|
|
|
moveToEnd = true;
|
|
|
|
} else if(self.topVisibleIndex > 0) {
|
|
|
|
self.scrollDocumentDown();
|
|
|
|
moveToEnd = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(moveToEnd) {
|
2015-06-06 06:33:59 +00:00
|
|
|
self.keyPressEnd(); // same as pressing 'end'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-06-14 03:49:12 +00:00
|
|
|
/*
|
|
|
|
this.cusorEndOfNextLine = function() {
|
|
|
|
var linesBelow = self.getRemainingLinesBelowRow();
|
|
|
|
|
|
|
|
if(linesBelow > 0) {
|
|
|
|
var lastVisibleRow = Math.min(self.dimens.height, self.textLines.length) - 1;
|
|
|
|
if(self.cursorPos.row < lastVisibleRow) {
|
|
|
|
self.cursorPos.row++;
|
|
|
|
} else {
|
|
|
|
self.scrollDocumentUp();
|
|
|
|
}
|
|
|
|
self.keyPressEnd(); // same as pressing 'end'
|
|
|
|
}
|
|
|
|
};
|
|
|
|
*/
|
|
|
|
|
2015-06-03 23:09:03 +00:00
|
|
|
this.scrollDocumentUp = function() {
|
|
|
|
//
|
|
|
|
// Note: We scroll *up* when the cursor goes *down* beyond
|
|
|
|
// the visible area!
|
|
|
|
//
|
2015-06-06 06:33:59 +00:00
|
|
|
var linesBelow = self.getRemainingLinesBelowRow();
|
2015-06-03 23:09:03 +00:00
|
|
|
if(linesBelow > 0) {
|
2015-06-04 04:00:33 +00:00
|
|
|
self.topVisibleIndex++;
|
|
|
|
self.redraw();
|
2015-06-03 23:09:03 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.scrollDocumentDown = function() {
|
|
|
|
//
|
|
|
|
// Note: We scroll *down* when the cursor goes *up* beyond
|
|
|
|
// the visible area!
|
2015-06-04 04:00:33 +00:00
|
|
|
//
|
|
|
|
if(self.topVisibleIndex > 0) {
|
|
|
|
self.topVisibleIndex--;
|
|
|
|
self.redraw();
|
|
|
|
}
|
2015-06-03 23:09:03 +00:00
|
|
|
};
|
|
|
|
|
2015-07-07 05:26:16 +00:00
|
|
|
this.emitEditPosition = function() {
|
|
|
|
self.emit('edit position', self.getEditPosition());
|
2015-07-06 01:05:55 +00:00
|
|
|
};
|
|
|
|
|
2015-07-06 05:16:22 +00:00
|
|
|
this.toggleTextEditMode = function() {
|
|
|
|
self.overtypeMode = !self.overtypeMode;
|
|
|
|
self.emit('text edit mode', self.getTextEditMode());
|
|
|
|
};
|
2015-08-13 22:05:17 +00:00
|
|
|
|
|
|
|
this.insertRawText(''); // init to blank/empty
|
2015-06-01 03:18:23 +00:00
|
|
|
}
|
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
require('util').inherits(MultiLineEditTextView, View);
|
2015-06-01 03:18:23 +00:00
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.prototype.setWidth = function(width) {
|
|
|
|
MultiLineEditTextView.super_.prototype.setWidth.call(this, width);
|
2015-06-15 03:19:25 +00:00
|
|
|
|
|
|
|
this.calculateTabStops();
|
|
|
|
};
|
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.prototype.redraw = function() {
|
|
|
|
MultiLineEditTextView.super_.prototype.redraw.call(this);
|
2015-06-01 03:18:23 +00:00
|
|
|
|
|
|
|
this.redrawVisibleArea();
|
|
|
|
};
|
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.prototype.setFocus = function(focused) {
|
2015-07-06 01:05:55 +00:00
|
|
|
this.client.term.rawWrite(this.getSGRFor('text'));
|
2015-09-20 04:55:09 +00:00
|
|
|
this.moveClientCursorToCursorPos();
|
2015-06-08 22:51:27 +00:00
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.super_.prototype.setFocus.call(this, focused);
|
2015-06-08 22:51:27 +00:00
|
|
|
};
|
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.prototype.setText = function(text) {
|
2015-07-22 05:52:20 +00:00
|
|
|
//text = require('fs').readFileSync('/home/nuskooler/Downloads/test_text.txt', { encoding : 'utf-8'});
|
2015-06-07 01:34:55 +00:00
|
|
|
|
2015-09-16 03:55:10 +00:00
|
|
|
this.textLines = [ ];
|
2015-09-16 04:44:31 +00:00
|
|
|
this.addText(text);
|
|
|
|
/*this.insertRawText(text);
|
|
|
|
|
|
|
|
if(this.isEditMode()) {
|
|
|
|
this.cursorEndOfDocument();
|
|
|
|
} else if(this.isPreviewMode()) {
|
|
|
|
this.cursorStartOfDocument();
|
|
|
|
}*/
|
|
|
|
};
|
|
|
|
|
|
|
|
MultiLineEditTextView.prototype.addText = function(text) {
|
2015-07-12 02:12:07 +00:00
|
|
|
this.insertRawText(text);
|
2015-09-07 06:12:01 +00:00
|
|
|
|
|
|
|
if(this.isEditMode()) {
|
|
|
|
this.cursorEndOfDocument();
|
|
|
|
} else if(this.isPreviewMode()) {
|
|
|
|
this.cursorStartOfDocument();
|
|
|
|
}
|
2015-06-02 05:00:54 +00:00
|
|
|
};
|
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.prototype.getData = function() {
|
2016-03-23 04:24:00 +00:00
|
|
|
return this.getOutputText(0, this.textLines.length, '\r\n');
|
2015-06-27 21:32:29 +00:00
|
|
|
};
|
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.prototype.setPropertyValue = function(propName, value) {
|
2015-09-03 05:11:17 +00:00
|
|
|
switch(propName) {
|
|
|
|
case 'mode' : this.mode = value; break;
|
|
|
|
}
|
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.super_.prototype.setPropertyValue.call(this, propName, value);
|
2015-06-30 05:14:17 +00:00
|
|
|
};
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
var HANDLED_SPECIAL_KEYS = [
|
|
|
|
'up', 'down', 'left', 'right',
|
|
|
|
'home', 'end',
|
2015-06-20 06:40:23 +00:00
|
|
|
'page up', 'page down',
|
|
|
|
'line feed',
|
2015-06-07 01:34:55 +00:00
|
|
|
'insert',
|
2015-06-14 03:49:12 +00:00
|
|
|
'tab',
|
|
|
|
'backspace', 'del',
|
2015-06-20 06:40:23 +00:00
|
|
|
'delete line',
|
2015-06-05 22:20:26 +00:00
|
|
|
];
|
|
|
|
|
2015-09-03 05:11:17 +00:00
|
|
|
var PREVIEW_MODE_KEYS = [
|
|
|
|
'up', 'down', 'page up', 'page down'
|
|
|
|
];
|
|
|
|
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.prototype.onKeyPress = function(ch, key) {
|
2015-06-05 22:20:26 +00:00
|
|
|
var self = this;
|
|
|
|
var handled;
|
|
|
|
|
|
|
|
if(key) {
|
|
|
|
HANDLED_SPECIAL_KEYS.forEach(function aKey(specialKey) {
|
2015-07-02 02:18:34 +00:00
|
|
|
if(self.isKeyMapped(specialKey, key.name)) {
|
2015-09-03 05:11:17 +00:00
|
|
|
|
|
|
|
if(self.isPreviewMode() && -1 === PREVIEW_MODE_KEYS.indexOf(specialKey)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-05 22:20:26 +00:00
|
|
|
self[_.camelCase('keyPress ' + specialKey)]();
|
|
|
|
handled = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-09-03 05:11:17 +00:00
|
|
|
if(self.isEditMode() && ch && strUtil.isPrintable(ch)) {
|
2015-06-05 22:20:26 +00:00
|
|
|
this.keyPressCharacter(ch);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!handled) {
|
2015-07-02 02:35:03 +00:00
|
|
|
MultiLineEditTextView.super_.prototype.onKeyPress.call(this, ch, key);
|
2015-06-05 22:20:26 +00:00
|
|
|
}
|
|
|
|
};
|
2015-07-06 05:16:22 +00:00
|
|
|
|
|
|
|
MultiLineEditTextView.prototype.getTextEditMode = function() {
|
|
|
|
return this.overtypeMode ? 'overtype' : 'insert';
|
|
|
|
};
|
|
|
|
|
|
|
|
MultiLineEditTextView.prototype.getEditPosition = function() {
|
2015-09-20 04:55:09 +00:00
|
|
|
var currentIndex = this.getTextLinesIndex() + 1;
|
|
|
|
|
|
|
|
return {
|
|
|
|
row : this.getTextLinesIndex(this.cursorPos.row),
|
|
|
|
col : this.cursorPos.col,
|
|
|
|
percent : Math.floor(((currentIndex / this.textLines.length) * 100)),
|
|
|
|
below : this.getRemainingLinesBelowRow(),
|
|
|
|
};
|
2015-07-06 05:16:22 +00:00
|
|
|
};
|
|
|
|
|