11local M = {}
2+ local config = require (" bullets.config" )
3+ local ordinal = require (" bullets.ordinal" )
24
35local function feed (keys )
46 vim .api .nvim_feedkeys (vim .api .nvim_replace_termcodes (keys , true , false , true ), " n" , false )
@@ -18,6 +20,127 @@ local function parse_standard(line)
1820 }
1921end
2022
23+ local function parse_numeric (line )
24+ local indent , marker , closure , spacing , text = line :match (" ^(%s*)(%d+)([.)])(%s+)(.*)$" )
25+ if not marker then
26+ return nil
27+ end
28+
29+ return {
30+ type = " num" ,
31+ indent = indent ,
32+ marker = marker ,
33+ closure = closure ,
34+ spacing = spacing ,
35+ text = text ,
36+ }
37+ end
38+
39+ local function parse_alpha (line )
40+ local max = config .options .max_alpha_characters
41+ if max == 0 then
42+ return nil
43+ end
44+
45+ local indent , marker , closure , spacing , text = line :match (" ^(%s*)(%a+)([.)])(%s+)(.*)$" )
46+ if not marker or # marker > max or not (marker == marker :lower () or marker == marker :upper ()) then
47+ return nil
48+ end
49+
50+ return {
51+ type = " abc" ,
52+ indent = indent ,
53+ marker = marker ,
54+ closure = closure ,
55+ spacing = spacing ,
56+ text = text ,
57+ }
58+ end
59+
60+ local function parse_roman (line )
61+ if not config .options .enable_roman_list then
62+ return nil
63+ end
64+
65+ local indent , marker , closure , spacing , text = line :match (" ^(%s*)(%a+)([.)])(%s+)(.*)$" )
66+ if not marker or not (marker == marker :lower () or marker == marker :upper ()) or not ordinal .is_roman (marker ) then
67+ return nil
68+ end
69+
70+ return {
71+ type = " rom" ,
72+ indent = indent ,
73+ marker = marker ,
74+ closure = closure ,
75+ spacing = spacing ,
76+ text = text ,
77+ }
78+ end
79+
80+ local function parse_line (line )
81+ local standard = parse_standard (line )
82+ if standard then
83+ standard .type = " std"
84+ return { standard }
85+ end
86+
87+ local numeric = parse_numeric (line )
88+ if numeric then
89+ return { numeric }
90+ end
91+
92+ local alpha = parse_alpha (line )
93+ local roman = parse_roman (line )
94+
95+ return vim .tbl_filter (function (item )
96+ return item ~= nil
97+ end , { alpha , roman })
98+ end
99+
100+ local resolve_bullet
101+
102+ local function previous_ordered_type (lnum , indent )
103+ for row = lnum - 1 , 1 , - 1 do
104+ local line = vim .api .nvim_buf_get_lines (0 , row - 1 , row , false )[1 ]
105+ if line == " " then
106+ return nil
107+ end
108+
109+ local bullet = resolve_bullet (parse_line (line ), row )
110+ if bullet and bullet .indent == indent and (bullet .type == " abc" or bullet .type == " rom" ) then
111+ return bullet .type
112+ end
113+ end
114+
115+ return nil
116+ end
117+
118+ function resolve_bullet (bullets , lnum )
119+ if # bullets == 0 then
120+ return nil
121+ end
122+ if # bullets == 1 then
123+ return bullets [1 ]
124+ end
125+
126+ local previous_type = previous_ordered_type (lnum , bullets [1 ].indent )
127+ if previous_type then
128+ for _ , bullet in ipairs (bullets ) do
129+ if bullet .type == previous_type then
130+ return bullet
131+ end
132+ end
133+ end
134+
135+ for _ , bullet in ipairs (bullets ) do
136+ if bullet .type == " rom" then
137+ return bullet
138+ end
139+ end
140+
141+ return bullets [1 ]
142+ end
143+
21144local function at_eol (line )
22145 return vim .fn .col (" ." ) == # line + 1
23146end
@@ -27,11 +150,52 @@ local function insert_line(lnum, line)
27150 vim .api .nvim_win_set_cursor (0 , { lnum + 1 , # line })
28151end
29152
153+ local function pad_right (prefix , width )
154+ if not config .options .pad_right or # prefix >= width then
155+ return prefix
156+ end
157+
158+ return prefix .. string.rep (" " , width - # prefix )
159+ end
160+
161+ local function next_marker (bullet )
162+ if bullet .type == " num" then
163+ return tostring (tonumber (bullet .marker ) + 1 )
164+ end
165+ if bullet .type == " abc" then
166+ local marker =
167+ ordinal .number_to_abc (ordinal .abc_to_number (bullet .marker ) + 1 , bullet .marker == bullet .marker :lower ())
168+ if # marker > config .options .max_alpha_characters then
169+ return nil
170+ end
171+ return marker
172+ end
173+ if bullet .type == " rom" then
174+ return ordinal .number_to_roman (ordinal .roman_to_number (bullet .marker ) + 1 , bullet .marker == bullet .marker :lower ())
175+ end
176+
177+ return bullet .marker
178+ end
179+
180+ local function next_prefix (bullet )
181+ local marker = next_marker (bullet )
182+ if not marker then
183+ return nil
184+ end
185+
186+ if bullet .type == " std" then
187+ return bullet .indent .. marker .. bullet .spacing
188+ end
189+
190+ local prefix = marker .. bullet .closure .. " "
191+ return bullet .indent .. pad_right (prefix , # bullet .marker + # bullet .closure + # bullet .spacing )
192+ end
193+
30194function M .insert_new_bullet ()
31195 local mode = vim .fn .mode ()
32196 local lnum = vim .api .nvim_win_get_cursor (0 )[1 ]
33197 local line = vim .api .nvim_get_current_line ()
34- local bullet = parse_standard ( line )
198+ local bullet = resolve_bullet ( parse_line ( line ), lnum )
35199
36200 if mode ~= " n" and not at_eol (line ) then
37201 feed (" <CR>" )
@@ -48,7 +212,13 @@ function M.insert_new_bullet()
48212 return " "
49213 end
50214
51- insert_line (lnum , bullet .indent .. bullet .marker .. bullet .spacing )
215+ local prefix = next_prefix (bullet )
216+ if not prefix then
217+ feed (" <CR>" )
218+ return " "
219+ end
220+
221+ insert_line (lnum , prefix )
52222
53223 if mode == " n" then
54224 vim .cmd .startinsert ({ bang = true })
0 commit comments