msdos.c 130 KB
Newer Older
1
/* MS-DOS specific C utilities.          -*- coding: raw-text -*-
2
   Copyright (C) 1993, 94, 95, 96, 97, 1999 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
3 4 5 6 7

This file is part of GNU Emacs.

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

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
along with GNU Emacs; see the file COPYING.  If not, write to
18 19
the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */
Richard M. Stallman's avatar
Richard M. Stallman committed
20

21
/* Contributed by Morten Welinder */
Kim F. Storm's avatar
Kim F. Storm committed
22
/* New display, keyboard, and mouse control by Kim F. Storm */
23

Richard M. Stallman's avatar
Richard M. Stallman committed
24 25
/* Note: some of the stuff here was taken from end of sysdep.c in demacs. */

Richard M. Stallman's avatar
Richard M. Stallman committed
26
#include <config.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
27 28 29 30 31

#ifdef MSDOS
#include "lisp.h"
#include <stdio.h>
#include <stdlib.h>
32
#include <time.h>
Richard M. Stallman's avatar
Richard M. Stallman committed
33 34 35
#include <sys/param.h>
#include <sys/time.h>
#include <dos.h>
36
#include <errno.h>
37
#include <string.h>	 /* for bzero and string functions */
38
#include <sys/stat.h>    /* for _fixpath */
39
#include <unistd.h>	 /* for chdir, dup, dup2, etc. */
40 41
#if __DJGPP__ >= 2
#include <fcntl.h>
42
#include <io.h>		 /* for setmode */
43 44
#include <dpmi.h>	 /* for __dpmi_xxx stuff */
#include <sys/farptr.h>	 /* for _farsetsel, _farnspokeb */
45
#include <libc/dosio.h>  /* for _USE_LFN */
46
#include <conio.h>	 /* for cputs */
47 48
#endif

Richard M. Stallman's avatar
Richard M. Stallman committed
49 50 51
#include "msdos.h"
#include "systime.h"
#include "termhooks.h"
52
#include "termchar.h"
Morten Welinder's avatar
Morten Welinder committed
53
#include "dispextern.h"
54
#include "dosfns.h"
Morten Welinder's avatar
Morten Welinder committed
55
#include "termopts.h"
56 57 58
#include "charset.h"
#include "coding.h"
#include "disptab.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
59
#include "frame.h"
Morten Welinder's avatar
Morten Welinder committed
60
#include "window.h"
61 62
#include "buffer.h"
#include "commands.h"
63
#include "blockinput.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
64 65 66 67 68 69 70
#include <go32.h>
#include <pc.h>
#include <ctype.h>
/* #include <process.h> */
/* Damn that local process.h!  Instead we can define P_WAIT ourselves.  */
#define P_WAIT 1

71 72 73 74
#ifndef _USE_LFN
#define _USE_LFN 0
#endif

75 76 77 78
#ifndef _dos_ds
#define _dos_ds _go32_info_block.selector_for_linear_memory
#endif

79 80
#if __DJGPP__ > 1

Richard M. Stallman's avatar
Richard M. Stallman committed
81
#include <signal.h>
82
#include "syssignal.h"
Richard M. Stallman's avatar
Richard M. Stallman committed
83

84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
#ifndef SYSTEM_MALLOC

#ifdef GNU_MALLOC

/* If other `malloc' than ours is used, force our `sbrk' behave like
   Unix programs expect (resize memory blocks to keep them contiguous).
   If `sbrk' from `ralloc.c' is NOT used, also zero-out sbrk'ed memory,
   because that's what `gmalloc' expects to get.  */
#include <crt0.h>

#ifdef REL_ALLOC
int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
#else  /* not REL_ALLOC */
int _crt0_startup_flags = (_CRT0_FLAG_UNIX_SBRK | _CRT0_FLAG_FILL_SBRK_MEMORY);
#endif /* not REL_ALLOC */
#endif /* GNU_MALLOC */

#endif /* not SYSTEM_MALLOC */
#endif /* __DJGPP__ > 1 */
103 104 105 106 107 108

static unsigned long
event_timestamp ()
{
  struct time t;
  unsigned long s;
Kim F. Storm's avatar
Kim F. Storm committed
109
  
110 111 112 113 114 115
  gettime (&t);
  s = t.ti_min;
  s *= 60;
  s += t.ti_sec;
  s *= 1000;
  s += t.ti_hund * 10;
Kim F. Storm's avatar
Kim F. Storm committed
116
  
117 118 119
  return s;
}

Kim F. Storm's avatar
Kim F. Storm committed
120 121 122 123 124 125

/* ------------------------ Mouse control ---------------------------
 *
 * Coordinates are in screen positions and zero based.
 * Mouse buttons are numbered from left to right and also zero based.
 */
Richard M. Stallman's avatar
Richard M. Stallman committed
126

Kim F. Storm's avatar
Kim F. Storm committed
127 128
int have_mouse;          /* 0: no, 1: enabled, -1: disabled */
static int mouse_visible;
Richard M. Stallman's avatar
Richard M. Stallman committed
129

Kim F. Storm's avatar
Kim F. Storm committed
130 131
static int mouse_last_x;
static int mouse_last_y;
Richard M. Stallman's avatar
Richard M. Stallman committed
132

Kim F. Storm's avatar
Kim F. Storm committed
133 134
static int mouse_button_translate[NUM_MOUSE_BUTTONS];
static int mouse_button_count;
Richard M. Stallman's avatar
Richard M. Stallman committed
135

