Support Debian's upcoming xulrunner-1.9.1 packages
[conkeror.git] / modules / rc.js
blob3b6df6a98dce5bdfb5894a1a5e015a80e094d522
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)
12     try {
13         var name;
14         if (typeof file == "string")
15             name = file;
16         else
17             name = file.path;
19         subscript_loader.loadSubScript("file://" + name, conkeror);
20     } catch (e) {
21         dump_error(e);
22     }
25 function load_rc_directory (file_o) {
26     var entries = file_o.directoryEntries;
27     var files = [];
28     while (entries.hasMoreElements ()) {
29         var entry = entries.getNext ();
30         entry.QueryInterface (Ci.nsIFile);
31         if (entry.leafName.match(/^[^.].*\.js$/i)) {
32             files.push(entry);
33         }
34     }
35     files.sort(function (a, b) {
36             if (a.leafName < b.leafName) {
37                 return -1;
38             } else if (a.leafName > b.leafName) {
39                 return 1;
40             } else {
41                 return 0;
42             }
43         });
44     for (var i = 0; i < files.length; i++) {
45         load_rc_file(files[i]);
46     }
51  * path_s: string path to load.  may be a file, a directory, or null.
52  *   if it is a file, that file will be loaded.  if it is a directory,
53  *   all `.js' files in that directory will be loaded.  if it is null,
54  *   the preference `conkeror.rcfile' will be read for the default.
55  */
56 function load_rc (path_s) {
57     if (! path_s) {
58         if (pref_has_user_value("conkeror.rcfile")) {
59             var rcfile = get_pref("conkeror.rcfile");
60             if (rcfile.length)
61                 path_s = rcfile;
62             else
63                 return;
64         } else {
65             var default_rc = get_home_directory();
66             default_rc.appendRelativePath(".conkerorrc");
67             if (default_rc.exists())
68                 path_s = default_rc.path;
69             else
70                 return;
71         }
72     }
74     var file_o = Cc["@mozilla.org/file/local;1"]
75         .createInstance(Ci.nsILocalFile);
76     file_o.initWithPath(path_s);
77     if (file_o.isDirectory()) {
78         load_rc_directory(file_o);
79     } else {
80         load_rc_file(path_s);
81     }
82     return file_o.path;