GNU TeXmacsUser manualReferenceAboutEnglish
Extending the graphics editor and pitfalls

1.Adding a graphical macro

The simplest way to add a new kind of object is a graphical macro: a macro whose arguments are points (and possibly labels) and whose expansion is graphical markup. The editor creates such objects exactly like curves: the user clicks the successive points, which become the arguments of the macro. Two ingredients are needed.

1.1.The macro itself

The macro must be defined in a style file or package. It can be written in pure markup, as rectangle in

packages/standard/std-graphics.ts

:

<assign|rectangle|<macro|p1|p2|<cline|p1|<point|<look-up|p1|0>|<look-up|p2|1>>|p2|<point|<look-up|p2|0>|<look-up|p1|1>>>>>

or delegate the computation to Scheme through extern, as arrow-with-text in the same file:

<assign|arrow-with-text|<macro|p1|p2|t|<extern|arrow-with-text|p1|p2|<quote-arg|t>>>>

<drd-props|arrow-with-text|arity|3|accessible|2>

The drd-props declaration makes only the text argument accessible, so that the cursor can enter the label but not the points. The package

packages/experimental/graphical-macros.ts

contains more examples which use extern for all their computations.

1.2.Registering the tag with the editor

The editor only offers tags which are listed in gr-tags-user (

graphics/graphics-drd.scm

). This list is extended by the macro define-graphics of

graphics/graphics-markup.scm

, which also defines the Scheme function used by extern and declares it secure:

