Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 41 additions & 16 deletions autoload/noteworthy.vim
Original file line number Diff line number Diff line change
@@ -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

""
Expand Down Expand Up @@ -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)
Expand All @@ -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')
Expand Down Expand Up @@ -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]
Expand All @@ -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
25 changes: 12 additions & 13 deletions doc/noteworthy.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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*
Expand Down
8 changes: 4 additions & 4 deletions plugin/noteworthy.vim
Original file line number Diff line number Diff line change
Expand Up @@ -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(<range>, <line1>, <line2>, <f-args>)

command! -range=0 -nargs=1 -complete=custom,noteworthy#Completion Vnote
command! -range=0 -nargs=? -complete=custom,noteworthy#Completion Vnote
\ call noteworthy#Vnote(<range>, <line1>, <line2>, <f-args>)

command! -range=0 -nargs=1 -complete=custom,noteworthy#Completion Snote
command! -range=0 -nargs=? -complete=custom,noteworthy#Completion Snote
\ call noteworthy#Snote(<range>, <line1>, <line2>, <f-args>)

command! -range=0 -nargs=1 -complete=custom,noteworthy#Completion Tnote
command! -range=0 -nargs=? -complete=custom,noteworthy#Completion Tnote
\ call noteworthy#Tnote(<range>, <line1>, <line2>, <f-args>)

command! -bang -nargs=? -complete=custom,noteworthy#LibraryCompletion NoteLibrary
Expand Down