From a8616ff090df652761a440dd32c832fb5ce3b242 Mon Sep 17 00:00:00 2001 From: Siarhei Siniak Date: Fri, 30 Sep 2022 15:36:03 +0300 Subject: [PATCH] [~] Refactor --- d1/f1.sh | 1 + .../profile_default/ipython_config.py | 48 ++++++++++++++++++- dotfiles/.vimrc | 7 ++- 3 files changed, 53 insertions(+), 3 deletions(-) diff --git a/d1/f1.sh b/d1/f1.sh index 435306b..4eb1455 100644 --- a/d1/f1.sh +++ b/d1/f1.sh @@ -5,6 +5,7 @@ cp dotfiles/.local/bin/commands ~/.local/bin/commands mkdir -p ~/.sway cp dotfiles/.sway/config ~/.sway/config cp dotfiles/.zshenv ~/.zshenv +cp dotfiles/.vimrc ~/.vimrc cp dotfiles/.tmux.conf ~/.tmux.conf cp -rp \ dotfiles/.ipython/profile_default/ipython_config.py \ diff --git a/dotfiles/.ipython/profile_default/ipython_config.py b/dotfiles/.ipython/profile_default/ipython_config.py index 9f64323..b298bc9 100644 --- a/dotfiles/.ipython/profile_default/ipython_config.py +++ b/dotfiles/.ipython/profile_default/ipython_config.py @@ -4,15 +4,59 @@ c.InteractiveShellApp.exec_lines = [ def ipython_update_shortcuts(): import IPython import prompt_toolkit.filters + import prompt_toolkit.document + import functools + import tempfile + import io + import subprocess + + def ipython_edit_in_vim(*args, pt_app): + content = pt_app.app.current_buffer.document.text + lines_count = lambda text: len(text.splitlines()) + + with tempfile.NamedTemporaryFile( + suffix='.py', + mode='w', + ) as f: + with io.open(f.name, 'w') as f2: + f2.write(content) + f2.flush() + + result = subprocess.call([ + 'vim', + '+%d' % lines_count(content), + f.name, + ]) + + if result != 0: + return + + f.seek(0, io.SEEK_SET) + + with io.open(f.name, 'r') as f2: + new_content = f2.read() + + pt_app.app.current_buffer.document = \ + prompt_toolkit.document.Document( + new_content, + cursor_position=len(new_content.rstrip()), + ) + t1 = IPython.get_ipython() t2 = t1.pt_app - t3 = [o for o in t2.key_bindings.bindings if 'f2' in repr(o).lower()] + t3 = [o for o in t2.key_bindings.bindings if 'f2' in repr(o.keys).lower()] assert len(t3) == 1 t4 = t3[0] t2.key_bindings.remove(t4.handler) t2.key_bindings.add( 'e', filter=~prompt_toolkit.filters.vi_insert_mode, - )(t4.handler) + )( + functools.partial( + ipython_edit_in_vim, + pt_app=t2, + ) + #t4.handler + ) ''', 'ipython_update_shortcuts()', ] diff --git a/dotfiles/.vimrc b/dotfiles/.vimrc index 6141b04..4967447 100644 --- a/dotfiles/.vimrc +++ b/dotfiles/.vimrc @@ -43,13 +43,18 @@ syntax on set hls map w -map r :source ~/.vimrc:echo 'reloaded' +map z :wqa map m :py3 f1() +map r :redraw! map cq :cq map f2 :py3 f2() +map f3 :source ~/.vimrc:echo 'reloaded' map qy :q! map cq :cq1 map dq :cq2 map i1 :set sw=4 sts=4 ts=4 et ai ci:retab map i2 :set sw=2 sts=2 ts=2 et ai ci:retab +map i3 :set t_Co=0 so=999 +map i4 :set t_Co=256 so=0 set foldmethod=indent +set nofoldenable