ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
From: Gerion Entrup <gerion.entrup-ctex@flump.de>
To: mailing list for ConTeXt users <ntg-context@ntg.nl>
Subject: Re: t-vim module: math in "normal" code and background color
Date: Wed, 19 Jul 2017 00:30:15 +0200	[thread overview]
Message-ID: <2316403.2IsKS5fdyP@gump> (raw)
In-Reply-To: <alpine.OSX.2.02.1707172154010.27788@nqv-znpobbx>

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

Am Dienstag, 18. Juli 2017, 04:04:51 CEST schrieb Aditya Mahajan:
> On Mon, 17 Jul 2017, Gerion Entrup wrote:
> 
> > 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 option, 
> > e.g. here [1], but this seems to work only with comments. Is there a 
> > possibility to use it directly in the code, too?
> 
> Short answer. No.
> 
> Long answer. t-vim relies on vim to syntax highlight the code. Since the 
> code is not valid python, the default python syntax highlighting will not 
> work. In principle, it is possible to write a vim syntax highlighting 
> script for a derivative of python where math terms are allowed, but that 
> requires a lot of work for each language. The whole point of t-vim module 
> was that I am lazy and don't want to write the parser for each language 
> :-)

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=python]
\definevimtyping[cpp][syntax=cpp]

\starttext
\startpython
# Returns range(°\m{\sum_{i=1}^{n}i \in F \int_i f}°
def sum_upto(°\m{n \in \mathbb{N} \sum_{i=1}}°)
    r = range(1, °\m{n}° + °\m{\sum_{i=1} 1}°)
    return sum(r)
\stoppython
\startcpp
  foobar(°\m{q_0}°);
\stopcpp
\stoptext
```

I'm not familiar enough with TeX or ConTeXt to get the TeX part to work, so
the escapechar '°' 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 '°"', but
I have no idea how to teach tex/lua to not interpret the '"'. Simply writing
 -c "let escapechar='°\"'" %
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=python, escape=on]

\starttext
\startpython[highlight=1]
# Returns \m{\sum_{i=1}^{n}i \in F \int_i f}
\stoppython
\stoptext
```
 
> > Minimal example:
> > ```
> > \usemodule[vim]
> > \definevimtyping[python][syntax=python, escape=on]
> >
> > \starttext
> > \startpython
> > # Returns \m{\sum_{i=1}^{n}i}
> > def sum_upto(\m{n \in \mathbb{N}})
> >    r = range(1, \m{n} + 1)
> >    return sum(r)
> > \stoppython
> > \stoptext
> > ```
> 
> Another option will be to use the algorithmic module: https://bitbucket.org/wolfs/algorithmic/src/
> 
> > 2. It would be cool, if I can define some background color for the code. 
> > Unfortunately I'm very new to context and haven't seen a direct option. I 
> > assume this is possible with some kind of extra environment around the code?
> 
> You can add
> 
> \setupbackground[background=color, backgroundcolor=gray]
> 
> \setupvimtyping[python]
>      [
>        before={\startbackground},
>        after={\stopbackground},
>      ]
> 
> or, instead of modifying the default background, define a new background 
> and use that.
OK, thank you.

Gerion


