login | register
Sat 04 of Jul, 2009 [14:15 UTC]

voip-info.org

History

vim syntax highlighting

Created by: brc007,Last modification on Fri 10 of Oct, 2008 [18:22 UTC] by Corydon76
If you make changes/bug fixes/add features to this file please either edit this version or paste yours at the end.


To use
paste the below into a text file called asterisk.vim and save it in your home dir
vim /usr/src/asterisk/configs/extensions.conf.sample
:syntax on
:source path/to/asterisk.vim

Assuming the file below is saved instead in ~/.vim/syntax/asterisk.vim, you can automate the commands above by adding the following line to the very bottom of each of your /etc/asterisk/*.conf files:
  ; vim: set ts=8 sw=2 filetype=asterisk:
This works on my latest FC2 machine without any other VIM adjustments.

or

Here's another approach to make Vim autoload the syntax file below.

First, create the file in:

 /usr/share/vim/vim63/syntax/asterisk.vim
OR
 /usr/share/vim/syntax/asterisk.vim (for Mandriva)

where all the other syntax files are. (This will cause this to affect all users, of course).

Next, make this addition to the

 /usr/share/vim/vim63/filetype.vim

file:

 " Asterisk config file
 au BufNewFile,BufRead *asterisk*/*voicemail.conf*        setf asterisk_voicemail
 au BufNewFile,BufRead *asterisk/*.conf*         setf asterisk

This will basically cause any .conf file in the /etc/asterisk directory to use the below "asterisk" type highlighting. You could make it more specific and this will naturally just pickup the .conf files in certain directories, but I found it more convenient than anything else.





" Vim syntax file
" Language:	Asterisk config file
" Maintainer:	brc007 
" Updated for 1.6.2 by Tilghman Lesher (Corydon76)
" Last Change:	2008 Oct 10 
" version 0.5
"
if version < 600
  syntax clear
elseif exists("b:current_syntax")
  finish
endif

syn sync clear
syn sync fromstart

syn keyword     asteriskTodo            TODO contained
syn match       asteriskComment         ";.*" contains=asteriskTodo
syn match       asteriskContext         "\[.\{-}\]"
syn match       asteriskExten           "^\s*exten\s*=>\?\s*[^,]\+" contains=asteriskPattern
syn match       asteriskExten           "^\s*same\s*=>\?\s*[^,]\+" contains=asteriskPriority
syn match       asteriskExten           "^\s*\(register\|channel\|ignorepat\|include\|\(no\)\?load\)\s*=>\?"
syn match       asteriskPattern         "_\(\[[[:alnum:]#*\-]\+\]\|[[:alnum:]#*\-]\)*\.\?" contained
syn match       asteriskPattern         "[^A-Za-z0-9,]\zs[[:alnum:]#*\-]\+\ze" contained
syn match       asteriskApp             ",\zs[a-zA-Z]\+\ze$"
syn match       asteriskApp             ",\zs[a-zA-Z]\+\ze("
" Digits plus oldlabel (newlabel)
syn match       asteriskPriority        "\zs[[:digit:]]\+\(+[[:alpha:]][[:alnum:]_]*\)\?\(([[:alpha:]][[:alnum:]_]*)\)\?\ze," contains=asteriskLabel
" oldlabel plus digits (newlabel)
syn match       asteriskPriority        "\zs[[:alpha:]][[:alnum:]_]*+[[:digit:]]\+\(([[:alpha:]][[:alnum:]_]*)\)\?\ze," contains=asteriskLabel
" s or n plus digits (newlabel)
syn match       asteriskPriority        "\zs[sn]\(+[[:digit:]]\+\)\?\(([[:alpha:]][[:alnum:]_]*)\)\?\ze," contains=asteriskLabel
syn match       asteriskLabel           "(\zs[[:alpha:]][[:alnum:]]*\ze)" contained
syn match       asteriskError           "^\s*#\s*[[:alnum:]]*"
syn match       asteriskInclude         "^\s*#\s*\(include\|exec\)\s.*"
syn match       asteriskVar             "\${_\{0,2}[[:alpha:]][[:alnum:]_]*\(:-\?[[:digit:]]\+\(:-\?[[:digit:]]\+\)\?\)\?}"
syn match       asteriskVar             "_\{0,2}[[:alpha:]][[:alnum:]_]*\ze="
syn match       asteriskVarLen          "\${_\{0,2}[[:alpha:]][[:alnum:]_]*(.*)\(:-\?[[:digit:]]\+\(:-\?[[:digit:]]\+\)\?\)\?}" contains=asteriskVar,asteriskVarLen,asteriskExp
syn match       asteriskVarLen          "(\zs[[:alpha:]][[:alnum:]_]*(.*)\ze=" contains=asteriskVar,asteriskVarLen,asteriskExp
syn match       asteriskExp             "\$\[.*\]" contains=asteriskVar,asteriskVarLen,asteriskExp
syn match       asteriskCodecsPermit    "^\s*\(allow\|disallow\)\s*=\s*.*$" contains=asteriskCodecs
syn match       asteriskCodecs          "\(g723\|gsm\|ulaw\|alaw\|g726\|adpcm\|slin\|lpc10\|g729\|speex\|ilbc\|all\s*$\)"
syn match       asteriskError           "^\(type\|auth\|permit\|deny\|bindaddr\|host\)\s*=.*$"
syn match       asteriskType            "^\zstype=\ze\<\(peer\|user\|friend\)\>$" contains=asteriskTypeType
syn match       asteriskTypeType        "\<\(peer\|user\|friend\)\>" contained
syn match       asteriskAuth            "^\zsauth\s*=\ze\s*\<\(md5\|rsa\|plaintext\)\>$" contains=asteriskAuthType
syn match       asteriskAuthType        "\<\(md5\|rsa\|plaintext\)\>"
syn match       asteriskAuth            "^\zs\(secret\|inkeys\|outkey\)\s*=\ze.*$"
syn match       asteriskAuth            "^\(permit\|deny\)\s*=\s*\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}/\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}\s*$" contains=asteriskIPRange
syn match       asteriskIPRange         "\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}/\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}" contained
syn match       asteriskIP              "\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}" contained
syn match       asteriskHostname        "[[:alnum:]][[:alnum:]\-\.]*\.[[:alpha:]]{2,10}" contained
syn match       asteriskPort            "\d\{1,5}" contained
syn match       asteriskSetting         "^bindaddr\s*=\s*\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}$" contains=asteriskIP
syn match       asteriskSetting         "^port\s*=\s*\d\{1,5}\s*$" contains=asteriskPort
syn match       asteriskSetting         "^host\s*=\s*\(dynamic\|\(\d\{1,3}\.\d\{1,3}\.\d\{1,3}\.\d\{1,3}\)\|\([[:alnum:]][[:alnum:]\-\.]*\.[[:alpha:]]{2,10}\)\)" contains=asteriskIP,asteriskHostname

" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
if version >= 508 || !exists("did_conf_syntax_inits")
  if version < 508
    let did_conf_syntax_inits = 1
    command -nargs=+ HiLink hi link <args>
  else
    command -nargs=+ HiLink hi def link <args>
  endif

  HiLink        asteriskComment		Comment
  HiLink        asteriskExten		String
  HiLink        asteriskContext         Preproc
  HiLink        asteriskPattern         Type
  HiLink        asteriskApp             Statement
  HiLink        asteriskInclude         Preproc
  HiLink        asteriskIncludeBad	Error
  HiLink        asteriskPriority        Preproc
  HiLink        asteriskLabel           Type
  HiLink        asteriskVar             String
  HiLink        asteriskVarLen          Function
  HiLink        asteriskExp             Type
  HiLink        asteriskCodecsPermit    Preproc
  HiLink        asteriskCodecs          String
  HiLink        asteriskType            Statement
  HiLink        asteriskTypeType        Type
  HiLink        asteriskAuth            String
  HiLink        asteriskAuthType        Type
  HiLink        asteriskIPRange         Identifier
  HiLink        asteriskIP              Identifier
  HiLink        asteriskPort            Identifier
  HiLink        asteriskHostname        Identifier
  HiLink        asteriskSetting         Statement
  HiLink        asteriskError           Error
 delcommand HiLink
endif
let b:current_syntax = "asterisk" 
" vim: ts=8 sw=2



We also have a separate configuration file for voicemail configuration now:


" Vim syntax file
" Language:	Asterisk voicemail config file
" Maintainer: Tilghman Lesher (Corydon76)
" Last Change:	2006 Mar 20 
" version 0.2
"
if version < 600
  syntax clear
elseif exists("b:current_syntax")
  finish
endif

syn sync clear
syn sync fromstart


syn keyword     asteriskTodo    TODO contained
syn match       asteriskComment         ";.*" contains=asteriskTodo
syn match       asteriskContext         "\[.\{-}\]"

" ZoneMessages
syn match       asteriskZone            "^[[:alnum:]]\+\s*=>\?\s*[[:alnum:]/_]\+|.*$" contains=zoneName,zoneDef
syn match       zoneName                "=\zs[[:alnum:]/_]\+\ze" contained
syn match       zoneDef                 "|\zs.*\ze$" contained

syn match       asteriskSetting         "\<\(format\|serveremail\|minmessage\|maxmessage\|maxgreet\|skipms\|maxsilence\|silencethreshold\|maxlogins\)="
syn match       asteriskSetting         "\<\(externnotify\|externpass\|directoryintro\|charset\|adsi\(fdn\|sec\|ver\)\|\(pager\)\?fromstring\|email\(subject\|body\|cmd\)\|tz\|cidinternalcontexts\|saydurationm\|dialout\|callback\)="
syn match       asteriskSettingBool     "\<\(attach\|pbxskip\|usedirectory\|saycid\|sayduration\|sendvoicemail\|review\|operator\|envelope\|delete\|nextaftercmd\|forcename\|forcegreeting\)=\(yes\|no\|1\|0\|true\|false\|t\|f\)"

" Individual mailbox definitions
syn match       asteriskMailbox         "^[[:digit:]]\+\s*=>\?\s*[[:digit:]]\+\(,[^,]*\(,[^,]*\(,[^,]*\(,[^,]*\)\?\)\?\)\?\)\?" contains=mailboxEmail,asteriskSetting,asteriskSettingBool,comma
syn match       mailboxEmail            ",\zs[^@=,]*@[[:alnum:]\-\.]\+\.[[:alpha:]]\{2,10}\ze" contains=comma
syn match       comma                   "[,|]" contained

" Define the default highlighting.
" For version 5.7 and earlier: only when not done already
" For version 5.8 and later: only when an item doesn't have highlighting yet
:if version >= 508 || !exists("did_conf_syntax_inits")
  if version < 508
    let did_conf_syntax_inits = 1
    command -nargs=+ HiLink hi link <args>
  else
    command -nargs=+ HiLink hi def link <args>
  endif

  HiLink        asteriskComment Comment
  HiLink        asteriskContext         Identifier
  HiLink        asteriskZone            Type
  HiLink        zoneName                String
  HiLink        zoneDef                 String
  HiLink        asteriskSetting         Type
  HiLink        asteriskSettingBool     Type

  HiLink        asteriskMailbox         Statement
  HiLink        mailboxEmail            String
 delcommand HiLink
endif

let b:current_syntax = "asterisk_voicemail"

" vim: ts=8 sw=2


Both above documents are BSD licensed. if you make any changes/improvements to these syntax files, please edit this page and add them! :-)

See Also


Comments

Comments Filter
222

333Emacs Asterisk Syntax Highlighting

by ricbat01, Monday 17 of July, 2006 [12:46:48 UTC]
Very quick hack of an emacs syntax file originally posted by Michael Taht Oct 05 on digium wiki, to provide slightly improved syntax highlighting of asterisk dialplan within emacs.
I knew nothing about el files before I began ... and knew even less when I finished ... !

1. Append these lines to the .emacs file in your home directory.
(autoload 'asterisk-mode "~/elisp/asterisk-mode.el"
 "Major mode for editing documents in Asterisk Dialplan." t)
(add-to-list 'auto-mode-alist
  '("\\.conf\\'" . asterisk-mode))

2. Create a directory in your home directory called elisp and create a file within that called asterisk-mode.el. Add the following to the file asterisk-mode.el:
;; asterisk mode
;; notes
;; quick hack of syntax highlighting for asterisk dialplan el file originally posted by Michael Taht
;; this is a crude enhancement but better than pages of text all the same colour ... appeal to experts in emacs/lisp/regex syntax to please improve
(setq max-specpdl-size 6000)
(define-generic-mode 'asterisk-mode
 ;; comments
 '(";")
 ;; keywords
 ;; these are all case sensitive ... asterisk isn't would be nice to fix
 '("AbsoluteTimeout" "AddQueueMember" "ADSIProg" "Agent" "AgentCallBackLogin" "AgentLogin"
   "AgentMonitorOutgoing" "AGI" "Answer" "AppendCDRUserField" "Authenticate"
   "Background" "Busy" "CallingPres" "ChangeMonitor" "ChanIsAvail"
   "Congestion" "Cut" "DateTime" "DBdel" "DBdeltree"
   "DBget" "DBput" "DeadAGI" "Dial" "DigitTimeout"
   "Directory" "DISA" "EAGI" "Echo" "EnumLookup"
   "Eval" "Festival" "Flash" "GetCPEID" "Goto"
   "GotoIf" "GotoIfTime" "Gosub" "Gosubif"
   "Hangup" "HasNewVoicemail" "ICES"
   "LookupBlacklist" "LookupCIDName" "Macro" "MailboxExists" "MeetMe"
   "MeetMeCount" "Milliwatt" "Monitor" "MP3Player" "MusicOnHold"
   "NBScat" "NoCDR" "NoOp" "ParkAndAnnounce" "ParkedCall"
   "Playback" "Playtones" "Prefix" "PrivacyManager" "Queue"
   "Random" "Read" "Record" "RemoveQueueMember" "ResetCDR" "Return"
   "ResponseTimeout" "Ringing" "SayDigits" "SayNumber" "SayUnixTime"
   "SendDTMF" "SendImage" "SendURL" "SetAccount" "SetCallerID"
   "SetCDRUserField" "SetCIDName" "SetCIDNum" "SetGlobalVar" "SetLanguage"
   "SetMusicOnHold" "Set" "SetVar" "SIPDtmfMode" "SoftHangup" "StopMonitor"
   "StopPlaytones" "StripLSD" "StripMSD" "SubString" "Suffix" "Swift"
   "System" "Transfer" "VoiceMail" "Voicemail" "VoiceMail2"
   "VoiceMailMain" "VoicemailMain"
   "VoiceMailMain2" "Wait" "WaitExten" "WaitForRing" "WaitMusicOnHold" "Zapateller"
   "ZapBarge" "ZapRAS" "ZapScan"
)
 ;; colours for the important bits ... need to find more faces and have
 ;; highlights within highlights ... ie ${} within $ 
 '(
   ("
\[a-zA-Z0-9-_]+\]" . 'font-lock-function-name-face)
   ("
exten=>a-zA-Z0-9_\.#*,0-9n\+*," . 'font-lock-constant-face )
   ("^include=>a-zA-Z0-9_\.#*" . 'font-lock-constant-face )
   ("A-Z+/A-Z+/\${a-zA-Z0-9:\(\)@,_${}-+}" . 'font-lock-variable-name-face)
   ("A-Z+/\${a-zA-Z0-9:\(\)@,_${}-+}" . 'font-lock-variable-name-face)
   ("A-Z+/0-9+" . 'font-lock-variable-name-face)
   ("__\${a-zA-Z0-9:\(\)@,_${}-+}" . 'font-lock-variable-name-face) 
   ("\${a-zA-Z0-9:\(\)@,_${}-+}" . 'font-lock-variable-name-face)
 )
 ;; files
 ;; left ... was only interested in improving readability of dialplans
 ;; god I hate writing dialplans ... but things are way better since gosubs appeared
 '("adsi.conf$" "cdr_pgsql.conf$" "logger.conf$" "musiconhold.conf$" "rpt.conf$"
   "zapata.conf$" "adtranvofr.conf$" "enum.conf$" "manager.conf$" "oss.conf$"
   "rtp.conf$" "agents.conf$" "extensions.conf$" "extconfig.conf$"
   "meetme.conf$" "parking.conf$"
   "sip.conf$" "alsa.conf$" "festival.conf$"   "mgcp.conf$"   "phone.conf$"
   "skinny.conf$" "asterisk.conf$" "iax.conf$" "modem.conf$" "privacy.conf$"
   "voicemail.conf$" "cdr_odbc.conf$" "indications.conf$" "modules.conf$"
  "queues.conf$" "vpb.conf$")
 nil
 "Mode for editing asterisk config files")

3. Then power up emacs and open a dialplan. Hopefully have some pretty colours rather than a wall on black text. If some keyword are not highlighted it's du to case ... either spell them as per the el file or change the el file keywords to what you use ... there doesn't seem to be any hard and fast rules on this!

222

333

by jon.webster, Friday 12 of May, 2006 [21:15:23 UTC]
changed
syn match asteriskPattern "^A-Za-z0-9,\zs[:alnum:]#*]\+\ze" contained
to
syn match asteriskPattern "^A-Za-z0-9,\zs[:alnum:]#*\-]\+\ze" contained

so in extensions like "exten => s-BUSY" s-BUSY is all one color.
222

333

by jon.webster, Friday 12 of May, 2006 [21:01:51 UTC]
changed
syn match asteriskPattern "^A-Za-z0-9,\zs[:alnum:]#*]\+\ze" contained
to
syn match asteriskPattern "^A-Za-z0-9,\zs[:alnum:]#*\-]\+\ze" contained

so in extensions like "exten => s-BUSY" s-BUSY is all one color.
222

333

by onyxvoip, Tuesday 21 of March, 2006 [18:51:45 UTC]
222

333Re:

by Corydon76, Friday 20 of January, 2006 [00:54:52 UTC]
sjobeck:

If you would like to have syntax turned on all the time, add the command 'syntax on' to the file .vimrc in your home directory. In fact, any command that you can type by using :, you can also add to your .vimrc. This file will be read each time vim starts.
222

333":syntax on" every time ?

by sjobeck, Thursday 30 of June, 2005 [07:11:33 UTC]
this is very nice, thanks, great stuff.

Quick question: must I enter ":syntax on" every single time I enter a file using vim? or is there a way to have syntx on all the time?

Thanks.

Peace.

Jason

222

333Re: Alternate match for Context

by , Tuesday 30 of November, 2004 [12:03:14 UTC]
Doh ! it should read

syn match asteriskContext "^\s*\[.*\]]"
222

333Alternate match for Context

by , Tuesday 30 of November, 2004 [12:00:20 UTC]
In my version, i've altered the match for context to the following

syn match asteriskContext "^\s*\.*\"

This way it does not match any number ranges in my extension.conf file.

Mark
222

333How to recognize .conf filetype automatically ...

by jordix, Sunday 28 of November, 2004 [13:04:53 UTC]
The large majority of .conf files that I am editing (besides bind.conf) are asterisk configuration files. So I decided to add the following entry in my /usr/share/vim/vim61/filetype.vim file :

" Asterisk
au BufNewFile,BufRead *.conf setf asterisk

As a result, every .conf file I open with vim is recognized as an asterisk configuration file.

Jordi
222

333Jedit Syntax highlighting

by , Thursday 25 of November, 2004 [09:22:53 UTC]
For all of you Jedit fans out there, here's a small hack to get s.h. for *:

http://marius.asesoft.ro/asterisk/sh.txt

Marius