Skip to content

Commit 3512163

Browse files
authored
Merge pull request #22 from evanthegrayt/21-start-note-with-visual-selection
Start note with visual selection
2 parents f620580 + 6960b72 commit 3512163

File tree

4 files changed

+46
-36
lines changed

4 files changed

+46
-36
lines changed

README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,7 @@ let g:noteworthy_delimiter = '-'
169169
#### Change the default window height for splits
170170

171171
You can change the default window height for splits. The default is to split the
172-
current window in half. Note that, if `:Snote` is called with a count, this
173-
variable will be overridden.
172+
current window in half.
174173

175174
```vim
176175
let g:noteworthy_split_size = 30
@@ -179,8 +178,7 @@ let g:noteworthy_split_size = 30
179178
#### Change the default window width for vsplits
180179

181180
You can change the default window width for vertical splits. The default is to
182-
split the current window in half. Note that, if `:Vnote` is called with a count,
183-
this variable will be overridden.
181+
split the current window in half.
184182

185183
```vim
186184
let g:noteworthy_vsplit_size = 80
@@ -213,6 +211,9 @@ You can also use subdirectories, and they will be created if they don't exist.
213211
Any one of the above commands will open a file called
214212
`rails/remember_this_about_rails.md` in your current library.
215213

214+
If called with a range (or a visual selection), the corresponding lines of your
215+
current file will be appended to the note being edited.
216+
216217
Note that `:Snote`, `:Vnote`, and `:Tnote` commands also exist. They behave the
217218
same as `:Note`, except they open the note in a split, vertical split, and new
218219
tab, respectively.

autoload/noteworthy.vim

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,29 @@
11
""
22
" Open/create a note.
3-
function! noteworthy#Note(...) abort
4-
call s:File('edit', a:000)
3+
function! noteworthy#Note(line1, line2, ...) abort
4+
call s:File('edit', a:000, a:line1, a:line2)
55
endfunction
66

77
""
88
" Open/create a note in a new tab.
9-
function! noteworthy#Tnote(...) abort
10-
call s:File('tabedit', a:000)
9+
function! noteworthy#Tnote(line1, line2, ...) abort
10+
call s:File('tabedit', a:000, a:line1, a:line2)
1111
endfunction
1212

1313
""
1414
" Open/create a note in a new split.
15-
function! noteworthy#Snote(size, ...) abort
16-
let l:prefix = a:size ? a:size : get(g:, 'noteworthy_split_size', '')
17-
call s:File(l:prefix . 'split', a:000)
15+
function! noteworthy#Snote(line1, line2, ...) abort
16+
call s:File(get(
17+
\ g:, 'noteworthy_split_size', ''
18+
\ ) . 'split', a:000, a:line1, a:line2)
1819
endfunction
1920

2021
""
2122
" Open/create a note in a new vertical split.
22-
function! noteworthy#Vnote(size, ...) abort
23-
let l:prefix = a:size ? a:size : get(g:, 'noteworthy_vsplit_size', '')
24-
call s:File(l:prefix . 'vsplit', a:000)
23+
function! noteworthy#Vnote(line1, line2, ...) abort
24+
call s:File(get(
25+
\ g:, 'noteworthy_vsplit_size', ''
26+
\ ) . 'vsplit', a:000, a:line1, a:line2)
2527
endfunction
2628

2729
""
@@ -179,18 +181,22 @@ endfunction
179181

180182
""
181183
" Create or open a note in the current library.
182-
function! s:File(command, segments) abort
184+
function! s:File(command, segments, line1, line2) abort
183185
let l:delim = s:GetNoteDelimiter()
184186
let l:fext = s:GetNoteFileExt()
185187
let l:file = s:GetFileName(a:segments, l:delim)
186188
let l:basedir = fnamemodify(l:file, ':h')
189+
if a:line1 != 0 | let l:lines = getline(a:line1, a:line2) | endif
187190
if !isdirectory(l:basedir) | call mkdir(l:basedir, 'p') | endif
188191
execute a:command l:file
189-
if getfsize(l:file) > 0 | return | endif
190-
let l:title = substitute(fnamemodify(l:file, ':t:r'), l:delim, ' ', 'g')
191-
if !get(g:, 'noteworthy_use_header', 1) | return | endif
192-
let l:formatted_title = s:GetFormattedTitle(l:title)
193-
call append(0, [l:formatted_title])
192+
if get(g:, 'noteworthy_use_header', 1) && getfsize(l:file) <= 0
193+
let l:title = substitute(fnamemodify(l:file, ':t:r'), l:delim, ' ', 'g')
194+
call append(0, s:GetFormattedTitle(l:title))
195+
endif
196+
if exists('l:lines')
197+
let l:last_line = trim(getline(line('$'))) == '' ? line('$') - 1 : line('$')
198+
call append(l:last_line, l:lines)
199+
endif
194200
endfunction
195201

