2 |
2 |
3 # Copyright (c) 2007 - 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
3 # Copyright (c) 2007 - 2020 Detlev Offenbach <detlev@die-offenbachs.de> |
4 # |
4 # |
5 |
5 |
6 """ |
6 """ |
7 Module implementing a minimalistic editor for simple editing tasks. |
7 Module implementing an editor for simple editing tasks. |
8 """ |
8 """ |
9 |
|
10 |
9 |
11 import os |
10 import os |
12 import re |
11 import re |
13 |
12 |
14 from PyQt5.QtCore import ( |
13 from PyQt5.QtCore import ( |
15 QSignalMapper, QPoint, QTimer, QFileInfo, pyqtSignal, QSize, QRegExp, Qt, |
14 pyqtSignal, Qt, QSignalMapper, QPoint, QTimer, QFileInfo, QSize, |
16 QCoreApplication |
15 QCoreApplication |
17 ) |
16 ) |
18 from PyQt5.QtGui import QKeySequence, QPalette, QFont, QPixmap |
17 from PyQt5.QtGui import QKeySequence, QPalette, QFont, QPixmap |
19 from PyQt5.QtWidgets import ( |
18 from PyQt5.QtWidgets import ( |
20 QWidget, QWhatsThis, QActionGroup, QDialog, QInputDialog, QApplication, |
19 QWidget, QWhatsThis, QActionGroup, QDialog, QInputDialog, QApplication, |
112 self.endUndoAction() |
111 self.endUndoAction() |
113 |
112 |
114 |
113 |
115 class MiniEditor(E5MainWindow): |
114 class MiniEditor(E5MainWindow): |
116 """ |
115 """ |
117 Class implementing a minimalistic editor for simple editing tasks. |
116 Class implementing an editor for simple editing tasks. |
118 |
117 |
119 @signal editorSaved() emitted after the file has been saved |
118 @signal editorSaved() emitted after the file has been saved |
120 @signal languageChanged(str) emitted when the editors language was set. The |
119 @signal languageChanged(str) emitted when the editors language was set. The |
121 language is passed as a parameter. |
120 language is passed as a parameter. |
122 @signal editorRenamed(str) emitted after the editor got a new name |
121 @signal editorRenamed(str) emitted after the editor got a new name |
3437 |
3436 |
3438 def __getWord(self, line, index): |
3437 def __getWord(self, line, index): |
3439 """ |
3438 """ |
3440 Private method to get the word at a position. |
3439 Private method to get the word at a position. |
3441 |
3440 |
3442 @param line number of line to look at (int) |
3441 @param line number of line to look at |
3443 @param index position to look at (int) |
3442 @type int |
3444 @return the word at that position (string) |
3443 @param index position to look at |
3445 """ |
3444 @type int |
3446 text = self.__textEdit.text(line) |
3445 @return the word at that position |
3447 if self.__textEdit.caseSensitive(): |
3446 @rtype str |
3448 cs = Qt.CaseSensitive |
3447 """ |
3449 else: |
|
3450 cs = Qt.CaseInsensitive |
|
3451 wc = self.__textEdit.wordCharacters() |
3448 wc = self.__textEdit.wordCharacters() |
3452 if wc is None: |
3449 if wc is None: |
3453 regExp = QRegExp(r'[^\w_]', cs) |
3450 pattern = r"\b[\w_]+\b" |
3454 else: |
3451 else: |
3455 wc = re.sub(r'\w', "", wc) |
3452 wc = re.sub(r'\w', "", wc) |
3456 regExp = QRegExp(r'[^\w{0}]'.format(re.escape(wc)), cs) |
3453 pattern = r"\b[\w{0}]+\b".format(re.escape(wc)) |
3457 start = regExp.lastIndexIn(text, index) + 1 |
3454 if self.__textEdit.caseSensitive(): |
3458 end = regExp.indexIn(text, index) |
3455 rx = re.compile(pattern) |
3459 if start == end + 1 and index > 0: |
3456 else: |
3460 # we are on a word boundary, try again |
3457 rx = re.compile(pattern, re.IGNORECASE) |
3461 start = regExp.lastIndexIn(text, index - 1) + 1 |
3458 |
3462 if start == -1: |
3459 text = self.text(line) |
3463 start = 0 |
3460 for match in rx.finditer(text): |
3464 if end == -1: |
3461 start, end = match.span() |
3465 end = len(text) |
3462 if start <= index <= end: |
3466 if end > start: |
3463 return match.group() |
3467 word = text[start:end] |
3464 |
3468 else: |
3465 return "" |
3469 word = '' |
|
3470 return word |
|
3471 |
3466 |
3472 def __getCurrentWord(self): |
3467 def __getCurrentWord(self): |
3473 """ |
3468 """ |
3474 Private method to get the word at the current position. |
3469 Private method to get the word at the current position. |
3475 |
3470 |