dccsillag / magma-nvim

Interact with Jupyter from NeoVim.
GNU General Public License v3.0
992 stars 50 forks source link

MagmaInit and MagmaLoad clash #13

Closed tzachar closed 3 years ago

tzachar commented 3 years ago

Im trying to set up a workflow, where I automatically load magma for *.jupyter files, and get consistent behavior between opening new files and reopening old files. Something like this:

function! MyMagmaInit()                                       
        setlocal filetype=python                              
        set syntax=python                                     
        MagmaInit python3                                     
        MagmaLoad                                             
endfunction                                                   

augroup magma                                                 
  au! BufRead,BufNewFile *.jupyter call MyMagmaInit()         
  au! BufWrite *.jupyter MagmaSave                            
augroup end                                                   

However, Im getting a clash between MagmaInit and MagmaLoad when reopening a file, as MagmaLoad tries to init a second time:

Error detected while processing function MyMagmaInit[4]..remote#define#CommandBootstrap[5]..remote#define#request:                                                    
line    2:                                                                                                                                                            
[Magma] Magma is already initialized; MagmaLoad initializes Magma.                   

Also, when opening a new file, the MagmaLoad fails gloriously. Any ideas how to solve this? Can we add a way of checking if a file has saved state, reload it id it exists and if not init magma? Maybe a new command, something like:

MagmaLoadInit('python3')
tzachar commented 3 years ago

ok, a bit hacky, but I came up with the following:

"magma setup
let g:magma_save_path = stdpath("data") .. "/magma/"
function! MyMagmaInit()
    setlocal filetype=python
    set syntax=python
    let l:mangled_fname = expand('%:p') .. '.json'
    let l:mangled_fname = substitute(l:mangled_fname, '%', '%%', 'g')
    let l:mangled_fname = substitute(l:mangled_fname, '/', '%', 'g')
    let l:save_file = g:magma_save_path .. l:mangled_fname
    if filereadable(l:save_file)
        MagmaLoad
    else
        MagmaInit python3
    endif
endfunction

augroup magma
  au! BufRead,BufNewFile *.jupyter call MyMagmaInit()
  au! BufWrite *.jupyter MagmaSave
augroup end
dccsillag commented 3 years ago

Yeah, for now this is what you should indeed do. I think the actual solution here is not to create a new version of MagmaInit/MagmaLoad for this specific scenario, but to create some functions to allow you to easily query things like

We can open a new issue for that.

tzachar commented 3 years ago

Sure, that would be helpfull.