Skip to content

Commit a487d6a

Browse files
committed
Add Vim mode setting to Preferences and fix editor reinit
1 parent 2f92da9 commit a487d6a

File tree

3 files changed

+35
-17
lines changed

3 files changed

+35
-17
lines changed

novelwriter/dialogs/preferences.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,13 @@ def buildForm(self) -> None:
566566
self.tr("Highlight current line"), self.lineHighlight
567567
)
568568

569+
# Enable Vim Mode
570+
self.vimMode = NSwitch(self)
571+
self.vimMode.setChecked(CONFIG.vimMode)
572+
self.mainForm.addRow(
573+
self.tr("Enable Vim mode"), self.vimMode,
574+
)
575+
569576
# Show Tabs and Spaces
570577
self.showTabsNSpaces = NSwitch(self)
571578
self.showTabsNSpaces.setChecked(CONFIG.showTabsNSpaces)
@@ -1075,6 +1082,7 @@ def _doSave(self) -> None:
10751082
CONFIG.lineHighlight = lineHighlight
10761083
CONFIG.showTabsNSpaces = self.showTabsNSpaces.isChecked()
10771084
CONFIG.showLineEndings = self.showLineEndings.isChecked()
1085+
CONFIG.vimMode = self.vimMode.isChecked()
10781086

10791087
# Editor Scrolling
10801088
CONFIG.autoScroll = self.autoScroll.isChecked()

novelwriter/enum.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ class nwVimMode(Enum):
255255
NORMAL = 0
256256
INSERT = 1
257257
VISUAL = 2
258-
VLINE = 3
258+
V_LINE = 3
259259

260260

261261
class nwStandardButton(Enum):

novelwriter/gui/doceditor.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,6 @@ def __init__(self, parent: QWidget) -> None:
236236

237237
# Vim State for vimMode
238238
self._vim = VimState()
239-
self.setVimMode(nwVimMode.NORMAL)
240239

241240
# Finalise
242241
self.updateSyntaxColors()
@@ -395,6 +394,9 @@ def initEditor(self) -> None:
395394
else:
396395
self.clearEditor()
397396

397+
# Refresh Vim Mode
398+
self.setVimMode(nwVimMode.NORMAL)
399+
398400
def loadText(self, tHandle: str, tLine: int | None = None) -> bool:
399401
"""Load text from a document into the editor. If we have an I/O
400402
error, we must handle this and clear the editor so that we don't
@@ -622,6 +624,8 @@ def setVimMode(self, mode: nwVimMode) -> None:
622624
self.setTextCursor(cursor)
623625
self._vim.setMode(mode)
624626
self.docFooter.updateVimModeStatusBar(mode)
627+
else:
628+
self.docFooter.updateVimModeStatusBar(None)
625629

626630
def setDocumentChanged(self, state: bool) -> None:
627631
"""Keep track of the document changed variable, and emit the
@@ -956,7 +960,7 @@ def keyPressEvent(self, event: QKeyEvent) -> None:
956960
if self._handleVimNormalModeModeSwitching(event):
957961
return
958962

959-
if self._vim.mode in (nwVimMode.VISUAL, nwVimMode.VLINE):
963+
if self._vim.mode in (nwVimMode.VISUAL, nwVimMode.V_LINE):
960964
self._handleVimVisualMode(event)
961965
else:
962966
self._handleVimNormalMode(event)
@@ -1940,7 +1944,7 @@ def _handleVimNormalModeModeSwitching(self, event: QKeyEvent) -> bool:
19401944
self.setTextCursor(cursor)
19411945
return True
19421946
elif text == "V":
1943-
self.setVimMode(nwVimMode.VLINE)
1947+
self.setVimMode(nwVimMode.V_LINE)
19441948
cursor = self.textCursor()
19451949
cursor.select(QtSelectLine)
19461950
self.setTextCursor(cursor)
@@ -3554,7 +3558,7 @@ def updateTheme(self) -> None:
35543558
nwVimMode.NORMAL: (self.tr("NORMAL"), SHARED.theme.getBaseColor("green")),
35553559
nwVimMode.INSERT: (self.tr("INSERT"), SHARED.theme.getBaseColor("blue")),
35563560
nwVimMode.VISUAL: (self.tr("VISUAL"), SHARED.theme.getBaseColor("orange")),
3557-
nwVimMode.VLINE: (self.tr("V-LINE"), SHARED.theme.getBaseColor("orange")),
3561+
nwVimMode.V_LINE: (self.tr("V-LINE"), SHARED.theme.getBaseColor("orange")),
35583562
}
35593563

