![]() |
When and how to use Scheme | ![]() |
You may invoke Scheme programs from TeXmacs in different ways, depending on whether you want to customize some aspects of TeXmacs, to extend the editor with new functionality, to make your markup more dynamic, and so on. In this section, we list the major ways to invoke Scheme routines.
In order to customize the basic aspects of TeXmacs, you may provide one or both of the initialization files
~/.TeXmacs/progs/my-init-texmacs.scm
~/.TeXmacs/progs/my-init-buffer.scm
The file my-init-texmacs.scm is loaded when booting TeXmacs and my-init-buffer.scm is executed each time a view of a document is created, after the system file $TEXMACS_PATH/progs/init-buffer.scm. Both files can be opened with Developer\(\rightarrow\)Open my-init-texmacs.scm resp. Developer\(\rightarrow\)Open my-init-buffer.scm (the Developer menu is shown when the developer tool is enabled in Tools).
Usually, the file my-init-texmacs.scm contains personal keyboard bindings and menus. For instance, when putting the following piece of code in this file, you define the keyboard shortcuts ⇧TH. and ⇧PROP. for starting a new theorem resp. proposition (and similar shortcuts for definitions and lemmas):
(kbd-map
("D e f ." (make 'definition))
("L e m ." (make 'lemma))
("P r o p ." (make 'proposition))
("T h ." (make 'theorem))) |
Similarly, the following commands extend the standard Insert menu with a special section for the insertion of greetings:
(import-from (generic insert-menu))
(menu-bind insert-menu
(former)
---
(-> "Opening"
("Dear Sir" (insert "Dear Sir,"))
("Dear Madam" (insert "Dear Madam,")))
(-> "Closing"
("Yours sincerely" (insert "Yours sincerely,"))
("Greetings" (insert "Greetings,")))) |
The import-from line is needed because the standard menu is defined lazily: at boot time, insert-menu is only a stub, and the module (generic insert-menu), loaded later when the editor is idle, would define it again and replace your version. Loading the module first makes (former) refer to the standard menu, and your definition comes after it.
The customization of the keyboard and menus is described in more detail in the chapter about the TeXmacs extensions of Scheme. Notice also that, because of the lazy loading mechanism, you can not always assume that the standard key-bindings and menus are loaded before my-init-texmacs.scm. This implies that some care is needed in the case of redefinitions.
The file my-init-buffer.scm can for instance be used in order to automatically select a certain style when starting a new document:
(if (not (buffer-has-name? (current-buffer)))
(begin
(init-style "article")
(buffer-pretend-saved (current-buffer)))) |
Notice that the “no name” check is important: when omitted, the styles of existing documents would also be changed to article. The function buffer-pretend-saved is used in order to avoid TeXmacs to complain about unsaved documents when leaving TeXmacs without changing the document.
Another typical use of my-init-buffer.scm is when you mainly want to use TeXmacs as a front-end to another system. For instance, the following code will force TeXmacs to automatically launch a Maxima session for every newly opened document:
(if (not (buffer-has-name? (current-buffer)))
(make-session "maxima" (url->string (current-buffer)))) |
Using (url->string (current-buffer)) as the second argument of make-session ensures that a different session will be opened for every new buffer. If you want all buffers to share a common instance of Maxima, then you should use "default" instead, for the second argument.
The above technique of Scheme initialization files is sufficient for personal customizations of TeXmacs, but not very convenient if you want to share extensions with other users. A more portable way to extend the editor is therefore to regroup your Scheme programs into a plug-in.
The simplest way to write a plug-in name with some additional Scheme functionality is to create two directories and a file
~/.TeXmacs/plugins/name
~/.TeXmacs/plugins/name/progs
~/.TeXmacs/plugins/name/progs/init-name.scm
Furthermore, the file init-name.scm should a piece of configuration code of the form
(plugin-configure name
(:require #t)) |
Any other Scheme code present in init-name.scm will then be executed when the plug-in is booted, that is, shortly after TeXmacs is started up. By using the additional (:prioritary #t) option, you may force the plug-in to be loaded earlier during the boot procedure.
Of course, the plug-in mechanism is more interesting when the plug-in contains more than a few customization routines. In general, a plug-in may also contain additional style files or packages, scripts for launching extern binaries, additional icons and internationalization files, and so on. Furthermore, Scheme extensions are usually regrouped into Scheme modules in the directory
~/.TeXmacs/plugins/name/progs
The initialization file init-name.scm should then be kept as short as possible so as to save boot time: it usually only contains lazy declarations which allow TeXmacs to load the appropriate modules only when needed.
For more information about how to write plug-ins, we refer to the corresponding chapter.
In order to rapidly test the effect of Scheme commands, it is convenient to execute them directly from within the editor. TeXmacs provides two mechanisms for doing this: directly type the command on the footer using the ⌘⌃X shortcut, or start a Scheme session using Insert\(\rightarrow\)Session\(\rightarrow\)Scheme.
The first mechanism is useful when you do not want to alter the document or when the current cursor position is important for the command you wish to execute. For instance, the command (inside? 'theorem) to test whether the cursor is inside a theorem usually makes no sense when you are inside a session.
Scheme sessions are useful when the results of the Scheme commands do not fit on the footer, or when you want to keep your session inside a document for later use. Some typical commands you might want to use inside a Scheme session are as follows (try positioning your cursor inside the session and execute them):
(define (square x) (* x x))
Scheme]
(square 1111111)
Scheme]
(kbd-map ("h i ." (insert "Hi there!")))
Scheme]
;; now try typing "hi."
Scheme]
TeXmacs also provides several command-line options for the execution of Scheme commands. This is useful when you want to use TeXmacs as a batch processor. The Scheme-related options are the following:
Executes the scheme command cmd when booting has completed. For instance,
texmacs -x "(display \"Hi there\\n\")" |
causes TeXmacs to print “Hi there” when starting up. Notice that the -x option may be used several times.
Without a window (-headless), TeXmacs quits by itself once the commands of -x and -c were executed and the files of the command line were loaded: what these commands delay (with delayed, a server, a client which connects later) does not run. The option -X keeps TeXmacs running, until a command such as (quit-TeXmacs) quits it. For instance,
texmacs -headless -X -x "(delayed (:pause 5000) (display \"done\\n\") (quit-TeXmacs))" |
prints “done” after five seconds, and quits.
This option causes TeXmacs to quit. It is usually used after a -x option. For instance,
texmacs text.tm -x "(print)" -q |
will cause TeXmacs to load the file text.tm, to print it, and quit.
This options may be used to convert the input file in into the output file out. The suffixes of in and out determine their file formats. It runs the commands load-buffer and export-buffer, and is usually followed by -q.
Runs TeXmacs without opening a window (also -H), for instance for conversions with -c or commands with -x.
Uses file instead of the standard initialization file of Scheme (also -initialize).
Uses file instead of the system file $TEXMACS_PATH/progs/init-buffer.scm, which is executed for each new view (also -initialize-buffer).
Typesets the documents of the directory dir into dir-check, compares them with the reference results in dir-ref (made with -reference-suite dir) and quits.
TeXmacs provides two major tags for invoking Scheme scripts from within the markup:
This tag works like a hyperlink with body text, but such that the Scheme command script is invoked when clicking on the text. For instance, when clicking here, you will launch an xterm.
This tag is used in order to implement macros whose body is written in Scheme rather than the TeXmacs macro language. The first argument fun is a scheme function with n arguments. During the typesetting phase, TeXmacs passes the arguments arg-1 until arg-n to fun, and the result will be typeset. For instance, the code
<extern|(lambda (x) ‘(concat "Hallo " ,x))|Piet> |
produces the output “Hallo Piet”. Notice that the argument “Piet” remains editable.
It should be noticed that the direct invocation of Scheme scripts from within documents carries as risk: an evil person might send you a document with a script which attempts to erase your hard disk (for instance). For this reason, TeXmacs implements a way to test whether scripts can be considered secure or not. For instance, when clicking here (so as to launch an xterm), the editor will prompt you by default in order to confirm whether you wish to execute this script. The desired level of security can be specified with the Security field in the Other tab of the preferences dialog (Edit\(\rightarrow\)Preferences). When writing your own Scheme extensions to TeXmacs, it is also possible to define routines as being secure.