Skip to content

Broken org buffer right after capturing #827

@wustho

Description

@wustho

Describe the bug

Pretty specific condition for this bug to happen. So, somehow this only happens if an org file is opened first (won't produce this bug if not opened first), unload the org file using :bdelete (somehow this issue doesn't happen with :bwipeout). Then do a capture of a task (for today, that will appear in agenda) that will be filed to the org file. Then open the agenda, and try to open the org file by pressing enter on the captured task. The visual indent, the foldexpr, the foldtext, iabbreviations, and maybe more will no longer work with the org buffer.

Steps to reproduce

  1. Open an org file, say main.org
  2. Close it using :bdelete
  3. Capture a task that will appear in agenda and filed to the main.org
  4. Open agenda buffer
  5. Press enter on the new task to open main.org
  6. Every org settings (visual indent, foldexpr, foldtext, iabbreviations) is broken with the opened (via agenda) main.org

Expected behavior

This should not happen, it should keep the org file behaviour.

Emacs functionality

No response

Minimal init.lua

return {
    {
        'nvim-treesitter/nvim-treesitter',
        version = false,
        event = { 'BufReadPost', 'BufNewFile' },
        run = ':TSUpdate',
        dependencies = {
            'nvim-treesitter/nvim-treesitter-textobjects',
            -- 'RRethy/nvim-treesitter-textsubjects',
            -- 'nvim-treesitter/nvim-treesitter-refactor',
        },
        config = function()
            require('nvim-treesitter.configs').setup({
                auto_install = true,
                -- https://github.com/nvim-orgmode/orgmode?tab=readme-ov-file#installation
                ignore_install = { 'org' },
                -- ensure_installed = 'all', -- slow! just manual :TSInstall all
                ensure_installed = {
                    'bash',
                    'c',
                    'css',
                    'diff',
                    'html',
                    'htmldjango', -- builtin is better
                    'http',
                    'javascript',
                    'json',
                    'lua',
                    'luadoc',
                    'markdown',
                    'markdown_inline',
                    -- 'org',
                    'proto',
                    'python',
                    'query',
                    'regex',
                    'rust',
                    'scss',
                    'toml',
                    'tsx',
                    'typescript',
                    'vim',
                    'vimdoc',
                    'yaml',
                },
                textobjects = {
                    select = {
                        enable = true,
                        keymaps = {
                            ['af'] = '@function.outer',
                            ['if'] = '@function.inner',
                            ['ac'] = '@class.outer',
                            ['ic'] = {
                                query = '@class.inner',
                                desc = 'Select inner part of a class region',
                            },
                            ['as'] = {
                                query = '@scope',
                                query_group = 'locals',
                                desc = 'Select language scope',
                            },
                        },
                    },
                    move = {
                        enable = true,
                        set_jumps = true,
                        goto_next_start = {
                            [']]'] = {
                                query = { '@class.outer', '@function.outer' },
                                desc = 'Next class/function start',
                            },
                        },
                        goto_previous_start = {
                            ['[['] = {
                                query = { '@class.outer', '@function.outer' },
                                desc = 'Previous class/function start',
                            },
                        },
                    },
                },
                highlight = {
                    enable = true,
                    -- additional_vim_regex_highlighting = { 'org' },  -- will mess with httpResult filetype
                    additional_vim_regex_highlighting = false,
                    disable = { 'htmldjango' }, -- buggy when changing class
                },
                indent = {
                    enable = true,
                    disable = {
                        'python',
                        'htmldjango', -- issue with html snippet (eg. emmet_ls)
                        'org',
                    },
                },
                incremental_selection = {
                    enable = true,
                    keymaps = {
                        -- init_selection = 'gnn', -- set to `false` to disable one of the mappings
                        -- node_incremental = 'grn',
                        -- scope_incremental = 'grc',
                        -- node_decremental = 'grm',
                        -- init_selection = '<Enter>', -- set to `false` to disable one of the mappings
                        -- node_incremental = '<Enter>',
                        -- node_decremental = '<BS>',
                        node_incremental = 'v',
                        node_decremental = 'V',
                    },
                },
                -- https://github.com/andymass/vim-matchup#tree-sitter-integration
                -- matchup = {
                --     enable = true,
                --     -- disable = { "c", "ruby" },
                --     --     include_match_words = false,
                -- },
                -- textsubjects = {
                --     enable = true,
                --     prev_selection = ',', -- (Optional) keymap to select the previous selection
                --     keymaps = {
                --         ['.'] = 'textsubjects-smart',
                --         [';'] = 'textsubjects-container-outer',
                --         ['i;'] = 'textsubjects-container-inner',
                --         -- ['i;'] = { 'textsubjects-container-inner',
                --         --     desc = 'Select inside containers (classes, functions, etc.)' },
                --     },
                -- },
            })
        end,
    },
    {
        -- NOTE: this plugins already provide treesitter parser for 'org'
        -- so, don't use 'org' parser from official treesitter (if installed, do :TSUninstall org)
        'nvim-orgmode/orgmode',
        dependencies = {
            'nvim-treesitter/nvim-treesitter',
        },
        event = 'VeryLazy',
        ft = { 'org' },
        init = function()
            vim.api.nvim_create_autocmd('FileType', {
                pattern = { 'org', 'orgagenda' },
                callback = function()
                    vim.bo.expandtab = true
                    vim.bo.tabstop = 2
                    vim.bo.softtabstop = 2
                    vim.bo.shiftwidth = 2
                    vim.opt_local.wrap = false

                    vim.opt_local.signcolumn = 'yes:1'
                    vim.opt_local.colorcolumn = ''
                    vim.opt_local.number = false

                    vim.opt_local.iskeyword:append({ ':', '#', '+' })

                    local bmap = function(mode, lhs, rhs)
                        vim.keymap.set(mode, lhs, rhs, { silent = true, buffer = true })
                    end
                    -- https://github.com/nvim-orgmode/orgmode/blob/master/DOCS.md#use-enter-in-insert-mode-to-add-list-itemscheckboxestodos
                    bmap('i', '<S-CR>', '<cmd>lua require("orgmode").action("org_mappings.meta_return")<CR>')
                    -- https://github.com/nvim-orgmode/orgmode/blob/fafb8f14d85a68d8f0fca812444cc0fd594f0168/lua/orgmode/org/mappings.lua#L141-L156
                    -- or just use zr and zm to increase/decrease foldlevel globally
                    bmap('n', '<S-Tab>', function()
                        local current_state = vim.w.my_org_foldstate or 'showall'
                        if current_state == 'showall' then
                            vim.w.my_org_foldstate = 'overview'
                            return vim.cmd([[silent! norm!zMzX]])
                        elseif current_state == 'overview' then
                            vim.w.my_org_foldstate = 'contents'
                            local max_foldlevel = 0
                            local line_count = vim.api.nvim_buf_line_count(0)
                            for line = 1, line_count do
                                max_foldlevel = math.max(max_foldlevel, vim.fn.foldlevel(line))
                            end
                            vim.wo.foldlevel = max_foldlevel - 2
                            return vim.cmd([[silent! norm!zx]])
                        elseif current_state == 'contents' then
                            vim.w.my_org_foldstate = 'showall'
                            return vim.cmd([[silent! norm!zR]])
                        end
                    end)
                end,
            })
        end,
        config = function()
            require('orgmode').setup({
                mappings = {
                    -- org_return_uses_meta_return = true,
                    prefix = '<leader>o',
                    global = {
                        org_agenda = '<leader>A',
                        org_capture = '<leader>C',
                    },
                    capture = {
                        org_capture_finalize = '<leader><space>',
                        org_capture_kill = '<leader>q',
                    },
                    org = {
                        org_global_cycle = false,
                    },
                },
                win_split_mode = '20split',
                org_hide_emphasis_markers = true,
                org_log_into_drawer = 'LOGBOOK',
                -- org_agenda_files = '~/org/agenda/**/*',
                org_agenda_files = '~/org/agenda/main.org',
                org_agenda_start_on_weekday = false,
                org_agenda_start_day = '0d',
                org_default_notes_file = '~/org/agenda/inbox_desktop.org',
                org_startup_indented = true,
                org_adapt_indentation = false,
                -- org_indent_mode_turns_off_org_adapt_indentation = true,
                org_indent_mode_turns_on_hiding_stars = false,
                org_hide_leading_stars = false,
                org_todo_keywords = {
                    'TODO',
                    'NEXT',
                    'WAIT',
                    '|',
                    'DONE',
                    'CANC',
                },
                org_todo_keyword_faces = {
                    TODO = ':foreground red :weight bold',
                    NEXT = ':foreground lightblue :weight bold :slant italic',
                    WAIT = ':foreground orange :weight bold',
                    DONE = ':foreground green',
                    CANC = ':foreground gray',
                },
                org_capture_templates = {
                    T = {
                        description = '⌚ Task for today',
                        template = '* TODO %?\nSCHEDULED: %t\n\nEntered on %U\n',
                        target = '~/org/agenda/main.org',
                        properties = {
                            empty_lines = {
                        before = 1
                            }
                        }
                    },
                    t = {
                        description = '✅ Task',
                        template = '* TODO %?\n\nEntered on %U\n',
                        target = '~/org/agenda/main.org',
                    },
                    j = {
                        description = '📝 Journal',
                        template = '* %?\n\nEntered on %U\n',
                        datetree = true,
                        target = '~/org/agenda/journal.org',
                    },
                },
                ui = {
                    folds = {
                        colored = true,
                    },
                },
            })

            -- NOTE: org_startup_indented doesn't work after capturing
        end,
    },
    {
        'akinsho/org-bullets.nvim',
        ft = { 'org' },
        opts = {
            concealcursor = false,
            symbols = {
                headlines = { '󰜡' },
                checkboxes = {
                    half = { '', '@org.checkbox.halfchecked' },
                    done = { '', '@org.keyword.done' },
                    todo = { '', '@org.keyword.todo' },
                },
            },
        },
    },

}

Screenshots and recordings

orgmode_capture_issue.mp4

OS / Distro

Kubuntu 22.04.4 LTS x86_64

Neovim version/commit

NVIM v0.10.2

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions