diff --git a/autoload/noteworthy.vim b/autoload/noteworthy.vim index e03a88e..73d8a02 100644 --- a/autoload/noteworthy.vim +++ b/autoload/noteworthy.vim @@ -1,29 +1,29 @@ "" " Open/create a note. -function! noteworthy#Note(range, line1, line2, file) abort - call s:File('edit', a:file, a:range, a:line1, a:line2) +function! noteworthy#Note(range, line1, line2, ...) abort + call s:File('edit', a:range, a:line1, a:line2, a:000) endfunction "" " Open/create a note in a new tab. -function! noteworthy#Tnote(range, line1, line2, file) abort - call s:File('tabedit', a:file, a:range, a:line1, a:line2) +function! noteworthy#Tnote(range, line1, line2, ...) abort + call s:File('tabedit', a:range, a:line1, a:line2, a:000) endfunction "" " Open/create a note in a new split. -function! noteworthy#Snote(range, line1, line2, file) abort +function! noteworthy#Snote(range, line1, line2, ...) abort call s:File(get( \ g:, 'noteworthy_split_size', '' - \ ) . 'split', a:file, a:range, a:line1, a:line2) + \ ) . 'split', a:range, a:line1, a:line2, a:000) endfunction "" " Open/create a note in a new vertical split. -function! noteworthy#Vnote(range, line1, line2, file) abort +function! noteworthy#Vnote(range, line1, line2, ...) abort call s:File(get( \ g:, 'noteworthy_vsplit_size', '' - \ ) . 'vsplit', a:file, a:range, a:line1, a:line2) + \ ) . 'vsplit', a:range, a:line1, a:line2, a:000) endfunction "" @@ -205,11 +205,18 @@ endfunction "" " Create or open a note in the current library. -function! s:File(command, file, range, line1, line2) abort +function! s:File(command, range, line1, line2, file) abort let l:delim = s:GetNoteDelimiter() let l:fext = s:GetNoteFileExt() - let l:file = s:GetFileName(a:file, l:delim, 1) - let l:basedir = fnamemodify(l:file, ':h') + if empty(a:file) + let l:file = '__Scratch_Note__.' . l:fext + let l:type = 'scratch' + else + let l:file = s:GetFileName(a:file[0], l:delim, 1) + let l:basedir = fnamemodify(l:file, ':h') + if !isdirectory(l:basedir) | call mkdir(l:basedir, 'p') | endif + let l:type = 'note' + endif if a:range let l:lines = getline(a:line1, a:line2) let l:indent_level = s:GetIndentLevel(l:lines) @@ -218,10 +225,18 @@ function! s:File(command, file, range, line1, line2) abort let l:lines = ['```' . &ft] + map(l:lines, l:callback) + ['```'] endif endif - if !isdirectory(l:basedir) | call mkdir(l:basedir, 'p') | endif - execute a:command l:file - if get(g:, 'noteworthy_use_header', 1) && getfsize(l:file) <= 0 - let l:title = substitute(fnamemodify(l:file, ':t:r'), l:delim, ' ', 'g') + let l:buf_nr = bufnr(l:file) + if l:buf_nr == -1 + execute a:command l:file + if l:type ==# 'scratch' | call s:SetScratchOptions() | endif + else + let l:win_nr = bufwinnr(l:buf_nr) + if l:win_nr != -1 + if winnr() != l:win_nr | execute l:win_nr . 'wincmd w' | endif + endif + endif + if get(g:, 'noteworthy_use_header', 1) && getfsize(l:file) <= 0 && empty(getline(1)) + let l:title = trim(substitute(fnamemodify(l:file, ':t:r'), l:delim, ' ', 'g')) call append(0, s:GetFormattedTitle(l:title)) endif if exists('l:lines') @@ -280,7 +295,7 @@ function! s:DynamicLibraryName() abort endfunction function! s:CacheLibrary(library) abort - let l:file = s:GetCacheFile() + let l:file = s:GetCacheFile() let l:dir = fnamemodify(l:file, ':h') if !isdirectory(l:dir) | call mkdir(l:dir, 'p') | endif let l:text = [a:library] @@ -301,3 +316,13 @@ function! s:GetIndentLevel(lines) abort endfor return min(l:indent_levels) endfunction + +function! s:SetScratchOptions() abort + setlocal bufhidden=hide + setlocal buftype=nofile + setlocal foldcolumn=0 + setlocal nobuflisted + setlocal nofoldenable + setlocal nonumber + setlocal noswapfile +endfunction diff --git a/doc/noteworthy.txt b/doc/noteworthy.txt index da1b279..35bf43c 100644 --- a/doc/noteworthy.txt +++ b/doc/noteworthy.txt @@ -233,29 +233,28 @@ Change where cache files are kept. COMMANDS *noteworthy-commands* *Note* *:Note* -:[{range}]Note {topic} [{, ...}] - Create new {topic} markdown file in the +:[{range}]Note [{topic}] Create new {topic} markdown file in the |b:noteworthy_current_library|, or open it if it already exists. The file extension can be omitted. Can be multiple words separated by spaces, which will be replaced by underscores in the file name. - If [{range}] is provided (works with visual - selection), the lines of the current file will be - appended to the note. By default, the copied text - will be wrapped in a markdown code block. To - disable this, see |g:noteworthy_use_code_block|. + If {topic} is not provided, a scratch buffer will + be created, which can't be saved, and will be + deleted once the window is closed. If {range} is + provided (works with visual selection), the lines + of the current file will be appended to the note. + By default, the copied text will be wrapped in a + markdown code block. To disable this, see + |g:noteworthy_use_code_block|. *Snote* *:Snote* -:[{range}]Snote {topic} [{, ...}] - Same as |:Note|, but opens in a split. +:[{range}]Snote [{topic}] Same as |:Note|, but opens in a split. *Vnote* *:Vnote* -:[{range}]Vnote {topic} [{, ...}] - Same as |:Note|, but opens in a vertical split. +:[{range}]Vnote [{topic}] Same as |:Note|, but opens in a vertical split. *Tnote* *:Tnote* -:[{range}]Tnote {topic} [{, ...}] - Same as |:Note|, but opens in new tab. +:[{range}]Tnote [{topic}] Same as |:Note|, but opens in new tab. *NoteSearch* *:NoteSearch* diff --git a/plugin/noteworthy.vim b/plugin/noteworthy.vim index 620acc1..74f7072 100644 --- a/plugin/noteworthy.vim +++ b/plugin/noteworthy.vim @@ -5,16 +5,16 @@ let g:loaded_noteworthy = 1 call noteworthy#Init() -command! -range=0 -nargs=1 -complete=custom,noteworthy#Completion Note +command! -range=0 -nargs=? -complete=custom,noteworthy#Completion Note \ call noteworthy#Note(, , , ) -command! -range=0 -nargs=1 -complete=custom,noteworthy#Completion Vnote +command! -range=0 -nargs=? -complete=custom,noteworthy#Completion Vnote \ call noteworthy#Vnote(, , , ) -command! -range=0 -nargs=1 -complete=custom,noteworthy#Completion Snote +command! -range=0 -nargs=? -complete=custom,noteworthy#Completion Snote \ call noteworthy#Snote(, , , ) -command! -range=0 -nargs=1 -complete=custom,noteworthy#Completion Tnote +command! -range=0 -nargs=? -complete=custom,noteworthy#Completion Tnote \ call noteworthy#Tnote(, , , ) command! -bang -nargs=? -complete=custom,noteworthy#LibraryCompletion NoteLibrary