(tm-define-macro (define-graphics head . l)
  (receive (opts body) (list-break l not-define-option?)
    ‘(begin
       (set! gr-tags-user (cons ',(ca*r head) gr-tags-user))
       (tm-define ,head ,@opts (:secure #t) ,@body))))

The functions receive their arguments as strees; points are (point x y) with string coordinates, and missing arguments must be tolerated, because the object is typeset while it is being created. The module provides helpers for this: tm-point?, point->complex, complex->point and graphics-transform. For instance:

(define-graphics (rectangle P1 P2)
  (let* ((p1 (if (tm-point? P1) P1 '(point "0" "0")))
         (p2 (if (tm-point? P2) P2 p1)))
    ‘(cline ,p1 (point ,(tm-x p2) ,(tm-y p1))
            ,p2 (point ,(tm-x p1) ,(tm-y p2)))))

Once the tag is in gr-tags-user and the current style defines it (style-has?), the Insert menu of graphics mode (graphics-mode-menu) offers it, and selecting it sets the mode to (edit tag). object_create then creates (tag (point x y) (point x y)) and the usual point insertion takes over. User tags receive the attributes of curves (graphics-attributes).

1.3.Optional refinements

1.4.Loading

gr-tags-user is only filled when the module containing the define-graphics forms is loaded.

graphics-markup.scm

is loaded lazily (

init-texmacs.scm

declares arrow-with-text and arrow-with-text* with lazy-define and define-secure-symbols), and the menu entries call (import-from (graphics graphics-markup)). A new module of graphical macros must similarly be loaded before its macros are offered in the menus. Since extern only calls secure functions, the functions of a lazily loaded module should also be declared with define-secure-symbols, as is done for arrow-with-text.

2.Adding a new primitive

A new built-in graphical tag requires changes at all levels. Taking a hypothetical closed curve as an example:

  1. Tree label. Add the label to the enumeration in

    Kernel/Types/tree_label.hpp

    (next to CSPLINE) and declare it in

    Data/Drd/drd_std.cpp

    with returns_graphical () and point_type for the point arguments, and an arity (repeat, fixed) which the editor will use to decide when the object is complete.

  2. Typesetting. Add a case to the dispatch in

    Typeset/Concat/concater.cpp

    and a method in

    concat_graphics.cpp

    . Follow typeset_line: evaluate the points with env->as_point (env->exec (t[i])), keep the paths descend (ip, i) of the points in a cip array, build a curve in graphical coordinates, map it with env->fr, apply adjust_extremities and produce a curve_box. Use BEGIN_MAGNIFY and END_MAGNIFY. If you write a new box type instead, implement graphical_select so that it returns selections whose cp paths point to the point subtrees; otherwise the editor cannot select individual points.

  3. Other C++ code. Some functions enumerate the graphical tags explicitly: is_graphical in

    Edit/Interface/edit_interface.cpp

    , complete in

    Typeset/Env/env_animate.cpp

    (closed curves in animations), and the upgrader in

    Data/Convert/Texmacs/upgradetm.cpp

    .

  4. Tag groups. Add the tag to the appropriate group in

    graphics/graphics-drd.scm

    , for instance graphical-closed-curve-tag. This automatically puts it into gr-tags-all and gr-tags-curves, gives it the curve attributes, and makes object_create treat it as a curve.

  5. Interface. Add entries to graphics-mode-menu and to graphics-insert-icons, and a name in gr-mode->string (

    graphics/graphics-menu.scm

    ).

  6. Documentation. Describe the tag in graphics primitives.

A new editing mode, rather than a new object type, is added by overloading the functions edit_move, edit_left-button, edit_right-button, edit_middle-button, edit_start-drag, edit_drag and edit_end-drag with a :require clause on the mode symbol and the option (:state graphics-state), as

graphics/graphics-single.scm

does for 'hand-edit, and by making graphics-mode-attributes, graphics-finish and gr-mode->string aware of it.

3.Pitfalls

Two families of variables

Changing gr-color does not change the color of anything that is already drawn; it changes the color of the next object. Conversely, setting color around a graphics changes all objects without an explicit color, but not the properties offered for new objects.

Strings for numbers

All coordinates exchanged between C++ and Scheme are strings. The abbreviations f2s and s2f (

graphics/graphics-utils.scm

) are used everywhere for the conversions.

Recomputed props

The variables current-x, current-path, current-obj, … are recomputed only when a function with the option (:state graphics-state) is entered. A helper called from elsewhere (a menu command, a keyboard shortcut) sees the values of the last mouse event, which may refer to an object that has been modified or removed since. Moreover, the state is global: it is not attached to a buffer or a picture, which is why graphics-reset-context must be called when the cursor enters or leaves a picture.

Objects outside the document

During an operation, the objects being edited are not in the document but in the sketch, as detached strees. Code which inspects the document at that moment, or which saves it, does not see them. Conversely, in selecting state the sketch contains live trees, which become invalid if they are removed by other means.

Undo during an operation

The removal of objects at checkout is a real modification of the document. If an operation ends in any other way than sketch-commit or graphics-reset-context, the objects are lost until the user performs an undo. New code which starts an operation must set graphics-undo-enabled and remove-undo-mark? consistently, as sketch-checkout does.

The overlay environment

The graphical object is typeset in the environment of the editor, not of the picture; only the frame is borrowed. Attributes inherited from with tags around the picture are therefore not applied automatically, which is why create-graphical-props puts all attributes explicitly.

Snapping happens first

Scheme only receives snapped coordinates, and the selection of the object under the mouse is done at the snapped position. When a snapping mode makes the mouse jump to a grid point, the object under the original position may not be found. The snapping category text used for labels and groups by can_snap is not in the list graphics-snap-types shown in the menus, so it can only be enabled through all.

Labels

Inside a text-at (more precisely, when inside_graphics (true) is false but inside_graphics (false) is true) the editor behaves as a text editor, except that the release of the left mouse button is still passed to graphics-release-left, which positions the cursor inside the label.

Auto-cropping

When gr-auto-crop is true, the extents and the origin are determined by the contents, and graphics-move-origin and graphics-change-extents do nothing.

Default values

The defaults in attribute-default-table are supposed to coincide with those of initialize_default_env. They currently differ for line-effects (normal in Scheme, none in C++).

Console messages

Messages like Uncaptured graphical move or Uncaptured reset-context come from the default implementations of the dispatch functions and usually indicate a mode for which a handler is missing.

4.Known problems in the code

The following problems were noticed while writing this documentation.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled "GNU Free Documentation License".