Debian package: Support xulrunner 9+10 in debian/conkeror.bin, drop support for unver...
[conkeror.git] / modules / builtin-commands.js
blobb75bb018bf8a716d7ca4153db7def858c4e388a0
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2010 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 in_module(null);
12 require("interactive.js");
14 interactive("set-mark",
15     "Toggle whether the mark is active.\n" +
16     "When the mark is active, movement commands affect the selection.",
17     function (I) {
18         var m = I.minibuffer;
19         var s = m.current_state;
20         if (m.active)
21             s.mark_active = !s.mark_active;
22         else
23             I.buffer.mark_active = !I.buffer.mark_active;
24     });
26 function mark_active_predicate (I) {
27     var m = I.minibuffer;
28     var s = m.current_state;
29     if (m.active)
30         return s.mark_active;
31     else
32         return I.buffer.mark_active;
35 function define_builtin_commands (prefix, do_command_function, mode) {
37     // Specify a docstring
38     function D (cmd, docstring) {
39         var o = new String(cmd);
40         o.doc = docstring;
41         return o;
42     }
44     // Specify a forward/reverse pair
45     function R (a, b) {
46         var o = [a,b];
47         o.is_reverse_pair = true;
48         return o;
49     }
51     // Specify a movement/select/scroll/move-caret command group.
52     function S (command, movement, select, scroll, caret) {
53         var o = [movement, select, scroll, caret];
54         o.command = command;
55         o.is_move_select_pair = true;
56         return o;
57     }
59     var builtin_commands = [
61         /*
62          * cmd_scrollBeginLine and cmd_scrollEndLine don't do what I
63          * want, either in or out of caret mode...
64          */
65         S(D("beginning-of-line", "Move or extend the selection to the beginning of the current line."),
66           D("cmd_beginLine", "Move point to the beginning of the current line."),
67           D("cmd_selectBeginLine", "Extend selection to the beginning of the current line."),
68           D("cmd_beginLine", "Scroll to the beginning of the line"),
69           D("cmd_beginLine", "Scroll to the beginning of the line")),
70         S(D("end-of-line", "Move or extend the selection to the end of the current line."),
71           D("cmd_endLine", "Move point to the end of the current line."),
72           D("cmd_selectEndLine", "Extend selection to the end of the current line."),
73           D("cmd_endLine", "Scroll to the end of the current line."),
74           D("cmd_endLine", "Scroll to the end of the current line.")),
75         S(D("beginning-of-first-line", "Move or extend the selection to the beginning of the first line."),
76           D("cmd_moveTop", "Move point to the beginning of the first line."),
77           D("cmd_selectTop", "Extend selection to the beginning of the first line."),
78           D("cmd_scrollTop", "Scroll to the top of the buffer"),
79           D("cmd_scrollTop", "Move point to the beginning of the first line.")),
80         S(D("end-of-last-line", "Move or extend the selection to the end of the last line."),
81           D("cmd_moveBottom", "Move point to the end of the last line."),
82           D("cmd_selectBottom", "Extend selection to the end of the last line."),
83           D("cmd_scrollBottom", "Scroll to the bottom of the buffer"),
84           D("cmd_scrollBottom", "Move point to the end of the last line.")),
85         "cmd_copyOrDelete",
86         "cmd_scrollBeginLine",
87         "cmd_scrollEndLine",
88         "cmd_cutOrDelete",
89         D("cmd_copy", "Copy the selection into the clipboard."),
90         D("cmd_cut", "Cut the selection into the clipboard."),
91         D("cmd_deleteToBeginningOfLine", "Delete to the beginning of the current line."),
92         D("cmd_deleteToEndOfLine", "Delete to the end of the current line."),
93         D("cmd_selectAll", "Select all."),
94         D("cmd_scrollTop", "Scroll to the top of the buffer."),
95         D("cmd_scrollBottom", "Scroll to the bottom of the buffer.")];
97     var builtin_commands_with_count = [
98         R(S(D("forward-char", "Move or extend the selection forward one character."),
99             D("cmd_charNext", "Move point forward one character."),
100             D("cmd_selectCharNext", "Extend selection forward one character."),
101             D("cmd_scrollRight", "Scroll to the right"),
102             D("cmd_scrollRight", "Scroll to the right")),
103           S(D("backward-char", "Move or extend the selection backward one character."),
104             D("cmd_charPrevious", "Move point backward one character."),
105             D("cmd_selectCharPrevious", "Extend selection backward one character."),
106             D("cmd_scrollLeft", "Scroll to the left."),
107             D("cmd_scrollLeft", "Scroll to the left."))),
108         R(D("cmd_deleteCharForward", "Delete the following character."),
109           D("cmd_deleteCharBackward", "Delete the previous character.")),
110         R(D("cmd_deleteWordForward", "Delete the following word."),
111           D("cmd_deleteWordBackward", "Delete the previous word.")),
112         R(S(D("forward-line", "Move or extend the selection forward one line."),
113             D("cmd_lineNext", "Move point forward one line."),
114             D("cmd_selectLineNext", "Extend selection forward one line."),
115             D("cmd_scrollLineDown", "Scroll down one line."),
116             D("cmd_scrollLineDown", "Scroll down one line.")),
117           S(D("backward-line", "Move or extend the selection backward one line."),
118             D("cmd_linePrevious", "Move point backward one line."),
119             D("cmd_selectLinePrevious", "Extend selection backward one line."),
120             D("cmd_scrollLineUp", "Scroll up one line."),
121             D("cmd_scrollLineUp", "Scroll up one line."))),
122         R(S(D("forward-page", "Move or extend the selection forward one page."),
123             D("cmd_movePageDown", "Move point forward one page."),
124             D("cmd_selectPageDown", "Extend selection forward one page."),
125             D("cmd_scrollPageDown", "Scroll forward one page."),
126             D("cmd_movePageDown", "Move point forward one page.")),
127           S(D("backward-page", "Move or extend the selection backward one page."),
128             D("cmd_movePageUp", "Move point backward one page."),
129             D("cmd_selectPageUp", "Extend selection backward one page."),
130             D("cmd_scrollPageUp", "Scroll backward one page."),
131             D("cmd_movePageUp", "Move point backward one page."))),
132         R(D("cmd_undo", "Undo last editing action."),
133           D("cmd_redo", "Redo last editing action.")),
134         R(S(D("forward-word", "Move or extend the selection forward one word."),
135             D("cmd_wordNext", "Move point forward one word."),
136             D("cmd_selectWordNext", "Extend selection forward one word."),
137             D("cmd_scrollRight", "Scroll to the right."),
138             D("cmd_wordNext", "Move point forward one word.")),
139           S(D("backward-word", "Move or extend the selection backward one word."),
140             D("cmd_wordPrevious", "Move point backward one word."),
141             D("cmd_selectWordPrevious", "Extend selection backward one word."),
142             D("cmd_scrollLeft", "Scroll to the left."),
143             D("cmd_wordPrevious", "Move point backward one word."))),
144         R(D("cmd_scrollPageUp", "Scroll up one page."),
145           D("cmd_scrollPageDown", "Scroll down one page.")),
146         R(D("cmd_scrollLineUp", "Scroll up one line."),
147           D("cmd_scrollLineDown", "Scroll down one line.")),
148         R(D("cmd_scrollLeft", "Scroll left."),
149           D("cmd_scrollRight", "Scroll right.")),
150         D("cmd_paste", "Insert the contents of the clipboard.")];
152     function get_mode_idx () {
153         if (mode == 'scroll') return 2;
154         else if (mode == 'caret') return 3;
155         else return 0;
156     }
158     function get_move_select_idx (I) {
159         return mark_active_predicate(I) ? 1 : get_mode_idx();
160     }
162     function doc_for_builtin (c) {
163         var s = "";
164         if (c.doc != null)
165             s += c.doc + "\n";
166         return s + "Run the built-in command " + c + ".";
167     }
169     function define_simple_command (c) {
170         interactive(prefix + c, doc_for_builtin(c), function (I) { do_command_function(I, c); });
171     }
173     function get_move_select_doc_string (c) {
174         return c.command.doc +
175             "\nSpecifically, if the mark is active, runs `" + prefix + c[1] + "'.  " +
176             "Otherwise, runs `" + prefix + c[get_mode_idx()] + "'\n" +
177             "To toggle whether the mark is active, use `" + prefix + "set-mark'.";
178     }
180     for each (let c_temp in builtin_commands) {
181         let c = c_temp;
182         if (c.is_move_select_pair) {
183             interactive(prefix + c.command, get_move_select_doc_string(c), function (I) {
184                 var idx = get_move_select_idx(I);
185                 do_command_function(I, c[idx]);
186             });
187             define_simple_command(c[0]);
188             define_simple_command(c[1]);
189         }
190         else
191             define_simple_command(c);
192     }
194     function get_reverse_pair_doc_string (main_doc, alt_command) {
195         return main_doc + "\n" +
196             "The prefix argument specifies a repeat count for this command.  " +
197             "If the count is negative, `" + prefix + alt_command + "' is performed instead with " +
198             "a corresponding positive repeat count.";
199     }
201     function define_simple_reverse_pair (a, b) {
202         interactive(prefix + a, get_reverse_pair_doc_string(doc_for_builtin(a), b),
203                     function (I) {
204                         do_repeatedly(do_command_function, I.p, [I, a], [I, b]);
205                     });
206         interactive(prefix + b, get_reverse_pair_doc_string(doc_for_builtin(b), a),
207                     function (I) {
208                         do_repeatedly(do_command_function, I.p, [I, b], [I, a]);
209                     });
210     }
212     for each (let c_temp in builtin_commands_with_count) {
213         let c = c_temp;
214         if (c.is_reverse_pair) {
215             if (c[0].is_move_select_pair) {
216                 interactive(prefix + c[0].command, get_reverse_pair_doc_string(get_move_select_doc_string(c[0]),
217                                                                                c[1].command),
218                             function (I) {
219                                 var idx = get_move_select_idx(I);
220                                 do_repeatedly(do_command_function, I.p, [I, c[0][idx]], [I, c[1][idx]]);
221                             });
222                 interactive(prefix + c[1].command, get_reverse_pair_doc_string(get_move_select_doc_string(c[1]),
223                                                                                c[0].command),
224                             function (I) {
225                                 var idx = get_move_select_idx(I);
226                                 do_repeatedly(do_command_function, I.p, [I, c[1][idx]], [I, c[0][idx]]);
227                             });
228                 define_simple_reverse_pair(c[0][0], c[1][0]);
229                 define_simple_reverse_pair(c[0][1], c[1][1]);
230             } else
231                 define_simple_reverse_pair(c[0], c[1]);
232         } else {
233             let doc = doc_for_builtin(c) +
234                 "\nThe prefix argument specifies a positive repeat count for this command.";
235             interactive(prefix + c, doc, function (I) {
236                 do_repeatedly_positive(do_command_function, I.p, I, c);
237             });
238         }
239     }
242 provide("builtin-commands");