├── LICENSE ├── README.md └── plugin └── syntaxMarkerFold.vim /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2020-2025 Jorenar 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be 11 | included in all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 14 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | DEALINGS IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | syntaxMarkerFold 2 | ================ 3 | 4 | Emulates marker folds while using _syntax_ [`foldmethod`](https://vimhelp.org/fold.txt.html#fold-methods) 5 | 6 | ## Installation 7 | 8 | #### [vim-plug](https://github.com/junegunn/vim-plug): 9 | ```vim 10 | Plug 'Jorenar/vim-syntaxMarkerFold' 11 | ``` 12 | 13 | #### Vim's packages 14 | ```bash 15 | cd ~/.vim/pack/plugins/start 16 | git clone git://github.com/Jorenar/vim-syntaxMarkerFold.git 17 | ``` 18 | 19 | ## Usage 20 | 21 | You only need to install this plugin and have `foldmethod` set to `syntax`. 22 | Markers should behave the way described in `:h fold-marker` (except `zf` 23 | and `zd` mappings). 24 | 25 | ## Configuration 26 | 27 | By default the max level of leveled marker (i.e. `{{{1`, `{{{2`, ...) is limited 28 | to 5. If you wish to increase it, set `g:syntaxMarkerFold_maxlevel` to desired 29 | value (local to buffer `b:syntaxMarkerFold_maxlevel` is also available). 30 | 31 | Matching pairs markers are independent from this variable. 32 | 33 | #### Minimal _vimrc_ setup 34 | ```vim 35 | filetype plugin on " must be before `syntax enable` 36 | syntax enable 37 | set foldmethod=syntax 38 | ``` 39 | -------------------------------------------------------------------------------- /plugin/syntaxMarkerFold.vim: -------------------------------------------------------------------------------- 1 | " syntaxMarkerFold 2 | " Maintainer: Jorengarenar 3 | " License: MIT 4 | 5 | if exists("g:loaded_syntaxMarkerFold") | finish | endif 6 | let s:cpo_save = &cpo | set cpo&vim 7 | 8 | let s:attr = " matchgroup=Comment transparent fold containedin=ALL" 9 | 10 | function! s:genLeveled(lvl) abort 11 | let l:higherLvls = "" 12 | for l:i in range(a:lvl-1, 0, -1) 13 | let l:higherLvls .= " end = ".s:d.'\ze' . s:op[1:] . l:i . s:ed 14 | let l:higherLvls .= " end = ".s:d.'\ze' . s:cl[1:] . l:i . s:ed 15 | endfor 16 | 17 | exec "syn region syntaxMarkerFold" . s:attr 18 | \ "start = " . s:op . a:lvl . s:ed 19 | \ "end = " . s:cl . a:lvl . s:ed 20 | \ "end = " . s:d . '\ze' . s:op[1:] . a:lvl . s:ed 21 | \ l:higherLvls 22 | \ "end = " . s:d . '\ze' . s:cl[1:] . s:ed[:1] . '$' . s:d 23 | endfunction 24 | 25 | function! s:init() abort 26 | let l:comm = split(&l:comments, ',') 27 | call map(l:comm, 'substitute(v:val, ".\\{-}:", "", "")') 28 | let l:comm = '\%(' . join(l:comm, '\|') . '\)' 29 | 30 | let s:d = l:comm !~ "/" ? "/" : "'" " pattern delimiter 31 | let [ l:m1, l:m2 ] = split(&l:foldmarker, ",") 32 | 33 | let s:st = s:d . '\V\s\*' . l:comm . '\.\{-}' 34 | let s:op = s:st . l:m1 35 | let s:cl = s:st . l:m2 36 | let s:ed = '\v(\s.*)?' . s:d 37 | 38 | silent! syn clear syntaxMarkerFold 39 | exec "syn region syntaxMarkerFold" . s:attr 40 | \ "start = " . s:op . s:ed 41 | \ "end = " . s:cl . s:ed 42 | 43 | let l:maxlvl = get(b:, "syntaxMarkerFold_maxlevel", get(g:, "syntaxMarkerFold_maxlevel", 5)) 44 | for l:lvl in range(1, l:maxlvl) 45 | call s:genLeveled(l:lvl) 46 | endfor 47 | endfunction 48 | 49 | augroup syntaxMarkerFold 50 | autocmd! 51 | autocmd Syntax *\v\c(off)@