forked from dail8859/LuaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquoting.lua
More file actions
Latest commit
112 lines (89 loc) · 2.52 KB
/
Copy pathquoting.lua
File metadata and controls
112 lines (89 loc) · 2.52 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
-- Mark selected or all lines with quoting char '>'
-- helper function to split lines
localfunctionsplit1(str, sep)
localresult= {}
localregex= ("([^%s]+)"):format(sep)
foreachinstr:gmatch(regex) do
table.insert(result, each)
end
returnresult
end
-- split without skip empty lines
localfunctionsplit(str, delimiter)
localresult= { }
localfrom=1
localdelim_from, delim_to=string.find( str, delimiter, from )
whiledelim_fromdo
table.insert( result, string.sub( str, from , delim_from-1 ) )
from=delim_to+1
delim_from, delim_to=string.find( str, delimiter, from )
end
table.insert( result, string.sub( str, from ) )
-- clean only space strings
fori=1, #resultdo
ifnotstring.find(result[i], '%S') then
result[i] =''
end
end
returnresult
end
localfunctionadd_quote_char(lines)
localres= {}
for_, lineinipairs(lines) do
if#line>0then
line='> ' ..line
end
table.insert(res, line)
end
returnres
end
localfunctionGetEOLchar()
localEOLchar
ifeditor.EOLMode==0then
EOLchar="\r\n"
elseifeditor.EOLMode==1then
EOLchar="\r"
elseifeditor.EOLMode==2then
EOLchar="\n"
end
returnEOLchar
end
--------------------------------------------------------------------
npp.AddShortcut("Make Quoting", "", function()
localSelectionStart, SelectionEnd
-- Preserve Undo Capability
editor:BeginUndoAction()
-- Get any selected text
localtext=editor:GetSelText()
-- read selected text or entire document
if#text>=1then
--read the selection points for restore after operation
SelectionStart=editor.SelectionStart
SelectionEnd=editor.SelectionEnd
else
text=editor:GetText()
end
-- Preserve EOL Mode when we send back the reordered lines
localEOLchar=GetEOLchar()
--------------------
-- process the text
--------------------
-- split the text into an array of lines
localrawtextlines=split(text, EOLchar);
-- randomize the lines
rawtextlines=add_quote_char(rawtextlines)
-- output to result var
text=table.concat(rawtextlines, EOLchar)
--------------------
-- end processing
--------------------
-- replace selected text or entire document
ifSelectionStartthen
editor:ReplaceSel(text)
-- restore selection
editor:SetSel(SelectionStart, SelectionEnd)
else
editor:SetText(text)
end
editor:EndUndoAction()
end)