Skip to content

Commit e605a1e

Browse files
committed
.
1 parent ea6a6ea commit e605a1e

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

autoload/mde_utils.vim

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,30 @@ def UndoFormatting()
2525
endif
2626
enddef
2727

28-
export def FormatWithoutMoving(a: number = 0, b: number = 0)
29-
# To be used for formatting through autocmds
28+
export def FormatWithoutMoving(type: string = '')
29+
# 'gq' is remapped to use this function.
30+
# However, when running the 'gq' as formatter, we need to use as it is
31+
# shipped with Vim, and therefore we use the bang in 'normal!'
3032
var view = winsaveview()
3133

34+
# defer so if the function gets error of any kind, the following are run
35+
# anyways
3236
defer UndoFormatting()
33-
if a == 0 && b == 0
37+
defer winrestview(view)
38+
39+
var start = 0
40+
var end = 0
41+
if type != ''
42+
start = line("'[")
43+
end = line("']")
44+
endif
45+
46+
if start == 0 && end == 0
3447
normal! gggqG
3548
else
36-
var interval = b - a + 1
37-
silent exe $":norm! {a}gg{interval}gqq"
49+
var interval = end - start + 1
50+
silent exe $"normal! {start}gg{interval}gqq"
3851
endif
39-
40-
winrestview(view)
4152
enddef
4253

4354
export def KeysFromValue(dict: dict<string>, target_value: string): list<string>

ftplugin/markdown.vim

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ command! -buffer -nargs=0 MDEConvertLinks links.ConvertLinks()
2929
# Jump back to the previous file
3030
nnoremap <buffer> <backspace> <ScriptCmd>funcs.GoToPrevVisitedBuffer()<cr>
3131
32-
3332
# -------------- prettier ------------------------
34-
#
33+
# TODO: you may want to use the same mechanism used in my personal
34+
# after/ftplugin/python.vim, where I set the local opfunc to
35+
# FormatWithoutMoving and where I hack the 'gq' operator
3536
if markdown_extras.use_prettier
3637
if exists('g:markdown_extras_config') != 0
3738
&& has_key(g:markdown_extras_config, 'formatprg')
@@ -51,6 +52,15 @@ if markdown_extras.use_prettier
5152
augroup END
5253
endif
5354
endif
55+
56+
def SetMarkdownOpFunc()
57+
&l:opfunc = function('utils.FormatWithoutMoving')
58+
enddef
59+
60+
# Hack on 'gq'
61+
nnoremap <silent> gq <ScriptCmd>SetMarkdownOpFunc()<cr>g@
62+
xnoremap <silent> gq <ScriptCmd>SetMarkdownOpFunc()<cr>g@
63+
5464
# --------------End prettier ------------------------
5565

5666
# -------------------- pandoc -----------------------

0 commit comments

Comments
 (0)