Skip to content

Commit 430de46

Browse files
committed
Added multibyte support
1 parent b664635 commit 430de46

File tree

4 files changed

+123
-62
lines changed

4 files changed

+123
-62
lines changed

autoload/mde_funcs.vim

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export def CR_Hacked()
104104
line_nr -= 1
105105
current_line = getline(line_nr)
106106
item_symbol = GetItemSymbol(current_line)
107-
echom item_symbol
107+
# echom item_symbol
108108
if !empty(item_symbol)
109109
break
110110
endif
@@ -145,7 +145,6 @@ enddef
145145
export def RemoveAll()
146146
# TODO could be refactored to increase speed, but it may not be necessary
147147
const range_info = utils.IsInRange()
148-
echom "A: " .. string(range_info)
149148
const prop_info = highlight.IsOnProp()
150149
const syn_info = synIDattr(synID(line("."), charcol("."), 1), "name")
151150
const is_quote_block = getline('.') =~ '^>\s'

autoload/mde_utils.vim

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ vim9script
22

33
import autoload "./mde_constants.vim" as constants
44

5+
export var save_surround_view = {}
6+
57
export def Echoerr(msg: string)
68
echohl ErrorMsg | echom $'[markdown_extras] {msg}' | echohl None
79
enddef
@@ -58,16 +60,13 @@ enddef
5860

5961
export def RemoveSurrounding(range_info: dict<list<list<number>>> = {})
6062
const style_interval = empty(range_info) ? IsInRange() : range_info
61-
echom string(style_interval)
6263
if !empty(style_interval)
6364
const style = keys(style_interval)[0]
6465
const interval = values(style_interval)[0]
6566

66-
echom "Siamo qui!"
6767
# Remove left delimiter
6868
const lA = interval[0][0]
6969
const cA = interval[0][1]
70-
echom $"(lA, cA): ({lA},{cA})"
7170
const lineA = getline(lA)
7271
var newline = strcharpart(lineA, 0,
7372
\ cA - 1 - strchars(constants.TEXT_STYLES_DICT[style].open_delim))
@@ -104,6 +103,7 @@ export def RemoveSurrounding(range_info: dict<list<list<number>>> = {})
104103
endif
105104
enddef
106105

106+
# TODO: REMOVE ME (also docs and config dict)
107107
export def SurroundSimple(style: string, type: string = '')
108108

109109
if getcharpos("'[") == getcharpos("']")
@@ -294,14 +294,9 @@ export def SurroundSmart(style: string, type: string = '')
294294
if style != 'markdownCode'
295295
A_to_B = RemoveDelimiters(A_to_B)
296296
endif
297-
# echom $'toA: ' .. toA
298-
# echom $'fromB: ' .. fromB
299-
# echom $'A_to_B:' .. A_to_B
300-
# echom '----------\n'
301297

302298
# Set the whole line
303299
setline(lA, toA .. A_to_B .. fromB)
304-
305300
else
306301
# Set line A
307302
var afterA = strcharpart(getline(lA), cA - 1)
@@ -336,6 +331,11 @@ export def SurroundSmart(style: string, type: string = '')
336331
ii += 1
337332
endwhile
338333
endif
334+
335+
if !empty('save_surround_view')
336+
winrestview(save_surround_view)
337+
save_surround_view = {}
338+
endif
339339
enddef
340340

341341
export def IsLess(l1: list<number>, l2: list<number>): bool
@@ -433,14 +433,17 @@ export def IsInRange(): dict<list<list<number>>>
433433
def SearchPosChar(pattern: string, options: string): list<number>
434434
# Like 'searchpos()' but the column is converted in char index
435435
var [l, c] = searchpos(pattern, options)
436-
var c_char = strchars(strpart(getline(l), 0, c - 1)) + 1
436+
var c_char = charidx(getline(l), c - 1) + 1
437437
return [l, c_char]
438438
enddef
439439

440+
# ================================
440441
# Main function start here
441-
# text_style comes from vim-markdown
442+
# ================================
443+
# TODO: text_style comes from vim-markdown. If vim-markdown changes, this will.
444+
# It is not enough to find separators, but such separators must be
445+
# named '*Delimiter' according to synIDAttr()
442446
const text_style = synIDattr(synID(line("."), col("."), 1), "name")
443-
echom "text_style: " .. text_style
444447
const text_style_adjusted =
445448
text_style == 'markdownItalic' || text_style == 'markdownBold'
446449
? StarOrUnderscore(synIDattr(synID(line("."), col("."), 1), "name"))
@@ -456,10 +459,8 @@ export def IsInRange(): dict<list<list<number>>>
456459
const open_delim =
457460
eval($'constants.TEXT_STYLES_DICT.{text_style_adjusted}.open_delim')
458461

459-
# TODO: the searchpos() return a byte index!
460462
var open_delim_pos = SearchPosChar($'\V{open_delim}', 'bW')
461463

