@@ -43,45 +43,14 @@ export def CR_Hacked()
4343 # Check if the current line starts with ' - [ ]' or ' - '
4444 # OBS! If there are issues, check ' formatlistpat' value for markdown
4545 # filetype
46-
4746 var variant_1 = ' -\s\[\(\s*\|x\)*\]\s\+' # - [ ] bla bla bla
4847 var variant_2 = ' -\s\+\(\[\)\@!' # - bla bla bla
4948 var variant_3 = ' \*\s\+' # * bla bla bla
5049 var variant_4 = ' \d\+\.\s\+' # 123 . bla bla bla
5150 var variant_5 = ' >\s\+' # Quoted block
5251
53- var current_line = getline (' .' )
54-
55- # Check if the current line is an item.
56- # OBS! The following scan the current line through the less general regex (a
57- # regex can be contained in another regex)
58- # TODO : search back the previous \n
59- var is_item = false
60- for variant in [variant_1, variant_2, variant_3, variant_4, variant_5]
61- if current_line = ~ $ ' ^\s*{variant}\s*'
62- is_item = true
63- break
64- endif
65- endfor
66-
67- # If the current line is not in an item list , act as normal ,
68- # i .e . <cr> = \n , otherwise split the current line depending on where is the
69- # cursor
70- var this_line = strcharpart (getline (' .' ), 0 , col (' .' ) - 1 )
71- var next_line = strcharpart (getline (' .' ), col (' .' ) - 1 )
72-
73- # double <cr> equal to finish the itemization
74- if this_line = ~
75- $ ' ^\s*\({variant_1}\|{variant_2}\|{variant_3}'
76- .. $ ' \|{variant_4}\|{variant_5}\)\s*$'
77- && next_line = ~ ' ^\s*$'
78- this_line = ' '
79- is_item = false
80- endif
81-
82- # Handle different cases if the current line is an item of a list
83- var item_symbol = ' '
84- if is_item
52+ def GetItemSymbol (current_line: string ): string
53+ var item_symbol = ' '
8554 if current_line = ~ $ ' ^\s*{variant_1}'
8655 # If - [x ], the next item should be - [ ] anyway.
8756 item_symbol = $ " {current_line->matchstr($'^\s*{variant_1}')
@@ -99,20 +68,61 @@ export def CR_Hacked()
9968 )
10069 item_symbol = $ " {current_line->matchstr($'^\s*{variant_4}')
10170 \ - >substitute (string (curr_nr), string (curr_nr + 1 ), ' ' )}"
71+ # elseif current_line = ~ $ ' ^\s\+'
72+ # item_symbol = $ " {current_line->matchstr($'^\s\+ ')}"
10273 endif
74+ return item_symbol
75+ enddef
10376
104- # The following is in case the cursor is on the lhs of the item_symbol
105- if col (' .' ) < len (item_symbol)
106- if current_line = ~ $ ' ^\s*{variant_4}'
107- this_line = $ " {current_line->matchstr($'^\s *{variant_4}')}"
108- next_line = strcharpart (current_line, len (item_symbol))
109- else
110- this_line = item_symbol
111- next_line = strcharpart (current_line, len (item_symbol))
77+ # Break line at cursor position
78+ var this_line = strcharpart (getline (' .' ), 0 , col (' .' ) - 1 )
79+ var next_line = strcharpart (getline (' .' ), col (' .' ) - 1 )
80+
81+ # Check if the current line is an item.
82+ # OBS! The following scan the current line through the less general regex (a
83+ # regex can be contained in another regex)
84+ # TODO : search back the previous \n
85+ # var is_item = false
86+ # for variant in [variant_1, variant_2, variant_3, variant_4, variant_5]
87+ # if current_line = ~ $ ' ^\s*{variant}\s*'
88+ # is_item = true
89+ # break
90+ # endif
91+ # endfor
92+
93+ # Handle different cases if the current line is an item of a list
94+ var line_nr = line (' .' )
95+ var current_line = getline (line_nr)
96+ var item_symbol = GetItemSymbol (current_line)
97+ if current_line = ~ ' ^\s\{2,}'
98+ while current_line !~ ' ^\s*$' && line_nr != 0 && empty (item_symbol)
99+ line_nr -= 1
100+ current_line = getline (line_nr)
101+ item_symbol = GetItemSymbol (current_line)
102+ echom item_symbol
103+ if ! empty (item_symbol)
104+ break
112105 endif
106+ endwhile
107+ endif
108+
109+ # The following is in case the cursor is on the lhs of the item_symbol
110+ if col (' .' ) < len (item_symbol)
111+ if current_line = ~ $ ' ^\s*{variant_4}'
112+ this_line = $ " {current_line->matchstr($'^\s *{variant_4}')}"
113+ next_line = strcharpart (current_line, len (item_symbol))
114+ else
115+ this_line = item_symbol
116+ next_line = strcharpart (current_line, len (item_symbol))
113117 endif
114118 endif
115119
120+ # double <cr> equal to finish the itemization
121+ if getline (' .' ) == item_symbol || getline (' .' ) = ~ ' ^\s*\d\+\.\s*$'
122+ this_line = ' '
123+ item_symbol = ' '
124+ endif
125+
116126 # Add the correct lines
117127 setline (line (' .' ), this_line)
118128 append (line (' .' ), item_symbol .. next_line)
0 commit comments