diff --git a/README.md b/README.md index e45a761..18ee024 100644 --- a/README.md +++ b/README.md @@ -5,3 +5,66 @@ Octave syntax and indentation support for Vim: * Syntax highlighting is taken from [Rik's script](https://www.vim.org/scripts/script.php?script_id=3600) * Identation is ported accordingly using upstream Lua's as a base. + +## Features + +From the syntax file description: + +* Highlights entire Octave grammar (`endwhile`, `endfor`, etc.), + not just Matlab keywords +* Updated to highlight all core Octave functions as of version 4.2.0 +* Highlights user functions and anonymous functions [`@(...)`] + from within the `.m` file being edited +* Use-dependent highlighting of Octave system variables + - When querying system variables, keyword is highlighted as a constant. + For example, `var = true`, highlights `true` as a constant. + - When setting variables or otherwise invoking keyword as a function, + keyword is highlighted as a function. For example, `var = true (2,4)`, + highlights `true` as a function. +* Support for multi-line strings with line continuation characters + as well as escaped quotes (`\"` or `\'`) within string. +* Support for multi-line block comments +* Support for highlighting numbers that use hex (0x) or binary (0b) syntax +* Error highlighting for bad number syntax, bad structure variable names, + bad block comments, bad line continuations. +* Optional support for highlighting operators (`+`, `-`, `*`, etc.), + user variables, or tabs, which can be achieved by uncommenting + appropriate tagged lines in the syntax file. + +The indentation file provides basic automatic indentation of blocks. + +## Installations + +1. Install [Pathogen](https://github.com/tpope/vim-pathogen), + [Vundle](https://github.com/VundleVim/Vundle.vim), + [NeoBundle](https://github.com/Shougo/neobundle.vim), + or [Plug](https://github.com/junegunn/vim-plug) package manager for Vim. +2. Use this repository as submodule or package. + +For example, when using [Plug](https://github.com/junegunn/vim-plug): + +```vim +Plug 'https://github.com/McSinyx/vim-octave.git', {'for': 'octave'} +``` + +You can also use Vim 8 built-in package manager: + +```sh +mkdir -p ~/.vim/pack/default/start +git clone https://github.com/McSinyx/vim-octave.git ~/.vim/pack/default/start/vim-polyglot +``` + +## Usage + +Add the following lines to your `vimrc` + +```vim +" Octave syntax +augroup filetypedetect + autocmd! + autocmd BufRead,BufNewFile *.m,*.oct setlocal filetype=octave +augroup END +``` + +Omni completion should works out-of-box by setting +`omnifunc=syntaxcomplete#Complete`. diff --git a/indent/octave.vim b/indent/octave.vim index 6f57ae3..b68986c 100644 --- a/indent/octave.vim +++ b/indent/octave.vim @@ -1,9 +1,9 @@ " Vim indent file -" Language: Octave -" Maintainer: Nguyễn Gia Phong -" Original Maintainers: Marcus Aurelius Farias -" First Author: Max Ischenko -" Last Change: 2019-10-11 +" Language: Octave +" Maintainer: Nguyễn Gia Phong +" Original Maintainer: Marcus Aurelius Farias +" First Author: Max Ischenko +" Last Change: 2019-10-11 " Only load this indent file when no other was loaded. if exists("b:did_indent") @@ -11,31 +11,28 @@ if exists("b:did_indent") endif let b:did_indent = 1 -setlocal indenteypr=GetOctaveIndent() +let s:beginBlock = ['for', 'parfor', 'function', 'if', 'switch', + \'try', 'unwind_protect', 'while', 'do', 'classdef', + \'enumeration', 'events', 'methods', 'properties'] +let s:midBlock = ['case', 'catch', 'else', 'elseif', 'otherwise', + \'unwind_protect_cleanup'] +let s:endBlock = ['end', 'endfor', 'endparfor', 'endfunction', 'endif', + \'end_try_catch', 'end_unwind_protect', 'endwhile', + \'endclassdef', 'endenumeration', 'endevents', + \'endproperties', 'endswitch', 'until', 'endmethods'] +let s:openBlock = s:beginBlock + s:midBlock +let s:closeBlock = s:midBlock + s:endBlock -" To make Vim call GetOctaveIndent() when it finds '\s*end' or '\s*until' +" To make Vim call GetOctaveIndent() when it finds a block closer " on the current line ('else' is default and includes 'elseif'). -setlocal indentkeys+=0=end,0=until,0=case,0=catch,0=otherwise,0=unwind_protect_cleanup - -setlocal autoindent +setlocal indentkeys+=0=end,0=until,0=case,0=catch,0=otherwise +setlocal indentkeys+=0=unwind_protect_cleanup " Only define the function once. if exists("*GetOctaveIndent") finish endif -let s:beginBlock = ['for', 'parfor', 'function', 'if', 'switch'] -extend(s:beginBlock, ['try', 'unwind_protect', 'while', 'do', 'classdef']) -extend(s:beginBlock, ['enumeration', 'events', 'methods', 'properties']) -let s:midBlock = ['case', 'catch', 'else', 'elseif', 'otherwise'] -add(s:midBlock, 'unwind_protect_cleanup') -let s:endBlock = ['end', 'endfor', 'endparfor', 'endfunction', 'endif'] -extend(s:endBlock, ['end_try_catch', 'end_unwind_protect', 'endwhile']) -extend(s:endBlock, ['endclassdef', 'endenumeration', 'endevents']) -extend(s:endBlock, ['endproperties', 'endswitch', 'until', 'endmethods']) -let s:openBlock = s:beginBlock + s:midBlock -let s:closeBlock = s:midBlock + s:endBlock - function! GetOctaveIndent() " Find a non-blank line above the current line. let prevlnum = prevnonblank(v:lnum - 1) @@ -49,18 +46,18 @@ function! GetOctaveIndent() let prevl = getline(prevlnum) let l = getline(v:lnum) - " Add a 'shiftwidth' after lines that start a block: + " Add a 'shiftwidth' after lines starting a block: let openCol = match(prevl, '^\s*\%(' . join(s:openBlock, '\>\|') . '\>\)') + 1 let hasNoEnd = prevl !~ ('\<' . join(s:endBlock, '\>\|\<') . '\>') - if hasOpen && hasNoEnd + if openCol && hasNoEnd let openSynID = synID(prevlnum, openCol, 1) if synIDattr(openSynID, "name") != "octaveComment" let ind = ind + shiftwidth() endif endif - " Subtract a 'shiftwidth' on s:closeBlock - " This is the part that requires 'indentkeys'. + " Subtract a 'shiftwidth' on closure of blocks, + " i.e. the part that required 'indentkeys'. let closeCol = match(l, '^\s*\%(' . join(s:closeBlock, '\>\|') . '\>\)') + 1 if closeCol let closeSynID = synID(v:lnum, closeCol, 1) @@ -71,3 +68,6 @@ function! GetOctaveIndent() return ind endfunction + +setlocal indentexpr=GetOctaveIndent() +setlocal autoindent diff --git a/syntax/octave.vim b/syntax/octave.vim index d92e87a..e353c7c 100644 --- a/syntax/octave.vim +++ b/syntax/octave.vim @@ -1,5 +1,3 @@ -if !exists('g:polyglot_disabled') || index(g:polyglot_disabled, 'octave') == -1 - " Vim syntax file " Language: Octave " Maintainer: Rik