462-
# echom $"open_del_pos_back: {open_delim_pos}"
463464
var current_style = synIDattr(synID(line("."), col("."), 1), "name")
464465
# We search for a markdown delimiter or an htmlTag.
465466
while current_style != $'{text_style}Delimiter'
@@ -474,30 +475,26 @@ export def IsInRange(): dict<list<list<number>>>
474475
return {}
475476
endif
476477
open_delim_pos[1] += strchars(open_delim)
477-
# echom "open del pos_back_plus_delim: " .. string(open_delim_pos)
478478

479-
# Search end delimiter.
479+
# ----- Search end delimiter. -------
480480
# The end delimiter may be a blank line, hence
481481
# things become a bit cumbersome.
482482
setcursorcharpos(saved_curpos[1 : 2])
483483
const close_delim =
484484
eval($'constants.TEXT_STYLES_DICT.{text_style_adjusted}.close_delim')
485-
var close_delim_pos = SearchPosChar($'\V{close_delim}', 'W')
486-
var blank_line_pos = SearchPosChar('^$', 'W')
485+
var close_delim_pos = SearchPosChar($'\V{close_delim}', 'nW')
486+
var blank_line_pos = SearchPosChar('^$', 'nW')
487487
var first_met = [0, 0]
488488
current_style = synIDattr(synID(line("."), col("."), 1), "name")
489-
echom $"close_delim_pos: '{close_delim_pos}'"
490-
echom $"blank_line_pos: '{blank_line_pos}'"
491489

492490
# The while loop is to robustify because you ultimately want to get a
493-
# '*Delimiter' text-style, like for example 'markdownBoldDelimiter'
491+
# '*Delimiter' text-style, like for example 'markdownBoldDelimiter' and
492+
# not just land on a '**'.
494493
while current_style != $'{text_style}Delimiter'
495494
&& current_style != 'htmlEndTag'
496495
&& getline(line('.')) !~ '^$'
497496
close_delim_pos = SearchPosChar($'\V{close_delim}', 'W')
498497
blank_line_pos = SearchPosChar('^$', 'W')
499-
echom $"close_delim_pos_inside: '{close_delim_pos}'"
500-
echom $"blank_line_pos_inside: '{blank_line_pos}'"
501498
if close_delim_pos == [0, 0]
502499
first_met = blank_line_pos
503500
elseif blank_line_pos == [0, 0]
@@ -510,7 +507,6 @@ export def IsInRange(): dict<list<list<number>>>
510507
setcursorcharpos(first_met)
511508
current_style = synIDattr(synID(line("."), col("."), 1), "name")
512509
endwhile
513-
echom $"first met_before: {first_met}"
514510

515511
# If we hit a blank line, then we take the previous line and last column,
516512
# to keep consistency in returning open-intervals
@@ -520,7 +516,6 @@ export def IsInRange(): dict<list<list<number>>>
520516
else
521517
first_met[1] -= 1
522518
endif
523-
echom $"first met: {first_met}"
524519

525520
setcursorcharpos(saved_curpos[1 : 2])
526521
return_val = {[text_style_adjusted]: [open_delim_pos, first_met]}

ftplugin/markdown.vim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ if markdown_extras.use_prettier
5454
endif
5555

5656
def SetMarkdownOpFunc()
57+
utils.save_surround_view = winsaveview()
5758
&l:opfunc = function('utils.FormatWithoutMoving')
5859
enddef
5960

@@ -136,6 +137,7 @@ if empty(maparg('<Plug>MarkdownToggleCheck'))
136137
endif
137138

138139
def SetLinkOpFunc()
140+
utils.save_surround_view = winsaveview()
139141
&l:opfunc = function(links.CreateLink)
140142
enddef
141143

@@ -170,6 +172,7 @@ if exists('g:markdown_extras_config')
170172
endif
171173

172174
def SetSurroundOpFunc(style: string)
175+
utils.save_surround_view = winsaveview()
173176
&l:opfunc = function(Surround, [style])
174177
enddef
175178

@@ -214,6 +217,7 @@ if empty(maparg('<Plug>MarkdownRemove'))
214217
endif
215218

216219
def SetHighlightOpFunc()
220+
utils.save_surround_view = winsaveview()
217221
&l:opfunc = function(highlights.AddProp)
218222
enddef
219223

@@ -223,6 +227,7 @@ if empty(maparg('<Plug>MarkdownHighlight'))
223227
endif
224228

225229
def SetCodeBlock()
230+
utils.save_surround_view = winsaveview()
226231
&l:opfunc = function(utils.SetBlock)
227232
enddef
228233

@@ -233,6 +238,7 @@ endif
233238

234239

235240
def SetQuoteBlockOpFunc()
241+
utils.save_surround_view = winsaveview()
236242
&l:opfunc = function(utils.SetQuoteBlock)
237243
enddef
238244

0 commit comments

Comments
 (0)