Kim F. Storm's avatar
Kim F. Storm committed
136 137
void
mouse_on ()
Richard M. Stallman's avatar
Richard M. Stallman committed
138 139 140
{
  union REGS regs;

Kim F. Storm's avatar
Kim F. Storm committed
141
  if (have_mouse > 0 && !mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
142
    {
Kim F. Storm's avatar
Kim F. Storm committed
143 144 145 146 147
      if (termscript)
	fprintf (termscript, "<M_ON>");
      regs.x.ax = 0x0001;
      int86 (0x33, &regs, &regs);
      mouse_visible = 1;
Richard M. Stallman's avatar
Richard M. Stallman committed
148 149 150
    }
}

Kim F. Storm's avatar
Kim F. Storm committed
151 152
void
mouse_off ()
Richard M. Stallman's avatar
Richard M. Stallman committed
153
{
Kim F. Storm's avatar
Kim F. Storm committed
154
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
155

Kim F. Storm's avatar
Kim F. Storm committed
156
  if (have_mouse > 0 && mouse_visible)
Richard M. Stallman's avatar
Richard M. Stallman committed
157
    {
Kim F. Storm's avatar
Kim F. Storm committed
158 159 160 161 162
      if (termscript)
	fprintf (termscript, "<M_OFF>");
      regs.x.ax = 0x0002;
      int86 (0x33, &regs, &regs);
      mouse_visible = 0;
Richard M. Stallman's avatar
Richard M. Stallman committed
163 164 165
    }
}

166 167 168 169 170 171 172 173 174 175 176
static void
mouse_get_xy (int *x, int *y)
{
  union REGS regs;

  regs.x.ax = 0x0003;
  int86 (0x33, &regs, &regs);
  *x = regs.x.cx / 8;
  *y = regs.x.dx / 8;
}

Kim F. Storm's avatar
Kim F. Storm committed
177 178 179
void
mouse_moveto (x, y)
     int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
180
{
Kim F. Storm's avatar
Kim F. Storm committed
181
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
182

Kim F. Storm's avatar
Kim F. Storm committed
183 184 185 186 187 188
  if (termscript)
    fprintf (termscript, "<M_XY=%dx%d>", x, y);
  regs.x.ax = 0x0004;
  mouse_last_x = regs.x.cx = x * 8;
  mouse_last_y = regs.x.dx = y * 8;
  int86 (0x33, &regs, &regs);
Richard M. Stallman's avatar
Richard M. Stallman committed
189 190
}

Kim F. Storm's avatar
Kim F. Storm committed
191 192 193
static int
mouse_pressed (b, xp, yp)
     int b, *xp, *yp;
Richard M. Stallman's avatar
Richard M. Stallman committed
194
{
Kim F. Storm's avatar
Kim F. Storm committed
195
  union REGS regs;
Richard M. Stallman's avatar
Richard M. Stallman committed
196

Kim F. Storm's avatar
Kim F. Storm committed
197 198 199 200 201 202 203 204
  if (b >= mouse_button_count)
    return 0;
  regs.x.ax = 0x0005;
  regs.x.bx = mouse_button_translate[b];
  int86 (0x33, &regs, &regs);
  if (regs.x.bx)
    *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
  return (regs.x.bx != 0);
Richard M. Stallman's avatar
Richard M. Stallman committed
205 206
}

Kim F. Storm's avatar
Kim F. Storm committed
207 208 209
static int
mouse_released (b, xp, yp)
     int b, *xp, *yp;
Richard M. Stallman's avatar
Richard M. Stallman committed
210 211 212
{
  union REGS regs;

Kim F. Storm's avatar
Kim F. Storm committed
213 214 215 216 217 218 219 220
  if (b >= mouse_button_count)
    return 0;
  regs.x.ax = 0x0006;
  regs.x.bx = mouse_button_translate[b];
  int86 (0x33, &regs, &regs);
  if (regs.x.bx)
    *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
  return (regs.x.bx != 0);
Richard M. Stallman's avatar
Richard M. Stallman committed
221 222
}

223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
static int
mouse_button_depressed (b, xp, yp)
     int b, *xp, *yp;
{
  union REGS regs;

  if (b >= mouse_button_count)
    return 0;
  regs.x.ax = 0x0003;
  int86 (0x33, &regs, &regs);
  if ((regs.x.bx & (1 << mouse_button_translate[b])) != 0)
    {
      *xp = regs.x.cx / 8;
      *yp = regs.x.dx / 8;
      return 1;
    }
  return 0;
}

Kim F. Storm's avatar
Kim F. Storm committed
242 243 244 245 246 247 248 249 250
void
mouse_get_pos (f, insist, bar_window, part, x, y, time)
     FRAME_PTR *f;
     int insist;
     Lisp_Object *bar_window, *x, *y;
     enum scroll_bar_part *part;
     unsigned long *time;
{
  int ix, iy;
251 252 253 254 255
  Lisp_Object frame, tail;

  /* Clear the mouse-moved flag for every frame on this display.  */
  FOR_EACH_FRAME (tail, frame)
    XFRAME (frame)->mouse_moved = 0;
Kim F. Storm's avatar
Kim F. Storm committed
256

257
  *f = SELECTED_FRAME();
Kim F. Storm's avatar
Kim F. Storm committed
258 259 260
  *bar_window = Qnil;
  mouse_get_xy (&ix, &iy);
  *time = event_timestamp ();
261 262
  *x = make_number (mouse_last_x = ix);
  *y = make_number (mouse_last_y = iy);
Kim F. Storm's avatar
Kim F. Storm committed
263
}
Richard M. Stallman's avatar
Richard M. Stallman committed
264

Kim F. Storm's avatar
Kim F. Storm committed
265 266
static void
mouse_check_moved ()
Richard M. Stallman's avatar
Richard M. Stallman committed
267
{
268
  int x, y;
Richard M. Stallman's avatar
Richard M. Stallman committed
269

Kim F. Storm's avatar
Kim F. Storm committed
270
  mouse_get_xy (&x, &y);
271
  SELECTED_FRAME()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
Kim F. Storm's avatar
Kim F. Storm committed
272 273 274
  mouse_last_x = x;
  mouse_last_y = y;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
275

Kim F. Storm's avatar
Kim F. Storm committed
276 277 278 279
void
mouse_init ()
{
  union REGS regs;
280
  int b;
281

Kim F. Storm's avatar
Kim F. Storm committed
282 283
  if (termscript)
    fprintf (termscript, "<M_INIT>");
Richard M. Stallman's avatar
Richard M. Stallman committed
284

Kim F. Storm's avatar
Kim F. Storm committed
285 286
  regs.x.ax = 0x0021;
  int86 (0x33, &regs, &regs);
287

288 289 290 291 292 293 294 295 296 297 298 299
  /* Reset the mouse last press/release info.  It seems that Windows
     doesn't do that automatically when function 21h is called, which
     causes Emacs to ``remember'' the click that switched focus to the
     window just before Emacs was started from that window.  */
  for (b = 0; b < mouse_button_count; b++)
    {
      int dummy_x, dummy_y;

      (void) mouse_pressed (b, &dummy_x, &dummy_y);
      (void) mouse_released (b, &dummy_x, &dummy_y);
    }

Kim F. Storm's avatar
Kim F. Storm committed
300 301 302 303
  regs.x.ax = 0x0007;
  regs.x.cx = 0;
  regs.x.dx = 8 * (ScreenCols () - 1);
  int86 (0x33, &regs, &regs);
Richard M. Stallman's avatar
Richard M. Stallman committed
304

Kim F. Storm's avatar
Kim F. Storm committed
305 306 307 308
  regs.x.ax = 0x0008;
  regs.x.cx = 0;
  regs.x.dx = 8 * (ScreenRows () - 1);
  int86 (0x33, &regs, &regs);
Richard M. Stallman's avatar
Richard M. Stallman committed
309

Kim F. Storm's avatar
Kim F. Storm committed
310 311 312
  mouse_moveto (0, 0);
  mouse_visible = 0;
}
Richard M. Stallman's avatar
Richard M. Stallman committed
313

Kim F. Storm's avatar
Kim F. Storm committed
314 315 316
/* ------------------------- Screen control ----------------------
 *
 */
317

Kim F. Storm's avatar
Kim F. Storm committed
318
static int internal_terminal = 0;
319

Kim F. Storm's avatar
Kim F. Storm committed
320 321 322 323
#ifndef HAVE_X_WINDOWS
extern unsigned char ScreenAttrib;
static int screen_face;
static int highlight;
324

Kim F. Storm's avatar
Kim F. Storm committed
325 326 327
static int screen_size_X;
static int screen_size_Y;
static int screen_size;
Richard M. Stallman's avatar
Richard M. Stallman committed
328

Kim F. Storm's avatar
Kim F. Storm committed
329 330 331 332
static int current_pos_X;
static int current_pos_Y;
static int new_pos_X;
static int new_pos_Y;
Richard M. Stallman's avatar
Richard M. Stallman committed
333

Kim F. Storm's avatar
Kim F. Storm committed
334 335 336 337 338
static void *startup_screen_buffer;
static int startup_screen_size_X;
static int startup_screen_size_Y;
static int startup_pos_X;
static int startup_pos_Y;
339
static unsigned char startup_screen_attrib;
Richard M. Stallman's avatar
Richard M. Stallman committed
340

341 342
static clock_t startup_time;

Kim F. Storm's avatar
Kim F. Storm committed
343
static int term_setup_done;
Richard M. Stallman's avatar
Richard M. Stallman committed
344

Kim F. Storm's avatar
Kim F. Storm committed
345
/* Similar to the_only_frame.  */
346
struct x_output the_only_x_display;
Richard M. Stallman's avatar
Richard M. Stallman committed
347

348 349 350 351 352 353 354 355
/* Support for DOS/V (allows Japanese characters to be displayed on
   standard, non-Japanese, ATs).  Only supported for DJGPP v2 and later.  */

/* Holds the address of the text-mode screen buffer.  */
static unsigned long screen_old_address = 0;
/* Segment and offset of the virtual screen.  If 0, DOS/V is NOT loaded.  */
static unsigned short screen_virtual_segment = 0;
static unsigned short screen_virtual_offset = 0;
356 357
/* A flag to control how to display unibyte 8-bit characters.  */
extern int unibyte_display_via_language_environment;
358 359 360 361 362 363 364 365 366

#if __DJGPP__ > 1
/* Update the screen from a part of relocated DOS/V screen buffer which
   begins at OFFSET and includes COUNT characters.  */
static void
dosv_refresh_virtual_screen (int offset, int count)
{
  __dpmi_regs regs;

367 368 369
  if (offset < 0 || count < 0)	/* paranoia; illegal values crash DOS/V */
    return;

370 371 372 373 374 375 376 377
  regs.h.ah = 0xff;	/* update relocated screen */
  regs.x.es = screen_virtual_segment;
  regs.x.di = screen_virtual_offset + offset;
  regs.x.cx = count;
  __dpmi_int (0x10, &regs);
}
#endif

378
static void
Kim F. Storm's avatar
Kim F. Storm committed
379 380 381 382 383
dos_direct_output (y, x, buf, len)
     int y;
     int x;
     char *buf;
     int len;
Richard M. Stallman's avatar
Richard M. Stallman committed
384
{
385 386
  int t0 = 2 * (x + y * screen_size_X);
  int t = t0 + (int) ScreenPrimary;
387
  int l0 = len;
388 389

#if (__DJGPP__ < 2)
Kim F. Storm's avatar
Kim F. Storm committed
390 391 392 393
  while (--len >= 0) {
    dosmemput (buf++, 1, t);
    t += 2;
  }
394 395 396 397
#else
  /* This is faster.  */
  for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
    _farnspokeb (t, *buf);
398 399 400

  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (t0, l0);
401
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
402
}
403
#endif
Richard M. Stallman's avatar
Richard M. Stallman committed
404 405 406

/* Flash the screen as a substitute for BEEPs.  */

Kim F. Storm's avatar
Kim F. Storm committed
407
#if (__DJGPP__ < 2)
408
static void
409
do_visible_bell (xorattr)
Richard M. Stallman's avatar
Richard M. Stallman committed
410 411
     unsigned char xorattr;
{
412
  asm volatile
413
    ("  movb   $1,%%dl
Richard M. Stallman's avatar
Richard M. Stallman committed
414
visible_bell_0:
415
	movl   _ScreenPrimary,%%eax
416
	call   dosmemsetup
417 418 419 420
	movl   %%eax,%%ebx
	movl   %1,%%ecx
	movb   %0,%%al
	incl   %%ebx
Richard M. Stallman's avatar
Richard M. Stallman committed
421
visible_bell_1:
422 423 424
	xorb   %%al,%%gs:(%%ebx)
	addl   $2,%%ebx
	decl   %%ecx
425
	jne    visible_bell_1
426
	decb   %%dl
427
	jne    visible_bell_3
Richard M. Stallman's avatar
Richard M. Stallman committed
428
visible_bell_2:
429 430 431 432 433
	movzwl %%ax,%%eax
        movzwl %%ax,%%eax
	movzwl %%ax,%%eax
	movzwl %%ax,%%eax
	decw   %%cx
434 435
	jne    visible_bell_2
	jmp    visible_bell_0
436 437
visible_bell_3:"
     : /* no output */
Kim F. Storm's avatar
Kim F. Storm committed
438
     : "m" (xorattr), "g" (screen_size)
439
     : "%eax", "%ebx", /* "%gs",*/ "%ecx", "%edx");
Richard M. Stallman's avatar
Richard M. Stallman committed
440 441
}

Kim F. Storm's avatar
Kim F. Storm committed
442 443 444 445 446 447 448 449 450 451 452 453 454
static void
ScreenVisualBell (void)
{
  /* This creates an xor-mask that will swap the default fore- and
     background colors.  */
  do_visible_bell (((the_only_x_display.foreground_pixel
		     ^ the_only_x_display.background_pixel)
		    * 0x11) & 0x7f);
}
#endif

#ifndef HAVE_X_WINDOWS

455 456
static int blink_bit = -1;	/* the state of the blink bit at startup */

457 458 459 460 461 462
/* Enable bright background colors.  */
static void
bright_bg (void)
{
  union REGS regs;

463 464 465 466 467
  /* Remember the original state of the blink/bright-background bit.
     It is stored at 0040:0065h in the BIOS data area.  */
  if (blink_bit == -1)
    blink_bit = (_farpeekb (_dos_ds, 0x465) & 0x20) == 0x20;

468 469 470 471 472
  regs.h.bl = 0;
  regs.x.ax = 0x1003;
  int86 (0x10, &regs, &regs);
}

473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
/* Disable bright background colors (and enable blinking) if we found
   the video system in that state at startup.  */
static void
maybe_enable_blinking (void)
{
  if (blink_bit == 1)
    {
      union REGS regs;

      regs.h.bl = 1;
      regs.x.ax = 0x1003;
      int86 (0x10, &regs, &regs);
    }
}

488 489
/* Set the screen dimensions so that it can show no less than
   ROWS x COLS frame.  */
490

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
void
dos_set_window_size (rows, cols)
     int *rows, *cols;
{
  char video_name[30];
  Lisp_Object video_mode;
  int video_mode_value;
  int have_vga = 0;
  union REGS regs;
  int current_rows = ScreenRows (), current_cols = ScreenCols ();

  if (*rows == current_rows && *cols == current_cols)
    return;

  /* Do we have a VGA?  */
  regs.x.ax = 0x1a00;
  int86 (0x10, &regs, &regs);
  if (regs.h.al == 0x1a && regs.h.bl > 5 && regs.h.bl < 13)
    have_vga = 1;

  mouse_off ();

513
  /* If the user specified a special video mode for these dimensions,
514 515 516 517 518 519 520 521 522 523
     use that mode.  */
  sprintf (video_name, "screen-dimensions-%dx%d", *rows, *cols);
  video_mode = XSYMBOL (Fintern_soft (build_string (video_name),
				      Qnil))-> value;

  if (INTEGERP (video_mode)
      && (video_mode_value = XINT (video_mode)) > 0)
    {
      regs.x.ax = video_mode_value;
      int86 (0x10, &regs, &regs);
524 525 526 527 528 529 530 531 532

      if (have_mouse)
	{
	  /* Must hardware-reset the mouse, or else it won't update
	     its notion of screen dimensions for some non-standard
	     video modes.  This is *painfully* slow...  */
	  regs.x.ax = 0;
	  int86 (0x33, &regs, &regs);
	}
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    }

  /* Find one of the dimensions supported by standard EGA/VGA
     which gives us at least the required dimensions.  */

#if __DJGPP__ > 1

  else
    {
      static struct {
	int rows;
	int need_vga;
      }	std_dimension[] = {
	  {25, 0},
	  {28, 1},
	  {35, 0},
	  {40, 1},
	  {43, 0},
	  {50, 1}
      };
      int i = 0;

      while (i < sizeof (std_dimension) / sizeof (std_dimension[0]))
	{
	 if (std_dimension[i].need_vga <= have_vga
	     && std_dimension[i].rows >= *rows)
	   {
	     if (std_dimension[i].rows != current_rows
		 || *cols != current_cols)
562
	       _set_screen_lines (std_dimension[i].rows);
563 564
	     break;
	   }
565
	 i++;
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
	}
    }

#else /* not __DJGPP__ > 1 */

  else if (*rows <= 25)
    {
      if (current_rows != 25 || current_cols != 80)
	{
	  regs.x.ax = 3;
	  int86 (0x10, &regs, &regs);
	  regs.x.ax = 0x1101;
	  regs.h.bl = 0;
	  int86 (0x10, &regs, &regs);
	  regs.x.ax = 0x1200;
	  regs.h.bl = 32;
	  int86 (0x10, &regs, &regs);
	  regs.x.ax = 3;
	  int86 (0x10, &regs, &regs);
	}
    }
  else if (*rows <= 50)
    if (have_vga && (current_rows != 50 || current_cols != 80)
	|| *rows <= 43 && (current_rows != 43 || current_cols != 80))
      {
	regs.x.ax = 3;
	int86 (0x10, &regs, &regs);
	regs.x.ax = 0x1112;
	regs.h.bl = 0;
	int86 (0x10, &regs, &regs);
	regs.x.ax = 0x1200;
	regs.h.bl = 32;
	int86 (0x10, &regs, &regs);
	regs.x.ax = 0x0100;
	regs.x.cx = 7;
	int86 (0x10, &regs, &regs);
      }
#endif /* not __DJGPP__ > 1 */

  if (have_mouse)
    {
      mouse_init ();
      mouse_on ();
    }

  /* Tell the caller what dimensions have been REALLY set.  */
  *rows = ScreenRows ();
  *cols = ScreenCols ();
614

615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
#if __DJGPP__ > 1
  /* If the dimensions changed, the mouse highlight info is invalid.  */
  if (current_rows != *rows || current_cols != *cols)
    {
      struct frame *f = SELECTED_FRAME();
      struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
      Lisp_Object window = dpyinfo->mouse_face_window;

      if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
	{
	  dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
	  dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
	  dpyinfo->mouse_face_window = Qnil;
	}
    }
#endif

632 633
  /* Enable bright background colors.  */
  bright_bg ();
634 635 636 637 638

  /* FIXME: I'm not sure the above will run at all on DOS/V.  But let's
     be defensive anyway.  */
  if (screen_virtual_segment)
    dosv_refresh_virtual_screen (0, *cols * *rows);
639 640
}

641 642
/* If we write a character in the position where the mouse is,
   the mouse cursor may need to be refreshed.  */
643 644

static void
Kim F. Storm's avatar
Kim F. Storm committed
645
mouse_off_maybe ()
646
{
Kim F. Storm's avatar
Kim F. Storm committed
647 648 649 650 651 652 653 654 655 656 657 658
  int x, y;
  
  if (!mouse_visible)
    return;
  
  mouse_get_xy (&x, &y);
  if (y != new_pos_Y || x < new_pos_X)
    return;
  
  mouse_off ();
}

659 660
static void
IT_ring_bell (void)
Kim F. Storm's avatar
Kim F. Storm committed
661 662
{
  if (visible_bell)
663
    {
Kim F. Storm's avatar
Kim F. Storm committed
664 665
      mouse_off ();
      ScreenVisualBell ();
666
    }
Kim F. Storm's avatar
Kim F. Storm committed
667
  else
668 669 670 671 672 673
    {
      union REGS inregs, outregs;
      inregs.h.ah = 2;
      inregs.h.dl = 7;
      intdos (&inregs, &outregs);
    }
674 675
}

676 677 678 679 680
/* Given a face id FACE, extract the face parameters to be used for
   display until the face changes.  The face parameters (actually, its
   color) are used to construct the video attribute byte for each
   glyph during the construction of the buffer that is then blitted to
   the video RAM.  */
Kim F. Storm's avatar
Kim F. Storm committed
681 682 683
static void
IT_set_face (int face)
{
684 685
  struct frame *sf = SELECTED_FRAME();
  struct face *fp = FACE_FROM_ID (sf, face);
686
  unsigned long fg, bg;
Kim F. Storm's avatar
Kim F. Storm committed
687

688
  if (!fp)
689
    {
690
      fp = FACE_FROM_ID (sf, DEFAULT_FACE_ID);
691 692 693 694 695
      /* The default face for the frame should always be realized and
	 cached.  */
      if (!fp)
	abort ();
    }
Kim F. Storm's avatar
Kim F. Storm committed
696
  screen_face = face;
697 698 699
  fg = fp->foreground;
  bg = fp->background;

700 701 702 703 704 705
  /* Don't use invalid colors.  In particular, FACE_TTY_DEFAULT_*
     colors mean use the colors of the default face, except that if
     highlight is on, invert the foreground and the background.  Note
     that we assume all 16 colors to be available for the background,
     since Emacs switches on this mode (and loses the blinking
     attribute) at startup.  */
706
  if (fg == FACE_TTY_DEFAULT_COLOR || fg == FACE_TTY_DEFAULT_FG_COLOR)
707
    fg = FRAME_FOREGROUND_PIXEL (sf);
708
  else if (fg == FACE_TTY_DEFAULT_BG_COLOR)
709 710 711
    fg = FRAME_BACKGROUND_PIXEL (sf);
  if (bg == FACE_TTY_DEFAULT_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR)
    bg = FRAME_BACKGROUND_PIXEL (sf);
712
  else if (bg == FACE_TTY_DEFAULT_FG_COLOR)
713 714 715 716 717 718 719 720 721 722 723 724
    bg = FRAME_FOREGROUND_PIXEL (sf);

  /* Make sure highlighted lines really stand out, come what may.  */
  if ((highlight || fp->tty_reverse_p)
      && (fg == FRAME_FOREGROUND_PIXEL (sf)
	  && bg == FRAME_BACKGROUND_PIXEL (sf)))
    {
      unsigned long tem = fg;

      fg = bg;
      bg = tem;
    }
725
  if (termscript)
726 727
    fprintf (termscript, "<FACE %d%s: %d/%d[FG:%d/BG:%d]>", face,
	     highlight ? "H" : "", fp->foreground, fp->background, fg, bg);
728 729 730 731 732 733 734 735 736 737
  if (fg >= 0 && fg < 16)
    {
      ScreenAttrib &= 0xf0;
      ScreenAttrib |= fg;
    }
  if (bg >= 0 && bg < 16)
    {
      ScreenAttrib &= 0x0f;
      ScreenAttrib |= ((bg & 0x0f) << 4);
    }
Kim F. Storm's avatar
Kim F. Storm committed
738 739
}

740 741
Lisp_Object Vdos_unsupported_char_glyph;

742
static void
743
IT_write_glyphs (struct glyph *str, int str_len)
Kim F. Storm's avatar
Kim F. Storm committed
744
{
745 746 747
  unsigned char *screen_buf, *screen_bp, *screen_buf_end, *bp;
  int unsupported_face = FAST_GLYPH_FACE (Vdos_unsupported_char_glyph);
  unsigned unsupported_char= FAST_GLYPH_CHAR (Vdos_unsupported_char_glyph);
748
  int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
749 750 751 752
  register int sl = str_len;
  register int tlen = GLYPH_TABLE_LENGTH;
  register Lisp_Object *tbase = GLYPH_TABLE_BASE;

753 754 755
  struct coding_system *coding = (CODING_REQUIRE_ENCODING (&terminal_coding)
				  ? &terminal_coding
				  : &safe_terminal_coding);
756
  struct frame *sf;
Morten Welinder's avatar
Morten Welinder committed
757

758 759 760
  /* Do we need to consider conversion of unibyte characters to
     multibyte?  */
  int convert_unibyte_characters
761 762
    = (NILP (current_buffer->enable_multibyte_characters)
       && unibyte_display_via_language_environment);
763

764
  if (str_len <= 0) return;
765
  
766 767
  screen_buf = screen_bp = alloca (str_len * 2);
  screen_buf_end = screen_buf + str_len * 2;
768
  sf = SELECTED_FRAME();
769 770 771 772 773 774 775

  /* Since faces get cached and uncached behind our back, we can't
     rely on their indices in the cache being consistent across
     invocations.  So always reset the screen face to the default
     face of the frame, before writing glyphs, and let the glyphs
     set the right face if it's different from the default.  */
  IT_set_face (DEFAULT_FACE_ID);
776
  
777 778 779 780 781
  /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
     the tail.  */
  terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
  while (sl)
    {
782
      int cf, chlen, enclen;
783
      unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *buf;
784
      unsigned ch;
785 786 787 788

      /* Glyphs with GLYPH_MASK_PADDING bit set are actually there
	 only for the redisplay code to know how many columns does
         this character occupy on the screen.  Skip padding glyphs.  */
789
      if (CHAR_GLYPH_PADDING_P (*str))
790 791 792 793 794 795
	{
	  str++;
	  sl--;
	}
      else
	{
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812
	  register GLYPH g = GLYPH_FROM_CHAR_GLYPH (*str);
	  int glyph_not_in_table = 0;

	  if (g < 0 || g >= tlen)
	    {
	      /* This glyph doesn't have an entry in Vglyph_table.  */
	      ch = str->u.ch;
	      glyph_not_in_table = 1;
	    }
	  else
	    {
	      /* This glyph has an entry in Vglyph_table, so process
		 any aliases before testing for simpleness.  */
	      GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
	      ch = FAST_GLYPH_CHAR (g);
	    }

813
	  /* Convert the character code to multibyte, if they
814 815 816 817
	     requested display via language environment.  We only want
	     to convert unibyte characters to multibyte in unibyte
	     buffers!  Otherwise, the 8-bit value in CH came from the
	     display table set up to display foreign characters.  */
818
	  if (SINGLE_BYTE_CHAR_P (ch) && convert_unibyte_characters
819 820
	      && (ch >= 0240
		  || (ch >= 0200 && !NILP (Vnonascii_translation_table))))
821 822 823
	    ch = unibyte_char_to_multibyte (ch);

	  /* Invalid characters are displayed with a special glyph.  */
824
	  if (! CHAR_VALID_P (ch, 0))
825 826 827
	    {
	      g = !NILP (Vdos_unsupported_char_glyph)
		? Vdos_unsupported_char_glyph
828
		: MAKE_GLYPH (sf, '\177', GLYPH_FACE (sf, g));
829 830 831 832 833 834 835 836 837
	      ch = FAST_GLYPH_CHAR (g);
	    }

	  /* If the face of this glyph is different from the current
	     screen face, update the screen attribute byte.  */
	  cf = FAST_GLYPH_FACE (g);
	  if (cf != screen_face)
	    IT_set_face (cf);	/* handles invalid faces gracefully */

838
	  if (glyph_not_in_table || GLYPH_SIMPLE_P (tbase, tlen, g))
839 840 841 842 843
	    {
	      /* We generate the multi-byte form of CH in WORKBUF.  */
	      chlen = CHAR_STRING (ch, workbuf);
	      buf = workbuf;
	    }
844 845 846 847 848 849 850
	  else
	    {
	      /* We have a string in Vglyph_table.  */
	      chlen = GLYPH_LENGTH (tbase, g);
	      buf = GLYPH_STRING (tbase, g);
	    }

851
	  /* If the character is not multibyte, don't bother converting it.  */
852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926
	  if (chlen == 1)
	    {
	      *conversion_buffer = (unsigned char)ch;
	      chlen = 0;
	      enclen = 1;
	    }
	  else
	    {
	      encode_coding (coding, buf, conversion_buffer, chlen,
			     conversion_buffer_size);
	      chlen -= coding->consumed;
	      enclen = coding->produced;

	      /* Replace glyph codes that cannot be converted by
		 terminal_coding with Vdos_unsupported_char_glyph.  */
	      if (*conversion_buffer == '?')
		{
		  char *cbp = conversion_buffer;

		  while (cbp < conversion_buffer + enclen && *cbp == '?')
		    *cbp++ = unsupported_char;
		  if (unsupported_face != screen_face)
		    IT_set_face (unsupported_face);
		}
	    }

	  if (enclen + chlen > screen_buf_end - screen_bp)
	    {
	      /* The allocated buffer for screen writes is too small.
		 Flush it and loop again without incrementing STR, so
		 that the next loop will begin with the same glyph.  */
	      int nbytes = screen_bp - screen_buf;

	      mouse_off_maybe ();
	      dosmemput (screen_buf, nbytes, (int)ScreenPrimary + offset);
	      if (screen_virtual_segment)
		dosv_refresh_virtual_screen (offset, nbytes / 2);
	      new_pos_X += nbytes / 2;
	      offset += nbytes;

	      /* Prepare to reuse the same buffer again.  */
	      screen_bp = screen_buf;
	    }
	  else
	    {
	      /* There's enough place in the allocated buffer to add
		 the encoding of this glyph.  */

	      /* First, copy the encoded bytes.  */
	      for (bp = conversion_buffer; enclen--; bp++)
		{
		  *screen_bp++ = (unsigned char)*bp;
		  *screen_bp++ = ScreenAttrib;
		  if (termscript)
		    fputc (*bp, termscript);
		}

	      /* Now copy the bytes not consumed by the encoding.  */
	      if (chlen > 0)
		{
		  buf += coding->consumed;
		  while (chlen--)
		    {
		      if (termscript)
			fputc (*buf, termscript);
		      *screen_bp++ = (unsigned char)*buf++;
		      *screen_bp++ = ScreenAttrib;
		    }
		}

	      /* Update STR and its remaining length.  */
	      str++;
	      sl--;
	    }
	}
927 928
    }

929
  /* Dump whatever is left in the screen buffer.  */
Kim F. Storm's avatar
Kim F. Storm committed
930
  mouse_off_maybe ();
931
  dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
932
  if (screen_virtual_segment)
933 934 935 936 937 938 939 940 941 942
    dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
  new_pos_X += (screen_bp - screen_buf) / 2;

  /* We may have to output some codes to terminate the writing.  */
  if (CODING_REQUIRE_FLUSHING (coding))
    {
      coding->mode |= CODING_MODE_LAST_BLOCK;
      encode_coding (coding, "", conversion_buffer, 0, conversion_buffer_size);
      if (coding->produced > 0)
	{
943
	  screen_buf = alloca (coding->produced * 2);
944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960
	  for (screen_bp = screen_buf, bp = conversion_buffer;
	       coding->produced--; bp++)
	    {
	      *screen_bp++ = (unsigned char)*bp;
	      *screen_bp++ = ScreenAttrib;
	      if (termscript)
		fputc (*bp, termscript);
	    }
	  offset += screen_bp - screen_buf;
	  mouse_off_maybe ();
	  dosmemput (screen_buf, screen_bp - screen_buf,
		     (int)ScreenPrimary + offset);
	  if (screen_virtual_segment)
	    dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
	  new_pos_X += (screen_bp - screen_buf) / 2;
	}
    }
Kim F. Storm's avatar
Kim F. Storm committed
961
}
962

963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 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 1088 1089 1090 1091 1092 1093 1094 1095 1096 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 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254
/************************************************************************
			  Mouse Highlight (and friends..)
 ************************************************************************/

/* This is used for debugging, to turn off note_mouse_highlight.  */
int disable_mouse_highlight;

/* If a string, dos_rawgetc generates an event to display that string.
   (The display is done in keyboard.c:read_char.)  */
static Lisp_Object help_echo;
static Lisp_Object previous_help_echo; /* a helper temporary variable */

static int mouse_preempted = 0;	/* non-zero when XMenu gobbles mouse events */

/* Set the mouse pointer shape according to whether it is in the
   area where the mouse highlight is in effect.  */
static void
IT_set_mouse_pointer (int mode)
{
  /* A no-op for now.  DOS text-mode mouse pointer doesn't offer too
     many possibilities to change its shape, and the available
     functionality pretty much sucks (e.g., almost every reasonable
     shape will conceal the character it is on).  Since the color of
     the pointer changes in the highlighted area, it is not clear to
     me whether anything else is required, anyway.  */
}

/* Display the active region described by mouse_face_*
   in its mouse-face if HL > 0, in its normal face if HL = 0.  */
static void
show_mouse_face (struct display_info *dpyinfo, int hl)
{
  struct window *w = XWINDOW (dpyinfo->mouse_face_window);
  struct frame *f = XFRAME (WINDOW_FRAME (w));
  int i;
  struct face *fp;

  
  /* If window is in the process of being destroyed, don't bother
     doing anything.  */
  if (w->current_matrix == NULL)
    goto set_cursor_shape;

  /* Recognize when we are called to operate on rows that don't exist
     anymore.  This can happen when a window is split.  */
  if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
    goto set_cursor_shape;

  /* There's no sense to do anything if the mouse face isn't realized.  */
  if (hl > 0)
    {
      fp = FACE_FROM_ID (SELECTED_FRAME(), dpyinfo->mouse_face_face_id);
      if (!fp)
	goto set_cursor_shape;
    }

  /* Note that mouse_face_beg_row etc. are window relative.  */
  for (i = dpyinfo->mouse_face_beg_row;
       i <= dpyinfo->mouse_face_end_row;
       i++)
    {
      int start_hpos, end_hpos;
      struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);

      /* Don't do anything if row doesn't have valid contents.  */
      if (!row->enabled_p)
	continue;

      /* For all but the first row, the highlight starts at column 0.  */
      if (i == dpyinfo->mouse_face_beg_row)
	start_hpos = dpyinfo->mouse_face_beg_col;
      else
	start_hpos = 0;

      if (i == dpyinfo->mouse_face_end_row)
	end_hpos = dpyinfo->mouse_face_end_col;
      else
	end_hpos = row->used[TEXT_AREA];

      if (end_hpos <= start_hpos)
	continue;
      if (hl > 0)
	{
	  int vpos = row->y + WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
	  int kstart = start_hpos + WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w);
	  int nglyphs = end_hpos - start_hpos;
	  int offset = ScreenPrimary + 2*(vpos*screen_size_X + kstart) + 1;
	  int start_offset = offset;

	  if (termscript)
	    fprintf (termscript, "\n<MH+ %d-%d:%d>",
		     kstart, kstart + nglyphs - 1, vpos);

	  mouse_off ();
	  IT_set_face (dpyinfo->mouse_face_face_id);
	  /* Since we are going to change only the _colors_ of the
	     displayed text, there's no need to go through all the
	     pain of generating and encoding the text from the glyphs.
	     Instead, we simply poke the attribute byte of each
	     affected position in video memory with the colors
	     computed by IT_set_face!  */
	  _farsetsel (_dos_ds);
	  while (nglyphs--)
	    {
	      _farnspokeb (offset, ScreenAttrib);
	      offset += 2;
	    }
	  if (screen_virtual_segment)
	    dosv_refresh_virtual_screen (start_offset, end_hpos - start_hpos);
	  mouse_on ();
	}
      else
	{
	  /* We are removing a previously-drawn mouse highlight.  The
	     safest way to do so is to redraw the glyphs anew, since
	     all kinds of faces and display tables could have changed
	     behind our back.  */
	  int nglyphs = end_hpos - start_hpos;
	  int save_x = new_pos_X, save_y = new_pos_Y;

	  if (end_hpos >= row->used[TEXT_AREA])
	    nglyphs = row->used[TEXT_AREA] - start_hpos;

	  /* IT_write_glyphs writes at cursor position, so we need to
	     temporarily move cursor coordinates to the beginning of
	     the highlight region.  */
	  new_pos_X = start_hpos + WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w);
	  new_pos_Y = row->y + WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);

	  if (termscript)
	    fprintf (termscript, "<MH- %d-%d:%d>",
		     new_pos_X, new_pos_X + nglyphs - 1, new_pos_Y);
	  IT_write_glyphs (row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
	  if (termscript)
	    fputs ("\n", termscript);
	  new_pos_X = save_x;
	  new_pos_Y = save_y;
	}
    }

 set_cursor_shape:
  
  /* Change the mouse pointer shape.  */
  IT_set_mouse_pointer (hl);
}

