2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2010 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
10 require("interactive.js");
12 interactive("set-mark",
13 "Toggle whether the mark is active.\n" +
14 "When the mark is active, movement commands affect the selection.",
17 var s = m.current_state;
19 s.mark_active = !s.mark_active;
21 I.buffer.mark_active = !I.buffer.mark_active;
24 function mark_active_predicate (I) {
26 var s = m.current_state;
30 return I.buffer.mark_active;
33 function define_builtin_commands (prefix, do_command_function, mode) {
35 // Specify a docstring
36 function D (cmd, docstring) {
37 var o = new String(cmd);
42 // Specify a forward/reverse pair
45 o.is_reverse_pair = true;
49 // Specify a movement/select/scroll/move-caret command group.
50 function S (command, movement, select, scroll, caret) {
51 var o = [movement, select, scroll, caret];
53 o.is_move_select_pair = true;
57 var builtin_commands = [
60 * cmd_scrollBeginLine and cmd_scrollEndLine don't do what I
61 * want, either in or out of caret mode...
63 S(D("beginning-of-line", "Move or extend the selection to the beginning of the current line."),
64 D("cmd_beginLine", "Move point to the beginning of the current line."),
65 D("cmd_selectBeginLine", "Extend selection to the beginning of the current line."),
66 D("cmd_beginLine", "Scroll to the beginning of the line"),
67 D("cmd_beginLine", "Scroll to the beginning of the line")),
68 S(D("end-of-line", "Move or extend the selection to the end of the current line."),
69 D("cmd_endLine", "Move point to the end of the current line."),
70 D("cmd_selectEndLine", "Extend selection to the end of the current line."),
71 D("cmd_endLine", "Scroll to the end of the current line."),
72 D("cmd_endLine", "Scroll to the end of the current line.")),
73 S(D("beginning-of-first-line", "Move or extend the selection to the beginning of the first line."),
74 D("cmd_moveTop", "Move point to the beginning of the first line."),
75 D("cmd_selectTop", "Extend selection to the beginning of the first line."),
76 D("cmd_scrollTop", "Scroll to the top of the buffer"),
77 D("cmd_scrollTop", "Move point to the beginning of the first line.")),
78 S(D("end-of-last-line", "Move or extend the selection to the end of the last line."),
79 D("cmd_moveBottom", "Move point to the end of the last line."),
80 D("cmd_selectBottom", "Extend selection to the end of the last line."),
81 D("cmd_scrollBottom", "Scroll to the bottom of the buffer"),
82 D("cmd_scrollBottom", "Move point to the end of the last line.")),
84 "cmd_scrollBeginLine",
87 D("cmd_copy", "Copy the selection into the clipboard."),
88 D("cmd_cut", "Cut the selection into the clipboard."),
89 D("cmd_deleteToBeginningOfLine", "Delete to the beginning of the current line."),
90 D("cmd_deleteToEndOfLine", "Delete to the end of the current line."),
91 D("cmd_selectAll", "Select all."),
92 D("cmd_scrollTop", "Scroll to the top of the buffer."),
93 D("cmd_scrollBottom", "Scroll to the bottom of the buffer.")];
95 var builtin_commands_with_count = [
96 R(S(D("forward-char", "Move or extend the selection forward one character."),
97 D("cmd_charNext", "Move point forward one character."),
98 D("cmd_selectCharNext", "Extend selection forward one character."),
99 D("cmd_scrollRight", "Scroll to the right"),
100 D("cmd_scrollRight", "Scroll to the right")),
101 S(D("backward-char", "Move or extend the selection backward one character."),
102 D("cmd_charPrevious", "Move point backward one character."),
103 D("cmd_selectCharPrevious", "Extend selection backward one character."),
104 D("cmd_scrollLeft", "Scroll to the left."),
105 D("cmd_scrollLeft", "Scroll to the left."))),
106 R(D("cmd_deleteCharForward", "Delete the following character."),
107 D("cmd_deleteCharBackward", "Delete the previous character.")),
108 R(D("cmd_deleteWordForward", "Delete the following word."),
109 D("cmd_deleteWordBackward", "Delete the previous word.")),
110 R(S(D("forward-line", "Move or extend the selection forward one line."),
111 D("cmd_lineNext", "Move point forward one line."),
112 D("cmd_selectLineNext", "Extend selection forward one line."),
113 D("cmd_scrollLineDown", "Scroll down one line."),
114 D("cmd_scrollLineDown", "Scroll down one line.")),
115 S(D("backward-line", "Move or extend the selection backward one line."),
116 D("cmd_linePrevious", "Move point backward one line."),
117 D("cmd_selectLinePrevious", "Extend selection backward one line."),
118 D("cmd_scrollLineUp", "Scroll up one line."),
119 D("cmd_scrollLineUp", "Scroll up one line."))),
120 R(S(D("forward-page", "Move or extend the selection forward one page."),
121 D("cmd_movePageDown", "Move point forward one page."),
122 D("cmd_selectPageDown", "Extend selection forward one page."),
123 D("cmd_scrollPageDown", "Scroll forward one page."),
124 D("cmd_movePageDown", "Move point forward one page.")),
125 S(D("backward-page", "Move or extend the selection backward one page."),
126 D("cmd_movePageUp", "Move point backward one page."),
127 D("cmd_selectPageUp", "Extend selection backward one page."),
128 D("cmd_scrollPageUp", "Scroll backward one page."),
129 D("cmd_movePageUp", "Move point backward one page."))),
130 R(D("cmd_undo", "Undo last editing action."),
131 D("cmd_redo", "Redo last editing action.")),
132 R(S(D("forward-word", "Move or extend the selection forward one word."),
133 D("cmd_wordNext", "Move point forward one word."),
134 D("cmd_selectWordNext", "Extend selection forward one word."),
135 D("cmd_scrollRight", "Scroll to the right."),
136 D("cmd_wordNext", "Move point forward one word.")),
137 S(D("backward-word", "Move or extend the selection backward one word."),
138 D("cmd_wordPrevious", "Move point backward one word."),
139 D("cmd_selectWordPrevious", "Extend selection backward one word."),
140 D("cmd_scrollLeft", "Scroll to the left."),
141 D("cmd_wordPrevious", "Move point backward one word."))),
142 R(D("cmd_scrollPageUp", "Scroll up one page."),
143 D("cmd_scrollPageDown", "Scroll down one page.")),
144 R(D("cmd_scrollLineUp", "Scroll up one line."),
145 D("cmd_scrollLineDown", "Scroll down one line.")),
146 R(D("cmd_scrollLeft", "Scroll left."),
147 D("cmd_scrollRight", "Scroll right.")),
148 D("cmd_paste", "Insert the contents of the clipboard.")];
150 function get_mode_idx () {
151 if (mode == 'scroll') return 2;
152 else if (mode == 'caret') return 3;
156 function get_move_select_idx (I) {
157 return mark_active_predicate(I) ? 1 : get_mode_idx();
160 function doc_for_builtin (c) {
164 return s + "Run the built-in command " + c + ".";
167 function define_simple_command (c) {
168 interactive(prefix + c, doc_for_builtin(c), function (I) { do_command_function(I, c); });
171 function get_move_select_doc_string (c) {
172 return c.command.doc +
173 "\nSpecifically, if the mark is active, runs `" + prefix + c[1] + "'. " +
174 "Otherwise, runs `" + prefix + c[get_mode_idx()] + "'\n" +
175 "To toggle whether the mark is active, use `" + prefix + "set-mark'.";
178 for each (let c_temp in builtin_commands) {
180 if (c.is_move_select_pair) {
181 interactive(prefix + c.command, get_move_select_doc_string(c), function (I) {
182 var idx = get_move_select_idx(I);
183 do_command_function(I, c[idx]);
185 define_simple_command(c[0]);
186 define_simple_command(c[1]);
189 define_simple_command(c);
192 function get_reverse_pair_doc_string (main_doc, alt_command) {
193 return main_doc + "\n" +
194 "The prefix argument specifies a repeat count for this command. " +
195 "If the count is negative, `" + prefix + alt_command + "' is performed instead with " +
196 "a corresponding positive repeat count.";
199 function define_simple_reverse_pair (a, b) {
200 interactive(prefix + a, get_reverse_pair_doc_string(doc_for_builtin(a), b),
202 do_repeatedly(do_command_function, I.p, [I, a], [I, b]);
204 interactive(prefix + b, get_reverse_pair_doc_string(doc_for_builtin(b), a),
206 do_repeatedly(do_command_function, I.p, [I, b], [I, a]);
210 for each (let c_temp in builtin_commands_with_count) {
212 if (c.is_reverse_pair) {
213 if (c[0].is_move_select_pair) {
214 interactive(prefix + c[0].command, get_reverse_pair_doc_string(get_move_select_doc_string(c[0]),
217 var idx = get_move_select_idx(I);
218 do_repeatedly(do_command_function, I.p, [I, c[0][idx]], [I, c[1][idx]]);
220 interactive(prefix + c[1].command, get_reverse_pair_doc_string(get_move_select_doc_string(c[1]),
223 var idx = get_move_select_idx(I);
224 do_repeatedly(do_command_function, I.p, [I, c[1][idx]], [I, c[0][idx]]);
226 define_simple_reverse_pair(c[0][0], c[1][0]);
227 define_simple_reverse_pair(c[0][1], c[1][1]);
229 define_simple_reverse_pair(c[0], c[1]);
231 let doc = doc_for_builtin(c) +
232 "\nThe prefix argument specifies a positive repeat count for this command.";
233 interactive(prefix + c, doc, function (I) {
234 do_repeatedly_positive(do_command_function, I.p, I, c);
240 provide("builtin-commands");