After years editing Elmer sif files with Emacs, I have realized today how easy is to create an Emacs mode for sif files and put a bit of colour in them.
This is my first attempt, essentially a copy and paste from the emacs generic mode tutorial in http://www.emacswiki.org/emacs/GenericMode . The following code have to be added to the ".emacs" file.
(require 'generic-x)
(define-generic-mode
'sif-mode ;; name of the mode to create
'("!" "#") ;; comments start with '!' or "#'
'("Header" "Constants" "Material" "End" "Solver" "Boundary Condition"
"Simulation" "Body" "Body Force" "Initial Condition" "Equation") ;; some keywords
'(("=" . 'font-lock-operator) ;; This doesn't do anything!
("$$" . 'font-lock-builtin) ;; or this!
("Real" "Logical" "String" "Integer" . 'font-lock-type-face)) ;; or this!
'("\\.sif$") ;; files for which to activate this mode
nil ;; other functions to call
"A mode for sif files" ;; doc string for this mode
)
It doesn't work perfectly but comments, keywords and MATC expresions look great.
I am just wondering if anyone has been working on this and doesn't mind to share it. Comments welcomed!
if version < 600
syn clear
elseif exists("b:current_syntax")
finish
endif
syn case ignore
syn keyword SifType Real Integer Logical String File Procedure MATC
syn match SifNumber display "[+-]\=\<\d\+\>"
syn match SifFloat "\<\d\+\(\.\d*\)\=\([edED][-+]\=\d\+\)\=[ij]\=\>"
syn match SifFloat "\.\d\+\([edED][-+]\=\d\+\)\=[ij]\=\>"
syn region SifString start=+"+ end=+"+ oneline
syn match SifComment "!.*"
syn region SifSolvers start="Linear" end="="
syn region SifSolvers start="nonlinear" end="="
syn match SifSolverHeader "Exec\s*Solver\s*="
syn match SifSolverHeader "Variable\s*="
syn match SifSolverHeader "Variable\s*Dofs\s*="
syn match SifSolverHeader "Equation\s*="
syn match SifSolverHeader "procedure\s*="
syn match SiftargetBC "Target\s*Boundaries\s*="
syntax match MatcFunction "\$\sfunction"
syn keyword SifUnit Simulation Header End
syn match SifUnit "Body\s*\d\+"
syn match SifUnit "Material\s*\d\+"
syn match SifUnit "Solver\s*\d\+"
syn match SifUnit "Equation\s*\d\+"
syn match SifUnit "Initial\s*Condition\s*\d\+"
syn match SifUnit "Body\s*Force\s*\d\+"
syn match SifUnit "Boundary\s*Condition\s*\d\+"
hi def SifBold cterm=bold
if version >= 508 || !exists("did_sif_syntax_inits")
if version < 508
let did_sif_syntax_inits = 1
command -nargs=+ HiLink hi link <args>
else
command -nargs=+ HiLink hi def link <args>
endif
HiLink SifNumber Number
HiLink SifFloat Float
HiLink SifString String
HiLink SifComment Comment
HiLink SifUnit Special
HiLink SifSolvers Keyword
HiLink SifSolverHeader SifBold
HiLink MatcFunction SifBold
HiLink SiftargetBC SifBold
HiLink SifType Type
delcommand HiLink
endif
let b:current_syntax = "sif"
It has some nice features:
- syntax highlighting
- code folding
- Custom Elmer installation Keyword fetching (ELMER_HOME environment variable needs to be set)