neorg icon indicating copy to clipboard operation
neorg copied to clipboard

Edit and capture use buffer instead of file

Open helmecke opened this issue 4 years ago • 7 comments

Prerequisites

  • [X] I have read contribution guidelines
  • [X] I am using the latest version of the plugin
  • [X] I am using either 0.6 stable or the latest compiled neovim version

Neovim Version

NVIM v0.6.1

Neorg setup

Config
require('neorg').setup {
  load = {
    ['core.defaults'] = {},
    ['core.keybinds'] = {
      config = {
        default_keybinds = false,
      },
    },
    ['core.norg.concealer'] = {},
    ['core.norg.completion'] = {
      config = {
        engine = 'nvim-cmp',
      },
    },
    ['core.norg.dirman'] = {
      config = {
        workspaces = {
          personal = '~/Documents/neorg',
          gtd = '~/Documents/gtd',
        },
        autochdir = true,
      },
    },
    ['core.gtd.base'] = {
      config = {
        workspace = 'gtd',
      },
    },
    ['core.integrations.telescope'] = {},
    ['core.presenter'] = {
      config = {
        zen_mode = 'zen-mode',
      },
    },
    ['core.norg.journal'] = {},
  },
  hook = function()
    local neorg_callbacks = require 'neorg.callbacks'
    local leader = '<leader>'
    local localleader = '<localleader>'

    neorg_callbacks.on_event('core.keybinds.events.enable_keybinds', function(_, keybinds)
      -- Map all the below keybinds only when the "norg" mode is active
      keybinds.map_event_to_mode('norg', {
        n = {
          -- Keys for managing TODO items and setting their states
          { localleader .. 'tu', 'core.norg.qol.todo_items.todo.task_undone' },
          { localleader .. 'tp', 'core.norg.qol.todo_items.todo.task_pending' },
          { localleader .. 'td', 'core.norg.qol.todo_items.todo.task_done' },
          { localleader .. 'th', 'core.norg.qol.todo_items.todo.task_on_hold' },
          { localleader .. 'tc', 'core.norg.qol.todo_items.todo.task_cancelled' },
          { localleader .. 'tr', 'core.norg.qol.todo_items.todo.task_recurring' },
          { localleader .. 'ti', 'core.norg.qol.todo_items.todo.task_important' },
          { '<C-Space>', 'core.norg.qol.todo_items.todo.task_cycle' },

          -- Keys for managing GTD
          { localleader .. 'te', 'core.gtd.base.edit' },
          { leader .. 'ne', 'core.gtd.base.edit' },

          -- Keys for managing notes
          { localleader .. 'nn', 'core.norg.dirman.new.note' },
          { leader .. 'nn', 'core.norg.dirman.new.note' },

          { '<CR>', 'core.norg.esupports.hop.hop-link' },
          { '<M-CR>', 'core.norg.esupports.hop.hop-link', 'vsplit' },

          { '<C-s>', 'core.integrations.telescope.find_linkable' },

          -- TODO: rebind
          -- { '<M-k>', 'core.norg.manoeuvre.item_up' },
          -- { '<M-j>', 'core.norg.manoeuvre.item_down' },
        },
        i = {
          { '<C-l>', 'core.integrations.telescope.insert_link' },
        },
        o = {
          { 'ah', 'core.norg.manoeuvre.textobject.around-heading' },
          { 'ih', 'core.norg.manoeuvre.textobject.inner-heading' },
          { 'at', 'core.norg.manoeuvre.textobject.around-tag' },
          { 'it', 'core.norg.manoeuvre.textobject.inner-tag' },
          { 'al', 'core.norg.manoeuvre.textobject.around-whole-list' },
        },
      }, {
        silent = true,
        noremap = true,
      })

      -- Map the below keys only when traverse-heading mode is active
      keybinds.map_event_to_mode('traverse-heading', {
        n = {
          -- Rebind j and k to move between headings in traverse-heading mode
          { 'j', 'core.integrations.treesitter.next.heading' },
          { 'k', 'core.integrations.treesitter.previous.heading' },
        },
      }, {
        silent = true,
        noremap = true,
      })

      -- Map the below keys on gtd displays
      keybinds.map_event_to_mode('gtd-displays', {
        n = {
          { '<CR>', 'core.gtd.ui.goto_task' },

          -- Keys for closing the current display
          { 'q', 'core.gtd.ui.close' },
          { '<Esc>', 'core.gtd.ui.close' },

          { 'e', 'core.gtd.ui.edit_task' },
          { '<Tab>', 'core.gtd.ui.details' },
        },
      }, {
        silent = true,
        noremap = true,
        nowait = true,
      })

      -- Map the below keys on presenter mode
      keybinds.map_event_to_mode('presenter', {
        n = {
          { '<CR>', 'core.presenter.next_page' },
          { 'l', 'core.presenter.next_page' },
          { 'h', 'core.presenter.previous_page' },

          -- Keys for closing the current display
          { 'q', 'core.presenter.close' },
          { '<Esc>', 'core.presenter.close' },
        },
      }, {
        silent = true,
        noremap = true,
        nowait = true,
      })

      -- Apply the below keys to all modes
      keybinds.map_to_mode('all', {
        n = {
          { localleader .. 'mn', ':Neorg mode norg<CR>' },
          { localleader .. 'mh', ':Neorg mode traverse-heading<CR>' },
          { leader .. 'nmn', ':Neorg mode norg<CR>' },
          { leader .. 'nmh', ':Neorg mode traverse-heading<CR>' },

          -- Keys for managing journal
          { localleader .. 'jm', ':Neorg journal tomorrow<CR>' },
          { localleader .. 'jt', ':Neorg journal today<CR>' },
          { localleader .. 'jy', ':Neorg journal yesterday<CR>' },
          { leader .. 'njm', ':Neorg journal tomorrow<CR>' },
          { leader .. 'njt', ':Neorg journal today<CR>' },
          { leader .. 'njy', ':Neorg journal yesterday<CR>' },

          -- Keys for managing presenter
          { localleader .. 'ps', ':Neorg presenter start<CR>' },
          { leader .. 'np', ':Neorg presenter start<CR>' },
        },
      }, {
        silent = true,
        noremap = true,
      })
    end)
  end,
}

