From 47b5dcdd530b3490b5a6e7b3b9b8e26bd6e48868 Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Wed, 24 Jan 2024 17:03:19 -0600 Subject: [PATCH] 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`. --- DOCS.md | 12 ++--- lua/orgmode/config/init.lua | 1 - lua/orgmode/org/indent.lua | 8 +-- lua/orgmode/treesitter/headline.lua | 13 +++-- tests/plenary/org/indent_spec.lua | 5 +- tests/plenary/ui/mappings/headline_spec.lua | 58 ++++++++++----------- 6 files changed, 45 insertions(+), 52 deletions(-) diff --git a/DOCS.md b/DOCS.md index 70d7a1117..bc10ca3a0 100644 --- a/DOCS.md +++ b/DOCS.md @@ -270,18 +270,18 @@ Possible values: *type*: `boolean`
*default value*: `false`
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`
*default value*: `true`
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`
diff --git a/lua/orgmode/config/init.lua b/lua/orgmode/config/init.lua index e2b8b7b67..57604ac70 100644 --- a/lua/orgmode/config/init.lua +++ b/lua/orgmode/config/init.lua @@ -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') diff --git a/lua/orgmode/org/indent.lua b/lua/orgmode/org/indent.lua index 0f9e5ead8..3f183de88 100644 --- a/lua/orgmode/org/indent.lua +++ b/lua/orgmode/org/indent.lua @@ -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 @@ -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 diff --git a/lua/orgmode/treesitter/headline.lua b/lua/orgmode/treesitter/headline.lua index b52e0419a..df68908d0 100644 --- a/lua/orgmode/treesitter/headline.lua +++ b/lua/orgmode/treesitter/headline.lua @@ -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') @@ -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 @@ -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 diff --git a/tests/plenary/org/indent_spec.lua b/tests/plenary/org/indent_spec.lua index 15ce83f21..3d7e5efc7 100644 --- a/tests/plenary/org/indent_spec.lua +++ b/tests/plenary/org/indent_spec.lua @@ -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. @@ -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>', @@ -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 diff --git a/tests/plenary/ui/mappings/headline_spec.lua b/tests/plenary/ui/mappings/headline_spec.lua index 12c10648a..522960dd5 100644 --- a/tests/plenary/ui/mappings/headline_spec.lua +++ b/tests/plenary/ui/mappings/headline_spec.lua @@ -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() @@ -92,23 +92,23 @@ 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]]) @@ -116,23 +116,23 @@ 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 4>s]]) @@ -205,23 +205,23 @@ 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', + ' 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