Skip to content

Commit

Permalink
refactor: make org_startup_indented responsible for Virtual indents
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
PriceHiller committed Jan 24, 2024
1 parent 8dcd91e commit 6686aea
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 58 deletions.
12 changes: 6 additions & 6 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,18 +270,18 @@ Possible values:
*type*: `boolean`<br />
*default value*: `false`<br />
Possible values:
* `true` - Enables indent mode on startup. Lines that follow headlines are indented to that headline level.
* `false` - Disables indent mode on startup. All lines start from the first column.
* `true` - Uses *Virtual* indents to align content visually. The indents are only visual, they are not saved to the file.
* `false` - Do not add any *Virtual* indentation.

This feature has no effect when enabled on Neovim verions < 0.10.0

#### **org_adapt_indentation**

*type*: `boolean`<br />
*default value*: `true`<br />
Possible values:
* `true` - Converts indents to *Virtual* indents. The indents are only visual, they are not saved to the file.
* `false` - Use real indents, indentation as seen is what is saved to the file.

This feature has no effect when enabled on Neovim verions < 0.10.0
* `true` - Use *hard* indents for content under headlines. Files will save with indents relative to headlines.
* `false` - Do not add any *hard* indents. Files will save without indentation relative to headlines.

#### **org_src_window_setup**
*type*: `string|function`<br />
Expand Down
5 changes: 2 additions & 3 deletions lua/orgmode/config/init.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local instance = {}
local VirtualIndent = require('orgmode.ui.virtual_indent')
local utils = require('orgmode.utils')
local fs = require('orgmode.utils.fs')
local defaults = require('orgmode.config.defaults')
Expand Down Expand Up @@ -149,7 +148,7 @@ function Config:_deprecation_notify(opts)
messages,
'"org_indent_mode" is deprecated in favor of "org_startup_indented". Check the documentation about the new option.'
)
opts.org_startup_indented = (opts.org_indent_mode == 'noindent')
opts.org_startup_indented = (opts.org_indent_mode == 'indent')
end

if #messages > 0 then
Expand Down Expand Up @@ -415,7 +414,7 @@ end
---@param amount number
---@return string
function Config:get_indent(amount)
if vim.b.org_indent_mode then
if self.org_adapt_indentation then
return string.rep(' ', amount)
end
return ''
Expand Down
8 changes: 2 additions & 6 deletions lua/orgmode/org/indent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local ts_utils = require('nvim-treesitter.ts_utils')
local query = nil

local function get_indent_pad(linenr)
if vim.b.org_indent_mode and not config.org_adapt_indentation then
if config.org_adapt_indentation then
local headline = require('orgmode.treesitter.headline').from_cursor({ linenr, 0 })
if not headline then
return 0
Expand Down Expand Up @@ -311,11 +311,7 @@ end
local function setup()
local v = vim.version()

if vim.version.lt({ v.major, v.minor, v.patch }, { 0, 10, 0 }) then
config.org_adapt_indentation = false
end

