forked from lkesteloot/pad
/
editor_pane.js
120 lines (100 loc) · 3.27 KB
/
editor_pane.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2014 Lawrence Kesteloot
"use strict";
var util = require("util");
var path = require("path");
var WrappingFormatter = require("./wrapping_formatter");
var Doc = require("./doc");
var Pane = require("./pane");
var strings = require("./strings");
// Subclass of Pane that edits a document.
var EditorPane = function (window, x, y, width, height, mainPane) {
Pane.call(this, window, x, y, width, height, mainPane);
this.setDoc(new Doc());
};
util.inherits(EditorPane, Pane);
// Override
EditorPane.prototype.format = function () {
Pane.prototype.format.call(this);
var formatter = new WrappingFormatter(this.contentWidth);
formatter.format(this.doc, this.layout);
};
// Override
EditorPane.prototype.generateStatusLine = function () {
// Left half of the status line (filename, modified).
var left;
if (this.doc.filename === "") {
left = "[No Name]";
} else {
left = strings.unexpandHome(this.doc.filename);
}
if (this.doc.isModified()) {
left += " [+]";
}
// Right half (key-specific state).
var right = this.keys.getState(this);
// Padding in the middle. Clip to make room.
var paddingSize = this.contentWidth - left.length - right.length;
if (paddingSize < 0) {
left = "..." + left.substring(-paddingSize + 3);
paddingSize = this.contentWidth - left.length - right.length;
}
var padding = strings.repeat(" ", paddingSize);
// The spot under the vertical divider.
var divider = strings.repeat(" ", this.width - this.contentWidth);
return left + padding + right + divider;
};
EditorPane.prototype.setDoc = function (doc) {
this.doc = doc;
this.doc.events.on("change", this.onDocModified.bind(this));
this.doc.events.on("modified", this.onDocModified.bind(this));
this.cursorX = 0; // In layout space.
this.cursorY = 0;
this.docIndex = 0;
this.topY = 0; // Top of pane, in layout space.
this.layoutDirty = true;
this.queueRedraw();
this.events.emit("change");
};
EditorPane.prototype.onDocModified = function () {
this.layoutDirty = true;
process.nextTick(this.sanitizeAndRefresh.bind(this));
this.events.emit("change");
};
EditorPane.prototype.loadFile = function (filename, callback) {
var doc = new Doc();
var self = this;
var pathname = path.resolve(filename);
doc.readFile(pathname, function (err) {
if (err) {
if (err.code === "ENOENT") {
console.log("File not found: " + pathname);
} else {
console.log("Error loading file: " + err);
}
} else {
self.setDoc(doc);
self.redrawIfNecessary();
if (callback) {
callback();
}
}
});
};
EditorPane.prototype.saveFile = function (callback) {
var self = this;
this.doc.saveFile(function (err) {
// XXX check err.
// Update the status line:
self.redrawDirty = true;
callback();
});
};
/**
* Return the current line, not including the EOL.
*/
EditorPane.prototype.getCurrentLine = function () {
var start = this.doc.findStartOfLine(this.docIndex);
var end = this.doc.findEndOfLine(start);
return this.doc.toString(start, end);
};
module.exports = EditorPane;