Prepare new Debian package
[conkeror.git] / modules / rc.js
blob882d6e9d7d5fc23c68b0285de113910fc2345e45
1 /**
2  * (C) Copyright 2004-2007 Shawn Betts
3  * (C) Copyright 2007-2008 John J. Foerch
4  * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5  *
6  * Use, modification, and distribution are subject to the terms specified in the
7  * COPYING file.
8 **/
10 function load_rc_file (file) {
11     try {
12         var prefix = "file://";
13         var uri;
14         if (typeof file == "string")
15             uri = prefix + file;
16         else if (file instanceof Ci.nsIURI)
17             uri = file.spec;
18         else
19             uri = prefix + file.path;
21         subscript_loader.loadSubScript(uri, conkeror);
22     } catch (e) {
23         dump_error(e);
24     }
27 function load_rc_directory (file_o) {
28     var entries = file_o.directoryEntries;
29     var files = [];
30     while (entries.hasMoreElements()) {
31         var entry = entries.getNext();
32         entry.QueryInterface(Ci.nsIFile);
33         if (entry.leafName.match(/^[^.].*\.js$/i)) {
34             files.push(entry);
35         }
36     }
37     files.sort(function (a, b) {
38             if (a.leafName < b.leafName) {
39                 return -1;
40             } else if (a.leafName > b.leafName) {
41                 return 1;
42             } else {
43                 return 0;
44             }
45         });
46     for (var i = 0; i < files.length; i++) {
47         load_rc_file(files[i]);
48     }
53  * The file to load may be specified as an nsILocalFile, an nsIURI or
54  *   a string uri or file path or null.  If path specifies a directory
55  *   all `.js' files in that directory will be loaded.  The default,
56  *   for a null path, is specified by the preference `conkeror.rcfile'
57  *   if it is set, otherwise it is $HOME/.conkerorrc.  A uri that is
58  *   not of file or chrome scheme will fail.
59  */
60 function load_rc (path, resolve) {
61     var file;
63     if (typeof path == "object")
64         file = path;
65     if (typeof path == "string") {
66         try {
67             file = make_uri(path);
68         } catch (e) {
69             if (resolve)
70                 file = resolve(path);
71         }
72     }
73     if (file instanceof Ci.nsIURI && file.schemeIs("chrome"))
74         try {
75             file = make_file_from_chrome(file);
76         } catch (e) { /* ignore */ }
77     if (file instanceof Ci.nsIURI && file.schemeIs("file"))
78         file = make_file(file.path);
80     if (!path) {
81         if (pref_has_user_value("conkeror.rcfile")) {
82             var rcfile = get_pref("conkeror.rcfile");
83             if (rcfile.length)
84                 path = rcfile;
85             else
86                 return;
87         } else {
88             file = get_home_directory();
89             file.appendRelativePath(".conkerorrc");
90             if (!file.exists())
91                 return;
92         }
93     }
95     if (!file)
96         file = make_file(path);
98     if (file instanceof Ci.nsILocalFile) {
99         path = file.path;
100         if (!file.exists())
101             throw interactive_error("File not found: " + path);
102     }
103     if (file instanceof Ci.nsIURI)
104         path = file.spec;
106     if (file instanceof Ci.nsILocalFile && file.isDirectory()) {
107         load_rc_directory(file);
108     } else {
109         load_rc_file(file);
110     }
112     return path;