Skip to content

Commit

Permalink
Merge pull request #45 from nakagami/on_open_buffer
Browse files Browse the repository at this point in the history
On open buffer issue #44
  • Loading branch information
nakagami authored Aug 12, 2024
2 parents 82e24f7 + 440eafb commit f5b8148
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
14 changes: 14 additions & 0 deletions examples/config/pyvimrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Pyvim configuration. Save to file to: ~/.pyvimrc
"""
import sys
import os
from prompt_toolkit.application import run_in_terminal
from prompt_toolkit.filters import ViInsertMode
from prompt_toolkit.key_binding.key_processor import KeyPress
Expand All @@ -14,6 +15,17 @@ __all__ = (
)


def on_open_buffer(location, buffer):
if location:
match os.path.splitext(location)[-1]:
case ".go":
buffer.expand_tab = False
case ".ex":
buffer.expand_tab = True
buffer.tabstop = 2
buffer.shifwidth = 2


def configure(editor):
"""
Configuration function. We receive a ``pyvim.editor.Editor`` instance as
Expand Down Expand Up @@ -48,6 +60,8 @@ def configure(editor):
# Apply colorscheme. (:colorscheme vim)
editor.use_colorscheme('vim')

editor.on_open_buffer = on_open_buffer


# Add custom key bindings:

Expand Down
23 changes: 20 additions & 3 deletions pyvim/buffer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ def __init__(self, *args, **kwargs):
self._editor = editor
self.mark = {}

# Vi options.
self._expand_tab = None
self._tabstop = None
self._shiftwidth = None

def _search(
self,
search_state: SearchState,
Expand Down Expand Up @@ -103,15 +108,27 @@ def search_once(

@property
def expand_tab(self):
return self._editor.expand_tab
return self._editor.expand_tab if self._expand_tab is None else self._expand_tab

@expand_tab.setter
def expand_tab(self, v):
self._expand_tab = v

@property
def tabstop(self):
return self._editor.tabstop
return self._editor.tabstop if self._tabstop is None else self._tabstop

@tabstop.setter
def tabstop(self, v):
self._tabstop = v

@property
def shiftwidth(self):
return self._editor.shiftwidth
return self._editor.shiftwidth if self._shiftwidth is None else self._shiftwidth

@shiftwidth.setter
def shiftwidth(self, v):
self._shiftwidth = v

def undo(self):
text = self.text
Expand Down
2 changes: 2 additions & 0 deletions pyvim/editor.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def __init__(self, config_directory='~/.pyvim', input=None, output=None):
# open file history
self.location_history = []

self.on_open_buffer = None

# Ensure config directory exists.
self.config_directory = os.path.abspath(os.path.expanduser(config_directory))
if not os.path.exists(self.config_directory):
Expand Down
4 changes: 2 additions & 2 deletions pyvim/key_bindings.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ def _unindent(event):
else:
for i in range(a, b):
if document.text[i] == '\t':
b.text = document.text[:i] + document.text[i + 1:]
b.cursor_position = cursor_position - 1
buffer.text = document.text[:i] + document.text[i + 1:]
buffer.cursor_position = cursor_position - 1
break

@kb.add("O", filter=vi_navigation_mode & ~is_read_only)
Expand Down
5 changes: 4 additions & 1 deletion pyvim/window_arrangement.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,15 +437,18 @@ def _get_or_create_editor_buffer(self, location=None, text=None):
self._add_editor_buffer(eb)

return eb
self.editor.location_history.append(location)
return eb

def open_buffer(self, location=None, show_in_current_window=False):
"""
Open/create a file, load it, and show it in a new buffer.
"""
eb = self._get_or_create_editor_buffer(location)
if location:
self.editor.location_history.append(location)

if self.editor.on_open_buffer:
self.editor.on_open_buffer(location, eb.buffer)
if show_in_current_window:
self.show_editor_buffer(eb)

Expand Down

0 comments on commit f5b8148

Please sign in to comment.