| 1 |
{- | Editor integration. -} |
| 2 |
|
| 3 |
module Hledger.UI.Editor ( |
| 4 |
-- TextPosition |
| 5 |
endPosition |
| 6 |
,runEditor |
| 7 |
,runIadd |
| 8 |
) |
| 9 |
where |
| 10 |
|
| 11 |
import Control.Applicative ((<|>)) |
| 12 |
import Data.List (intercalate) |
| 13 |
import Data.Maybe (catMaybes) |
| 14 |
import Data.Bifunctor (bimap) |
| 15 |
import Safe |
| 16 |
import System.Environment |
| 17 |
import System.Exit |
| 18 |
import System.FilePath |
| 19 |
import System.Info (os) |
| 20 |
import System.Process |
| 21 |
|
| 22 |
import Hledger |
| 23 |
|
| 24 |
-- | A position we can move to in a text editor: a line and optional column number. |
| 25 |
-- Line number 1 or 0 means the first line. A negative line number means the last line. |
| 26 |
type TextPosition = (Int, Maybe Int) |
| 27 |
|
| 28 |
-- | The text position meaning "last line, first column". |
| 29 |
endPosition :: Maybe TextPosition |
| 30 |
endPosition = Just (-1, Nothing) |
| 31 |
|
| 32 |
-- | Run the hledger-iadd executable on the given file, blocking until it exits, |
| 33 |
-- and return the exit code; or raise an error. |
| 34 |
-- hledger-iadd is an alternative to the built-in add command. |
| 35 |
runIadd :: FilePath -> IO ExitCode |
| 36 |
runIadd f = runCommand ("hledger-iadd -f " ++ f) >>= waitForProcess |
| 37 |
|
| 38 |
-- | Run the user's preferred text editor (or try a default editor), |
| 39 |
-- on the given file, blocking until it exits, and return the exit |
| 40 |
-- code; or raise an error. If a text position is provided, the editor |
| 41 |
-- will be focussed at that position in the file, if we know how. |
| 42 |
runEditor :: Maybe TextPosition -> FilePath -> IO ExitCode |
| 43 |
runEditor mpos f = editFileAtPositionCommand mpos f >>= runCommand >>= waitForProcess |
| 44 |
|
| 45 |
-- | Get a shell command line to open the user's preferred text editor |
| 46 |
-- (or a default editor) on the given file, and to focus it at the |
| 47 |
-- given text position if one is provided and if we know how. |
| 48 |
-- |
| 49 |
-- Just ('-' : _, _) is any text position with a negative line number. |
| 50 |
-- A text position with a negative line number means the last line. |
| 51 |
-- |
| 52 |
-- Some tests: |
| 53 |
-- @ |
| 54 |
-- EDITOR program: Maybe TextPosition Command should be: |
| 55 |
-- --------------- --------------------- ------------------------------------ |
| 56 |
-- emacs Just (line, Just col) emacs +LINE:COL FILE |
| 57 |
-- Just (line, Nothing) emacs +LINE FILE |
| 58 |
-- Just ('-' : _, _) emacs FILE -f end-of-buffer |
| 59 |
-- Nothing emacs FILE |
| 60 |
-- |
| 61 |
-- emacsclient Just (line, Just col) emacsclient +LINE:COL FILE |
| 62 |
-- Just (line, Nothing) emacsclient +LINE FILE |
| 63 |
-- Just ('-' : _, _) emacsclient FILE |
| 64 |
-- Nothing emacsclient FILE |
| 65 |
-- |
| 66 |
-- nano Just (line, Just col) nano +LINE:COL FILE |
| 67 |
-- Just (line, Nothing) nano +LINE FILE |
| 68 |
-- Just ('-' : _, _) nano FILE |
| 69 |
-- Nothing nano FILE |
| 70 |
-- |
| 71 |
-- vscode Just (line, Just col) vscode --goto FILE:LINE:COL |
| 72 |
-- Just (line, Nothing) vscode --goto FILE:LINE |
| 73 |
-- Just ('-' : _, _) vscode FILE |
| 74 |
-- Nothing vscode FILE |
| 75 |
-- |
| 76 |
-- kak Just (line, Just col) kak +LINE:COL FILE |
| 77 |
-- Just (line, Nothing) kak +LINE FILE |
| 78 |
-- Just ('-' : _, _) kak +: FILE |
| 79 |
-- Nothing kak FILE |
| 80 |
-- |
| 81 |
-- vi & variants Just (line, _) vi +LINE FILE |
| 82 |
-- Just ('-' : _, _) vi + FILE |
| 83 |
-- Nothing vi FILE |
| 84 |
-- |
| 85 |
-- (other PROG) _ PROG FILE |
| 86 |
-- |
| 87 |
-- (not set) Just (line, Just col) emacsclient -a '' -nw +LINE:COL FILE |
| 88 |
-- Just (line, Nothing) emacsclient -a '' -nw +LINE FILE |
| 89 |
-- Just ('-' : _, _) emacsclient -a '' -nw FILE |
| 90 |
-- Nothing emacsclient -a '' -nw FILE |
| 91 |
-- @ |
| 92 |
-- |
| 93 |
editFileAtPositionCommand :: Maybe TextPosition -> FilePath -> IO String |
| 94 |
editFileAtPositionCommand mpos f = do |
| 95 |
cmd <- getEditCommand |
| 96 |
let editor = lowercase $ takeBaseName $ headDef "" $ words' cmd |
| 97 |
f' = singleQuoteIfNeeded f |
| 98 |
mpos' = Just . bimap show (fmap show) =<< mpos |
| 99 |
join sep = intercalate sep . catMaybes |
| 100 |
args = case editor of |
| 101 |
"emacs" -> case mpos' of |
| 102 |
Nothing -> [f'] |
| 103 |
Just ('-' : _, _) -> [f', "-f", "end-of-buffer"] |
| 104 |
Just (l, mc) -> ['+' : join ":" [Just l, mc], f'] |
| 105 |
e | e `elem` ["emacsclient", "nano"] -> case mpos' of |
| 106 |
Nothing -> [f'] |
| 107 |
Just ('-' : _, _) -> [f'] |
| 108 |
Just (l, mc) -> ['+' : join ":" [Just l, mc], f'] |
| 109 |
"vscode" -> case mpos' of |
| 110 |
Nothing -> [f'] |
| 111 |
Just ('-' : _, _) -> [f'] |
| 112 |
Just (l, mc) -> ["--goto", join ":" [Just f', Just l, mc]] |
| 113 |
"kak" -> case mpos' of |
| 114 |
Nothing -> [f'] |
| 115 |
Just ('-' : _, _) -> ["+:", f'] |
| 116 |
Just (l, mc) -> ['+' : join ":" [Just l, mc], f'] |
| 117 |
e | e `elem` ["vi", "vim", "view", "nvim", "evim", "eview", |
| 118 |
"gvim", "gview", "rvim", "rview", |
| 119 |
"rgvim", "rgview", "ex"] -> case mpos' of |
| 120 |
Nothing -> [f'] |
| 121 |
Just ('-' : _, _) -> ["+", f'] |
| 122 |
Just (l, _) -> ['+' : l, f'] |
| 123 |
-- arch linux creates a symlink to /usr/lib/helix/hx at /usr/bin/helix |
| 124 |
e | e `elem` ["hx", "helix"] -> case mpos' of |
| 125 |
Nothing -> [f'] |
| 126 |
Just ('-' : _, _) -> [f'] |
| 127 |
Just (l, _) -> ['+' : l, f'] |
| 128 |
_ -> [f'] |
| 129 |
return $ unwords $ cmd:args |
| 130 |
|
| 131 |
-- | Get the user's preferred edit command. This is the value of the |
| 132 |
-- $HLEDGER_UI_EDITOR environment variable, or of $EDITOR, or an OS-specific default. |
| 133 |
-- |
| 134 |
-- For non-windows machines that would be "emacsclient -a '' -nw", |
| 135 |
-- which starts/connects to an emacs daemon in terminal mode. |
| 136 |
-- |
| 137 |
-- For windows the default is a plain "notepad.exe" |
| 138 |
getEditCommand :: IO String |
| 139 |
getEditCommand = do |
| 140 |
hledger_ui_editor_env <- lookupEnv "HLEDGER_UI_EDITOR" |
| 141 |
editor_env <- lookupEnv "EDITOR" |
| 142 |
let defaultEditor = Just $ if os == "mingw32" then "notepad.exe" else "emacsclient -a '' -nw" |
| 143 |
let Just cmd = hledger_ui_editor_env <|> editor_env <|> defaultEditor |
| 144 |
return cmd |
| 145 |
|