Skip to content

Commit 3bcb574

Browse files
MartyLakegpanders
authored andcommitted
Fix EvalBlock in destination block executing destination instead of source
When the cursor is in a named destination block (has name: but no target:), EvalBlock now searches the buffer for the source block that targets it and evaluates that instead.
1 parent 7d437ca commit 3bcb574

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

autoload/medieval.vim

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,27 @@ function! medieval#eval(...) abort
280280
return
281281
endif
282282

283+
" If cursor is in a named destination block, find and evaluate its source
284+
let opts = s:parseopts(&filetype, start - 1)
285+
if has_key(opts, 'name') && !has_key(opts, 'target')
286+
call cursor(1, 1)
287+
let pat = get(get(g:, 'medieval_option_pat', {}), &filetype, s:optionpat)
288+
while 1
289+
let srcline = search(pat . s:optspat, 'cW')
290+
if !srcline
291+
call winrestview(view)
292+
return s:error('No source block targeting "' . opts.name . '"')
293+
endif
294+
call cursor(srcline + 1, 1)
295+
if getline(srcline) =~# '\<target:\s*' . opts.name
296+
break
297+
endif
298+
endwhile
299+
call call('medieval#eval', a:000)
300+
call winrestview(view)
301+
return
302+
endif
303+
283304
call cursor(start, 1)
284305

285306
let lang = ''

test/medieval.vader

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ Execute (Eval source block with named target):
3535
Then (Output written to named target block):
3636
AssertEqual 'computed', getline(8)
3737

38+
" === EvalBlock from destination block redirects to source ===
39+
40+
Given markdown (EvalBlock in destination block evaluates its source):
41+
<!-- target: dest -->
42+
```sh
43+
echo "from source"
44+
```
45+
46+
<!-- name: dest -->
47+
```
48+
old output
49+
```
50+
51+
Execute (Cursor in destination block):
52+
8
53+
EvalBlock
54+
55+
Then (Source block was evaluated, output written to destination):
56+
AssertEqual 'from source', getline(8)
57+
3858
" === :EvalBlock {target} — explicit target argument (doc line 65) ===
3959

4060
Given markdown (Explicit target argument overrides header):

0 commit comments

Comments
 (0)