Add initial OpenSearch search engine support
[conkeror.git] / modules / minibuffer-read-file.js
blob0742161a1c4c503caf0fa285d3ceb7bc2c724b58
1 require("io.js");
3 minibuffer.prototype.read_file_path = function () {
4     keywords(arguments, $prompt = "File:", $initial_value = default_directory.path,
5              $history = "file");
6     var result = yield this.read(
7         $prompt = arguments.$prompt,
8         $initial_value = arguments.$initial_value,
9         $history = arguments.$history,
10         $completer = file_path_completer(),
11         $auto_complete = true);
12     yield co_return(result);
15 minibuffer.prototype.read_file = function () {
16     var result = yield this.read_file_path(forward_keywords(arguments));
17     yield co_return(get_file(result));
20 // FIXME
21 minibuffer.prototype.read_existing_file = minibuffer.prototype.read_file;
24 minibuffer.prototype.read_file_check_overwrite = function () {
25     keywords(arguments);
26     var initial_value = arguments.$initial_value;
27     do {
28         var path = yield this.read_file_path(forward_keywords(arguments), $initial_value = initial_value);
30         var file = get_file(path);
32         if (file.exists()) {
33             var overwrite = yield this.read_yes_or_no($prompt = "Overwrite existing file " + path + "?");
34             if (!overwrite) {
35                 initial_value = path;
36                 continue;
37             }
38         }
39         yield co_return(file);
40     } while (true);
43 function file_path_completer() {
44     return function(input, pos, conservative) {
45         var f = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
46         var ents = [];
47         var dir;
48         try {
49             f.initWithPath(input);
50             if(f.exists() && f.isDirectory())
51                 dir = f;
52             else
53                 dir = f.parent;
54             if(!dir.exists()) return null;
55             var iter = dir.directoryEntries;
56             while(iter.hasMoreElements()) {
57                 var e = iter.getNext();
58                 ents.push(e.path);
59             }
60         } catch(e) {
61             return null;
62         }
63         function id(x) { return x};
64         return prefix_completer($completions = ents,
65                                 $get_string  = id,
66                                 $get_description = id,
67                                 $get_value = id)(input, pos, conservative);
68     };