@@ -2,6 +2,8 @@ vim9script
22
33import autoload " ./mde_constants.vim" as constants
44
5+ export var save_surround_view = {}
6+
57export def Echoerr (msg: string )
68 echohl ErrorMsg | echom $ ' [markdown_extras] {msg}' | echohl None
79enddef
@@ -58,16 +60,13 @@ enddef
5860
5961export 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
105104enddef
106105
106+ # TODO : REMOVE ME (also docs and config dict )
107107export 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
339339enddef
340340
341341export 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]}
0 commit comments