diff --git a/doc/orgmode.txt b/doc/orgmode.txt index c206fb498..228b27ddc 100644 --- a/doc/orgmode.txt +++ b/doc/orgmode.txt @@ -979,6 +979,9 @@ Templates have the following fields: capture content will be added. If a function is provided, it will be called during refile and must return the headline title as string. If no headline is specified, the content will be appended to the end of the file +- `prepend` (`boolean`) - captured items are by default added as the last item at + the specfied target, setting this field places captured items as the first item + at the target - `datetree (boolean | { time_prompt?: boolean, reversed?: boolean, tree_type: 'day' | 'month' | 'week' | 'custom' })` Create a date tree with current day in the target file and put the capture content there. - `true` - Create ascending datetree (newer dates go to end) with the current date diff --git a/docs/configuration.org b/docs/configuration.org index 63f0f9723..982266a72 100644 --- a/docs/configuration.org +++ b/docs/configuration.org @@ -920,6 +920,9 @@ Templates have the following fields: capture content will be added. If a function is provided, it will be called during refile and must return the headline title as string. If no headline is specified, the content will be appended to the end of the file +- =prepend= (=boolean=) --- captured items are by default added as the last item at + the specfied target, setting this field places captured items as the first item + at the target - =datetree (boolean | { time_prompt?: boolean, reversed?: boolean, tree_type: 'day' | 'month' | 'week' | 'custom' })= Create a [[https://orgmode.org/manual/Template-elements.html#FOOT84][date tree]] with current day in the target file and put the capture content there. - =true= - Create ascending datetree (newer dates go to end) with the current date diff --git a/lua/orgmode/capture/init.lua b/lua/orgmode/capture/init.lua index 9f89c837d..fd5681f46 100644 --- a/lua/orgmode/capture/init.lua +++ b/lua/orgmode/capture/init.lua @@ -167,9 +167,14 @@ function Capture:_refile_from_capture_buffer(opts) local target_line = -1 local destination_file = opts.destination_file local destination_headline = opts.destination_headline + local prepend = opts.template.prepend if destination_headline then - target_line = destination_headline:get_range().end_line + if prepend then + target_line = destination_headline:get_range().start_line + else + target_line = destination_headline:get_range().end_line + end end if opts.template.datetree then diff --git a/lua/orgmode/capture/template/init.lua b/lua/orgmode/capture/template/init.lua index 5bf95b3fd..52fe13255 100644 --- a/lua/orgmode/capture/template/init.lua +++ b/lua/orgmode/capture/template/init.lua @@ -84,6 +84,7 @@ local expansions = { ---@field target? string ---@field datetree? OrgCaptureTemplateDatetree ---@field headline? string|fun():string +---@field prepend? boolean ---@field regexp? string ---@field properties? OrgCaptureTemplateProperties ---@field subtemplates? table @@ -103,6 +104,7 @@ function Template:new(opts) vim.validate('target', opts.target, 'string', true) vim.validate('regexp', opts.regexp, 'string', true) vim.validate('headline', opts.headline, { 'string', 'function' }, true) + vim.validate('prepend', opts.prepend, 'boolean', true) vim.validate('properties', opts.properties, 'table', true) vim.validate('subtemplates', opts.subtemplates, 'table', true) vim.validate('datetree', opts.datetree, { 'boolean', 'table' }, true) @@ -113,6 +115,7 @@ function Template:new(opts) this.template = opts.template or '' this.target = opts.target or '' this.headline = opts.headline + this.prepend = opts.prepend this.properties = TemplateProperties:new(opts.properties) this.datetree = opts.datetree this.regexp = opts.regexp