Skip to Content.
Sympa Menu

texmacs-users - Re: [TeXmacs] scheme functions for keyboard input

Subject: mailing-list for TeXmacs Users

List archive

Re: [TeXmacs] scheme functions for keyboard input


Chronological Thread 
  • From: Miguel de Benito Delgado <address@hidden>
  • To: address@hidden
  • Cc: "address@hidden" <address@hidden>
  • Subject: Re: [TeXmacs] scheme functions for keyboard input
  • Date: Sun, 2 Feb 2014 00:43:08 +0100

Hi Scott,

On Thu, Jan 30, 2014 at 11:41 PM, Scott Michael Sullivan <address@hidden> wrote:
I've already figured out how to set static keyboard maps, but I would really like to create dynamic keymaps. For example, suppose I wanted to map the pauli matrices to M-x, M-y, and M-z during a texmacs session, without opening the Guile interpreter (obviously I would also need to set aside a shortcut for mapping, let's call it "C-m"). I want a function where I could type out the x-matrix, select it, type "C-m M-x <enter>", and from now on in this session, typing "M-x" creates the x-matrix. This type of dynamic mapping is only one example of what I have in mind.

Let's try to implement this particular feature.

A first but not very useful approximation (see below for more) is to use :interactive procedures, i.e. asking in popup/footer for parameters, and Texmacs' named clipboards. You can basically define as many clipboards as you want using (clipboard-copy "some-name") and (clipboard-paste "some-name"). What you then need is to create these names dynamically.

(tm-define (clipboard-copy-interactive name)
  (:interactive #t)
  (:argument name)
  (clipboard-copy name))

(tm-define (clipboard-paste-interactive name)
  (:interactive #t)
  (:argument name)
  (clipboard-paste name))

(kbd-map
  ("C-m" (interactive clipboard-copy-interactive))
  ("C-M" (interactive clipboard-paste-interactive)))

Then you select your text, hit C-m and type the name for the buffer. To use it hit C-M and type /select the name of the buffer.

But this obviously sucks.

A better approach (although not yet what you want) is to actually define keyboard maps using :interactive.

(tm-define (set-shortcut-for-selection)
  (with s (tree->string (selection-tree))
    (interactive 
        (lambda (key) 
          (if (string? key)
              (begin
                (display* "Mapping key= " key ", to selection= " s "\n")
                (eval `(kbd-map (,key ,s))))))))

(kbd-map
  ("C-m" (set-shortcut-for-selection)))

(There might be a more elegant way of calling the macro kbd-map than using eval, I don't know and it's late.)

This solution sucks just a little less because you still have to imput the key-combination in string form inside a dialog, then you have to click the accept button, which is slow.

The solution closest to what you want captures raw keystrokes inside set-shortcut-for-selection like this:

(define capturing-key? #f)

(tm-define (keyboard-press key time)
  (:require capturing-key?)
  ;(display* "Got key= " key "\n")
  (eval `(kbd-map (,key ,(tree->string (selection-tree)))))
  (set! capturing-key? #f))

(tm-define (set-shortcut-for-selection)
  (if (selection-active-any?) (set! capturing-key? #t)))

(kbd-map
  ("C-m" (set-shortcut-for-selection)))

I think this does exactly what you want (modulo bugs X-D).

Also, as François suggested you will want to use "contextual overloading" to limit the scope of the keymaps you define to particular situations. For instance you can check whether the cursor is in math mode when the shortcut is being created and if it is enforce that it must only be active in math mode adding (:require in-math?) to the kbd-map.

My goal for now is being able to type mathematics extremely fast, mainly since I have to do a lot of physics assignments for grad school. I currently use pencil/paper for everything, but if I could type math faster than I could write, I would switch over to Texmacs for everything (also I like that sage can add powerful functionality).

I love TeXmacs and type a lot with it, but I still cannot do any actual maths with it. I get constantly distracted with "hey, wouldn't it be cool to implement xyz feature?" ;)
 
Anyways, will I have to write a c++ plugin, or can I do everything in Scheme? I suppose I could redefine the keyboard-press for search-mode. But how do I tell texmacs to enter/leave search-mode?

No, you don't need to write any C++ code. That was only FYI. You also don't need to / can't use search-mode. The stuff above should get you started.

Hth,
--
Miguel de  Benito.

On Thu, Jan 30, 2014 at 7:29 AM, Miguel de Benito Delgado <address@hidden> wrote:
Hi,

On Thu, Jan 30, 2014 at 8:08 AM, Scott Michael Sullivan <address@hidden> wrote:
Is there a way in scheme to read input from the keyboard, while not adding anything to the texmacs buffer? I'm trying to write functions which can redefine keyboard maps on the fly, and I require this sort of functionality.

Keyboard events are captured by c++ code (especifically by QTMWidget if you are using the Qt port, then sent to editor_rep::handle_keypress()) and redirected to the scheme handlers "keyboard-press". You can search in the scheme code for examples of how you may use contextual overloading to handle keypresses only when the editor is in a particular state or "mode" (e.g. while in "search mode" keypresses are handled differently: they produce no output in the editor but instead are interpreted as part of the search string)

However, if you want to set keyboard maps then you may skip the low level stuff and use kbd-map and all the related infrastructure. Just look for the definition of kbd-map and around it you'll find lots of related interesting stuff.

Best,
--
Miguel de  Benito.





Archive powered by MHonArc 2.6.19.

Top of Page