forked from DGivney/geany-lua-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
195 lines (169 loc) · 7.18 KB
/
util.lua
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
194
195
#! /usr/bin/env lua
-- Provide utility functions
--
-- v0.1
-- (c) 2013 by Carl Antuar.
-- Distribution is permitted under the terms of the GPLv3
-- or any later version.
---- Define constants ----
_SPACER = "\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t"
KEY_GROUPS = { ["lower"]="abcdefghijklmnopqrstuvwxyz", ["upper"]="ABCDEFGHIJKLMNOPQRSTUVWXYZ", ["whitespace"]=" \t\n\r" }
---- Define functions ----
function debugMessage(message)
if debugEnabled then geany.message("DEBUG", message) end
end
function isProjectOpen()
return not (geany.appinfo().project == nil)
end
function getOutputLines(command)
local lines = {}
local lineCount = 0
local result = io.popen(command, 'r')
if result == nil then
geany.message("ERROR", "Failed to get output of command ["..command.."]")
return
end
for line in result:lines() do
-- need to index from 1 to show up properly in choose dialog
lineCount = lineCount + 1
lines[lineCount] = line
end
result:close()
debugMessage("Returning "..lineCount.." output lines")
return lineCount,lines
end
function getCurrentLine()
local line, column = geany.rowcol()
return geany.lines(line)
end
function trim(text)
return text:gsub("^%s*(.-)%s*$", "%1")
end
function atDocumentEdge()
return geany.caret() < 2 or geany.caret() == geany.length()
end
function isLowerCase(charCode)
return string.find(KEY_GROUPS["lower"], string.char(charCode), 1, true)
end
function isUpperCase(charCode)
return string.find(KEY_GROUPS["upper"], string.char(charCode), 1, true)
end
function isWhitespace(charCode)
return string.find(KEY_GROUPS["whitespace"], string.char(charCode), 1, true)
end
-- Navigating by word parts almost matches Vim word navigation, except:
-- - if the cursor starts on whitespace, it goes to the closer end of the next word, not the further end
-- - if a word is camel-cased, it steps through each segment.
function navWordEndRight(extend)
if extend then geany.select() end
debugMessage("Character at caret is "..geany.byte().." ["..string.char(geany.byte()).."]")
repeat
geany.navigate("part", 1, extend)
local charCode, previousCharCode = geany.byte(), geany.byte(geany.caret() - 1)
debugMessage("Current char is "..charCode.." ["..string.char(charCode).."]")
debugMessage("Previous char is "..previousCharCode.." ["..string.char(previousCharCode).."]")
until not isWhitespace(previousCharCode) and not (isUpperCase(charCode) and isLowerCase(previousCharCode)) or atDocumentEdge()
end
function navWordEndLeft(extend)
if extend then geany.select() end
debugMessage("Character at caret is "..geany.byte().." ["..string.char(geany.byte()).."]")
repeat
geany.navigate("part", -1, extend)
local charCode, previousCharCode = geany.byte(), geany.byte(geany.caret() - 1)
debugMessage("Current char is "..charCode.." ["..string.char(charCode).."]")
debugMessage("Previous char is "..previousCharCode.." ["..string.char(previousCharCode).."]")
until not isWhitespace(previousCharCode) and not (isUpperCase(charCode) and isLowerCase(previousCharCode)) or atDocumentEdge()
end
function navWordStartRight(extend)
if extend then geany.select() end
debugMessage("Character at caret is "..geany.byte().." ["..string.char(geany.byte()).."]")
repeat
geany.navigate("part", 1, extend)
local charCode, previousCharCode = geany.byte(), geany.byte(geany.caret() - 1)
debugMessage("Current char is "..charCode.." ["..string.char(charCode).."]")
debugMessage("Previous char is "..previousCharCode.." ["..string.char(previousCharCode).."]")
until not isWhitespace(charCode) and not (isUpperCase(charCode) and isLowerCase(previousCharCode)) or atDocumentEdge()
end
function navWordStartLeft(extend)
if extend then geany.select() end
debugMessage("Character at caret is "..geany.byte().." ["..string.char(geany.byte()).."]")
repeat
geany.navigate("part", -1, extend)
local charCode, previousCharCode = geany.byte(), geany.byte(geany.caret() - 1)
debugMessage("Current char is "..charCode.." ["..string.char(charCode).."]")
debugMessage("Previous char is "..previousCharCode.." ["..string.char(previousCharCode).."]")
until not isWhitespace(charCode) and not (isUpperCase(charCode) and isLowerCase(previousCharCode)) or atDocumentEdge()
end
function navWORDEndRight(extend)
if extend then geany.select() end
debugMessage("Character at caret is "..geany.byte().." ["..string.char(geany.byte()).."]")
repeat
geany.navigate("part", 1, extend)
local charCode, previousCharCode = geany.byte(), geany.byte(geany.caret() - 1)
debugMessage("Current char is "..charCode.." ["..string.char(charCode).."]")
debugMessage("Previous char is "..previousCharCode.." ["..string.char(previousCharCode).."]")
until isWhitespace(charCode) or atDocumentEdge()
end
function navWORDEndLeft(extend)
if extend then geany.select() end
debugMessage("Character at caret is "..geany.byte().." ["..string.char(geany.byte()).."]")
repeat
geany.navigate("part", -1, extend)
local charCode, previousCharCode = geany.byte(), geany.byte(geany.caret() - 1)
debugMessage("Current char is "..charCode.." ["..string.char(charCode).."]")
debugMessage("Previous char is "..previousCharCode.." ["..string.char(previousCharCode).."]")
until isWhitespace(charCode) or atDocumentEdge()
end
function navWORDStartRight(extend)
if extend then geany.select() end
debugMessage("Character at caret is "..geany.byte().." ["..string.char(geany.byte()).."]")
repeat
geany.navigate("part", 1, extend)
local charCode, previousCharCode = geany.byte(), geany.byte(geany.caret() - 1)
debugMessage("Current char is "..charCode.." ["..string.char(charCode).."]")
debugMessage("Previous char is "..previousCharCode.." ["..string.char(previousCharCode).."]")
until isWhitespace(previousCharCode) or atDocumentEdge()
end
function navWORDStartLeft(extend)
if extend then geany.select() end
debugMessage("Character at caret is "..geany.byte().." ["..string.char(geany.byte()).."]")
repeat
geany.navigate("part", -1, extend)
local charCode, previousCharCode = geany.byte(), geany.byte(geany.caret() - 1)
debugMessage("Current char is "..charCode.." ["..string.char(charCode).."]")
debugMessage("Previous char is "..previousCharCode.." ["..string.char(previousCharCode).."]")
until isWhitespace(previousCharCode) or atDocumentEdge()
end
function tryCommand(command)
local result = os.execute(command)
debugMessage("Result of executing ["..command.."] is "..tostring(result))
return result == 0 or result == true
end
function getListCommand(dirname, fileFilter)
return "ls -t "..dirname..geany.dirsep..fileFilter
end
function ensureDirExists(dirname)
if not (os.execute(getListCommand(dirname, "")) == 0) then
debugMessage("Creating directory "..dirname)
os.execute("mkdir -p "..dirname)
end
end
function getSupportDir()
local dir = geany.appinfo().scriptdir..geany.dirsep.."support"
ensureDirExists(dir)
return dir
end
function getFileContents(filename)
local stringBuilder = ""
for line in io.lines(filename) do
if not (stringBuilder == "") then stringBuilder = stringBuilder.."\n" end
stringBuilder = stringBuilder..line
end
return stringBuilder
end
function setFileContents(filename, contents)
local fileHandle = io.open(filename, "w")
fileHandle:write(contents)
fileHandle:flush()
fileHandle:close()
end