rc.js: style
[conkeror.git] / modules / rc.js
blob1049bf5c856d4150d00a2e297901e420518b2b17
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 in_module(null);
12 function load_rc_file (file) {
13     try {
14         var prefix = "file://";
15         var uri;
16         if (typeof file == "string")
17             uri = prefix + file;
18         else if (file instanceof Ci.nsIURI)
19             uri = file.spec;
20         else
21             uri = prefix + file.path;
22         subscript_loader.loadSubScript(uri, conkeror);
23     } catch (e) {
24         dump_error(e);
25     }
28 function load_rc_directory (file_o) {
29     var entries = file_o.directoryEntries;
30     var files = [];
31     while (entries.hasMoreElements()) {
32         var entry = entries.getNext();
33         entry.QueryInterface(Ci.nsIFile);
34         if (entry.leafName.substr(-3).toLowerCase() == '.js')
35             files.push(entry);
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     for (var i = 0; files[i]; i++) {
46         load_rc_file(files[i]);
47     }
52  * The file to load may be specified as an nsILocalFile, an nsIURI or
53  *   a string uri or file path or null.  If path specifies a directory
54  *   all `.js' files in that directory will be loaded.  The default,
55  *   for a null path, is specified by the preference `conkeror.rcfile'
56  *   if it is set, otherwise it is $HOME/.conkerorrc.  A uri that is
57  *   not of file or chrome scheme will fail.
58  */
59 function load_rc (path, resolve) {
60     var file;
62     if (typeof path == "object")
63         file = path;
64     else if (typeof path == "string") {
65         try {
66             file = make_uri(path);
67         } catch (e) {
68             if (resolve)
69                 file = resolve(path);
70         }
71     }
72     if (file instanceof Ci.nsIURI && file.schemeIs("chrome"))
73         try {
74             file = make_file_from_chrome(file);
75         } catch (e) { /* ignore */ }
76     if (file instanceof Ci.nsIURI && file.schemeIs("file"))
77         file = make_file(file.path);
79     if (!path) {
80         if (pref_has_user_value("conkeror.rcfile")) {
81             var rcfile = get_pref("conkeror.rcfile");
82             if (rcfile.length)
83                 path = rcfile;
84             else
85                 return;
86         } else {
87             file = get_home_directory();
88             file.appendRelativePath(".conkerorrc");
89             if (!file.exists())
90                 return;
91         }
92     }
94     if (!file)
95         file = make_file(path);
97     if (file instanceof Ci.nsILocalFile) {
98         path = file.path;
99         if (!file.exists())
100             throw interactive_error("File not found: " + path);
101     }
102     if (file instanceof Ci.nsIURI)
103         path = file.spec;
105     if (file instanceof Ci.nsILocalFile && file.isDirectory())
106         load_rc_directory(file);
107     else
108         load_rc_file(file);
110     return path;
113 provide("rc");