if config.org_startup_indented and config.org_adapt_indentation then
if config.org_startup_indented and not vim.version.lt({ v.major, v.minor, v.patch }, { 0, 10, 0 }) then
VirtualIndent:new():attach()
end
end
Expand Down
4 changes: 2 additions & 2 deletions lua/orgmode/parser/section.lua
Original file line number Diff line number Diff line change
Expand Up @@ -406,7 +406,7 @@ end
function Section:demote(amount, demote_child_sections, dryRun)
amount = amount or 1
demote_child_sections = demote_child_sections or false
local should_indent = vim.b.org_indent_mode
local should_indent = config.org_adapt_indentation
local lines = {}
local headline_line = string.rep('*', amount) .. self.line
table.insert(lines, headline_line)
Expand Down Expand Up @@ -444,7 +444,7 @@ end
function Section:promote(amount, promote_child_sections, dryRun)
amount = amount or 1
promote_child_sections = promote_child_sections or false
local should_dedent = vim.b.org_indent_mode
local should_dedent = config.org_adapt_indentation
local lines = {}
if self.level == 1 then
utils.echo_warning('Cannot demote top level heading.')
Expand Down
13 changes: 6 additions & 7 deletions lua/orgmode/treesitter/headline.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
local utils = require('orgmode.utils')
local ts_utils = require('nvim-treesitter.ts_utils')
local VirtualIndent = require('orgmode.ui.virtual_indent')
local tree_utils = require('orgmode.utils.treesitter')
local Date = require('orgmode.objects.date')
local Range = require('orgmode.parser.range')
Expand Down Expand Up @@ -69,12 +68,12 @@ function Headline:promote(amount, recursive)
if line:sub(1, 1) == '*' then
lines[i] = line:sub(1 + amount)
elseif vim.trim(line:sub(1, amount)) == '' then
if vim.b.org_indent_mode and config.org_adapt_indentation then
if config.org_adapt_indentation then
lines[i] = line:sub(1 + amount)
else
line, _ = line:gsub('^%s+', '')
local indent_amount = indent.indentexpr(start_line + i)
lines[i] = string.rep(' ', indent_amount) .. line
else
lines[i] = line:sub(1 + amount)
end
end
end
Expand All @@ -93,12 +92,12 @@ function Headline:demote(amount, recursive)
if line:sub(1, 1) == '*' then
lines[i] = string.rep('*', amount) .. line
else
if vim.b.org_indent_mode and config.org_adapt_indentation then
if config.org_adapt_indentation then
lines[i] = config:apply_indent(line, amount)
else
line, _ = line:gsub('^%s+', '')
local indent_amount = indent.indentexpr(start_line + i)
lines[i] = string.rep(' ', indent_amount) .. line
else
lines[i] = config:apply_indent(line, amount)
end
end
end
Expand Down
2 changes: 0 additions & 2 deletions tests/minimal_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,4 @@ require('nvim-treesitter.configs').setup({
require('orgmode').setup({
org_agenda_files = { base_root_path .. '/plenary/fixtures/*' },
org_default_notes_file = base_root_path .. '/plenary/fixtures/refile.org',
-- Many of the tests were originally written with indent mode being enabled in mind :/
org_startup_indented = true,
})
5 changes: 2 additions & 3 deletions tests/plenary/org/indent_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local config = require('orgmode.config')
local VirtualIndent = require('orgmode.ui.virtual_indent')
local helpers = require('tests.plenary.ui.helpers')

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

it('has the correct amount of virtual indentation', function()
if not VirtualIndent.enabled then
if not vim.b.org_indent_mode then
return
end

Expand Down
58 changes: 29 additions & 29 deletions tests/plenary/ui/mappings/headline_spec.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local helpers = require('tests.plenary.ui.helpers')
local VirtualIndent = require('orgmode.ui.virtual_indent')
local config = require('orgmode.config')

describe('Heading mappings', function()
after_each(function()
Expand Down Expand Up @@ -92,47 +92,47 @@ describe('Heading mappings', function()
}, vim.api.nvim_buf_get_lines(0, 2, 8, false))
vim.fn.cursor(3, 1)
local check
if VirtualIndent.enabled then
if config.org_adapt_indentation then
check = {
'** TODO Test orgmode',
'DEADLINE: <2021-07-21 Wed 22:02>',
' DEADLINE: <2021-07-21 Wed 22:02>',
'*** TODO [#A] Test orgmode level 2 :PRIVATE:',
'Some content for level 2',
' Some content for level 2',
'**** NEXT [#1] Level 3',
'Content Level 3',
' Content Level 3',
}
else
check = {
'** TODO Test orgmode',
' DEADLINE: <2021-07-21 Wed 22:02>',
'DEADLINE: <2021-07-21 Wed 22:02>',
'*** TODO [#A] Test orgmode level 2 :PRIVATE:',
' Some content for level 2',
'Some content for level 2',
'**** NEXT [#1] Level 3',
' Content Level 3',
'Content Level 3',
}
end
vim.cmd([[norm >s]])
assert.are.same(check, vim.api.nvim_buf_get_lines(0, 2, 8, false))

-- Support count
local check
if VirtualIndent.enabled then
if config.org_adapt_indentation then
check = {
'****** TODO Test orgmode',
'DEADLINE: <2021-07-21 Wed 22:02>',
' DEADLINE: <2021-07-21 Wed 22:02>',
'******* TODO [#A] Test orgmode level 2 :PRIVATE:',
'Some content for level 2',
' Some content for level 2',
'******** NEXT [#1] Level 3',
'Content Level 3',
' Content Level 3',
}
else
check = {
'****** TODO Test orgmode',
' DEADLINE: <2021-07-21 Wed 22:02>',
'DEADLINE: <2021-07-21 Wed 22:02>',
'******* TODO [#A] Test orgmode level 2 :PRIVATE:',
' Some content for level 2',
'Some content for level 2',
'******** NEXT [#1] Level 3',
' Content Level 3',
'Content Level 3',
}
end
vim.cmd([[norm 4>s]])
Expand Down Expand Up @@ -205,47 +205,47 @@ describe('Heading mappings', function()

-- Support count
local check
if VirtualIndent.enabled then
if config.org_adapt_indentation then
check = {
'*** TODO Test orgmode',
'DEADLINE: <2021-07-21 Wed 22:02>',
' DEADLINE: <2021-07-21 Wed 22:02>',
'**** TODO [#A] Test orgmode level 2 :PRIVATE:',
'Some content for level 2',
' Some content for level 2',
'***** NEXT [#1] Level 3',
'Content Level 3',
' Content Level 3',
}
else
check = {
'*** TODO Test orgmode',
' DEADLINE: <2021-07-21 Wed 22:02>',
'DEADLINE: <2021-07-21 Wed 22:02>',
'**** TODO [#A] Test orgmode level 2 :PRIVATE:',
' Some content for level 2',
'Some content for level 2',
'***** NEXT [#1] Level 3',
' Content Level 3',
'Content Level 3',
}
end
vim.cmd([[norm 2<s]])
assert.are.same(check, vim.api.nvim_buf_get_lines(0, 0, 6, false))

-- Handle overflow
local check
if VirtualIndent.enabled then
if config.org_adapt_indentation then
check = {
'* TODO Test orgmode',
'DEADLINE: <2021-07-21 Wed 22:02>',
' DEADLINE: <2021-07-21 Wed 22:02>',
'** TODO [#A] Test orgmode level 2 :PRIVATE:',
'Some content for level 2',
' Some content for level 2',
'*** NEXT [#1] Level 3',
'Content Level 3',
' Content Level 3',
}
else
check = {
'* TODO Test orgmode',
' DEADLINE: <2021-07-21 Wed 22:02>',
'DEADLINE: <2021-07-21 Wed 22:02>',
'** TODO [#A] Test orgmode level 2 :PRIVATE:',
' Some content for level 2',
'Some content for level 2',
'*** NEXT [#1] Level 3',
' Content Level 3',
'Content Level 3',
}
end
vim.cmd([[norm 5<s]])
Expand Down

0 comments on commit 6686aea

Please sign in to comment.