Commit 533037cc authored by niksaz's avatar niksaz Committed by Ben Gamari

Greater customization of GHCi prompt

This patch is trying to redesign the :set prompt option to take not a
String but a Haskell function, like [String] -> Int -> IO String, where
[String] is the list of the names of the currently loaded modules and
Int is the line number. Currently you may set prompt function with
**:set promt-function [String] -> Int -> IO String** option and old
version is also available - :set prompt String.

So, it looks like I've almost completed this patch:

1) Now we have a lot of escape sequences - 13 to be exact. Most of them
   are similar to bash prompt escape sequences. Thus they are quite handy.

2) We may use the special escape sequence to call shell functions, for
   example "%call(ls -l -a)".

3) We may use :set prompt-function to set PFunction to handle prompt.
   It is just [String] -> Int -> IO String.

Reviewers: erikd, austin, mpickering, bgamari

Reviewed By: mpickering, bgamari

Subscribers: mpickering, thomie

Differential Revision: https://phabricator.haskell.org/D2084

GHC Trac Issues: #5850
parent 18676a4a
......@@ -2469,17 +2469,51 @@ commonly used commands.
single: GHCi prompt; setting
Sets the string to be used as the prompt in GHCi. Inside ⟨prompt⟩,
the sequence ``%s`` is replaced by the names of the modules
currently in scope, ``%l`` is replaced by the line number (as
referenced in compiler messages) of the current prompt, and ``%%``
is replaced by ``%``. If ⟨prompt⟩ starts with ``"`` then it is parsed as
a Haskell String; otherwise it is treated as a literal string.
.. ghci-cmd:: :set prompt2; ⟨prompt⟩
the next sequences are replaced:
- ``%s`` by the names of the modules currently in scope.
- ``%l`` by the line number (as referenced in compiler messages) of the
current prompt.
- ``%d`` by the date in "Weekday Month Date" format (e.g., "Tue May 26") .
- ``%t`` by the current time in 24-hour HH:MM:SS format.
- ``%T`` by the current time in 12-hour HH:MM:SS format.
- ``%@`` by the current time in 12-hour am/pm format.
- ``%A`` by the current time in 24-hour HH:MM format.
- ``%u`` by the username of the current user.
- ``%w`` by the current working directory.
- ``%o`` by the operating system.
- ``%a`` by the machine architecture.
- ``%N`` by the compiler name.
- ``%V`` by the compiler version.
- ``%call(cmd [args])`` by the result of calling ``cmd args``.
- ``%%`` by ``%``.
If ⟨prompt⟩ starts with ``"`` then it is parsed as a Haskell String;
otherwise it is treated as a literal string.
.. ghci-cmd:: :set prompt-cont; ⟨prompt⟩
Sets the string to be used as the continuation prompt (used when
using the :ghci-cmd:`:{` command) in GHCi.
.. ghci-cmd:: :set prompt-function; <prompt-function>
.. index::
single: GHCi prompt function; setting
Sets the function to be used for the prompt displaying in GHCi. The
function should be of the type ``[String] -> Int -> IO String``. This
function is called each time the prompt is being made. The first argument
stands for the names of the modules currently in scope(the name of the
"topmost" module will begin with a ``*``; see :ref:`ghci-scope` for
more information). The second arguments is the line number (as referenced
in compiler messages) of the current prompt.
.. ghci-cmd:: :set prompt-cont-function; <prompt-function>
Sets the function to be used for the continuation prompt (used when
using the :ghci-cmd:`:{` command) displaying in GHCi.
.. ghci-cmd:: :set stop; ⟨num⟩ ⟨cmd⟩
Set a command to be executed when a breakpoint is hit, or a new item
......
This diff is collapsed.
......@@ -15,6 +15,7 @@ module GHCi.UI.Monad (
GHCiState(..), setGHCiState, getGHCiState, modifyGHCiState,
GHCiOption(..), isOptionSet, setOption, unsetOption,
Command(..),
PromptFunction,
BreakLocation(..),
TickArray,
getDynFlags,
......@@ -67,8 +68,8 @@ data GHCiState = GHCiState
progname :: String,
args :: [String],
evalWrapper :: ForeignHValue, -- ^ of type @IO a -> IO a@
prompt :: String,
prompt2 :: String,
prompt :: PromptFunction,
prompt_cont :: PromptFunction,
editor :: String,
stop :: String,
options :: [GHCiOption],
......@@ -137,6 +138,10 @@ data Command
-- ^ 'CompletionFunc' for arguments
}
type PromptFunction = [String]
-> Int
-> GHCi SDoc
data GHCiOption
= ShowTiming -- show time/allocs after evaluation
| ShowType -- show the type of expressions
......
......@@ -92,6 +92,9 @@ test('ghci056',
test('ghci057', extra_hc_opts('-fwarn-tabs'), ghci_script, ['ghci057.script'])
test('ghci060', normal, ghci_script, ['ghci060.script'])
test('ghci061', normal, ghci_script, ['ghci061.script'])
test('T2452', normal, ghci_script, ['T2452.script'])
test('T2766', normal, ghci_script, ['T2766.script'])
......
:set prompt "%call()> "
:set prompt "%call(pwd) %call()"
:set prompt "%callasfasfasfsaf(pwd)"
:set prompt "%call(pwd"
:set prompt "%a> "
:set prompt-cont "%call()| "
:set prompt-cont "%t| "
Incorrect %call syntax. Should be %call(a command and arguments).
Incorrect %call syntax. Should be %call(a command and arguments).
Incorrect %call syntax. Should be %call(a command and arguments).
Incorrect %call syntax. Should be %call(a command and arguments).
Incorrect %call syntax. Should be %call(a command and arguments).
two_args :: [String] -> IO String
two_args _ = return "two_args> "
three_args :: [String] -> Int -> IO String
three_args _ _ = return $ "three_args> "
:l ghci061.hs
:set prompt-function two_args
:set prompt-function three_args
:set prompt-cont-function two_args
:set prompt-cont-function three_args
<interactive>:1:2: error:
• Couldn't match type ‘IO String’ with ‘Int -> IO String’
Expected type: [String] -> Int -> IO String
Actual type: [String] -> IO String
• In the expression: (two_args) :: [String] -> Int -> IO String
In an equation for ‘_compileParsedExpr’:
_compileParsedExpr = (two_args) :: [String] -> Int -> IO String
<interactive>:1:2: error:
• Couldn't match type ‘IO String’ with ‘Int -> IO String’
Expected type: [String] -> Int -> IO String
Actual type: [String] -> IO String
• In the expression: (two_args) :: [String] -> Int -> IO String
In an equation for ‘_compileParsedExpr’:
_compileParsedExpr = (two_args) :: [String] -> Int -> IO String
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment