fringe.c 47.2 KB
Newer Older
Kenichi Handa's avatar
Kenichi Handa committed
1
/* Fringe handling (split from xdisp.c).
Paul Eggert's avatar
Paul Eggert committed
2
   Copyright (C) 1985-1988, 1993-1995, 1997-2020 Free Software
3
   Foundation, Inc.
Kenichi Handa's avatar
Kenichi Handa committed
4 5 6

This file is part of GNU Emacs.

7
GNU Emacs is free software: you can redistribute it and/or modify
Kenichi Handa's avatar
Kenichi Handa committed
8
it under the terms of the GNU General Public License as published by
9 10
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
Kenichi Handa's avatar
Kenichi Handa committed
11 12 13 14 15 16 17

GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
18
along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.  */
Kenichi Handa's avatar
Kenichi Handa committed
19 20 21

#include <config.h>

22 23
#include <byteswap.h>

Kenichi Handa's avatar
Kenichi Handa committed
24 25
#include "lisp.h"
#include "frame.h"
26
#include "ptr-bounds.h"
Kenichi Handa's avatar
Kenichi Handa committed
27 28 29 30
#include "window.h"
#include "dispextern.h"
#include "buffer.h"
#include "blockinput.h"
31
#include "termhooks.h"
Daniel Colascione's avatar
Daniel Colascione committed
32
#include "pdumper.h"
Kenichi Handa's avatar
Kenichi Handa committed
33

34 35 36 37 38
/* Fringe bitmaps are represented in three different ways:

   Logical bitmaps are used internally to denote things like
   'end-of-buffer', 'left-truncation', 'overlay-arrow', etc.

Paul Eggert's avatar
Paul Eggert committed
39
   Physical bitmaps specify the visual appearance of the bitmap,
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
   e.g. 'bottom-left-angle', 'left-arrow', 'left-triangle', etc.
   User defined bitmaps are physical bitmaps.

   Internally, fringe bitmaps for a specific display row are
   represented as a simple integer that is used as an index
   into the table of all defined bitmaps.  This index is stored
   in the `fringe' property of the physical bitmap symbol.

   Logical bitmaps are mapped to physical bitmaps through the
   buffer-local `fringe-indicator-alist' variable.

   Each element of this alist is a cons (LOGICAL . PHYSICAL)
   mapping a logical bitmap to a physical bitmap.
   PHYSICAL is either a symbol to use in both left and right fringe,
   or a cons of two symbols (LEFT . RIGHT) denoting different
   bitmaps to use in left and right fringe.

   LOGICAL is first looked up in the window's buffer's buffer-local
   value of the fringe-indicator-alist variable, and if not present,
   in the global value of fringe-indicator-alist.

   If LOGICAL is not present in either alist, or the PHYSICAL value
   found is nil, no bitmap is shown for the logical bitmap.

   The `left-fringe' and `right-fringe' display properties
   must specify physical bitmap symbols.
*/

Kenichi Handa's avatar
Kenichi Handa committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81
enum fringe_bitmap_align
{
  ALIGN_BITMAP_CENTER = 0,
  ALIGN_BITMAP_TOP,
  ALIGN_BITMAP_BOTTOM
};

struct fringe_bitmap
{
  unsigned short *bits;
  unsigned height : 8;
  unsigned width : 8;
  unsigned period : 8;
  unsigned align : 2;
82
  bool_bf dynamic : 1;
Kenichi Handa's avatar
Kenichi Handa committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
};


/***********************************************************************
			       Fringe bitmaps
 ***********************************************************************/

/* Undefined bitmap.  A question mark.  */
/*
  ..xxxx..
  .xxxxxx.
  xx....xx
  xx....xx
  ....xx..
  ...xx...
  ...xx...
  ........
  ...xx...
  ...xx...
*/
103
static unsigned short question_mark_bits[] = {
Kenichi Handa's avatar
Kenichi Handa committed
104 105
  0x3c, 0x7e, 0x7e, 0x0c, 0x18, 0x18, 0x00, 0x18, 0x18};

106
/* An exclamation mark.  */
Leo Liu's avatar
Leo Liu committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
/*
  ...XX...
  ...XX...
  ...XX...
  ...XX...
  ...XX...
  ...XX...
  ...XX...
  ........
  ...XX...
  ...XX...
*/
static unsigned short exclamation_mark_bits[] = {
  0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x00, 0x18};

Kenichi Handa's avatar
Kenichi Handa committed
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
/* An arrow like this: `<-'.  */
/*
  ...xx...
  ..xx....
  .xx.....
  xxxxxx..
  xxxxxx..
  .xx.....
  ..xx....
  ...xx...
*/
static unsigned short left_arrow_bits[] = {
   0x18, 0x30, 0x60, 0xfc, 0xfc, 0x60, 0x30, 0x18};


/* Right truncation arrow bitmap `->'.  */
/*
  ...xx...
  ....xx..
  .....xx.
  ..xxxxxx
  ..xxxxxx
  .....xx.
  ....xx..
  ...xx...
*/
static unsigned short right_arrow_bits[] = {
   0x18, 0x0c, 0x06, 0x3f, 0x3f, 0x06, 0x0c, 0x18};


/* Up arrow bitmap.  */
/*
  ...xx...
  ..xxxx..
  .xxxxxx.
  xxxxxxxx
  ...xx...
  ...xx...
  ...xx...
  ...xx...
*/
static unsigned short up_arrow_bits[] = {
   0x18, 0x3c, 0x7e, 0xff, 0x18, 0x18, 0x18, 0x18};


/* Down arrow bitmap.  */
/*
  ...xx...
  ...xx...
  ...xx...
  ...xx...
  xxxxxxxx
  .xxxxxx.
  ..xxxx..
  ...xx...
*/
static unsigned short down_arrow_bits[] = {
   0x18, 0x18, 0x18, 0x18, 0xff, 0x7e, 0x3c, 0x18};

/* Marker for continuation lines.  */
/*
  ..xxxx..
  .xxxxx..
  xx......
  xxx..x..
  xxxxxx..
  .xxxxx..
  ..xxxx..
  .xxxxx..
*/
192
static unsigned short left_curly_arrow_bits[] = {
Kenichi Handa's avatar
Kenichi Handa committed
193 194
   0x3c, 0x7c, 0xc0, 0xe4, 0xfc, 0x7c, 0x3c, 0x7c};

195
/* Marker for continued lines.  */
Kenichi Handa's avatar
Kenichi Handa committed
196
/*
197 198 199 200 201 202 203 204
  ..xxxx..
  ..xxxxx.
  ......xx
  ..x..xxx
  ..xxxxxx
  ..xxxxx.
  ..xxxx..
  ..xxxxx.
Kenichi Handa's avatar
Kenichi Handa committed
205
*/
206 207
static unsigned short right_curly_arrow_bits[] = {
   0x3c, 0x3e, 0x03, 0x27, 0x3f, 0x3e, 0x3c, 0x3e};
Kenichi Handa's avatar
Kenichi Handa committed
208 209 210 211 212 213 214 215 216 217 218 219

/* Reverse Overlay arrow bitmap.  A triangular arrow.  */
/*
  ......xx
  ....xxxx
  ...xxxxx
  ..xxxxxx
  ..xxxxxx
  ...xxxxx
  ....xxxx
  ......xx
*/
220
static unsigned short left_triangle_bits[] = {
Kenichi Handa's avatar
Kenichi Handa committed
221
   0x03, 0x0f, 0x1f, 0x3f, 0x3f, 0x1f, 0x0f, 0x03};
222 223 224 225 226 227 228 229 230 231 232 233 234 235

/* Overlay arrow bitmap.  A triangular arrow.  */
/*
  xx......
  xxxx....
  xxxxx...
  xxxxxx..
  xxxxxx..
  xxxxx...
  xxxx....
  xx......
*/
static unsigned short right_triangle_bits[] = {
   0xc0, 0xf0, 0xf8, 0xfc, 0xfc, 0xf8, 0xf0, 0xc0};
Kenichi Handa's avatar
Kenichi Handa committed
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

/* First line bitmap.  An top-left angle.  */
/*
  xxxxxx..
  xxxxxx..
  xx......
  xx......
  xx......
  xx......
  xx......
  ........
*/
static unsigned short top_left_angle_bits[] = {
   0xfc, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0x00};

/* First line bitmap.  An right-up angle.  */
/*
  ..xxxxxx
  ..xxxxxx
  ......xx
  ......xx
  ......xx
  ......xx
  ......xx
  ........
*/
static unsigned short top_right_angle_bits[] = {
   0x3f, 0x3f, 0x03, 0x03, 0x03, 0x03, 0x03, 0x00};

/* Last line bitmap.  An left-down angle.  */
/*
  ........
  xx......
  xx......
  xx......
  xx......
  xx......
  xxxxxx..
  xxxxxx..
*/
static unsigned short bottom_left_angle_bits[] = {
   0x00, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xfc};

/* Last line bitmap.  An right-down angle.  */
/*
  ........
  ......xx
  ......xx
  ......xx
  ......xx
  ......xx
  ..xxxxxx
  ..xxxxxx
*/
static unsigned short bottom_right_angle_bits[] = {
   0x00, 0x03, 0x03, 0x03, 0x03, 0x03, 0x3f, 0x3f};

/* First/last line bitmap.  An left bracket.  */
/*
  xxxxxx..
  xxxxxx..
  xx......
  xx......
  xx......
  xx......
  xx......
  xx......
  xxxxxx..
  xxxxxx..
*/
static unsigned short left_bracket_bits[] = {
   0xfc, 0xfc, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xfc, 0xfc};

/* First/last line bitmap.  An right bracket.  */
/*
  ..xxxxxx
  ..xxxxxx
  ......xx
  ......xx
  ......xx
  ......xx
  ......xx
  ......xx
  ..xxxxxx
  ..xxxxxx
*/
static unsigned short right_bracket_bits[] = {
  0x3f, 0x3f, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x3f, 0x3f};

/* Filled box cursor bitmap.  A filled box; max 13 pixels high.  */
/*
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
  xxxxxxx.
*/
341
static unsigned short filled_rectangle_bits[] = {
Kenichi Handa's avatar
Kenichi Handa committed
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
   0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe, 0xfe};

/* Hollow box cursor bitmap.  A hollow box; max 13 pixels high.  */
/*
  xxxxxxx.
  x.....x.
  x.....x.
  x.....x.
  x.....x.
  x.....x.
  x.....x.
  x.....x.
  x.....x.
  x.....x.
  x.....x.
  x.....x.
  xxxxxxx.
*/
360
static unsigned short hollow_rectangle_bits[] = {
Kenichi Handa's avatar
Kenichi Handa committed
361 362
   0xfe, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0x82, 0xfe};

363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
/* Hollow square bitmap.  */
/*
  .xxxxxx.
  .x....x.
  .x....x.
  .x....x.
  .x....x.
  .xxxxxx.
*/
static unsigned short hollow_square_bits[] = {
   0x7e, 0x42, 0x42, 0x42, 0x42, 0x7e};

/* Filled square bitmap.  */
/*
  .xxxxxx.
  .xxxxxx.
  .xxxxxx.
  .xxxxxx.
  .xxxxxx.
  .xxxxxx.
*/
static unsigned short filled_square_bits[] = {
   0x7e, 0x7e, 0x7e, 0x7e, 0x7e, 0x7e};

Kenichi Handa's avatar
Kenichi Handa committed
387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
/* Bar cursor bitmap.  A vertical bar; max 13 pixels high.  */
/*
  xx......
  xx......
  xx......
  xx......
  xx......
  xx......
  xx......
  xx......
  xx......
  xx......
  xx......
  xx......
  xx......
*/
403
static unsigned short vertical_bar_bits[] = {
Kenichi Handa's avatar
Kenichi Handa committed
404 405
   0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0};

406
/* HBar cursor bitmap.  A horizontal bar; 2 pixels high.  */
Kenichi Handa's avatar
Kenichi Handa committed
407 408 409 410
/*
  xxxxxxx.
  xxxxxxx.
*/
411
static unsigned short horizontal_bar_bits[] = {
Kenichi Handa's avatar
Kenichi Handa committed
412 413 414 415 416 417 418 419 420 421 422 423 424
  0xfe, 0xfe};


/* Bitmap drawn to indicate lines not displaying text if
   `indicate-empty-lines' is non-nil.  */
/*
  ........
  ..xxxx..
  ........
  ........
  ..xxxx..
  ........
*/
425
static unsigned short empty_line_bits[] = {
Kenichi Handa's avatar
Kenichi Handa committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439
  0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
  0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
  0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
  0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
  0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
  0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
  0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00,
  0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x3c, 0x00};


#define BYTES_PER_BITMAP_ROW  (sizeof (unsigned short))
#define STANDARD_BITMAP_HEIGHT(bits) (sizeof (bits)/BYTES_PER_BITMAP_ROW)
#define FRBITS(bits)  bits, STANDARD_BITMAP_HEIGHT (bits)

440 441 442
/* NOTE:  The order of these bitmaps must match the sequence
   used in fringe.el to define the corresponding symbols.  */

443
static struct fringe_bitmap standard_bitmaps[] =
Kenichi Handa's avatar
Kenichi Handa committed
444 445
{
  { NULL, 0, 0, 0, 0, 0 }, /* NO_FRINGE_BITMAP */
446
  { FRBITS (question_mark_bits),      8, 0, ALIGN_BITMAP_CENTER, 0 },
Leo Liu's avatar
Leo Liu committed
447
  { FRBITS (exclamation_mark_bits),   8, 0, ALIGN_BITMAP_CENTER, 0 },
Kenichi Handa's avatar
Kenichi Handa committed
448 449 450 451
  { FRBITS (left_arrow_bits),         8, 0, ALIGN_BITMAP_CENTER, 0 },
  { FRBITS (right_arrow_bits),        8, 0, ALIGN_BITMAP_CENTER, 0 },
  { FRBITS (up_arrow_bits),           8, 0, ALIGN_BITMAP_TOP,    0 },
  { FRBITS (down_arrow_bits),         8, 0, ALIGN_BITMAP_BOTTOM, 0 },
452 453 454 455
  { FRBITS (left_curly_arrow_bits),   8, 0, ALIGN_BITMAP_CENTER, 0 },
  { FRBITS (right_curly_arrow_bits),  8, 0, ALIGN_BITMAP_CENTER, 0 },
  { FRBITS (left_triangle_bits),      8, 0, ALIGN_BITMAP_CENTER, 0 },
  { FRBITS (right_triangle_bits),     8, 0, ALIGN_BITMAP_CENTER, 0 },
Kenichi Handa's avatar
Kenichi Handa committed
456 457 458 459 460 461
  { FRBITS (top_left_angle_bits),     8, 0, ALIGN_BITMAP_TOP,    0 },
  { FRBITS (top_right_angle_bits),    8, 0, ALIGN_BITMAP_TOP,    0 },
  { FRBITS (bottom_left_angle_bits),  8, 0, ALIGN_BITMAP_BOTTOM, 0 },
  { FRBITS (bottom_right_angle_bits), 8, 0, ALIGN_BITMAP_BOTTOM, 0 },
  { FRBITS (left_bracket_bits),       8, 0, ALIGN_BITMAP_CENTER, 0 },
  { FRBITS (right_bracket_bits),      8, 0, ALIGN_BITMAP_CENTER, 0 },
462 463 464
  { FRBITS (filled_rectangle_bits),   8, 0, ALIGN_BITMAP_CENTER, 0 },
  { FRBITS (hollow_rectangle_bits),   8, 0, ALIGN_BITMAP_CENTER, 0 },
  { FRBITS (filled_square_bits),      8, 0, ALIGN_BITMAP_CENTER, 0 },
Kenichi Handa's avatar
Kenichi Handa committed
465
  { FRBITS (hollow_square_bits),      8, 0, ALIGN_BITMAP_CENTER, 0 },
466
  { FRBITS (vertical_bar_bits),       8, 0, ALIGN_BITMAP_CENTER, 0 },
467
  { FRBITS (horizontal_bar_bits),     8, 0, ALIGN_BITMAP_BOTTOM, 0 },
468
  { FRBITS (empty_line_bits),         8, 3, ALIGN_BITMAP_TOP,    0 },
Kenichi Handa's avatar
Kenichi Handa committed
469 470
};

471 472
#define NO_FRINGE_BITMAP 0
#define UNDEF_FRINGE_BITMAP 1
473
#define MAX_STANDARD_FRINGE_BITMAPS ARRAYELTS (standard_bitmaps)
474

475
static struct fringe_bitmap **fringe_bitmaps;
476
static Lisp_Object *fringe_faces;
477
static int max_fringe_bitmaps;
Kenichi Handa's avatar
Kenichi Handa committed
478

479
int max_used_fringe_bitmap = MAX_STANDARD_FRINGE_BITMAPS;
Kenichi Handa's avatar
Kenichi Handa committed
480

Kim F. Storm's avatar
Kim F. Storm committed
481 482 483

/* Lookup bitmap number for symbol BITMAP.
   Return 0 if not a bitmap.  */
Kenichi Handa's avatar
Kenichi Handa committed
484 485

int
486
lookup_fringe_bitmap (Lisp_Object bitmap)
487
{
488
  EMACS_INT bn;
489

Kim F. Storm's avatar
Kim F. Storm committed
490
  bitmap = Fget (bitmap, Qfringe);
491
  if (!FIXNUMP (bitmap))
492 493
    return 0;

Tom Tromey's avatar
Tom Tromey committed
494
  bn = XFIXNUM (bitmap);
Kim F. Storm's avatar
Kim F. Storm committed
495 496 497 498 499 500 501
  if (bn > NO_FRINGE_BITMAP
      && bn < max_used_fringe_bitmap
      && (bn < MAX_STANDARD_FRINGE_BITMAPS
	  || fringe_bitmaps[bn] != NULL))
    return bn;

  return 0;
502 503 504 505 506 507 508 509 510 511
}

/* Get fringe bitmap name for bitmap number BN.

   Found by traversing Vfringe_bitmaps comparing BN to the
   fringe property for each symbol.

   Return BN if not found in Vfringe_bitmaps.  */

static Lisp_Object
512
get_fringe_bitmap_name (int bn)
513 514 515 516 517 518 519 520 521
{
  Lisp_Object bitmaps;
  Lisp_Object num;

  /* Zero means no bitmap -- return nil.  */
  if (bn <= 0)
    return Qnil;

  bitmaps = Vfringe_bitmaps;
522
  num = make_fixnum (bn);
523 524 525 526 527 528 529 530 531 532 533 534

  while (CONSP (bitmaps))
    {
      Lisp_Object bitmap = XCAR (bitmaps);
      if (EQ (num, Fget (bitmap, Qfringe)))
	return bitmap;
      bitmaps = XCDR (bitmaps);
    }

  return num;
}

535 536 537 538 539 540 541 542 543 544 545 546 547 548
/* Get fringe bitmap data for bitmap number BN.  */

static struct fringe_bitmap *
get_fringe_bitmap_data (int bn)
{
  struct fringe_bitmap *fb;

  fb = fringe_bitmaps[bn];
  if (fb == NULL)
    fb = &standard_bitmaps[bn < MAX_STANDARD_FRINGE_BITMAPS
			   ? bn : UNDEF_FRINGE_BITMAP];

  return fb;
}
549

Kenichi Handa's avatar
Kenichi Handa committed
550 551 552 553 554 555 556
/* Draw the bitmap WHICH in one of the left or right fringes of
   window W.  ROW is the glyph row for which to display the bitmap; it
   determines the vertical position at which the bitmap has to be
   drawn.
   LEFT_P is 1 for left fringe, 0 for right fringe.
*/

557
static void
558
draw_fringe_bitmap_1 (struct window *w, struct glyph_row *row, int left_p, int overlay, int which)
Kenichi Handa's avatar
Kenichi Handa committed
559 560 561 562 563 564
{
  struct frame *f = XFRAME (WINDOW_FRAME (w));
  struct draw_fringe_bitmap_params p;
  struct fringe_bitmap *fb;
  int period;
  int face_id = DEFAULT_FACE_ID;
565
  int offset, header_line_height;
Kenichi Handa's avatar
Kenichi Handa committed
566 567 568 569 570 571

  p.overlay_p = (overlay & 1) == 1;
  p.cursor_p = (overlay & 2) == 2;

  if (which != NO_FRINGE_BITMAP)
    {
572
      offset = 0;
Kenichi Handa's avatar
Kenichi Handa committed
573 574 575 576 577
    }
  else if (left_p)
    {
      which = row->left_fringe_bitmap;
      face_id = row->left_fringe_face_id;
578
      offset = row->left_fringe_offset;
Kenichi Handa's avatar
Kenichi Handa committed
579 580 581 582 583
    }
  else
    {
      which = row->right_fringe_bitmap;
      face_id = row->right_fringe_face_id;
584
      offset = row->right_fringe_offset;
Kenichi Handa's avatar
Kenichi Handa committed
585 586 587
    }

  if (face_id == DEFAULT_FACE_ID)
588
    {
589
      Lisp_Object face = fringe_faces[which];
590 591
      face_id = NILP (face) ? lookup_named_face (w, f, Qfringe, false)
	: lookup_derived_face (w, f, face, FRINGE_FACE_ID, 0);
592
      if (face_id < 0)
593 594
	face_id = FRINGE_FACE_ID;
    }
Kenichi Handa's avatar
Kenichi Handa committed
595

596
  fb = get_fringe_bitmap_data (which);
Kenichi Handa's avatar
Kenichi Handa committed
597 598 599 600

  period = fb->period;

  /* Convert row to frame coordinates.  */
601
  p.y = WINDOW_TO_FRAME_PIXEL_Y (w, row->y) + offset;
Kenichi Handa's avatar
Kenichi Handa committed
602 603 604 605 606 607 608 609

  p.which = which;
  p.bits = fb->bits;
  p.wd = fb->width;

  p.h = fb->height;
  p.dh = (period > 0 ? (p.y % period) : 0);
  p.h -= p.dh;
610 611 612 613 614 615 616 617 618 619 620 621 622

  /* Adjust y to the offset in the row to start drawing the bitmap.  */
  switch (fb->align)
    {
    case ALIGN_BITMAP_CENTER:
      p.y += (row->height - p.h) / 2;
      break;
    case ALIGN_BITMAP_BOTTOM:
      p.y += (row->visible_height - p.h);
      break;
    case ALIGN_BITMAP_TOP:
      break;
    }
Kenichi Handa's avatar
Kenichi Handa committed
623

624
  p.face = FACE_FROM_ID_OR_NULL (f, face_id);
Kenichi Handa's avatar
Kenichi Handa committed
625 626
  if (p.face == NULL)
    {
627 628
      /* This could happen after clearing face cache.
	 But it shouldn't happen anymore.  ++kfs */
Kenichi Handa's avatar
Kenichi Handa committed
629 630 631
      return;
    }

632
  prepare_face_for_display (f, p.face);
Kenichi Handa's avatar
Kenichi Handa committed
633 634 635 636

  /* Clear left fringe if no bitmap to draw or if bitmap doesn't fill
     the fringe.  */
  p.bx = -1;
637 638
  header_line_height = WINDOW_TAB_LINE_HEIGHT (w)
		       + WINDOW_HEADER_LINE_HEIGHT (w);
639 640
  p.by = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, row->y));
  p.ny = row->visible_height;
Kenichi Handa's avatar
Kenichi Handa committed
641 642 643 644 645 646 647 648 649 650
  if (left_p)
    {
      int wd = WINDOW_LEFT_FRINGE_WIDTH (w);
      int x = window_box_left (w, (WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
				   ? LEFT_MARGIN_AREA
				   : TEXT_AREA));
      if (p.wd > wd)
	p.wd = wd;
      p.x = x - p.wd - (wd - p.wd) / 2;

651
      if (p.wd < wd || p.y > p.by || p.y + p.h < p.by + p.ny)
Kenichi Handa's avatar
Kenichi Handa committed
652 653 654
	{
	  /* If W has a vertical border to its left, don't draw over it.  */
	  wd -= ((!WINDOW_LEFTMOST_P (w)
655 656 657 658
		  /* This could be wrong when we allow window local
		     right dividers - but the window on the left is hard
		     to get.  */
		  && !FRAME_RIGHT_DIVIDER_WIDTH (f)
659 660 661 662 663 664 665
		  && !WINDOW_HAS_VERTICAL_SCROLL_BAR (w)
		  /* But don't reduce the fringe width if the window
		     has a left margin, because that means we are not
		     in danger of drawing over the vertical border,
		     and OTOH leaving out that one pixel leaves behind
		     traces of the cursor, if it was in column zero
		     before drawing non-empty margin area.  */
666
		  && w->left_margin_cols == 0)
Kenichi Handa's avatar
Kenichi Handa committed
667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
		 ? 1 : 0);
	  p.bx = x - wd;
	  p.nx = wd;
	}
    }
  else
    {
      int x = window_box_right (w,
				(WINDOW_HAS_FRINGES_OUTSIDE_MARGINS (w)
				 ? RIGHT_MARGIN_AREA
				 : TEXT_AREA));
      int wd = WINDOW_RIGHT_FRINGE_WIDTH (w);
      if (p.wd > wd)
	p.wd = wd;
      p.x = x + (wd - p.wd) / 2;
      /* Clear right fringe if no bitmap to draw of if bitmap doesn't fill
	 the fringe.  */
684
      if (p.wd < wd || p.y > p.by || p.y + p.h < p.by + p.ny)
Kenichi Handa's avatar
Kenichi Handa committed
685 686 687 688 689 690
	{
	  p.bx = x;
	  p.nx = wd;
	}
    }

691 692 693
  if (p.x >= WINDOW_BOX_LEFT_EDGE_X (w)
      && (p.x + p.wd) <= WINDOW_BOX_LEFT_EDGE_X (w) + WINDOW_PIXEL_WIDTH (w))
    FRAME_RIF (f)->draw_fringe_bitmap (w, row, &p);
Kenichi Handa's avatar
Kenichi Handa committed
694 695
}

696
static int
697
get_logical_cursor_bitmap (struct window *w, Lisp_Object cursor)
698 699 700
{
  Lisp_Object cmap, bm = Qnil;

701
  if ((cmap = BVAR (XBUFFER (w->contents), fringe_cursor_alist)), !NILP (cmap))
702 703 704 705 706 707 708 709 710
    {
      bm = Fassq (cursor, cmap);
      if (CONSP (bm))
	{
	  if ((bm = XCDR (bm)), NILP (bm))
	    return NO_FRINGE_BITMAP;
	  return lookup_fringe_bitmap (bm);
	}
    }
Tom Tromey's avatar
Tom Tromey committed
711
  if (EQ (cmap, BVAR (&buffer_defaults, fringe_cursor_alist)))
712
    return NO_FRINGE_BITMAP;
Tom Tromey's avatar
Tom Tromey committed
713
  bm = Fassq (cursor, BVAR (&buffer_defaults, fringe_cursor_alist));
714 715 716 717 718 719
  if (!CONSP (bm) || ((bm = XCDR (bm)), NILP (bm)))
    return NO_FRINGE_BITMAP;
  return lookup_fringe_bitmap (bm);
}

static int
720
get_logical_fringe_bitmap (struct window *w, Lisp_Object bitmap, int right_p, int partial_p)
721 722
{
  Lisp_Object cmap, bm1 = Qnil, bm2 = Qnil, bm;
Paul Eggert's avatar
Paul Eggert committed
723
  ptrdiff_t ln1 = 0, ln2 = 0;
724 725 726 727 728 729 730 731
  int ix1 = right_p;
  int ix2 = ix1 + (partial_p ? 2 : 0);

  /* Lookup in buffer-local fringe-indicator-alist before global alist.

     Elements are:
	BITMAP		-- use for all
	(L R)		-- use for left right (whether partial or not)
Juanma Barranquero's avatar
Juanma Barranquero committed
732
	(L R PL PR)	-- use for left right partial-left partial-right
733 734 735 736 737
	If any value in local binding is not present or t, use global value.

     If partial, lookup partial bitmap in default value if not found here.
     If not partial, or no partial spec is present, use non-partial bitmap.  */

738
  if ((cmap = BVAR (XBUFFER (w->contents), fringe_indicator_alist)), !NILP (cmap))
739 740 741 742 743 744 745 746
    {
      bm1 = Fassq (bitmap, cmap);
      if (CONSP (bm1))
	{
	  if ((bm1 = XCDR (bm1)), NILP (bm1))
	    return NO_FRINGE_BITMAP;
	  if (CONSP (bm1))
	    {
Paul Eggert's avatar
Paul Eggert committed
747
	      ln1 = list_length (bm1);
748 749 750 751
	      if (partial_p)
		{
		  if (ln1 > ix2)
		    {
752
		      bm = Fnth (make_fixnum (ix2), bm1);
753 754 755 756 757 758 759 760
		      if (!EQ (bm, Qt))
			goto found;
		    }
		}
	      else
		{
		  if (ln1 > ix1)
		    {
761
		      bm = Fnth (make_fixnum (ix1), bm1);
762 763 764 765 766 767 768 769 770 771
		      if (!EQ (bm, Qt))
			goto found;
		    }
		}
	    }
	  else if ((bm = bm1, !EQ (bm, Qt)))
	    goto found;
	}
    }

Tom Tromey's avatar
Tom Tromey committed
772 773
  if (!EQ (cmap, BVAR (&buffer_defaults, fringe_indicator_alist))
      && !NILP (BVAR (&buffer_defaults, fringe_indicator_alist)))
774
    {
Tom Tromey's avatar
Tom Tromey committed
775
      bm2 = Fassq (bitmap, BVAR (&buffer_defaults, fringe_indicator_alist));
776 777 778 779 780 781
      if (CONSP (bm2))
	{
	  if ((bm2 = XCDR (bm2)), !NILP (bm2))
	    {
	      if (CONSP (bm2))
		{
Paul Eggert's avatar
Paul Eggert committed
782
		  ln2 = list_length (bm2);
783 784 785 786
		  if (partial_p)
		    {
		      if (ln2 > ix2)
			{
787
			  bm = Fnth (make_fixnum (ix2), bm2);
788 789 790 791 792 793 794 795 796 797 798
			  if (!EQ (bm, Qt))
			    goto found;
			}
		    }
		}
	    }
	}
    }

  if (ln1 > ix1)
    {
799
      bm = Fnth (make_fixnum (ix1), bm1);
800 801 802 803 804 805
      if (!EQ (bm, Qt))
	goto found;
    }

  if (ln2 > ix1)
    {
806
      bm = Fnth (make_fixnum (ix1), bm2);
807 808 809 810 811 812 813 814 815 816 817 818
      if (!EQ (bm, Qt))
	goto found;
      return NO_FRINGE_BITMAP;
    }
  else if ((bm = bm2, NILP (bm)))
    return NO_FRINGE_BITMAP;

 found:
  return lookup_fringe_bitmap (bm);
}


Kenichi Handa's avatar
Kenichi Handa committed
819
void
820
draw_fringe_bitmap (struct window *w, struct glyph_row *row, int left_p)
Kenichi Handa's avatar
Kenichi Handa committed
821 822 823
{
  int overlay = 0;

824
  if (left_p == row->reversed_p && row->cursor_in_fringe_p)
Kenichi Handa's avatar
Kenichi Handa committed
825
    {
826
      Lisp_Object cursor = Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
827 828 829 830

      switch (w->phys_cursor_type)
	{
	case HOLLOW_BOX_CURSOR:
831 832
	  if (row->visible_height >= STANDARD_BITMAP_HEIGHT (hollow_rectangle_bits))
	    cursor = Qhollow;
Kenichi Handa's avatar
Kenichi Handa committed
833
	  else
834
	    cursor = Qhollow_small;
Kenichi Handa's avatar
Kenichi Handa committed
835 836
	  break;
	case FILLED_BOX_CURSOR:
837
	  cursor = Qbox;
Kenichi Handa's avatar
Kenichi Handa committed
838 839
	  break;
	case BAR_CURSOR:
840
	  cursor = Qbar;
Kenichi Handa's avatar
Kenichi Handa committed
841 842
	  break;
	case HBAR_CURSOR:
843
	  cursor = Qhbar;
Kenichi Handa's avatar
Kenichi Handa committed
844 845 846 847 848 849 850
	  break;
	case NO_CURSOR:
	default:
	  w->phys_cursor_on_p = 0;
	  row->cursor_in_fringe_p = 0;
	  break;
	}
851
      if (!NILP (cursor))
Kenichi Handa's avatar
Kenichi Handa committed
852
	{
853 854 855
	  int bm = get_logical_cursor_bitmap (w, cursor);
	  if (bm != NO_FRINGE_BITMAP)
	    {
856
	      draw_fringe_bitmap_1 (w, row, left_p, 2, bm);
857 858
	      overlay = EQ (cursor, Qbox) ? 3 : 1;
	    }
Kenichi Handa's avatar
Kenichi Handa committed
859 860 861 862 863
	}
    }

  draw_fringe_bitmap_1 (w, row, left_p, overlay, NO_FRINGE_BITMAP);

864
  if (left_p && row->overlay_arrow_bitmap != NO_FRINGE_BITMAP)
865
    draw_fringe_bitmap_1 (w, row, 1, 1, row->overlay_arrow_bitmap);
Kenichi Handa's avatar
Kenichi Handa committed
866 867 868 869 870 871 872
}


/* Draw fringe bitmaps for glyph row ROW on window W.  Call this
   function with input blocked.  */

void
873
draw_row_fringe_bitmaps (struct window *w, struct glyph_row *row)
Kenichi Handa's avatar
Kenichi Handa committed
874
{
875
  eassert (input_blocked_p ());
Kenichi Handa's avatar
Kenichi Handa committed
876 877 878 879 880 881 882 883 884 885 886 887 888 889

  /* If row is completely invisible, because of vscrolling, we
     don't have to draw anything.  */
  if (row->visible_height <= 0)
    return;

  if (WINDOW_LEFT_FRINGE_WIDTH (w) != 0)
    draw_fringe_bitmap (w, row, 1);

  if (WINDOW_RIGHT_FRINGE_WIDTH (w) != 0)
    draw_fringe_bitmap (w, row, 0);
}

/* Draw the fringes of window W.  Only fringes for rows marked for
890
   update in redraw_fringe_bitmaps_p are drawn.
Kenichi Handa's avatar
Kenichi Handa committed
891

892
   Return nonzero if left or right fringe was redrawn in any way.
893

894 895
   If NO_FRINGE_P is non-zero, also return nonzero if either fringe
   has zero width.
896

897 898
   A return nonzero value indicates that the vertical line between
   windows needs update (as it may be drawn in the fringe).
899 900
*/

901 902
bool
draw_window_fringes (struct window *w, bool no_fringe_p)
Kenichi Handa's avatar
Kenichi Handa committed
903 904 905 906
{
  struct glyph_row *row;
  int yb = window_text_bottom_y (w);
  int nrows = w->current_matrix->nrows;
907
  int y, rn;
908
  bool updated_p = 0;
Kenichi Handa's avatar
Kenichi Handa committed
909 910

  if (w->pseudo_window_p)
911
    return updated_p;
912

913 914 915 916 917 918
  /* We must switch to the window's buffer to use its local value of
     the fringe face, in case it's been remapped in face-remapping-alist.  */
  Lisp_Object window_buffer = w->contents;
  struct buffer *oldbuf = current_buffer;
  set_buffer_internal_1 (XBUFFER (window_buffer));

919
  /* Must draw line if no fringe */
920
  if (no_fringe_p
921 922
      && (WINDOW_LEFT_FRINGE_WIDTH (w) == 0
	  || WINDOW_RIGHT_FRINGE_WIDTH (w) == 0))
923
    updated_p = 1;
Kenichi Handa's avatar
Kenichi Handa committed
924

925
  for (y = w->vscroll, rn = 0, row = w->current_matrix->rows;
Kenichi Handa's avatar
Kenichi Handa committed
926 927 928 929 930 931 932
       y < yb && rn < nrows;
       y += row->height, ++row, ++rn)
    {
      if (!row->redraw_fringe_bitmaps_p)
	continue;
      draw_row_fringe_bitmaps (w, row);
      row->redraw_fringe_bitmaps_p = 0;
933
      updated_p = 1;
Kenichi Handa's avatar
Kenichi Handa committed
934
    }
935

936 937
  set_buffer_internal_1 (oldbuf);

938
  return updated_p;
Kenichi Handa's avatar
Kenichi Handa committed
939 940 941 942
}


/* Recalculate the bitmaps to show in the fringes of window W.
943 944 945
   Only mark rows with modified bitmaps for update in redraw_fringe_bitmaps_p.

   If KEEP_CURRENT_P is 0, update current_matrix too.  */
Kenichi Handa's avatar
Kenichi Handa committed
946

947 948
bool
update_window_fringes (struct window *w, bool keep_current_p)
Kenichi Handa's avatar
Kenichi Handa committed
949 950 951 952 953
{
  struct glyph_row *row, *cur = 0;
  int yb = window_text_bottom_y (w);
  int rn, nrows = w->current_matrix->nrows;
  int y;
954
  bool redraw_p = 0;
955 956 957 958
  Lisp_Object boundary_top = Qnil, boundary_bot = Qnil;
  Lisp_Object arrow_top = Qnil, arrow_bot = Qnil;
  Lisp_Object empty_pos;
  Lisp_Object ind = Qnil;
959 960
#define MAX_BITMAP_CACHE (8*4)
  int bitmap_cache[MAX_BITMAP_CACHE];
961 962
  int top_ind_rn, bot_ind_rn;
  int top_ind_min_y, bot_ind_max_y;
Paul Eggert's avatar
Paul Eggert committed
963

Paul Eggert's avatar
Paul Eggert committed
964
  /* top_ind_rn is set to a nonnegative value whenever
Paul Eggert's avatar
Paul Eggert committed
965 966 967
     row->indicate_bob_p is set, so it's OK that top_row_ends_at_zv_p
     is not initialized here.  Similarly for bot_ind_rn,
     row->indicate_eob_p and bot_row_ends_at_zv_p.  */
968
  int top_row_ends_at_zv_p UNINIT, bot_row_ends_at_zv_p UNINIT;
Kenichi Handa's avatar
Kenichi Handa committed
969 970 971 972 973

  if (w->pseudo_window_p)
    return 0;

  if (!MINI_WINDOW_P (w)
974
      && (ind = BVAR (XBUFFER (w->contents), indicate_buffer_boundaries), !NILP (ind)))
Kenichi Handa's avatar
Kenichi Handa committed
975
    {
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
      if (EQ (ind, Qleft) || EQ (ind, Qright))
	boundary_top = boundary_bot = arrow_top = arrow_bot = ind;
      else if (CONSP (ind) && CONSP (XCAR (ind)))
	{
	  Lisp_Object pos;
	  if (pos = Fassq (Qt, ind), !NILP (pos))
	    boundary_top = boundary_bot = arrow_top = arrow_bot = XCDR (pos);
	  if (pos = Fassq (Qtop, ind), !NILP (pos))
	    boundary_top = XCDR (pos);
	  if (pos = Fassq (Qbottom, ind), !NILP (pos))
	    boundary_bot = XCDR (pos);
	  if (pos = Fassq (Qup, ind), !NILP (pos))
	    arrow_top = XCDR (pos);
	  if (pos = Fassq (Qdown, ind), !NILP (pos))
	    arrow_bot = XCDR (pos);
	}
Kenichi Handa's avatar
Kenichi Handa committed
992
      else
993 994
	/* Anything else means boundary on left and no arrows.  */
	boundary_top = boundary_bot = Qleft;
995
    }
Kenichi Handa's avatar
Kenichi Handa committed
996

997
  top_ind_rn = bot_ind_rn = -1;
998 999
  if (!NILP (ind))
    {
1000
      for (y = w->vscroll, rn = 0;
Kenichi Handa's avatar
Kenichi Handa committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	   y < yb && rn < nrows;
	   y += row->height, ++rn)
	{
	  row = w->desired_matrix->rows + rn;
	  if (!row->enabled_p)
	    row = w->current_matrix->rows + rn;

	  row->indicate_bob_p = row->indicate_top_line_p = 0;
	  row->indicate_eob_p = row->indicate_bottom_line_p = 0;

1011 1012
	  if (!row->mode_line_p)
	    {
1013
	      if (top_ind_rn < 0 && row->visible_height > 0)
1014
		{
1015
		  if (MATRIX_ROW_START_CHARPOS (row) <= BUF_BEGV (XBUFFER (w->contents))
1016
		      && !MATRIX_ROW_PARTIALLY_VISIBLE_AT_TOP_P (w, row))
1017 1018 1019
		    row->indicate_bob_p = !NILP (boundary_top);
		  else
		    row->indicate_top_line_p = !NILP (arrow_top);
1020
		  top_ind_rn = rn;
1021
		}
Kenichi Handa's avatar
Kenichi Handa committed
1022

1023
	      if (bot_ind_rn < 0)
1024
		{
1025
		  if (MATRIX_ROW_END_CHARPOS (row) >= BUF_ZV (XBUFFER (w->contents))
1026
		      && !MATRIX_ROW_PARTIALLY_VISIBLE_AT_BOTTOM_P (w, row))
1027
		    row->indicate_eob_p = !NILP (boundary_bot), bot_ind_rn = rn;
1028
		  else if (y + row->height >= yb)
1029
		    row->indicate_bottom_line_p = !NILP (arrow_bot), bot_ind_rn = rn;
1030 1031
		}
	    }
Kenichi Handa's avatar
Kenichi Handa committed
1032 1033 1034
	}
    }

1035
  empty_pos = BVAR (XBUFFER (w->contents), indicate_empty_lines);
1036 1037
  if (!NILP (empty_pos) && !EQ (empty_pos, Qright))
    empty_pos = WINDOW_LEFT_FRINGE_WIDTH (w) == 0 ? Qright : Qleft;
Kenichi Handa's avatar
Kenichi Handa committed
1038

1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
  for (y = 0; y < MAX_BITMAP_CACHE; y++)
    bitmap_cache[y] = -1;

#define LEFT_FRINGE(cache, which, partial_p)			\
  (bitmap_cache[cache*4+partial_p] >= 0				\
   ? bitmap_cache[cache*4+partial_p]				\
   : (bitmap_cache[cache*4+partial_p] =				\
      get_logical_fringe_bitmap (w, which, 0, partial_p)))

#define RIGHT_FRINGE(cache, which, partial_p)			\
  (bitmap_cache[cache*4+2+partial_p] >= 0			\
   ? bitmap_cache[cache*4+2+partial_p]				\
   : (bitmap_cache[cache*4+2+partial_p] =			\
      get_logical_fringe_bitmap (w, which, 1, partial_p)))


1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
  /* Extend top-aligned top indicator (or bottom-aligned bottom
     indicator) to adjacent rows if it doesn't fit in one row.  */
  top_ind_min_y = bot_ind_max_y = -1;
  if (top_ind_rn >= 0)
    {
      int bn = NO_FRINGE_BITMAP;

      row = w->desired_matrix->rows + top_ind_rn;
      if (!row->enabled_p)
	row = w->current_matrix->rows + top_ind_rn;

      top_row_ends_at_zv_p = row->ends_at_zv_p;
      if (row->indicate_bob_p)
	{
	  if (EQ (boundary_top, Qleft))
	    bn = ((row->indicate_eob_p && EQ (boundary_bot, Qleft))
		  ? LEFT_FRINGE (1, Qtop_bottom, row->ends_at_zv_p)
		  : LEFT_FRINGE (2, Qtop, 0));
	  else
	    bn = ((row->indicate_eob_p && EQ (boundary_bot, Qright))
		  ? RIGHT_FRINGE (1, Qtop_bottom, row->ends_at_zv_p)
		  : RIGHT_FRINGE (2, Qtop, 0));
	}
      else if (row->indicate_top_line_p)
	{
	  if (EQ (arrow_top, Qleft))
	    bn = LEFT_FRINGE (6, Qup, 0);
	  else
	    bn = RIGHT_FRINGE (6, Qup, 0);
	}

      if (bn != NO_FRINGE_BITMAP)
	{
1088
	  struct fringe_bitmap *fb = get_fringe_bitmap_data (bn);
1089 1090 1091 1092 1093 1094

	  if (fb->align == ALIGN_BITMAP_TOP && fb->period == 0)
	    {
	      struct glyph_row *row1;
	      int top_ind_max_y;

1095 1096
	      top_ind_min_y = WINDOW_TAB_LINE_HEIGHT (w)
			      + WINDOW_HEADER_LINE_HEIGHT (w);
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
	      top_ind_max_y = top_ind_min_y + fb->height;
	      if (top_ind_max_y > yb)
		top_ind_max_y = yb;

	      for (y = row->y + row->height, rn = top_ind_rn + 1;
		   y < top_ind_max_y && rn < nrows;
		   y += row1->height, rn++)
		{
		  if (bot_ind_rn >= 0 && rn >= bot_ind_rn)
		    break;

		  row1 = w->desired_matrix->rows + rn;
		  if (!row1->enabled_p)
		    row1 = w->current_matrix->rows + rn;

		  row1->indicate_bob_p = row->indicate_bob_p;
		  row1->indicate_top_line_p = row->indicate_top_line_p;
		}
	    }
	}
    }
  if (bot_ind_rn >= 0)
    {
      int bn = NO_FRINGE_BITMAP;

      row = w->desired_matrix->rows + bot_ind_rn;
      if (!row->enabled_p)
	row = w->current_matrix->rows + bot_ind_rn;

      bot_row_ends_at_zv_p = row->ends_at_zv_p;
      if (row->indicate_eob_p)
	{
	  if (EQ (boundary_bot, Qleft))
	    bn = LEFT_FRINGE (3, Qbottom, row->ends_at_zv_p);
	  else
	    bn = RIGHT_FRINGE (3, Qbottom, row->ends_at_zv_p);
	}
      else if (row->indicate_bottom_line_p)
	{
	  if (EQ (arrow_bot, Qleft))
	    bn = LEFT_FRINGE (7, Qdown, 0);
	  else
	    bn = RIGHT_FRINGE (7, Qdown, 0);
	}

      if (bn != NO_FRINGE_BITMAP)
	{
1144
	  struct fringe_bitmap *fb = get_fringe_bitmap_data (bn);
1145 1146 1147 1148 1149 1150 1151 1152

	  if (fb->align == ALIGN_BITMAP_BOTTOM && fb->period == 0)
	    {
	      struct glyph_row *row1;
	      int bot_ind_min_y;

	      bot_ind_max_y = row->y + row->visible_height;
	      bot_ind_min_y = bot_ind_max_y - fb->height;
1153 1154 1155 1156
	      if (bot_ind_min_y < WINDOW_TAB_LINE_HEIGHT (w)
				  + WINDOW_HEADER_LINE_HEIGHT (w))
		bot_ind_min_y = WINDOW_TAB_LINE_HEIGHT (w)
				+ WINDOW_HEADER_LINE_HEIGHT (w);
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176

	      for (y = row->y, rn = bot_ind_rn - 1;
		   y >= bot_ind_min_y && rn >= 0;
		   y -= row1->height, rn--)
		{
		  if (top_ind_rn >= 0 && rn <= top_ind_rn)
		    break;

		  row1 = w->desired_matrix->rows + rn;
		  if (!row1->enabled_p)
		    row1 = w->current_matrix->rows + rn;

		  row1->indicate_eob_p = row->indicate_eob_p;
		  row1->indicate_bottom_line_p = row->indicate_bottom_line_p;
		}
	    }
	}
    }

  for (y = w->vscroll, rn = 0;
Kenichi Handa's avatar
Kenichi Handa committed
1177 1178 1179
       y < yb && rn < nrows;
       y += row->height, rn++)
    {
1180
      int left, right;
Kenichi Handa's avatar
Kenichi Handa committed
1181
      unsigned left_face_id, right_face_id;
1182
      int left_offset, right_offset;
1183
      bool periodic_p;
Kenichi Handa's avatar
Kenichi Handa committed
1184 1185 1186 1187 1188 1189 1190

      row = w->desired_matrix->rows + rn;
      cur = w->current_matrix->rows + rn;
      if (!row->enabled_p)
	row = cur;

      left_face_id = right_face_id = DEFAULT_FACE_ID;
1191
      left_offset = right_offset = 0;
1192
      periodic_p = 0;
Kenichi Handa's avatar
Kenichi Handa committed
1193 1194 1195 1196 1197 1198 1199 1200 1201

      /* Decide which bitmap to draw in the left fringe.  */
      if (WINDOW_LEFT_FRINGE_WIDTH (w) == 0)
	left = NO_FRINGE_BITMAP;
      else if (row->left_user_fringe_bitmap != NO_FRINGE_BITMAP)
	{
	  left = row->left_user_fringe_bitmap;
	  left_face_id = row->left_user_fringe_face_id;
	}
1202 1203
      else if ((!row->reversed_p && row->truncated_on_left_p)
	       || (row->reversed_p && row->truncated_on_right_p))
Juanma Barranquero's avatar
Juanma Barranquero committed
1204
	left = LEFT_FRINGE (0, Qtruncation, 0);
1205
      else if (row->indicate_bob_p && EQ (boundary_top, Qleft))
1206 1207 1208 1209 1210 1211 1212
	{
	  left = ((row->indicate_eob_p && EQ (boundary_bot, Qleft))
		  ? LEFT_FRINGE (1, Qtop_bottom, top_row_ends_at_zv_p)
		  : LEFT_FRINGE (2, Qtop, 0));
	  if (top_ind_min_y >= 0)
	    left_offset = top_ind_min_y - row->y;
	}
1213
      else if (row->indicate_eob_p && EQ (boundary_bot, Qleft))
1214 1215 1216 1217 1218
	{
	  left = LEFT_FRINGE (3, Qbottom, bot_row_ends_at_zv_p);
	  if (bot_ind_max_y >= 0)
	    left_offset = bot_ind_max_y - (row->y + row->visible_height);
	}
1219 1220
      else if ((!row->reversed_p && MATRIX_ROW_CONTINUATION_LINE_P (row))
	       || (row->reversed_p && row->continued_p))
1221
	left = LEFT_FRINGE (4, Qcontinuation, 0);
1222
      else if (row->indicate_empty_line_p && EQ (empty_pos, Qleft))
1223
	left = LEFT_FRINGE (5, Qempty_line, 0);
1224
      else if (row->indicate_top_line_p && EQ (arrow_top, Qleft))
1225 1226 1227 1228 1229
	{
	  left = LEFT_FRINGE (6, Qup, 0);
	  if (top_ind_min_y >= 0)
	    left_offset = top_ind_min_y - row->y;
	}
1230
      else if (row->indicate_bottom_line_p && EQ (arrow_bot, Qleft))
1231 1232 1233 1234 1235
	{
	  left = LEFT_FRINGE (7, Qdown, 0);
	  if (bot_ind_max_y >= 0)
	    left_offset = bot_ind_max_y - (row->y + row->visible_height);
	}
Kenichi Handa's avatar
Kenichi Handa committed
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
      else
	left = NO_FRINGE_BITMAP;

      /* Decide which bitmap to draw in the right fringe.  */
      if (WINDOW_RIGHT_FRINGE_WIDTH (w) == 0)
	right = NO_FRINGE_BITMAP;
      else if (row->right_user_fringe_bitmap != NO_FRINGE_BITMAP)
	{
	  right = row->right_user_fringe_bitmap;
	  right_face_id = row->right_user_fringe_face_id;
	}
1247 1248
      else if ((!row->reversed_p && row->truncated_on_right_p)
	       || (row->reversed_p && row->truncated_on_left_p))
1249
	right = RIGHT_FRINGE (0, Qtruncation, 0);
1250
      else if (row->indicate_bob_p && EQ (boundary_top, Qright))
1251 1252 1253 1254 1255 1256 1257
	{
	  right = ((row->indicate_eob_p && EQ (boundary_bot, Qright))
		   ? RIGHT_FRINGE (1, Qtop_bottom, top_row_ends_at_zv_p)
		   : RIGHT_FRINGE (2, Qtop, 0));
	  if (top_ind_min_y >= 0)
	    right_offset = top_ind_min_y - row->y;
	}
1258
      else if (row->indicate_eob_p && EQ (boundary_bot, Qright))
1259 1260 1261 1262 1263
	{
	  right = RIGHT_FRINGE (3, Qbottom, bot_row_ends_at_zv_p);
	  if (bot_ind_max_y >= 0)
	    right_offset = bot_ind_max_y - (row->y + row->visible_height);
	}
1264 1265
      else if ((!row->reversed_p && row->continued_p)
	       || (row->reversed_p && MATRIX_ROW_CONTINUATION_LINE_P (row)))
1266
	right = RIGHT_FRINGE (4, Qcontinuation, 0);
1267
      else if (row->indicate_top_line_p && EQ (arrow_top, Qright))
1268 1269 1270 1271 1272
	{
	  right = RIGHT_FRINGE (6, Qup, 0);
	  if (top_ind_min_y >= 0)
	    right_offset = top_ind_min_y - row->y;
	}
1273
      else if (row->indicate_bottom_line_p && EQ (arrow_bot, Qright))
1274 1275 1276 1277 1278
	{
	  right = RIGHT_FRINGE (7, Qdown, 0);
	  if (bot_ind_max_y >= 0)
	    right_offset = bot_ind_max_y - (row->y + row->visible_height);
	}
1279
      else if (row->indicate_empty_line_p && EQ (empty_pos, Qright))
1280
	right = RIGHT_FRINGE (5, Qempty_line, 0);
Kenichi Handa's avatar
Kenichi Handa committed
1281 1282 1283
      else
	right = NO_FRINGE_BITMAP;

1284 1285 1286
      periodic_p = (get_fringe_bitmap_data (left)->period != 0
		    || get_fringe_bitmap_data (right)->period != 0);

1287
      if (row->y != cur->y
Kenichi Handa's avatar
Kenichi Handa committed
1288
	  || row->visible_height != cur->visible_height
1289
	  || row->ends_at_zv_p != cur->ends_at_zv_p
Kenichi Handa's avatar
Kenichi Handa committed
1290 1291 1292 1293
	  || left != cur->left_fringe_bitmap
	  || right != cur->right_fringe_bitmap
	  || left_face_id != cur->left_fringe_face_id
	  || right_face_id != cur->right_fringe_face_id
1294 1295
	  || left_offset != cur->left_fringe_offset
	  || right_offset != cur->right_fringe_offset
1296
	  || periodic_p != cur->fringe_bitmap_periodic_p
Kenichi Handa's avatar
Kenichi Handa committed
1297 1298
	  || cur->redraw_fringe_bitmaps_p)
	{
1299
	  redraw_p = 1, row->redraw_fringe_bitmaps_p = 1;
1300 1301 1302 1303 1304 1305 1306
	  if (!keep_current_p)
	    {
	      cur->redraw_fringe_bitmaps_p = 1;
	      cur->left_fringe_bitmap = left;
	      cur->right_fringe_bitmap = right;
	      cur->left_fringe_face_id = left_face_id;
	      cur->right_fringe_face_id = right_face_id;
1307 1308
	      cur->left_fringe_offset = left_offset;
	      cur->right_fringe_offset = right_offset;
1309
	      cur->fringe_bitmap_periodic_p = periodic_p;
1310
	    }
Kenichi Handa's avatar
Kenichi Handa committed
1311 1312
	}

1313 1314 1315
      if (row->overlay_arrow_bitmap < 0)
	row->overlay_arrow_bitmap = get_logical_fringe_bitmap (w, Qoverlay_arrow, 0, 0);

1316
      if (row->overlay_arrow_bitmap != cur->overlay_arrow_bitmap)
Kenichi Handa's avatar
Kenichi Handa committed
1317
	{
1318
	  redraw_p = 1, row->redraw_fringe_bitmaps_p = 1;
1319 1320 1321 1322 1323
	  if (!keep_current_p)
	    {
	      cur->redraw_fringe_bitmaps_p = 1;
	      cur->overlay_arrow_bitmap = row->overlay_arrow_bitmap;
	    }
Kenichi Handa's avatar
Kenichi Handa committed
1324 1325 1326 1327 1328 1329
	}

      row->left_fringe_bitmap = left;
      row->right_fringe_bitmap = right;
      row->left_fringe_face_id = left_face_id;
      row->right_fringe_face_id = right_face_id;
1330 1331
      row->left_fringe_offset = left_offset;
      row->right_fringe_offset = right_offset;
1332
      row->fringe_bitmap_periodic_p = periodic_p;
Kenichi Handa's avatar
Kenichi Handa committed
1333 1334
    }

1335
  return redraw_p && !keep_current_p;
Kenichi Handa's avatar
Kenichi Handa committed
1336 1337 1338
}


1339

Kim F. Storm's avatar
Kim F. Storm committed
1340 1341
/* Free resources used by a user-defined bitmap.  */

1342
static void
1343
destroy_fringe_bitmap (int n)
Kenichi Handa's avatar
Kenichi Handa committed
1344 1345 1346
{
  struct fringe_bitmap **fbp;

1347
  fringe_faces[n] = Qnil;
Kenichi Handa's avatar
Kenichi Handa committed
1348 1349 1350 1351

  fbp = &fringe_bitmaps[n];
  if (*fbp && (*fbp)->dynamic)
    {
1352
      /* XXX Is SELECTED_FRAME OK here? */
1353
      struct redisplay_interface *rif = FRAME_RIF (SELECTED_FRAME ());
1354
      if (rif && rif->destroy_fringe_bitmap)
Kenichi Handa's avatar
Kenichi Handa committed
1355 1356 1357 1358 1359 1360 1361 1362
	rif->destroy_fringe_bitmap (n);
      xfree (*fbp);
      *fbp = NULL;
    }

  while (max_used_fringe_bitmap > MAX_STANDARD_FRINGE_BITMAPS
	 && fringe_bitmaps[max_used_fringe_bitmap - 1] == NULL)
    max_used_fringe_bitmap--;
1363 1364
}

Kenichi Handa's avatar
Kenichi Handa committed
1365

1366 1367 1368 1369
DEFUN ("destroy-fringe-bitmap", Fdestroy_fringe_bitmap, Sdestroy_fringe_bitmap,
       1, 1, 0,
       doc: /* Destroy fringe bitmap BITMAP.
If BITMAP overrides a standard fringe bitmap, the original bitmap is restored.  */)
1370
  (Lisp_Object bitmap)
1371 1372 1373
{
  int n;

Kim F. Storm's avatar
Kim F. Storm committed
1374 1375 1376
  CHECK_SYMBOL (bitmap);
  n = lookup_fringe_bitmap (bitmap);
  if (!n)
1377 1378 1379
    return Qnil;

  destroy_fringe_bitmap (n);
1380

Kim F. Storm's avatar
Kim F. Storm committed
1381
  if (n >= MAX_STANDARD_FRINGE_BITMAPS)
1382
    {
Kim F. Storm's avatar
Kim F. Storm committed
1383
      Vfringe_bitmaps = Fdelq (bitmap, Vfringe_bitmaps);
1384
      /* It would be better to remove the fringe property.  */
Kim F. Storm's avatar
Kim F. Storm committed
1385
      Fput (bitmap, Qfringe, Qnil);
1386
    }
Kim F. Storm's avatar
Kim F. Storm committed
1387

Kenichi Handa's avatar
Kenichi Handa committed
1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401
  return Qnil;
}


/* Initialize bitmap bit.

   On X, we bit-swap the built-in bitmaps and reduce bitmap
   from short to char array if width is <= 8 bits.

   On MAC with big-endian CPU, we need to byte-swap each short.

   On W32 and MAC (little endian), there's no need to do this.
*/

1402
#if defined (HAVE_X_WINDOWS)
1403
static const unsigned char swap_nibble[16] = {
1404 1405 1406 1407 1408 1409
  0x0, 0x8, 0x4, 0xc,           /* 0000 1000 0100 1100 */
  0x2, 0xa, 0x6, 0xe,           /* 0010 1010 0110 1110 */
  0x1, 0x9, 0x5, 0xd,           /* 0001 1001 0101 1101 */
  0x3, 0xb, 0x7, 0xf};          /* 0011 1011 0111 1111 */
#endif                          /* HAVE_X_WINDOWS */

1410
static void
1411
init_fringe_bitmap (int which, struct fringe_bitmap *fb, int once_p)
Kenichi Handa's avatar
Kenichi Handa committed
1412 1413 1414 1415 1416 1417 1418
{
  if (once_p || fb->dynamic)
    {
#if defined (HAVE_X_WINDOWS)
      unsigned short *bits = fb->bits;
      int j;

1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433
#ifdef USE_CAIRO
      for (j = 0; j < fb->height; j++)
	{
	  unsigned short b = *bits;
#ifdef WORDS_BIGENDIAN
	  *bits++ = (b << (16 - fb->width));
#else
	  b = (unsigned short)((swap_nibble[b & 0xf] << 12)
			       | (swap_nibble[(b>>4) & 0xf] << 8)
			       | (swap_nibble[(b>>8) & 0xf] << 4)
			       | (swap_nibble[(b>>12) & 0xf]));
	  *bits++ = (b >> (16 - fb->width));
#endif
	}
#else  /* not USE_CAIRO */
Kenichi Handa's avatar
Kenichi Handa committed
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454
      if (fb->width <= 8)
	{
	  unsigned char *cbits = (unsigned char *)fb->bits;
	  for (j = 0; j < fb->height; j++)
	    {
	      unsigned short b = *bits++;
	      unsigned char c;
	      c = (unsigned char)((swap_nibble[b & 0xf] << 4)
				  | (swap_nibble[(b>>4) & 0xf]));
	      *cbits++ = (c >> (8 - fb->width));
	    }
	}
      else
	{
	  for (j = 0; j < fb->height; j++)
	    {
	      unsigned short b = *bits;
	      b = (unsigned short)((swap_nibble[b & 0xf] << 12)
				   | (swap_nibble[(b>>4) & 0xf] << 8)
				   | (swap_nibble[(b>>8) & 0xf] << 4)
				   | (swap_nibble[(b>>12) & 0xf]));
1455
	      b >>= (16 - fb->width);
1456
#ifdef WORDS_BIGENDIAN
1457
	      b = bswap_16 (b);