Skip to content

Commit 6686aea

Browse files
committed
refactor: make org_startup_indented responsible for Virtual indents
This makes `org_adapt_indentation` responsible for raw indents. This is more in line with how Emacs works -- you can have *hard* indents with the virtual indentation in emacs by using `org_adapt_indentation`.
1 parent 8dcd91e commit 6686aea

File tree

8 files changed

+49
-58
lines changed

8 files changed

+49
-58
lines changed

DOCS.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -270,18 +270,18 @@ Possible values:
270270
*type*: `boolean`<br />
271271
*default value*: `false`<br />
272272
Possible values:
273-
* `true` - Enables indent mode on startup. Lines that follow headlines are indented to that headline level.
274-
* `false` - Disables indent mode on startup. All lines start from the first column.
273+
* `true` - Uses *Virtual* indents to align content visually. The indents are only visual, they are not saved to the file.
274+
* `false` - Do not add any *Virtual* indentation.
275+
276+
This feature has no effect when enabled on Neovim verions < 0.10.0
275277

276278
#### **org_adapt_indentation**
277279

278280
*type*: `boolean`<br />
279281
*default value*: `true`<br />
280282
Possible values:
281-
* `true` - Converts indents to *Virtual* indents. The indents are only visual, they are not saved to the file.
282-
* `false` - Use real indents, indentation as seen is what is saved to the file.
283-
284-
This feature has no effect when enabled on Neovim verions < 0.10.0
283+
* `true` - Use *hard* indents for content under headlines. Files will save with indents relative to headlines.
284+
* `false` - Do not add any *hard* indents. Files will save without indentation relative to headlines.
285285

286286
#### **org_src_window_setup**
287287
*type*: `string|function`<br />

lua/orgmode/config/init.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local instance = {}
2-
local VirtualIndent = require('orgmode.ui.virtual_indent')
32
local utils = require('orgmode.utils')
43
local fs = require('orgmode.utils.fs')
54
local defaults = require('orgmode.config.defaults')
@@ -149,7 +148,7 @@ function Config:_deprecation_notify(opts)
149148
messages,
150149
'"org_indent_mode" is deprecated in favor of "org_startup_indented". Check the documentation about the new option.'
151150
)
152-
opts.org_startup_indented = (opts.org_indent_mode == 'noindent')
151+
opts.org_startup_indented = (opts.org_indent_mode == 'indent')
153152
end
154153

155154
if #messages > 0 then
@@ -415,7 +414,7 @@ end
415414
---@param amount number
416415
---@return string
417416
function Config:get_indent(amount)
418-
if vim.b.org_indent_mode then
417+
if self.org_adapt_indentation then
419418
return string.rep(' ', amount)
420419
end
421420
return ''

lua/orgmode/org/indent.lua

+2-6
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ local ts_utils = require('nvim-treesitter.ts_utils')
44
local query = nil
55

66
local function get_indent_pad(linenr)
7-
if vim.b.org_indent_mode and not config.org_adapt_indentation then
7+
if config.org_adapt_indentation then
88
local headline = require('orgmode.treesitter.headline').from_cursor({ linenr, 0 })
99
if not headline then
1010
return 0
@@ -311,11 +311,7 @@ end
311311
local function setup()
312312
local v = vim.version()
313313

314-
if vim.version.lt({ v.major, v.minor, v.patch }, { 0, 10, 0 }) then
315-
config.org_adapt_indentation = false
316-
end
317-
318-
if config.org_startup_indented and config.org_adapt_indentation then
314+
if config.org_startup_indented and not vim.version.lt({ v.major, v.minor, v.patch }, { 0, 10, 0 }) then
319315
VirtualIndent:new():attach()
320316
end
321317
end

lua/orgmode/parser/section.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ end
406406
function Section:demote(amount, demote_child_sections, dryRun)
407407
amount = amount or 1
408408
demote_child_sections = demote_child_sections or false
409-
local should_indent = vim.b.org_indent_mode
409+
local should_indent = config.org_adapt_indentation
410410
local lines = {}
411411
local headline_line = string.rep('*', amount) .. self.line
412412
table.insert(lines, headline_line)
@@ -444,7 +444,7 @@ end
444444
function Section:promote(amount, promote_child_sections, dryRun)
445445
amount = amount or 1
446446
promote_child_sections = promote_child_sections or false
447-
local should_dedent = vim.b.org_indent_mode
447+
local should_dedent = config.org_adapt_indentation
448448
local lines = {}
449449
if self.level == 1 then
450450
utils.echo_warning('Cannot demote top level heading.')