[-- Attachment #2: 0001-t-vim-extended-escape-mode.patch --]
[-- Type: text/x-patch, Size: 5310 bytes --]

From 994de513a23210f1f4d50d24270f8f988abdd21c Mon Sep 17 00:00:00 2001
From: Gerion Entrup <gerion.entrup@flump.de>
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.
---
 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
--- a/2context.vim
+++ b/2context.vim
@@ -8,6 +8,11 @@
 " output buffer. The script parses content line-by-line from the first buffer
 " and pastes the modified result on the second buffer.
 
+" Compare a char of <string> at <bytepos> with some Unicode <ochar>
+function UnicodeCompare(string, bytepos, ochar)
+    return strpart(a:string, a:bytepos, max([strlen(a:ochar), 1])) ==# a:ochar
+endfunction
+
 " Split screen and go to the second buffer, ensure modifiable is set, and the
 " buffer is empty.
 sblast 
@@ -38,9 +43,9 @@ if !exists("highlight")
   let highlight=[]
 endif
 
-" Set escapecomments
-if !exists("escapecomments")
-  let escapecomments=0
+" Set escapechar
+if !exists("escapechar")
+  let escapechar=''
 endif
 
 let s:strip = strlen( matchstr( getline(s:lstart), '^\s*' ) )
@@ -71,37 +76,48 @@ let s:lines = []
 let s:buffer_lnum = 1
 let s:lnum = s:lstart
 
+let s:lenesc = strlen(escapechar)
+
 while s:lnum <= s:lstop
 " Get the current line
   let s:line = getline(s:lnum)
   let s:len  = strlen(s:line)
-  let s:new  = '' 
+  let s:new  = ''
 
 " Loop over each character in the line
-  let s:col = s:strip + 1
-  while s:col <= s:len
+  let s:col = s:strip
+  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
+    let s:temp = ''
+" ignore escaped sequences
+    if UnicodeCompare(s:line, s:col, escapechar)
+      let s:nextesc = stridx(s:line, escapechar, s:col + 1)
+      let s:temp      = strpart(s:line, s:col + s:lenesc, s:nextesc - s:col - s:lenesc)
+      let s:col       = s:nextesc + s:lenesc
+      let s:startcol  = s:col
+    endif
+    if s:col > s:len
+      let s:id   = 0 " dummy id if whole line is escaped
+    else
+      let s:id       = synID (s:lnum, s:col + 1, 1)
+      let s:col      = s:col + 1
 " Speed loop (it's small - that's the trick)
-" 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
+" 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 == synID(s:lnum, s:col + 1, 1)
+        let s:col = s:col + 1
+      endwhile
+    endif
 
 " 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)
+    let s:part    = strpart(s:line, s:startcol, s:col - s:startcol)
+    let s:temp    = s:temp . escape( s:part, '\{}')
 " Remove line endings (on unix machines reading windows files)
     let s:temp    = 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
-" Change special TeX characters to escape sequences.
-      if !(escapecomments && s:id_name == "Comment")
-        let s:temp = escape( s:temp, '\{}')
-      endif
       if !empty(s:id_name)
         let s:temp = '\SYN[' . s:id_name . ']{' . s:temp .  '}'
       endif
diff --git a/t-vim.tex b/t-vim.tex
index 542261f..9d0bb50 100644
--- a/t-vim.tex
+++ b/t-vim.tex
@@ -129,7 +129,7 @@
        -c "let contextstartline=\externalfilterparameter\c!start \letterbar\space %
            let contextstopline=\externalfilterparameter\c!stop   \letterbar\space %
            let strip=\getvalue{\vimtyping@id-\c!strip-\externalfilterparameter\c!strip}" %
-       -c "let escapecomments=\getvalue{\vimtyping@id-\c!escape-\externalfilterparameter\c!escape}" %
+       -c "let escapechar='°'" %
        -c "let highlight=[\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}
 
-\setvalue{\vimtyping@id-\c!escape-\v!off}{0}
-\setvalue{\vimtyping@id-\c!escape-\v!on}{1}
-
-
 % 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}
 
-\setvalue{\vimtyping@id-\c!escape-\v!no}{0}
-\setvalue{\vimtyping@id-\c!escape-\v!yes}{1}
-
-
 \setupvimtyping
   [% \c!tab=4,
    % \c!start=1,
@@ -163,7 +155,7 @@
    % \c!style=\tttf,
    % \c!color=,
    \c!strip=\v!off,
-   \c!escape=\v!off,
+   % \c!escapechar=,
    % \c!highlight=,
    % \c!highlightcolor=lightgray,
    \c!filtercommand=\vimtyping@filter_command,
-- 
2.13.0


[-- Attachment #3: Type: text/plain, Size: 492 bytes --]

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

  reply	other threads:[~2017-07-18 22:30 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-07-17 19:13 Gerion Entrup
2017-07-17 21:17 ` Henri Menke
2017-07-18  2:04 ` Aditya Mahajan
2017-07-18 22:30   ` Gerion Entrup [this message]
2017-07-19 19:07     ` Aditya Mahajan
2017-07-19 19:45       ` Gerion Entrup
2017-07-20  0:06         ` Aditya Mahajan
2017-07-19 20:39       ` Henri Menke
2017-07-19 23:56         ` 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=2316403.2IsKS5fdyP@gump \
    --to=gerion.entrup-ctex@flump.de \
    --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).