11local M = {}
2+ local config = require (" bullets.config" )
3+ local ordinal = require (" bullets.ordinal" )
24
35local function parse_standard (line )
46 local indent , marker , spacing , text = line :match (" ^(%s*)([-*+])(%s+)(.*)$" )
@@ -14,6 +16,127 @@ local function parse_standard(line)
1416 }
1517end
1618
19+ local function parse_numeric (line )
20+ local indent , marker , closure , spacing , text = line :match (" ^(%s*)(%d+)([.)])(%s+)(.*)$" )
21+ if not marker then
22+ return nil
23+ end
24+
25+ return {
26+ type = " num" ,
27+ indent = indent ,
28+ marker = marker ,
29+ closure = closure ,
30+ spacing = spacing ,
31+ text = text ,
32+ }
33+ end
34+
35+ local function parse_alpha (line )
36+ local max = config .options .max_alpha_characters
37+ if max == 0 then
38+ return nil
39+ end
40+
41+ local indent , marker , closure , spacing , text = line :match (" ^(%s*)(%a+)([.)])(%s+)(.*)$" )
42+ if not marker or # marker > max or not (marker == marker :lower () or marker == marker :upper ()) then
43+ return nil
44+ end
45+
46+ return {
47+ type = " abc" ,
48+ indent = indent ,
49+ marker = marker ,
50+ closure = closure ,
51+ spacing = spacing ,
52+ text = text ,
53+ }
54+ end
55+
56+ local function parse_roman (line )
57+ if not config .options .enable_roman_list then
58+ return nil
59+ end
60+
61+ local indent , marker , closure , spacing , text = line :match (" ^(%s*)(%a+)([.)])(%s+)(.*)$" )
62+ if not marker or not (marker == marker :lower () or marker == marker :upper ()) or not ordinal .is_roman (marker ) then
63+ return nil
64+ end
65+
66+ return {
67+ type = " rom" ,
68+ indent = indent ,
69+ marker = marker ,
70+ closure = closure ,
71+ spacing = spacing ,
72+ text = text ,
73+ }
74+ end
75+
76+ local function parse_line (line )
77+ local standard = parse_standard (line )
78+ if standard then
79+ standard .type = " std"
80+ return { standard }
81+ end
82+
83+ local numeric = parse_numeric (line )
84+ if numeric then
85+ return { numeric }
86+ end
87+
88+ local alpha = parse_alpha (line )
89+ local roman = parse_roman (line )
90+
91+ return vim .tbl_filter (function (item )
92+ return item ~= nil
93+ end , { alpha , roman })
94+ end
95+
96+ local resolve_bullet
97+
98+ local function previous_ordered_type (lnum , indent )
99+ for row = lnum - 1 , 1 , - 1 do
100+ local line = vim .api .nvim_buf_get_lines (0 , row - 1 , row , false )[1 ]
101+ if line == " " then
102+ return nil
103+ end
104+
105+ local bullet = resolve_bullet (parse_line (line ), row )
106+ if bullet and bullet .indent == indent and (bullet .type == " abc" or bullet .type == " rom" ) then
107+ return bullet .type
108+ end
109+ end
110+
111+ return nil
112+ end
113+
114+ function resolve_bullet (bullets , lnum )
115+ if # bullets == 0 then
116+ return nil
117+ end
118+ if # bullets == 1 then
119+ return bullets [1 ]
120+ end
121+
122+ local previous_type = previous_ordered_type (lnum , bullets [1 ].indent )
123+ if previous_type then
124+ for _ , bullet in ipairs (bullets ) do
125+ if bullet .type == previous_type then
126+ return bullet
127+ end
128+ end
129+ end
130+
131+ for _ , bullet in ipairs (bullets ) do
132+ if bullet .type == " rom" then
133+ return bullet
134+ end
135+ end
136+
137+ return bullets [1 ]
138+ end
139+
17140local function at_eol (line )
18141 return vim .fn .col (" ." ) == # line + 1
19142end
@@ -25,19 +148,73 @@ local function open_line_below()
25148 vim .cmd .startinsert ()
26149end
27150
151+ local function split_line ()
152+ local cursor = vim .api .nvim_win_get_cursor (0 )
153+ local lnum , col = cursor [1 ], cursor [2 ]
154+ local line = vim .api .nvim_get_current_line ()
155+ local before = line :sub (1 , col )
156+ local after = line :sub (col + 1 )
157+
158+ vim .api .nvim_buf_set_lines (0 , lnum - 1 , lnum , false , { before , after })
159+ vim .api .nvim_win_set_cursor (0 , { lnum + 1 , 0 })
160+ vim .cmd .startinsert ()
161+ end
162+
28163local function insert_line (lnum , line )
29164 vim .api .nvim_buf_set_lines (0 , lnum , lnum , false , { line })
30165 vim .api .nvim_win_set_cursor (0 , { lnum + 1 , # line })
31166end
32167
168+ local function pad_right (prefix , width )
169+ if not config .options .pad_right or # prefix >= width then
170+ return prefix
171+ end
172+
173+ return prefix .. string.rep (" " , width - # prefix )
174+ end
175+
176+ local function next_marker (bullet )
177+ if bullet .type == " num" then
178+ return tostring (tonumber (bullet .marker ) + 1 )
179+ end
180+ if bullet .type == " abc" then
181+ local marker =
182+ ordinal .number_to_abc (ordinal .abc_to_number (bullet .marker ) + 1 , bullet .marker == bullet .marker :lower ())
183+ if # marker > config .options .max_alpha_characters then
184+ return nil
185+ end
186+ return marker
187+ end
188+ if bullet .type == " rom" then
189+ return ordinal .number_to_roman (ordinal .roman_to_number (bullet .marker ) + 1 , bullet .marker == bullet .marker :lower ())
190+ end
191+
192+ return bullet .marker
193+ end
194+
195+ local function next_prefix (bullet )
196+ local marker = next_marker (bullet )
197+ if not marker then
198+ return nil
199+ end
200+
201+ if bullet .type == " std" then
202+ return bullet .indent .. marker .. bullet .spacing
203+ end
204+
205+ local prefix = marker .. bullet .closure .. " "
206+ return bullet .indent .. pad_right (prefix , # bullet .marker + # bullet .closure + # bullet .spacing )
207+ end
208+
33209function M .insert_new_bullet ()
34210 local mode = vim .fn .mode ()
35211 local lnum = vim .api .nvim_win_get_cursor (0 )[1 ]
36212 local line = vim .api .nvim_get_current_line ()
37- local bullet = parse_standard ( line )
213+ local bullet = resolve_bullet ( parse_line ( line ), lnum )
38214
39215 if mode ~= " n" and not at_eol (line ) then
40- return " \r "
216+ split_line ()
217+ return " "
41218 end
42219
43220 if not bullet then
@@ -46,16 +223,28 @@ function M.insert_new_bullet()
46223 return " "
47224 end
48225
49- return " \r "
226+ split_line ()
227+ return " "
50228 end
51229
52- local next_bullet = bullet .indent .. bullet .marker .. bullet .spacing
230+ local prefix = next_prefix (bullet )
231+ if not prefix then
232+ if mode == " n" then
233+ open_line_below ()
234+ return " "
235+ end
236+
237+ split_line ()
238+ return " "
239+ end
53240
54241 if mode ~= " n" then
55- return " \r \21 " .. next_bullet
242+ insert_line (lnum , prefix )
243+ vim .cmd .startinsert ({ bang = true })
244+ return " "
56245 end
57246
58- insert_line (lnum , next_bullet )
247+ insert_line (lnum , prefix )
59248 vim .cmd .startinsert ({ bang = true })
60249
61250 return " "
0 commit comments