From: Aditya Mahajan <adityam@umich.edu>
Subject: Re: Making a verbatim file for another language
Date: Thu, 28 Dec 2006 12:09:57 -0500 (EST) [thread overview]
Message-ID: <alpine.WNT.0.81.0612281148220.2372@nqvgln> (raw)
In-Reply-To: <491FFCED-C4FF-4827-9F7C-C5AFF7F27D86@mac.com>
[-- Attachment #1: Type: TEXT/PLAIN, Size: 2149 bytes --]
On Thu, 28 Dec 2006, cormullion@mac.com wrote:
> On 2006-12-27, at 21:41.0, Aditya Mahajan wrote:
>
> > If you have vim and are interested in testing, you can be the first
> > tester of the module :)
>
> Hi! Yes I can find a vim module, so if you can tell me which files to
> put where, I'll give it a try!
I have attached the files. Please note that we are still playing
around with the module, so certain features will change.
Right now, place t-vimsyntax.tex and 2context.vim files in the working
directory. You need to have write18 enabled.
Add \usemodule[vimsyntax] on the top of your file. (And ofcourse,
\setupcolors[state=start])
1. If you want to typeset an existing file, and lets say the filetype
(in vim's terms) is ruby. Then
\definetypeVIMfile [name of macro] %Usually typeRUBY, typeMATLAB, etc.
[ % Optional arguments
start=<linenumber>, %starting line number, defaults to 1
stop=linenumber, %stopping line number, defaults to 0,
%and 2context makes it end of file
tab=<tabstop>, %How many spaces should a tab mean,
%defaults to 8.
%Support for visible tab will be added.
space=(yes|on|no) %no ignores spaces, yes honors file spaces
%on makes spaces visible.
vimcolorscheme=<colorscheme>, %Which vim colorscheme to use
%defaults to pscolor, which is the only
%scheme implemented so far.
syntax=<syntax>, %Which syntax highlighing to use in vim.
] % End of optional arguments
In the simplest case, you will have
\definetypeVIMfile [typeRUBY] [syntax=ruby] % tab and spaces to your preference
Then you can use
\typeRUBY [optional arguments, as above] {filename}
%filename needs extension also
2. If you need to type code snippets
\defineVIMtyping [RUBY] [syntax=ruby] %Other options same as above
and then
\startRUBY
# This is a ruby program
puts "Hello World"
\stopRUBY
Let us know if you come across any problems.
Aditya
[-- Attachment #2: Type: APPLICATION/octet-stream, Size: 9418 bytes --]
%D \module
%D [ file=t-vimsyntax
%D version=2006.12.26,
%D title=ViM to \CONTEXT,
%D subtitle=Use ViM to generate code listing,
%D author=Mojca Miklavec \& Aditya Mahajan,
%D email=<mojca.miklavec.lists@gmail.com>
%D date=\currentdate,
%D copyright=Public Domain]
%D \CONTEXT\ has an excellent pretty printing capabilities for many languages.
%D The code is written in \TEX, and due to catcode jugglary verbatim
%D typesetting is perhaps the trickiest part of \TEX. This makes it difficult
%D for a \quotation{normal} user to define syntax highlighting rules for a new
%D language. This module, takes the onus of defining syntax highlighting rules
%D away from the user and uses ViM editor to generate the syntax highlighting.
%D There is a helper \filename{2context.vim} script to do the syntax parsing
%D in ViM. This is a stop|-|gap method, and hopefully with \LUATEX, things
%D will be much easier.
\writestatus {loading} {Context Module for ViM Sytax Highlighting}
\startmodule[vimsyntax]
\unprotect
\definesystemvariable {vs} % Vim Syntax
%D \macros{startvimcolorscheme}
%D To start a new vim colorscheme.
\def\startvimcolorscheme[#1]%
{\pushmacro\vimcolorscheme
\edef\vimcolorscheme{#1}}
\def\stopvimcolorscheme
{\popmacro\vimcolorscheme}
%D \macros{definevimsyntax, definevimsyntaxsynonyms}
%D These macros should always occur inside a \type{\startvimcolorschme}
%D \unknown \type{\stopvimcolorscheme} pair. The \type{\definevimsyntax}
%D macro defines syntax highlighting rules for ViM's syntax highlighting
%D regions. It takes three arguments \type{style}, \type{color} and
%D \type{command}. The most common ViM syntax highlighting regions are defined
%D in the end of this file. The \type{\definevimsyntaxsynonyms} macro just
%D copies the settings from another syntax highlighting region.
\def\definevimsyntax
{\dodoubleargumentwithset\dodefinevimsyntax}
\def\dodefinevimsyntax[#1]% [#2]
{\getparameters[\??vs\vimcolorscheme#1]} %[#2]
\def\definevimsyntaxsynonyms
{\dodoubleargumentwithset\dodefinevimsyntaxsynonyms}
\def\dodefinevimsyntaxsynonyms[#1][#2]%
{\copyparameters[\??vs\vimcolorscheme#1][\??vs\vimcolorscheme#2]
[\c!style,\c!color,\c!command]}
%D \macro{vimsyntax}
%D This macro is inserted by \filename{2context.vim} script, and takes care of
%D properly highlighting different syntax regions.
\def\vimsyntax
{\dosingleargument\dovimsyntax}
\def\dovimsyntax[#1]#2%
{\dostartattributes{\??vs\vimcolorscheme Normal}\c!style\c!color\empty%
\dostartattributes{\??vs\vimcolorscheme #1}\c!style\c!color\empty%
\getvalue{\??vs#1\c!command}{#2}%
\dostopattributes%
\dostopattributes}
%D \macros{setupVIMtyping, typeVIMfile}
%D There are three settings for \type{\setupVIMtyping}: \type{syntax}, which
%D tells ViM which syntax rules to use; \type{tab}, which sets the
%D \type{tabstop} in ViM; and \type{space} which takes care of spaces.
%D
%D \type{\typeVIMfile} macro basically calls ViM with appropriate settings and
%D sources the \filename{2context.vim} script. The result is slow, because
%D parsing by ViM is slow. Do not use this method for anything larger than a
%D few hundred lines. For large files, one option is to pre||prase them, and
%D then typeset the result. We have not provided any interface for that, but
%D it is relatively easy to implement.
\def\setupVIMtyping
{\dosingleargument\getparameters[\??vs]}
\def\typeVIMfile
{\dosingleempty\dotypeVIMfile}
\def\notypeVIMfile[#1][#2]#3%
{\dotypeVIMfile[#1,#2]{#3}}
\def\dotypeVIMfile[#1]#2%
{\doiffileelse{#2}
{\dodotypeVIMfile[#1]{#2}}
{\reporttypingerror{#2}}}
\def\dodotypeVIMfile[#1]#2%
{\bgroup
\initializeVIMtyping{#1}
\def\vimcolorscheme{\@@vscolorscheme}
\runVIMsyntax{#2}
\startlines \input #2-vimsyntax.tmp \stoplines
\@@vsafter
\egroup}
\def\initializeVIMtyping#1
{\setupVIMtyping[#1]
%Make sure that stop is not empty
\doifempty{\@@vsstop}{\setvalue{\@@vsstop}{0}}
\@@vsbefore
\setuplines[space=\@@vsspace]}
\def\runVIMsyntax#1
{\executesystemcommand
{vim -u NONE % No need to read unnessary configurations
-e % run in ex mode
% -V10log % For debugging only, will go away later.
-c "set noswapfile"
-c "set tabstop=\@@vstab"
-c "set cp"
-c "syntax on"
-c "set syntax=\@@vssyntax"
-c "let contextstartline=\@@vsstart"
-c "let contextstopline=\@@vsstop"
-c "source 2context.vim"
-c "wqa" #1}}
%D \macro{definetypeVIMfile}
%D This macro allows you to define new file typing commands. For example
%D \starttyping
%D \definetypeVIMfile[typeRUBY] [syntax=ruby]
%D \stoptyping
%D after which one can use
%D \starttyping
%D \typeRUBY[option]{rubyfile}
%D \stoptyping
\def\definetypeVIMfile
{\dodoubleargument\dodefinetypeVIMfile}
\def\dodefinetypeVIMfile[#1][#2]%
{\unexpanded\setvalue{#1}{\dodoubleempty\notypeVIMfile[#2]}}
\def\defineVIMtyping
{\dodoubleargument\dodefineVIMtyping}
\def\dodefineVIMtyping[#1][#2]%
{\setevalue{\e!start#1}{\noexpand\dostartbuffer[vimsyntax][\e!start#1][\e!stop#1]}%
\setvalue{\e!stop#1}{\dodotypeVIMfile[#2]{\TEXbufferfile{vimsyntax}}}}
%D Some defaults.
\setupVIMtyping
[ syntax=context,
\c!tab=8,
\c!space=\v!yes,
\c!start=1,
\c!stop=0, %stop should be set to some value.
\c!before=,
\c!after=,
colorscheme=pscolor,
]
%D Pre-defined Syntax : {{{ Based on \filename{ps_color.vim}
%D Perhaps this should be customizable
%D ViM uses hex mode for setting colors, I do not want to convert them to rgb
%D values.
\startvimcolorscheme[pscolor]
\setupcolor[hex]
\definecolor [vimsyntax!Special] [h=907000]
\definecolor [vimsyntax!Comment] [h=606000]
\definecolor [vimsyntax!Number] [h=907000]
\definecolor [vimsyntax!Constant] [h=007068]
\definecolor [vimsyntax!PreProc] [h=009030]
\definecolor [vimsyntax!Statement] [h=2060a8]
\definecolor [vimsyntax!Type] [h=0850a0]
\definecolor [vimsyntax!Todo] [h=e0e090]
\definecolor [vimsyntax!Error] [h=c03000]
\definecolor [vimsyntax!Identifier] [h=a030a0]
\definecolor [vimsyntax!SpecialKey] [h=1050a0]
\definecolor [vimsyntax!Underline] [h=6a5acd]
\definevimsyntax
[Normal]
[\c!style=\v!mono,\c!color=\maintextcolor]
\definevimsyntax
[Constant]
[\c!style=\v!mono,\c!color=vimsyntax!Constant]
\definevimsyntaxsynonyms
[Character,Boolean,Float]
[Constant]
\definevimsyntax
[Number]
[\c!style=\v!mono,\c!color=vimsyntax!Number]
\definevimsyntax
[Identifier]
[\c!style=\v!mono,\c!color=vimsyntax!Identifier]
\definevimsyntaxsynonyms
[Function]
[Identifier]
\definevimsyntax
[Statement]
[\c!style=\v!mono,\c!color=vimsyntax!Statement]
\definevimsyntaxsynonyms
[Conditional,Repeat,Label,Operator,Keyword,Exception]
[Statement]
\definevimsyntax
[PreProc]
[\c!style=\v!mono,\c!color=vimsyntax!PreProc]
\definevimsyntaxsynonyms
[Include,Define,Macro,PreCondit]
[PreProc]
\definevimsyntax
[Type,StorageClass, Structure, Typedef]
[\c!style=\v!mono, \c!color=vimsyntax!Type]
\definevimsyntax
[Special]
[\c!style=\v!mono,\c!color=vimsyntax!Special]
\definevimsyntax
[SpecialKey]
[\c!style=\v!mono,\c!color=vimsyntax!SpecialKey]
\definevimsyntax
[Tag,Delimiter]
[\c!style=\v!mono]
\definevimsyntax
[Comment,SpecialComment]
[\c!style=\v!mono,\c!color=vimsyntax!Comment]
\definevimsyntax
[Debug]
[\c!style=\v!mono]
\definevimsyntax
[Underlined]
[\c!style=\v!mono,\c!command=\underbar]
\definevimsyntax
[Ignore]
[\c!style=\v!mono]
\definevimsyntax
[Error]
[\c!style=\v!mono,\c!color=vimsyntax!Error]
\definevimsyntax
[Todo]
[\c!style=\v!mono,\c!color=vimsyntax!Todo]
\stopvimcolorscheme
% }}}
\protect
\stopmodule
% TODO: Enable line numbering
% TODO: Get start=continue to work.
\doifnotmode{demo}{\endinput}
\setupcolors[state=start]
\setupbodyfont[10pt]
\defineVIMtyping [MATLAB] [syntax=matlab]
\starttext
\startMATLAB
function russell_demo()
% Do the example in ch 17 (p501) of Russell and Norvig
% (1,1) is top left corner.
r = 3; c = 4; p = 0.8; action_cost = -1/25;
obstacle = zeros(r,c); obstacle(2,2)=1;
terminal = zeros(r,c); terminal(1,4)=1; terminal(2,4)=1;
absorb = 1;
wrap_around = 0;
noop = 0;
T = mk_grid_world(r, c, p, obstacle, terminal, absorb, wrap_around, noop);
% Add rewards for terminal states
nstates = r*c + 1;
if noop
nact = 5;
else
nact = 4;
end
R = action_cost*ones(nstates, nact);
R(10,:) = 1;
R(11,:) = -1;
R(nstates,:) = 0;
discount_factor = 1;
V = value_iteration(T, R, discount_factor);
%reshape(V(1:end-1),[r c])
% 0.8116 0.8678 0.9178 1.0000
% 0.7616 0.7964 0.6603 -1.0000
% 0.7053 0.6553 0.6114 0.3878
% Same as the book p501
Q = Q_from_V(V, T, R, discount_factor);
[V, p] = max(Q, [], 2);
use_val_iter = 1;
% (I-gT) is singular since g=1 and there is an absorbing state (i.e., T(i,i)=1)
% Hence we cannot use value determination.
[p,V] = policy_iteration(T, R, discount_factor, use_val_iter);
%reshape(V(1:end-1),[r c])
% 0.8115 0.8678 0.9178 1.0000
% 0.7615 0.7964 0.6603 -1.0000
% 0.7048 0.6539 0.6085 0.3824
\stopMATLAB
\page
\typeVIMfile{\jobname.tex}
\stoptext
[-- Attachment #3: Type: APPLICATION/octet-stream, Size: 4717 bytes --]
"D \module
"D [ file=2context.vim,
"D version=2006.12.26,
"D title=ViM to \CONTEXT,
"D subtitle=Use ViM to generate code listing,
"D author=Mojca Miklavec \& Aditya Mahajan,
"D email=<mojca.miklavec.lists@gmail.com>
"D date=\currentdate,
"D copyright=Public Domain]
"D This file is based on \filename{2html.vim}. It uses ViM systax highlighting
"D to generate a \CONTEXT\ file which is parsed using \filename{t-vimsyntax}
"D module.
"D We use \filename{.tmp} extension so that the file is purged by
"D \filename{ctxtools}
if expand("%") == ""
new vimsyntax.tmp
else
new %-vimsyntax.tmp
endif
"D We are right now in the new buffer.
set modifiable
%d "This empties the buffer.
wincmd p
let s:expandedtab = ' '
while strlen(s:expandedtab) < &ts
let s:expandedtab = s:expandedtab . ' '
endwhile
"D Loop over all lines in the original text.
"D Use contextstartline and contextstopline if they are set.
if exists("contextstartline")
let s:lnum = contextstartline
if !(s:lnum >= 1 && s:lnum <= line("$"))
let s:lnum = 1
endif
else
let s:lnum = 1
endif
if exists("contextstopline")
let s:end = contextstopline
if !(s:end >= s:lnum && s:end <= line("$"))
let s:end = line("$")
endif
else
let s:end = line("$")
endif
let s:buffer_lnum = 1
while s:lnum <= s:end
"D Get the current line
let s:line = getline(s:lnum)
let s:len = strlen(s:line)
let s:new = ""
"D Loop over each character in the line
let s:col = 1
while s:col <= s:len
let s:startcol = s:col " The start column for processing text
let s:id = synID (s:lnum, s:col, 1)
let s:col = s:col + 1
"D Speed loop (it's small - that's the trick)
"D Go along till we find a change in synID
while s:col <= s:len && s:id == synID(s:lnum, s:col, 1)
let s:col = s:col + 1
endwhile
"D Output the text with the same synID, with class set to {s:id_name}
let s:id = synIDtrans (s:id)
let s:id_name = synIDattr (s:id, "name", "gui")
let s:temp = strpart(s:line, s:startcol - 1, s:col - s:startcol)
"D We need to get rid of the characters that can cause trouble in \CONTEXT.
"D The funny \type{||||||||||} and \type{$$$$$$$$$} characters should never
"D appear in {\em normal} \TEX\ file. As a side||effect, this script can not
"D pretty print itself.
let s:temp = substitute( s:temp, '\\', '||||||||||\\letterbackslash$$$$$$$$$$', 'g')
let s:temp = substitute( s:temp, '{', '||||||||||\\letteropenbrace$$$$$$$$$$', 'g')
let s:temp = substitute( s:temp, '}', '||||||||||\\letterclosebrace$$$$$$$$$$', 'g')
let s:temp = substitute( s:temp, '||||||||||' , '{' , 'g')
let s:temp = substitute( s:temp, '\$\$\$\$\$\$\$\$\$\$' , '}' , 'g')
let s:temp = substitute( s:temp, '&', '{\\letterampersand}', 'g')
let s:temp = substitute( s:temp, '<', '{\\letterless}', 'g')
let s:temp = substitute( s:temp, '>', '{\\lettermore}', 'g')
let s:temp = substitute( s:temp, '#', '{\\letterhash}', 'g')
let s:temp = substitute( s:temp, '"', '{\\letterdoublequote}', 'g')
let s:temp = substitute( s:temp, "'", '{\\lettersinglequote}', 'g')
let s:temp = substitute( s:temp, '\$', '{\\letterdollar}', 'g')
let s:temp = substitute( s:temp, '%', '{\\letterpercent}', 'g')
let s:temp = substitute( s:temp, '\^', '{\\letterhat}', 'g')
let s:temp = substitute( s:temp, '_', '{\\letterunderscore}', 'g')
let s:temp = substitute( s:temp, '|', '{\\letterbar}', 'g')
let s:temp = substitute( s:temp, '\~', '{\\lettertilde}', 'g')
let s:temp = substitute( s:temp, '/', '{\\letterslash}', 'g')
let s:temp = substitute( s:temp, '?', '{\\letterquestionmark}', 'g')
let s:temp = substitute( s:temp, '!', '{\\letterexclamationmark}', 'g')
let s:temp = substitute( s:temp, '@', '{\\letterat}', 'g')
let s:temp = substitute( s:temp, ':', '{\\lettercolon}', 'g')
let s:new = s:new . '\vimsyntax[' . s:id_name . ']{' . s:temp . '}'
" Why will we ever enter this loop
" if s:col > s:len
" break
" endif
endwhile
"D Expand tabs
let s:pad = 0
let s:start = 0
let s:idx = stridx(s:line, "\t")
while s:idx >= 0
let s:i = &ts - ((s:start + s:pad + s:idx) % &ts)
let s:new = substitute(s:new, '\t', strpart(s:expandedtab, 0, s:i), '')
let s:pad = s:pad + s:i - 1
let s:start = s:start + s:idx + 1
let s:idx = stridx(strpart(s:line, s:start), "\t")
endwhile
"D Go back and paste the current line
wincmd p
call append (s:buffer_lnum-1, s:new)
wincmd p
"D Increment line numbers
let s:lnum = s:lnum + 1
let s:buffer_lnum = s:buffer_lnum + 1
endwhile
wincmd p
[-- Attachment #4: Type: text/plain, Size: 139 bytes --]
_______________________________________________
ntg-context mailing list
ntg-context@ntg.nl
http://www.ntg.nl/mailman/listinfo/ntg-context
next prev parent reply other threads:[~2006-12-28 17:09 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2006-12-25 9:24 cormullion
2006-12-25 10:38 ` Taco Hoekwater
2006-12-25 15:22 ` cormullion
2006-12-26 16:59 ` Henning Hraban Ramm
2006-12-26 18:47 ` Mojca Miklavec
2006-12-26 21:25 ` cormullion
2006-12-27 21:41 ` Aditya Mahajan
2006-12-28 8:15 ` cormullion
2006-12-28 17:09 ` Aditya Mahajan [this message]
2006-12-28 18:05 ` cormullion
2006-12-28 23:47 ` Mojca Miklavec
2006-12-29 8:46 ` cormullion
2006-12-29 13:09 ` Mojca Miklavec
2006-12-29 15:37 ` Aditya Mahajan
2006-12-29 15:42 ` cormullion
2006-12-29 15:59 ` Aditya Mahajan
2006-12-29 16:36 ` cormullion
2006-12-29 18:39 ` Mojca Miklavec
2006-12-30 9:22 ` Jarimatti Valkonen
2006-12-30 14:18 ` cormullion
2006-12-30 21:24 ` Jarimatti Valkonen
2006-12-31 11:51 ` cormullion
2006-12-31 13:40 ` Mojca Miklavec
2006-12-31 16:57 ` cormullion
2006-12-25 18:12 ` Mojca Miklavec
2006-12-25 19:37 ` Aditya Mahajan
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=alpine.WNT.0.81.0612281148220.2372@nqvgln \
--to=adityam@umich.edu \
--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).