Skip to content

Commit

Permalink
feat: remove buffer empty lines
Browse files Browse the repository at this point in the history
  • Loading branch information
danilshvalov committed Sep 24, 2023
1 parent 56c89ec commit 5ad97c6
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
6 changes: 3 additions & 3 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,9 +409,9 @@ Templates have the following fields:
* `target` (`string?`) — name of the file to which the capture content will be added. If the target is not specified, the content will be added to the [`org_default_notes_file`](#orgdefaultnotesfile) file
* `headline` (`string?`) — title of the headline after which the capture content will be added. If no headline is specified, the content will be appended to the end of the file
* `properties` (`table?`):
* `empty_lines` (`table?`):
* `before` (`integer`) — add empty lines to the beginning of the content
* `after` (`integer`) — add empty lines to the end of the content
* `empty_lines` (`table|number?`) — if the value is a number, then empty lines are added before and after the content. If the value is a table, then the following fields are expected:
* `before` (`integer?`) — add empty lines to the beginning of the content
* `after` (`integer?`) — add empty lines to the end of the content

Example:<br />
```lua
Expand Down
29 changes: 29 additions & 0 deletions lua/orgmode/capture/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ function Capture:refile_to_headline(opts)
return true
end

---@param opts CaptureOpts
local function add_empty_lines(opts)
local empty_lines = opts.template.properties.empty_lines

Expand All @@ -297,12 +298,38 @@ local function add_empty_lines(opts)
end
end

---@param opts CaptureOpts
local function apply_properties(opts)
if opts.template then
add_empty_lines(opts)
end
end

local function remove_buffer_empty_lines(opts)
local range = opts.range
local start_line = range.end_line - 1
local end_line = range.end_line

local is_line_empty = function(row)
local line = vim.api.nvim_buf_get_lines(0, row, row + 1, true)[1]
line = vim.trim(line)
return #line == 0
end

while start_line >= 0 and is_line_empty(start_line) do
start_line = start_line - 1
end
start_line = start_line + 1

local line_count = vim.api.nvim_buf_line_count(0)
while end_line < line_count and is_line_empty(end_line) do
end_line = end_line + 1
end

range.start_line = start_line
range.end_line = end_line
end

---@private
---@param opts CaptureOpts
---@return boolean
Expand Down Expand Up @@ -336,6 +363,8 @@ function Capture:_refile_to(opts)
})
end

remove_buffer_empty_lines(opts)

local range = opts.range
vim.api.nvim_buf_set_lines(0, range.start_line, range.end_line, false, opts.lines)

Expand Down
9 changes: 7 additions & 2 deletions lua/orgmode/capture/template.lua
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ function TemplateProperties:new(opts)
opts = opts or {}

vim.validate({
empty_lines = { opts.empty_lines, 'table', true },
empty_lines = { opts.empty_lines, { 'table', 'number' }, true },
})

local empty_lines = opts.empty_lines or {}
if type(empty_lines) == 'number' then
empty_lines = { before = empty_lines, after = empty_lines }
end

local this = {}
this.empty_lines = TemplateEmptyLines:new(opts.empty_lines or {})
this.empty_lines = TemplateEmptyLines:new(empty_lines)

setmetatable(this, self)
self.__index = self
Expand Down

0 comments on commit 5ad97c6

Please sign in to comment.