Christopher Creutzig wrote:

> Nikolai Weibull wrote:
> 
> > this than I have is welcome to finish it.  The \highlight command
> > should be defined something like this (pseudo-tex-code):
> > 
> > \pdef\highlight[#1]{#2}%
> >   {\bgroup
> >    \setupcolorforgroup[#1]%
> >    \type{#2}%
> >    \egroup}
> > 
> > #1 is a group name, such as Statement, Operator, or Comment.  #2 may
> > contain multiple lines, and I don’t know how well this will work on
> > the TeX side.  It may also contain special characters like {, #, &,
> > and so on.  Suggestions?
> 
>  The most simple one I expect to work would be
> 
> \def\highlight[#1]{%
>   \bgroup
>   \setuphighlightcolors[{#1}]% never forget those {},
> %                            % since [] aren't balanced!
>   \processinlineverbatim\egroup}% \egroup is an argument to
> %                               % \processinlineverbatim here
> 

OK, check the attachments for what I’ve got so far.  It works well it
seems.  Still need to do more escaping, though.

        nikolai

-- 
Nikolai Weibull: now available free of charge at http://bitwi.se/!
Born in Chicago, IL USA; currently residing in Gothenburg, Sweden.
main(){printf(&linux["\021%six\012\0"],(linux)["have"]+"fun"-97);}
" Vim syntax support file
" Maintainer:       Nikolai Weibull <[EMAIL PROTECTED]>
" Latest Revision:  2005-12-04

function! s:Format(text, group)
  let formatted = strtrans(a:text)

  let formatted = substitute(formatted, '{', '\\leftargument', 'g')
  let formatted = substitute(formatted, '}', '\\rightargument', 'g')
  " TODO: Replace the reserved ConTeXt characters.
  " TODO: make sure to escape everything.

  if a:group != ""
    let formatted = "\\highlight[" . a:group . ']{' . formatted . '}'
  endif

  return formatted
endfunction

function! s:Append(text)
  exe s:new_win . 'wincmd w'
  call append(line('$'), a:text)
  exe s:org_win . 'wincmd w'
endfunction

function! s:AppendFillers(filler, diff_fillchar)
  let n = filler
  while n > 0
    let new = repeat(diff_fillchar, 3)

    if n > 2 && n < filler && !exists('context_whole_filler')
      let new .= ' ' . filler . ' inserted lines '
      let n = 2
    endif

    let new .= repeat(diff_fillchar, &columns - strlen(new))
    call s:Append(s:Format(new, 'DiffDelete'))
    let n -= 1
  endwhile
endfunction

function! s:GetFillChar(char, default)
  let fillchar = &fillchars[matchend(&fillchars, a:char . ':')]
  return (fillchar != '' ? fillchar : default)
endfunction

function! s:MainLoop()
  " Set up stuff for handling folding.
  if has('folding') && !exists('context_ignore_folding')
    let fold_fillchar = s:GetFillChar('fold', '-')
  endif

  " Set up stuff for handling diffs.
  let diff_fillchar = s:GetFillChar('diff', '-')

  let lnum = 1
  let last = line('$')

  while lnum <= last
    " If there are filler lines for diff mode, show these above the line.
    let filler = diff_filler(lnum)
    if filler > 0
      call s:AppendFillers(filler, diff_fillchar)
    endif

    let new = ""
    if has('folding') && !exists('context_ignore_folding') && foldclosed(lnum) 
> -1
      let new = s:Format(new . foldtextresult(lnum), 'Folded')
      let lnum = foldclosedend(lnum)
    else
      let line = getline(lnum)
      let len = strlen(line)
      let diff_attr = diff_hlID(lnum, 1)

      let col = 1
      while col <= len || (col == 1 && diff_attr)
        let start_col = col " The start column for processing text.
        if diff_attr
          let id = diff_hlID(lnum, col)
          let col += 1
          while col <= len && id == diff_hlID(lnum, col) | let col += 1 | 
endwhile
          if len < &columns
            " Add spaces at the end to mark the changed line.
            let line = line . repeat(' ', &columns - len)
            let len = &columns
          endif
        else
          " TODO: why not use synIDtrans here instead?  That’t give longest
          " possible matches.
          let id = synID(lnum, col, 1)
          let col += 1
          while col <= len && id == synID(lnum, col, 1) | let col += 1 | 
endwhile
        endif

        " Expand tabs.
        " TODO: make a function? let new .= s:ExpandAndFormat(strpart(...), id)
        let expanded = strpart(line, start_col - 1, col - start_col)
        let idx = stridx(expanded, "\t")
        while idx >= 0
          let i = &ts - (idx + start_col - 1) % &ts
          let expanded = substitute(expanded, '\t', repeat(' ', i), '')
          let idx = stridx(expanded, "\t")
        endwhile

        if expanded =~ '^\s*$'
          let new .= expanded
        else
          let new .= s:Format(expanded, synIDattr(synIDtrans(id), 'name'))
        endif
      endwhile
    endif

    call s:Append(new)
    let lnum = lnum + 1
  endwhile
endfunction

" Set up options.
let s:old_title = &title
let s:old_icon = &icon
let s:old_search = @/
set notitle noicon

" Split window to create a buffer with the ConTeXt file.
let s:org_bufnr = winbufnr(0)
if expand("%") == ""
  new untitled.tex
else
  new %.tex
endif
let s:new_win = winnr()
let s:org_win = bufwinnr(s:org_bufnr)

" Set up the new buffer.
set modifiable
%delete
call setline(1, '\startlines')

" Switch to the original window.
exe s:org_win . 'wincmd w'

call s:MainLoop()

" Cleanup.
exe s:new_win . 'wincmd w'
call append(line('$'), '\stoplines')
silent %s:\s\+$::e

" Restore old settings.
let &title = s:old_title
let &icon = s:old_icon
let @/ = s:old_search

" Save a little bit of memory.  (Is this worth doing?)
unlet s:old_title s:old_icon s:old_search
unlet s:org_bufnr s:new_win s:org_win
if !v:profiling
  delfunction s:Format
  delfunction s:Append
  delfunction s:AppendFillers
  delfunction s:GetFillChar
  delfunction s:MainLoop
endif
/*
 * contents: unknown
 *
 * Copyright © 2005 Nikolai Weibull <[EMAIL PROTECTED]>
 */


int
main(void)
{
        return 0;
}
\enableregime[utf]

\setupcolors
  [state=start]

\definecolor  [Comment]     [r=0.14509803921568629, g=0.45098039215686275, b=0.14509803921568629]
\definecolor  [Type]        [r=0.37647058823529411, g=0.18431372549019609, b=0.50196078431372548]
\definecolor  [Identifier]  [r=0.18431372549019609, g=0.35294117647058826, b=0.60784313725490191]
\definecolor  [Statement]   [r=0.46274509803921571, g=0.37647058823529411, b=0.12549019607843137]
\definecolor  [Constant]    [r=0.58431372549019611, g=0.086274509803921567, b=0.086274509803921567]

\def\highlight[#1]%
  {\bgroup\startcolor[#1]\processinlineverbatim{\stopcolor\egroup}}

\setuplines[space=yes]

\starttext
\startlines
\setverbatimspaceskip
\frenchspacing
\parindent\zeropoint
\verbatimfont
\highlight[Comment]{/*}
\highlight[Comment]{ * contents: A minimal C program.}
\highlight[Comment]{ *}
\highlight[Comment]{ * Copyright © 2005 Nikolai Weibull <[EMAIL PROTECTED]>}
 \highlight[Comment]{*/}


\highlight[Type]{int}
\highlight[Identifier]{main}(\highlight[Type]{void})
\leftargument
        \highlight[Statement]{return} \highlight[Constant]{0};
\rightargument
\stoplines
\stoptext
_______________________________________________
ntg-context mailing list
[email protected]
http://www.ntg.nl/mailman/listinfo/ntg-context

Reply via email to