My favorites | Sign in
Project Home Downloads Issues Source
New issue   Search
for
  Advanced search   Search tips   Subscriptions

Issue 1002 attachment: text.py (6.1 KB)

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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
###############################################################################
## Some basic script functions shipped with Programmer's Notepad by various
## authors including:
## Simon Steele
## Scott (wischeese)
## Benoit

import pn
import scintilla
import string
import textwrap
from pypn.decorators import script
import re

@script("Sort Lines", "Text")
def SortLines():
""" Sort Lines (By Scott (wischeese)) """
editor = scintilla.Scintilla(pn.CurrentDoc())
editor.BeginUndoAction()
lsSelection = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd)
laLines = lsSelection.splitlines(0)
laLines_orig = laLines[:]
laLines.sort()
if laLines == laLines_orig:
laLines.reverse()
lsReplace = string.join(laLines, '\r\n' )
editor.ReplaceSel(lsReplace)
editor.EndUndoAction()

@script("Sort Lines (ignore case)", "Text")
def SortLines_no_case():
""" Sort Lines (By Scott (wischeese), Mark (mtnbikingmark)) """
editor = scintilla.Scintilla(pn.CurrentDoc())

debug_out = open(r'c:\_temp\scintilla_working_with.txt', "w")

lsSelection = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd)
debug_out.write(repr(lsSelection) + "\n")

laLines = lsSelection.splitlines(0)
# make a copy - and structure in a way that it will sort case insenstively
to_sort = [(l.lower(), l) for l in laLines]
to_sort.sort()
# get the actual line (not the lower case copy)
sorted = [p[1] for p in to_sort]

# if it was already sorted - then reverse it
if laLines == sorted:
laLines.reverse()
else:
laLines = sorted

debug_out.write(repr("\r\n".join(laLines)) + "\n")
lsReplace = KeepLeadingTrailingNewlines(lsSelection, "\r\n".join(laLines))
debug_out.write(repr(lsReplace) + "\n")
debug_out.close()

editor.BeginUndoAction()
editor.ReplaceSel(lsReplace)
editor.EndUndoAction()

def WrapText(wrap_length):
"Helper function to do the actual wrapping"
editor = scintilla.Scintilla(pn.CurrentDoc())

lsSelection = editor.GetTextRange(
editor.SelectionStart, editor.SelectionEnd)

updated = textwrap.wrap(lsSelection, wrap_length)

# leading and trailing whitespace will have been stripped - so
# see if there was any that we need to add back
lsReplace = KeepLeadingTrailingNewlines(lsSelection, "\r\n".join(updated))

editor.BeginUndoAction()
editor.ReplaceSel(lsReplace)
editor.EndUndoAction()

def KeepLeadingTrailingNewlines(original_text, updated_text):
"""Add any leading/trailing whitespace from original to updated string

Some operations strip leading and trailing newlines so this will put it back
"""
# note - searching only for \r\n at the start - as textwrap does not strip
# leading spaces
leading_newlines = re.search(r"^([\r\n]*)", original_text)
if leading_newlines:
if not updated_text.startswith(leading_newlines.group(1)):
updated_text = leading_newlines.group(1) + updated_text

trailing_newlines = re.search(r"([\r\n]*)$", original_text)
if trailing_newlines:
if not updated_text.endswith(trailing_newlines.group(1)):
updated_text += trailing_newlines.group(1)

return updated_text

@script("Wrap at 80 chars", "Text")
def WrapAt80():
""" Re-wrap selection at 80 characters """
WrapText(80)

@script("Wrap at 100 chars", "Text")
def WrapAt100():
""" Re-wrap at 100 characters """
WrapText(100)


def SetTarget(e, x, y):
e.TargetStart = x
e.TargetEnd = y

@script("Tabs to Spaces", "Text")
def TabsToSpaces():
editor = scintilla.Scintilla(pn.CurrentDoc())

tabSpaces = editor.TabWidth
spaces = ""
for x in range(tabSpaces):
spaces = spaces + " "

end = editor.Length

SetTarget(editor, 0, end)
editor.SearchFlags = 0
editor.BeginUndoAction()

pos = editor.SearchInTarget(1, "\t")

while(pos != -1):
l1 = editor.TargetEnd - editor.TargetStart
editor.ReplaceTarget(tabSpaces, spaces)

# adjust doc length
end = end + tabSpaces - l1
start = pos + tabSpaces

if start >= end:
pos = -1
else:
SetTarget(editor, start, end)
pos = editor.SearchInTarget(1, "\t")

editor.EndUndoAction()

@script("Upper case", "Text")
def UpperCase():
""" Convert text to Upper Case by Benoit """
editor = scintilla.Scintilla(pn.CurrentDoc())
editor.BeginUndoAction()
selText = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd)
selText = selText.upper()
editor.ReplaceSel(selText)
editor.EndUndoAction()

@script("Lower case", "Text")
def LowerCase():
""" Convert text to Lower Case by Benoit """
editor = scintilla.Scintilla(pn.CurrentDoc())
editor.BeginUndoAction()
selText = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd)
selText = selText.lower()
editor.ReplaceSel(selText)
editor.EndUndoAction()

@script("Capitalize case", "Text")
def Capitalize():
""" Capitalise text by Benoit """
editor = scintilla.Scintilla(pn.CurrentDoc())
editor.BeginUndoAction()
selText = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd)
selText = selText.capitalize()
editor.ReplaceSel(selText)
editor.EndUndoAction()

@script("Title case", "Text")
def TitleCase():
""" Title case text by Benoit """
editor = scintilla.Scintilla(pn.CurrentDoc())
editor.BeginUndoAction()
selText = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd)
selText = selText.title()
editor.ReplaceSel(selText)
editor.EndUndoAction()

@script("Invert case", "Text")
def InvertCase():
editor = scintilla.Scintilla(pn.CurrentDoc())
editor.BeginUndoAction()
selText = editor.GetTextRange(editor.SelectionStart, editor.SelectionEnd)
selText = selText.swapcase()
editor.ReplaceSel(selText)
editor.EndUndoAction()
Powered by Google Project Hosting