<commit_before>var _ = require('lazy.js');
var extend = require('xtend');
var blessed = require('blessed');
var clipboard = require('copy-paste').noConflict();

var util = require('./util');
var word = require('./word');
var Coordinate = require('./coordinate');

function Editor (opts) {
  var self = this;

  if (!(self instanceof blessed.Node)) { return new Editor(opts); }

  blessed.Box.call(self, extend({
    tags: true,
    wrap: false,

    // Custom
    tabSize: 4,
    pageLines: 10,
    selectStyle: '{blue-bg}'
  }, opts));

  self
    .toggleInsertMode()
    .text('')
    .cursor({x: 0, y: 0})
    .scroll({x: 0, y: 0})
    ._initHandlers();
}
Editor.prototype.__proto__ = blessed.Box.prototype;

Editor.prototype._initHandlers = function () {
  var self = this;

  self.on('keypress', function (ch, key) {
    var direction = {
      left: -1, right: 1,
      up: -1, down: 1,
      pageup: -1, pagedown: 1,
      home: -1, end: 1,
      backspace: -1, 'delete': 1
    }[key.name];
    if (direction) {
      if (key.name === 'backspace' || key.name === 'delete') {
        if (!self.select().text) {
          self
            .startSelection(self.cursor())
            .moveCursorHorizontal(direction, key.ctrl);
        }
        self.delete(); return;
      }

      var prevSelection = self.startSelection();
      if (!key.shift) {
        self.startSelection(null);
      } else if (!prevSelection) {
        self.startSelection(self.cursor());
      }

      if (key.name === 'left' || key.name === 'right') {
        if (!key.shift && prevSelection && Coordinate.linear.cmp(prevSelection, self.cursor()) === direction) {
          self.cursor(prevSelection);
        } else {
          self.moveCursorHorizontal(direction, key.ctrl);
        }
      } else if (key.name === 'up' || key.name === 'down') {
        self.moveCursorVertical(direction, key.ctrl);
      } else if (key.name === 'pageup' || key.name === 'pagedown') {
        self.moveCursorVertical(direction * self.options.pageLines);
      } else if (key.name === 'home') {
        this.cursor({x: 0, y: self.cursor().y});
      } else if (key.name === 'end') {
        this.cursor({x: Infinity, y: self.cursor().y});
      }
    } else if (key.full === 'C-a') {
      self.select(Coordinate.returnsOrigin(), Coordinate.returnsInfinity());
    } else if (key.full === 'C-c' || key.full === 'C-x') {
      var selection = self.select();
      if (selection.text) { clipboard.copy(selection.text); }
      if (key.full === 'C-x') { self.delete(); }
    } else if (key.full === 'C-v') {
      clipboard.paste(function (err, text) {
        if (err) { throw err; }
        self.change(text);
      });
    } else if (key.name === 'insert') {
      self.toggleInsertMode();
    } else if (!key.ctrl && ch) {
      if (ch === '\r') {
        // FIXME: hack
        ch = '\n';
        if (self.data.enterPressed) { return; }
        self.data.enterPressed = true;
        process.nextTick(function () { self.data.enterPressed = false; });
      }
      var overwrite = !self.insertMode() && !self.select().text && !self.data.enterPressed;
      var cursor = self.cursor();
      self.change(ch, cursor, extend(cursor, {x: cursor.x + overwrite}));
    }
  });

  self.on('mouse', function (mouseData) {
    var mouse = Coordinate(mouseData).subtract(self.pos()).add(self.scroll());

    if (mouseData.action === 'wheeldown' || mouseData.action === 'wheelup') {
      if (!mouseData.shift && !self.data.mouseDown) {
        self.startSelection(null);
      } else if (!self.startSelection()) {
        self.startSelection(self.cursor());
      }
      self.moveCursorVertical({
        wheelup: -1,
        wheeldown: 1
      }[mouseData.action] * self.options.pageLines);
    } else {
      if (mouseData.action === 'mousedown') {
        self.data.mouseDown = true;
        self.startSelection(mouse);
      }
      if (self.data.mouseDown) {<commit_msg>Fixes editing selections<commit_after>var _ = require('lazy.js');
var extend = require('xtend');
var blessed = require('blessed');
var clipboard = require('copy-paste').noConflict();

var util = require('./util');
var word = require('./word');
var Coordinate = require('./coordinate');

function Editor (opts) {
  var self = this;

  if (!(self instanceof blessed.Node)) { return new Editor(opts); }

  blessed.Box.call(self, extend({
    tags: true,
    wrap: false,

    // Custom
    tabSize: 4,
    pageLines: 10,
    selectStyle: '{blue-bg}'
  }, opts));

  self
    .toggleInsertMode()
    .text('')
    .cursor({x: 0, y: 0})
    .scroll({x: 0, y: 0})
    ._initHandlers();
}
Editor.prototype.__proto__ = blessed.Box.prototype;

Editor.prototype._initHandlers = function () {
  var self = this;

  self.on('keypress', function (ch, key) {
    var direction = {
      left: -1, right: 1,
      up: -1, down: 1,
      pageup: -1, pagedown: 1,
      home: -1, end: 1,
      backspace: -1, 'delete': 1
    }[key.name];
    if (direction) {
      if (key.name === 'backspace' || key.name === 'delete') {
        if (!self.select().text) {
          self
            .startSelection(self.cursor())
            .moveCursorHorizontal(direction, key.ctrl);
        }
        self.delete(); return;
      }

      var prevSelection = self.startSelection();
      if (!key.shift) {
        self.startSelection(null);
      } else if (!prevSelection) {
        self.startSelection(self.cursor());
      }

      if (key.name === 'left' || key.name === 'right') {
        if (!key.shift && prevSelection && Coordinate.linear.cmp(prevSelection, self.cursor()) === direction) {
          self.cursor(prevSelection);
        } else {
          self.moveCursorHorizontal(direction, key.ctrl);
        }
      } else if (key.name === 'up' || key.name === 'down') {
        self.moveCursorVertical(direction, key.ctrl);
      } else if (key.name === 'pageup' || key.name === 'pagedown') {
        self.moveCursorVertical(direction * self.options.pageLines);
      } else if (key.name === 'home') {
        this.cursor({x: 0, y: self.cursor().y});
      } else if (key.name === 'end') {
        this.cursor({x: Infinity, y: self.cursor().y});
      }
    } else if (key.full === 'C-a') {
      self.select(Coordinate.returnsOrigin(), Coordinate.returnsInfinity());
    } else if (key.full === 'C-c' || key.full === 'C-x') {
      var selection = self.select();
      if (selection.text) { clipboard.copy(selection.text); }
      if (key.full === 'C-x') { self.delete(); }
    } else if (key.full === 'C-v') {
      clipboard.paste(function (err, text) {
        if (err) { throw err; }
        self.change(text);
      });
    } else if (key.name === 'insert') {
      self.toggleInsertMode();
    } else if (!key.ctrl && ch) {
      if (ch === '\r') {
        // FIXME: hack
        ch = '\n';
        if (self.data.enterPressed) { return; }
        self.data.enterPressed = true;
        process.nextTick(function () { self.data.enterPressed = false; });
      }
      if (self.select().text) {
        self.change(ch);
      } else {
        var overwrite = !self.insertMode() && !self.data.enterPressed;
        var cursor = self.cursor();
        self.change(ch, cursor, extend(cursor, {x: cursor.x + overwrite}));
      }
    }
  });

  self.on('mouse', function (mouseData) {
    var mouse = Coordinate(mouseData).subtract(self.pos()).add(self.scroll());

    if (mouseData.action === 'wheeldown' || mouseData.action === 'wheelup') {
      if (!mouseData.shift && !self.data.mouseDown) {
        self.startSelection(null);
      } else if (!self.startSelection()) {
        self.startSelection(self.cursor());
      }
      self.moveCursorVertical({
        wheelup: -1,
        wheeldown: 1
      }[mouseData.action] * self.options.pageLines);
    } else {
      if (mouseData.action === 'mousedown') {
        self.data.mouseDown = true;
        self.startSelection(mouse);
      }
      if (self.data.mouseDown) {
{'fraction_non_alphanumeric': 0.18240181268882175, 'fraction_numerical': 0.005035246727089627, 'mean_word_length': 2.4245689655172415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '173951', 'n_tokens_mistral': 2575, 'n_tokens_neox': 2515, 'n_words': 549}