Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit ab87a9b

Browse files
feat(folds): Add org_cycle_separator_lines
Closes #711
1 parent 98bf44d commit ab87a9b

File tree

7 files changed

+79
-110
lines changed

7 files changed

+79
-110
lines changed

docs/configuration.org

+39
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,45 @@ require('orgmode').setup({
436436
Number of minutes to increase/decrease when using
437437
[[#org_timestamp_up][org_timestamp_up]]/[[#org_timestamp_down][org_timestamp_down]]
438438

439+
*** =org_cycle_separator_lines=
440+
:PROPERTIES:
441+
:CUSTOM_ID: org_cycle_separator_lines
442+
:END:
443+
- Type =number=
444+
- Default: =2=
445+
Minimum number of empty lines needed at the end of the headline to show a single empty line when headline is folded.
446+
447+
For example, given this structure:
448+
#+begin_src org
449+
* One empty space headline
450+
Content
451+
452+
* Two empty space headline
453+
Content
454+
455+
456+
* Three empty space headline
457+
Content
458+
459+
460+
461+
* Last headline
462+
Content
463+
#+end_src
464+
465+
When folded, it will appear like this:
466+
#+begin_src org
467+
* One empty space headline ...
468+
* Two empty space headline ...
469+
470+
* Three empty space headline ...
471+
472+
* Last headline ...
473+
#+end_src
474+
475+
When value is =0=, all empty lines are folded together with headline.
476+
477+
Cannot be negative.
439478
*** =org_blank_before_new_entry=
440479
:PROPERTIES:
441480
:CUSTOM_ID: org_blank_before_new_entry

ftplugin/org.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ require('orgmode.org.indent').setup_virtual_indent()
2323
vim.bo.modeline = false
2424
vim.opt_local.fillchars:append('fold: ')
2525
vim.opt_local.foldmethod = 'expr'
26-
vim.opt_local.foldexpr = 'v:lua.require("orgmode.org.fold").foldexpr()'
26+
vim.opt_local.foldexpr = 'v:lua.vim.treesitter.foldexpr()'
2727
if utils.has_version_10() and config.ui.folds.colored then
2828
vim.opt_local.foldtext = ''
2929
else

lua/orgmode/config/_meta.lua

+1
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,7 @@
226226
---@field org_indent_mode_turns_off_org_adapt_indentation? boolean If true, turning on indent mode will turn off `org_adapt_indentation`. Default: true
227227
---@field org_indent_mode_turns_on_hiding_stars? boolean If true, turning on indent mode will hide leading stars. Default: true
228228
---@field org_time_stamp_rounding_minutes? number Rounding minutes for time stamps. Default: 5
229+
---@field org_cycle_separator_lines? number Min number of spaces are needed at the end of headline to show empty line between folds. Default: 2
229230
---@field org_blank_before_new_entry? { heading: boolean, plain_list_item: boolean } Should blank line be prepended. Default: { heading = true, plain_list_item = false }
230231
---@field org_src_window_setup? string | fun() How to open "special edit" buffer window. Default: 'top 16new'
231232
---@field org_edit_src_content_indentation? number Addditional ndentation number applied when editing a SRC block through special edit. Default: 0

lua/orgmode/config/defaults.lua

+1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ local DefaultConfig = {
5454
org_indent_mode_turns_off_org_adapt_indentation = true,
5555
org_indent_mode_turns_on_hiding_stars = true,
5656
org_time_stamp_rounding_minutes = 5,
57+
org_cycle_separator_lines = 2,
5758
org_blank_before_new_entry = {
5859
heading = true,
5960
plain_list_item = false,

lua/orgmode/config/init.lua

+36
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,42 @@ function Config:setup_ts_predicates()
374374
return false
375375
end, { force = true, all = false })
376376

377+
local org_cycle_separator_lines = math.max(self.opts.org_cycle_separator_lines, 0)
378+
379+
vim.treesitter.query.add_directive('org-set-fold-offset!', function(match, _, bufnr, pred, metadata)
380+
if org_cycle_separator_lines == 0 then
381+
return
382+
end
383+
---@type TSNode | nil
384+
local capture_id = pred[2]
385+
local section_node = match[capture_id]
386+
if not capture_id or not section_node or section_node:type() ~= 'section' then
387+
return
388+
end
389+
if not metadata[capture_id] then
390+
metadata[capture_id] = {}
391+
end
392+
local range = metadata[capture_id].range or { section_node:range() }
393+
local start_row = range[1]
394+
local end_row = range[3]
395+
396+
local empty_lines = 0
397+
while end_row > start_row do
398+
local line = vim.api.nvim_buf_get_lines(bufnr, end_row - 1, end_row, false)[1]
399+
if vim.trim(line) ~= '' then
400+
break
401+
end
402+
empty_lines = empty_lines + 1
403+
end_row = end_row - 1
404+
end
405+
406+
if empty_lines < org_cycle_separator_lines then
407+
return
408+
end
409+
range[3] = range[3] - 1
410+
metadata[capture_id].range = range
411+
end, { force = true, all = false })
412+
377413
vim.treesitter.query.add_predicate('org-is-valid-priority?', function(match, _, source, predicate)
378414
local node = match[predicate[2]]
379415
local type = predicate[3]

lua/orgmode/org/fold.lua

-108
This file was deleted.

queries/org/folds.scm

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
(drawer)
55
(property_drawer)
66
(block)
7-
] @fold)
7+
] @fold (#org-set-fold-offset! @fold))

0 commit comments

Comments
 (0)