File: external-editor.js

package info (click to toggle)
conkeror 0.9~git080629-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 1,132 kB
  • ctags: 906
  • sloc: sh: 340; ansic: 246; xml: 105; makefile: 77
file content (72 lines) | stat: -rw-r--r-- 2,882 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * (C) Copyright 2008 Jeremy Maitin-Shepard
 *
 * Use, modification, and distribution are subject to the terms specified in the
 * COPYING file.
**/

define_variable("editor_shell_command", getenv("EDITOR") || "emacs",
                     "Shell command used to invoke an external editor.\n" +
                     "This defaults to the value of the EDITOR environment variable.  If " +
                     "`run_external_editor_function' is non-null, it is used instead to " +
                     "invoke an external editor and the value of this variable is ignored." +
                     "It is used as part of a shell command in the following two ways:\n" +
                     "<editor_shell_command> <file>\n" +
                     "<editor_shell_command> +<line> <file>");


define_variable("run_external_editor_function", null,
                     "Coroutine function called to invoke an external editor.\n" +
                     "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.");

define_keyword("$temporary", "$line");
function open_file_with_external_editor(file) {
    keywords(arguments);

    if (run_external_editor_function) {
        yield run_external_editor_function(file, forward_keywords(arguments));
        return;
    }

    var line = arguments.$line;

    var cmd = editor_shell_command + " ";
    if (line != null)
        cmd += "+" + line + " ";
    cmd += "\"" + shell_quote(file.path) + "\"";

    try {
        yield shell_command(cmd);
    } finally {
        if (arguments.$temporary)  {
            try {
                file.remove(false /* not recursive */);
            } catch (e) {}
        }
    }
}

function create_external_editor_launcher(program, args) {
    return function (file) {
        keywords(arguments);
        var arr = [null].concat(args.slice());
        if (arguments.$line != null)
            arr.push("+" + arguments.$line);
        arr.push(file.path);
        try {
            yield spawn_and_wait_for_process(program, arr);
        } finally {
            if (arguments.$temporary) {
                try {
                    file.remove(false);
                } catch (e) {}
            }
        }
    };
}

function open_with_external_editor(lspec) {
    keywords(arguments);
    let [file, temp] = yield download_as_temporary(lspec);
    yield open_file_with_external_editor(file, $line = arguments.$line, $temporary = temp);
}