Skip to content

Commit

Permalink
Implement a new word auto select feature that is more careful with pu…
Browse files Browse the repository at this point in the history
…nctuation
  • Loading branch information
vkbo committed Nov 12, 2023
1 parent e4206bd commit 597b788
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
3 changes: 1 addition & 2 deletions novelwriter/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,8 +327,7 @@ class nwQuotes:


class nwUnicode:
"""Supported unicode character constants and their HTML equivalents.
"""
"""Supported unicode character constants and their HTML equivalents."""
# Unicode Constants
# =================

Expand Down
46 changes: 29 additions & 17 deletions novelwriter/gui/doceditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -1934,27 +1934,39 @@ def _allowSpaceBeforeColon(text: str, char: str) -> bool:

def _autoSelect(self) -> QTextCursor:
"""Return a cursor which may or may not have a selection based
on user settings and document action.
on user settings and document action. The selection will be the
word closest to the cursor consisting of alphanumerical unicode
characters.
"""
cursor = self.textCursor()
if CONFIG.autoSelect and not cursor.hasSelection():
cursor.select(QTextCursor.WordUnderCursor)
posS = cursor.selectionStart()
posE = cursor.selectionEnd()
cPos = cursor.position()
bPos = cursor.block().position()
bLen = cursor.block().length()

# Scan backwards
sPos = cPos
for i in range(cPos - bPos):
sPos = cPos - i - 1
if not self._qDocument.characterAt(sPos).isalnum():
sPos += 1
break

# Underscore counts as a part of the word, so check that the
# selection isn't wrapped in italics markers.
reSelect = False
if self._qDocument.characterAt(posS) == "_":
posS += 1
reSelect = True
if self._qDocument.characterAt(posE) == "_":
posE -= 1
reSelect = True
if reSelect:
cursor.clearSelection()
cursor.setPosition(posS, QTextCursor.MoveAnchor)
cursor.setPosition(posE-1, QTextCursor.KeepAnchor)
# Scan forwards
ePos = cPos
for i in range(bPos + bLen - cPos):
ePos = cPos + i
if not self._qDocument.characterAt(ePos).isalnum():
# ePos -= 1
break

if ePos - sPos <= 0:
# No selection possible
return cursor

cursor.clearSelection()
cursor.setPosition(sPos, QTextCursor.MoveAnchor)
cursor.setPosition(ePos, QTextCursor.KeepAnchor)

self.setTextCursor(cursor)

Expand Down

0 comments on commit 597b788

Please sign in to comment.