-
Notifications
You must be signed in to change notification settings - Fork 268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve inline syntax highlight #147
Comments
Would you specify your request a bit more. Maybe an example would be good. |
I'd like to see this implemented as well :) What exactly were your problems? |
I added the function proposed in http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file to my .vimrc. function! TextEnableCodeSnip(filetype,start,end,textSnipHl) abort
let ft=toupper(a:filetype)
let group='textGroup'.ft
if exists('b:current_syntax')
let s:current_syntax=b:current_syntax
" Remove current syntax definition, as some syntax files (e.g. cpp.vim)
" do nothing if b:current_syntax is defined.
unlet b:current_syntax
endif
execute 'syntax include @'.group.' syntax/'.a:filetype.'.vim'
try
execute 'syntax include @'.group.' after/syntax/'.a:filetype.'.vim'
catch
endtry
if exists('s:current_syntax')
let b:current_syntax=s:current_syntax
else
unlet b:current_syntax
endif
execute 'syntax region textSnip'.ft.'
\ matchgroup='.a:textSnipHl.'
\ start="'.a:start.'" end="'.a:end.'"
\ contains=@'.group
endfunction Then calling call TextEnableCodeSnip( 'python', '#+BEGIN_SRC python', '#+END_SRC', 'SpecialComment') kinda works. The inline code is highlighted, but end marker ("#+END_SRC") is not unique and the new syntax eats all the rest of the file. Extending the end marker to "#+END_SRC python" works, but that is not the orgmode way. I guess all I want is a non greedy end marker. |
The c syntax hl seems to break it. |
I have a semi working version. It needs more testing! Add this to after line 289 in syntax/org.vim " Enable syntag highligthing within a region of a file
" http://vim.wikia.com/wiki/Different_syntax_highlighting_within_regions_of_a_file
function! EnableInlineSyntaxHighlight(filetype, start, end, textSnipHl) abort
" echo "executing text enable code snip"
let ft=toupper(a:filetype)
let group='textGroup'.ft
if exists('b:current_syntax')
let s:current_syntax=b:current_syntax
" Remove current syntax definition, as some syntax files (e.g. cpp.vim)
" do nothing if b:current_syntax is defined.
unlet b:current_syntax
" echo 'unletting'
endif
execute 'syntax include @'.group.' syntax/'.a:filetype.'.vim'
try
execute 'syntax include @'.group.' after/syntax/'.a:filetype.'.vim'
catch
endtry
if exists('s:current_syntax')
let b:current_syntax=s:current_syntax
" echo 'let current syntax'
else
unlet b:current_syntax
endif
execute 'syntax region textSnip'.ft.'
\ matchgroup='.a:textSnipHl.'
\ start="'.a:start.'" end="'.a:end.'"
\ contains=@'.group
endfunction
" we have to enable the inline syntax hl by hand :(
call EnableInlineSyntaxHighlight('python', '#+BEGIN_SRC python', '#+END_SRC', 'PreProc')
call EnableInlineSyntaxHighlight('lisp', '#+BEGIN_SRC lisp', '#+END_SRC', 'Comment')
call EnableInlineSyntaxHighlight('lua', '#+BEGIN_SRC lua', '#+END_SRC', 'Comment')
" c and cpp break the syntax hl
" TODO fix inline syntax hl for c and c++
" call EnableInlineSyntaxHighlight('c', '#+BEGIN_SRC c', '#+END_SRC', 'Comment')
" call EnableInlineSyntaxHighlight('cpp', '#+BEGIN_SRC cpp', '#+END_SRC', 'Comment') The limitations:
|
Hm, I have an automatic version, but that does not work with c and c++. I'll try to figure it out. And at least I'll add a helper function to enable the syntax hl easier. If you want to help or have any ideas you're more than welcome :) |
I'm wondering where a good place for this function is. But I don't know if you put a feature like this into a syntax file. |
I just added the inline syntax hl feature with #163. Feedback is welcome! |
@riazrizvi I haven't tried the feature yet. However, it seems like you need to install the https://github.com/vim-scripts/SyntaxRange plugin. I found the clue in this commit 9e8f5e5. Hope it helps. |
Did anyone manage to this in a better way than having to manually change the syntax/org.vim file to add languages? |
Well, we could make it configurable, i.e. let the user define a list of supported languages .. or even better, extract the language dynamically. |
Dynamically would be the best, but if it could start with something in vimrc would be great already. |
You can use Markdown syntax highlight as an example of the possible settings: |
Is it possible to insert code snippets with syntax highlight?
The text was updated successfully, but these errors were encountered: