Skip to content

Commit 6fc7f68

Browse files
Add mapping for inserting plain timestamp under cursor. Closes #88.
1 parent aa69451 commit 6fc7f68

File tree

8 files changed

+65
-5
lines changed

8 files changed

+65
-5
lines changed

DOCS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,12 @@ Insert/Update deadline date.<br />
578578
#### **org_schedule**
579579
*mapped to*: `<Leader>ois`<br />
580580
Insert/Update scheduled date.<br />
581+
#### **org_time_stamp**
582+
*mapped to*: `<Leader>oi.`<br />
583+
Insert/Update date under cursor.<br />
584+
#### **org_time_stamp_inactive**
585+
*mapped to*: `<Leader>oi!`<br />
586+
Insert/Update inactive date under cursor.<br />
581587
#### **org_show_help**
582588
*mapped to*: `?`<br />
583589
Show help popup with mappings

doc/orgmode.txt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,9 @@ CONTENTS *orgmode-content
9090
1.2.4.29. outline_up_heading..............|orgmode-outline_up_heading|
9191
1.2.4.30. org_deadline..........................|orgmode-org_deadline|
9292
1.2.4.31. org_schedule..........................|orgmode-org_schedule|
93-
1.2.4.32. org_show_help........................|orgmode-org_show_help|
93+
1.2.4.32. org_time_stamp......................|orgmode-org_time_stamp|
94+
1.2.4.33. org_time_stamp_inactive....|orgmode-org_time_stamp_inactive|
95+
1.2.4.34. org_show_help........................|orgmode-org_show_help|
9496
1.3. Autocompletion...................................|orgmode-autocompletion|
9597
1.4. Abbreviations.....................................|orgmode-abbreviations|
9698
1.5. Colors...................................................|orgmode-colors|
@@ -799,6 +801,16 @@ ORG_SCHEDULE *orgmode-org_schedul
799801
mapped to: `<Leader>ois`
800802
Insert/Update scheduled date.
801803

804+
ORG_TIME_STAMP *orgmode-org_time_stamp*
805+
806+
mapped to: `<Leader>oi.`
807+
Insert/Update date under cursor.
808+
809+
ORG_TIME_STAMP_INACTIVE *orgmode-org_time_stamp_inactive*
810+
811+
mapped to: `<Leader>oi!`
812+
Insert/Update inactive date under cursor.
813+
802814
ORG_SHOW_HELP *orgmode-org_show_help*
803815

804816
mapped to: `?`

lua/orgmode/config/defaults.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ return {
9797
outline_up_heading = 'g{',
9898
org_deadline = '<Leader>oid',
9999
org_schedule = '<Leader>ois',
100+
org_time_stamp = '<Leader>oi.',
101+
org_time_stamp_inactive = '<Leader>oi!',
100102
org_show_help = '?',
101103
},
102104
},

lua/orgmode/config/mappings.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ return {
5353
outline_up_heading = { 'org_mappings.outline_up_heading' },
5454
org_deadline = { 'org_mappings.org_deadline' },
5555
org_schedule = { 'org_mappings.org_schedule' },
56+
org_time_stamp = { 'org_mappings.org_time_stamp' },
57+
org_time_stamp_inactive = { 'org_mappings.org_time_stamp', true },
5658
org_show_help = { 'org_mappings.show_help' },
5759
},
5860
}

lua/orgmode/objects/date.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,18 @@ function Date:to_string()
235235
return date
236236
end
237237

238+
---@param active boolean|nil
239+
---@return string
240+
function Date:to_wrapped_string(active)
241+
if type(active) ~= 'boolean' then
242+
active = self.active
243+
end
244+
local date = self:to_string()
245+
local open = active and '<' or '['
246+
local close = active and '>' or ']'
247+
return string.format('%s%s%s', open, date, close)
248+
end
249+
238250
---@return string
239251
function Date:format_time()
240252
if self.date_only then

lua/orgmode/objects/help.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ local helps = {
3434
{ key = 'outline_up_heading', description = 'Go to parent heading' },
3535
{ key = 'org_deadline', description = 'Insert/Update deadline date' },
3636
{ key = 'org_schedule', description = 'Insert/Update scheduled date' },
37+
{ key = 'org_time_stamp', description = 'Insert date under cursor' },
38+
{ key = 'org_time_stamp_inactive', description = 'Insert/Update inactive date under cursor' },
3739
{ key = 'org_show_help', description = 'Show this help' },
3840
},
3941
orgagenda = {

lua/orgmode/org/mappings.lua

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -418,8 +418,31 @@ function OrgMappings:org_schedule()
418418
Calendar.new({ callback = cb, date = scheduled_date or Date.today() }).open()
419419
end
420420

421+
---@param inactive boolean
422+
function OrgMappings:org_time_stamp(inactive)
423+
local date = self:_get_date_under_cursor()
424+
if date then
425+
local cb = function(new_date)
426+
self:_replace_date(new_date)
427+
end
428+
return Calendar.new({ callback = cb, date = date }).open()
429+
end
430+
431+
local date_start = self:_get_date_under_cursor(-1)
432+
433+
local new_cb = function(new_date)
434+
local date_string = new_date:to_wrapped_string(not inactive)
435+
if date_start then
436+
date_string = '--' .. date_string
437+
end
438+
vim.cmd(string.format('norm!i%s', date_string))
439+
end
440+
441+
return Calendar.new({ callback = new_cb, date = Date.today() }).open()
442+
end
443+
421444
---@param direction string
422-
---@param skip_fast_access boolean
445+
---@param use_fast_access boolean
423446
---@return string
424447
function OrgMappings:_change_todo_state(direction, use_fast_access)
425448
local item = Files.get_current_file():get_closest_headline()
@@ -476,9 +499,10 @@ function OrgMappings:_replace_date(date)
476499
end
477500

478501
---@return Date|nil
479-
function OrgMappings:_get_date_under_cursor()
502+
function OrgMappings:_get_date_under_cursor(col_offset)
503+
col_offset = col_offset or 0
480504
local item = Files.get_current_item()
481-
local col = vim.fn.col('.')
505+
local col = vim.fn.col('.') + col_offset
482506
local line = vim.fn.line('.')
483507
local dates = vim.tbl_filter(function(date)
484508
return date.range:is_in_range(line, col)

lua/orgmode/parser/headline.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ end
507507
---@return string
508508
function Headline:_add_planning_date(date, type, active)
509509
local planning = self.content[1]
510-
local date_string = string.format('%s%s%s', active and '<' or '[', date:to_string(), active and '>' or ']')
510+
local date_string = date:to_wrapped_string(active)
511511
if planning and planning:is_planning() then
512512
planning.line = string.format('%s %s: %s', planning.line, type, date_string)
513513
return vim.api.nvim_call_function('setline', {

0 commit comments

Comments
 (0)