Debian package: Add support for xulrunner 18
[conkeror.git] / modules / minibuffer-read-file.js
blob73ce3919e1a16636da4f591831564f4edc1a1c86
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  * (C) Copyright 2008 Nelson Elhage
4  *
5  * Use, modification, and distribution are subject to the terms specified in the
6  * COPYING file.
7 **/
9 require("io.js");
10 require("minibuffer-completion.js");
13 function file_path_completer () {
14     return function (input, pos, conservative) {
15         var f = Cc["@mozilla.org/file/local;1"]
16             .createInstance(Ci.nsILocalFile);
17         var ents = [];
18         var dir;
19         try {
20             f.initWithPath(input);
21             if(f.exists() && f.isDirectory())
22                 dir = f;
23             else
24                 dir = f.parent;
25             if (!dir.exists())
26                 return null;
27             var iter = dir.directoryEntries;
28             while (iter.hasMoreElements()) {
29                 var e = iter.getNext().QueryInterface(Ci.nsIFile);
30                 ents.push(e.path);
31             }
32         } catch(e) {
33             return null;
34         }
35         function id (x) { return x; };
36         return prefix_completer($completions = ents,
37                                 $get_string  = id,
38                                 $get_description = id,
39                                 $get_value = id)(input, pos, conservative);
40     };
44 /* keywords: $prompt, $initial_value, $history, $completer, $auto_complete */
45 minibuffer.prototype.read_file_path = function () {
46     keywords(arguments, $prompt = "File:", $initial_value = cwd.path,
47              $history = "file");
48     var result = yield this.read(
49         $prompt = arguments.$prompt,
50         $initial_value = arguments.$initial_value,
51         $history = arguments.$history,
52         $completer = file_path_completer(),
53         $auto_complete = true);
54     yield co_return(result);
57 minibuffer.prototype.read_file = function () {
58     var result = yield this.read_file_path(forward_keywords(arguments));
59     yield co_return(make_file(result));
62 // FIXME
63 minibuffer.prototype.read_existing_file = minibuffer.prototype.read_file;
64 minibuffer.prototype.read_directory_path = minibuffer.prototype.read_file_path;
65 minibuffer.prototype.read_existing_directory_path = minibuffer.prototype.read_directory_path;
67 minibuffer.prototype.read_file_check_overwrite = function () {
68     keywords(arguments);
69     var initial_value = arguments.$initial_value;
70     do {
71         var path = yield this.read_file_path(forward_keywords(arguments),
72                                              $initial_value = initial_value);
73         var file = make_file(path);
74         if (file.exists()) {
75             var overwrite = yield this.read_yes_or_no($prompt = "Overwrite existing file " + path + "?");
76             if (!overwrite) {
77                 initial_value = path;
78                 continue;
79             }
80         }
81         yield co_return(file);
82     } while (true);
85 provide("minibuffer-read-file");