;ui: clarify editorOpenPositionCommand

This commit is contained in:
Simon Michael 2019-12-14 17:50:34 -08:00
parent a09513c3fb
commit 1bf271a8c4

View File

@ -67,28 +67,35 @@ identifyEditor cmd
-- editor command, with the appropriate options added, if we know how. -- editor command, with the appropriate options added, if we know how.
-- Currently we know how to do this for emacs and vi. -- Currently we know how to do this for emacs and vi.
-- --
-- @
-- Some tests: -- Some tests:
-- With: The command should be: -- When EDITOR is: The command should be:
-- EDITOR=notepad -> "notepad FILE" -- --------------- -----------------------------------
-- EDITOR=vi -> "vi +LINE FILE" -- notepad notepad FILE
-- EDITOR=vi, line -1 -> "vi + FILE" -- vi vi +LINE FILE
-- EDITOR=emacs -> "emacs +LINE:COL FILE" -- emacs emacs +LINE:COL FILE
-- EDITOR=emacs, line -1 -> "emacs FILE -f end-of-buffer" -- (unset) emacs -nw FILE -f end-of-buffer
-- EDITOR not set -> "emacs -nw FILE -f end-of-buffer" -- @
--
-- How to open editors at the last line of a file:
-- @
-- emacs: emacs FILE -f end-of-buffer
-- vi: vi + FILE
-- @
-- --
editorOpenPositionCommand :: Maybe TextPosition -> FilePath -> IO String editorOpenPositionCommand :: Maybe TextPosition -> FilePath -> IO String
editorOpenPositionCommand mpos f = do editorOpenPositionCommand mpos f = do
cmd <- editorCommand cmd <- editorCommand
let f' = singleQuoteIfNeeded f return $ cmd ++ " " ++
return $
case (identifyEditor cmd, mpos) of case (identifyEditor cmd, mpos) of
(EmacsClient, Just (l,mc)) | l >= 0 -> cmd ++ " " ++ emacsposopt l mc ++ " " ++ f' (EmacsClient , Just (l,mc)) | l >= 0 -> emacsposopt l mc ++ " " ++ f'
(EmacsClient, Just (l,mc)) | l < 0 -> cmd ++ " " ++ emacsposopt 999999999 mc ++ " " ++ f' (EmacsClient , Just (l,mc)) | l < 0 -> emacsposopt 999999999 mc ++ " " ++ f'
(Emacs, Just (l,mc)) | l >= 0 -> cmd ++ " " ++ emacsposopt l mc ++ " " ++ f' (Emacs , Just (l,mc)) | l >= 0 -> emacsposopt l mc ++ " " ++ f'
(Emacs, Just (l,_)) | l < 0 -> cmd ++ " " ++ f' ++ " -f end-of-buffer" (Emacs , Just (l,_)) | l < 0 -> f' ++ " -f end-of-buffer"
(Vi, Just (l,_)) -> cmd ++ " " ++ viposopt l ++ " " ++ f' (Vi , Just (l,_)) -> viposopt l ++ " " ++ f'
_ -> cmd ++ " " ++ f' _ -> f'
where where
f' = singleQuoteIfNeeded f
emacsposopt l mc = "+" ++ show l ++ maybe "" ((":"++).show) mc emacsposopt l mc = "+" ++ show l ++ maybe "" ((":"++).show) mc
viposopt l = "+" ++ if l >= 0 then show l else "" viposopt l = "+" ++ if l >= 0 then show l else ""