yanking sage doctests in vim

117 views
Skip to first unread message

Marc Mezzarobba

unread,
Mar 8, 2020, 2:57:58 PM3/8/20
to sage-...@googlegroups.com
Just in case it may be useful to somebody else, here is a vim config
snippet for copy-pasting examples and doctests to the sage repl.
(Improvements welcome!)

function! YankSageTest(type, ...)
if a:0
let lines = getline("'<", "'>")
else
let lines = getline("'[", "']")
endif
let pattern = '^\s*\(sage\|\.\.\.\.\): '
call filter(lines, {i, l -> l =~ pattern})
call map(lines, {i, l -> substitute(l, pattern, "", "")})
call setreg(v:register, join(lines, "\n"), "l")
endfunction
nnoremap <localleader>Y :set opfunc=YankSageTest<CR>g@
vnoremap <localleader>Y :<C-U>call YankSageTest(visualmode(), 1)<CR

--
Marc

Sébastien Labbé

unread,
Mar 9, 2020, 10:45:05 AM3/9/20
to sage-devel
Thanks Marc!

Should I understand that it filters out the output? Can you tell how you use it?

In vim, I am able to use "+y to yank some block of sage doctest to the register + (i.e., the paste bin). I tried "+Y with your code, but it does not seem to work.

Since 2 years with some update of the ipython, I am not able to copy paste in tmux a multiline block from one pane to the pane containing the sage command line: it only copies the first line. A workaround is to type %cpaste first in sage which is painful. If you have a fix for that, I am interested.

Sébastien

Marc Mezzarobba

unread,
Mar 9, 2020, 11:08:20 AM3/9/20
to sage-...@googlegroups.com
Sébastien Labbé wrote:
> Should I understand that it filters out the output? Can you tell how
> you use it?

It only works linewise, and is mapped to <localleader>Y, which is
probably something like \Y depending on your setup. You can use it
either by selecting some text in visual mode and typing \Y, or typing \Y
followed by a motion (e.g. \Yap). It will keep the input lines only and
strip the sage: and ....: markers.

--
Marc

Sébastien Labbé

unread,
Mar 9, 2020, 11:20:59 AM3/9/20
to sage-devel
Ok, I see how to call it, I needed to define some <localleader>.

But I am not getting errors, maybe my vim is too old (7.4.1689). I need to update my machine but I am always postponing this to tomorrow.

 │Erreur détectée en traitant function YankSageTest :                                                     
 │ligne    7 :                                                                                            
 │E121: Variable non définie : i                                                                          
 │E116: Arguments invalides pour la fonction filter                                                       
 │ligne    8 :                                                                                            
 │E121: Variable non définie : i                                                                          
 │E116: Arguments invalides pour la fonction map                                                          
 │E488: Caractères surnuméraires                                                                          
 │Appuyez sur ENTRÉE ou tapez une commande pour continuer

Marc Mezzarobba

unread,
Mar 9, 2020, 11:25:18 AM3/9/20
to sage-...@googlegroups.com
Marc Mezzarobba wrote:
> vnoremap <localleader>Y :<C-U>call YankSageTest(visualmode(), 1)<CR

uh, there is a '>' missing at the end

--
Marc

Simon King

unread,
Mar 10, 2020, 4:48:36 PM3/10/20
to sage-devel
Hi Marc,

sorry, I still don't get what *exactly* one needs to do in order to achieve *what*.

In your original mail, you wrote that it is "for copy-pasting examples and doctests to the sage repl".

If I understand correctly, repl is abbreviation for read-eval-print-loop. I guess that' what I call "command line version of Sage". That makes me wonder two things:
- By "copy-pasting doctests to the sage repl", do you mean that you mark the doctest including the expected output, and when you paste it to Sage then the expected output is automatically stripped, which is of course useful because it is Sage's job to compute that output?
- How is vim involved in my interaction with Sage? Do you mean the following: Open some file in vim that contains doctests, mark a doctest in vim, then go to the Sage command linel and paste what you've copied?

Best regards,
Simon

Sébastien Labbé

unread,
Mar 10, 2020, 5:31:32 PM3/10/20
to sage-devel
I get the same errors after adding the missing `>` at the end

Marc Mezzarobba

unread,
Mar 11, 2020, 7:47:16 AM3/11/20
to sage-...@googlegroups.com
Simon King wrote:
> sorry, I still don't get what *exactly* one needs to do in order to
> achieve *what*.

My post was intended mainly for vim users who would be able to figure
out on their own what the code does and find it useful for their
workflow. I didn't realize anyone else might be interested. Sorry about
that.

> If I understand correctly, repl is abbreviation for
> read-eval-print-loop. I guess that' what I call "command line version
> of Sage".

Yes.

> That makes me wonder two things:
> - By "copy-pasting doctests to the sage repl", do you mean that you
> mark the doctest including the expected output, and when you paste it
> to Sage then the expected output is automatically stripped, which is
> of course useful because it is Sage's job to compute that output?

Yes, except it is stripped at copying time.