lua/orgmode/treesitter/headline.lua

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
local utils = require('orgmode.utils')
22
local ts_utils = require('nvim-treesitter.ts_utils')
3-
local VirtualIndent = require('orgmode.ui.virtual_indent')
43
local tree_utils = require('orgmode.utils.treesitter')
54
local Date = require('orgmode.objects.date')
65
local Range = require('orgmode.parser.range')
@@ -69,12 +68,12 @@ function Headline:promote(amount, recursive)
6968
if line:sub(1, 1) == '*' then
7069
lines[i] = line:sub(1 + amount)
7170
elseif vim.trim(line:sub(1, amount)) == '' then
72-
if vim.b.org_indent_mode and config.org_adapt_indentation then
71+
if config.org_adapt_indentation then
72+
lines[i] = line:sub(1 + amount)
73+
else
7374
line, _ = line:gsub('^%s+', '')
7475
local indent_amount = indent.indentexpr(start_line + i)
7576
lines[i] = string.rep(' ', indent_amount) .. line
76-
else
77-
lines[i] = line:sub(1 + amount)
7877
end
7978
end
8079
end
@@ -93,12 +92,12 @@ function Headline:demote(amount, recursive)
9392
if line:sub(1, 1) == '*' then
9493
lines[i] = string.rep('*', amount) .. line
9594
else
96-
if vim.b.org_indent_mode and config.org_adapt_indentation then
95+
if config.org_adapt_indentation then
96+
lines[i] = config:apply_indent(line, amount)
97+
else
9798
line, _ = line:gsub('^%s+', '')
9899
local indent_amount = indent.indentexpr(start_line + i)
99100
lines[i] = string.rep(' ', indent_amount) .. line
100-
else
101-
lines[i] = config:apply_indent(line, amount)
102101
end
103102
end
104103
end

tests/minimal_init.lua

