1 require("minibuffer.js");
4 function apply_completion_string(str, m) {
10 * Completions is either a visit function or an array.
12 define_keywords("$completions", "$get_string", "$get_description", "$get_value");
13 function all_word_completer()
16 var completions = arguments.$completions;
17 var get_string = arguments.$get_string;
18 var get_description = arguments.$get_description;
19 var get_value = arguments.$get_value;
21 if (typeof(completions) == "function")
24 completions(function (x) { arr.push(x); });
27 return function (input, pos, conservative) {
28 if (input.length == 0 && conservative)
30 var words = input.toLowerCase().split(" ");
31 var data = arr.filter(function (x) {
32 var s = get_string(x);
33 var d = get_description(x);
34 for (var i = 0; i < words.length; ++i)
36 if (s.toLowerCase().indexOf(words[i]) == -1 && d.toLowerCase().indexOf(words[i]) == -1)
41 return {count: data.length,
42 index_of: function (x) data.indexOf(x),
43 get_string: function (i) get_string(data[i]),
44 get_description : function (i) get_description(data[i]),
45 apply : function (i, m) apply_completion_string(get_string(data[i]), m),
46 get_value : function(i) (get_value ? get_value(data[i]) : data[i])
51 function get_common_prefix_length(a, b, len) {
53 if (len != null && len < a.length)
60 for (i = 0; i < lim && a[i] == b[i]; ++i);
64 function apply_partial_completion(x, prefix_end, suffix_begin, orig_str, m) {
65 if (suffix_begin < orig_str.length) {
66 if (orig_str[suffix_begin] == " ")
68 m._input_text = orig_str.substring(0, prefix_end) + x + " " + orig_str.substring(suffix_begin);
69 let sel = x.length + prefix_end + 1;
70 m._set_selection(sel, sel);
72 m._input_text = orig_str.substring(0, prefix_end) + x;
73 let sel = x.length + prefix_end;
74 m._set_selection(sel, sel);
78 function prefix_completer()
81 var completions = arguments.$completions;
82 var get_string = arguments.$get_string;
83 var get_description = arguments.$get_description;
84 var get_value = arguments.$get_value;
86 if (typeof(completions) == "function")
89 completions(function (x) { arr.push(x); });
91 arr = completions.slice();
92 arr.sort(function (a,b) {
101 return function (input, pos, conservative) {
102 var common_prefix = null;
103 if (pos == 0 && conservative)
105 var input_prefix = input.substring(0,pos);
106 var default_completion = null;
108 var data = arr.filter(function (x) {
109 var s = get_string(x);
113 default_completion = i;
116 retval = (s.length >= pos && s.substring(0,pos) == input_prefix);
123 let a = get_string(data[0]);
124 let b = get_string(data[data.length - 1]);
125 let i = get_common_prefix_length(a, b);
127 common_prefix = a.substring(0,i);
129 return {count:data.length,
130 index_of: function (x) data.indexOf(x),
131 get_string: function (i) get_string(data[i]),
132 get_description : function (i) get_description(data[i]),
133 apply : function (i, m) apply_partial_completion(get_string(data[i]), 0, pos, input, m),
134 get_value : function(i) get_value(data[i]),
135 apply_common_prefix: (common_prefix &&
136 function (m) apply_partial_completion(common_prefix, 0, pos, input, m)),
137 default_completion: default_completion
142 function javascript_completer(buffer) {
143 var window = buffer.window;
145 return function (input, pos, conservative) {
146 // Derived from Vimperator JavaScript completion
147 if (pos == 0 && conservative)
149 var str = input.substr(0, pos);
150 var matches = str.match(/^(.*?)(\s*\.\s*)?(\w*)$/);
151 var filter = matches[3] || "";
152 var start = matches[1].length - 1;
153 var offset = matches[1] ? matches[1].length : 0;
154 offset += matches[2] ? matches[2].length : 0;
158 let brackets = 0, parentheses = 0;
160 for (; start >= 0; start--)
162 switch (matches[1][start])
181 if (brackets > 0 || parentheses > 0)
189 var common_prefix_len = null;
190 var common_prefix = null;
192 function add_completion(str, desc) {
193 if (common_prefix != null)
194 common_prefix_len = get_common_prefix_length(common_prefix, str, common_prefix_len);
197 data.push([str,desc]);
199 if (matches[1].substr(start+1))
202 source_obj = eval(matches[1].substr(start+1));
207 source_obj = conkeror;
208 if ("window".substring(0,filter.length) == filter)
209 add_completion("window", "object");
210 if ("buffer".substring(0,filter.length) == filter)
211 add_completion("buffer", "object");
214 if (source_obj != null) {
216 for (let i in source_obj) {
217 if (i.substring(0,filter.length) != filter)
219 let type, description;
221 type = typeof(source_obj[i]);
222 } catch (e) { type = "unknown type"; }
223 if (type == "number" || type == "string" || type == "boolean") {
224 description = type + ": " + source_obj[i];
227 add_completion(i, description);
231 if (common_prefix != null && common_prefix_len > 0)
232 common_prefix = common_prefix.substr(0, common_prefix_len);
233 else if (common_prefix_len != null)
234 common_prefix = null;
235 return {count:data.length,
236 get_string: function (i) data[i][0],
237 get_description: function (i) data[i][1],
238 apply: function (i,m) apply_partial_completion(data[i][0], offset, pos, input, m),
239 apply_common_prefix: (common_prefix &&
240 function (m) apply_partial_completion(common_prefix, offset, pos, input, m))