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
|
//> This file is part of Ymacs, an Emacs-like editor for the Web
//> http://www.ymacs.org/
//>
//> Copyright (c) 2009-2010, Mihai Bazon, Dynarch.com. All rights reserved.
//>
//> Redistribution and use in source and binary forms, with or without
//> modification, are permitted provided that the following conditions are
//> met:
//>
//> * Redistributions of source code must retain the above copyright
//> notice, this list of conditions and the following disclaimer.
//>
//> * Redistributions in binary form must reproduce the above copyright
//> notice, this list of conditions and the following disclaimer in
//> the documentation and/or other materials provided with the
//> distribution.
//>
//> * Neither the name of Dynarch.com nor the names of its contributors
//> may be used to endorse or promote products derived from this
//> software without specific prior written permission.
//>
//> THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER “AS IS” AND ANY
//> EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//> IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
//> PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE
//> FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//> CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
//> SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//> INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//> CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//> ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
//> THE POSSIBILITY OF SUCH DAMAGE.
// @require ymacs-buffer.js
Ymacs_Buffer.newCommands({
get_region: function() {
return this.getRegion();
},
cperl_lineup: Ymacs_Interactive("r", function(begin, end){
this.cmd("save_excursion", function(){
var rcend = this._positionToRowCol(end), max = 0, lines = [];
this.cmd("goto_char", begin);
this.cmd("forward_whitespace", true);
var ch = this.charAt();
if (ch.toLowerCase() != ch.toUpperCase()) {
this.signalError("Cannot lineup here");
return;
}
while (this._rowcol.row <= rcend.row) {
var pos = this.getLine().indexOf(ch);
if (pos >= 0) {
if (pos > max)
max = pos;
lines.push([ this._rowcol.row, pos ]);
}
if (!this.cmd("forward_line"))
break;
}
++max;
lines.foreach(function(l){
this.cmd("goto_char", this._rowColToPosition(l[0], l[1]));
this.cmd("insert", " ".x(max - l[1]));
}, this);
});
}),
htmlize_region: Ymacs_Interactive("r\nP", function(begin, end, lineNum) {
this.tokenizer.finishParsing();
var row = this._positionToRowCol(begin).row,
html = String.buffer(),
line = row, pad;
if (lineNum && !lineNum.empty)
line = parseInt(lineNum, 10);
end = this._positionToRowCol(end).row;
pad = String(end).length;
while (row <= end) {
html("<div class='line'>");
if (lineNum)
html("<span class='line-number'>", line.zeroPad(pad, " "), "</span>");
++line;
html(this._textProperties.getLineHTML(row, this.code[row], null), "</div>\n");
++row;
}
html = html.get();
var tmp = this.ymacs.switchToBuffer("*Htmlize*");
tmp.setCode(html);
tmp.cmd("xml_mode", true);
}),
execute_extended_command: Ymacs_Interactive("^CM-x ", function(cmd) {
this.callInteractively(cmd);
}),
set_variable: Ymacs_Interactive("vSet variable: \nsTo value: ", function(variable, value) {
var tmp = parseFloat(value);
if (!isNaN(tmp))
value = tmp;
this.setq(variable, value);
}),
eval_string: Ymacs_Interactive("^MEval string: ", function(code){
try {
var variables = [
this, // buffer
this.ymacs // ymacs
];
code = new Function("buffer", "ymacs", code);
code.apply(this, variables);
this.clearTransientMark();
} catch(ex) {
this.signalError(ex.type + ": " + ex.message);
if (window.console)
console.log(ex);
}
}),
eval_region: Ymacs_Interactive("^r", function(begin, end) {
this.cmd("eval_string", this.cmd("buffer_substring", begin, end));
}),
eval_buffer: Ymacs_Interactive(function(){
this.cmd("eval_string", this.getCode());
}),
toggle_line_numbers: Ymacs_Interactive("^", function(){
this.whenActiveFrame("toggleLineNumbers");
}),
save_file: Ymacs_Interactive("FWrite file: ", function(name){
var files = this.ymacs.ls_getFileDirectory(name, "file");
files.dir[files.other[0]] = this.getCode();
this.ymacs.ls_set(files.store);
this.signalInfo("Saved in local storage");
}),
load_file: Ymacs_Interactive("fFind file: ", function(name){
var code = this.ymacs.ls_getFileContents(name);
var buffer = this.ymacs.createBuffer({ name: name });
buffer.setCode(code);
this.cmd("switch_to_buffer", name);
}),
delete_file: Ymacs_Interactive("fDelete file: ", function(name){
this.ymacs.ls_getFileContents(name);
var files = this.ymacs.ls_get();
delete files[name];
this.ymacs.ls_set(files);
}),
eval_file: Ymacs_Interactive("fEval file: ", function(name){
this.cmd("eval_string", this.ymacs.ls_getFileContents(name));
})
});
|