session.js: correct parenthesization in 2 places
[conkeror/arlinius.git] / modules / builtin-commands.js
blobca89ba990716aa56b005087bd8575f4ba9f81985
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2009 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 function define_builtin_commands (prefix, do_command_function, toggle_mark, mark_active_predicate, mode) {
12     // Specify a docstring
13     function D (cmd, docstring) {
14         var o = new String(cmd);
15         o.doc = docstring;
16         return o;
17     }
19     // Specify a forward/reverse pair
20     function R (a, b) {
21         var o = [a,b];
22         o.is_reverse_pair = true;
23         return o;
24     }
26     // Specify a movement/select/scroll/move-caret command group.
27     function S (command, movement, select, scroll, caret) {
28         var o = [movement, select, scroll, caret];
29         o.command = command;
30         o.is_move_select_pair = true;
31         return o;
32     }
34     var builtin_commands = [
36         /*
37          * cmd_scrollBeginLine and cmd_scrollEndLine don't do what I
38          * want, either in or out of caret mode...
39          */
40         S(D("beginning-of-line", "Move or extend the selection to the beginning of the current line."),
41           D("cmd_beginLine", "Move point to the beginning of the current line."),
42           D("cmd_selectBeginLine", "Extend selection to the beginning of the current line."),
43           D("cmd_beginLine", "Scroll to the beginning of the line"),
44           D("cmd_beginLine", "Scroll to the beginning of the line")),
45         S(D("end-of-line", "Move or extend the selection to the end of the current line."),
46           D("cmd_endLine", "Move point to the end of the current line."),
47           D("cmd_selectEndLine", "Extend selection to the end of the current line."),
48           D("cmd_endLine", "Scroll to the end of the current line."),
49           D("cmd_endLine", "Scroll to the end of the current line.")),
50         S(D("beginning-of-first-line", "Move or extend the selection to the beginning of the first line."),
51           D("cmd_moveTop", "Move point to the beginning of the first line."),
52           D("cmd_selectTop", "Extend selection to the beginning of the first line."),
53           D("cmd_scrollTop", "Scroll to the top of the buffer"),
54           D("cmd_scrollTop", "Move point to the beginning of the first line.")),
55         S(D("end-of-last-line", "Move or extend the selection to the end of the last line."),
56           D("cmd_moveBottom", "Move point to the end of the last line."),
57           D("cmd_selectBottom", "Extend selection to the end of the last line."),
58           D("cmd_scrollBottom", "Scroll to the bottom of the buffer"),
59           D("cmd_scrollBottom", "Move point to the end of the last line.")),
60         "cmd_copyOrDelete",
61         "cmd_scrollBeginLine",
62         "cmd_scrollEndLine",
63         "cmd_cutOrDelete",
64         D("cmd_copy", "Copy the selection into the clipboard."),
65         D("cmd_cut", "Cut the selection into the clipboard."),
66         D("cmd_deleteToBeginningOfLine", "Delete to the beginning of the current line."),
67         D("cmd_deleteToEndOfLine", "Delete to the end of the current line."),
68         D("cmd_selectAll", "Select all."),
69         D("cmd_scrollTop", "Scroll to the top of the buffer."),
70         D("cmd_scrollBottom", "Scroll to the bottom of the buffer.")];
72     var builtin_commands_with_count = [
73         R(S(D("forward-char", "Move or extend the selection forward one character."),
74             D("cmd_charNext", "Move point forward one character."),
75             D("cmd_selectCharNext", "Extend selection forward one character."),
76             D("cmd_scrollRight", "Scroll to the right"),
77             D("cmd_scrollRight", "Scroll to the right")),
78           S(D("backward-char", "Move or extend the selection backward one character."),
79             D("cmd_charPrevious", "Move point backward one character."),
80             D("cmd_selectCharPrevious", "Extend selection backward one character."),
81             D("cmd_scrollLeft", "Scroll to the left."),
82             D("cmd_scrollLeft", "Scroll to the left."))),
83         R(D("cmd_deleteCharForward", "Delete the following character."),
84           D("cmd_deleteCharBackward", "Delete the previous character.")),
85         R(D("cmd_deleteWordForward", "Delete the following word."),
86           D("cmd_deleteWordBackward", "Delete the previous word.")),
87         R(S(D("forward-line", "Move or extend the selection forward one line."),
88             D("cmd_lineNext", "Move point forward one line."),
89             D("cmd_selectLineNext", "Extend selection forward one line."),
90             D("cmd_scrollLineDown", "Scroll down one line."),
91             D("cmd_scrollLineDown", "Scroll down one line.")),
92           S(D("backward-line", "Move or extend the selection backward one line."),
93             D("cmd_linePrevious", "Move point backward one line."),
94             D("cmd_selectLinePrevious", "Extend selection backward one line."),
95             D("cmd_scrollLineUp", "Scroll up one line."),
96             D("cmd_scrollLineUp", "Scroll up one line."))),
97         R(S(D("forward-page", "Move or extend the selection forward one page."),
98             D("cmd_movePageDown", "Move point forward one page."),
99             D("cmd_selectPageDown", "Extend selection forward one page."),
100             D("cmd_scrollPageDown", "Scroll forward one page."),
101             D("cmd_movePageDown", "Move point forward one page.")),
102           S(D("backward-page", "Move or extend the selection backward one page."),
103             D("cmd_movePageUp", "Move point backward one page."),
104             D("cmd_selectPageUp", "Extend selection backward one page."),
105             D("cmd_scrollPageUp", "Scroll backward one page."),
106             D("cmd_movePageUp", "Move point backward one page."))),
107         R(D("cmd_undo", "Undo last editing action."),
108           D("cmd_redo", "Redo last editing action.")),
109         R(S(D("forward-word", "Move or extend the selection forward one word."),
110             D("cmd_wordNext", "Move point forward one word."),
111             D("cmd_selectWordNext", "Extend selection forward one word."),
112             D("cmd_scrollRight", "Scroll to the right."),
113             D("cmd_wordNext", "Move point forward one word.")),
114           S(D("backward-word", "Move or extend the selection backward one word."),
115             D("cmd_wordPrevious", "Move point backward one word."),
116             D("cmd_selectWordPrevious", "Extend selection backward one word."),
117             D("cmd_scrollLeft", "Scroll to the left."),
118             D("cmd_wordPrevious", "Move point backward one word."))),
119         R(D("cmd_scrollPageUp", "Scroll up one page."),
120           D("cmd_scrollPageDown", "Scroll down one page.")),
121         R(D("cmd_scrollLineUp", "Scroll up one line."),
122           D("cmd_scrollLineDown", "Scroll down one line.")),
123         R(D("cmd_scrollLeft", "Scroll left."),
124           D("cmd_scrollRight", "Scroll right.")),
125         D("cmd_paste", "Insert the contents of the clipboard.")];
127     interactive(prefix + "set-mark",
128                 "Toggle whether the mark is active.\n" +
129                 "When the mark is active, movement commands affect the selection.",
130                 toggle_mark);
132     function get_mode_idx () {
133         if (mode == 'scroll') return 2;
134         else if (mode == 'caret') return 3;
135         else return 0;
136     }
138     function get_move_select_idx (I) {
139         return mark_active_predicate(I) ? 1 : get_mode_idx();
140     }
142     function doc_for_builtin (c) {
143         var s = "";
144         if (c.doc != null)
145             s += c.doc + "\n";
146         return s + "Run the built-in command " + c + ".";
147     }
149     function define_simple_command (c) {
150         interactive(prefix + c, doc_for_builtin(c), function (I) { do_command_function(I, c); });
151     }
153     function get_move_select_doc_string (c) {
154         return c.command.doc +
155             "\nSpecifically, if the mark is active, runs `" + prefix + c[1] + "'.  " +
156             "Otherwise, runs `" + prefix + c[get_mode_idx()] + "'\n" +
157             "To toggle whether the mark is active, use `" + prefix + "set-mark'.";
158     }
160     for each (let c_temp in builtin_commands) {
161         let c = c_temp;
162         if (c.is_move_select_pair) {
163             interactive(prefix + c.command, get_move_select_doc_string(c), function (I) {
164                 var idx = get_move_select_idx(I);
165                 do_command_function(I, c[idx]);
166             });
167             define_simple_command(c[0]);
168             define_simple_command(c[1]);
169         }
170         else
171             define_simple_command(c);
172     }
174     function get_reverse_pair_doc_string (main_doc, alt_command) {
175         return main_doc + "\n" +
176             "The prefix argument specifies a repeat count for this command.  " +
177             "If the count is negative, `" + prefix + alt_command + "' is performed instead with " +
178             "a corresponding positive repeat count.";
179     }
181     function define_simple_reverse_pair (a, b) {
182         interactive(prefix + a, get_reverse_pair_doc_string(doc_for_builtin(a), b),
183                     function (I) {
184                         do_repeatedly(do_command_function, I.p, [I, a], [I, b]);
185                     });
186         interactive(prefix + b, get_reverse_pair_doc_string(doc_for_builtin(b), a),
187                     function (I) {
188                         do_repeatedly(do_command_function, I.p, [I, b], [I, a]);
189                     });
190     }
192     for each (let c_temp in builtin_commands_with_count) {
193         let c = c_temp;
194         if (c.is_reverse_pair) {
195             if (c[0].is_move_select_pair) {
196                 interactive(prefix + c[0].command, get_reverse_pair_doc_string(get_move_select_doc_string(c[0]),
197                                                                                c[1].command),
198                             function (I) {
199                                 var idx = get_move_select_idx(I);
200                                 do_repeatedly(do_command_function, I.p, [I, c[0][idx]], [I, c[1][idx]]);
201                             });
202                 interactive(prefix + c[1].command, get_reverse_pair_doc_string(get_move_select_doc_string(c[1]),
203                                                                                c[0].command),
204                             function (I) {
205                                 var idx = get_move_select_idx(I);
206                                 do_repeatedly(do_command_function, I.p, [I, c[1][idx]], [I, c[0][idx]]);
207                             });
208                 define_simple_reverse_pair(c[0][0], c[1][0]);
209                 define_simple_reverse_pair(c[0][1], c[1][1]);
210             } else
211                 define_simple_reverse_pair(c[0], c[1]);
212         } else {
213             let doc = doc_for_builtin(c) +
214                 "\nThe prefix argument specifies a positive repeat count for this command.";
215             interactive(prefix + c, doc, function (I) {
216                 do_repeatedly_positive(do_command_function, I.p, I, c);
217             });
218         }
219     }