Skip to content
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

Closed
astery opened this issue May 14, 2013 · 18 comments
Closed

Improve inline syntax highlight #147

astery opened this issue May 14, 2013 · 18 comments
Milestone

Comments

@astery
Copy link

astery commented May 14, 2013

Is it possible to insert code snippets with syntax highlight?

@jceb
Copy link
Owner

jceb commented Jun 1, 2013

Would you specify your request a bit more. Maybe an example would be good.

@astery
Copy link
Author

astery commented Jun 3, 2013

I want to be able get syntax highlight like here in Org
I found this article but it doesn't work well with already highlighted files and solutions mentioned here I don't know how to apply to vimorg file.

@astery
Copy link
Author

astery commented Jun 3, 2013

babel

@sotte
Copy link
Contributor

sotte commented Jun 18, 2013

I'd like to see this implemented as well :)

What exactly were your problems?

@sotte
Copy link
Contributor

sotte commented Jul 7, 2013

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.

@sotte
Copy link
Contributor

sotte commented Jul 7, 2013

However, calling it manually after an org file was loaded seems to work though.
org-multi-syntax

Sometimes, calling the function multiple times breaks the syntax hl. I'm not sure how to reproduce it.

@sotte
Copy link
Contributor

sotte commented Jul 7, 2013

The c syntax hl seems to break it.

@sotte
Copy link
Contributor

sotte commented Jul 7, 2013

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:

  • syntax hl for certain file types does not work
  • the syntax hl must be enabled manually and explicitly for each file type. a generic version would be better.

@astery
Copy link
Author

astery commented Jul 8, 2013

Thank @sotte for response. Calling manually really does the trick. I'm using 5c2aeb4, append to end of syntax/org.vim and it works too. I've seen through python, ruby, lua, lisp, c, cpp and I don't have troubles with C and Cpp, they highlighted correctly. Thank you @sotte.

@sotte
Copy link
Contributor

sotte commented Jul 8, 2013

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 :)

@sotte
Copy link
Contributor

sotte commented Jul 8, 2013

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.

@sotte sotte added this to the v0.5 milestone Apr 1, 2014
@sotte sotte changed the title Syntax highlight Improve inline syntax highlight Apr 20, 2014
@sotte
Copy link
Contributor

sotte commented Apr 20, 2014

I just added the inline syntax hl feature with #163.

Feedback is welcome!

@sotte sotte closed this as completed Apr 20, 2014
@riazrizvi
Copy link

riazrizvi commented May 24, 2018

Does this still work? I have syntax highlighting working in the rest of vim, but within .org files when I write BEGIN_SRC/END_SRC code blocks, all the code is in red:

screen shot 2018-05-24 at 10 57 24 am

This is what the same code looks like in a .js file in vim:

screen shot 2018-05-24 at 11 00 08 am

@jceb
Copy link
Owner

jceb commented Jun 10, 2018

@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.

@execb5
Copy link

execb5 commented Apr 23, 2019

Did anyone manage to this in a better way than having to manually change the syntax/org.vim file to add languages?

@jceb
Copy link
Owner

jceb commented Apr 24, 2019

Well, we could make it configurable, i.e. let the user define a list of supported languages .. or even better, extract the language dynamically.

@execb5
Copy link

execb5 commented May 3, 2019

Dynamically would be the best, but if it could start with something in vimrc would be great already.

@XVilka
Copy link
Contributor

XVilka commented Jul 23, 2019

You can use Markdown syntax highlight as an example of the possible settings:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants