Skip to content

Commit

Permalink
Add support for LOGGING property
Browse files Browse the repository at this point in the history
  • Loading branch information
chuck-flowers committed Sep 19, 2023
1 parent 449add6 commit f204e57
Showing 1 changed file with 46 additions and 12 deletions.
58 changes: 46 additions & 12 deletions lua/orgmode/org/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ local ts = require('orgmode.treesitter.compat')
local ts_table = require('orgmode.treesitter.table')
local EventManager = require('orgmode.events')
local Promise = require('orgmode.utils.promise')
local TodoConfig = require('orgmode.parser.todo-config')
local events = EventManager.event

---@class OrgMappings
Expand Down Expand Up @@ -377,6 +378,8 @@ end
function OrgMappings:_todo_change_state(direction)
local headline = ts_org.closest_headline()
local _, old_state, was_done = headline:todo()
--- @cast old_state -nil
--- @cast was_done -nil
local changed = self:_change_todo_state(direction, true)
if not changed then
return
Expand All @@ -394,9 +397,37 @@ function OrgMappings:_todo_change_state(direction)
return dispatchEvent()
end

local log_note = config.org_log_done == 'note'
local log_time = config.org_log_done == 'time'
local should_log_time = log_note or log_time
local new_state = item.todo_keyword.value

-- Determine which configuration to use
local global_config = config.org_log_done

--- @type nil | false | 'time' | 'note'
local section_config
local logging_prop = item.properties.items.logging
if logging_prop == 'nil' then
section_config = false
elseif logging_prop ~= nil then
local todoConfig = TodoConfig:parse(logging_prop)
if todoConfig ~= nil then
section_config = todoConfig:get_logging_behavior(old_state, new_state)
else
-- TODO: Report invalid config?
section_config = nil
end
else
section_config = nil
end

-- Use the most locally available log config
--- @type false | 'time' | 'note'
local log_config
if section_config ~= nil then
log_config = section_config
else
log_config = global_config
end

local indent = config:get_indent(headline:level() + 1)

local get_note = function(note)
Expand All @@ -414,11 +445,12 @@ function OrgMappings:_todo_change_state(direction)

local repeater_dates = item:get_repeater_dates()
if #repeater_dates == 0 then
if should_log_time and item:is_done() and not was_done then
-- If going from "not done" to "done", set the closed date and add the note/time
if log_config ~= false and item:is_done() and not was_done then
headline:set_closed_date()
item = Files.get_closest_headline()

if log_note then
if log_config == 'note' then
dispatchEvent()
return self.capture.closing_note:open():next(function(note)
local valid_note = get_note(note)
Expand All @@ -429,7 +461,9 @@ function OrgMappings:_todo_change_state(direction)
end)
end
end
if should_log_time and not item:is_done() and was_done then

-- If going from "done" to "not done", remove the close date
if log_config ~= false and not item:is_done() and was_done then
headline:remove_closed_date()
end
return dispatchEvent()
Expand All @@ -441,19 +475,19 @@ function OrgMappings:_todo_change_state(direction)

self:_change_todo_state('reset')
local state_change = {
string.format('%s- State "%s" from "%s" [%s]', indent, item.todo_keyword.value, old_state, Date.now():to_string()),
string.format('%s- State "%s" from "%s" [%s]', indent, new_state, old_state, Date.now():to_string()),
}

dispatchEvent()
return Promise.resolve()
:next(function()
if not log_note then
if log_config == 'time' then
return state_change
elseif log_config == 'note' then
return self.capture.closing_note:open():next(function(closing_note)
return get_note(closing_note)
end)
end

return self.capture.closing_note:open():next(function(closing_note)
return get_note(closing_note)
end)
end)
:next(function(note)
headline:set_property('LAST_REPEAT', Date.now():to_wrapped_string(false))
Expand Down

0 comments on commit f204e57

Please sign in to comment.