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
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.",
19 var s = m.current_state;
21 s.mark_active = !s.mark_active;
23 I.buffer.mark_active = !I.buffer.mark_active;
26 function mark_active_predicate (I) {
28 var s = m.current_state;
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);
44 // Specify a forward/reverse pair
47 o.is_reverse_pair = true;
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];
55 o.is_move_select_pair = true;
59 var builtin_commands = [
62 * cmd_scrollBeginLine and cmd_scrollEndLine don't do what I
63 * want, either in or out of caret mode...
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.")),
86 "cmd_scrollBeginLine",
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;
158 function get_move_select_idx (I) {
159 return mark_active_predicate(I) ? 1 : get_mode_idx();
162 function doc_for_builtin (c) {
166 return s + "Run the built-in command " + c + ".";
169 function define_simple_command (c) {
170 interactive(prefix + c, doc_for_builtin(c), function (I) { do_command_function(I, c); });
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'.";
180 for each (let c_temp in builtin_commands) {
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]);
187 define_simple_command(c[0]);
188 define_simple_command(c[1]);
191 define_simple_command(c);
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.";
201 function define_simple_reverse_pair (a, b) {
202 interactive(prefix + a, get_reverse_pair_doc_string(doc_for_builtin(a), b),
204 do_repeatedly(do_command_function, I.p, [I, a], [I, b]);
206 interactive(prefix + b, get_reverse_pair_doc_string(doc_for_builtin(b), a),
208 do_repeatedly(do_command_function, I.p, [I, b], [I, a]);
212 for each (let c_temp in builtin_commands_with_count) {
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]),
219 var idx = get_move_select_idx(I);
220 do_repeatedly(do_command_function, I.p, [I, c[0][idx]], [I, c[1][idx]]);
222 interactive(prefix + c[1].command, get_reverse_pair_doc_string(get_move_select_doc_string(c[1]),
225 var idx = get_move_select_idx(I);
226 do_repeatedly(do_command_function, I.p, [I, c[1][idx]], [I, c[0][idx]]);
228 define_simple_reverse_pair(c[0][0], c[1][0]);
229 define_simple_reverse_pair(c[0][1], c[1][1]);
231 define_simple_reverse_pair(c[0], c[1]);
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);
242 provide("builtin-commands");