196202
function! s:GetFileName(segments, delim) abort

doc/noteworthy.txt

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,24 +251,27 @@ In the root of the project, in a file named .vimrc
251251
COMMANDS *noteworthy-commands*
252252

253253
*Note* *:Note*
254-
:Note {topic} [{, ...}] Create new {topic} markdown file in the
254+
:[{range}]Note {topic} [{, ...}]
255+
Create new {topic} markdown file in the
255256
|b:noteworthy_current_library|, or open it if it
256257
already exists. The file extension can be omitted.
257258
Can be multiple words separated by spaces, which
258259
will be replaced by underscores in the file name.
260+
If [{range}] is provided (works with visual
261+
selection), the lines of the current file will be
262+
appended to the note.
259263

260264
*Snote* *:Snote*
261-
:[N]Snote {topic} [{, ...}] Same as |Note|, but opens in a split. If N is
262-
provided, it will be used as the height of the
263-
window.
265+
:[{range}]Snote {topic} [{, ...}]
266+
Same as |:Note|, but opens in a split.
264267

265268
*Vnote* *:Vnote*
266-
:[N]Vnote {topic} [{, ...}] Same as |Note|, but opens in a vertical split. If N
267-
is provided, it will be used as the width of the
268-
window.
269+
:[{range}]Vnote {topic} [{, ...}]
270+
Same as |:Note|, but opens in a vertical split.
269271

270272
*Tnote* *:Tnote*
271-
:Tnote {topic} [{, ...}] Same as |Note|, but opens in new tab.
273+
:[{range}]Tnote {topic} [{, ...}]
274+
Same as |:Note|, but opens in new tab.
272275

273276

274277
*NoteSearch* *:NoteSearch*

plugin/noteworthy.vim

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,28 @@ endif
1313
""
1414
" :Note {topic}, ...
1515
" Create or edit a note. Opens in current window.
16-
command! -nargs=+ -complete=custom,noteworthy#Completion Note
17-
\ call noteworthy#Note(<f-args>)
16+
command! -nargs=+ -range=0 -complete=custom,noteworthy#Completion Note
17+
\ call noteworthy#Note(<range> ? <line1> : 0, <range> ? <line2> : 0, <f-args>)
1818

1919
""
2020
" :[N]VNote {topic}, ...
2121
" Create or edit a note. Opens in vertical split, [N] columns wide (default:
2222
" split current window in half)
23-
command! -count -nargs=+ -complete=custom,noteworthy#Completion Vnote
24-
\ call noteworthy#Vnote(<count>, <f-args>)
23+
command! -range=0 -nargs=+ -complete=custom,noteworthy#Completion Vnote
24+
\ call noteworthy#Vnote(<range> ? <line1> : 0, <range> ? <line2> : 0, <f-args>)
2525

2626
""
2727
" :[N]SNote {topic}, ...
2828
" Create or edit a note. Opens in split, [N] columns wide (default: split
2929
" current window in half)
30-
command! -count -nargs=+ -complete=custom,noteworthy#Completion Snote
31-
\ call noteworthy#Snote(<count>, <f-args>)
30+
command! -range=0 -nargs=+ -complete=custom,noteworthy#Completion Snote
31+
\ call noteworthy#Snote(<range> ? <line1> : 0, <range> ? <line2> : 0, <f-args>)
3232

3333
""
3434
" :TNote {topic}, ...
3535
" Create or edit a note. Opens in new tab.
36-
command! -nargs=+ -complete=custom,noteworthy#Completion Tnote
37-
\ call noteworthy#Tnote(<f-args>)
36+
command! -range=0 -nargs=+ -complete=custom,noteworthy#Completion Tnote
37+
\ call noteworthy#Tnote(<range> ? <line1> : 0, <range> ? <line2> : 0, <f-args>)
3838

3939
""
4040
" :NoteLibrary [{directory}]

0 commit comments

Comments
 (0)