|
2 | 2 |
|
3 | 3 | # editing files
|
4 | 4 |
|
5 |
| -function edit(file::AbstractString, line::Integer) |
| 5 | +doc""" |
| 6 | + editor() |
| 7 | +
|
| 8 | +Determines the editor to use when running functions like `edit`. Returns an Array compatible |
| 9 | +for use within backticks. You can change the editor by setting JULIA_EDITOR, VISUAL, or |
| 10 | +EDITOR as an environmental variable. |
| 11 | +""" |
| 12 | +function editor() |
6 | 13 | if OS_NAME == :Windows || OS_NAME == :Darwin
|
7 | 14 | default_editor = "open"
|
8 | 15 | elseif isreadable("/etc/alternatives/editor")
|
9 | 16 | default_editor = "/etc/alternatives/editor"
|
10 | 17 | else
|
11 | 18 | default_editor = "emacs"
|
12 | 19 | end
|
13 |
| - editor = get(ENV,"JULIA_EDITOR", get(ENV,"VISUAL", get(ENV,"EDITOR", default_editor))) |
14 |
| - if ispath(editor) |
15 |
| - if isreadable(editor) |
16 |
| - edpath = realpath(editor) |
17 |
| - edname = basename(edpath) |
18 |
| - else |
19 |
| - error("can't find \"$editor\"") |
20 |
| - end |
21 |
| - else |
22 |
| - edpath = edname = editor |
23 |
| - end |
| 20 | + # Note: the editor path can include spaces (if escaped) and flags. |
| 21 | + command = shell_split(get(ENV,"JULIA_EDITOR", get(ENV,"VISUAL", get(ENV,"EDITOR", default_editor)))) |
| 22 | + isempty(command) && error("editor is empty") |
| 23 | + return command |
| 24 | +end |
| 25 | + |
| 26 | +function edit(file::AbstractString, line::Integer) |
| 27 | + command = editor() |
| 28 | + name = basename(first(command)) |
24 | 29 | issrc = length(file)>2 && file[end-2:end] == ".jl"
|
25 | 30 | if issrc
|
26 | 31 | f = find_source_file(file)
|
27 | 32 | f !== nothing && (file = f)
|
28 | 33 | end
|
29 | 34 | const no_line_msg = "Unknown editor: no line number information passed.\nThe method is defined at line $line."
|
30 |
| - if startswith(edname, "emacs") || edname == "gedit" |
31 |
| - spawn(`$edpath +$line $file`) |
32 |
| - elseif edname == "vi" || edname == "vim" || edname == "nvim" || edname == "mvim" || edname == "nano" |
33 |
| - run(`$edpath +$line $file`) |
34 |
| - elseif edname == "textmate" || edname == "mate" || edname == "kate" |
35 |
| - spawn(`$edpath $file -l $line`) |
36 |
| - elseif startswith(edname, "subl") || edname == "atom" |
37 |
| - spawn(`$(shell_split(edpath)) $file:$line`) |
38 |
| - elseif OS_NAME == :Windows && (edname == "start" || edname == "open") |
| 35 | + if startswith(name, "emacs") || name == "gedit" |
| 36 | + spawn(`$command +$line $file`) |
| 37 | + elseif name == "vi" || name == "vim" || name == "nvim" || name == "mvim" || name == "nano" |
| 38 | + run(`$command +$line $file`) |
| 39 | + elseif name == "textmate" || name == "mate" || name == "kate" |
| 40 | + spawn(`$command $file -l $line`) |
| 41 | + elseif startswith(name, "subl") || name == "atom" |
| 42 | + spawn(`$command $file:$line`) |
| 43 | + elseif OS_NAME == :Windows && (name == "start" || name == "open") |
39 | 44 | spawn(`cmd /c start /b $file`)
|
40 | 45 | println(no_line_msg)
|
41 |
| - elseif OS_NAME == :Darwin && (edname == "start" || edname == "open") |
| 46 | + elseif OS_NAME == :Darwin && (name == "start" || name == "open") |
42 | 47 | spawn(`open -t $file`)
|
43 | 48 | println(no_line_msg)
|
44 | 49 | else
|
45 |
| - run(`$(shell_split(edpath)) $file`) |
| 50 | + run(`$command $file`) |
46 | 51 | println(no_line_msg)
|
47 | 52 | end
|
48 | 53 | nothing
|
|
0 commit comments