GNU TeXmacsUser manualReferenceAboutEnglish
Views and the current view

A view is an editor on a buffer. There may be several views on the same buffer; each has its own cursor, selection, typeset box tree, zoom factor and undo history, but they all share the document tree, so that a change made in one view appears at once in all the others. A view is shown in at most one window, and a window shows exactly one view at a time. A view which is not shown in any window is called passive; passive views are used to work on documents which are not displayed, and to keep the “modified” status of a buffer alive after its last window has been closed.

At any moment one view is current. The current view is the hidden argument of most of the program: it determines the current buffer, the current window, the current editor and the current DRD. Most surprises in this layer come from code which runs with another current view than its author expected; the second half of this page explains when the current view changes and how to change it safely.

1.The class tm_view_rep

class tm_view_rep
(an editor on a buffer)

Declared in

Texmacs/tm_window.hpp

(its constructor is in

Texmacs/Data/new_view.cpp

); tm_view is a plain pointer to it. Its fields are:

tm_buffer buf

The buffer.

editor ed

The editor (a reference counted handle; see the editor).

tm_window win

The window which displays the view, or NULL for a passive view.

int nr

A number which distinguishes the views on the same buffer. It is allocated by new_view_number from the static table view_number_table, indexed by buffer name, above the numbers of the views the buffer already has (a view keeps its number when its buffer is renamed, so that the numbers of the new name may already be taken by it, #560); a rename also raises the table of the new name to these numbers. A number is never given again under the same name, not even after the buffer has been closed and opened again, or after the views which brought it with a rename have been deleted.

The editor points back to its buffer (ed->buf) and, once the view is shown, to the canvas widget of its window (ed->cvw). Views are owned by their buffer (the array tm_buffer_rep::vws); they are not reference counted.

2.View identifiers

Outside

Texmacs/Data/

, and in particular in Scheme, a view is designated by an URL of the form

tmfs://view/nr/encoded-buffer-name

which is computed by abstract_view and decoded by concrete_view in

new_view.cpp

. The buffer name is encoded by the static function encode_url: a relative name becomes here/…, a local absolute name default/… (for instance tmfs://view/1/default/home/joris/a.tm), and another root (tmfs, http, …) is followed by a slash and the rest of the name; decode_url reverses this, with special handling of drive letters on Windows. concrete_view looks up the buffer first and then the view with the given number among the views of that buffer.

Two consequences follow. A view URL becomes invalid as soon as its buffer is closed. And since the identifier contains the buffer name, it changes when the buffer is renamed: rename_buffer removes the old view URLs from the view history before the rename and puts the new ones back afterwards (notify_rename_before, notify_rename_after). Any view URL kept elsewhere, for instance in a Scheme variable, silently becomes invalid after File\(\rightarrow\)Save as.

3.Creating and destroying views

New views are created by get_new_view (url name), which

  1. creates an empty buffer if necessary (create_buffer);

  2. creates a new editor with new_editor (get_server () -> get_server (), buf);

  3. appends the new tm_view_rep to buf->vws and passes the document data to the editor with set_data;

  4. temporarily makes the new view current and executes $TEXMACS_PATH/progs/init-buffer.scm (or the file given with the -b option) and $TEXMACS_HOME_PATH/progs/my-init-buffer.scm; the default

    init-buffer.scm

    sets the default style of unnamed buffers.

A new view is passive and is not in the view history (see below). Other ways to obtain a view are

get_passive_view (name)

An existing view on the buffer which is not attached to a window. It loads the buffer if it does not exist yet (concrete_buffer_insist) and creates a new view if all existing views are shown in windows. This is the view which is attached when a buffer is shown in a window.

get_recent_view (name)

Creates a new view if the buffer has none; otherwise it prefers the current view, then the most recent attached view on the buffer, then the most recent view on the buffer in the history, and finally the first view of the buffer. It is used when an editor is needed for a computation on the buffer, for instance by buffer_export.

get_recent_view (name, same, other, active, passive)

The first view of the history which passes the given filters: on the same buffer, on another buffer, attached to a window, not attached.

Views are destroyed with delete_view, which removes the view from its buffer and from the history, sets ed->buf to NULL and deletes the tm_view_rep; the editor itself is reference counted and dies with its last reference. A window which shows the view first shows another one (a passive view of the same buffer if there is one, else a passive view of another buffer, else a new view of the most recent other buffer, as kill_buffer does; only when there is no other buffer is the view detached and the window left empty), and when the view is the current one, another view becomes current (#559). The window of an embedded editor, which is not in the table of the windows, gets no new view. Views are also deleted when their buffer is removed. They are not deleted when their window is closed: delete_window only detaches them, since a buffer without views would always be reported as unmodified.

4.The view history

The array view_history lists the URLs of the views which have been attached to a window at least once, most recently attached first; notify_set_view (called by attach_view) moves a view to the front and notify_delete_view (called by delete_view) removes it. Detaching a view does not remove it from the history. Renaming a buffer is an exception to the rule: notify_rename_after puts all views of the renamed buffer at the front, including views which have never been attached.

The history is the list returned by get_all_views (view-list) and is used for all “most recent” queries, and also by window_to_view, which finds the view shown in a window by searching the history. Since a view which has never been attached to a window (for instance the passive view created to load a buffer in the background) is normally not in the history, get_all_views does not return all views; use buffer_to_views to enumerate the views of a buffer.

5.Showing a view in a window

A view is shown in a window with attach_view (url win, url view), which

  1. sets vw->win;

  2. installs the editor as the scrollable canvas of the window widget (set_scrollable (wid, vw->ed)) and stores the canvas widget in ed->cvw;

  3. calls ed->resume (), which installs the menus, icon bars and side tools of this editor in the window, makes the cursor accessible and requests a full update;

  4. sets the title and the URL of the window from the buffer;

  5. moves the view to the front of the view history.

detach_view does the converse: it calls ed->suspend () (which interrupts a pending shortcut, clears the footer message and frees the cached renderers), replaces the canvas by an empty glue_widget and resets the title to TeXmacs.

These two routines are rarely called directly. The usual entry points are

window_set_view (win, view, focus)

Detaches the previous view of the window and attaches the new one. It does nothing if the view is already shown there, asserts that the new view is not attached to another window, and makes the new view current if focus is set or if the old view was current.

switch_to_buffer (name)

Shows a passive view on the buffer (loading it if needed) in the current window, gives it the focus and re-applies the zoom factor of the window (switch-to-buffer).

window_set_buffer (win, name)

Shows a passive view on the buffer in the given window, without changing the focus (window-set-buffer).

Creating windows (open_window, clone_window, new_buffer_in_new_window) is described in windows.

6.The current view

The current view is stored in the global pointer the_view of

new_view.cpp

(no other file uses it directly) and manipulated by

set_current_view (url u)

Makes u current. As a side effect, the global the_drd is set to the DRD of the editor and the last_visit time of the buffer is updated. An invalid URL leaves no current view.

get_current_view ()

Returns the current view; asserts that there is one.

get_current_view_safe (), has_current_view ()

Return url_none (), respectively false, if there is no current view.

get_current_editor ()

Returns the editor of the current view.

Everything else that is “current” is derived from the current view:

Note that the current view may be passive. Then there is a current buffer and a current editor, but no current window: current-window returns the empty URL, the frame routines do nothing, and the few which do not check (see the partial server tm_frame_rep) crash.

7.When the current view changes

The current view is changed

switch_to_window (url win) is different: it suspends the editor of the current window, maps the new window and resumes its editor, and sends the GUI keyboard focus to it, but it does not change the current view. The current view is updated only when the toolkit reports the focus change and handle_keyboard_focus runs, that is, during a later round of the event loop. Code which calls switch-to-window and then immediately acts on the “current” buffer still acts on the old one.

8.Working in the context of another view

In C++.
The routines of the server act on the current window. An editor which needs such a routine for its own window (for instance to install its menus or to show a message in its footer) uses the SERVER macro of
Edit/editor.hpp

, which makes the editor current for the duration of the call:

#define SERVER(cmd) {                  \
  url temp= get_current_view_safe (); \
  focus_on_this_editor ();            \
  sv->cmd;                            \
  set_current_view (temp);            \
}

The same pattern (save with get_current_view_safe, change, restore with set_current_view) is used by get_new_view. Note that the restoration is not protected against exceptions.

In Scheme.
The macros with-buffer and with-window of
utils/library/cursor.scm

execute a body in the context of another buffer:

(with-buffer buf
  (insert "Hello"))

with-buffer does nothing special if buf is the current buffer; otherwise it calls buffer-focus (focus_on_buffer) on it, evaluates the body and calls buffer-focus on the old buffer. with-window calls with-buffer on the buffer of the window. Three details matter:

9.Pitfalls

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".