Add initial OpenSearch search engine support
[conkeror.git] / modules / external-editor.js
blob61b9aed2a89b2739a68c803d589892c2c6064b33
2 define_variable("editor_shell_command", getenv("EDITOR") || "emacs",
3                      "Shell command used to invoke an external editor.\n" +
4                      "This defaults to the value of the EDITOR environment variable.  If " +
5                      "`run_external_editor_function' is non-null, it is used instead to " +
6                      "invoke an external editor and the value of this variable is ignored." +
7                      "It is used as part of a shell command in the following two ways:\n" +
8                      "<editor_shell_command> <file>\n" +
9                      "<editor_shell_command> +<line> <file>");
12 define_variable("run_external_editor_function", null,
13                      "Coroutine function called to invoke an external editor.\n" +
14                      "If this variable is set to a function, it is used to invoke an external editor in place of `editor_shell_command'.  It is called with the filename as the first argument, and optionally the boolean keyword argument $temporary specifying whether the file should be deleted after the editor is closed, and optionally the keyword argument $line specifying a line number to display.  The `create_external_editor_launcher' function may be convenient for generating a function suitable for use as the value of this variable.");
16 define_keyword("$temporary", "$line");
17 function open_file_with_external_editor(file) {
18     keywords(arguments);
20     if (run_external_editor_function) {
21         yield run_external_editor_function(file, forward_keywords(arguments));
22         return;
23     }
25     var line = arguments.$line;
27     var cmd = editor_shell_command + " ";
28     if (line != null)
29         cmd += "+" + line + " ";
30     cmd += "\"" + shell_quote(file.path) + "\"";
32     try {
33         yield shell_command(cmd);
34     } finally {
35         if (arguments.$temporary)  {
36             try {
37                 file.remove(false /* not recursive */);
38             } catch (e) {}
39         }
40     }
43 function create_external_editor_launcher(program, args) {
44     return function (file) {
45         keywords(arguments);
46         var arr = [null].concat(args.slice());
47         if (arguments.$line != null)
48             arr.push("+" + arguments.$line);
49         arr.push(file.path);
50         try {
51             yield spawn_and_wait_for_process(program, arr);
52         } finally {
53             if (arguments.$temporary) {
54                 try {
55                     file.remove(false);
56                 } catch (e) {}
57             }
58         }
59     };
62 function open_with_external_editor(lspec) {
63     keywords(arguments);
64     let [file, temp] = yield download_as_temporary(lspec);
65     yield open_file_with_external_editor(file, $line = arguments.$line, $temporary = temp);