nvim-orgmode / orgmode

Orgmode clone written in Lua for Neovim 0.9+.
https://nvim-orgmode.github.io/
MIT License
3.03k stars 134 forks source link

Handle missing capture file destination and report to user #429

Closed milanglacier closed 8 months ago

milanglacier commented 2 years ago

Describe the bug

when try to use capture template with a non default target file and a specified headline, get the following error:

Error executing vim.schedule lua callback: ...ite/pack/packer/opt/orgmode/lua/orgmode/capture/init.lua:230: attempt to index local 'destination_file' (a nil val
ue)
stack traceback:
        ...ite/pack/packer/opt/orgmode/lua/orgmode/capture/init.lua:230: in function 'refile_to_headline'
        ...ite/pack/packer/opt/orgmode/lua/orgmode/capture/init.lua:124: in function ''
        vim/_editor.lua: in function ''
        vim/_editor.lua: in function <vim/_editor.lua:0>

Steps to reproduce

  1. <Leader>oc to open the org capture
  2. select the template, say t, which will write the buffer to a non default file with a specified headline.
  3. press C-c, and get the error

Expected behavior

write the buffer to the specified file with a specified headline

Emacs functionality

write the buffer to the specified file with a specified headline

Minimal init.lua

vim.cmd [[set runtimepath=$VIMRUNTIME]]
vim.cmd [[set packpath=/tmp/nvim/site]]

local package_root = '/tmp/nvim/site/pack'
local install_path = package_root .. '/packer/start/packer.nvim'

local function load_plugins()
    require('packer').startup {
        {
            'wbthomason/packer.nvim',
            { 'nvim-treesitter/nvim-treesitter' },
            { 'kristijanhusak/orgmode.nvim', branch = 'master' },
        },
        config = {
            package_root = package_root,
            compile_path = install_path .. '/plugin/packer_compiled.lua',
        },
    }
end

_G.load_config = function()
    require('orgmode').setup_ts_grammar()
    require('nvim-treesitter.configs').setup {
        highlight = {
            enable = true,
            additional_vim_regex_highlighting = { 'org' },
        },
    }

    vim.cmd [[packadd nvim-treesitter]]
    vim.cmd [[runtime plugin/nvim-treesitter.lua]]
    vim.cmd [[TSUpdateSync org]]

    -- Close packer after install
    if vim.bo.filetype == 'packer' then
        vim.api.nvim_win_close(0, true)
    end

    local org_dir = '~/Desktop/orgmode' -- change this line to an org dir
    require('orgmode').setup {
        org_agenda_files = { org_dir .. '/*' },
        org_default_notes_file = org_dir .. '/capture/todo.org',
        org_capture_templates = {
            n = {
                description = 'personal notes',
                template = '** %u %?',
                target = org_dir .. '/capture/notes.org',
                headline = 'Notes'
            },
        },
    }

    -- Reload current file if it's org file to reload tree-sitter
    if vim.bo.filetype == 'org' then
        vim.cmd [[edit!]]
    end
end

if vim.fn.isdirectory(install_path) == 0 then
    vim.fn.system { 'git', 'clone', 'https://github.com/wbthomason/packer.nvim', install_path }
    load_plugins()
    require('packer').sync()
    vim.cmd [[autocmd User PackerCompileDone ++once lua load_config()]]
else
    load_plugins()
    load_config()
end

and the target org dir, say ~/Desktop/orgmode, needs to have the following file, capture/notes.org, with the following cotents

* Notes

Screenshots and recordings

No response

OS / Distro

macOS 12.4

Neovim version/commit

0.8

Additional context

No response

kristijanhusak commented 2 years ago

Does file ~/Desktop/orgmode/capture/notes.org exist? It seems that it cannot be reached when capturing.

milanglacier commented 2 years ago

Does file ~/Desktop/orgmode/capture/notes.org exist? It seems that it cannot be reached when capturing.

yes, it does contain, for sure.

kristijanhusak commented 2 years ago

Can you try to pull latest master and give it another try?

milanglacier commented 2 years ago

Can you try to pull latest master and give it another try?

Hi! The error is still there. Since ~/Desktop/orgmode/capture/notes.org does exist, I think the issue does not come from there.

kristijanhusak commented 2 years ago

This is because you need to set org_agenda_files to also find files in subdirectories, like this:

org_agenda_files = { org_dir .. '/**/*' },

Problem is that your notes.org file is in the capture folder, and it is not your default file, so it's not considered.

milanglacier commented 2 years ago

thank you. This is the true reason.