-2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,4 @@ require('nvim-treesitter.configs').setup({
134134
require('orgmode').setup({
135135
org_agenda_files = { base_root_path .. '/plenary/fixtures/*' },
136136
org_default_notes_file = base_root_path .. '/plenary/fixtures/refile.org',
137-
-- Many of the tests were originally written with indent mode being enabled in mind :/
138-
org_startup_indented = true,
139137
})

tests/plenary/org/indent_spec.lua

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local config = require('orgmode.config')
2-
local VirtualIndent = require('orgmode.ui.virtual_indent')
32
local helpers = require('tests.plenary.ui.helpers')
43

54
-- Helper assert function.
@@ -67,7 +66,7 @@ local function test_full_reindent()
6766
helpers.load_file_content(unformatted_file)
6867
vim.cmd([[silent norm 0gg=G]])
6968
local expected
70-
if config.org_startup_indented and not VirtualIndent.enabled then
69+
if config.org_adapt_indentation then
7170
expected = {
7271
'* TODO First task',
7372
' SCHEDULED: <1970-01-01 Thu>',
@@ -269,7 +268,7 @@ describe('with "indent" and "VirtualIndent" is enabled', function()
269268
end)
270269

271270
it('has the correct amount of virtual indentation', function()
272-
if not VirtualIndent.enabled then
271+
if not vim.b.org_indent_mode then
273272
return
274273
end
275274

tests/plenary/ui/mappings/headline_spec.lua

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
local helpers = require('tests.plenary.ui.helpers')
2-
local VirtualIndent = require('orgmode.ui.virtual_indent')
2+
local config = require('orgmode.config')
33

44
describe('Heading mappings', function()
55
after_each(function()
@@ -92,47 +92,47 @@ describe('Heading mappings', function()
9292
}, vim.api.nvim_buf_get_lines(0, 2, 8, false))
9393
vim.fn.cursor(3, 1)
9494
local check
95-
if VirtualIndent.enabled then
95+
if config.org_adapt_indentation then
9696
check = {
9797
'** TODO Test orgmode',
98-
'DEADLINE: <2021-07-21 Wed 22:02>',
98+
' DEADLINE: <2021-07-21 Wed 22:02>',
9999
'*** TODO [#A] Test orgmode level 2 :PRIVATE:',
100-
'Some content for level 2',
100+
' Some content for level 2',
101101
'**** NEXT [#1] Level 3',
102-
'Content Level 3',
102+
' Content Level 3',
103103
}
104104
else
105105
check = {
106106
'** TODO Test orgmode',
107-
' DEADLINE: <2021-07-21 Wed 22:02>',
107+
'DEADLINE: <2021-07-21 Wed 22:02>',
108108
'*** TODO [#A] Test orgmode level 2 :PRIVATE:',
109-
' Some content for level 2',
109+
'Some content for level 2',
110110
'**** NEXT [#1] Level 3',
111-
' Content Level 3',
111+
'Content Level 3',
112112
}
113113
end
114114
vim.cmd([[norm >s]])
115115
assert.are.same(check, vim.api.nvim_buf_get_lines(0, 2, 8, false))
116116

117117
-- Support count
118118
local check
119-
if VirtualIndent.enabled then
119+
if config.org_adapt_indentation then
120120
check = {
121121
'****** TODO Test orgmode',
122-
'DEADLINE: <2021-07-21 Wed 22:02>',
122+
' DEADLINE: <2021-07-21 Wed 22:02>',
123123
'******* TODO [#A] Test orgmode level 2 :PRIVATE:',
124-
'Some content for level 2',
124+
' Some content for level 2',
125125
'******** NEXT [#1] Level 3',
126-
'Content Level 3',
126+
' Content Level 3',
127127
}
128128
else
129129
check = {
130130
'****** TODO Test orgmode',
131-
' DEADLINE: <2021-07-21 Wed 22:02>',
131+
'DEADLINE: <2021-07-21 Wed 22:02>',
132132
'******* TODO [#A] Test orgmode level 2 :PRIVATE:',
133-
' Some content for level 2',
133+
'Some content for level 2',
134134
'******** NEXT [#1] Level 3',
135-
' Content Level 3',
135+
'Content Level 3',
136136
}
137137
end
138138
vim.cmd([[norm 4>s]])
@@ -205,47 +205,47 @@ describe('Heading mappings', function()
205205

206206
-- Support count
207207
local check
208-
if VirtualIndent.enabled then
208+
if config.org_adapt_indentation then
209209
check = {
210210
'*** TODO Test orgmode',
211-
'DEADLINE: <2021-07-21 Wed 22:02>',
211+
' DEADLINE: <2021-07-21 Wed 22:02>',
212212
'**** TODO [#A] Test orgmode level 2 :PRIVATE:',
213-
'Some content for level 2',
213+
' Some content for level 2',
214214
'***** NEXT [#1] Level 3',
215-
'Content Level 3',
215+
' Content Level 3',
216216
}
217217
else
218218
check = {
219219
'*** TODO Test orgmode',
220-
' DEADLINE: <2021-07-21 Wed 22:02>',
220+
'DEADLINE: <2021-07-21 Wed 22:02>',
221221
'**** TODO [#A] Test orgmode level 2 :PRIVATE:',
222-
' Some content for level 2',
222+
'Some content for level 2',
223223
'***** NEXT [#1] Level 3',
224-
' Content Level 3',
224+
'Content Level 3',
225225
}
226226
end
227227
vim.cmd([[norm 2<s]])
228228
assert.are.same(check, vim.api.nvim_buf_get_lines(0, 0, 6, false))
229229

230230
-- Handle overflow
231231
local check
232-
if VirtualIndent.enabled then
232+
if config.org_adapt_indentation then
233233
check = {
234234
'* TODO Test orgmode',
235-
'DEADLINE: <2021-07-21 Wed 22:02>',
235+
' DEADLINE: <2021-07-21 Wed 22:02>',
236236
'** TODO [#A] Test orgmode level 2 :PRIVATE:',
237-
'Some content for level 2',
237+
' Some content for level 2',
238238
'*** NEXT [#1] Level 3',
239-
'Content Level 3',
239+
' Content Level 3',
240240
}
241241
else
242242
check = {
243243
'* TODO Test orgmode',
244-
' DEADLINE: <2021-07-21 Wed 22:02>',
244+
'DEADLINE: <2021-07-21 Wed 22:02>',
245245
'** TODO [#A] Test orgmode level 2 :PRIVATE:',
246-
' Some content for level 2',
246+
'Some content for level 2',
247247
'*** NEXT [#1] Level 3',
248-
' Content Level 3',
248+
'Content Level 3',
249249
}
250250
end
251251
vim.cmd([[norm 5<s]])

0 commit comments

Comments
 (0)