Fix zoom bindings to use characters rather than keycode names
[conkeror.git] / modules / minibuffer-read-file.js
bloba2ff419e99988ac3cbf49e55d1ead670073e07ca
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");
11 minibuffer.prototype.read_file_path = function () {
12     keywords(arguments, $prompt = "File:", $initial_value = default_directory.path,
13              $history = "file");
14     var result = yield this.read(
15         $prompt = arguments.$prompt,
16         $initial_value = arguments.$initial_value,
17         $history = arguments.$history,
18         $completer = file_path_completer(),
19         $auto_complete = true);
20     yield co_return(result);
23 minibuffer.prototype.read_file = function () {
24     var result = yield this.read_file_path(forward_keywords(arguments));
25     yield co_return(get_file(result));
28 // FIXME
29 minibuffer.prototype.read_existing_file = minibuffer.prototype.read_file;
32 minibuffer.prototype.read_file_check_overwrite = function () {
33     keywords(arguments);
34     var initial_value = arguments.$initial_value;
35     do {
36         var path = yield this.read_file_path(forward_keywords(arguments), $initial_value = initial_value);
38         var file = get_file(path);
40         if (file.exists()) {
41             var overwrite = yield this.read_yes_or_no($prompt = "Overwrite existing file " + path + "?");
42             if (!overwrite) {
43                 initial_value = path;
44                 continue;
45             }
46         }
47         yield co_return(file);
48     } while (true);
51 function file_path_completer() {
52     return function(input, pos, conservative) {
53         var f = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
54         var ents = [];
55         var dir;
56         try {
57             f.initWithPath(input);
58             if(f.exists() && f.isDirectory())
59                 dir = f;
60             else
61                 dir = f.parent;
62             if(!dir.exists()) return null;
63             var iter = dir.directoryEntries;
64             while(iter.hasMoreElements()) {
65                 var e = iter.getNext().QueryInterface(Ci.nsIFile);
66                 ents.push(e.path);
67             }
68         } catch(e) {
69             return null;
70         }
71         function id(x) { return x};
72         return prefix_completer($completions = ents,
73                                 $get_string  = id,
74                                 $get_description = id,
75                                 $get_value = id)(input, pos, conservative);
76     };