Skip to content

Commit 5368c0b

Browse files
feat(indent)!: Add Org indent_mode command in favor of toggling vim.b.org_indent_mode (#932)
Previously, toggling virtual indentation on the buffer was done by manually changing value of the buffer variable `vim.b.org_indent_mode` to true or false respectively. This PR introduces an subcommand `indent_mode` to the global `Org` command which will toggle the virtual indentation on/off. This allows us to simplify and remove some of the code that might become deprecated in the future.
1 parent e2a85fe commit 5368c0b

File tree

8 files changed

+29
-114
lines changed

8 files changed

+29
-114
lines changed

docs/configuration.org

+3-3
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,9 @@ Possible values:
307307
- =true= - Uses /Virtual/ indents to align content visually. The indents are only visual, they are not saved to the file.
308308
- =false= - Do not add any /Virtual/ indentation.
309309

310-
You can toggle Virtual indents on the fly by setting =vim.b.org_indent_mode= to either =true= or =false= when in a org
311-
buffer. For example, if virtual indents were enabled in the current buffer then you could disable them immediately by
312-
setting ~vim.b.org_indent_mode = false~.
310+
You can toggle Virtual indents on the fly by executing command =:Org indent_mode= when in a org buffer.
311+
This additionally sets the buffer variable =vim.b.org_indent_mode= to =true= or =false=, depending on the current state.
312+
Value of this buffer variable is then used to determine behavior of few options below.
313313

314314
*** org_adapt_indentation
315315
:PROPERTIES:

ftplugin/org.lua

+7-8
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ vim.b.did_ftplugin = true
66

77
local config = require('orgmode.config')
88

9-
vim.b.org_bufnr = vim.api.nvim_get_current_buf()
10-
119
vim.treesitter.start()
1210

13-
config:setup_mappings('org', vim.b.org_bufnr)
14-
config:setup_mappings('text_objects', vim.b.org_bufnr)
11+
local bufnr = vim.api.nvim_get_current_buf()
12+
13+
config:setup_mappings('org', bufnr)
14+
config:setup_mappings('text_objects', bufnr)
1515
config:setup_foldlevel()
1616

1717
if config.org_startup_indented then
18-
vim.b.org_indent_mode = true
18+
require('orgmode.ui.virtual_indent'):new(bufnr):attach()
1919
end
20-
require('orgmode.org.indent').setup_virtual_indent()
2120

2221
vim.bo.modeline = false
2322
vim.opt_local.fillchars:append('fold: ')
@@ -56,7 +55,7 @@ for _, char in ipairs({ '*', '=', '/', '+', '~', '_' }) do
5655
end
5756

5857
if config.org_highlight_latex_and_related then
59-
vim.bo[vim.b.org_bufnr].syntax = 'ON'
58+
vim.bo[bufnr].syntax = 'ON'
6059
end
6160

6261
vim.b.undo_ftplugin = table.concat({
@@ -70,5 +69,5 @@ vim.b.undo_ftplugin = table.concat({
7069
'formatexpr<',
7170
'omnifunc<',
7271
'indentkeys<',
73-
'| unlet! b:org_bufnr b:org_tmp_edit_window',
72+
'| unlet! b:org_tmp_edit_window',
7473
}, ' ')

lua/orgmode/config/init.lua

-19
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ function Config:extend(opts)
4545
self.todo_keywords = nil
4646
self.priorities = nil
4747
opts = opts or {}
48-
self:_deprecation_notify(opts)
4948
if not self:_are_priorities_valid(opts) then
5049
opts.org_priority_highest = self.opts.org_priority_highest
5150
opts.org_priority_lowest = self.opts.org_priority_lowest
@@ -138,24 +137,6 @@ function Config:_are_priorities_valid(opts)
138137
return true
139138
end
140139

141-
function Config:_deprecation_notify(opts)
142-
local messages = {}
143-
if opts.org_indent_mode and type(opts.org_indent_mode) == 'string' then
144-
table.insert(
145-
messages,
146-
'"org_indent_mode" is deprecated in favor of "org_startup_indented". Check the documentation about the new option.'
147-
)
148-
opts.org_startup_indented = (opts.org_indent_mode == 'indent')
149-
end
150-
151-
if #messages > 0 then
152-
-- Schedule so it gets printed out once whole init.vim is loaded
153-
vim.schedule(function()
154-
utils.echo_warning(table.concat(messages, '\n'))
155-
end)
156-
end
157-
end
158-
159140
---@return number
160141
function Config:get_week_start_day_number()
161142
return utils.convert_from_isoweekday(1)

lua/orgmode/org/global.lua

+3
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ local build = function(orgmode)
9191
orgmode.links:store_link_to_headline(headline)
9292
return require('orgmode.utils').echo_info('Stored: ' .. headline:get_title())
9393
end,
94+
indent_mode = function()
95+
require('orgmode.ui.virtual_indent').toggle_buffer_indent_mode()
96+
end,
9497
}
9598

9699
_G.Org = OrgGlobal

lua/orgmode/org/indent.lua

-11
Original file line numberDiff line numberDiff line change
@@ -322,18 +322,7 @@ local function foldtext()
322322
return line .. config.org_ellipsis
323323
end
324324

325-
local function setup_virtual_indent()
326-
local virtualIndent = VirtualIndent:new()
327-
328-
if config.org_startup_indented or vim.b.org_indent_mode then
329-
return virtualIndent:attach()
330-
end
331-
332-
return virtualIndent:start_watch_org_indent()
333-
end
334-
335325
return {
336-
setup_virtual_indent = setup_virtual_indent,
337326
indentexpr = indentexpr,
338327
foldtext = foldtext,
339328
}

lua/orgmode/ui/virtual_indent.lua

+16-23
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
local tree_utils = require('orgmode.utils.treesitter')
2-
local dict_watcher = require('orgmode.utils.dict_watcher')
32
---@class OrgVirtualIndent
43
---@field private _ns_id number extmarks namespace id
54
---@field private _bufnr integer Buffer VirtualIndent is attached to
65
---@field private _attached boolean Whether or not VirtualIndent is attached for its buffer
76
---@field private _bufnrs table<integer, OrgVirtualIndent> Buffers with VirtualIndent attached
8-
---@field private _watcher_running boolean Whether or not VirtualIndent is reacting to `vim.b.org_indent_mode`
97
local VirtualIndent = {
108
_ns_id = vim.api.nvim_create_namespace('orgmode.ui.indent'),
119
_bufnrs = {},
@@ -23,13 +21,26 @@ function VirtualIndent:new(bufnr)
2321
end
2422
local this = setmetatable({
2523
_bufnr = bufnr,
26-
_watcher_running = false,
2724
_attached = false,
2825
}, self)
2926
self._bufnrs[bufnr] = this
3027
return this
3128
end
3229

30+
function VirtualIndent.toggle_buffer_indent_mode(bufnr)
31+
bufnr = bufnr or vim.api.nvim_get_current_buf()
32+
local instance = VirtualIndent:new(bufnr)
33+
local message = ''
34+
if vim.b[bufnr].org_indent_mode then
35+
message = 'disabled'
36+
instance:detach()
37+
else
38+
message = 'enabled'
39+
instance:attach()
40+
end
41+
require('orgmode.utils').echo_info('Org-Indent mode ' .. message .. ' in current buffer')
42+
end
43+
3344
function VirtualIndent:_delete_old_extmarks(start_line, end_line)
3445
local ok, old_extmarks = pcall(
3546
vim.api.nvim_buf_get_extmarks,
@@ -117,32 +128,12 @@ function VirtualIndent:set_indent(start_line, end_line, ignore_ts)
117128
end
118129
end
119130

120-
--- Make all VirtualIndent instances react to changes in `org_indent_mode`
121-
function VirtualIndent:start_watch_org_indent()
122-
if self._watcher_running then
123-
return
124-
end
125-
dict_watcher.watch_buffer_variable('org_indent_mode', function(indent_mode, _, buf_vars)
126-
local instance = self._bufnrs[buf_vars.org_bufnr]
127-
if not instance then
128-
return
129-
end
130-
local indent_mode_enabled = indent_mode.new or false
131-
if indent_mode_enabled then
132-
return instance:attach()
133-
end
134-
return instance:detach()
135-
end)
136-
self._watcher_running = true
137-
end
138-
139131
--- Enables virtual indentation in registered buffer
140132
function VirtualIndent:attach()
141133
if self._attached then
142134
return
143135
end
144136
self:set_indent(0, vim.api.nvim_buf_line_count(self._bufnr) - 1, true)
145-
self:start_watch_org_indent()
146137

147138
vim.api.nvim_buf_attach(self._bufnr, false, {
148139
on_lines = function(_, _, _, start_line, _, end_line)
@@ -163,6 +154,7 @@ function VirtualIndent:attach()
163154
end,
164155
})
165156
self._attached = true
157+
vim.b[self._bufnr].org_indent_mode = true
166158
end
167159

168160
function VirtualIndent:detach()
@@ -171,6 +163,7 @@ function VirtualIndent:detach()
171163
end
172164
self:_delete_old_extmarks(0, vim.api.nvim_buf_line_count(self._bufnr) - 1)
173165
self._attached = false
166+
vim.b[self._bufnr].org_indent_mode = false
174167
end
175168

176169
return VirtualIndent

lua/orgmode/utils/dict_watcher.lua

-32
This file was deleted.

plugin/orgmode.vim

-18
This file was deleted.

0 commit comments

Comments
 (0)