Page 1 of 1

emacs

Posted: 15 Jul 2011, 20:30
by Carlos Martin
Elmeristas,

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.

Code: Select all

(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!

Re: emacs

Posted: 17 Jul 2011, 22:31
by raback
Hi Carlos

Nice work! I'll certainly give it a try. The idea has been there before but I don't know that anybody would have actually implemented it before.

-Peter

Re: emacs

Posted: 21 Jul 2011, 15:35
by fgillet
Thanks for the tip Carlos.
As I'm a vi fan I did a quick syntax file for vi.

to put under ~/.vim/syntax/sif.vim

Code: Select all


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"

and add this under ~/.vim/filetype.vim

Code: Select all

augroup filetypedetect
au BufNewFile,BufRead *.sif setf sif
augroup END
sure it can be improved but it already gives nice colors.

Best
Fabien

Re: emacs

Posted: 02 Sep 2011, 17:32
by kglub
And thanks for your vim code Fabien!

Miguel

Re: emacs

Posted: 06 Oct 2011, 20:16
by hazelsct
Another thanks Carlos, I like it!

Re: emacs

Posted: 02 Nov 2011, 10:48
by Sleeper
That's really amazing code! How long did it take you to develop it?

Vim: ElmerFEM Solver Input File Syntax highlighting

Posted: 12 Nov 2015, 15:35
by Takala
Hello Elmer users,

I have set up a repository for vim-elmer plugin.

It has some nice features:
- syntax highlighting
- code folding
- Custom Elmer installation Keyword fetching (ELMER_HOME environment variable needs to be set)

See the attached image