Debian package: Support xulrunner 9+10 in debian/conkeror.bin, drop support for unver...
[conkeror.git] / modules / text.js
blob036d506c072deb787714ba1657342cbce1708d9e
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 /**
13  * Given a callback func and an interactive context I, call func, passing
14  * either a focused field, or the minibuffer's input element if the
15  * minibuffer is active. Afterward, call `scroll_selection_into_view' on
16  * the field. See `paste_x_primary_selection' and `open_line' for
17  * examples.
18  */
19 function call_on_focused_field (I, func, clear_mark) {
20     var m = I.window.minibuffer;
21     var s = m.current_state;
22     var e = null;
23     var frame;
24     if (m._input_mode_enabled) {
25         m._restore_normal_state();
26         e = m.input_element;
27     } else if ((e = I.buffer.focused_element)) {
28         if (e.getAttribute("contenteditable") == 'true')
29             e = e.ownerDocument;
30     } else if ((frame = I.buffer.focused_frame) &&
31                frame.document.designMode &&
32                frame.document.designMode == "on") {
33         e = frame.document;
34     }
35     func(e);
36     if (clear_mark) {
37         if (m._input_mode_enabled)
38             s.mark_active = false;
39         else
40             I.buffer.mark_active = false;
41     }
42     scroll_selection_into_view(e);
46 /**
47  * Replace the current region with modifier(selection). Deactivates region
48  * and sets point to the end of the inserted text.  The modifier can
49  * control the exact placement of point by returning an array
50  * [replacement, point_offset] instead of just a string.
51  */
52 function modify_region (field, modifier) {
53     if (field instanceof Ci.nsIDOMDocument) {
54         // richedit
55         var win = field.defaultView;
56         var sel = win.getSelection();
57         let replacement = modifier(win.getSelection().toString());
58         let point = null;
59         if (array_p(replacement)) {
60             point = replacement[1];
61             replacement = replacement[0];
62         } else
63             point = replacement.length;
64         field.execCommand("insertHTML", false,
65                         html_escape(replacement)
66                             .replace(/\n/g, '<br>')
67                             .replace(/  /g, ' &nbsp;'));
68         if (point != replacement.length) {
69             sel.extend(sel.focusNode, point);
70             sel.collapse(sel.focusNode, point);
71         }
72     } else {
73         // normal text field
74         let replacement =
75             modifier(field.value.substring(field.selectionStart, field.selectionEnd));
76         let point = field.selectionStart;
77         if (array_p(replacement)) {
78             point += replacement[1];
79             replacement = replacement[0];
80         } else
81             point += replacement.length;
82         field.value =
83             field.value.substr(0, field.selectionStart) + replacement +
84             field.value.substr(field.selectionEnd);
85         field.setSelectionRange(point, point);
86     }
90 /**
91  * Takes an interactive context and a function to call with the word
92  * at point as its sole argument, and which returns a modified word.
93  */
94 //XXX: this should be implemented in terms of modify_region,
95 //     in order to work in richedit fields.
96 function modify_word_at_point (field, func) {
97     // Skip any whitespaces at point and move point to the right place.
98     var point = field.selectionStart;
99     var rest = field.value.substring(point);
101     // Skip any whitespaces.
102     for (var i = 0, rlen = rest.length; i < rlen; i++) {
103         if (" \n".indexOf(rest.charAt(i)) == -1) {
104             point += i;
105             break;
106         }
107     }
109     // Find the next whitespace, as it is the end of the word.  If no next
110     // whitespace is found, we're at the end of input.  TODO: Add "\n" support.
111     var goal = field.value.indexOf(" ", point);
112     if (goal == -1)
113         goal = field.value.length;
115     // Change the value of the text field.
116     var input = field.value;
117     field.value =
118         input.substring(0, point) +
119         func(input.substring(point, goal)) +
120         input.substring(goal);
122     // Move point.
123     field.selectionStart = goal;
124     field.selectionEnd = goal;
127 provide("text");