Refreshed debian/patches/sensible-editor.diff
[conkeror.git] / modules / minibuffer-read-file.js
blobaa538ce4df7380cdd62bca5d8ffbcec13f90765e
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 /* keywords: $prompt, $initial_value, $history, $completer, $auto_complete */
12 minibuffer.prototype.read_file_path = function () {
13     keywords(arguments, $prompt = "File:", $initial_value = cwd.path,
14              $history = "file");
15     var result = yield this.read(
16         $prompt = arguments.$prompt,
17         $initial_value = arguments.$initial_value,
18         $history = arguments.$history,
19         $completer = file_path_completer(),
20         $auto_complete = true);
21     yield co_return(result);
24 minibuffer.prototype.read_file = function () {
25     var result = yield this.read_file_path(forward_keywords(arguments));
26     yield co_return(make_file(result));
29 // FIXME
30 minibuffer.prototype.read_existing_file = minibuffer.prototype.read_file;
31 minibuffer.prototype.read_directory_path = minibuffer.prototype.read_file_path;
32 minibuffer.prototype.read_existing_directory_path = minibuffer.prototype.read_directory_path;
34 minibuffer.prototype.read_file_check_overwrite = function () {
35     keywords(arguments);
36     var initial_value = arguments.$initial_value;
37     do {
38         var path = yield this.read_file_path(forward_keywords(arguments), $initial_value = initial_value);
40         var file = make_file(path);
42         if (file.exists()) {
43             var overwrite = yield this.read_yes_or_no($prompt = "Overwrite existing file " + path + "?");
44             if (!overwrite) {
45                 initial_value = path;
46                 continue;
47             }
48         }
49         yield co_return(file);
50     } while (true);
53 function file_path_completer() {
54     return function(input, pos, conservative) {
55         var f = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
56         var ents = [];
57         var dir;
58         try {
59             f.initWithPath(input);
60             if(f.exists() && f.isDirectory())
61                 dir = f;
62             else
63                 dir = f.parent;
64             if(!dir.exists()) return null;
65             var iter = dir.directoryEntries;
66             while(iter.hasMoreElements()) {
67                 var e = iter.getNext().QueryInterface(Ci.nsIFile);
68                 ents.push(e.path);
69             }
70         } catch(e) {
71             return null;
72         }
73         function id(x) { return x; };
74         return prefix_completer($completions = ents,
75                                 $get_string  = id,
76                                 $get_description = id,
77                                 $get_value = id)(input, pos, conservative);
78     };