/* Clear out the mouse-highlighted active region.
   Redraw it un-highlighted first.  */
static void
clear_mouse_face (struct display_info *dpyinfo)
{
  if (! NILP (dpyinfo->mouse_face_window))
    show_mouse_face (dpyinfo, 0);

  dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
  dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
  dpyinfo->mouse_face_window = Qnil;
}

/* Find the glyph matrix position of buffer position POS in window W.
   *HPOS and *VPOS are set to the positions found.  W's current glyphs
   must be up to date.  If POS is above window start return (0, 0).
   If POS is after end of W, return end of last line in W.  */
static int
fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
{
  int i;
  int lastcol;
  int maybe_next_line_p = 0;
  int line_start_position;
  int yb = window_text_bottom_y (w);
  struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
  struct glyph_row *best_row = row;

  while (row->y < yb)
    {
      if (row->used[TEXT_AREA])
	line_start_position = row->glyphs[TEXT_AREA]->charpos;
      else
	line_start_position = 0;

      if (line_start_position > pos)
	break;
      /* If the position sought is the end of the buffer,
	 don't include the blank lines at the bottom of the window.  */
      else if (line_start_position == pos
	       && pos == BUF_ZV (XBUFFER (w->buffer)))
	{
	  maybe_next_line_p = 1;
	  break;
	}
      else if (line_start_position > 0)
	best_row = row;
      
      ++row;
    }
  
  /* Find the right column within BEST_ROW.  */
  lastcol = 0;
  row = best_row;
  for (i = 0; i < row->used[TEXT_AREA]; i++)
    {
      struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
      int charpos;

      charpos = glyph->charpos;
      if (charpos == pos)
	{
	  *hpos = i;
	  *vpos = row->y;
	  return 1;
	}
      else if (charpos > pos)
	break;
      else if (charpos > 0)
	lastcol = i;
    }

  /* If we're looking for the end of the buffer,
     and we didn't find it in the line we scanned,
     use the start of the following line.  */
  if (maybe_next_line_p)
    {
      ++row;
      lastcol = 0;
    }

  *vpos = row->y;
  *hpos = lastcol + 1;
  return 0;
}

