From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/51315 Path: news.gmane.org!not-for-mail From: Matthijs Kooijman Newsgroups: gmane.comp.tex.context Subject: Re: [PATCH] Add begin_of_buffer and end_of_buffer hooks for lua pretty printers Date: Mon, 15 Jun 2009 11:00:11 +0200 Message-ID: <20090615090010.GN1611@katherina.student.utwente.nl> References: <20090611154718.GD1611@katherina.student.utwente.nl> <4A34F5B6.1020704@wxs.nl> Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="===============0448665894==" X-Trace: ger.gmane.org 1245056479 8160 80.91.229.12 (15 Jun 2009 09:01:19 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 15 Jun 2009 09:01:19 +0000 (UTC) To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Mon Jun 15 11:01:16 2009 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from ronja.vet.uu.nl ([131.211.172.88] helo=ronja.ntg.nl) by lo.gmane.org with esmtp (Exim 4.50) id 1MG843-0005dL-GL for gctc-ntg-context-518@m.gmane.org; Mon, 15 Jun 2009 11:01:15 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 280F81FBC1; Mon, 15 Jun 2009 11:01:14 +0200 (CEST) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 18016-01; Mon, 15 Jun 2009 11:00:29 +0200 (CEST) Original-Received: from ronja.vet.uu.nl (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 56E961FB71; Mon, 15 Jun 2009 11:00:29 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by ronja.ntg.nl (Postfix) with ESMTP id 580B41FB71 for ; Mon, 15 Jun 2009 11:00:27 +0200 (CEST) Original-Received: from ronja.ntg.nl ([127.0.0.1]) by localhost (smtp.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with LMTP id 17371-02-2 for ; Mon, 15 Jun 2009 11:00:18 +0200 (CEST) Original-Received: from filter2-nij.mf.surf.net (filter2-nij.mf.surf.net [195.169.124.153]) by ronja.ntg.nl (Postfix) with ESMTP id F08341FB1F for ; Mon, 15 Jun 2009 11:00:17 +0200 (CEST) Original-Received: from katherina.student.utwente.nl (katherina.student.utwente.nl [130.89.168.160]) by filter2-nij.mf.surf.net (8.13.8/8.13.8/Debian-3) with ESMTP id n5F90BWe025982 for ; Mon, 15 Jun 2009 11:00:11 +0200 Original-Received: from matthijs by katherina.student.utwente.nl with local (Exim 4.69) (envelope-from ) id 1MG831-0003E0-AK for ntg-context@ntg.nl; Mon, 15 Jun 2009 11:00:11 +0200 Mail-Followup-To: mailing list for ConTeXt users In-Reply-To: <4A34F5B6.1020704@wxs.nl> X-PGP-Fingerprint: 7F6A 9F44 2820 18E2 18DE 24AA CF49 D0E6 8A2F AFBC X-PGP-Key: http://katherina.student.utwente.nl/~matthijs/gpg_pubkey.asc User-Agent: Mutt/1.5.18 (2008-05-17) X-Bayes-Prob: 0.0001 (Score 0, tokens from: @@RPTN) X-CanIt-Geo: ip=130.89.168.160; country=NL; region=15; city=Enschede; latitude=52.2167; longitude=6.9000; http://maps.google.com/maps?q=52.2167,6.9000&z=6 X-CanItPRO-Stream: uu:ntg-context@ntg.nl (inherits from uu:default, base:default) X-Canit-Stats-ID: 243150876 - a78d9cc31940 - 20090615 X-Scanned-By: CanIt (www . roaringpenguin . com) on 195.169.124.153 X-Virus-Scanned: amavisd-new at ntg.nl X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.11 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: ntg-context-bounces@ntg.nl Errors-To: ntg-context-bounces@ntg.nl X-Virus-Scanned: amavisd-new at ntg.nl Xref: news.gmane.org gmane.comp.tex.context:51315 Archived-At: --===============0448665894== Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="eWmFpGZayiNrn4FL" Content-Disposition: inline --eWmFpGZayiNrn4FL Content-Type: multipart/mixed; boundary="PpocKf6TCvdC9BKE" Content-Disposition: inline --PpocKf6TCvdC9BKE Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Hi Hans, > (no patch nor example of usage attached) *sigh*, attaching files is always hard :-) Anyway, I've attached it now, together with an exapmle prettyprinter that uses it. I've also slightly changed the patch since my last message. Now, begin_of_buffer and end_of_buffer are always called, even for single line \type's. I found that I had to do all kinds of magic to make things work for \type as well, so this seems cleaner. To be able to differentiate, the type of buffer ('single', 'buffer' or 'file') and the name of the buffer/file gets passed to begin/end_of_buffer, though my example prettyprinter does not use these yet. Before, \type directly called hooks.flush_line. I've added buffers.typesingle to be called instead, which calls begin_of_line, flush_line and end_of_line. To prevent confusing with \type, I've renamed buffers.type to buffers.typebuffer. Finally, I've added some comments to the hooks.* functions, to document when they are called. Is there any place where lua pretty printers are documented? The wiki seems to say only "wait for lua", but not how to use them. If there isn't, I'll try to put something useful on the wiki. Gr. Matthijs --PpocKf6TCvdC9BKE Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename=begin-end-buffer Content-Transfer-Encoding: quoted-printable Index: base/buff-ini.lua =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- base.orig/buff-ini.lua 2009-06-15 10:23:58.000000000 +0200 +++ base/buff-ini.lua 2009-06-15 10:44:03.000000000 +0200 @@ -120,7 +120,15 @@ return first, last, last - first + 1 end =20 -function buffers.type(name) +-- Types a single line of text (used by \type) +function buffers.typesingle(text) + hooks.begin_of_buffer('single') + hooks.flush_line(text) + hooks.end_of_buffer('single') +end + +-- Types text from the named buffer (used by \typebuffer and \starttyping) +function buffers.typebuffer(name) local lines =3D data[name] local action =3D buffers.typeline if lines then @@ -129,9 +137,11 @@ end local line, n =3D 0, 0 local first, last, m =3D buffers.strip(lines) + hooks.begin_of_buffer('buffer', name) for i=3Dfirst,last do n, line =3D action(lines[i], n, m, line) end + hooks.end_of_buffer('buffer', name) end end =20 @@ -145,15 +155,18 @@ return str or "" end =20 +-- Types text from the named file (used by \typefile) function buffers.typefile(name) -- still somewhat messy, since name can be= be suffixless local str =3D buffers.loaddata(name) if str and str~=3D "" then local lines =3D str:splitlines() local line, n, action =3D 0, 0, buffers.typeline local first, last, m =3D buffers.strip(lines) + hooks.begin_of_buffer('file', name) for i=3Dfirst,last do n, line =3D action(lines[i], n, m, line) end + hooks.end_of_buffer('file', name) end end =20 @@ -301,23 +314,43 @@ =20 -- calling routines, don't change =20 +-- Called at the start of every piece of text that is prettyprinted. Type = can +-- be: 'single', when only a single line is prettyprinted (e.g. \type), +-- 'buffer', when a buffer is prettyprinted (e.g. \typebuffer) or 'file', = when +-- a file is prettyprinted (e.g. \typefile). +-- name is the name of the buffer or file, and empty for 'single'. The spe= cial +-- buffer name '_typing_' is used for \starttyping. +function hooks.begin_of_buffer(type, name) + (visualizers[buffers.currentvisualizer].begin_of_buffer or default.beg= in_of_buffer)(type, name) +end + +-- Called at the end of every piece of text that is prettyprinted. +function hooks.end_of_buffer(type, name) + (visualizers[buffers.currentvisualizer].end_of_buffer or default.end_o= f_buffer)(type, name) +end + +-- Called for every non-empty line function hooks.flush_line(str,nesting) str =3D gsub(str," *[\n\r]+ *"," ") ; -- semi colon needed (visualizers[buffers.currentvisualizer].flush_line or default.flush_li= ne)(str,nesting) end =20 +-- Called at the start of every non-empty line (before flush_line) function hooks.begin_of_line(n) (visualizers[buffers.currentvisualizer].begin_of_line or default.begin= _of_line)(n) end =20 +-- Called at the end of every non-empty line (after flush_line) function hooks.end_of_line() (visualizers[buffers.currentvisualizer].end_of_line or default.end_of_= line)() end =20 +-- Called for every empty line function hooks.empty_line() (visualizers[buffers.currentvisualizer].empty_line or default.empty_li= ne)() end =20 +-- Called for every non-empty line. The result is passed to flush_line. function hooks.line(str) if visualizers.enabletab then str =3D string.tabtospace(str,visualizers.tablength) @@ -329,6 +362,12 @@ =20 -- defaults =20 +function default.begin_of_buffer(type, name) +end + +function default.end_of_buffer(type, name) +end + function default.begin_of_line(n) texsprint(ctxcatcodes, commands.begin_of_line_command,"{",n,"}") end Index: base/buff-ini.mkiv =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- base.orig/buff-ini.mkiv 2009-06-15 10:26:17.000000000 +0200 +++ base/buff-ini.mkiv 2009-06-15 10:30:03.000000000 +0200 @@ -161,13 +161,13 @@ =20 \def\doprocessbufferverbatim {\doinitializeverbatim - \ctxlua{buffers.type("\currentbuffer")}} + \ctxlua{buffers.typebuffer("\currentbuffer")}} =20 \def\doprocessbufferlinesverbatim#1#2#3% {#2% % todo, set up numbers \doinitializeverbatim - \ctxlua{buffers.type("\currentbuffer")} + \ctxlua{buffers.typebuffer("\currentbuffer")} #3} =20 \def\doifelsebuffer#1% Index: base/buff-ver.mkiv =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- base.orig/buff-ver.mkiv 2009-06-15 10:24:38.000000000 +0200 +++ base/buff-ver.mkiv 2009-06-15 10:31:34.000000000 +0200 @@ -292,7 +292,7 @@ \def\dodotypeAA#1% {\doinitializeverbatim \def\obs{\obeyedspace}% - \ctxlua{buffers.hooks.flush_line(\!!bs\detokenize{#1}\!!es)}% + \ctxlua{buffers.typesingle(\!!bs\detokenize{#1}\!!es)}% \egroup} =20 \def\dodotypeB#1% @@ -316,7 +316,7 @@ \def\dodotypeCC#1% {\doinitializeverbatim \ifx\obeycharacters\setupprettytype % temp hack, we need a proper signal - \ctxlua{buffers.hooks.flush_line([\!!bs\detokenize{#1}\!!es,true)}% + \ctxlua{buffers.typesingle([\!!bs\detokenize{#1}\!!es,true)}% \else \def\obs{\obeyedspace}% \ctxlua{buffers.visualizers.flush_nested(\!!bs\detokenize{#1}\!!es,tr= ue)}% @@ -333,7 +333,7 @@ =20 \def\dodotypeDD#1% {\doinitializeverbatim - \ctxlua{buffers.hooks.flush_line(\!!bs\detokenize{#1}\!!es,true)}% + \ctxlua{buffers.typesingle(\!!bs\detokenize{#1}\!!es,true)}% \egroup \gobbleoneargument} % grab last > =20 @@ -573,7 +573,7 @@ {} {\doinitializeverbatim \beginofverbatimlines - \ctxlua{buffers.type("_typing_")}% + \ctxlua{buffers.typebuffer("_typing_")}% \endofverbatimlines \getvalue{\strippedcsname#2}}} =20 --PpocKf6TCvdC9BKE Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="pret-lam.lua" Content-Transfer-Encoding: quoted-printable -- filename : type-lam.lua -- comment : Pretty printing of (extended) lambda calculus -- author : Matthijs Kooijman, Universiteit Twente, NL -- copyright: Matthijs Kooijman -- license : None local utf =3D unicode.utf8 if not buffers then buffers =3D { } end if not buffers.visualizers then buffers.visualizers =3D { } end if not buffers.visualizers.lam then buffers.visualizers.lam =3D { } end buffers.visualizers.lam.colors =3D { "prettytwo", "prettyone", "prettythree", "prettyfour" } -- Symbols that should have a different representation buffers.visualizers.lam.symbols =3D { [' '] =3D {repr =3D '\\obs '}, ['_'] =3D {repr =3D '\\_'}, ['->'] =3D {repr =3D '\\rightarrow'}, -- The default * sits very high above the baseline, \ast (u+2217) looks -- better. ['*'] =3D {repr =3D '\\ast'}, } -- Keywords that should be bold buffers.visualizers.lam.keywords =3D { ['case'] =3D {}, ['of'] =3D {}, ['let'] =3D {}, ['in'] =3D {}, } -- See if str starts with a symbol, and return the remaining string and that -- symbol. If no symbol from the table is matched, just returns the first -- character. We can do a lookup directly, since symbols can be different = in -- length, so we just loop over all symbols, trying them in turn. function buffers.visualizers.lam.take_symbol(str) for symbol,props in pairs(buffers.visualizers.lam.symbols) do -- Try to remove symbol from the start of str=20 symbol, newstr =3D utf.match(str, "^(" .. symbol .. ")(.*)") if symbol then -- Return this tokens repr, or just the token if it has no -- repr. res =3D props.repr or symbol -- Enclose the token in {\style .. } if props.style then res =3D "{\\" .. props.style .. " " .. res .. "}" end return res, newstr end end -- No symbol found, just return the first character return utf.match(str, "^(.)(.*)") end -- Take a single word from str, if posible. Returns the rest of the string = and -- the word taken. function buffers.visualizers.lam.take_word(str) res, newstr =3D utf.match(str, "^(%a[%a%d_]+)(.*)") return res, newstr or str end -- Tries to match each of the patterns and returns the captures of the first -- matching pattern (up to 5 captures are supported). Returns nil when noth= ing -- matches. function buffers.visualizers.lam.match_mul(str, patterns) for i, pat in ipairs(patterns) do a, b, c, d, e =3D utf.match(str, pat) if a then return a, b, c, d, e end end return nil end -- Find any subscripts in the given word and typeset them function buffers.visualizers.lam.do_subscripts(word) local match_mul =3D buffers.visualizers.lam.match_mul base, sub =3D match_mul(res, submatches) if sub then word =3D base .. "\\low{" .. sub .. "}" -- After a word has been used as a base, allow subscripts -- without _, even for non-numbers. if not bases[base] then -- Register that we've added this base bases[base] =3D true -- Add a pattern for this base submatches[#submatches+1] =3D "^(" .. base .. ")([%a%d,]+)$" end end return word end function buffers.visualizers.lam.begin_of_buffer(type, name) -- Initially allow subscripts using _ or just appending a number (later, -- we will add extra patterns here. submatches =3D {"^(.*)_([%a%d,]+)$", "^(.*[^%d])(%d+)$"} -- This stores all the bases we've encountered so far (to prevent -- duplicates). For each of them there will be a pattern in submatches -- above. bases =3D {} end function buffers.visualizers.lam.flush_line(str,nested) local result, state =3D { }, 0 local finish, change =3D buffers.finish_state, buffers.change_state local take_symbol =3D buffers.visualizers.lam.take_symbol local take_word =3D buffers.visualizers.lam.take_word local do_subscripts =3D buffers.visualizers.lam.do_subscripts -- Set the colorscheme, which is used by finish_state and change_state buffers.currentcolors =3D buffers.visualizers.lam.colors while str ~=3D "" do local found =3D false local word, symbol -- See if the next token is a word word, str =3D take_word(str) if word then if buffers.visualizers.lam.keywords[res] then -- Make all keywords bold word =3D "{\\bold " .. word .. "}" else -- Process any subscripts in the word word =3D do_subscripts(word) end else -- The next token is not a word, it must be a symbol symbol, str =3D take_symbol(str) end -- Append the resulting token result[#result+1] =3D word or symbol end state =3D finish(state, result) buffers.flush_result(result,nested) end -- vim: set sw=3D4 sts=3D4 expandtab ai: --PpocKf6TCvdC9BKE-- --eWmFpGZayiNrn4FL Content-Type: application/pgp-signature; name="signature.asc" Content-Description: Digital signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (GNU/Linux) iEYEARECAAYFAko2DZoACgkQz0nQ5oovr7xirgCgrMEf/OnXNtFSWBcC/bhz184a acEAoMTgNqWHXIBuBY2ORlgZHoJREbCd =IlZ8 -----END PGP SIGNATURE----- --eWmFpGZayiNrn4FL-- --===============0448665894== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline ___________________________________________________________________________________ If your question is of interest to others as well, please add an entry to the Wiki! maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context webpage : http://www.pragma-ade.nl / http://tex.aanhet.net archive : https://foundry.supelec.fr/projects/contextrev/ wiki : http://contextgarden.net ___________________________________________________________________________________ --===============0448665894==--