![]() |
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.
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:
The buffer.
The editor (a reference counted handle; see the editor).
The window which displays the view, or NULL for a passive view.
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.
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.
New views are created by get_new_view (url name), which
creates an empty buffer if necessary (create_buffer);
creates a new editor with new_editor (get_server () -> get_server (), buf);
appends the new tm_view_rep to buf->vws and passes the document data to the editor with set_data;
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
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.
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.
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.
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.
A view is shown in a window with attach_view (url win, url view), which
sets vw->win;
installs the editor as the scrollable canvas of the window widget (set_scrollable (wid, vw->ed)) and stores the canvas widget in ed->cvw;
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;
sets the title and the URL of the window from the buffer;
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
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.
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).
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.
The current view is stored in the global pointer the_view of
new_view.cpp
(no other file uses it directly) and manipulated by
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.
Returns the current view; asserts that there is one.
Return url_none (), respectively false, if there is no current view.
Returns the editor of the current view.
Everything else that is “current” is derived from the current view:
get_current_buffer returns the buffer of the current view;
has_current_window, get_current_window and concrete_window () refer to the window of the current view, if it is attached; there is no separately stored current window;
all routines of
build-glue-editor.scm
are exported as get_current_editor()->…, so that every Scheme editing command acts on the current view;
all routines of tm_frame_rep (menus, footer, zoom, dialogs) act on concrete_window ();
the typesetter, the DRD based predicates and the menus use the_drd.
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.
The current view is changed
when a window obtains the keyboard focus: edit_interface_rep::handle_keyboard_focus calls focus_on_this_editor (), which calls focus_on_editor, which searches the view of the editor among the views of all buffers and makes it current;
when a view is shown in a window with focus (window_set_view with focus, switch_to_buffer) or in the window of the current view;
explicitly, by window_focus (url win) (which makes the view of the window current, without touching the GUI focus), focus_on_buffer (url name) (which chooses the most recent view on the buffer, preferably an attached one) or var_focus_on_buffer (which in addition suspends the old editor, resumes the new one and moves the keyboard focus);
when windows are closed: kill_window makes a view in another window current before deleting the window;
temporarily, by the SERVER macro in C++ and by the Scheme macros with-buffer and with-window, described below.
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.
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.
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:
If the buffer does not exist or has no view, buffer-focus fails and the body is not executed; the macro returns #f. This is why, for instance, buffer-copy in
tm-files.scm
calls view-new on the new buffer before using with-buffer.
The previous context is restored by buffer, not by view: if the old buffer had several views, focus_on_buffer may return to a different one. Likewise, with-window focuses on the most recent view of the window's buffer, which is not necessarily the view shown in that window if the buffer is shown in several windows.
The restoration does not use dynamic-wind: if the body raises an error, the current view is left on the other buffer, and the next event is processed in the wrong context.
Do not keep tm_view pointers or view URLs across operations which may close or rename buffers. concrete_view returns NULL for a stale URL, and view_to_editor removes it from the history and returns a nil editor (or fails in ADVANCED_DEVELOPER_MODE); most callers do not check.
get_current_view, get_current_editor, get_current_buffer and the one argument get_recent_view assert that there is a current view, which is not the case very early during startup. Use the _safe variants in code which may run in such situations.
Code which temporarily changes the current view must restore it, also in case of errors.
get_all_views only returns views which have been attached to a window at least once (or belong to a renamed buffer).
Passive views are not retypeset by the event loop (see the interpose handler); their typesetting information may be out of date until it is explicitly requested.