ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Nikolai Weibull <mailing-lists.context-users@rawuncut.elitemail.org>
Subject: Re: An idea
Date: Sun, 4 Dec 2005 18:57:48 +0100	[thread overview]
Message-ID: <20051204175748.GB8819@puritan.petwork> (raw)
In-Reply-To: <439220C0.5000007@creutzig.de>

[-- Attachment #1: Type: text/plain, Size: 1271 bytes --]

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);}

[-- Attachment #2: 2context.vim --]
[-- Type: text/plain, Size: 4646 bytes --]

" Vim syntax support file
" Maintainer:       Nikolai Weibull <nikolai@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

[-- Attachment #3: b.c --]
[-- Type: text/x-csrc, Size: 137 bytes --]

/*
 * contents: unknown
 *
 * Copyright © 2005 Nikolai Weibull <nikolai@bitwi.se>
 */


int
main(void)
{
        return 0;
}

[-- Attachment #4: b.c.tex --]
[-- Type: text/x-tex, Size: 1181 bytes --]

\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 <nikolai@bitwi.se>}
 \highlight[Comment]{*/}


\highlight[Type]{int}
\highlight[Identifier]{main}(\highlight[Type]{void})
\leftargument
        \highlight[Statement]{return} \highlight[Constant]{0};
\rightargument
\stoplines
\stoptext

[-- Attachment #5: Type: text/plain, Size: 139 bytes --]

_______________________________________________
ntg-context mailing list
ntg-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/ntg-context

  reply	other threads:[~2005-12-04 17:57 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-12-01 16:29 Zeljko Vrba
2005-12-02 14:14 ` Hans Hagen
2005-12-02 17:43   ` Zeljko Vrba
2005-12-02 16:56 ` Nikolai Weibull
2005-12-02 17:04   ` Taco Hoekwater
2005-12-02 19:15     ` Re[2]: " Giuseppe Bilotta
2005-12-02 17:46   ` Zeljko Vrba
2005-12-02 18:43     ` Nikolai Weibull
2005-12-03 19:45       ` Nikolai Weibull
2005-12-03 22:48         ` Christopher Creutzig
2005-12-04 17:57           ` Nikolai Weibull [this message]
2005-12-04 19:00           ` Hans Hagen
2005-12-11 21:17             ` Christopher Creutzig
2005-12-04 18:59         ` Hans Hagen
2005-12-04 19:14           ` Nikolai Weibull
2005-12-04 20:38             ` Hans Hagen
2005-12-05 20:48               ` Nikolai Weibull
2005-12-06  9:11                 ` Hans Hagen
2005-12-05 19:27   ` Mojca Miklavec

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20051204175748.GB8819@puritan.petwork \
    --to=mailing-lists.context-users@rawuncut.elitemail.org \
    --cc=ntg-context@ntg.nl \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).