Skip to content

Commit

Permalink
emacs,vi: support "bleopt prompt_{emacs,vi}_mode_indicator"
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jul 12, 2022
1 parent 14f3c81 commit 2b905f8
Show file tree
Hide file tree
Showing 7 changed files with 236 additions and 98 deletions.
57 changes: 40 additions & 17 deletions blerc
Expand Up @@ -840,15 +840,25 @@
## Settings for Emacs mode

function blerc/emacs-load-hook {
## This option specifies the mode string shown in the information line in the
## multiline editing mode. When an empty string is specified, the mode
## string is not shown in the multiline editing mode.
#----------------------------------------------------------------------------
# Settings for the mode indicator

## The following option specifies the content of the mode indicator shown in
## the info line as a prompt sequence.

#bleopt prompt_emacs_mode_indicator='\q{keymap:emacs/mode-indicator}'


## The following option specifies the multiline mode name used in the prompt
## sequence \q{keymap:emacs/mode-indicator} in the multiline editing mode.

# default
#bleopt keymap_emacs_mode_string_multiline=$'\e[1m-- MULTILINE --\e[m'
# do not show mode string
# do not show the mode name
#bleopt keymap_emacs_mode_string_multiline=

#----------------------------------------------------------------------------
# Keybindings

## With the following setting, RET and C-m always causes the execution of the
## command even in the multiline mode or when the command is not
Expand All @@ -868,15 +878,25 @@ function blerc/vim-load-hook {
((_ble_bash>=40300)) && builtin bind 'set keyseq-timeout 1'

#----------------------------------------------------------------------------
# Settings for the mode name
# Settings for the mode indicator

## The following option specifies the content of the mode indicator shown in
## the info line as a prompt sequence.

#bleopt prompt_vi_mode_indicator='\q{keymap:vi/mode-indicator}'

## The option "keymap_vi_mode_show" controls if the mode line is shown. When
## this option has a non-empty value, the mode line is shown.

## The following option controls whether the prompt sequence
## \q{keymap:vi/mode-indicator} is activated. When this option has a
## non-empty value, \q{keymap:vi/mode-indicator} is expanded to the mode
## indicator. Otherwise, \q{keymap:vi/mode-indicator} is expanded to the
## empty string.

#bleopt keymap_vi_mode_show=1


## The following settings specify the name of modes in the mode line.
## The following options specify the name of modes in
## \q{keymap:vi/mode-indicator}.

#bleopt keymap_vi_mode_name_insert=INSERT
#bleopt keymap_vi_mode_name_replace=REPLACE
Expand All @@ -887,26 +907,29 @@ function blerc/vim-load-hook {
#bleopt keymap_vi_mode_name_blockwise=BLOCK


## This option specifies the string shown below the edited line in normal
## mode. For example, if you want to show an explicit name of the mode like
## in other modes, please use the following setting:
## This option specifies the result of \q{keymap:vi/mode-indicator} in the
## normal mode. For example, if you want to show an explicit name of the
## normal mode like in other modes, please use the following setting:

#bleopt keymap_vi_mode_string_nmap:=$'\e[1m~\e[m'
#bleopt keymap_vi_mode_string_nmap:=$'\e[1m-- NORMAL --\e[m'


## The setting "keymap_vi_mode_update_prompt" specifies that the prompt
## should be recalculated on the mode change. When this option has a
## non-empty value, the prompt will be recalculated.
## This option specifies that all the prompts should be recalculated on the
## mode change. When this option has a non-empty value, the prompt will be
## recalculated.

#bleopt keymap_vi_mode_update_prompt=

#----------------------------------------------------------------------------
# Keybindings


## The following setting sets up the keymap settings with Meta modifiers
## The following setting sets up the keymap settings with Meta modifiers.
## With this setting, M-RET can be used to insert a newline in the
## commandline. In this case, C-RET can be optionally configured so that it
## forcibly executes the command.

#ble-decode/keymap:vi_imap/define-meta-bindings
#ble-bind -m vi_imap -f 'C-RET' 'accept-line'


## The default mapping of C-o is vi_imap/single-command-mode. If you want to
Expand Down
2 changes: 1 addition & 1 deletion contrib
Submodule contrib updated 2 files
+2 −2 README-ja.md
+12 −12 README.md
11 changes: 6 additions & 5 deletions docs/ChangeLog.md
Expand Up @@ -103,6 +103,7 @@
- util: synchronize rlvars with `bleopt complete_{menu_color{,_match},skip_matched} term_bracketed_paste_mode` (motivated by ArianaAsl) `#D1819` 6d20f51
- util: suppress false warnings of `bind` inside non-interactive shells (reported by wukuan405) `#D1823` 1e19a67
- history: support `bleopt history_erasedups_limit` (motivated by SuperSandro2000) `#D1822` e4afb5a 3110967
- prompt: support `bleopt prompt_{emacs,vi}_mode_indicator` (motivated by ferdinandyb) `#D1843` XXXXXXX

## Changes

Expand Down Expand Up @@ -200,7 +201,7 @@
- global: fix declaration of associative arrays for `ble-reload` (reported by 0xC0ncord) `#D1471` 3cae6e4
- global: use a better workaround of bash-4.2 `declare -gA` by separating assignment `#D1567` 2408a20
- bind: work around broken `cmd_xmap` after switching the editing mode `#D1478` 8d354c1
- decode (`encoding:C`): fix initialization for isolated ESC `#D1839` XXXXXXX
- decode (`encoding:C`): fix initialization for isolated ESC `#D1839` c3bba5b
- edit: clear graphic rendition on newlines and external commands `#D1479` 18bb2d5
- decode (rlfunc): work around incomplete bytes in keyseq (reported by onelittlehope) `#D1483` 3559658 beb0383 37363be
- main: fix a bug that unset `IFS` is not correctly restored `#D1489` 808f6f7
Expand Down Expand Up @@ -268,8 +269,8 @@
- edit: fix an unexpected leave from the command layout on `read` `#D1800` 4dbf16f
- history: work around possible dirty prefix `*` in the history output `#D1808` 64a740d
- decode (`ble-bind`): fix the printed definition of `-c`/`-x` bindings `#D1821` 94de078
- command-help (`.read-man`): add missing `ble/util/assign/.rmtmp` `#D1840` XXXXXXX
- complete: fix wrong `COMP_POINT` with `progcomp_alias` `#D1841` XXXXXXX
- command-help (`.read-man`): add missing `ble/util/assign/.rmtmp` `#D1840` 937a164
- complete: fix wrong `COMP_POINT` with `progcomp_alias` `#D1841` 369f7c0

## Documentation

Expand Down Expand Up @@ -369,8 +370,8 @@
- history: work around broken timestamps in `HISTFILE` (reported by johnyaku) `#D1831` 5ef28eb
- progcomp: disable `command_not_found_handle` (reported by telometto, wisnoskij) `#D1834` 64d471a d5fe1d1 973ae8c
- complete: add completion integration with `zoxide` (reported by ferdinandyb) `#D1838` a96bafe
- util (`modifyOtherKeys`): work around delayed terminal identification `#D1842` XXXXXXX
- util (`modifyOtherKeys`): fix a bug that kitty protocol is never activated `#D1842` XXXXXXX
- util (`modifyOtherKeys`): work around delayed terminal identification `#D1842` 14f3c81
- util (`modifyOtherKeys`): fix a bug that kitty protocol is never activated `#D1842` 14f3c81

## Internal changes and fixes

Expand Down
87 changes: 64 additions & 23 deletions keymap/emacs.sh
Expand Up @@ -77,45 +77,86 @@ function ble/widget/emacs/revert {
# mode name の更新は基本的に __after_widget__ で行う。
# 但し、_ble_decode_{char,key}__hook 経由で実行されると、
# __after_widget__ は実行されないので、
# その様な編集コマンドについてだけは個別に update-mode-name を呼び出す。
# その様な編集コマンドについてだけは個別に update-mode-indicator を呼び出す。
#

## @var _ble_keymap_emacs_mode
## 複数行モードかどうか。
_ble_keymap_emacs_modeline=::
ble/array#push _ble_textarea_local_VARNAMES \
_ble_keymap_emacs_modeline
function ble/keymap:emacs/update-mode-name {
function ble/keymap:emacs/.get-emacs-keymap {
ble/prompt/unit/add-hash '$_ble_decode_keymap,${_ble_decode_keymap_stack[*]}'
local i=${#_ble_decode_keymap_stack[@]}
keymap=$_ble_decode_keymap
while [[ $keymap != vi_?map && $keymap != emacs ]]; do
((i--)) || return 1
keymap=${_ble_decode_keymap_stack[i]}
done
[[ $keymap == emacs ]]
}

bleopt/declare -v prompt_emacs_mode_indicator '\q{keymap:emacs/mode-indicator}'
function bleopt/check:prompt_emacs_mode_indicator {
local bleopt_prompt_emacs_mode_indicator=$value
[[ $_ble_attached ]] && ble/keymap:emacs/update-mode-indicator
return 0
}

_ble_keymap_emacs_mode_indicator_data=()
function ble/prompt/unit:_ble_keymap_emacs_mode_indicator/update {
local trace_opts=truncate:relative:noscrc:ansi
local prompt_rows=1
local prompt_cols=${COLUMNS:-80}
((prompt_cols&&prompt_cols--))
local "${_ble_prompt_cache_vars[@]/%/=}" # WA #D1570 checked
ble/prompt/unit:{section}/update _ble_keymap_emacs_mode_indicator "$bleopt_prompt_emacs_mode_indicator" "$trace_opts"
}

function ble/keymap:emacs/update-mode-indicator {
local keymap
ble/keymap:emacs/.get-emacs-keymap || return 0

# prefilter by _ble_edit_str/_ble_edit_arg/_ble_edit_kbdmacro_record
local opt_multiline=
[[ $bleopt_keymap_emacs_mode_string_multiline && $_ble_edit_str == *$'\n'* ]] && opt_multiline=1
[[ $_ble_edit_str == *$'\n'* ]] && opt_multiline=1
local footprint=$opt_multiline:$_ble_edit_arg:$_ble_edit_kbdmacro_record

[[ $footprint == "$_ble_keymap_emacs_modeline" ]] && return 0
_ble_keymap_emacs_modeline=$footprint

local name=
[[ $opt_multiline ]] && name=$bleopt_keymap_emacs_mode_string_multiline
# prompt_emacs_mode_indicator
local version=$COLUMNS,$_ble_edit_lineno,$_ble_history_count,$_ble_edit_CMD
local prompt_hashref_base='$version'
ble/prompt/unit#update _ble_keymap_emacs_mode_indicator
ble/prompt/unit:{section}/get _ble_keymap_emacs_mode_indicator; local str=$ret

local info=
[[ $_ble_edit_arg ]] &&
info=$info$' (arg: \e[1;34m'$_ble_edit_arg$'\e[m)'
str=${str:+"$str "}$'(arg: \e[1;34m'$_ble_edit_arg$'\e[m)'
[[ $_ble_edit_kbdmacro_record ]] &&
info=$info$' \e[1;31mREC\e[m'
if [[ ! $info && $opt_multiline ]]; then
str=${str:+"$str "}$'\e[1;31mREC\e[m'

ble/edit/info/default ansi "$str"
}
blehook PRECMD+=ble/keymap:emacs/update-mode-indicator

## @fn ble/prompt/backslash:keymap:emacs/mode-indicator
function ble/prompt/backslash:keymap:emacs/mode-indicator {
ble/prompt/unit/add-hash '$_ble_edit_str'
[[ $_ble_edit_str == *$'\n'* ]] || return 0

ble/prompt/unit/add-hash '$bleopt_keymap_emacs_mode_string_multiline'
local str=$bleopt_keymap_emacs_mode_string_multiline

# 他の付加情報がない時にだけ keybinding のヒントを出す
ble/prompt/unit/add-hash '${_ble_edit_arg:+1}${_ble_edit_kbdmacro_record:+1}'
if [[ ! ${_ble_edit_arg:+1}${_ble_edit_kbdmacro_record:+1} ]]; then
local keybinding_C_m=${_ble_decode_emacs_kmap_[_ble_decode_Ctrl|0x6d]}
local keybinding_C_j=${_ble_decode_emacs_kmap_[_ble_decode_Ctrl|0x6a]}
[[ $keybinding_C_m == *:ble/widget/accept-single-line-or-newline ]] &&
[[ $keybinding_C_j == *:ble/widget/accept-line ]] &&
info=$' (\e[35mRET\e[m or \e[35mC-m\e[m: insert a newline, \e[35mC-j\e[m: run)'
str=${str:+"$str "}$'(\e[35mRET\e[m or \e[35mC-m\e[m: insert a newline, \e[35mC-j\e[m: run)'
fi

[[ $name ]] || info=${info#' '}
name=$name$info
ble/edit/info/default ansi "$name"
[[ ! $str ]] || ble/prompt/print "$str"
}

function ble/widget/emacs/__after_widget__ {
ble/keymap:emacs/update-mode-name
ble/keymap:emacs/update-mode-indicator
}

# quoted-insert
Expand All @@ -126,7 +167,7 @@ function ble/widget/emacs/quoted-insert-char {
}
function ble/widget/emacs/quoted-insert-char.hook {
ble/widget/quoted-insert-char.hook
ble/keymap:emacs/update-mode-name
ble/keymap:emacs/update-mode-indicator
}
function ble/widget/emacs/quoted-insert {
_ble_edit_mark_active=
Expand All @@ -135,7 +176,7 @@ function ble/widget/emacs/quoted-insert {
}
function ble/widget/emacs/quoted-insert.hook {
ble/widget/quoted-insert.hook
ble/keymap:emacs/update-mode-name
ble/keymap:emacs/update-mode-indicator
}

function ble/widget/emacs/bracketed-paste {
Expand All @@ -145,7 +186,7 @@ function ble/widget/emacs/bracketed-paste {
}
function ble/widget/emacs/bracketed-paste.proc {
ble/widget/bracketed-paste.proc "$@"
ble/keymap:emacs/update-mode-name
ble/keymap:emacs/update-mode-indicator
}

#------------------------------------------------------------------------------
Expand Down

0 comments on commit 2b905f8

Please sign in to comment.