Skip to content

Commit

Permalink
prompt: track dependencies and detect changes
Browse files Browse the repository at this point in the history
  • Loading branch information
akinomyoga committed Jun 10, 2021
1 parent 73b037f commit cf8d949
Show file tree
Hide file tree
Showing 10 changed files with 1,292 additions and 523 deletions.
4 changes: 2 additions & 2 deletions ble.pp
Expand Up @@ -340,7 +340,7 @@ function ble/base/adjust-bash-options {
fi
ble/variable#copy-state LC_COLLATE _ble_bash_LC_COLLATE
LC_COLLATE=C
} 2>/dev/null # set -x 対策 #D0930
} 2>/dev/null # set -x 対策 #D0930 / locale 変更
function ble/base/restore-bash-options {
[[ $_ble_bash_options_adjusted ]] || return 1
_ble_bash_options_adjusted=
Expand All @@ -361,7 +361,7 @@ function ble/base/restore-bash-options {
[[ $_ble_bash_setv && ! -o verbose ]] && set -v
[[ $_ble_bash_setx && ! -o xtrace ]] && set -x
[[ $_ble_bash_sete && ! -o errexit ]] && set -e # set -e は最後
} 2>/dev/null # set -x 対策 #D0930
} 2>/dev/null # set -x 対策 #D0930 / locale 変更
function ble/base/recover-bash-options {
# bind -x が終わる度に設定が復元されてしまうので毎回設定し直す #D1526 #D1574
if [[ $_ble_bash_expand_aliases ]]; then
Expand Down
2 changes: 1 addition & 1 deletion contrib
33 changes: 17 additions & 16 deletions keymap/vi.sh
Expand Up @@ -384,6 +384,7 @@ function bleopt/check:keymap_vi_mode_name_blockwise { ble/keymap:vi/update-mode-
## 現在の vi キーマップ名 (vi_?map) を取得します。
## もし現在 vi キーマップにない場合には失敗します。
function ble/keymap:vi/script/get-vi-keymap {
ble/prompt/unit/add-hash '$_ble_decode_keymap,${_ble_decode_keymap_stack[*]}'
local i=${#_ble_decode_keymap_stack[@]}

keymap=$_ble_decode_keymap
Expand All @@ -397,6 +398,9 @@ function ble/keymap:vi/script/get-vi-keymap {
## @fn ble/keymap:vi/script/get-mode
## @var[out] mode
function ble/keymap:vi/script/get-mode {
ble/prompt/unit/add-hash '$_ble_decode_keymap,${_ble_decode_keymap_stack[*]}'
ble/prompt/unit/add-hash '$_ble_keymap_vi_single_command,$_ble_edit_mark_active'

mode=

local keymap; ble/keymap:vi/script/get-vi-keymap
Expand Down Expand Up @@ -464,9 +468,7 @@ function ble/keymap:vi/update-mode-name {
return 0
fi

if [[ $bleopt_keymap_vi_mode_update_prompt ]] || ble/util/test-rl-variable show-mode-in-prompt; then
ble/prompt/clear
fi
[[ $bleopt_keymap_vi_mode_update_prompt ]] && ble/prompt/clear

local name=
if [[ $bleopt_keymap_vi_mode_show ]]; then
Expand Down Expand Up @@ -1939,7 +1941,7 @@ _ble_keymap_vi_filter_repeat=()
_ble_keymap_vi_filter_history=()
_ble_keymap_vi_filter_history_edit=()
_ble_keymap_vi_filter_history_dirt=()
_ble_keymap_vi_filter_history_ind=0
_ble_keymap_vi_filter_history_index=0

function ble/highlight/layer:region/mark:vi_filter/get-face {
face=region_target
Expand Down Expand Up @@ -1974,7 +1976,7 @@ function ble/keymap:vi/operator:filter {
_ble_edit_mark_active=vi_filter
ble/keymap:vi/async-commandline-mode 'ble/keymap:vi/operator:filter/.hook'
_ble_edit_PS1='!'
_ble_history_prefix=_ble_keymap_vi_filter
ble/history/set-prefix _ble_keymap_vi_filter
_ble_keymap_vi_cmap_before_command=ble/keymap:vi/commandline/before-command.hook
_ble_keymap_vi_cmap_cancel_hook=ble/keymap:vi/operator:filter/cancel.hook
_ble_syntax_lang=bash
Expand Down Expand Up @@ -2379,7 +2381,7 @@ function ble/keymap:vi/mark/history-onleave.hook {
fi
}

# 履歴がロードされていない時は取り敢えず _ble_history_ind=0 で登録をしておく。
# 履歴がロードされていない時は取り敢えず _ble_history_index=0 で登録をしておく。
# 履歴がロードされた後の初めての利用のときに正しい履歴番号に修正する。
function ble/keymap:vi/mark/update-mark-history {
local h; ble/history/get-index -v h
Expand Down Expand Up @@ -5040,7 +5042,7 @@ function ble/widget/vi-command/text-object {
_ble_keymap_vi_commandline_history=()
_ble_keymap_vi_commandline_history_edit=()
_ble_keymap_vi_commandline_history_dirt=()
_ble_keymap_vi_commandline_history_ind=0
_ble_keymap_vi_commandline_history_index=0

## @arr _ble_keymap_vi_cmap_is_cancel_key
## コマンドラインが空の時にキャンセルに使うキーの辞書です。
Expand All @@ -5059,7 +5061,7 @@ function ble/widget/vi-command/commandline {
ble/keymap:vi/clear-arg
ble/keymap:vi/async-commandline-mode ble/widget/vi-command/commandline.hook
_ble_edit_PS1=:
_ble_history_prefix=_ble_keymap_vi_commandline
ble/history/set-prefix _ble_keymap_vi_commandline
_ble_keymap_vi_cmap_before_command=ble/keymap:vi/commandline/before-command.hook
return 147
}
Expand Down Expand Up @@ -5121,7 +5123,7 @@ _ble_keymap_vi_search_matched=
_ble_keymap_vi_search_history=()
_ble_keymap_vi_search_history_edit=()
_ble_keymap_vi_search_history_dirt=()
_ble_keymap_vi_search_history_ind=0
_ble_keymap_vi_search_history_index=0

## @bleopt keymap_vi_search_match_current
## 非空の文字列が設定されている時 /, ?, n, N で
Expand Down Expand Up @@ -5388,14 +5390,14 @@ function ble/widget/vi-command/search.impl {
function ble/widget/vi-command/search-forward {
ble/keymap:vi/async-commandline-mode 'ble/widget/vi-command/search.impl +:history'
_ble_edit_PS1='/'
_ble_history_prefix=_ble_keymap_vi_search
ble/history/set-prefix _ble_keymap_vi_search
_ble_keymap_vi_cmap_before_command=ble/keymap:vi/commandline/before-command.hook
return 147
}
function ble/widget/vi-command/search-backward {
ble/keymap:vi/async-commandline-mode 'ble/widget/vi-command/search.impl -:history'
_ble_edit_PS1='?'
_ble_history_prefix=_ble_keymap_vi_search
ble/history/set-prefix _ble_keymap_vi_search
_ble_keymap_vi_cmap_before_command=ble/keymap:vi/commandline/before-command.hook
return 147
}
Expand Down Expand Up @@ -7646,7 +7648,6 @@ function ble-decode/keymap:vi_smap/define {
# vi_imap

function ble/widget/vi_imap/__attach__ {
ble/prompt/notify-readline-mode-change
ble/keymap:vi/update-mode-name
return 0
}
Expand Down Expand Up @@ -7989,7 +7990,7 @@ _ble_keymap_vi_cmap_before_command=
_ble_keymap_vi_cmap_history=()
_ble_keymap_vi_cmap_history_edit=()
_ble_keymap_vi_cmap_history_dirt=()
_ble_keymap_vi_cmap_history_ind=0
_ble_keymap_vi_cmap_history_index=0

function ble/keymap:vi/async-commandline-mode {
local hook=$1
Expand All @@ -8014,7 +8015,7 @@ function ble/keymap:vi/async-commandline-mode {

# edit/prompt
_ble_edit_PS1=$PS2
_ble_edit_prompt=("" 0 0 0 32 0 "" "")
_ble_prompt_ps1_data=("" 0 0 0 32 0 "" "")

# edit
# Note: ble/widget/.newline/clear-content の中で
Expand All @@ -8029,7 +8030,7 @@ function ble/keymap:vi/async-commandline-mode {
ble-edit/undo/clear-all

# edit/history
_ble_history_prefix=_ble_keymap_vi_cmap
ble/history/set-prefix _ble_keymap_vi_cmap

# syntax, highlight
_ble_syntax_lang=text
Expand All @@ -8053,7 +8054,7 @@ function ble/widget/vi_cmap/accept {
ble/textarea#clear-state _ble_keymap_vi_cmap
ble/util/restore-vars _ble_keymap_vi_cmap _ble_canvas_panel_focus
[[ $_ble_edit_overwrite_mode ]] && ble/util/buffer "$_ble_term_civis"
_ble_history_prefix=$_ble_keymap_vi_cmap_history_prefix
ble/history/set-prefix "$_ble_keymap_vi_cmap_history_prefix"

ble/decode/keymap/pop
ble/keymap:vi/update-mode-name
Expand Down
112 changes: 46 additions & 66 deletions lib/vim-airline.sh
Expand Up @@ -32,20 +32,12 @@ function bleopt/check:vim_airline_theme {
return 0
}

function ble/lib/vim-airline/invalidate { ble/prompt/clear; }

bleopt/declare -v vim_airline_section_a '\e[1m\q{lib/vim-airline/mode}'
bleopt/declare -v vim_airline_section_b '\q{lib/vim-airline/gitstatus}'
bleopt/declare -v vim_airline_section_c '\w'
bleopt/declare -v vim_airline_section_x 'bash'
bleopt/declare -v vim_airline_section_y '$_ble_util_locale_encoding[unix]'
bleopt/declare -v vim_airline_section_z ' \q{ble/history/percentile} !\e[1m\q{ble/history/index}/\!'
function bleopt/check:vim_airline_section_a { ble/lib/vim-airline/invalidate; }
function bleopt/check:vim_airline_section_b { ble/lib/vim-airline/invalidate; }
function bleopt/check:vim_airline_section_c { ble/lib/vim-airline/invalidate; }
function bleopt/check:vim_airline_section_x { ble/lib/vim-airline/invalidate; }
function bleopt/check:vim_airline_section_x { ble/lib/vim-airline/invalidate; }
function bleopt/check:vim_airline_section_y { ble/lib/vim-airline/invalidate; }
bleopt/declare -v vim_airline_section_z ' \q{history-percentile} \e[1m!\q{history-index}/\!\e[22m \q{position}'

bleopt/declare -v vim_airline_left_sep $'\uE0B0'
bleopt/declare -v vim_airline_left_alt_sep $'\uE0B1'
Expand All @@ -54,10 +46,10 @@ bleopt/declare -v vim_airline_right_alt_sep $'\uE0B3'
bleopt/declare -v vim_airline_symbol_branch $'\uE0A0'
bleopt/declare -v vim_airline_symbol_dirty $'\u26A1'

function bleopt/check:vim_airline_left_sep { ble/lib/vim-airline/invalidate; }
function bleopt/check:vim_airline_left_alt_sep { ble/lib/vim-airline/invalidate; }
function bleopt/check:vim_airline_right_sep { ble/lib/vim-airline/invalidate; }
function bleopt/check:vim_airline_right_alt_sep { ble/lib/vim-airline/invalidate; }
function bleopt/check:vim_airline_left_sep { ble/prompt/unit#clear _ble_prompt_status; }
function bleopt/check:vim_airline_left_alt_sep { ble/prompt/unit#clear _ble_prompt_status; }
function bleopt/check:vim_airline_right_sep { ble/prompt/unit#clear _ble_prompt_status; }
function bleopt/check:vim_airline_right_alt_sep { ble/prompt/unit#clear _ble_prompt_status; }

builtin eval -- "${_ble_util_gdict_declare//NAME/_ble_lib_vim_airline_mode_map_default}"
ble/gdict#set _ble_lib_vim_airline_mode_map_default 'i' 'INSERT'
Expand Down Expand Up @@ -165,10 +157,13 @@ ble/color/setface vim_airline_c_commandline fg=158,bg=234 # fg=#9cffd3,bg=#20202

#------------------------------------------------------------------------------

# unit:_ble_lib_vim_airline_mode

_ble_lib_vim_airline_mode_data=()
_ble_lib_vim_airline_keymap=
_ble_lib_vim_airline_mode=
_ble_lib_vim_airline_rawmode=
function ble/lib/vim-airline/.update-mode {
function ble/prompt/unit:_ble_lib_vim_airline_mode/update {
local keymap mode m
ble/keymap:vi/script/get-vi-keymap
ble/keymap:vi/script/get-mode
Expand All @@ -181,14 +176,17 @@ function ble/lib/vim-airline/.update-mode {
(*) m='inactive' ;;
esac

local index entry
ble/history/get-index
ble/history/get-entry "$index"
ble/prompt/unit/add-hash '$_ble_edit_str'
ble/prompt/unit/add-hash '$_ble_history_INDEX'

local entry
ble/history/get-entry "$_ble_history_INDEX"
[[ $_ble_edit_str != "$entry" ]] && m=${m}_modified

_ble_lib_vim_airline_keymap=$keymap
_ble_lib_vim_airline_mode=$m
_ble_lib_vim_airline_rawmode=$mode
ble/prompt/unit/assign _ble_lib_vim_airline_keymap "$keymap"
ble/prompt/unit/assign _ble_lib_vim_airline_mode "$m"
ble/prompt/unit/assign _ble_lib_vim_airline_rawmode "$mode"
[[ $prompt_unit_changed ]]
}

## @fn ble/prompt/backslash:lib/vim-airline/mode/.resolve rawmode
Expand Down Expand Up @@ -261,48 +259,24 @@ function ble/prompt/backslash:lib/vim-airline/gitstatus {
fi
}

blehook history_onleave-+=ble/lib/vim-airline/invalidate
function ble/prompt/backslash:ble/history/index {
local index
ble/history/get-index -v index
ble/canvas/put.draw $((index+1))
}
function ble/prompt/backslash:ble/history/percentile {
local index count
ble/history/get-index
ble/history/get-count
((count||count++))
ble/canvas/put.draw "$(((index-1)*100/(count-1)))%"
}

function ble/lib/vim-airline/.instantiate-section {
function ble/prompt/unit:{vim-airline-section}/update {
local section=$1
local bleopt=bleopt_vim_airline_section_$section
local ref_ps=bleopt_vim_airline_section_$section
local face=vim_airline_${section}_$_ble_lib_vim_airline_mode
local save_prefix=_ble_lib_vim_airline_section_${section}_
local -a save_vars=(show data bbox)
local "${save_vars[@]/%/=}" # WA #D1570 checked
if [[ ${!bleopt} ]]; then
local ps=${!bleopt}
local trace_opts=confine:relative:measure-bbox:noscrc:face0="$face":ansi:measure-gbox
ble/util/restore-vars "$save_prefix" "${save_vars[@]}"

local trace_hash esc x y g lc lg
local x1=${bbox[0]} y1=${bbox[1]} x2=${bbox[2]} y2=${bbox[3]}
local gx1=${bbox[4]} gy1=${bbox[5]} gx2=${bbox[6]} gy2=${bbox[7]}
LINES=1 COLUMNS=$cols ble/prompt/.instantiate "$ps" "$trace_opts" "${data[@]:1}"

local version=N/A
data=("$version" "$x" "$y" "$g" "$lc" "$lg" "$esc" "$trace_hash")
bbox=("$x1" "$y1" "$x2" "$y2" "$gx1" "$gy1" "$gx2" "$gy2")
if [[ $gx1 ]] && ((x2+2<=rest_cols)); then
((show=1,rest_cols-=x2+2))
else
show=
fi
fi
ble/util/save-vars "$save_prefix" "${save_vars[@]}"
local prefix=_ble_lib_vim_airline_section_$section

ble/prompt/unit/add-hash '$_ble_lib_vim_airline_mode_data'
ble/prompt/unit/add-hash "\$$ref_ps"
local trace_opts=confine:relative:noscrc:face0="$face":ansi:measure-bbox:measure-gbox
LINES=1 COLUMNS=$cols ble/prompt/unit:{section}/update "$prefix" "${!ref_ps}" "$trace_opts"
}
function ble/prompt/unit:_ble_lib_vim_airline_section_a/update { ble/prompt/unit:{vim-airline-section}/update a; }
function ble/prompt/unit:_ble_lib_vim_airline_section_b/update { ble/prompt/unit:{vim-airline-section}/update b; }
function ble/prompt/unit:_ble_lib_vim_airline_section_c/update { ble/prompt/unit:{vim-airline-section}/update c; }
function ble/prompt/unit:_ble_lib_vim_airline_section_x/update { ble/prompt/unit:{vim-airline-section}/update x; }
function ble/prompt/unit:_ble_lib_vim_airline_section_y/update { ble/prompt/unit:{vim-airline-section}/update y; }
function ble/prompt/unit:_ble_lib_vim_airline_section_z/update { ble/prompt/unit:{vim-airline-section}/update z; }

function ble/lib/vim-airline/.print-section {
local section=$1
local ret g0 bg
Expand Down Expand Up @@ -339,10 +313,10 @@ function ble/lib/vim-airline/.print-section {
fi

local ref_show=_ble_lib_vim_airline_section_${section}_show
local ref_esc=_ble_lib_vim_airline_section_${section}_data[6]
if [[ ${!ref_show} ]]; then
ble/prompt/unit:{section}/get "_ble_lib_vim_airline_section_$section"; local esc=$ret
ble/color/g2sgr-ansi "$g0"
ble/prompt/print "$ret ${!ref_esc}$ret "
ble/prompt/print "$ret $esc$ret "
fi
[[ $section == c ]] && ble/prompt/print $'\r'

Expand All @@ -353,7 +327,7 @@ function ble/lib/vim-airline/.print-section {

function ble/prompt/backslash:lib/vim-airline {
local "${_ble_contrib_prompt_git_vars[@]/%/=}" # WA #D1570 checked
ble/lib/vim-airline/.update-mode
ble/prompt/unit#update _ble_lib_vim_airline_mode

# Set background color
local ret bg=0
Expand All @@ -363,12 +337,18 @@ function ble/prompt/backslash:lib/vim-airline {
ble/color/setface prompt_status_line "g:$bg"

local cols=$COLUMNS; ((_ble_term_xenl||cols--))
local section rest_cols=$((cols-4))
for section in a c z b y x; do
ble/lib/vim-airline/.instantiate-section "$section"
local unit rest_cols=$((cols-4))
for unit in _ble_lib_vim_airline_section_{a,c,z,b,y,x}; do
ble/prompt/unit#update "$unit"

local gx1=${unit}_gbox[0]; gx1=${!gx1}
local x2=${unit}_bbox[2]; x2=${!x2}
local show=
[[ $gx1 ]] && ((x2+2<=rest_cols)) && ((show=1,rest_cols-=x2+2))
builtin eval -- "${unit}_show=\$show"
done

local prev_section= prev_g0= prev_bg=
local section prev_section= prev_g0= prev_bg=
for section in a b c x y z; do
ble/lib/vim-airline/.print-section "$section"
done
Expand Down
1 change: 1 addition & 0 deletions memo/ChangeLog.md
Expand Up @@ -42,6 +42,7 @@
- lib: support `lib/vim-airline` (motivated by huresche) `#D1565` da1d0ff
- util (`ble/gdict`): refactor `#D1569` 7732eed
- vim-airline: support `bleopt vim_airline_theme` `#D1589` 0000000
- prompt: track dependencies and detect changes `#D1590` `#D1591` 0000000
- util, color: refactor configuration interfaces (`bleopt`, `blehook`, `ble-face`) `#D1568` c94d292
- color: support new face setting function `ble-face`
- util (`bleopt`): support option `-r` and `-u` and wildcards in option names
Expand Down

0 comments on commit cf8d949

Please sign in to comment.