From d74247037586ec790ad82fdf92ca2774061c10f0 Mon Sep 17 00:00:00 2001 From: Max Bucknell Date: Tue, 14 Jan 2025 00:04:28 +0000 Subject: [PATCH 1/2] Add operators for making new notes from selections. --- autoload/mbnotes.vim | 72 ++++++++++++++++++++++++++++++++++++++++++++ plugin/mbnotes.vim | 10 ++++++ 2 files changed, 82 insertions(+) diff --git a/autoload/mbnotes.vim b/autoload/mbnotes.vim index 988d68f..6cb9c7b 100644 --- a/autoload/mbnotes.vim +++ b/autoload/mbnotes.vim @@ -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": "`[\`]", + }[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 diff --git a/plugin/mbnotes.vim b/plugin/mbnotes.vim index 5ea0a8e..fef0eba 100644 --- a/plugin/mbnotes.vim +++ b/plugin/mbnotes.vim @@ -100,4 +100,14 @@ command -nargs=0 MBNotesNewSplit { mbnotes.NewNote() } +nnoremap MBNotesNew mbnotes.Operator() +xnoremap MBNotesNew mbnotes.Operator() +nnoremap MBNotesNewLine mbnotes.Operator() .. '_' + +if !hasmapto('MBNotesNew') || maparg('gb', 'n') ==# '' + nmap gb MBNotesNew + xmap gb MBNotesNew + nmap gbb MBNotesNewLine +endif + g:mbnotes_loaded = 1 From 3c8d4d84b2b0a76a7f6b491dd9bc5db2122755da Mon Sep 17 00:00:00 2001 From: Max Bucknell Date: Tue, 14 Jan 2025 00:30:47 +0000 Subject: [PATCH 2/2] Add documentation for operator and snippets. --- UltiSnips/mbnotes.snippets | 22 +++++++++-------- doc/mbnotes.txt | 49 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/UltiSnips/mbnotes.snippets b/UltiSnips/mbnotes.snippets index 534e9fa..c4d88cd 100644 --- a/UltiSnips/mbnotes.snippets +++ b/UltiSnips/mbnotes.snippets @@ -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 - diff --git a/doc/mbnotes.txt b/doc/mbnotes.txt index 42bfe62..c1fd1d3 100644 --- a/doc/mbnotes.txt +++ b/doc/mbnotes.txt @@ -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 `MBNotesNew`, and a normal mode +mapping to `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: