(linenum→info "unix/slp.c:2238")

emacs/22.1/src/dispnew.c

    1: /* Updating of data structures for redisplay.
    2:    Copyright (C) 1985, 1986, 1987, 1988, 1993, 1994, 1995,
    3:                  1997, 1998, 1999, 2000, 2001, 2002, 2003,
    4:                  2004, 2005, 2006, 2007 Free Software Foundation, Inc.
    5: 
    6: This file is part of GNU Emacs.
    7: 
    8: GNU Emacs is free software; you can redistribute it and/or modify
    9: it under the terms of the GNU General Public License as published by
   10: the Free Software Foundation; either version 2, or (at your option)
   11: any later version.
   12: 
   13: GNU Emacs is distributed in the hope that it will be useful,
   14: but WITHOUT ANY WARRANTY; without even the implied warranty of
   15: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   16: GNU General Public License for more details.
   17: 
   18: You should have received a copy of the GNU General Public License
   19: along with GNU Emacs; see the file COPYING.  If not, write to
   20: the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   21: Boston, MA 02110-1301, USA.  */
   22: 
   23: #include <config.h>
   24: #include <signal.h>
   25: #include <stdio.h>
   26: #include <ctype.h>
   27: 
   28: #ifdef HAVE_UNISTD_H
   29: #include <unistd.h>
   30: #endif
   31: 
   32: #include "lisp.h"
   33: #include "termchar.h"
   34: #include "termopts.h"
   35: #include "termhooks.h"
   36: /* cm.h must come after dispextern.h on Windows.  */
   37: #include "dispextern.h"
   38: #include "cm.h"
   39: #include "buffer.h"
   40: #include "charset.h"
   41: #include "keyboard.h"
   42: #include "frame.h"
   43: #include "window.h"
   44: #include "commands.h"
   45: #include "disptab.h"
   46: #include "indent.h"
   47: #include "intervals.h"
   48: #include "blockinput.h"
   49: #include "process.h"
   50: 
   51: /* I don't know why DEC Alpha OSF1 fail to compile this file if we
   52:    include the following file.  */
   53: /* #include "systty.h" */
   54: #include "syssignal.h"
   55: 
   56: #ifdef HAVE_X_WINDOWS
   57: #include "xterm.h"
   58: #endif /* HAVE_X_WINDOWS */
   59: 
   60: #ifdef HAVE_NTGUI
   61: #include "w32term.h"
   62: #endif /* HAVE_NTGUI */
   63: 
   64: #ifdef MAC_OS
   65: #include "macterm.h"
   66: #endif /* MAC_OS */
   67: 
   68: /* Include systime.h after xterm.h to avoid double inclusion of time.h.  */
   69: 
   70: #include "systime.h"
   71: #include <errno.h>
   72: 
   73: /* To get the prototype for `sleep'.  */
   74: 
   75: #ifdef HAVE_UNISTD_H
   76: #include <unistd.h>
   77: #endif
   78: 
   79: /* Get number of chars of output now in the buffer of a stdio stream.
   80:    This ought to be built in in stdio, but it isn't.  Some s- files
   81:    override this because their stdio internals differ.  */
   82: 
   83: #ifdef __GNU_LIBRARY__
   84: 
   85: /* The s- file might have overridden the definition with one that
   86:    works for the system's C library.  But we are using the GNU C
   87:    library, so this is the right definition for every system.  */
   88: 
   89: #ifdef GNU_LIBRARY_PENDING_OUTPUT_COUNT
   90: #define PENDING_OUTPUT_COUNT GNU_LIBRARY_PENDING_OUTPUT_COUNT
   91: #else
   92: #undef  PENDING_OUTPUT_COUNT
   93: #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->__bufp - (FILE)->__buffer)
   94: #endif
   95: #else /* not __GNU_LIBRARY__ */
   96: #if !defined (PENDING_OUTPUT_COUNT) && HAVE_STDIO_EXT_H && HAVE___FPENDING
   97: #include <stdio_ext.h>
   98: #define PENDING_OUTPUT_COUNT(FILE) __fpending (FILE)
   99: #endif
  100: #ifndef PENDING_OUTPUT_COUNT
  101: #define PENDING_OUTPUT_COUNT(FILE) ((FILE)->_ptr - (FILE)->_base)
  102: #endif
  103: #endif /* not __GNU_LIBRARY__ */
  104: 
  105: #if defined(HAVE_TERM_H) && defined (GNU_LINUX) && defined (HAVE_LIBNCURSES)
  106: #include <term.h>               /* for tgetent */
  107: #endif
  108: ^L
  109: /* Structure to pass dimensions around.  Used for character bounding
  110:    boxes, glyph matrix dimensions and alike.  */
  111: 
  112: struct dim
  113: {
  114:   int width;
  115:   int height;
  116: };
  117: 
  118: ^L
  119: /* Function prototypes.  */
  120: 
  121: static struct glyph_matrix *save_current_matrix P_ ((struct frame *));
  122: static void restore_current_matrix P_ ((struct frame *, struct glyph_matrix *));
  123: static int showing_window_margins_p P_ ((struct window *));
  124: static void fake_current_matrices P_ ((Lisp_Object));
  125: static void redraw_overlapping_rows P_ ((struct window *, int));
  126: static void redraw_overlapped_rows P_ ((struct window *, int));
  127: static int count_blanks P_ ((struct glyph *, int));
  128: static int count_match P_ ((struct glyph *, struct glyph *,
  129:                             struct glyph *, struct glyph *));
  130: static unsigned line_draw_cost P_ ((struct glyph_matrix *, int));
  131: static void update_frame_line P_ ((struct frame *, int));
  132: static struct dim allocate_matrices_for_frame_redisplay
  133:      P_ ((Lisp_Object, int, int, int, int *));
  134: static void allocate_matrices_for_window_redisplay P_ ((struct window *));
  135: static int realloc_glyph_pool P_ ((struct glyph_pool *, struct dim));
  136: static void adjust_frame_glyphs P_ ((struct frame *));
  137: struct glyph_matrix *new_glyph_matrix P_ ((struct glyph_pool *));
  138: static void free_glyph_matrix P_ ((struct glyph_matrix *));
  139: static void adjust_glyph_matrix P_ ((struct window *, struct glyph_matrix *,
  140:                                      int, int, struct dim));
  141: static void change_frame_size_1 P_ ((struct frame *, int, int, int, int, int));
  142: static void swap_glyph_pointers P_ ((struct glyph_row *, struct glyph_row *));
  143: #if GLYPH_DEBUG
  144: static int glyph_row_slice_p P_ ((struct glyph_row *, struct glyph_row *));
  145: #endif
  146: static void fill_up_frame_row_with_spaces P_ ((struct glyph_row *, int));
  147: static void build_frame_matrix_from_window_tree P_ ((struct glyph_matrix *,
  148:                                                      struct window *));
  149: static void build_frame_matrix_from_leaf_window P_ ((struct glyph_matrix *,
  150:                                                      struct window *));
  151: static struct glyph_pool *new_glyph_pool P_ ((void));
  152: static void free_glyph_pool P_ ((struct glyph_pool *));
  153: static void adjust_frame_glyphs_initially P_ ((void));
  154: static void adjust_frame_message_buffer P_ ((struct frame *));
  155: static void adjust_decode_mode_spec_buffer P_ ((struct frame *));
  156: static void fill_up_glyph_row_with_spaces P_ ((struct glyph_row *));
  157: static void build_frame_matrix P_ ((struct frame *));
  158: void clear_current_matrices P_ ((struct frame *));
  159: void scroll_glyph_matrix_range P_ ((struct glyph_matrix *, int, int,
  160:                                     int, int));
  161: static void clear_window_matrices P_ ((struct window *, int));
  162: static void fill_up_glyph_row_area_with_spaces P_ ((struct glyph_row *, int));
  163: static int scrolling_window P_ ((struct window *, int));
  164: static int update_window_line P_ ((struct window *, int, int *));
  165: static void update_marginal_area P_ ((struct window *, int, int));
  166: static int update_text_area P_ ((struct window *, int));
  167: static void make_current P_ ((struct glyph_matrix *, struct glyph_matrix *,
  168:                               int));
  169: static void mirror_make_current P_ ((struct window *, int));
  170: void check_window_matrix_pointers P_ ((struct window *));
  171: #if GLYPH_DEBUG
  172: static void check_matrix_pointers P_ ((struct glyph_matrix *,
  173:                                        struct glyph_matrix *));
  174: #endif
  175: static void mirror_line_dance P_ ((struct window *, int, int, int *, char *));
  176: static int update_window_tree P_ ((struct window *, int));
  177: static int update_window P_ ((struct window *, int));
  178: static int update_frame_1 P_ ((struct frame *, int, int));
  179: static void set_window_cursor_after_update P_ ((struct window *));
  180: static int row_equal_p P_ ((struct window *, struct glyph_row *,
  181:                             struct glyph_row *, int));
  182: static void adjust_frame_glyphs_for_window_redisplay P_ ((struct frame *));
  183: static void adjust_frame_glyphs_for_frame_redisplay P_ ((struct frame *));
  184: static void reverse_rows P_ ((struct glyph_matrix *, int, int));
  185: static int margin_glyphs_to_reserve P_ ((struct window *, int, Lisp_Object));
  186: static void sync_window_with_frame_matrix_rows P_ ((struct window *));
  187: struct window *frame_row_to_window P_ ((struct window *, int));
  188: 
  189: ^L
  190: /* Non-zero means don't pause redisplay for pending input.  (This is
  191:    for debugging and for a future implementation of EDT-like
  192:    scrolling.  */
  193: 
  194: int redisplay_dont_pause;
  195: 
  196: /* Define PERIODIC_PREEMPTION_CHECKING to 1, if micro-second timers
  197:    are supported, so we can check for input during redisplay at
  198:    regular intervals.  */
  199: #ifdef EMACS_HAS_USECS
  200: #define PERIODIC_PREEMPTION_CHECKING 1
  201: #else
  202: #define PERIODIC_PREEMPTION_CHECKING 0
  203: #endif
  204: 
  205: #if PERIODIC_PREEMPTION_CHECKING
  206: 
  207: /* If a number (float), check for user input every N seconds.  */
  208: 
  209: Lisp_Object Vredisplay_preemption_period;
  210: 
  211: /* Redisplay preemption timers.  */
  212: 
  213: static EMACS_TIME preemption_period;
  214: static EMACS_TIME preemption_next_check;
  215: 
  216: #endif
  217: 
  218: /* Nonzero upon entry to redisplay means do not assume anything about
  219:    current contents of actual terminal frame; clear and redraw it.  */
  220: 
  221: int frame_garbaged;
  222: 
  223: /* Nonzero means last display completed.  Zero means it was preempted.  */
  224: 
  225: int display_completed;
  226: 
  227: /* Lisp variable visible-bell; enables use of screen-flash instead of
  228:    audible bell.  */
  229: 
  230: int visible_bell;
  231: 
  232: /* Invert the color of the whole frame, at a low level.  */
  233: 
  234: int inverse_video;
  235: 
  236: /* Line speed of the terminal.  */
  237: 
  238: EMACS_INT baud_rate;
  239: 
  240: /* Either nil or a symbol naming the window system under which Emacs
  241:    is running.  */
  242: 
  243: Lisp_Object Vwindow_system;
  244: 
  245: /* Version number of X windows: 10, 11 or nil.  */
  246: 
  247: Lisp_Object Vwindow_system_version;
  248: 
  249: /* Vector of glyph definitions.  Indexed by glyph number, the contents
  250:    are a string which is how to output the glyph.
  251: 
  252:    If Vglyph_table is nil, a glyph is output by using its low 8 bits
  253:    as a character code.
  254: 
  255:    This is an obsolete feature that is no longer used.  The variable
  256:    is retained for compatibility.  */
  257: 
  258: Lisp_Object Vglyph_table;
  259: 
  260: /* Display table to use for vectors that don't specify their own.  */
  261: 
  262: Lisp_Object Vstandard_display_table;
  263: 
  264: /* Nonzero means reading single-character input with prompt so put
  265:    cursor on mini-buffer after the prompt.  Positive means at end of
  266:    text in echo area; negative means at beginning of line.  */
  267: 
  268: int cursor_in_echo_area;
  269: 
  270: Lisp_Object Qdisplay_table, Qredisplay_dont_pause;
  271: 
  272: ^L
  273: /* The currently selected frame.  In a single-frame version, this
  274:    variable always equals the_only_frame.  */
  275: 
  276: Lisp_Object selected_frame;
  277: 
  278: /* A frame which is not just a mini-buffer, or 0 if there are no such
  279:    frames.  This is usually the most recent such frame that was
  280:    selected.  In a single-frame version, this variable always holds
  281:    the address of the_only_frame.  */
  282: 
  283: struct frame *last_nonminibuf_frame;
  284: 
  285: /* Stdio stream being used for copy of all output.  */
  286: 
  287: FILE *termscript;
  288: 
  289: /* Structure for info on cursor positioning.  */
  290: 
  291: struct cm Wcm;
  292: 
  293: /* 1 means SIGWINCH happened when not safe.  */
  294: 
  295: int delayed_size_change;
  296: 
  297: /* 1 means glyph initialization has been completed at startup.  */
  298: 
  299: static int glyphs_initialized_initially_p;
  300: 
  301: /* Updated window if != 0.  Set by update_window.  */
  302: 
  303: struct window *updated_window;
  304: 
  305: /* Glyph row updated in update_window_line, and area that is updated.  */
  306: 
  307: struct glyph_row *updated_row;
  308: int updated_area;
  309: 
  310: /* A glyph for a space.  */
  311: 
  312: struct glyph space_glyph;
  313: 
  314: /* Non-zero means update has been performed directly, so that there's
  315:    no need for redisplay_internal to do much work.  Set by
  316:    direct_output_for_insert.  */
  317: 
  318: int redisplay_performed_directly_p;
  319: 
  320: /* Counts of allocated structures.  These counts serve to diagnose
  321:    memory leaks and double frees.  */
  322: 
  323: int glyph_matrix_count;
  324: int glyph_pool_count;
  325: 
  326: /* If non-null, the frame whose frame matrices are manipulated.  If
  327:    null, window matrices are worked on.  */
  328: 
  329: static struct frame *frame_matrix_frame;
  330: 
  331: /* Current interface for window-based redisplay.  Set from init_xterm.
  332:    A null value means we are not using window-based redisplay.  */
  333: 
  334: struct redisplay_interface *rif;
  335: 
  336: /* Non-zero means that fonts have been loaded since the last glyph
  337:    matrix adjustments.  Redisplay must stop, and glyph matrices must
  338:    be adjusted when this flag becomes non-zero during display.  The
  339:    reason fonts can be loaded so late is that fonts of fontsets are
  340:    loaded on demand.  */
  341: 
  342: int fonts_changed_p;
  343: 
  344: /* Convert vpos and hpos from frame to window and vice versa.
  345:    This may only be used for terminal frames.  */
  346: 
  347: #if GLYPH_DEBUG
  348: 
  349: static int window_to_frame_vpos P_ ((struct window *, int));
  350: static int window_to_frame_hpos P_ ((struct window *, int));
  351: #define WINDOW_TO_FRAME_VPOS(W, VPOS) window_to_frame_vpos ((W), (VPOS))
  352: #define WINDOW_TO_FRAME_HPOS(W, HPOS) window_to_frame_hpos ((W), (HPOS))
  353: 
  354: /* One element of the ring buffer containing redisplay history
  355:    information.  */
  356: 
  357: struct redisplay_history
  358: {
  359:   char trace[512 + 100];
  360: };
  361: 
  362: /* The size of the history buffer.  */
  363: 
  364: #define REDISPLAY_HISTORY_SIZE  30
  365: 
  366: /* The redisplay history buffer.  */
  367: 
  368: static struct redisplay_history redisplay_history[REDISPLAY_HISTORY_SIZE];
  369: 
  370: /* Next free entry in redisplay_history.  */
  371: 
  372: static int history_idx;
  373: 
  374: /* A tick that's incremented each time something is added to the
  375:    history.  */
  376: 
  377: static unsigned history_tick;
  378: 
  379: static void add_frame_display_history P_ ((struct frame *, int));
  380: static void add_window_display_history P_ ((struct window *, char *, int));
  381: ^L
  382: /* Add to the redisplay history how window W has been displayed.
  383:    MSG is a trace containing the information how W's glyph matrix
  384:    has been constructed.  PAUSED_P non-zero means that the update
  385:    has been interrupted for pending input.  */
  386: 
  387: static void
  388: add_window_display_history (w, msg, paused_p)
  389:      struct window *w;
  390:      char *msg;
  391:      int paused_p;
  392: {
  393:   char *buf;
  394: 
  395:   if (history_idx >= REDISPLAY_HISTORY_SIZE)
  396:     history_idx = 0;
  397:   buf = redisplay_history[history_idx].trace;
  398:   ++history_idx;
  399: 
  400:   sprintf (buf, "%d: window %p (`%s')%s\n",
  401:            history_tick++,
  402:            w,
  403:            ((BUFFERP (w->buffer)
  404:              && STRINGP (XBUFFER (w->buffer)->name))
  405:             ? (char *) SDATA (XBUFFER (w->buffer)->name)
  406:             : "???"),
  407:            paused_p ? " ***paused***" : "");
  408:   strcat (buf, msg);
  409: }
  410: 
  411: 
  412: /* Add to the redisplay history that frame F has been displayed.
  413:    PAUSED_P non-zero means that the update has been interrupted for
  414:    pending input.  */
  415: 
  416: static void
  417: add_frame_display_history (f, paused_p)
  418:      struct frame *f;
  419:      int paused_p;
  420: {
  421:   char *buf;
  422: 
  423:   if (history_idx >= REDISPLAY_HISTORY_SIZE)
  424:     history_idx = 0;
  425:   buf = redisplay_history[history_idx].trace;
  426:   ++history_idx;
  427: 
  428:   sprintf (buf, "%d: update frame %p%s",
  429:            history_tick++,
  430:            f, paused_p ? " ***paused***" : "");
  431: }
  432: 
  433: 
  434: DEFUN ("dump-redisplay-history", Fdump_redisplay_history,
  435:        Sdump_redisplay_history, 0, 0, "",
  436:        doc: /* Dump redisplay history to stderr.  */)
  437:      ()
  438: {
  439:   int i;
  440: 
  441:   for (i = history_idx - 1; i != history_idx; --i)
  442:     {
  443:       if (i < 0)
  444:         i = REDISPLAY_HISTORY_SIZE - 1;
  445:       fprintf (stderr, "%s\n", redisplay_history[i].trace);
  446:     }
  447: 
  448:   return Qnil;
  449: }
  450: 
  451: 
  452: #else /* GLYPH_DEBUG == 0 */
  453: 
  454: #define WINDOW_TO_FRAME_VPOS(W, VPOS) ((VPOS) + WINDOW_TOP_EDGE_LINE (W))
  455: #define WINDOW_TO_FRAME_HPOS(W, HPOS) ((HPOS) + WINDOW_LEFT_EDGE_COL (W))
  456: 
  457: #endif /* GLYPH_DEBUG == 0 */
  458: 
  459: 
  460: /* Like bcopy except never gets confused by overlap.  Let this be the
  461:    first function defined in this file, or change emacs.c where the
  462:    address of this function is used.  */
  463: 
  464: void
  465: safe_bcopy (from, to, size)
  466:      const char *from;
  467:      char *to;
  468:      int size;
  469: {
  470:   if (size <= 0 || from == to)
  471:     return;
  472: 
  473:   /* If the source and destination don't overlap, then bcopy can
  474:      handle it.  If they do overlap, but the destination is lower in
  475:      memory than the source, we'll assume bcopy can handle that.  */
  476:   if (to < from || from + size <= to)
  477:     bcopy (from, to, size);
  478: 
  479:   /* Otherwise, we'll copy from the end.  */
  480:   else
  481:     {
  482:       register const char *endf = from + size;
  483:       register char *endt = to + size;
  484: 
  485:       /* If TO - FROM is large, then we should break the copy into
  486:          nonoverlapping chunks of TO - FROM bytes each.  However, if
  487:          TO - FROM is small, then the bcopy function call overhead
  488:          makes this not worth it.  The crossover point could be about
  489:          anywhere.  Since I don't think the obvious copy loop is too
  490:          bad, I'm trying to err in its favor.  */
  491:       if (to - from < 64)
  492:         {
  493:           do
  494:             *--endt = *--endf;
  495:           while (endf != from);
  496:         }
  497:       else
  498:         {
  499:           for (;;)
  500:             {
  501:               endt -= (to - from);
  502:               endf -= (to - from);
  503: 
  504:               if (endt < to)
  505:                 break;
  506: 
  507:               bcopy (endf, endt, to - from);
  508:             }
  509: 
  510:           /* If SIZE wasn't a multiple of TO - FROM, there will be a
  511:              little left over.  The amount left over is (endt + (to -
  512:              from)) - to, which is endt - from.  */
  513:           bcopy (from, to, endt - from);
  514:         }
  515:     }
  516: }
  517: 
  518: 
  519: ^L
  520: /***********************************************************************
  521:                             Glyph Matrices
  522:  ***********************************************************************/
  523: 
  524: /* Allocate and return a glyph_matrix structure.  POOL is the glyph
  525:    pool from which memory for the matrix should be allocated, or null
  526:    for window-based redisplay where no glyph pools are used.  The
  527:    member `pool' of the glyph matrix structure returned is set to
  528:    POOL, the structure is otherwise zeroed.  */
  529: 
  530: struct glyph_matrix *
  531: new_glyph_matrix (pool)
  532:      struct glyph_pool *pool;
  533: {
  534:   struct glyph_matrix *result;
  535: 
  536:   /* Allocate and clear.  */
  537:   result = (struct glyph_matrix *) xmalloc (sizeof *result);
  538:   bzero (result, sizeof *result);
  539: 
  540:   /* Increment number of allocated matrices.  This count is used
  541:      to detect memory leaks.  */
  542:   ++glyph_matrix_count;
  543: 
  544:   /* Set pool and return.  */
  545:   result->pool = pool;
  546:   return result;
  547: }
  548: 
  549: 
  550: /* Free glyph matrix MATRIX.  Passing in a null MATRIX is allowed.
  551: 
  552:    The global counter glyph_matrix_count is decremented when a matrix
  553:    is freed.  If the count gets negative, more structures were freed
  554:    than allocated, i.e. one matrix was freed more than once or a bogus
  555:    pointer was passed to this function.
  556: 
  557:    If MATRIX->pool is null, this means that the matrix manages its own
  558:    glyph memory---this is done for matrices on X frames.  Freeing the
  559:    matrix also frees the glyph memory in this case.  */
  560: 
  561: static void
  562: free_glyph_matrix (matrix)
  563:      struct glyph_matrix *matrix;
  564: {
  565:   if (matrix)
  566:     {
  567:       int i;
  568: 
  569:       /* Detect the case that more matrices are freed than were
  570:          allocated.  */
  571:       if (--glyph_matrix_count < 0)
  572:         abort ();
  573: 
  574:       /* Free glyph memory if MATRIX owns it.  */
  575:       if (matrix->pool == NULL)
  576:         for (i = 0; i < matrix->rows_allocated; ++i)
  577:           xfree (matrix->rows