VIM 自定义语法摇滚学派

发布于 2024-12-09 15:30:08 字数 372 浏览 0 评论 0原文

我尝试压缩一些和弦文件,然后需要突出显示一些内容,使其更加直观。在这种语法中,和弦后面跟着一个逗号,然后是一个显示节拍数的数字,所有这些都用大括号括起来。这是您可能听过的一首经典摇滚歌曲的示例:

{Dm,4}Don't cry, don't {C/D,4}raise your eye
It's {Bb/D,2}on-ly {Am,2} teen-age {Dm,4hold}waste-land

我只想将和弦名称设置为一种颜色,将时间符号设置为另一种颜色...简而言之,这就是 { 和 next 之间的所有内容,作为一种颜色然后到下一个 } 作为第二种颜色。也许括号也应该是另一种颜色。我不擅长正则表达式,但我希望这个论坛可以为我指明正确的方向,最终掌握它们。

I try to compact some chord files and then need to highlight some stuff so it is more visual. This syntax is where chord is followed by a comma then a number showing how many beats, and all surrounded in curly brackets. Here is the example from a classic rock song you might have heard:

{Dm,4}Don't cry, don't {C/D,4}raise your eye
It's {Bb/D,2}on-ly {Am,2} teen-age {Dm,4hold}waste-land

I simply want to make the chord names one color, and the time notation another color... In a nutshell that's everything between { and next , as one color then up to the next } as a second color. Maybe the brackets should be another color too. I suck at regular expressions but this forum can point me in right direction to eventually master them I hope.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2024-12-16 15:30:08

这是一个非常简单的版本,仅突出显示整个 {...} 块。将以下内容放入 ${HOME}/.vim/syntax/rock.vim 中:

if exists("b:current_syntax")
    finish
endif

syntax match rockNotation      "{[^}]*}"

highlight link rockNotation    String

将以下内容放入 ${HOME}/.vim/ftDetect/rock.vim 中:

autocmd BufRead,BufNewFile *.rock set filetype=rock

现在,每当您打开 .rock 文件时,和弦都应该突出显示。 (如果检测不起作用,请尝试手动设置 :set filetype=rock,看看它是否会改变某些内容。)请注意,如果您从 Vim 编辑了这些文件,则可能需要重新启动它才能生效要应用的效果。

这是一个较长的版本,其中和弦和时间指示以不同的颜色突出显示:

if exists("b:current_syntax")
    finish
endif

syntax clear
syntax case match

setlocal iskeyword+=#                                                                         
setlocal iskeyword+=-
setlocal iskeyword+=+

syntax match rockChord /\<[A-G]\(b\|#\)\=\(m\|-\|dim\|+\|aug\|7\|m7\|Maj7\|m7b5\)\=\(\/[A-G]\(b\|#\)\=\)\=\>/ contained
syntax match rockDuration /[1-9][0-9]*\(hold\|mute\)\=/ contained

syntax region rockAnnotRegion start=/{/ end=/}/ contains=rockChord,rockDuration

highlight link rockChord          Type
highlight link rockDuration       Constant
highlight link rockAnnotRegion    Delimiter

如您所见,我的和弦正则表达式有点疯狂。它支持诸如 A#m7b5/Db 之类的东西,这当然没什么意义(而且听起来很糟糕),但你明白了。

Here is a very simple version that will just highlight the whole {...} blocks. Put the following in ${HOME}/.vim/syntax/rock.vim:

if exists("b:current_syntax")
    finish
endif

syntax match rockNotation      "{[^}]*}"

highlight link rockNotation    String

And the following in ${HOME}/.vim/ftdetect/rock.vim:

autocmd BufRead,BufNewFile *.rock set filetype=rock

Now whenever you open a .rock file, the chords should be highlighted. (If detection doesn't work, try to set :set filetype=rock manually, see if it changes something.) Note that if you edited these files from Vim, you may need to restart it for the effects to apply.

Here is a longer version, where chords and time indications are highlighted in different colors:

if exists("b:current_syntax")
    finish
endif

syntax clear
syntax case match

setlocal iskeyword+=#                                                                         
setlocal iskeyword+=-
setlocal iskeyword+=+

syntax match rockChord /\<[A-G]\(b\|#\)\=\(m\|-\|dim\|+\|aug\|7\|m7\|Maj7\|m7b5\)\=\(\/[A-G]\(b\|#\)\=\)\=\>/ contained
syntax match rockDuration /[1-9][0-9]*\(hold\|mute\)\=/ contained

syntax region rockAnnotRegion start=/{/ end=/}/ contains=rockChord,rockDuration

highlight link rockChord          Type
highlight link rockDuration       Constant
highlight link rockAnnotRegion    Delimiter

As you can see, my regexp for chords got a little wild. It supports things like A#m7b5/Db, which of course makes little sense (and sounds horrible), but you get the idea.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文