Skip to content

Commit

Permalink
feat: Allow for configuration of foldlevel similar to Emacs Orgmode
Browse files Browse the repository at this point in the history
  • Loading branch information
PriceHiller committed Sep 24, 2023
1 parent ac5f48c commit d979d92
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
9 changes: 9 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,15 @@ Applies to:
- agenda window
- capture window

#### **org_startup_folded**
*type*: `string`<br />
*default value*: `overview`<br />
How many headings and other foldable items should be shown when an org file is opened.<br />
Available options:
* `overview` - Only show top level elements (default)
* `content` - Only show the first two levels
* `showeverything` - A double line box
* `inherit` - Use the fold level set in Neovim's global `foldlevel` option

#### **org_todo_keyword_faces**
*type*: `table<string, string>`<br />
Expand Down
1 change: 0 additions & 1 deletion ftplugin/org.vim
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ setlocal foldmethod=expr
setlocal foldexpr=nvim_treesitter#foldexpr()
setlocal foldtext=OrgmodeFoldText()
setlocal formatexpr=OrgmodeFormatExpr()
setlocal foldlevel=0
setlocal omnifunc=OrgmodeOmni
setlocal commentstring=#\ %s
inoreabbrev <silent><buffer> :today: <C-R>=luaeval("require('orgmode.objects.date').today():to_wrapped_string(true)")<CR>
Expand Down
6 changes: 6 additions & 0 deletions lua/orgmode/config/defaults.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ local DefaultConfig = {
org_agenda_start_on_weekday = 1,
org_agenda_start_day = nil, -- start from today + this modifier
calendar_week_start_day = 1,
---@type string?
---| 'overview' Only show the top level elements
---| 'content' Show the first two levels
---| 'showeverything' Expand all folds by default
---| 'inherit' Use the foldlevel set in 'foldlevel'
org_startup_folded = 'overview',
org_capture_templates = {
t = {
description = 'Task',
Expand Down
23 changes: 17 additions & 6 deletions lua/orgmode/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ local instance = nil
---@field notifications Notifications
local Org = {}

function Org:new()
---@param config Config
function Org:new(config)
local data = { initialized = false }
setmetatable(data, self)
self.__index = self
data:setup_autocmds()
data:setup_autocmds(config)
return data
end

Expand Down Expand Up @@ -45,7 +46,8 @@ function Org:reload(file)
return self.files.reload(file)
end

function Org:setup_autocmds()
---@param config Config
function Org:setup_autocmds(config)
local org_augroup = vim.api.nvim_create_augroup('orgmode_nvim', { clear = true })
vim.api.nvim_create_autocmd('BufWritePost', {
pattern = { '*.org', '*.org_archive' },
Expand All @@ -59,6 +61,15 @@ function Org:setup_autocmds()
group = org_augroup,
callback = function()
require('orgmode').reload(vim.fn.expand('<afile>:p'))
if config.org_startup_folded == 'overview' then
vim.opt_local.foldlevel = 0
elseif config.org_startup_folded == 'content' then
vim.opt_local.foldlevel = 1
elseif config.org_startup_folded == 'showeverything' then
vim.opt_local.foldlevel = 99
elseif config.org_startup_folded == 'inherit' then
vim.opt_local.foldlevel = vim.opt.foldlevel:get()
end
end,
})
end
Expand Down Expand Up @@ -101,13 +112,13 @@ local function check_ts_grammar()
end, 200)
end

---@param opts? table
---@param opts? DefaultConfig
---@return Org
local function setup(opts)
opts = opts or {}
instance = Org:new()
check_ts_grammar()
local config = require('orgmode.config'):extend(opts)
instance = Org:new(config)
check_ts_grammar()
vim.defer_fn(function()
if config.notifications.enabled and #vim.api.nvim_list_uis() > 0 then
require('orgmode.parser.files').load(vim.schedule_wrap(function()
Expand Down

0 comments on commit d979d92

Please sign in to comment.