Skip to content

Commit f560d3c

Browse files
Merge pull request #23 from chuwy/feature/todo-plus-tags
2 parents 54b17c1 + e359660 commit f560d3c

File tree

2 files changed

+49
-13
lines changed

2 files changed

+49
-13
lines changed

lua/orgmode/agenda/init.lua

+18-5
Original file line numberDiff line numberDiff line change
@@ -306,17 +306,29 @@ function Agenda:search(clear_search)
306306
end
307307

308308
-- TODO: Add PROP/TODO Query
309-
function Agenda:tags(clear_search)
310-
if clear_search then
309+
function Agenda:tags(opts)
310+
opts = opts or {}
311+
local tags = opts.tags
312+
313+
if opts.clear_search then
311314
self.last_search = ''
312315
end
313-
local tags = vim.fn.input('Match: ', self.last_search, 'customlist,v:lua.orgmode.autocomplete_agenda_filter_tags')
316+
317+
if not tags then
318+
tags = vim.fn.input('Match: ', self.last_search, 'customlist,v:lua.orgmode.autocomplete_agenda_filter_tags')
319+
end
314320
if vim.trim(tags) == '' then
315321
return utils.echo_warning('Invalid tag.')
316322
end
317323
local headlines = {}
318324
for _, orgfile in ipairs(Files.all()) do
319-
for _, headline in ipairs(orgfile:get_headlines_with_tags(tags)) do
325+
local headlines_filtered
326+
if opts.todo_only then
327+
headlines_filtered = orgfile:get_unfinished_todo_entries_with_tags(tags)
328+
else
329+
headlines_filtered = orgfile:get_headlines_with_tags(tags)
330+
end
331+
for _, headline in ipairs(headlines_filtered) do
320332
table.insert(headlines, headline)
321333
end
322334
end
@@ -376,7 +388,8 @@ function Agenda:prompt()
376388
{ label = '', separator = '-', length = 34 },
377389
{ label = 'Agenda for current week or day', key = 'a', action = function() return self:agenda() end },
378390
{ label = 'List of all TODO entries', key = 't', action = function() return self:todos() end },
379-
{ label = 'Match a TAGS query', key = 'm', action = function() return self:tags(true) end },
391+
{ label = 'Match a TAGS query', key = 'm', action = function() return self:tags({clear_search = true, tags = nil, todo_only = false}) end },
392+
{ label = 'Like m, but only TODO entries', key = 'M', action = function() return self:tags({clear_search = true, tags = nil, todo_only = true}) end },
380393
{ label = 'Search for keywords', key = 's', action = function() return self:search(true) end },
381394
{ label = 'Quit', key = 'q' },
382395
{ label = '', separator = ' ', length = 1 },

lua/orgmode/parser/root.lua

+31-8
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,26 @@ function Root:get_opened_unfinished_headlines()
177177
end, self.items)
178178
end
179179

180+
function Root:get_unfinished_todo_entries_with_tags(tags)
181+
if self.is_archive_file then return {} end
182+
local taglist = Root:_parse_taglist(tags)
183+
184+
return vim.tbl_filter(function(item)
185+
if item.type ~= Types.HEADLINE or item:is_archived() or not item:is_todo() or not item.tags or #item.tags == 0 then
186+
return false
187+
end
188+
189+
local has_tag = true
190+
for _, tag in ipairs(taglist) do
191+
if not vim.tbl_contains(item.tags, tag) then
192+
has_tag = false
193+
break
194+
end
195+
end
196+
return has_tag
197+
end, self.items)
198+
end
199+
180200
function Root:get_unfinished_todo_entries()
181201
if self.is_archive_file then return {} end
182202

@@ -211,14 +231,7 @@ end
211231

212232
function Root:get_headlines_with_tags(tags)
213233
if self.is_archive_file then return {} end
214-
215-
local taglist = vim.tbl_map(function(tag)
216-
return vim.trim(tag)
217-
end , vim.split(tags, '+', true))
218-
219-
taglist = vim.tbl_filter(function(t)
220-
return t ~= ''
221-
end, taglist)
234+
local taglist = Root:_parse_taglist(tags)
222235

223236
if #taglist == 0 then return {} end
224237

@@ -286,4 +299,14 @@ function Root:get_archive_file_location()
286299
return Config:parse_archive_location(self.file)
287300
end
288301

302+
function Root:_parse_taglist(tags)
303+
local taglist = vim.tbl_map(function(tag)
304+
return vim.trim(tag)
305+
end , vim.split(tags, '+', true))
306+
307+
return vim.tbl_filter(function(t)
308+
return t ~= ''
309+
end, taglist)
310+
end
311+
289312
return Root

0 commit comments

Comments
 (0)