nvim-orgmode / orgmode

Orgmode clone written in Lua for Neovim 0.9+.

Home Page:https://nvim-orgmode.github.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

"Add new headline" moves cursor one line too much

swnakamura opened this issue · comments

Describe the bug

"Add new headline after current subtree" or "Add new TODO headline" inserts a new heading or list item with the same level as the current line, and moves the cursor to the inserted heading. When another heading exists later, however, this shortcut seems to move the cursor one line too much, leaving it on the next heading.

Steps to reproduce

  1. Make a file with two or more heading, like below:
* Test heading
* Test heading 2
  1. Place the cursor to the first line.
  2. Run "insert heading" by pressing <leader>oih. A heading is inserted and the file will look like below:
* Test heading

* 
* Test heading 2
  1. The cursor is, however, on the 4th line (the line with * Test heading 2).

Expected behavior

The cursor should be on the 3rd line to start editing the inserted heading. The same issue happens when a TODO headline is inserted with <leader>oit.

Emacs functionality

https://orgmode.org/manual/Structure-Editing.html

Minimal init.lua

local tmp_dir = vim.env.TMPDIR or vim.env.TMP or vim.env.TEMP or '/tmp'
local nvim_root = tmp_dir .. '/nvim_orgmode'
local lazy_root = nvim_root .. '/lazy'
local lazypath = lazy_root .. '/lazy.nvim'

-- Install lazy.nvim if not already installed
if not vim.loop.fs_stat(lazypath) then
  vim.fn.system({
    'git',
    'clone',
    '--filter=blob:none',
    'https://github.com/folke/lazy.nvim.git',
    '--branch=stable', -- latest stable release
    lazypath,
  })
end
vim.opt.rtp:prepend(lazypath)

require('lazy').setup({
  {
    'nvim-orgmode/orgmode',
    dependencies = {
      { 'nvim-treesitter/nvim-treesitter', lazy = true },
    },
    config = function()
      -- Load treesitter grammar for org
      require('orgmode').setup_ts_grammar()

      -- Setup treesitter
      require('nvim-treesitter.configs').setup({
        highlight = {
          enable = true,
        },
        ensure_installed = { 'org' },
      })

      -- Setup orgmode
      require('orgmode').setup()
    end,
  },
}, {
  root = lazy_root,
  lockfile = nvim_root .. '/lazy.json',
  install = {
    missing = false,
  },
})

require('lazy').sync({
  wait = true,
  show = false,
})

Screenshots and recordings

before.mp4

I could fix this problem by doing below

diff --git a/lua/orgmode/org/mappings.lua b/lua/orgmode/org/mappings.lua
index 94282ab..8b5b840 100644
--- a/lua/orgmode/org/mappings.lua
+++ b/lua/orgmode/org/mappings.lua
@@ -676,7 +676,7 @@ function OrgMappings:insert_heading_respect_content(suffix)
   else
     local line = config:respect_blank_before_new_entry({ string.rep('*', item:get_level()) .. ' ' .. suffix })
     vim.fn.append(item:get_range().end_line, line)
-    vim.fn.cursor(item:get_range().end_line + #line, 1)
+    vim.fn.cursor(item:get_range().end_line + #line - 1, 1)
   end
   return vim.cmd([[startinsert!]])
 end
after.mp4

However, I'm not sure if this is the correct solution.

OS / Distro

macOS Sonoma 14.3.1

Neovim version/commit

NVIM v0.10.0-dev-2452+gad5a155b1 Build type: Release LuaJIT 2.1.1707061634

Additional context

No response

Thanks for reporting! Should be fixed now.

Thank you for the quick response! Now I'm enjoying even more comfortable org experience 👍