/
kate_selection_info.py
125 lines (90 loc) · 4.04 KB
/
kate_selection_info.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# -*- coding: utf-8 -*-
# Author: Lars Banner-Voigt
# License: see LICENSE
from PyKDE4.kdecore import i18n
from PyKDE4.kdeui import KAction, KIcon
from PyQt4.QtCore import QObject, Qt
from PyQt4.QtGui import *
from libkatepate.errors import showOk, showError
import kate
import re
class Plugin(QObject):
def __init__(self):
QObject.__init__(self)
self.window = kate.mainInterfaceWindow().window()
self.addAction('open_selection_info', None, 'Selection info', 'Ctrl+Alt+I', self.selection_info, 'Tools')
showOk('Selection inits!')
self.dia = QDialog(None, Qt.CustomizeWindowHint | Qt.WindowStaysOnTopHint)
self.dia.setStyleSheet('QDialog { border: 1px solid black; }')
layout = QFormLayout(self.dia)
layout.setSpacing(5)
ok = QPushButton('Close')
ok.clicked.connect(self.dia.hide)
self.chars = QLabel()
self.lines = QLabel()
self.spaces = QLabel()
self.word_count = QLabel()
self.chars_no_space = QLabel()
self.occurences = QLabel()
self.chars .setTextInteractionFlags(Qt.TextSelectableByMouse)
self.lines .setTextInteractionFlags(Qt.TextSelectableByMouse)
self.spaces .setTextInteractionFlags(Qt.TextSelectableByMouse)
self.word_count .setTextInteractionFlags(Qt.TextSelectableByMouse)
self.chars_no_space .setTextInteractionFlags(Qt.TextSelectableByMouse)
self.occurences .setTextInteractionFlags(Qt.TextSelectableByMouse)
self.search = QLineEdit()
self.search.textChanged.connect(self.updateInfo)
self.search.setPlaceholderText('Regex')
layout.addRow('Characters w. spaces:', self.chars)
layout.addRow('Newlines:', self.lines)
layout.addRow('Word Count:', self.word_count)
layout.addRow('Characters:', self.chars_no_space)
layout.addRow('Spaces:', self.spaces)
layout.addRow( self.search, self.occurences)
layout.addRow( ok )
def selection_info(self):
self.updateInfo()
self.dia.exec()
def updateInfo(self):
text = kate.activeView().selectionText()
if text == '':
text = kate.activeView().document().text()
chars = len(text)
spaces = sum([x.isspace() for x in text])
lines = sum([x == '\n' for x in text])
word_count = len(text.split(None))
chars_no_space = chars - spaces
try:
junk, occurences = re.subn(self.search.text(), '', text)
except:
occurences = "Invalid"
self.chars.setText( str(chars ).ljust(10))
self.lines.setText( str(lines ))
self.spaces.setText( str(spaces ))
self.word_count.setText( str(word_count ))
self.occurences.setText( str(occurences ))
self.chars_no_space.setText( str(chars_no_space ))
def addAction(self, objectName, icon, text, shortcut = "", slot = None, menuName = None):
act = KAction(KIcon(icon), text, self)
act.setObjectName(objectName)
# Set shortcut
if not act.objectName() in kate.configuration:
kate.configuration[act.objectName()] = shortcut
act.setShortcut(kate.configuration[act.objectName()])
# Set slots
if slot != None:
act.triggered.connect( slot )
kate.mainInterfaceWindow().window().actionCollection().addAction(act.objectName(), act)
# Add to menu
if menuName != None:
menu = kate.mainInterfaceWindow().window().findChild(QMenu, menuName.lower())
if menu == None:
menu = kate.mainInterfaceWindow().window().menuBar().addMenu(menuName)
menu.addAction(act)
# Save changes to shortcut
act.changed.connect( self.onActionChange )
return act
def onActionChange(self):
kate.configuration[self.sender().objectName()] = self.sender().shortcut().toString()
kate.configuration.save()
print(self.sender().objectName() + ': Save ' + kate.configuration[self.sender().objectName()])