-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
187 lines (170 loc) · 5.21 KB
/
cli.js
File metadata and controls
187 lines (170 loc) · 5.21 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
* Vim mode for the Cloud9 IDE
*
* @author Sergi Mansilla <sergi AT c9 DOT io>
* @copyright 2011, Ajax.org B.V.
* @license GPLv3 <http://www.gnu.org/licenses/gpl.txt>
*/
define(function(require, exports, module) {
var filesystem = require("ext/filesystem/filesystem");
var gotofile = require("ext/gotofile/gotofile");
var editors = require("ext/editors/editors");
var save = require("ext/save/save");
var ide = require("core/ide");
var cliCmds = exports.cliCmds = {
w: function(editor, data) {
var page = tabEditors.getPage();
if (!page)
return;
var lines = editor.session.getLength();
if (data.argv.length === 2) {
var path = (ide.davPrefix + "/" + data.argv[1]).replace(/\/+/, "/");
page.$model.data.setAttribute("path", path);
save.saveas(page, function() {
console.log(path + " [New] " + lines + "L, ##C written");
});
}
else {
save.quicksave(null, function() {
console.log(page.name + " " + lines +"L, ##C written");
});
}
},
e: function(editor, data) {
var path = data.argv[1];
if (!path) {
gotofile.toggleDialog(1);
return false;
}
else {
path = (ide.davPrefix + "/" + path).replace(/\/+/, "/");
filesystem.exists(path, function(exists){
if (exists) {
editors.gotoDocument({path: path});
}
else {
var node = filesystem.createFileNodeFromPath(path);
node.setAttribute("newfile", "1");
node.setAttribute("changed", "1");
node.setAttribute("cli", "1"); // blocks Save As dialog
var doc = ide.createDocument(node);
doc.cachedValue = "";
editors.gotoDocument({
doc: doc,
type: "newfile",
origin: "newfile"
});
}
});
}
},
x: function(editor, data) {
var page = tabEditors.getPage();
if (!page)
return;
if (page.$doc.getNode().getAttribute("changed"))
cliCmds.w(editor, data);
cliCmds.q();
},
wq: function(editor, data) {
cliCmds.w(editor, data);
cliCmds.q();
},
q: function(editor, data) {
var page = tabEditors.getPage();
var corrected = ide.dispatchEvent("beforeclosetab", {
page: page
});
if (corrected)
page = corrected;
if (data && data.force) {
var at = page.$at;
at.undo_ptr = at.$undostack[at.$undostack.length-1];
page.$doc.getNode().removeAttribute("changed");
}
editors.close(page);
},
"q!": function() {
cliCmds.q(null, {force: true})
}
};
// aliases
cliCmds.write = cliCmds.w;
exports.searchStore = {
current: "",
options: {
needle: "",
backwards: false,
wrap: true,
caseSensitive: false,
wholeWord: false,
regExp: false
}
};
exports.focusCommandLine = function(val) {
txtConsoleInput.$editor.setValue(val, 1);
setTimeout(function() { txtConsoleInput.focus(); });
}
exports.actions = {
":": {
fn: function(editor, range, count, param) {
exports.focusCommandLine(":");
}
},
"/": {
fn: function(editor, range, count, param) {
exports.focusCommandLine("/");
}
},
"?": {
fn: function(editor, range, count, param) {
exports.focusCommandLine("?");
}
}
};
exports.addCommands = function(handler) {
var actions = handler.actions;
Object.keys(exports.actions).forEach(function(i){
actions[i] = exports.actions[i];
})
}
exports.onConsoleCommand = function(e) {
var cmd = e.data.command, success;
if ((typeof ceEditor !== "undefined") && cmd && typeof cmd === "string") {
var ed = ceEditor.$editor;
if (cmd[0] === ":") {
cmd = cmd.substr(1);
if (cliCmds[cmd]) {
success = cliCmds[cmd](ed, e.data);
}
else if (cmd.match(/^\d+$/)) {
ed.gotoLine(cmd, 0);
ed.navigateLineStart();
}
else {
console.log("Vim command '" + cmd + "' not implemented.");
}
e.returnValue = false;
}
else if (cmd[0] === "/" || cmd[0] === "?") {
var options = exports.searchStore.options;
options.backwards = cmd[0] === "?";
cmd = cmd.substr(1);
if (cmd)
exports.searchStore.current = cmd;
else
cmd = exports.searchStore.current;
ed.find(cmd, options);
setTimeout(function(){ ed.focus(); });
e.returnValue = false;
}
// something blocks focusing without timeout
if (success !== false) {
setTimeout(function(){
if (apf.activeElement == txtConsoleInput)
ceEditor.focus();
});
}
}
};
});