Merge pull request #1 from maxbucknell/note-operator

Note operator
This commit is contained in:
Max Bucknell 2025-01-14 00:31:23 +00:00 committed by GitHub
commit 34b1b55fb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 143 additions and 10 deletions

View file

@ -1,3 +1,11 @@
# Pre-amble
extends quarto, markdown, rmarkdown, pandoc
priority -1
# Header
snippet note "Note Front Matter" b
---
title: "${1:Title}"
@ -9,6 +17,8 @@ date: "${3:`!v strftime(g:mbnotes_date_format_short, localtime() + (g:mbnotes_ne
$0
endsnippet
# Callouts
snippet :::n "Callout: Note" b
::: {.callout-note}
${0:${VISUAL:text...}}
@ -33,6 +43,8 @@ ${0:${VISUAL:text...}}
:::
endsnippet
# Code
snippet `r "R Code Block" b
\`\`\`{r}
${1:#| output: ${2:false}}
@ -57,13 +69,3 @@ ${0:${VISUAL:# code...}}
\`\`\`
endsnippet
snippet todo "Make a To-Do List!" b
- [ ] ${0:What would you like to do..?}
endsnippet
snippet --- "Horizontal Rule" b
--------------------------------------------------------------------------------
$0
endsnippet

View file

@ -114,3 +114,75 @@ export def NewNote()
var file = date .. "_new-note.qmd"
execute "edit " .. g:mbnotes_dir .. "/" .. file
enddef
export def Operator(context = {}, type: string = ''): string
if type == ''
var _context = {
"dot_command": false,
"extend_block": '',
"virtualedit": [&l:virtualedit, &g:virtualedit]
}
&operatorfunc = function(Operator, [_context])
set virtualedit=block
return 'g@'
endif
var save = {
"clipboard": &clipboard,
"selection": &selection,
"virtualedit": [&l:virtualedit, &g:virtualedit],
"register": getreginfo('m'),
"visual_marks": [getpos("'<"), getpos("'>")]
}
try
set clipboard= selection=inclusive virtualedit=
var commands = {
"line": "'[V']",
"char": "`[v`]",
"block": "`[\<c-V>`]",
}[type]
var [_, _, col, off] = getpos("']")
if off != 0
var vcol = getline("'[")->strpart(0, col + off)->strdisplaywidth()
if vcol >= [line("'["), '$']->virtcol() - 1
context['extend_block'] = '$'
else
context['extend_block'] = vcol .. '|'
endif
endif
if context['extend_block'] != ''
commands ..= 'oO' .. context['extend_block']
endif
commands ..= '"my'
execute 'silent noautocmd keepjumps normal! ' .. commands
NewNote()
# normal! "mPG"_dd
normal! "mPG
finally
setreg('m', save['register'])
setpos("'<", save['visual_marks'][0])
setpos("'>", save['visual_marks'][1])
&clipboard = save['clipboard']
&selection = save['selection']
if context['dot_command']
&l:virtualedit = save['virtualedit'][0]
&g:virtualedit = save['virtualedit'][1]
else
&l:virtualedit = context['virtualedit'][0]
&g:virtualedit = context['virtualedit'][1]
endif
context['dot_command'] = true
endtry
return ""
enddef

View file

@ -7,7 +7,10 @@
2. Setup ............................. |mbnotes-setup|
3. Configuration ..................... |mbnotes-config|
4. Commands .......................... |mbnotes-commands|
4.1. Operator .................... |mbnotes-operator|
5. Front Matter ...................... |mbnotes-front-matter|
6. Integrations ...................... |mbnotes-integrations|
6.1. UltiSnips ................... |mbnotes-ultisnips|
==============================================================================
1. Introduction *mbnotes*
@ -214,6 +217,28 @@ results of the quarto render command that was attempted.
Render the current buffer as an HTML file and open it. See |:MBNotesRenderPDF|
for details on behaviour.
==============================================================================
4.1 Operator *mbnotes-operator*
New notes can be created from any text within Vim. These will be put into a new
note as created by `:MBNotesNew`, which will be opened.
*gb*
gb{motion} Yank the text that {motion} moves over and put it into a
new note buffer.
*gbb*
gbb Yank the current line into a new note buffer.
*v_gb*
{Visual}gb Yank the highlighted text into a new note buffer (for
{Visual} see |Visual-mode|).
These mappings are only set if `gb` is not already mapped to something. If they
are, or if you wish to customise these mappings, create new normal and visual
mode mappings (`nmap` and `xmap`) to `<Plug>MBNotesNew`, and a normal mode
mapping to `<Plug>MBNotesNewLine`.
==============================================================================
5. Front Matter *mbnotes-front-matter*
@ -245,4 +270,28 @@ Anything supported by Quarto can be specified here and will be imported and
merged with any front matter specified at the top of any file. See the Quarto
docs for more information, specifically on projects.
==============================================================================
6. Integrations *mbnotes-integrations*
There are many different plugins that I use to help my notes work better. I
have packaged some of the integrations in this plugin, such that they will load
if those plugin are installed.
==============================================================================
6.1. Snippets *mbnotes-ultisnips*
This plugin packages a few UltiSnips snippets out of the box. These facilitate
creating Quarto front matter with a date and title, callout blocks (as per the
Quarto documentation), and fenced code blocks that Quarto will execute.
If you use UltiSnips and would not like these, create a file called
`mbnotes.snippets` in your snippets directory, and include a line: >snippets
clearsnippets
The snippets vended by this plugin are at priority -1, so should be cleared by
a default priority instruction. Furthermore, any of these snippets can be very
easily overwritten. See the `priority` keyword in |ultisnips-basic-syntax| for
more information.
vim:tw=78:ts=8:noet:ft=help:norl:

View file

@ -100,4 +100,14 @@ command -nargs=0 MBNotesNewSplit {
mbnotes.NewNote()
}
nnoremap <expr> <Plug>MBNotesNew <SID>mbnotes.Operator()
xnoremap <expr> <Plug>MBNotesNew <SID>mbnotes.Operator()
nnoremap <expr> <Plug>MBNotesNewLine <SID>mbnotes.Operator() .. '_'
if !hasmapto('<Plug>MBNotesNew') || maparg('gb', 'n') ==# ''
nmap gb <Plug>MBNotesNew
xmap gb <Plug>MBNotesNew
nmap gbb <Plug>MBNotesNewLine
endif
g:mbnotes_loaded = 1