keywords.js: make write_keywords public and more flexible
[conkeror.git] / modules / rc.js
blob2dac6f864e0730bc28c3f26c0ba0055e95e7dda5
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)
58     if (! path_s)
59     {
60         if (pref_has_user_value("conkeror.rcfile")) {
61             var rcfile = get_pref("conkeror.rcfile");
62             if (rcfile.length)
63                 path_s = rcfile;
64             else
65                 return;
66         } else {
67             var default_rc = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
68             default_rc.initWithPath(get_home_directory());
69             default_rc.appendRelativePath(".conkerorrc");
70             if (default_rc.exists())
71                 path_s = default_rc.path;
72             else
73                 return;
74         }
75     }
77     var file_o = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
78     file_o.initWithPath(path_s);
79     if (file_o.isDirectory()) {
80         load_rc_directory (file_o);
81     } else {
82         load_rc_file (path_s);
83     }
86 require_later("command-line.js");
88 call_after_load("command-line.js", function () {
89         command_line_param_handler("l", false, function (path) {
90                 try {
91                     load_rc (path);
92                 } catch (e) { dump_error(e);  }
93             });
94     });