From mboxrd@z Thu Jan 1 00:00:00 1970 X-Msuck: nntp://news.gmane.io/gmane.comp.tex.context/98466 Path: news.gmane.org!.POSTED!not-for-mail From: Gerion Entrup Newsgroups: gmane.comp.tex.context Subject: Re: t-vim module: math in "normal" code and background color Date: Wed, 19 Jul 2017 00:30:15 +0200 Message-ID: <2316403.2IsKS5fdyP@gump> References: <2045783.Ocy15AQZ2X@gump> Reply-To: mailing list for ConTeXt users NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart3534905.MP5nxbaxpe" Content-Transfer-Encoding: 7Bit X-Trace: blaine.gmane.org 1500417063 1225 195.159.176.226 (18 Jul 2017 22:31:03 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 18 Jul 2017 22:31:03 +0000 (UTC) To: mailing list for ConTeXt users Original-X-From: ntg-context-bounces@ntg.nl Wed Jul 19 00:30:58 2017 Return-path: Envelope-to: gctc-ntg-context-518@m.gmane.org Original-Received: from zapf.boekplan.nl ([5.39.185.232] helo=zapf.ntg.nl) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dXb11-00087z-Jh for gctc-ntg-context-518@m.gmane.org; Wed, 19 Jul 2017 00:30:51 +0200 Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 5B1C410494F; Wed, 19 Jul 2017 00:30:29 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id PQ64cBYDgD1d; Wed, 19 Jul 2017 00:30:28 +0200 (CEST) Original-Received: from zapf.ntg.nl (localhost [IPv6:::1]) by zapf.ntg.nl (Postfix) with ESMTP id 91C571045AD; Wed, 19 Jul 2017 00:30:28 +0200 (CEST) Original-Received: from localhost (localhost [127.0.0.1]) by zapf.ntg.nl (Postfix) with ESMTP id 75E091045AD for ; Wed, 19 Jul 2017 00:30:27 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at zapf.boekplan.nl Original-Received: from zapf.ntg.nl ([127.0.0.1]) by localhost (zapf.ntg.nl [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id y00a6S2bDkVg for ; Wed, 19 Jul 2017 00:30:26 +0200 (CEST) Original-Received: from mail.flump.de (unknown [84.200.20.107]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by zapf.ntg.nl (Postfix) with ESMTPS id 36A351011FC for ; Wed, 19 Jul 2017 00:30:16 +0200 (CEST) Original-Received: from gump.localnet (p5B0DC819.dip0.t-ipconnect.de [91.13.200.25]) by mail.flump.de (Postfix) with ESMTPSA id 66A21183DEE for ; Wed, 19 Jul 2017 00:30:13 +0200 (CEST) In-Reply-To: X-BeenThere: ntg-context@ntg.nl X-Mailman-Version: 2.1.16 Precedence: list List-Id: mailing list for ConTeXt users List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: ntg-context-bounces@ntg.nl Original-Sender: "ntg-context" Xref: news.gmane.org gmane.comp.tex.context:98466 Archived-At: This is a multi-part message in MIME format. --nextPart3534905.MP5nxbaxpe Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="UTF-8" Am Dienstag, 18. Juli 2017, 04:04:51 CEST schrieb Aditya Mahajan: > On Mon, 17 Jul 2017, Gerion Entrup wrote: >=20 > > Hi, > > > > I have two questions with the vim module. > > > > 1. I want to use the math mode inside the code. I've seen the escape op= tion,=20 > > e.g. here [1], but this seems to work only with comments. Is there a=20 > > possibility to use it directly in the code, too? >=20 > Short answer. No. >=20 > Long answer. t-vim relies on vim to syntax highlight the code. Since the= =20 > code is not valid python, the default python syntax highlighting will not= =20 > work. In principle, it is possible to write a vim syntax highlighting=20 > script for a derivative of python where math terms are allowed, but that= =20 > requires a lot of work for each language. The whole point of t-vim module= =20 > was that I am lazy and don't want to write the parser for each language=20 > :-) I've invested some time and rewrote parts of the vimscript file of t-vim. Now one or more escapechars can be defined, that helps vim to not interpret the text: ``` \usemodule[vim] \definevimtyping[python][syntax=3Dpython] \definevimtyping[cpp][syntax=3Dcpp] \starttext \startpython # Returns range(=C2=B0\m{\sum_{i=3D1}^{n}i \in F \int_i f}=C2=B0 def sum_upto(=C2=B0\m{n \in \mathbb{N} \sum_{i=3D1}}=C2=B0) r =3D range(1, =C2=B0\m{n}=C2=B0 + =C2=B0\m{\sum_{i=3D1} 1}=C2=B0) return sum(r) \stoppython \startcpp foobar(=C2=B0\m{q_0}=C2=B0); \stopcpp \stoptext ``` I'm not familiar enough with TeX or ConTeXt to get the TeX part to work, so the escapechar '=C2=B0' is hardcoded at the moment. If you like the patch, = this has to be fixed. Another problem, as you see in the above example, is, that vim interprets the second ')' in the startcpp section as color code "Error", so the whole part is colorized. This can be circumventented with the escapechars '=C2=B0= "', but I have no idea how to teach tex/lua to not interpret the '"'. Simply writing -c "let escapechar=3D'=C2=B0\"'" % etc. in the t-vim.tex does not work. Another point I saw is, that highlight together with TeX-code is not really= usable: ``` \usemodule[vim] \definevimtyping[python][syntax=3Dpython, escape=3Don] \starttext \startpython[highlight=3D1] # Returns \m{\sum_{i=3D1}^{n}i \in F \int_i f} \stoppython \stoptext ``` =20 > > Minimal example: > > ``` > > \usemodule[vim] > > \definevimtyping[python][syntax=3Dpython, escape=3Don] > > > > \starttext > > \startpython > > # Returns \m{\sum_{i=3D1}^{n}i} > > def sum_upto(\m{n \in \mathbb{N}}) > > r =3D range(1, \m{n} + 1) > > return sum(r) > > \stoppython > > \stoptext > > ``` >=20 > Another option will be to use the algorithmic module: https://bitbucket.o= rg/wolfs/algorithmic/src/ >=20 > > 2. It would be cool, if I can define some background color for the code= =2E=20 > > Unfortunately I'm very new to context and haven't seen a direct option.= I=20 > > assume this is possible with some kind of extra environment around the = code? >=20 > You can add >=20 > \setupbackground[background=3Dcolor, backgroundcolor=3Dgray] >=20 > \setupvimtyping[python] > [ > before=3D{\startbackground}, > after=3D{\stopbackground}, > ] >=20 > or, instead of modifying the default background, define a new background= =20 > and use that. OK, thank you. Gerion --nextPart3534905.MP5nxbaxpe Content-Disposition: attachment; filename="0001-t-vim-extended-escape-mode.patch" Content-Transfer-Encoding: quoted-printable Content-Type: text/x-patch; charset="UTF-8"; name="0001-t-vim-extended-escape-mode.patch" =46rom 994de513a23210f1f4d50d24270f8f988abdd21c Mon Sep 17 00:00:00 2001 =46rom: Gerion Entrup Date: Wed, 19 Jul 2017 00:13:52 +0200 Subject: [PATCH] t-vim: extended escape mode With this change it is possible to define one or more escapechars, that are used by vim to recognize parts that should not be interpreted. =2D-- 2context.vim | 50 +++++++++++++++++++++++++++++++++----------------- t-vim.tex | 12 ++---------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/2context.vim b/2context.vim index 9c72ba1..b208e65 100644 =2D-- a/2context.vim +++ b/2context.vim @@ -8,6 +8,11 @@ " output buffer. The script parses content line-by-line from the first buf= fer " and pastes the modified result on the second buffer. =20 +" Compare a char of at with some Unicode +function UnicodeCompare(string, bytepos, ochar) + return strpart(a:string, a:bytepos, max([strlen(a:ochar), 1])) =3D=3D#= a:ochar +endfunction + " Split screen and go to the second buffer, ensure modifiable is set, and = the " buffer is empty. sblast=20 @@ -38,9 +43,9 @@ if !exists("highlight") let highlight=3D[] endif =20 =2D" Set escapecomments =2Dif !exists("escapecomments") =2D let escapecomments=3D0 +" Set escapechar +if !exists("escapechar") + let escapechar=3D'' endif =20 let s:strip =3D strlen( matchstr( getline(s:lstart), '^\s*' ) ) @@ -71,37 +76,48 @@ let s:lines =3D [] let s:buffer_lnum =3D 1 let s:lnum =3D s:lstart =20 +let s:lenesc =3D strlen(escapechar) + while s:lnum <=3D s:lstop " Get the current line let s:line =3D getline(s:lnum) let s:len =3D strlen(s:line) =2D let s:new =3D ''=20 + let s:new =3D '' =20 " Loop over each character in the line =2D let s:col =3D s:strip + 1 =2D while s:col <=3D s:len + let s:col =3D s:strip + while s:col < s:len let s:startcol =3D s:col " The start column for processing text =2D let s:id =3D synID (s:lnum, s:col, 1) =2D let s:col =3D s:col + 1 + let s:temp =3D '' +" ignore escaped sequences + if UnicodeCompare(s:line, s:col, escapechar) + let s:nextesc =3D stridx(s:line, escapechar, s:col + 1) + let s:temp =3D strpart(s:line, s:col + s:lenesc, s:nextesc - s:= col - s:lenesc) + let s:col =3D s:nextesc + s:lenesc + let s:startcol =3D s:col + endif + if s:col > s:len + let s:id =3D 0 " dummy id if whole line is escaped + else + let s:id =3D synID (s:lnum, s:col + 1, 1) + let s:col =3D s:col + 1 " Speed loop (it's small - that's the trick) =2D" Go along till we find a change in synID =2D while s:col <=3D s:len && s:id =3D=3D synID(s:lnum, s:col, 1)=20 =2D let s:col =3D s:col + 1=20 =2D endwhile +" Go along till we find a change in synID or an escapechar + while s:col < s:len && !UnicodeCompare(s:line, s:col, escapechar) &&= s:id =3D=3D synID(s:lnum, s:col + 1, 1) + let s:col =3D s:col + 1 + endwhile + endif =20 " Output the text with the same synID, with class set to {s:id_name} let s:id =3D synIDtrans (s:id) let s:id_name =3D synIDattr (s:id, "name", "gui") =2D let s:temp =3D strpart(s:line, s:startcol - 1, s:col - s:startcol) + let s:part =3D strpart(s:line, s:startcol, s:col - s:startcol) + let s:temp =3D s:temp . escape( s:part, '\{}') " Remove line endings (on unix machines reading windows files) let s:temp =3D substitute(s:temp, '\r*$', '', '') " It might have happened that that one has been the last item in a row, so " we don't need to print in in that case if strlen(s:temp) > 0 =2D" Change special TeX characters to escape sequences. =2D if !(escapecomments && s:id_name =3D=3D "Comment") =2D let s:temp =3D escape( s:temp, '\{}') =2D endif if !empty(s:id_name) let s:temp =3D '\SYN[' . s:id_name . ']{' . s:temp . '}' endif diff --git a/t-vim.tex b/t-vim.tex index 542261f..9d0bb50 100644 =2D-- a/t-vim.tex +++ b/t-vim.tex @@ -129,7 +129,7 @@ -c "let contextstartline=3D\externalfilterparameter\c!start \letter= bar\space % let contextstopline=3D\externalfilterparameter\c!stop \letter= bar\space % let strip=3D\getvalue{\vimtyping@id-\c!strip-\externalfilterpar= ameter\c!strip}" % =2D -c "let escapecomments=3D\getvalue{\vimtyping@id-\c!escape-\exter= nalfilterparameter\c!escape}" % + -c "let escapechar=3D'=C2=B0'" % -c "let highlight=3D[\externalfilterparameter\c!highlight]" % \vimrc_extras\space -c "source \vimtyping@script_name" % @@ -140,18 +140,10 @@ \setvalue{\vimtyping@id-\c!strip-\v!off}{0} \setvalue{\vimtyping@id-\c!strip-\v!on}{1} =20 =2D\setvalue{\vimtyping@id-\c!escape-\v!off}{0} =2D\setvalue{\vimtyping@id-\c!escape-\v!on}{1} =2D =2D % Undocumented ... but useful if the user makes a mistake \setvalue{\vimtyping@id-\c!strip-\v!no}{0} \setvalue{\vimtyping@id-\c!strip-\v!yes}{1} =20 =2D\setvalue{\vimtyping@id-\c!escape-\v!no}{0} =2D\setvalue{\vimtyping@id-\c!escape-\v!yes}{1} =2D =2D \setupvimtyping [% \c!tab=3D4, % \c!start=3D1, @@ -163,7 +155,7 @@ % \c!style=3D\tttf, % \c!color=3D, \c!strip=3D\v!off, =2D \c!escape=3D\v!off, + % \c!escapechar=3D, % \c!highlight=3D, % \c!highlightcolor=3Dlightgray, \c!filtercommand=3D\vimtyping@filter_command, =2D-=20 2.13.0 --nextPart3534905.MP5nxbaxpe Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX18KSWYgeW91ciBxdWVzdGlvbiBpcyBvZiBpbnRlcmVz dCB0byBvdGhlcnMgYXMgd2VsbCwgcGxlYXNlIGFkZCBhbiBlbnRyeSB0byB0aGUgV2lraSEKCm1h aWxsaXN0IDogbnRnLWNvbnRleHRAbnRnLm5sIC8gaHR0cDovL3d3dy5udGcubmwvbWFpbG1hbi9s aXN0aW5mby9udGctY29udGV4dAp3ZWJwYWdlICA6IGh0dHA6Ly93d3cucHJhZ21hLWFkZS5ubCAv IGh0dHA6Ly9jb250ZXh0LmFhbmhldC5uZXQKYXJjaGl2ZSAgOiBodHRwczovL2JpdGJ1Y2tldC5v cmcvcGhnL2NvbnRleHQtbWlycm9yL2NvbW1pdHMvCndpa2kgICAgIDogaHR0cDovL2NvbnRleHRn YXJkZW4ubmV0Cl9fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19f --nextPart3534905.MP5nxbaxpe--