Paste from LaTeX via keyboard shortcuts

As I’ve been pasting from LaTeX quite often recently, I wonder if there a way to avoid the tedious Edit –> Paste from –> LaTeX cycle by using a keyboard shortcut, say M-F7 ( ⌘-F7 on macOS).

Following @pireddag’s advice given in http://forum.texmacs.cn/t/inserting-footnotes-via-keyboard-shortcuts/1025/2, I ran ack "Paste from" on .../TeXmacs/progs/* to find it mentioned in three different files:

texmacs/keyboard/prefix-kbd.scm
    335:  ("pastefrom" "" "Paste from (1, 2, 3, *:other)")
texmacs/menus/edit-menu.scm
    90:   (-> "Paste from"
utils/edit/selections.scm
    29:   (:argument from "Paste from")

Having had a look at these files, I found no code that could give me a clue how Paste from –> LaTeX is actually called. Running ack "clipboard-paste-import-menu", which is mentioned in ll. 32 and 91 of texmacs/menus/edit-menu.scm, on .../TeXmacs/* yielded

texmacs/menus/edit-menu.scm
32:(tm-define (clipboard-paste-import-menu)
91:          (link clipboard-paste-import-menu)

and brought me back to where I began.

So, does anyone have an idea where how Paste from –> LaTeX is actually called. Any help appreciated.

Cheers
Tilda

I think it is

(clipboard-paste-import "latex" "primary")

I got to it via “some guessing”.

Starting from your results: the one that seemed to me the most likely candidate was

texmacs/menus/edit-menu.scm
    90:   (-> "Paste from"

because it is as it appears in the menu. Then I got to clipboard-paste-import-menu, and I looked for that. That brought me to line 32 of texmacs/menus/edit-menu.scm

(tm-define (clipboard-paste-import-menu)
(clipboard-extern-menu converters-to-special clipboard-paste-import))

so I looked in the same file for clipboard-extern-menu, which I guessed is a way to generate the menu. I found that in line 22 of the same file as

tm-menu (clipboard-extern-menu cvs fun)
   (with l (cvs "texmacs-snippet" "-snippet" #t)
     (for (fm l)
       (with name (format-get-name fm)
         ((eval name) (fun fm "primary"))))))

and after some more guessing (which included trying the commands in a document using the Evaluate Scheme expression menu command—under the Debug menu) I thought that fm is the format from which we are pasting, a list of which gets generated by (cvs "texmacs-snippet" "-snippet" #t).

cvs is an argument of clipboard-extern-menu, and looking at the function call it is converters-to-special, so in a Scheme session I executed (hoping that converters-to-special be defined globally)

(converters-to-special "texmacs-snippet" "-snippet" #t)

which returned

("tmbib" "cpp" "html" "java" "julia" "latex" "python" "scala" "scheme" "code" "texmacs" "stm" "verbatim" "tmml")

Further, I got the function fun of the function call (fun fm "primary") generated by clipboard-extern-menu again from its call: it is clipboard-paste-import (see above).

Putting things together, the pasting command might be

(clipboard-paste-import "latex" "primary")

and testing it, it seems ok.

There were one or two times I went along a wrong road and I had to turn back, and it took a while (but not very long) to figure out that I had to run the function (converters-to-special "texmacs-snippet" "-snippet" #t).

Sometimes it is straightforward though :slight_smile:, sometimes a bit involved when the code is a bit synthetic.

2 Likes

texmacs/menus/edit-menu.scm is the right place. There the relevant submenu is constructed by the function

(tm-define (clipboard-paste-import-menu)
  (clipboard-extern-menu converters-to-special clipboard-paste-import))

which uses the utility function clipboard-extern-menu to create the menu. This is defined as

(tm-menu (clipboard-extern-menu cvs fun)
  (with l (cvs "texmacs-snippet" "-snippet" #t)
    (for (fm l)
      (with name (format-get-name fm)
        ((eval name) (fun fm "primary"))))))

this strange definition is the way TeXmacs scheme defines menus. Is a macro which returns a menu. In our case it calls (converters-from-special "texmacs-snippet" "-snippet" #t) to get a list of converters. You can try it in a scheme session and get as result

("tmbib" "html" "latex" "code" "texmacs" "stm" "verbatim" "tmml")

This will be the value of l, then the macro makes a loop across all these values and calls (format-get-name fm) to get a printable name for the format. At this point it has all the information and creates a list which looks like

( 
 ("BibTeX" (clipboard-paste-import "tmbib" "primary"))
 ("Html" (clipboard-paste-import "html" "primary"))
 ("LaTeX" (clipboard-paste-import "latex" "primary"))
 ("Source code" (clipboard-paste-import "code" "primary"))
...
) 

which is a specification of the menu you see when you choose “Edit -> Paste from -> …”. So what you are looking for is the command (clipboard-paste-import "latex" "primary") which paste the content of the primary clipboard into the document from a “latex” document.

2 Likes

I got confused there :slight_smile:

Although I think that at least sometimes the trial-and-error method of following what a function does is useful.

By the way, perhaps I have to add in the forum post, and in the blog post at https://texmacs.github.io/notes/docs/menu-shortcuts.html, that sometimes one needs to work a bit to figure out the code.

1 Like

Thanks dear @pireddag and @mgubi for the fast replies, the solution to the problem and the in-depth explanations on how scheme defines menu items.

So, the command (clipboard-paste-import "latex" "primary") does indeed solve the issue. :relaxed:

btw, how did you get the code highlighting in your answers?

Cheers
Tilda

The forum does that automatically, it does not seem to be set up for Scheme, and I did not (maybe: I did not yet :slight_smile: ) check how to set it up.

Maybe this update to my editing shortcuts in my-init-texmacs.scm is useful to others:

(delayed
(lazy-keyboard-force)
(kbd-map
    ("M-F6" (make 'footnote)) 
    ("M-F7" (clipboard-paste-import "latex" "primary"))
    ("A-f"  (make 'footnote))    ;; alt-f from trusty old wp5.x
    ("A-c"  (make 'cite))    
    ("A-d"  (make 'cite-detail)) ;; overrides ∂
    ("A-h"  (make 'hlink))       ;; overrides ordfeminine    
;;  ("C-h"  (make 'hlink))       ;; overrides  ??
    ("C-p"  (make 'problem))
    ("C-s"  (make 'solution))    ;; overrides "strong"  
    ("C-t"  (make 'theorem))     ;; overrides ??
;;  ("C-q"  (make 'quote-env))   ;; overrides unicode compose
    ("C-y"  (make 'quote-env))   
    ("C-z"  (make 'quotation))   
  )
)

Cheers
Tilda

4 Likes

If you put the code in a code block, i.e. between three backquotes at the start and three at the end, each group of three in a separate line.

2 Likes