@@ -58,13 +58,16 @@ enddef
5858
5959export def RemoveSurrounding (range_info: dict <list<list<number> >> = {})
6060 const style_interval = empty (range_info) ? IsInRange () : range_info
61+ echom string (style_interval)
6162 if ! empty (style_interval)
6263 const style = keys (style_interval)[0 ]
6364 const interval = values (style_interval)[0 ]
6465
66+ echom " Siamo qui!"
6567 # Remove left delimiter
6668 const lA = interval[0 ][0 ]
6769 const cA = interval[0 ][1 ]
70+ echom $ " (lA, cA): ({lA},{cA})"
6871 const lineA = getline (lA)
6972 var newline = strcharpart (lineA, 0 ,
7073 \ cA - 1 - strchars (constants.TEXT_STYLES_DICT[style].open_delim))
@@ -216,7 +219,7 @@ export def SurroundSmart(style: string, type: string = '')
216219 # so that all the styles are visible
217220
218221 # Check if A falls in an existing interval
219- cursor (lA, cA)
222+ setcursorcharpos (lA, cA)
220223 var old_right_delimiter = ' '
221224 var found_interval = IsInRange ()
222225 if ! empty (found_interval)
@@ -255,7 +258,7 @@ export def SurroundSmart(style: string, type: string = '')
255258 endif
256259
257260 # Check if B falls in an existing interval
258- cursor (lB, cB)
261+ setcursorcharpos (lB, cB)
259262 var old_left_delimiter = ' '
260263 found_interval = IsInRange ()
261264 if ! empty (found_interval)
@@ -427,13 +430,21 @@ export def IsInRange(): dict<list<list<number>>>
427430 return text_style_refined
428431 enddef
429432
433+ def SearchPosChar (pattern: string , options : string ): list <number>
434+ # Like ' searchpos()' but the column is converted in char index
435+ var [l , c ] = searchpos (pattern, options )
436+ var c_char = strchars (strpart (getline (l ), 0 , c - 1 )) + 1
437+ return [l , c_char]
438+ enddef
439+
430440 # Main function start here
431441 # text_style comes from vim - markdown
432- const text_style = synIDattr (synID (line (" ." ), charcol (" ." ), 1 ), " name" )
442+ const text_style = synIDattr (synID (line (" ." ), col (" ." ), 1 ), " name" )
443+ echom " text_style: " .. text_style
433444 const text_style_adjusted =
434445 text_style == ' markdownItalic' || text_style == ' markdownBold'
435- ? StarOrUnderscore (synIDattr (synID (line (" ." ), charcol (" ." ), 1 ), " name" ))
436- : synIDattr (synID (line (" ." ), charcol (" ." ), 1 ), " name" )
446+ ? StarOrUnderscore (synIDattr (synID (line (" ." ), col (" ." ), 1 ), " name" ))
447+ : synIDattr (synID (line (" ." ), col (" ." ), 1 ), " name" )
437448 var return_val = {}
438449
439450 if ! empty (text_style_adjusted)
@@ -445,38 +456,48 @@ export def IsInRange(): dict<list<list<number>>>
445456 const open_delim =
446457 eval ($ ' constants.TEXT_STYLES_DICT.{text_style_adjusted}.open_delim' )
447458
448- var open_delim_pos = searchpos ($ ' \V{open_delim}' , ' bW' )
449- var current_style = synIDattr (synID (line (" ." ), charcol (" ." ), 1 ), " name" )
459+ # TODO : the searchpos () return a byte index !
460+ var open_delim_pos = SearchPosChar ($ ' \V{open_delim}' , ' bW' )
461+
462+ # echom $ " open_del_pos_back: {open_delim_pos}"
463+ var current_style = synIDattr (synID (line (" ." ), col (" ." ), 1 ), " name" )
450464 # We search for a markdown delimiter or an htmlTag.
451465 while current_style != $ ' {text_style}Delimiter'
452466 && current_style != ' htmlTag'
453467 && open_delim_pos != [0 , 0 ]
454- open_delim_pos = searchpos ($ ' \V{open_delim}' , ' bW' )
455- current_style = synIDattr (synID (line (" ." ), charcol (" ." ), 1 ), " name" )
468+ open_delim_pos = SearchPosChar ($ ' \V{open_delim}' , ' bW' )
469+ current_style = synIDattr (synID (line (" ." ), col (" ." ), 1 ), " name" )
456470 endwhile
457471
458472 # To avoid infinite loops if some weird delimited text is highlighted
459473 if open_delim_pos == [0 , 0 ]
460474 return {}
461475 endif
462476 open_delim_pos[1 ] += strchars (open_delim)
477+ # echom " open del pos_back_plus_delim: " .. string (open_delim_pos)
463478
464479 # Search end delimiter .
465480 # The end delimiter may be a blank line , hence
466481 # things become a bit cumbersome.
467482 setcursorcharpos (saved_curpos[1 : 2 ])
468483 const close_delim =
469484 eval ($ ' constants.TEXT_STYLES_DICT.{text_style_adjusted}.close_delim' )
470- var close_delim_pos = searchpos ($ ' \V{close_delim}' , ' nW ' )
471- var blank_line_pos = searchpos ( $ ' ^$' , ' nW ' )
485+ var close_delim_pos = SearchPosChar ($ ' \V{close_delim}' , ' W ' )
486+ var blank_line_pos = SearchPosChar ( ' ^$' , ' W ' )
472487 var first_met = [0 , 0 ]
473- current_style = synIDattr (synID (line (" ." ), charcol (" ." ), 1 ), " name" )
488+ current_style = synIDattr (synID (line (" ." ), col (" ." ), 1 ), " name" )
489+ echom $ " close_delim_pos: '{close_delim_pos}'"
490+ echom $ " blank_line_pos: '{blank_line_pos}'"
474491
492+ # The while loop is to robustify because you ultimately want to get a
493+ # ' *Delimiter' text- style, like for example ' markdownBoldDelimiter'
475494 while current_style != $ ' {text_style}Delimiter'
476495 && current_style != ' htmlEndTag'
477496 && getline (line (' .' )) !~ ' ^$'
478- close_delim_pos = searchpos ($ ' \V{close_delim}' , ' nW' )
479- blank_line_pos = searchpos ($ ' ^$' , ' nW' )
497+ close_delim_pos = SearchPosChar ($ ' \V{close_delim}' , ' W' )
498+ blank_line_pos = SearchPosChar (' ^$' , ' W' )
499+ echom $ " close_delim_pos_inside: '{close_delim_pos}'"
500+ echom $ " blank_line_pos_inside: '{blank_line_pos}'"
480501 if close_delim_pos == [0 , 0 ]
481502 first_met = blank_line_pos
482503 elseif blank_line_pos == [0 , 0 ]
@@ -487,8 +508,9 @@ export def IsInRange(): dict<list<list<number>>>
487508 : blank_line_pos
488509 endif
489510 setcursorcharpos (first_met)
490- current_style = synIDattr (synID (line (" ." ), charcol (" ." ), 1 ), " name" )
511+ current_style = synIDattr (synID (line (" ." ), col (" ." ), 1 ), " name" )
491512 endwhile
513+ echom $ " first met_before: {first_met}"
492514
493515 # If we hit a blank line , then we take the previous line and last column,
494516 # to keep consistency in returning open - intervals
@@ -498,6 +520,7 @@ export def IsInRange(): dict<list<list<number>>>
498520 else
499521 first_met[1 ] -= 1
500522 endif
523+ echom $ " first met: {first_met}"
501524
502525 setcursorcharpos (saved_curpos[1 : 2 ])
503526 return_val = {[text_style_adjusted]: [open_delim_pos, first_met]}
0 commit comments