aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEli Zaretskii2020-04-24 18:09:20 +0300
committerEli Zaretskii2020-04-24 18:09:20 +0300
commit369761b36db12c65296a39bb874bb181da466009 (patch)
treeaf3ac9de0e3633e6af07358bde0b1a7de7e5261c /src
parenta92ca1f177547837516bb6aba959ffeb23c2519c (diff)
downloademacs-369761b36db12c65296a39bb874bb181da466009.tar.gz
emacs-369761b36db12c65296a39bb874bb181da466009.zip
; * src/xdisp.c: Improve the introductory commentary.
Diffstat (limited to 'src')
-rw-r--r--src/xdisp.c271
1 files changed, 176 insertions, 95 deletions
diff --git a/src/xdisp.c b/src/xdisp.c
index a4de2698ca0..19f4f326186 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -30,8 +30,9 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
30 Updating the display is triggered by the Lisp interpreter when it 30 Updating the display is triggered by the Lisp interpreter when it
31 decides it's time to do it. This is done either automatically for 31 decides it's time to do it. This is done either automatically for
32 you as part of the interpreter's command loop or as the result of 32 you as part of the interpreter's command loop or as the result of
33 calling Lisp functions like `sit-for'. The C function `redisplay' 33 calling Lisp functions like `sit-for'. The C function
34 in xdisp.c is the only entry into the inner redisplay code. 34 `redisplay_internal' in xdisp.c is the only entry into the inner
35 redisplay code.
35 36
36 The following diagram shows how redisplay code is invoked. As you 37 The following diagram shows how redisplay code is invoked. As you
37 can see, Lisp calls redisplay and vice versa. 38 can see, Lisp calls redisplay and vice versa.
@@ -89,7 +90,15 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
89 second glyph matrix is constructed, the so called `desired glyph 90 second glyph matrix is constructed, the so called `desired glyph
90 matrix' or short `desired matrix'. Current and desired matrix are 91 matrix' or short `desired matrix'. Current and desired matrix are
91 then compared to find a cheap way to update the display, e.g. by 92 then compared to find a cheap way to update the display, e.g. by
92 reusing part of the display by scrolling lines. 93 reusing part of the display by scrolling lines. The actual update
94 of the display of each window by comparing the desired and the
95 current matrix is done by `update_window', which calls functions
96 which draw to the glass (those functions are specific to the type
97 of the window's frame: X, w32, NS, etc.).
98
99 Once the display of a window on the glass has been updated, its
100 desired matrix is used to update the corresponding rows of the
101 current matrix, and then the desired matrix is discarded.
93 102
94 You will find a lot of redisplay optimizations when you start 103 You will find a lot of redisplay optimizations when you start
95 looking at the innards of redisplay. The overall goal of all these 104 looking at the innards of redisplay. The overall goal of all these
@@ -119,13 +128,13 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
119 128
120 . try_window 129 . try_window
121 130
122 This function performs the full redisplay of a single window 131 This function performs the full, unoptimized, redisplay of a
123 assuming that its fonts were not changed and that the cursor 132 single window assuming that its fonts were not changed and that
124 will not end up in the scroll margins. (Loading fonts requires 133 the cursor will not end up in the scroll margins. (Loading
125 re-adjustment of dimensions of glyph matrices, which makes this 134 fonts requires re-adjustment of dimensions of glyph matrices,
126 method impossible to use.) 135 which makes this method impossible to use.)
127 136
128 These optimizations are tried in sequence (some can be skipped if 137 The optimizations are tried in sequence (some can be skipped if
129 it is known that they are not applicable). If none of the 138 it is known that they are not applicable). If none of the
130 optimizations were successful, redisplay calls redisplay_windows, 139 optimizations were successful, redisplay calls redisplay_windows,
131 which performs a full redisplay of all windows. 140 which performs a full redisplay of all windows.
@@ -145,38 +154,62 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
145 154
146 Desired matrices. 155 Desired matrices.
147 156
148 Desired matrices are always built per Emacs window. The function 157 Desired matrices are always built per Emacs window. It is
149 `display_line' is the central function to look at if you are 158 important to know that a desired matrix is in general "sparse": it
150 interested. It constructs one row in a desired matrix given an 159 only has some of the glyph rows "enabled". This is because
160 redisplay tries to optimize its work, and thus only generates
161 glyphs for rows that need to be updated on the screen. Rows that
162 don't need to be updated are left "disabled", and their contents
163 should be ignored.
164
165 The function `display_line' is the central function to look at if
166 you are interested in how the rows of the desired matrix are
167 produced. It constructs one row in a desired matrix given an
151 iterator structure containing both a buffer position and a 168 iterator structure containing both a buffer position and a
152 description of the environment in which the text is to be 169 description of the environment in which the text is to be
153 displayed. But this is too early, read on. 170 displayed. But this is too early, read on.
154 171
172 Glyph rows.
173
174 A glyph row is an array of `struct glyph', where each glyph element
175 describes a "display element" to be shown on the screen. More
176 accurately, a glyph row can have up to 3 different arrays of
177 glyphs: one each for every display margins, and one for the "text
178 area", where buffer text is displayed. The text-area glyph array
179 is always present, whereas the arrays for the marginal areas are
180 present (non-empty) only if the corresponding display margin is
181 shown in the window. If the glyph array for a marginal area is not
182 present its beginning and end coincide, i.e. such arrays are
183 actually empty (they contain no glyphs). Frame glyph matrics, used
184 on text-mode terminals (see below) never have marginal areas, they
185 treat the entire frame-wide row of glyphs as a single large "text
186 area".
187
155 Iteration over buffer and strings. 188 Iteration over buffer and strings.
156 189
157 Characters and pixmaps displayed for a range of buffer text depend 190 Characters and pixmaps displayed for a range of buffer text depend
158 on various settings of buffers and windows, on overlays and text 191 on various settings of buffers and windows, on overlays and text
159 properties, on display tables, on selective display. The good news 192 properties, on display tables, on selective display. The good news
160 is that all this hairy stuff is hidden behind a small set of 193 is that all this hairy stuff is hidden behind a small set of
161 interface functions taking an iterator structure (struct it) 194 interface functions taking an iterator structure (`struct it')
162 argument. 195 argument.
163 196
164 Iteration over things to be displayed is then simple. It is 197 Iteration over things to be displayed is then simple. It is
165 started by initializing an iterator with a call to init_iterator, 198 started by initializing an iterator with a call to `init_iterator',
166 passing it the buffer position where to start iteration. For 199 passing it the buffer position where to start iteration. For
167 iteration over strings, pass -1 as the position to init_iterator, 200 iteration over strings, pass -1 as the position to `init_iterator',
168 and call reseat_to_string when the string is ready, to initialize 201 and call `reseat_to_string' when the string is ready, to initialize
169 the iterator for that string. Thereafter, calls to 202 the iterator for that string. Thereafter, calls to
170 get_next_display_element fill the iterator structure with relevant 203 `get_next_display_element' fill the iterator structure with
171 information about the next thing to display. Calls to 204 relevant information about the next thing to display. Calls to
172 set_iterator_to_next move the iterator to the next thing. 205 `set_iterator_to_next' move the iterator to the next thing.
173 206
174 Besides this, an iterator also contains information about the 207 Besides this, an iterator also contains information about the
175 display environment in which glyphs for display elements are to be 208 display environment in which glyphs for display elements are to be
176 produced. It has fields for the width and height of the display, 209 produced. It has fields for the width and height of the display,
177 the information whether long lines are truncated or continued, a 210 the information whether long lines are truncated or continued, a
178 current X and Y position, and lots of other stuff you can better 211 current X and Y position, the face currently in effect, and lots of
179 see in dispextern.h. 212 other stuff you can better see in dispextern.h.
180 213
181 The "stop position". 214 The "stop position".
182 215
@@ -184,57 +217,62 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
184 infrequently. These include the face of the characters, whether 217 infrequently. These include the face of the characters, whether
185 text is invisible, the object (buffer or display or overlay string) 218 text is invisible, the object (buffer or display or overlay string)
186 being iterated, character composition info, etc. For any given 219 being iterated, character composition info, etc. For any given
187 buffer or string position, the sources of information that 220 buffer or string position, the sources of information that affects
188 affects the display can be determined by calling the appropriate 221 the display can be determined by calling the appropriate
189 primitives, such as Fnext_single_property_change, but both these 222 primitives, such as `Fnext_single_property_change', but both these
190 calls and the processing of their return values is relatively 223 calls and the processing of their return values is relatively
191 expensive. To optimize redisplay, the display engine checks these 224 expensive. To optimize redisplay, the display engine checks these
192 sources of display information only when needed. To that end, it 225 sources of display information only when needed, not for every
193 always maintains the position of the next place where it must stop 226 character. To that end, it always maintains the position of the
194 and re-examine all those potential sources. This is called "stop 227 next place where it must stop and re-examine all those potential
195 position" and is stored in the stop_charpos field of the iterator. 228 sources. This is called "the stop position" and is stored in the
196 The stop position is updated by compute_stop_pos, which is called 229 `stop_charpos' field of the iterator. The stop position is updated
197 whenever the iteration reaches the current stop position and 230 by `compute_stop_pos', which is called whenever the iteration
198 processes it. Processing a stop position is done by handle_stop, 231 reaches the current stop position and processes it. Processing a
199 which invokes a series of handlers, one each for every potential 232 stop position is done by `handle_stop', which invokes a series of
200 source of display-related information; see the it_props array for 233 handlers, one each for every potential source of display-related
201 those handlers. For example, one handler is handle_face_prop, 234 information; see the `it_props' array for those handlers. For
202 which detects changes in face properties, and supplies the face ID 235 example, one handler is `handle_face_prop', which detects changes
203 that the iterator will use for all the glyphs it generates up to 236 in face properties, and supplies the face ID that the iterator will
204 the next stop position; this face ID is the result of realizing the 237 use for all the glyphs it generates up to the next stop position;
205 face specified by the relevant text properties at this position. 238 this face ID is the result of "realizing" the face specified by the
206 Each handler called by handle_stop processes the sources of display 239 relevant text properties at this position (see xfaces.c). Each
240 handler called by `handle_stop' processes the sources of display
207 information for which it is "responsible", and returns a value 241 information for which it is "responsible", and returns a value
208 which tells handle_stop what to do next. 242 which tells `handle_stop' what to do next.
243
244 Once `handle_stop' returns, the information it stores in the
245 iterator fields will not be refreshed until the iteration reaches
246 the next stop position, which is computed by `compute_stop_pos'
247 called at the end of `handle_stop'. `compute_stop_pos' examines
248 the buffer's or string's interval tree to determine where the text
249 properties change, finds the next position where overlays and
250 character composition can change, and stores in `stop_charpos' the
251 closest position where any of these factors should be reconsidered.
209 252
210 Once handle_stop returns, the information it stores in the iterator 253 Handling of the stop position is done as part of the code in
211 fields will not be refreshed until the iteration reaches the next 254 `get_next_display_element'.
212 stop position, which is computed by compute_stop_pos called at the
213 end of handle_stop. compute_stop_pos examines the buffer's or
214 string's interval tree to determine where the text properties
215 change, finds the next position where overlays and character
216 composition can change, and stores in stop_charpos the closest
217 position where any of these factors should be reconsidered.
218 255
219 Producing glyphs. 256 Producing glyphs.
220 257
221 Glyphs in a desired matrix are normally constructed in a loop 258 Glyphs in a desired matrix are normally constructed in a loop
222 calling get_next_display_element and then PRODUCE_GLYPHS. The call 259 calling `get_next_display_element' and then `PRODUCE_GLYPHS'. The
223 to PRODUCE_GLYPHS will fill the iterator structure with pixel 260 call to `PRODUCE_GLYPHS' will fill the iterator structure with
224 information about the element being displayed and at the same time 261 pixel information about the element being displayed and at the same
225 produce glyphs for it. If the display element fits on the line 262 time will produce glyphs for it. If the display element fits on
226 being displayed, set_iterator_to_next is called next, otherwise the 263 the line being displayed, `set_iterator_to_next' is called next,
227 glyphs produced are discarded. The function display_line is the 264 otherwise the glyphs produced are discarded, and `display_line'
228 workhorse of filling glyph rows in the desired matrix with glyphs. 265 marks this glyph row as a "continued line". The function
229 In addition to producing glyphs, it also handles line truncation 266 `display_line' is the workhorse of filling glyph rows in the
230 and continuation, word wrap, and cursor positioning (for the 267 desired matrix with glyphs. In addition to producing glyphs, it
231 latter, see also set_cursor_from_row). 268 also handles line truncation and continuation, word wrap, and
269 cursor positioning (for the latter, see `set_cursor_from_row').
232 270
233 Frame matrices. 271 Frame matrices.
234 272
235 That just couldn't be all, could it? What about terminal types not 273 That just couldn't be all, could it? What about terminal types not
236 supporting operations on sub-windows of the screen (a.k.a. "TTY" or 274 supporting operations on sub-windows of the screen (a.k.a. "TTY" or
237 "text-mode terminal")? To update the display on such a terminal, 275 "text-mode terminals")? To update the display on such a terminal,
238 window-based glyph matrices are not well suited. To be able to 276 window-based glyph matrices are not well suited. To be able to
239 reuse part of the display (scrolling lines up and down), we must 277 reuse part of the display (scrolling lines up and down), we must
240 instead have a view of the whole screen. This is what `frame 278 instead have a view of the whole screen. This is what `frame
@@ -252,19 +290,62 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
252 using the frame matrices, which allows frame-global optimization of 290 using the frame matrices, which allows frame-global optimization of
253 what is actually written to the glass. 291 what is actually written to the glass.
254 292
255 To be honest, there is a little bit more done, but not much more. 293 Frame matrices don't have marginal areas, only a text area. That
256 If you plan to extend that code, take a look at dispnew.c. The 294 is, the entire row of glyphs that spans the width of a text-mode
257 function build_frame_matrix is a good starting point. 295 frame is treated as a single large "text area" for the purposes of
296 manipulating and updating a frame glyph matrix.
297
298 To be honest, there is a little bit more done for frame matrices,
299 but not much more. If you plan to extend that code, take a look at
300 dispnew.c. The function build_frame_matrix is a good starting
301 point.
302
303 Simulating display.
304
305 Some of Emacs commands and functions need to take display layout
306 into consideration. For example, C-n moves to the next screen
307 line, but to implement that, Emacs needs to find the buffer
308 position which is directly below the cursor position on display.
309 This is not trivial when buffer display includes variable-size
310 elements such as different fonts, tall images, etc.
311
312 To solve this problem, the display engine implements several
313 functions that can move through buffer text in the same manner as
314 `display_line' and `display_string' do, but without producing any
315 glyphs for the glyph matrices. The workhorse of this is
316 `move_it_in_display_line_to'. Its code and logic are very similar
317 to `display_line', but it differs in two important aspects: it
318 doesn't produce glyphs for any glyph matrix, and it returns a
319 status telling the caller how it ended the iteration: whether it
320 reached the required position, hit the end of line, arrived at the
321 window edge without exhausting the buffer's line, etc. Since the
322 glyphs are not produced, the layout information available to the
323 callers of this function is what is recorded in `struct it' by the
324 iteration process.
325
326 Several higher-level functions call `move_it_in_display_line_to' to
327 perform more complex tasks: `move_it_by_lines' can move N lines up
328 or down from a given buffer position and `move_it_to' can move to a
329 given buffer position or to a given X or Y pixel coordinate.
330
331 These functions are called by the display engine itself as well,
332 when it needs to make layout decisions before producing the glyphs.
333 For example, one of the first things to decide when redisplaying a
334 window is where to put the `window-start' position; if the window
335 is to be recentered (the default), Emacs makes that decision by
336 starting from the position of point, then moving up the number of
337 lines corresponding to half the window height using
338 `move_it_by_lines'.
258 339
259 Bidirectional display. 340 Bidirectional display.
260 341
261 Bidirectional display adds quite some hair to this already complex 342 Bidirectional display adds quite some hair to this already complex
262 design. The good news are that a large portion of that hairy stuff 343 design. The good news are that a large portion of that hairy stuff
263 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a 344 is hidden in bidi.c behind only 3 interfaces. bidi.c implements a
264 reordering engine which is called by set_iterator_to_next and 345 reordering engine which is called by `set_iterator_to_next' and
265 returns the next character to display in the visual order. See 346 returns the next character to display in the visual order. See
266 commentary on bidi.c for more details. As far as redisplay is 347 commentary on bidi.c for more details. As far as redisplay is
267 concerned, the effect of calling bidi_move_to_visually_next, the 348 concerned, the effect of calling `bidi_move_to_visually_next', the
268 main interface of the reordering engine, is that the iterator gets 349 main interface of the reordering engine, is that the iterator gets
269 magically placed on the buffer or string position that is to be 350 magically placed on the buffer or string position that is to be
270 displayed next in the visual order. In other words, a linear 351 displayed next in the visual order. In other words, a linear
@@ -279,27 +360,27 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
279 monotonously changing with vertical positions. Also, accounting 360 monotonously changing with vertical positions. Also, accounting
280 for face changes, overlays, etc. becomes more complex because 361 for face changes, overlays, etc. becomes more complex because
281 non-linear iteration could potentially skip many positions with 362 non-linear iteration could potentially skip many positions with
282 changes, and then cross them again on the way back (see 363 such changes, and then cross them again on the way back (see
283 handle_stop_backwards)... 364 `handle_stop_backwards')...
284 365
285 One other prominent effect of bidirectional display is that some 366 One other prominent effect of bidirectional display is that some
286 paragraphs of text need to be displayed starting at the right 367 paragraphs of text need to be displayed starting at the right
287 margin of the window---the so-called right-to-left, or R2L 368 margin of the window---the so-called right-to-left, or R2L
288 paragraphs. R2L paragraphs are displayed with R2L glyph rows, 369 paragraphs. R2L paragraphs are displayed with R2L glyph rows,
289 which have their reversed_p flag set. The bidi reordering engine 370 which have their `reversed_p' flag set. The bidi reordering engine
290 produces characters in such rows starting from the character which 371 produces characters in such rows starting from the character which
291 should be the rightmost on display. PRODUCE_GLYPHS then reverses 372 should be the rightmost on display. `PRODUCE_GLYPHS' then reverses
292 the order, when it fills up the glyph row whose reversed_p flag is 373 the order, when it fills up the glyph row whose `reversed_p' flag
293 set, by prepending each new glyph to what is already there, instead 374 is set, by prepending each new glyph to what is already there,
294 of appending it. When the glyph row is complete, the function 375 instead of appending it. When the glyph row is complete, the
295 extend_face_to_end_of_line fills the empty space to the left of the 376 function `extend_face_to_end_of_line' fills the empty space to the
296 leftmost character with special glyphs, which will display as, 377 left of the leftmost character with special glyphs, which will
297 well, empty. On text terminals, these special glyphs are simply 378 display as, well, empty. On text terminals, these special glyphs
298 blank characters. On graphics terminals, there's a single stretch 379 are simply blank characters. On graphics terminals, there's a
299 glyph of a suitably computed width. Both the blanks and the 380 single stretch glyph of a suitably computed width. Both the blanks
300 stretch glyph are given the face of the background of the line. 381 and the stretch glyph are given the face of the background of the
301 This way, the terminal-specific back-end can still draw the glyphs 382 line. This way, the terminal-specific back-end can still draw the
302 left to right, even for R2L lines. 383 glyphs left to right, even for R2L lines.
303 384
304 Bidirectional display and character compositions. 385 Bidirectional display and character compositions.
305 386
@@ -310,23 +391,23 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
310 391
311 Emacs display supports this by providing "character compositions", 392 Emacs display supports this by providing "character compositions",
312 most of which is implemented in composite.c. During the buffer 393 most of which is implemented in composite.c. During the buffer
313 scan that delivers characters to PRODUCE_GLYPHS, if the next 394 scan that delivers characters to `PRODUCE_GLYPHS', if the next
314 character to be delivered is a composed character, the iteration 395 character to be delivered is a composed character, the iteration
315 calls composition_reseat_it and next_element_from_composition. If 396 calls `composition_reseat_it' and `next_element_from_composition'.
316 they succeed to compose the character with one or more of the 397 If they succeed to compose the character with one or more of the
317 following characters, the whole sequence of characters that were 398 following characters, the whole sequence of characters that were
318 composed is recorded in the `struct composition_it' object that is 399 composed is recorded in the `struct composition_it' object that is
319 part of the buffer iterator. The composed sequence could produce 400 part of the buffer iterator. The composed sequence could produce
320 one or more font glyphs (called "grapheme clusters") on the screen. 401 one or more font glyphs (called "grapheme clusters") on the screen.
321 Each of these grapheme clusters is then delivered to PRODUCE_GLYPHS 402 Each of these grapheme clusters is then delivered to
322 in the direction corresponding to the current bidi scan direction 403 `PRODUCE_GLYPHS' in the direction corresponding to the current bidi
323 (recorded in the scan_dir member of the `struct bidi_it' object 404 scan direction (recorded in the `scan_dir' member of the `struct
324 that is part of the iterator). In particular, if the bidi iterator 405 bidi_it' object that is part of the iterator). In particular, if
325 currently scans the buffer backwards, the grapheme clusters are 406 the bidi iterator currently scans the buffer backwards, the
326 delivered back to front. This reorders the grapheme clusters as 407 grapheme clusters are delivered back to front. This reorders the
327 appropriate for the current bidi context. Note that this means 408 grapheme clusters as appropriate for the current bidi context.
328 that the grapheme clusters are always stored in the LGSTRING object 409 Note that this means that the grapheme clusters are always stored
329 (see composite.c) in the logical order. 410 in the `LGSTRING' object (see composite.c) in the logical order.
330 411
331 Moving an iterator in bidirectional text 412 Moving an iterator in bidirectional text
332 without producing glyphs. 413 without producing glyphs.
@@ -337,18 +418,18 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
337 As far as the iterator is concerned, the geometry of such rows is 418 As far as the iterator is concerned, the geometry of such rows is
338 still left to right, i.e. the iterator "thinks" the first character 419 still left to right, i.e. the iterator "thinks" the first character
339 is at the leftmost pixel position. The iterator does not know that 420 is at the leftmost pixel position. The iterator does not know that
340 PRODUCE_GLYPHS reverses the order of the glyphs that the iterator 421 `PRODUCE_GLYPHS' reverses the order of the glyphs that the iterator
341 delivers. This is important when functions from the move_it_* 422 delivers. This is important when functions from the `move_it_*'
342 family are used to get to certain screen position or to match 423 family are used to get to certain screen position or to match
343 screen coordinates with buffer coordinates: these functions use the 424 screen coordinates with buffer coordinates: these functions use the
344 iterator geometry, which is left to right even in R2L paragraphs. 425 iterator geometry, which is left to right even in R2L paragraphs.
345 This works well with most callers of move_it_*, because they need 426 This works well with most callers of `move_it_*', because they need
346 to get to a specific column, and columns are still numbered in the 427 to get to a specific column, and columns are still numbered in the
347 reading order, i.e. the rightmost character in a R2L paragraph is 428 reading order, i.e. the rightmost character in a R2L paragraph is
348 still column zero. But some callers do not get well with this; a 429 still column zero. But some callers do not get well with this; a
349 notable example is mouse clicks that need to find the character 430 notable example is mouse clicks that need to find the character
350 that corresponds to certain pixel coordinates. See 431 that corresponds to certain pixel coordinates. See
351 buffer_posn_from_coords in dispnew.c for how this is handled. */ 432 `buffer_posn_from_coords' in dispnew.c for how this is handled. */
352 433
353#include <config.h> 434#include <config.h>
354#include <stdlib.h> 435#include <stdlib.h>