0

In this function I would like to substitute everything within (" and ") with emptiness

function! ListJoinFolds() abort
    0
    let bufnr = bufnr('%')
    let items = []
    while 1
        let start = search('^{{{', 'cW')
        if !start
            break
        endif
        let end = search('^}}}', 'W')
        let lines = getline(start, end)
        let lines = map(lines, 'trim(v:val)')
        call add(items, {
            \ 'bufnr': bufnr,
            \ 'lnum': start,
            \ 'text': join(lines, ' '),
        \ })
        " credits to: https://www.reddit.com/r/vim/comments/akv0wl/function_sort_output_alphabetically/ef8nlrr
        call sort(items, {a, b -> a["text"] == b["text"] ? 0 : a["text"] > b["text"] ? 1 : -1})
    endwhile
    call setloclist(winnr(), items)
    lopen
endfunction

I've added the following nearly every thinkable order I could think of, but my intelligence couldn't find the right place to succeed for the attempted success.

let lines = substitute(items, '(".\{-}")', '', 'g')

Hopefully someone could assist me with this.

Thank you.

PS

I've also posted this question to:

reddit.com/r/vim (link)

If question solved, I will post it here, and vice versa.

2
  • You might want to update vi.stackexchange.com/questions/18733/… now that you've solved that problem.
    – user4879
    Commented Feb 9, 2019 at 6:19
  • Braces can be embedded. Even with a correct syntax, we can have things like f(g(), h(y())).. And unfortunately, regexes are really bad with recursive patterns. searchpair() could have helped, but it works on buffers, not on strings. May be there is a Python library somewhere that can help, in that case it may be better to delegate your problem to that library from your vim function. Commented Jul 9, 2019 at 17:25

1 Answer 1

1

substitute() only works on a string. You appear to have been trying it with the items list. Does something like this work for you (as a modification of the part where you add the text to the items list)?

    call add(items, {
        \ 'bufnr': bufnr,
        \ 'lnum': start,
        \ 'text': substitute(join(lines, ' '), '(".\{-}")', '', 'g'),
    \ })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.