-
-
Notifications
You must be signed in to change notification settings - Fork 122
/
init.lua
295 lines (261 loc) · 7.6 KB
/
init.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
local Promise = require('orgmode.utils.promise')
local OrgFile = require('orgmode.files.file')
local utils = require('orgmode.utils')
local config = require('orgmode.config')
local ts_utils = require('orgmode.utils.treesitter')
local Listitem = require('orgmode.files.elements.listitem')
---@class OrgFilesOpts
---@field paths string | string[]
---@class OrgFiles
---@field paths string[]
---@field files table<string, OrgFile> table with files that are part of paths
---@field all_files table<string, OrgFile> all loaded files, no matter if they are part of paths
---@field load_state 'loading' | 'loaded' | nil
local OrgFiles = {}
---@param opts OrgFilesOpts
function OrgFiles:new(opts)
local data = {
paths = opts.paths or {},
files = {},
all_files = {},
load_state = nil,
}
setmetatable(data, self)
self.__index = self
data:load_sync()
return data
end
---@param force? boolean Force reload all files
---@return OrgPromise
function OrgFiles:load(force)
if not force and self.load_state then
if self.load_state == 'loading' then
self:ensure_loaded()
end
return Promise.resolve(self.files)
end
self.load_state = 'loading'
local actions = vim.tbl_map(function(filename)
return self:load_file(filename):next(function(orgfile)
if orgfile then
self.files[filename] = orgfile
end
return orgfile
end)
end, self:_files())
return Promise.all(actions):next(function()
self.load_state = 'loaded'
return self.files
end)
end
function OrgFiles:get_tags()
local tags = {}
for _, orgfile in ipairs(self:all()) do
if not orgfile:is_archive_file() then
local file_tags = orgfile:get_filetags()
if file_tags and #file_tags > 0 then
for _, tag in ipairs(file_tags) do
tags[tag] = 1
end
end
for _, headline in ipairs(orgfile:get_headlines()) do
local htags = headline:get_tags()
if htags and #htags > 0 then
for _, tag in ipairs(htags) do
tags[tag] = 1
end
end
end
end
end
local taglist = vim.tbl_keys(tags)
table.sort(taglist)
return taglist
end
function OrgFiles:unload()
self.files = {}
self.all_files = {}
self.paths = {}
self.load_state = nil
return self
end
function OrgFiles:get_clocked_headline()
-- TODO: Optimize
for _, file in ipairs(self:all()) do
for _, headline in ipairs(file:get_headlines()) do
if headline:is_clocked_in() then
return headline
end
end
end
return nil
end
function OrgFiles:get_current_file()
local filename = utils.current_file_path()
local orgfile = self:load_file_sync(filename)
assert(orgfile, 'Current file not found or not an org file')
return orgfile
end
---@return OrgFile[]
function OrgFiles:all()
self:ensure_loaded()
local valid_files = {}
local filenames = self:_files()
for _, file in ipairs(filenames) do
if self.files[file] then
table.insert(valid_files, self.files[file])
end
end
return valid_files
end
---@return string[]
function OrgFiles:filenames()
return vim.tbl_map(function(file)
return file.filename
end, self:all())
end
---@return OrgPromise<OrgFile>
function OrgFiles:load_file(filename)
local file = self.all_files[filename]
if file then
return file:reload()
end
local promise = OrgFile.load(filename):next(function(orgfile)
if orgfile then
self.all_files[filename] = orgfile
end
return orgfile
end)
return promise
end
---@return OrgFile | nil
function OrgFiles:load_file_sync(filename, timeout)
return self:load_file(filename):wait(timeout)
end
function OrgFiles:get(filename)
local file = self:load_file_sync(filename)
assert(file, 'File ' .. filename .. ' not found or is in invalid format')
return file
end
function OrgFiles:reload(filename)
self:load_file(filename):next(function(orgfile)
return orgfile
end)
end
---@param cursor? table (1, 0) indexed base position tuple
---@return OrgHeadline
function OrgFiles:get_closest_headline(cursor)
local file = self:load_file_sync(utils.current_file_path())
assert(file, 'Current file is not a valid org file')
local headline = file:get_closest_headline(cursor)
assert(headline, 'No headline found')
return headline
end
function OrgFiles:get_closest_listitem()
local node = ts_utils.closest_node(ts_utils.get_node_at_cursor(), 'listitem')
if node then
return Listitem:new(node, self:get_current_file())
end
return nil
end
---@param cursor? table (1, 0) indexed base position tuple
---@return OrgHeadline | nil
function OrgFiles:get_closest_headline_or_nil(cursor)
local file = self:load_file_sync(utils.current_file_path())
return file and file:get_closest_headline_or_nil(cursor)
end
---@param force? boolean
---@param timeout? number
---@return OrgFiles
function OrgFiles:load_sync(force, timeout)
return self:load(force):wait(timeout)
end
---@param title string
---@param exact? boolean
---@return OrgHeadline[]
function OrgFiles:find_headlines_by_title(title, exact)
local headlines = {}
for _, orgfile in ipairs(self:all()) do
for _, headline in ipairs(orgfile:find_headlines_by_title(title, exact)) do
table.insert(headlines, headline)
end
end
return headlines
end
---@param property_name string
---@param term string
function OrgFiles:find_headlines_with_property_matching(property_name, term)
local headlines = {}
for _, orgfile in ipairs(self:all()) do
for _, headline in ipairs(orgfile:find_headlines_with_property_matching(property_name, term)) do
table.insert(headlines, headline)
end
end
return headlines
end
---@param term string
---@param no_escape boolean
---@param search_extra_files boolean
---@return OrgHeadline[]
function OrgFiles:find_headlines_matching_search_term(term, no_escape, search_extra_files)
local headlines = {}
local ignore_archive_flag = search_extra_files
and vim.tbl_contains(config.org_agenda_text_search_extra_files, 'agenda-archives')
for _, orgfile in ipairs(self:all()) do
for _, headline in ipairs(orgfile:find_headlines_matching_search_term(term, no_escape, ignore_archive_flag)) do
table.insert(headlines, headline)
end
end
return headlines
end
---@param filename string
---@param action fun(...:OrgFile):any
function OrgFiles:update_file(filename, action)
local file = self:load_file_sync(filename)
if not file then
return Promise.resolve()
end
local is_same_file = filename == utils.current_file_path()
if is_same_file then
return Promise.resolve(action(file)):next(function(result)
vim.cmd(':silent! w')
return result
end)
end
local edit_file = utils.edit_file(filename)
edit_file.open()
return Promise.resolve(action(file)):next(function(result)
edit_file.close()
return result
end)
end
function OrgFiles:ensure_loaded()
if self.load_state == 'loaded' then
return true
end
vim.wait(5000, function()
return self.load_state == 'loaded'
end, 5)
end
---@private
function OrgFiles:_files()
local all_filenames = {}
local files = self.paths
if not files or files == '' or (type(files) == 'table' and vim.tbl_isempty(files)) then
return all_filenames
end
if type(files) ~= 'table' then
files = { files }
end
local all_files = vim.tbl_map(function(file)
return vim.tbl_map(function(path)
return vim.fn.resolve(path)
end, vim.fn.glob(vim.fn.fnamemodify(file, ':p'), false, true))
end, files)
all_files = utils.concat(vim.tbl_flatten(all_files), all_filenames, true)
return vim.tbl_filter(function(file)
local ext = vim.fn.fnamemodify(file, ':e')
return ext == 'org' or ext == 'org_archive'
end, all_files)
end
return OrgFiles