> - How is vim involved in my interaction with Sage? Do you mean the
> following: Open some file in vim that contains doctests, mark a
> doctest in vim, then go to the Sage command linel and paste what
> you've copied?

Yes, or combine that with some other way of sending the content of your
clipboard (or X selection, or other vim registers) to a sage command
line.

--
Marc

Marc Mezzarobba

unread,
Mar 11, 2020, 7:50:07 AM3/11/20
to sage-...@googlegroups.com
Sébastien Labbé wrote:
> But I am not getting errors, maybe my vim is too old (7.4.1689). I
> need to update my machine but I am always postponing this to tomorro

Indeed, it looks like I'm using features that appeared with vim 8.

--
Marc

Simon King

unread,
Mar 11, 2020, 6:11:57 PM3/11/20
to sage-devel
Hi Marc,
I opened a Sage source file in vim, marked a doctest in visual mode, and hit \Y. In the status line, vim shows the text
        :call YankSageTest(visualmode(), 1)
So, apparently it did something.

But when I then go to Sage command line and hit <shift>-insert (which usually inserts what was previously copied), nothing happens (resp. some text was inserted that I have copied previously). This is with vim version 8.2.343

What am I doing wrong?

Best regards,
Simon

Marc Mezzarobba

unread,
Mar 12, 2020, 7:28:05 AM3/12/20
to sage-...@googlegroups.com
Simon King wrote:
> But when I then go to Sage command line and hit <shift>-insert (which
> usually inserts what was previously copied), nothing happens (resp.
> some text was inserted that I have copied previously). This is with
> vim version 8.2.343
>
> What am I doing wrong?

I don't know. Does y in vim otherwise copy text to the X clipboard? What
happens if you try to paste it in vim itself?

--
Marc

Dima Pasechnik

unread,
Mar 12, 2020, 8:58:19 AM3/12/20
to sage-devel
vim is very configurable, there could be stuff in ~/.vim/, in
~/.vimrc, in system-wide
locations as listed in FILES section of "man vim" output, which can
change how it behaves. YMMV.
> --
> You received this message because you are subscribed to the Google Groups "sage-devel" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sage-devel+...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/sage-devel/r4d6bq%241aac%241%40ciao.gmane.io.

Sébastien Labbé

unread,
Mar 12, 2020, 9:11:16 AM3/12/20
to sage-devel
You may not be using the good register. You may see what is in the current registers by writing:

:registers<ENTER>

To yank in the register +, you do "+y or "+\Y to yank with Marc's code. The register + is the clipboard on my machine (Linux). On OSX, the register + was not the clipboard...

Sébastien

Simon King

unread,
Mar 12, 2020, 9:56:38 AM3/12/20
to sage-...@googlegroups.com
Hi Sébastien, hi Marc,

On 2020-03-12, Sébastien Labbé <sla...@gmail.com> wrote:
> You may not be using the good register. You may see what is in the current
> registers by writing:
>
>:registers<ENTER>
>
> To yank in the register +, you do "+y or "+\Y to yank with Marc's code. The
> register + is the clipboard on my machine (Linux). On OSX, the register +
> was not the clipboard...

By some duckduckgo search, I found
https://stackoverflow.com/questions/3961859/how-to-copy-to-clipboard-in-vim,
from which I learned two things:

1. To access the OS's clipboard, one either needs to compile vim with the
+clipboard flag, or (easier) "sudo apt-get install vim-gnome"
(or perhaps installing vim-gtk will work, too)
2. In Windows, the registers + and * are equivalent. For X11 systems,
though, they differ. For X11 systems, * is the selection, and + is
the cut buffer (like clipboard).

After installing vim-gnome and opening a Sage source file in vim, I
hit V, moved down a few lines (marking a doctest) including the expected
output, then hit "*\Y and went to Sage, hit <shift>-insrt, et voilà: The
expected output and the prompts were removed and the test correctly
evaluated.

Caveat: It was treated as a single block of code, i.e. it was
not evaluated line by line, i.e. the original doctest was not recreated.
For example, the test from the file

sage: M = MatrixSpace(GF(25,'z'),2,3)([1,2,3,4,5,6])
sage: print(M)
[1 2 3]
[4 0 1]
sage: type(M) # optional: meataxe
<type 'sage.matrix.matrix_gfpn_dense.Matrix_gfpn_dense'>

eventually became

sage: M = MatrixSpace(GF(25,'z'),2,3)([1,2,3,4,5,6])
....: print(M)
....: type(M) # optional: meataxe
....:
[1 2 3]
[4 0 1]
<class 'sage.matrix.matrix_gfpn_dense.Matrix_gfpn_dense'>

in Sage command line after pasting.

In spite of the caveat, I think it is a quite useful addition!

Best regards,
Simon


Vincent Delecroix

unread,
Mar 16, 2020, 10:08:51 AM3/16/20
to sage-...@googlegroups.com
Just adopted it!

Thanks Marc

PS: what about adding it somewhere to the developer guide?
Reply all
Reply to author
Forward
0 new messages