[NTG-context] An idea

Nikolai Weibull mailing-lists.context-users at rawuncut.elitemail.org
Sun Dec 4 18:57:48 CET 2005


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);}
-------------- next part --------------
" Vim syntax support file
" Maintainer:       Nikolai Weibull <nikolai at bitwi.se>
" 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: b.c
Type: text/x-csrc
Size: 125 bytes
Desc: not available
Url : http://www.ntg.nl/pipermail/ntg-context/attachments/20051204/010a2fcd/b.bin
-------------- next part --------------
A non-text attachment was scrubbed...
Name: b.c.tex
Type: text/x-tex
Size: 1145 bytes
Desc: not available
Url : http://www.ntg.nl/pipermail/ntg-context/attachments/20051204/010a2fcd/b.c.bin


More information about the ntg-context mailing list