keywords.js: make write_keywords public and more flexible
[conkeror.git] / modules / minibuffer-read-file.js
blob8dae22e06c1b716cb091fe17ea19f6655f548341
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;
30 minibuffer.prototype.read_directory_path = minibuffer.prototype.read_file_path;
31 minibuffer.prototype.read_existing_directory_path = minibuffer.prototype.read_directory_path;
33 minibuffer.prototype.read_file_check_overwrite = function () {
34     keywords(arguments);
35     var initial_value = arguments.$initial_value;
36     do {
37         var path = yield this.read_file_path(forward_keywords(arguments), $initial_value = initial_value);
39         var file = get_file(path);
41         if (file.exists()) {
42             var overwrite = yield this.read_yes_or_no($prompt = "Overwrite existing file " + path + "?");
43             if (!overwrite) {
44                 initial_value = path;
45                 continue;
46             }
47         }
48         yield co_return(file);
49     } while (true);
52 function file_path_completer() {
53     return function(input, pos, conservative) {
54         var f = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
55         var ents = [];
56         var dir;
57         try {
58             f.initWithPath(input);
59             if(f.exists() && f.isDirectory())
60                 dir = f;
61             else
62                 dir = f.parent;
63             if(!dir.exists()) return null;
64             var iter = dir.directoryEntries;
65             while(iter.hasMoreElements()) {
66                 var e = iter.getNext().QueryInterface(Ci.nsIFile);
67                 ents.push(e.path);
68             }
69         } catch(e) {
70             return null;
71         }
72         function id(x) { return x};
73         return prefix_completer($completions = ents,
74                                 $get_string  = id,
75                                 $get_description = id,
76                                 $get_value = id)(input, pos, conservative);
77     };