35603564
def matchColors(self) -> None:
@@ -3621,16 +3625,21 @@ def updateMainCount(self, count: int, selection: bool) -> None:
36213625
text = self._trMainCount.format("0", "+0")
36223626
self.wordsText.setText(text)
36233627

3624-
def updateVimModeStatusBar(self, vimMode: nwVimMode) -> None:
3628+
def updateVimModeStatusBar(self, mode: nwVimMode | None) -> None:
36253629
"""Update the vim Mode status information."""
3626-
if vimMode != self._vimMode:
3627-
text, color = self._vimModes.get(vimMode, ("", QtBlack))
3630+
if mode is None:
3631+
self.vimStatus.setText("")
3632+
self.vimStatus.setVisible(False)
3633+
self._vimMode = None
3634+
elif mode != self._vimMode:
3635+
text, color = self._vimModes.get(mode, ("", QtBlack))
36283636
palette = self.vimStatus.palette()
36293637
palette.setColor(QPalette.ColorRole.WindowText, self._vimColor)
36303638
palette.setColor(QPalette.ColorRole.Window, color)
3631-
self.vimStatus.setText(f" {text} ")
3639+
self.vimStatus.setText(f" {text} ")
36323640
self.vimStatus.setPalette(palette)
3633-
self._vimMode = vimMode
3641+
self.vimStatus.setVisible(True)
3642+
self._vimMode = mode
36343643

36353644

36363645
class VimState:
@@ -3657,16 +3666,17 @@ def mode(self) -> nwVimMode:
36573666
@property
36583667
def command(self) -> str:
36593668
"""Return the current vim command."""
3660-
if self._mode in (nwVimMode.VISUAL, nwVimMode.VLINE):
3669+
if self._mode in (nwVimMode.VISUAL, nwVimMode.V_LINE):
36613670
return self._visualCommand
36623671
else:
36633672
return self._normalCommand
36643673

3665-
def setMode(self, newMode: nwVimMode) -> None:
3674+
def setMode(self, mode: nwVimMode) -> None:
36663675
"""Switch vim mode."""
3667-
logger.debug("Vim Mode changed to %s", newMode.name)
3668-
self._mode = newMode
3669-
self.resetCommand()
3676+
if mode != self._mode:
3677+
logger.debug("Vim Mode changed to %s", mode.name)
3678+
self._mode = mode
3679+
self.resetCommand()
36703680

36713681
def resetCommand(self) -> None:
36723682
"""Reset internal vim command."""
@@ -3677,14 +3687,14 @@ def pushCommandKey(self, key: str) -> None:
36773687
"""Push key to the current command building stack."""
36783688
if self._mode is nwVimMode.NORMAL:
36793689
self._normalCommand += key
3680-
elif self._mode in (nwVimMode.VISUAL, nwVimMode.VLINE):
3690+
elif self._mode in (nwVimMode.VISUAL, nwVimMode.V_LINE):
36813691
self._visualCommand += key
36823692

36833693
def setCommand(self, key: str) -> None:
36843694
"""Set the state of the current vim command."""
36853695
if self._mode is nwVimMode.NORMAL:
36863696
self._normalCommand = key
3687-
elif self._mode in (nwVimMode.VISUAL, nwVimMode.VLINE):
3697+
elif self._mode in (nwVimMode.VISUAL, nwVimMode.V_LINE):
36883698
self._visualCommand = key
36893699

36903700
def yankToInternal(self, text: str) -> None:

0 commit comments

Comments
 (0)