/* Take proper action when mouse has moved to the mode or top line of
   window W, x-position X.  MODE_LINE_P non-zero means mouse is on the
   mode line.  X is relative to the start of the text display area of
   W, so the width of bitmap areas and scroll bars must be subtracted
   to get a position relative to the start of the mode line.  */
static void
IT_note_mode_line_highlight (struct window *w, int x, int mode_line_p)
{
  struct frame *f = XFRAME (w->frame);
  struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
  struct glyph_row *row;

  if (mode_line_p)
    row = MATRIX_MODE_LINE_ROW (w->current_matrix);
  else
    row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
  
  if (row->enabled_p)
    {
      extern Lisp_Object Qhelp_echo;
      struct glyph *glyph, *end;
      Lisp_Object help, map;
      
      /* Find the glyph under X.  */
      glyph = row->glyphs[TEXT_AREA]
	+ x - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
      end = glyph + row->used[TEXT_AREA];
      if (glyph < end
	  && STRINGP (glyph->object)
	  && XSTRING (glyph->object)->intervals
	  && glyph->charpos >= 0
	  && glyph->charpos < XSTRING (glyph->object)->size)
	{
	  /* If we're on a string with `help-echo' text property,
	     arrange for the help to be displayed.  This is done by
	     setting the global variable help_echo to the help string.  */
	  help = Fget_text_property (make_number (glyph->charpos),
				     Qhelp_echo, glyph->object);
	  if (STRINGP (help))
	    help_echo = help;
	}
    }
}

/* Take proper action when the mouse has moved to position X, Y on
   frame F as regards highlighting characters that have mouse-face
   properties.  Also de-highlighting chars where the mouse was before.
   X and Y can be negative or out of range.  */
static void
IT_note_mouse_highlight (struct frame *f, int x, int y)
{
  struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
  int portion;
  Lisp_Object window;
  struct window *w;

  /* When a menu is active, don't highlight because this looks odd.  */
  if (mouse_preempted)
    return;

1255 1256
  if (disable_mouse_highlight
      || !f->glyphs_initialized_p)
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491