whitespace
[conkeror.git] / modules / external-editor.js
blob9343c8b9b4a5adc1d97161b2d387e4b5944628b3
1 /**
2  * (C) Copyright 2008 Jeremy Maitin-Shepard
3  *
4  * Use, modification, and distribution are subject to the terms specified in the
5  * COPYING file.
6 **/
8 in_module(null);
10 define_variable("editor_shell_command", getenv("VISUAL") || getenv("EDITOR") || "emacs",
11     "Shell command used to invoke an external editor.\n" +
12     "This defaults to the value of the EDITOR environment variable.  If " +
13     "`run_external_editor_function' is non-null, it is used instead to " +
14     "invoke an external editor and the value of this variable is ignored." +
15     "It is used as part of a shell command in the following two ways:\n" +
16     "<editor_shell_command> <file>\n" +
17     "<editor_shell_command> +<line> <file>");
20 define_variable("run_external_editor_function", null,
21     "Coroutine function called to invoke an external editor.\n" +
22     "If this variable is set to a function, it is used to invoke "+
23     "an external editor in place of `editor_shell_command'.  It "+
24     "is called with the filename as the first argument, and "+
25     "optionally the boolean keyword argument $temporary specifying "+
26     "whether the file should be deleted after the editor is closed, "+
27     "and optionally the keyword argument $line specifying a line "+
28     "number to display.  The `create_external_editor_launcher' "+
29     "function may be convenient for generating a function suitable "+
30     "for use as the value of this variable.");
32 define_keyword("$temporary", "$line");
33 function open_file_with_external_editor (file) {
34     keywords(arguments);
36     if (run_external_editor_function) {
37         yield run_external_editor_function(file, forward_keywords(arguments));
38         return;
39     }
41     var line = arguments.$line;
43     var cmd = editor_shell_command + " ";
44     if (line != null)
45         cmd += "+" + line + " ";
46     cmd += "\"" + shell_quote(file.path) + "\"";
48     try {
49         yield shell_command(cmd);
50     } finally {
51         if (arguments.$temporary)  {
52             try {
53                 file.remove(false /* not recursive */);
54             } catch (e) {}
55         }
56     }
59 function create_external_editor_launcher (program, args) {
60     return function (file) {
61         keywords(arguments);
62         var arr = [null].concat(args.slice());
63         if (arguments.$line != null)
64             arr.push("+" + arguments.$line);
65         arr.push(file.path);
66         try {
67             yield spawn_and_wait_for_process(program, arr);
68         } finally {
69             if (arguments.$temporary) {
70                 try {
71                     file.remove(false);
72                 } catch (e) {}
73             }
74         }
75     };
78 function open_with_external_editor (lspec) {
79     keywords(arguments);
80     let [file, temp] = yield download_as_temporary(lspec);
81     yield open_file_with_external_editor(file, $line = arguments.$line, $temporary = temp);
84 provide("external-editor");