summaryrefslogtreecommitdiff
path: root/gstudio/static/gstudio/js/Gnowmacs/src/js/.svn/text-base/ymacs-interactive.js.svn-base
blob: 565c2646b5b1d4d4748746090c1d96546c2ff44a (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
//> 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-exception.js

(function(){

        /*
         * Ymacs_Interactive(args_description, function_reference)
         *
         * This is a wrapper that makes it easy to define "interactive" commands.  Pass two arguments: arguments
         * description (args), and a function (func).  args can be null, or a string, an array or a function.  When null
         * it is assumed that the function should not receive any arguments.  When an array or a string, it contains
         * some argument descriptions similar to Emacs:
         *
         *    http://www.gnu.org/s/emacs/manual/html_node/elisp/Interactive-Codes.html#Interactive-Codes
         *
         * (note that Emacs does not take a list for this argument).
         *
         * Ymacs_Interactive returns func.  When not called interactively, the code should supply all the required
         * arguments and the function is called with no performance penalty.  To call it interactively, use
         * func.ymacsCallInteractively(), which will read arguments from the minibuffer according to their description.
         */

        window.Ymacs_Interactive = function(args, func) {
                if (arguments.length == 1) {
                        func = args;
                        args = null;
                } else {
                        var documentation;
                        if (!(func instanceof Function)) {
                                documentation = func;
                                func = arguments[2];
                                func.ymacsDoc = documentation;
                        }
                }
                func.ymacsInteractive = true;
                if (args instanceof Function) {
                        func.ymacsGetArgs = args;
                }
                else if (args != null) {
                        if (!(args instanceof Array)) {
                                var m = /^[\^\@\*]+/.exec(args);
                                if (m) {
                                        m = m[0];
                                        args = args.substr(m.length);
                                        if (m.indexOf("^") >= 0) {
                                                func.ymacsMarkExtend = true;
                                        }
                                        if (m.indexOf("*") >= 0) {
                                                func.ymacsWarnReadonly = true;
                                        }
                                        if (m.indexOf("@") >= 0) {
                                                func.ymacsSelectFrame = true;
                                        }
                                }
                                if (args)
                                        args = args.split(/\n+/);
                        }
                        if (args) {
                                var collect,
                                    execute = function() {
                                            collect.append(Array.$(arguments));
                                            return this.callInteractively(func, collect, true);
                                    };
                                while (args.length > 0) {
                                        execute = createArgumentFunction(args.pop(), function(next) {
                                                collect.append(Array.$(arguments, 1));
                                                next.call(this);
                                        }.$(null, execute));
                                }
                                func.ymacsCallInteractively = function(){
                                        collect = [];
                                        return execute.call(this);
                                };
                        }
                }
                return func;
        };

        window.Ymacs_Interactive_X = function(func) {
                return Ymacs_Interactive("p", function(n){
                        if (n == null) n = 1;
                        n.times(func, this);
                });
        };

        var $TRUE = (function(){});
        $TRUE.toString = function() { return "" };
        $TRUE.empty = true;

        /* -----[ argument reader functions ]----- */

        function prompt(arg) {
                var pr = this.getPrefixArg(true /* noDiscard */);
                if (pr) {
                        arg = pr + " " + arg;
                }
                this.cmd("minibuffer_prompt", arg);
        };

        function read_function_name(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_function", cont);
                // XXX: enforce it!
        };

        function read_existing_buffer_name(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_buffer", cont);
                // XXX: enforce it!
        };

        function read_buffer_name(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_buffer", cont);
        };

        function read_character(arg, cont) {

        };

        function read_command_name(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_command", cont);
                // XXX: enforce it!
        };

        function get_point(arg, cont) {
                cont.call(this, this.point());
        };

        function get_mouse_event(arg, cont) {

        };

        function irrelevant(arg, cont) {
                cont.call(this, null);
        };

        function read_key_sequence(arg, cont) {

        };

        function read_key_sequence2(arg, cont) {

        };

        function get_mark(arg, cont) {
                cont.call(this, this.markMarker.getPosition());
        };

        function read_arbitrary_text(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_string", null, cont);
        };

        function read_number(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_number", cont);
        };

        function read_number_or_prefix(arg, cont) {
                var n = parseInt(this.getPrefixArg(), 10);
                if (!isNaN(n))
                        cont.call(this, n);
                else
                        read_number.call(this, arg, cont);
        };

        function get_numeric_prefix(arg, cont) {
                var n = parseInt(this.getPrefixArg(), 10);
                if (isNaN(n))
                        n = null;
                cont.call(this, n);
        };

        function get_raw_prefix(arg, cont) {
                arg = this.getPrefixArg();
                if (arg === "")
                        arg = $TRUE;
                cont.call(this, arg);
        };

        function get_point_and_mark(arg, cont) {
                var r = this.getRegion();
                cont.call(this, r.begin, r.end);
        };

        function read_key_sequence3(arg, cont) {

        };

        function read_variable_name(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_variable", cont);
        };

        function read_existing_file_name(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_existing_file", cont);
        };

        function read_file_name(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_file", cont);
        };

        function read_file_or_directory_name(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_file_or_directory", cont);
        };

        function read_existing_directory_name(arg, cont) {
                prompt.call(this, arg);
                this.cmd("minibuffer_read_directory", cont);
        };

        var ARG_READERS = {
                a: read_function_name,
                b: read_existing_buffer_name,
                B: read_buffer_name,
                c: read_character,
                C: read_command_name,
                d: get_point,
                e: get_mouse_event,
                i: irrelevant,
                k: read_key_sequence,
                K: read_key_sequence2,
                m: get_mark,
                M: read_arbitrary_text,
                n: read_number,
                N: read_number_or_prefix,
                p: get_numeric_prefix,
                P: get_raw_prefix,
                r: get_point_and_mark,
                s: read_arbitrary_text,
                U: read_key_sequence3,
                v: read_variable_name,

                f: read_existing_file_name,
                F: read_file_name,
                G: read_file_or_directory_name,
                D: read_existing_directory_name

                // S: no reader for interned symbols in Ymacs
                // no x, X, z and Z either
        };

        function createArgumentFunction(arg, cont) {
                var code = arg.charAt(0);
                arg = arg.substr(1);
                return ARG_READERS[code].$(null, arg, cont);
        };

})();