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
11 * Given a callback func and an interactive context I, call func, passing
12 * either a focused field, or the minibuffer's input element if the
13 * minibuffer is active. Afterward, call `scroll_selection_into_view' on
14 * the field. See `paste_x_primary_selection' and `open_line' for
17 function call_on_focused_field (I, func, clear_mark) {
18 var m = I.window.minibuffer;
19 var s = m.current_state;
22 if (m._input_mode_enabled) {
23 m._restore_normal_state();
25 } else if ((e = I.buffer.focused_element)) {
26 if (e.getAttribute("contenteditable") == 'true')
28 } else if ((frame = I.buffer.focused_frame) &&
29 frame.document.designMode &&
30 frame.document.designMode == "on") {
35 if (m._input_mode_enabled)
36 s.mark_active = false;
38 I.buffer.mark_active = false;
40 scroll_selection_into_view(e);
45 * Replace the current region with modifier(selection). Deactivates region
46 * and sets point to the end of the inserted text. The modifier can
47 * control the exact placement of point by returning an array
48 * [replacement, point_offset] instead of just a string.
50 function modify_region (field, modifier) {
51 if (field instanceof Ci.nsIDOMDocument) {
53 var win = field.defaultView;
54 var sel = win.getSelection();
55 let replacement = modifier(win.getSelection().toString());
57 if (array_p(replacement)) {
58 point = replacement[1];
59 replacement = replacement[0];
61 point = replacement.length;
62 field.execCommand("insertHTML", false,
63 html_escape(replacement)
64 .replace(/\n/g, '<br>')
65 .replace(/ /g, ' '));
66 if (point != replacement.length) {
67 sel.extend(sel.focusNode, point);
68 sel.collapse(sel.focusNode, point);
73 modifier(field.value.substring(field.selectionStart, field.selectionEnd));
74 let point = field.selectionStart;
75 if (array_p(replacement)) {
76 point += replacement[1];
77 replacement = replacement[0];
79 point += replacement.length;
81 field.value.substr(0, field.selectionStart) + replacement +
82 field.value.substr(field.selectionEnd);
83 field.setSelectionRange(point, point);
89 * Takes an interactive context and a function to call with the word
90 * at point as its sole argument, and which returns a modified word.
92 //XXX: this should be implemented in terms of modify_region,
93 // in order to work in richedit fields.
94 function modify_word_at_point (field, func) {
95 // Skip any whitespaces at point and move point to the right place.
96 var point = field.selectionStart;
97 var rest = field.value.substring(point);
99 // Skip any whitespaces.
100 for (var i = 0, rlen = rest.length; i < rlen; i++) {
101 if (" \n".indexOf(rest.charAt(i)) == -1) {
107 // Find the next whitespace, as it is the end of the word. If no next
108 // whitespace is found, we're at the end of input. TODO: Add "\n" support.
109 var goal = field.value.indexOf(" ", point);
111 goal = field.value.length;
113 // Change the value of the text field.
114 var input = field.value;
116 input.substring(0, point) +
117 func(input.substring(point, goal)) +
118 input.substring(goal);
121 field.selectionStart = goal;
122 field.selectionEnd = goal;