Skip to content

Commit 3a8ace9

Browse files
committed
Working CR hacked
1 parent 14af2a5 commit 3a8ace9

File tree

4 files changed

+83
-42
lines changed

4 files changed

+83
-42
lines changed

after/ftplugin/markdown.vim

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,14 @@ endif
126126
# This is very ugly: you add a - [ ] by pasting the content of register 'o'
127127
setreg("o", "- [ ] ")
128128

129-
# Redefinition of <cr>
129+
# Redefinition of <cr>. Unmap if user does not want it.
130130
inoremap <buffer> <silent> <CR> <ScriptCmd>funcs.CR_Hacked()<CR>
131+
# if !exists('g:markdown_extras_config')
132+
# && has_key(g:markdown_extras_config, 'hack_CR')
133+
# && !g:markdown_extras_config['hack_CR']
134+
# iunmap <buffer> <cr>
135+
# endif
136+
131137
nnoremap <buffer> <expr> <CR> empty(links.IsLink())
132138
\ ? '<ScriptCmd>SetLinkOpFunc()<CR>g@iw'
133139
\ : '<ScriptCmd>links.OpenLink()<CR>'

lib/funcs.vim

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

lib/links.vim

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ export def OpenLink()
205205
if filereadable(link)
206206
exe $'edit {link}'
207207
elseif exists(':Open') != 0
208+
echom link
208209
exe $":Open {link}"
209210
else
210211
utils.Echowarn('You need a Vim version that has the :Open command')

test/test_markdown_extras.vim

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,27 @@ def g:Test_check_uncheck_todo_keybinding()
227227
:%bw!
228228
Cleanup_testfile(src_name_1)
229229
enddef
230+
231+
def g:Test_CR_hacked_wrapped_lines()
232+
# OBS! normal shall be without ! because we are testing <CR> which is
233+
# hacked!
234+
Generate_testfile(lines_1, src_name_1)
235+
vnew
236+
exe $"edit {src_name_1}"
237+
setlocal spell spelllang=la
238+
setlocal textwidth=78
239+
240+
const long_line = 'Itaque earum rerum hic *tenetur a sapiente `delectus`, '
241+
.. 'ut aut reiciendis voluptatibus maiores*'
242+
const expected_lines_36_38 = [
243+
'- Itaque earum rerum hic *tenetur a sapiente `delectus`, ut aut reiciendis',
244+
' voluptatibus maiores*',
245+
'- hello'
246+
]
247+
248+
execute $"silent norm Go\<cr>-\<space>{long_line}\<cr>hello"
249+
echom assert_equal(expected_lines_36_38, getline(36, 38))
250+
251+
:%bw!
252+
Cleanup_testfile(src_name_1)
253+
enddef

0 commit comments

Comments
 (0)