Further adapt debian webjumps as suggested by John J. Foerch
[conkeror.git] / modules / rc.js
blob4971063bbe7efc4a1dac96b4b8614d72c0c676d3
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;
23         subscript_loader.loadSubScript(uri, conkeror);
24     } catch (e) {
25         dump_error(e);
26     }
29 function load_rc_directory (file_o) {
30     var entries = file_o.directoryEntries;
31     var files = [];
32     while (entries.hasMoreElements()) {
33         var entry = entries.getNext();
34         entry.QueryInterface(Ci.nsIFile);
35         if (entry.leafName.match(/^[^.].*\.js$/i)) {
36             files.push(entry);
37         }
38     }
39     files.sort(function (a, b) {
40             if (a.leafName < b.leafName) {
41                 return -1;
42             } else if (a.leafName > b.leafName) {
43                 return 1;
44             } else {
45                 return 0;
46             }
47         });
48     for (var i = 0; i < files.length; i++) {
49         load_rc_file(files[i]);
50     }
55  * The file to load may be specified as an nsILocalFile, an nsIURI or
56  *   a string uri or file path or null.  If path specifies a directory
57  *   all `.js' files in that directory will be loaded.  The default,
58  *   for a null path, is specified by the preference `conkeror.rcfile'
59  *   if it is set, otherwise it is $HOME/.conkerorrc.  A uri that is
60  *   not of file or chrome scheme will fail.
61  */
62 function load_rc (path, resolve) {
63     var file;
65     if (typeof path == "object")
66         file = path;
67     if (typeof path == "string") {
68         try {
69             file = make_uri(path);
70         } catch (e) {
71             if (resolve)
72                 file = resolve(path);
73         }
74     }
75     if (file instanceof Ci.nsIURI && file.schemeIs("chrome"))
76         try {
77             file = make_file_from_chrome(file);
78         } catch (e) { /* ignore */ }
79     if (file instanceof Ci.nsIURI && file.schemeIs("file"))
80         file = make_file(file.path);
82     if (!path) {
83         if (pref_has_user_value("conkeror.rcfile")) {
84             var rcfile = get_pref("conkeror.rcfile");
85             if (rcfile.length)
86                 path = rcfile;
87             else
88                 return;
89         } else {
90             file = get_home_directory();
91             file.appendRelativePath(".conkerorrc");
92             if (!file.exists())
93                 return;
94         }
95     }
97     if (!file)
98         file = make_file(path);
100     if (file instanceof Ci.nsILocalFile) {
101         path = file.path;
102         if (!file.exists())
103             throw interactive_error("File not found: " + path);
104     }
105     if (file instanceof Ci.nsIURI)
106         path = file.spec;
108     if (file instanceof Ci.nsILocalFile && file.isDirectory()) {
109         load_rc_directory(file);
110     } else {
111         load_rc_file(file);
112     }
114     return path;
117 provide("rc");