]> Sergey Matveev's repositories - dotfiles.git/commitdiff
Simplify buftabline and support filenames with spaces
authorSergey Matveev <stargrave@stargrave.org>
Wed, 12 Aug 2020 21:52:41 +0000 (00:52 +0300)
committerSergey Matveev <stargrave@stargrave.org>
Wed, 12 Aug 2020 21:52:41 +0000 (00:52 +0300)
vim/.vim/plugin/buftabline.vim

index 4e48497bed24e1b63643f3c63f56335c40124b71..6c4d9336bbdbc1d63ed2c8b52723bb47488d6d5c 100644 (file)
@@ -4,25 +4,17 @@ function! BufTabline()
     redir END
     let bufs = []
     for buf in split(bufsRaw, "\n")[:16]
-        let buf = substitute(buf, "\"", "", "g")
-        let ents = []
-        for ent in split(buf, " ")
-            if len(ent) == 0 | continue | endif
-            let ents = add(ents, substitute(ent, "%", "%%", "g"))
-        endfor
-        let linenum = ents[-1]
-        let ents = ents[:-3] " strip message with line number
-        if len(ents) == 4 | let ents = [ents[0], ents[1] . ents[2], ents[3]] | endif
-        if len(ents) == 3 | let ents = [ents[0] . ":" . ents[1], ents[2]] | endif
-        let ents[0] = "%#TabLine#" . ents[0] . "%*"
-        if len(ents[1]) > 20 | let ents[1] = "<" . ents[1][-(20-1):] | endif
-        let ents[1] = ents[1] . ":" . linenum
-        if match(ents[0], "%%") == -1
-            let ents[1] = "%#TabLine#:" . ents[1] . "%*"
-        else
-            let ents[1] = "%#TabLineSel#" . ents[1] . "%*"
-        endif
-        let bufs = add(bufs, join(ents[:1], ""))
+        let leftIdx = stridx(buf, '"')
+        let rightIdx = strridx(buf, '"')
+        let filename = buf[leftIdx + 1 : rightIdx - 1]
+        let linenum = split(buf[rightIdx:], " ")[-1]
+        let attrs = split(buf[:leftIdx-1], '\s\+')
+        let attrs = map(attrs, 'substitute(v:val, "%", "%%", "g")')
+        let attrs = (len(attrs) == 1) ? attrs[0] : attrs[0] . ":" . join(attrs[1:], "")
+        let attrs = "%#TabLine#" . attrs . "%*"
+        if len(filename) > 20 | let filename = "<" . filename[-(20-1):] | endif
+        let hl = (match(attrs, "%%") == -1) ? "%#TabLine#:" : "%#TabLineSel#"
+        let bufs = add(bufs, attrs . hl . filename . ":" . linenum . "%*")
     endfor
     return join(bufs, "  ")
 endfunction