Actual behavior

:Neorg gtd edit

[neorg] (06:45:14)                                                                                                                                                                                                  
...r/opt/neorg/lua/neorg/modules/core/gtd/ui/edit_popup.lua:18                                                                                                                                                      
No task at cursor position

:Neorg gtd capture

E5108: Error executing lua ...r/start/nvim-treesitter/lua/nvim-treesitter/ts_utils.lua:266: attempt to index local 'node_or_range' (a nil value)                                                                    
stack traceback:                                                                                                                                                                                                    
        ...r/start/nvim-treesitter/lua/nvim-treesitter/ts_utils.lua:266: in function 'get_node_range'                                                                                                               
        ...pt/neorg/lua/neorg/modules/core/gtd/queries/creators.lua:235: in function 'insert_tag'                                                                                                                   
        ...pt/neorg/lua/neorg/modules/core/gtd/queries/creators.lua:62: in function 'create'                                                                                                                        
        ...pt/neorg/lua/neorg/modules/core/gtd/ui/capture_popup.lua:77: in function <...pt/neorg/lua/neorg/modules/core/gtd/ui/capture_popup.lua:74>                                                                
        .../opt/neorg/lua/neorg/modules/core/ui/selection_popup.lua:343: in function <.../opt/neorg/lua/neorg/modules/core/ui/selection_popup.lua:329>                                                              
        .../opt/neorg/lua/neorg/modules/core/ui/selection_popup.lua:22: in function 'invoke_key_in_selection'                                                                                                       
        [string ":lua"]:1: in main chunk                                                                                                                                                                            
[neorg] (06:46:28)                                                                                                                                                                                                  
...pt/neorg/lua/neorg/modules/core/gtd/queries/creators.lua:55                                                                                                                                                      
Error in inserting new content

Expected behavior

:Neorg gtd edit should always use buffer.

:Neorg gtd capture

  • x -> Add to cursor position should always use buffer.
  • p -> Add to project should use buffer if file is open and has changes or if file is open and is current buffer.
  • <CR> -> Add to inbox should use buffer if file is open and has changes or if file is open and is current buffer.

Steps to reproduce

Open norg file with tasks. Insert empty line above task without saving. :Neorg gtd edit

Create new note with core.norg.dirman.new.note without saving. :Neorg gtd capture -> x -> Add to cursor position.

Potentially conflicting plugins

No response

Other information

No response

Help

No

Implementation help

No response

helmecke avatar Jan 22 '22 05:01 helmecke

Hello ! I quite don't get the problem here. What do you mean that "use file instead of buffer "?

Oh i think I know what you mean there. Indeed we read the file content from file system in order to prevent creating a lot of buffers. Try to save the file before opening any view, so that you don't have any errors. Tell me if it works for you !

danymat avatar Jan 22 '22 08:01 danymat

Damn, I got the title reverse. I fixed it. The description was correct though.

helmecke avatar Jan 22 '22 09:01 helmecke

It's true saving bevorhand works, but it's a workaround.

If I have a lot of buffers I would have to think before using :Neorg gtd capture. Do I have that project(file) open, did i make change. Do I have inbox.norg open with changes not saved. Even with :Neorg gtd edit I have to make a binding which saves the current buffer before executing :Neorg gtd edit.

helmecke avatar Jan 22 '22 09:01 helmecke

Hello again !

Indeed, this is related to #225 and https://github.com/nvim-neorg/neorg/pull/259#issuecomment-1001768316.

Before doing any real change, I think we need to discuss more about the potential requirement of this feature

danymat avatar Jan 22 '22 13:01 danymat

I had a similar issue today, I had changed some things in my inbox.norg and then I did Neorg gtd edit and afterwards my previous edits where gone since I didn't save :/ I'm not sure I understand why using the buffer would imply creating a lot of buffers, can't you reuse the existing one?

Anyway, even if you still want to read from file I would say either:

  • Have neorg automatically save the current file before opening the Neorg gtd edit popup.
  • Don't even allow opening the Neorg gtd edit if the buffer is changed but not saved, and give an error.

AckslD avatar May 19 '22 07:05 AckslD

I'm not sure I understand why using the buffer would imply creating a lot of buffers, can't you reuse the existing one?

  • Basically, reading from buffers change pwd (I don't know if this is a limitation of neorg or Neovim
  • https://github.com/nvim-neorg/neorg/pull/259#issuecomment-1001768316

We plan to rewrite GTD core with @vhyrro, so we will definitely handle this problematic

danymat avatar Jul 08 '22 09:07 danymat

One other example where this is an issue:

  • do Neorg gtd capture
  • type a todo and press enter twice
  • do a new capture Neorg gtd capture
  • type a todo and press enter twice

The second todo will then overwrite the first since we didn't save in between.

AckslD avatar Aug 30 '22 07:08 AckslD

Any updates on this? Not sure how to workaround this issue, Neorg + GTD is exactly what I was looking for.

Paracelsus-Rose avatar Oct 31 '22 05:10 Paracelsus-Rose

For reference: https://github.com/nvim-neorg/neorg/issues/695

danymat avatar Dec 23 '22 22:12 danymat