(large-file-warning-threshold): Add type, groups.
[emacs.git] / src / msdos.c
blobc558515ddfa4a4f3fe470a62b02679668b70ca84
1 /* MS-DOS specific C utilities. -*- coding: raw-text -*-
2 Copyright (C) 1993, 94, 95, 96, 97, 1999, 2000, 2001
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* Contributed by Morten Welinder */
23 /* New display, keyboard, and mouse control by Kim F. Storm */
25 /* Note: some of the stuff here was taken from end of sysdep.c in demacs. */
27 #include <config.h>
29 #ifdef MSDOS
30 #include "lisp.h"
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <time.h>
34 #include <sys/param.h>
35 #include <sys/time.h>
36 #include <dos.h>
37 #include <errno.h>
38 #include <string.h> /* for bzero and string functions */
39 #include <sys/stat.h> /* for _fixpath */
40 #include <unistd.h> /* for chdir, dup, dup2, etc. */
41 #include <dir.h> /* for getdisk */
42 #if __DJGPP__ >= 2
43 #include <fcntl.h>
44 #include <io.h> /* for setmode */
45 #include <dpmi.h> /* for __dpmi_xxx stuff */
46 #include <sys/farptr.h> /* for _farsetsel, _farnspokeb */
47 #include <libc/dosio.h> /* for _USE_LFN */
48 #include <conio.h> /* for cputs */
49 #endif
51 #include "msdos.h"
52 #include "systime.h"
53 #include "termhooks.h"
54 #include "termchar.h"
55 #include "dispextern.h"
56 #include "dosfns.h"
57 #include "termopts.h"
58 #include "charset.h"
59 #include "coding.h"
60 #include "disptab.h"
61 #include "frame.h"
62 #include "window.h"
63 #include "buffer.h"
64 #include "commands.h"
65 #include "blockinput.h"
66 #include "keyboard.h"
67 #include <go32.h>
68 #include <pc.h>
69 #include <ctype.h>
70 /* #include <process.h> */
71 /* Damn that local process.h! Instead we can define P_WAIT ourselves. */
72 #define P_WAIT 1
74 #ifndef _USE_LFN
75 #define _USE_LFN 0
76 #endif
78 #ifndef _dos_ds
79 #define _dos_ds _go32_info_block.selector_for_linear_memory
80 #endif
82 #if __DJGPP__ > 1
84 #include <signal.h>
85 #include "syssignal.h"
87 #ifndef SYSTEM_MALLOC
89 #ifdef GNU_MALLOC
91 /* If other `malloc' than ours is used, force our `sbrk' behave like
92 Unix programs expect (resize memory blocks to keep them contiguous).
93 If `sbrk' from `ralloc.c' is NOT used, also zero-out sbrk'ed memory,
94 because that's what `gmalloc' expects to get. */
95 #include <crt0.h>
97 #ifdef REL_ALLOC
98 int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
99 #else /* not REL_ALLOC */
100 int _crt0_startup_flags = (_CRT0_FLAG_UNIX_SBRK | _CRT0_FLAG_FILL_SBRK_MEMORY);
101 #endif /* not REL_ALLOC */
102 #endif /* GNU_MALLOC */
104 #endif /* not SYSTEM_MALLOC */
105 #endif /* __DJGPP__ > 1 */
107 static unsigned long
108 event_timestamp ()
110 struct time t;
111 unsigned long s;
113 gettime (&t);
114 s = t.ti_min;
115 s *= 60;
116 s += t.ti_sec;
117 s *= 1000;
118 s += t.ti_hund * 10;
120 return s;
124 /* ------------------------ Mouse control ---------------------------
126 * Coordinates are in screen positions and zero based.
127 * Mouse buttons are numbered from left to right and also zero based.
130 /* This used to be in termhooks.h, but mainstream Emacs code no longer
131 uses it, and it was removed... */
132 #define NUM_MOUSE_BUTTONS (5)
134 int have_mouse; /* 0: no, 1: enabled, -1: disabled */
135 static int mouse_visible;
137 static int mouse_last_x;
138 static int mouse_last_y;
140 static int mouse_button_translate[NUM_MOUSE_BUTTONS];
141 static int mouse_button_count;
143 void
144 mouse_on ()
146 union REGS regs;
148 if (have_mouse > 0 && !mouse_visible)
150 if (termscript)
151 fprintf (termscript, "<M_ON>");
152 regs.x.ax = 0x0001;
153 int86 (0x33, &regs, &regs);
154 mouse_visible = 1;
158 void
159 mouse_off ()
161 union REGS regs;
163 if (have_mouse > 0 && mouse_visible)
165 if (termscript)
166 fprintf (termscript, "<M_OFF>");
167 regs.x.ax = 0x0002;
168 int86 (0x33, &regs, &regs);
169 mouse_visible = 0;
173 static void
174 mouse_setup_buttons (int n_buttons)
176 if (n_buttons == 3)
178 mouse_button_count = 3;
179 mouse_button_translate[0] = 0; /* Left */
180 mouse_button_translate[1] = 2; /* Middle */
181 mouse_button_translate[2] = 1; /* Right */
183 else /* two, what else? */
185 mouse_button_count = 2;
186 mouse_button_translate[0] = 0;
187 mouse_button_translate[1] = 1;
191 DEFUN ("msdos-set-mouse-buttons", Fmsdos_set_mouse_buttons, Smsdos_set_mouse_buttons,
192 1, 1, "NSet number of mouse buttons to: ",
193 doc: /* Set the number of mouse buttons to use by Emacs.
194 This is useful with mice that report the number of buttons inconsistently,
195 e.g., if the number of buttons is reported as 3, but Emacs only sees 2 of
196 them. This happens with wheeled mice on Windows 9X, for example. */)
197 (nbuttons)
198 Lisp_Object nbuttons;
200 int n;
202 CHECK_NUMBER (nbuttons);
203 n = XINT (nbuttons);
204 if (n < 2 || n > 3)
205 Fsignal (Qargs_out_of_range,
206 Fcons (build_string ("only 2 or 3 mouse buttons are supported"),
207 Fcons (nbuttons, Qnil)));
208 mouse_setup_buttons (n);
209 return Qnil;
212 static void
213 mouse_get_xy (int *x, int *y)
215 union REGS regs;
217 regs.x.ax = 0x0003;
218 int86 (0x33, &regs, &regs);
219 *x = regs.x.cx / 8;
220 *y = regs.x.dx / 8;
223 void
224 mouse_moveto (x, y)
225 int x, y;
227 union REGS regs;
229 if (termscript)
230 fprintf (termscript, "<M_XY=%dx%d>", x, y);
231 regs.x.ax = 0x0004;
232 mouse_last_x = regs.x.cx = x * 8;
233 mouse_last_y = regs.x.dx = y * 8;
234 int86 (0x33, &regs, &regs);
237 static int
238 mouse_pressed (b, xp, yp)
239 int b, *xp, *yp;
241 union REGS regs;
243 if (b >= mouse_button_count)
244 return 0;
245 regs.x.ax = 0x0005;
246 regs.x.bx = mouse_button_translate[b];
247 int86 (0x33, &regs, &regs);
248 if (regs.x.bx)
249 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
250 return (regs.x.bx != 0);
253 static int
254 mouse_released (b, xp, yp)
255 int b, *xp, *yp;
257 union REGS regs;
259 if (b >= mouse_button_count)
260 return 0;
261 regs.x.ax = 0x0006;
262 regs.x.bx = mouse_button_translate[b];
263 int86 (0x33, &regs, &regs);
264 if (regs.x.bx)
265 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
266 return (regs.x.bx != 0);
269 static int
270 mouse_button_depressed (b, xp, yp)
271 int b, *xp, *yp;
273 union REGS regs;
275 if (b >= mouse_button_count)
276 return 0;
277 regs.x.ax = 0x0003;
278 int86 (0x33, &regs, &regs);
279 if ((regs.x.bx & (1 << mouse_button_translate[b])) != 0)
281 *xp = regs.x.cx / 8;
282 *yp = regs.x.dx / 8;
283 return 1;
285 return 0;
288 void
289 mouse_get_pos (f, insist, bar_window, part, x, y, time)
290 FRAME_PTR *f;
291 int insist;
292 Lisp_Object *bar_window, *x, *y;
293 enum scroll_bar_part *part;
294 unsigned long *time;
296 int ix, iy;
297 Lisp_Object frame, tail;
299 /* Clear the mouse-moved flag for every frame on this display. */
300 FOR_EACH_FRAME (tail, frame)
301 XFRAME (frame)->mouse_moved = 0;
303 *f = SELECTED_FRAME();
304 *bar_window = Qnil;
305 mouse_get_xy (&ix, &iy);
306 *time = event_timestamp ();
307 *x = make_number (mouse_last_x = ix);
308 *y = make_number (mouse_last_y = iy);
311 static void
312 mouse_check_moved ()
314 int x, y;
316 mouse_get_xy (&x, &y);
317 SELECTED_FRAME()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
318 mouse_last_x = x;
319 mouse_last_y = y;
322 /* Force the mouse driver to ``forget'' about any button clicks until
323 now. */
324 static void
325 mouse_clear_clicks (void)
327 int b;
329 for (b = 0; b < mouse_button_count; b++)
331 int dummy_x, dummy_y;
333 (void) mouse_pressed (b, &dummy_x, &dummy_y);
334 (void) mouse_released (b, &dummy_x, &dummy_y);
338 void
339 mouse_init ()
341 union REGS regs;
343 if (termscript)
344 fprintf (termscript, "<M_INIT>");
346 regs.x.ax = 0x0021;
347 int86 (0x33, &regs, &regs);
349 /* Reset the mouse last press/release info. It seems that Windows
350 doesn't do that automatically when function 21h is called, which
351 causes Emacs to ``remember'' the click that switched focus to the
352 window just before Emacs was started from that window. */
353 mouse_clear_clicks ();
355 regs.x.ax = 0x0007;
356 regs.x.cx = 0;
357 regs.x.dx = 8 * (ScreenCols () - 1);
358 int86 (0x33, &regs, &regs);
360 regs.x.ax = 0x0008;
361 regs.x.cx = 0;
362 regs.x.dx = 8 * (ScreenRows () - 1);
363 int86 (0x33, &regs, &regs);
365 mouse_moveto (0, 0);
366 mouse_visible = 0;
369 /* ------------------------- Screen control ----------------------
373 static int internal_terminal = 0;
375 #ifndef HAVE_X_WINDOWS
376 extern unsigned char ScreenAttrib;
377 static int screen_face;
379 static int screen_size_X;
380 static int screen_size_Y;
381 static int screen_size;
383 static int current_pos_X;
384 static int current_pos_Y;
385 static int new_pos_X;
386 static int new_pos_Y;
388 static void *startup_screen_buffer;
389 static int startup_screen_size_X;
390 static int startup_screen_size_Y;
391 static int startup_pos_X;
392 static int startup_pos_Y;
393 static unsigned char startup_screen_attrib;
395 static clock_t startup_time;
397 static int term_setup_done;
399 static unsigned short outside_cursor;
401 /* Similar to the_only_frame. */
402 struct x_output the_only_x_display;
404 /* Support for DOS/V (allows Japanese characters to be displayed on
405 standard, non-Japanese, ATs). Only supported for DJGPP v2 and later. */
407 /* Holds the address of the text-mode screen buffer. */
408 static unsigned long screen_old_address = 0;
409 /* Segment and offset of the virtual screen. If 0, DOS/V is NOT loaded. */
410 static unsigned short screen_virtual_segment = 0;
411 static unsigned short screen_virtual_offset = 0;
412 /* A flag to control how to display unibyte 8-bit characters. */
413 extern int unibyte_display_via_language_environment;
415 Lisp_Object Qbar, Qhbar;
417 /* The screen colors of the current frame, which serve as the default
418 colors for newly-created frames. */
419 static int initial_screen_colors[2];
421 #if __DJGPP__ > 1
422 /* Update the screen from a part of relocated DOS/V screen buffer which
423 begins at OFFSET and includes COUNT characters. */
424 static void
425 dosv_refresh_virtual_screen (int offset, int count)
427 __dpmi_regs regs;
429 if (offset < 0 || count < 0) /* paranoia; invalid values crash DOS/V */
430 return;
432 regs.h.ah = 0xff; /* update relocated screen */
433 regs.x.es = screen_virtual_segment;
434 regs.x.di = screen_virtual_offset + offset;
435 regs.x.cx = count;
436 __dpmi_int (0x10, &regs);
438 #endif
440 static void
441 dos_direct_output (y, x, buf, len)
442 int x, y;
443 char *buf;
444 int len;
446 int t0 = 2 * (x + y * screen_size_X);
447 int t = t0 + (int) ScreenPrimary;
448 int l0 = len;
450 #if (__DJGPP__ < 2)
451 while (--len >= 0) {
452 dosmemput (buf++, 1, t);
453 t += 2;
455 #else
456 /* This is faster. */
457 for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
458 _farnspokeb (t, *buf);
460 if (screen_virtual_segment)
461 dosv_refresh_virtual_screen (t0, l0);
462 #endif
464 #endif
466 /* Flash the screen as a substitute for BEEPs. */
468 #if (__DJGPP__ < 2)
469 static void
470 do_visible_bell (xorattr)
471 unsigned char xorattr;
473 asm volatile
474 (" movb $1,%%dl \n\
475 visible_bell_0: \n\
476 movl _ScreenPrimary,%%eax \n\
477 call dosmemsetup \n\
478 movl %%eax,%%ebx \n\
479 movl %1,%%ecx \n\
480 movb %0,%%al \n\
481 incl %%ebx \n\
482 visible_bell_1: \n\
483 xorb %%al,%%gs:(%%ebx) \n\
484 addl $2,%%ebx \n\
485 decl %%ecx \n\
486 jne visible_bell_1 \n\
487 decb %%dl \n\
488 jne visible_bell_3 \n\
489 visible_bell_2: \n\
490 movzwl %%ax,%%eax \n\
491 movzwl %%ax,%%eax \n\
492 movzwl %%ax,%%eax \n\
493 movzwl %%ax,%%eax \n\
494 decw %%cx \n\
495 jne visible_bell_2 \n\
496 jmp visible_bell_0 \n\
497 visible_bell_3:"
498 : /* no output */
499 : "m" (xorattr), "g" (screen_size)
500 : "%eax", "%ebx", /* "%gs",*/ "%ecx", "%edx");
503 static void
504 ScreenVisualBell (void)
506 /* This creates an xor-mask that will swap the default fore- and
507 background colors. */
508 do_visible_bell (((the_only_x_display.foreground_pixel
509 ^ the_only_x_display.background_pixel)
510 * 0x11) & 0x7f);
512 #endif
514 #ifndef HAVE_X_WINDOWS
516 static int blink_bit = -1; /* the state of the blink bit at startup */
518 /* Enable bright background colors. */
519 static void
520 bright_bg (void)
522 union REGS regs;
524 /* Remember the original state of the blink/bright-background bit.
525 It is stored at 0040:0065h in the BIOS data area. */
526 if (blink_bit == -1)
527 blink_bit = (_farpeekb (_dos_ds, 0x465) & 0x20) == 0x20;
529 regs.h.bl = 0;
530 regs.x.ax = 0x1003;
531 int86 (0x10, &regs, &regs);
534 /* Disable bright background colors (and enable blinking) if we found
535 the video system in that state at startup. */
536 static void
537 maybe_enable_blinking (void)
539 if (blink_bit == 1)
541 union REGS regs;
543 regs.h.bl = 1;
544 regs.x.ax = 0x1003;
545 int86 (0x10, &regs, &regs);
549 /* Return non-zero if the system has a VGA adapter. */
550 static int
551 vga_installed (void)
553 union REGS regs;
555 regs.x.ax = 0x1a00;
556 int86 (0x10, &regs, &regs);
557 if (regs.h.al == 0x1a && regs.h.bl > 5 && regs.h.bl < 13)
558 return 1;
559 return 0;
562 /* Set the screen dimensions so that it can show no less than
563 ROWS x COLS frame. */
565 void
566 dos_set_window_size (rows, cols)
567 int *rows, *cols;
569 char video_name[30];
570 union REGS regs;
571 Lisp_Object video_mode;
572 int video_mode_value, have_vga = 0;
573 int current_rows = ScreenRows (), current_cols = ScreenCols ();
575 if (*rows == current_rows && *cols == current_cols)
576 return;
578 mouse_off ();
579 have_vga = vga_installed ();
581 /* If the user specified a special video mode for these dimensions,
582 use that mode. */
583 sprintf (video_name, "screen-dimensions-%dx%d", *rows, *cols);
584 video_mode = XSYMBOL (Fintern_soft (build_string (video_name),
585 Qnil))-> value;
587 if (INTEGERP (video_mode)
588 && (video_mode_value = XINT (video_mode)) > 0)
590 regs.x.ax = video_mode_value;
591 int86 (0x10, &regs, &regs);
593 if (have_mouse)
595 /* Must hardware-reset the mouse, or else it won't update
596 its notion of screen dimensions for some non-standard
597 video modes. This is *painfully* slow... */
598 regs.x.ax = 0;
599 int86 (0x33, &regs, &regs);
603 /* Find one of the dimensions supported by standard EGA/VGA
604 which gives us at least the required dimensions. */
606 #if __DJGPP__ > 1
608 else
610 static struct {
611 int rows, need_vga;
612 } std_dimension[] = {
613 {25, 0},
614 {28, 1},
615 {35, 0},
616 {40, 1},
617 {43, 0},
618 {50, 1}
620 int i = 0;
622 while (i < sizeof (std_dimension) / sizeof (std_dimension[0]))
624 if (std_dimension[i].need_vga <= have_vga
625 && std_dimension[i].rows >= *rows)
627 if (std_dimension[i].rows != current_rows
628 || *cols != current_cols)
629 _set_screen_lines (std_dimension[i].rows);
630 break;
632 i++;
636 #else /* not __DJGPP__ > 1 */
638 else if (*rows <= 25)
640 if (current_rows != 25 || current_cols != 80)
642 regs.x.ax = 3;
643 int86 (0x10, &regs, &regs);
644 regs.x.ax = 0x1101;
645 regs.h.bl = 0;
646 int86 (0x10, &regs, &regs);
647 regs.x.ax = 0x1200;
648 regs.h.bl = 32;
649 int86 (0x10, &regs, &regs);
650 regs.x.ax = 3;
651 int86 (0x10, &regs, &regs);
654 else if (*rows <= 50)
655 if (have_vga && (current_rows != 50 || current_cols != 80)
656 || *rows <= 43 && (current_rows != 43 || current_cols != 80))
658 regs.x.ax = 3;
659 int86 (0x10, &regs, &regs);
660 regs.x.ax = 0x1112;
661 regs.h.bl = 0;
662 int86 (0x10, &regs, &regs);
663 regs.x.ax = 0x1200;
664 regs.h.bl = 32;
665 int86 (0x10, &regs, &regs);
666 regs.x.ax = 0x0100;
667 regs.x.cx = 7;
668 int86 (0x10, &regs, &regs);
670 #endif /* not __DJGPP__ > 1 */
672 if (have_mouse)
674 mouse_init ();
675 mouse_on ();
678 /* Tell the caller what dimensions have been REALLY set. */
679 *rows = ScreenRows ();
680 *cols = ScreenCols ();
682 /* Update Emacs' notion of screen dimensions. */
683 screen_size_X = *cols;
684 screen_size_Y = *rows;
685 screen_size = *cols * *rows;
687 #if __DJGPP__ > 1
688 /* If the dimensions changed, the mouse highlight info is invalid. */
689 if (current_rows != *rows || current_cols != *cols)
691 struct frame *f = SELECTED_FRAME();
692 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
693 Lisp_Object window = dpyinfo->mouse_face_window;
695 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
697 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
698 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
699 dpyinfo->mouse_face_window = Qnil;
702 #endif
704 /* Enable bright background colors. */
705 bright_bg ();
707 /* FIXME: I'm not sure the above will run at all on DOS/V. But let's
708 be defensive anyway. */
709 if (screen_virtual_segment)
710 dosv_refresh_virtual_screen (0, *cols * *rows);
713 /* If we write a character in the position where the mouse is,
714 the mouse cursor may need to be refreshed. */
716 static void
717 mouse_off_maybe ()
719 int x, y;
721 if (!mouse_visible)
722 return;
724 mouse_get_xy (&x, &y);
725 if (y != new_pos_Y || x < new_pos_X)
726 return;
728 mouse_off ();
731 #define DEFAULT_CURSOR_START (-1)
732 #define DEFAULT_CURSOR_WIDTH (-1)
733 #define BOX_CURSOR_WIDTH (-32)
735 /* Set cursor to begin at scan line START_LINE in the character cell
736 and extend for WIDTH scan lines. Scan lines are counted from top
737 of the character cell, starting from zero. */
738 static void
739 msdos_set_cursor_shape (struct frame *f, int start_line, int width)
741 #if __DJGPP__ > 1
742 unsigned desired_cursor;
743 __dpmi_regs regs;
744 int max_line, top_line, bot_line;
746 /* Avoid the costly BIOS call if F isn't the currently selected
747 frame. Allow for NULL as unconditionally meaning the selected
748 frame. */
749 if (f && f != SELECTED_FRAME())
750 return;
752 /* The character cell size in scan lines is stored at 40:85 in the
753 BIOS data area. */
754 max_line = _farpeekw (_dos_ds, 0x485) - 1;
755 switch (max_line)
757 default: /* this relies on CGA cursor emulation being ON! */
758 case 7:
759 bot_line = 7;
760 break;
761 case 9:
762 bot_line = 9;
763 break;
764 case 13:
765 bot_line = 12;
766 break;
767 case 15:
768 bot_line = 14;
769 break;
772 if (width < 0)
774 if (width == BOX_CURSOR_WIDTH)
776 top_line = 0;
777 bot_line = max_line;
779 else if (start_line != DEFAULT_CURSOR_START)
781 top_line = start_line;
782 bot_line = top_line - width - 1;
784 else if (width != DEFAULT_CURSOR_WIDTH)
786 top_line = 0;
787 bot_line = -1 - width;
789 else
790 top_line = bot_line + 1;
792 else if (width == 0)
794 /* [31, 0] seems to DTRT for all screen sizes. */
795 top_line = 31;
796 bot_line = 0;
798 else /* WIDTH is positive */
800 if (start_line != DEFAULT_CURSOR_START)
801 bot_line = start_line;
802 top_line = bot_line - (width - 1);
805 /* If the current cursor shape is already what they want, we are
806 history here. */
807 desired_cursor = ((top_line & 0x1f) << 8) | (bot_line & 0x1f);
808 if (desired_cursor == _farpeekw (_dos_ds, 0x460))
809 return;
811 regs.h.ah = 1;
812 regs.x.cx = desired_cursor;
813 __dpmi_int (0x10, &regs);
814 #endif /* __DJGPP__ > 1 */
817 static void
818 IT_set_cursor_type (struct frame *f, Lisp_Object cursor_type)
820 if (EQ (cursor_type, Qbar) || EQ (cursor_type, Qhbar))
822 /* Just BAR means the normal EGA/VGA cursor. */
823 msdos_set_cursor_shape (f, DEFAULT_CURSOR_START, DEFAULT_CURSOR_WIDTH);
825 else if (CONSP (cursor_type)
826 && (EQ (XCAR (cursor_type), Qbar)
827 || EQ (XCAR (cursor_type), Qhbar)))
829 Lisp_Object bar_parms = XCDR (cursor_type);
830 int width;
832 if (INTEGERP (bar_parms))
834 /* Feature: negative WIDTH means cursor at the top
835 of the character cell, zero means invisible cursor. */
836 width = XINT (bar_parms);
837 msdos_set_cursor_shape (f, width >= 0 ? DEFAULT_CURSOR_START : 0,
838 width);
840 else if (CONSP (bar_parms)
841 && INTEGERP (XCAR (bar_parms))
842 && INTEGERP (XCDR (bar_parms)))
844 int start_line = XINT (XCDR (bar_parms));
846 width = XINT (XCAR (bar_parms));
847 msdos_set_cursor_shape (f, start_line, width);
850 else
851 /* Treat anything unknown as "box cursor". This includes nil, so
852 that a frame which doesn't specify a cursor type gets a box,
853 which is the default in Emacs. */
854 msdos_set_cursor_shape (f, 0, BOX_CURSOR_WIDTH);
857 static void
858 IT_ring_bell (void)
860 if (visible_bell)
862 mouse_off ();
863 ScreenVisualBell ();
865 else
867 union REGS inregs, outregs;
868 inregs.h.ah = 2;
869 inregs.h.dl = 7;
870 intdos (&inregs, &outregs);
874 /* Given a face id FACE, extract the face parameters to be used for
875 display until the face changes. The face parameters (actually, its
876 color) are used to construct the video attribute byte for each
877 glyph during the construction of the buffer that is then blitted to
878 the video RAM. */
879 static void
880 IT_set_face (int face)
882 struct frame *sf = SELECTED_FRAME();
883 struct face *fp = FACE_FROM_ID (sf, face);
884 struct face *dfp = FACE_FROM_ID (sf, DEFAULT_FACE_ID);
885 unsigned long fg, bg, dflt_fg, dflt_bg;
887 if (!fp)
889 fp = dfp;
890 /* The default face for the frame should always be realized and
891 cached. */
892 if (!fp)
893 abort ();
895 screen_face = face;
896 fg = fp->foreground;
897 bg = fp->background;
898 dflt_fg = dfp->foreground;
899 dflt_bg = dfp->background;
901 /* Don't use invalid colors. In particular, FACE_TTY_DEFAULT_* colors
902 mean use the colors of the default face. Note that we assume all
903 16 colors to be available for the background, since Emacs switches
904 on this mode (and loses the blinking attribute) at startup. */
905 if (fg == FACE_TTY_DEFAULT_COLOR || fg == FACE_TTY_DEFAULT_FG_COLOR)
906 fg = FRAME_FOREGROUND_PIXEL (sf);
907 else if (fg == FACE_TTY_DEFAULT_BG_COLOR)
908 fg = FRAME_BACKGROUND_PIXEL (sf);
909 if (bg == FACE_TTY_DEFAULT_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR)
910 bg = FRAME_BACKGROUND_PIXEL (sf);
911 else if (bg == FACE_TTY_DEFAULT_FG_COLOR)
912 bg = FRAME_FOREGROUND_PIXEL (sf);
914 /* Make sure highlighted lines really stand out, come what may. */
915 if (fp->tty_reverse_p && (fg == dflt_fg && bg == dflt_bg))
917 unsigned long tem = fg;
919 fg = bg;
920 bg = tem;
922 /* If the user requested inverse video, obey. */
923 if (inverse_video)
925 unsigned long tem2 = fg;
927 fg = bg;
928 bg = tem2;
930 if (termscript)
931 fprintf (termscript, "<FACE %d: %d/%d[FG:%d/BG:%d]>", face,
932 fp->foreground, fp->background, fg, bg);
933 if (fg >= 0 && fg < 16)
935 ScreenAttrib &= 0xf0;
936 ScreenAttrib |= fg;
938 if (bg >= 0 && bg < 16)
940 ScreenAttrib &= 0x0f;
941 ScreenAttrib |= ((bg & 0x0f) << 4);
945 Lisp_Object Vdos_unsupported_char_glyph;
947 static void
948 IT_write_glyphs (struct glyph *str, int str_len)
950 unsigned char *screen_buf, *screen_bp, *screen_buf_end, *bp;
951 int unsupported_face = FAST_GLYPH_FACE (Vdos_unsupported_char_glyph);
952 unsigned unsupported_char= FAST_GLYPH_CHAR (Vdos_unsupported_char_glyph);
953 int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
954 register int sl = str_len;
955 register int tlen = GLYPH_TABLE_LENGTH;
956 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
958 /* If terminal_coding does any conversion, use it, otherwise use
959 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
960 because it always returns 1 if terminal_coding.src_multibyte is 1. */
961 struct coding_system *coding =
962 (terminal_coding.common_flags & CODING_REQUIRE_ENCODING_MASK
963 ? &terminal_coding
964 : &safe_terminal_coding);
965 struct frame *sf;
967 /* Do we need to consider conversion of unibyte characters to
968 multibyte? */
969 int convert_unibyte_characters
970 = (NILP (current_buffer->enable_multibyte_characters)
971 && unibyte_display_via_language_environment);
973 unsigned char conversion_buffer[256];
974 int conversion_buffer_size = sizeof conversion_buffer;
976 if (str_len <= 0) return;
978 screen_buf = screen_bp = alloca (str_len * 2);
979 screen_buf_end = screen_buf + str_len * 2;
980 sf = SELECTED_FRAME();
982 /* Since faces get cached and uncached behind our back, we can't
983 rely on their indices in the cache being consistent across
984 invocations. So always reset the screen face to the default
985 face of the frame, before writing glyphs, and let the glyphs
986 set the right face if it's different from the default. */
987 IT_set_face (DEFAULT_FACE_ID);
989 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
990 the tail. */
991 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
992 while (sl)
994 int cf, chlen, enclen;
995 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *buf;
996 unsigned ch;
998 /* Glyphs with GLYPH_MASK_PADDING bit set are actually there
999 only for the redisplay code to know how many columns does
1000 this character occupy on the screen. Skip padding glyphs. */
1001 if (CHAR_GLYPH_PADDING_P (*str))
1003 str++;
1004 sl--;
1006 else
1008 register GLYPH g = GLYPH_FROM_CHAR_GLYPH (*str);
1009 int glyph_not_in_table = 0;
1011 /* If g is negative, it means we have a multibyte character
1012 in *str. That's what GLYPH_FROM_CHAR_GLYPH returns for
1013 multibyte characters. */
1014 if (g < 0 || g >= tlen)
1016 /* This glyph doesn't have an entry in Vglyph_table. */
1017 ch = str->u.ch;
1018 glyph_not_in_table = 1;
1020 else
1022 /* This glyph has an entry in Vglyph_table, so process
1023 any aliases before testing for simpleness. */
1024 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
1025 ch = FAST_GLYPH_CHAR (g);
1028 /* Convert the character code to multibyte, if they
1029 requested display via language environment. We only want
1030 to convert unibyte characters to multibyte in unibyte
1031 buffers! Otherwise, the 8-bit value in CH came from the
1032 display table set up to display foreign characters. */
1033 if (SINGLE_BYTE_CHAR_P (ch) && convert_unibyte_characters
1034 && (ch >= 0240
1035 || (ch >= 0200 && !NILP (Vnonascii_translation_table))))
1036 ch = unibyte_char_to_multibyte (ch);
1038 /* Invalid characters are displayed with a special glyph. */
1039 if (! CHAR_VALID_P (ch, 0))
1041 g = !NILP (Vdos_unsupported_char_glyph)
1042 ? Vdos_unsupported_char_glyph
1043 : MAKE_GLYPH (sf, '\177', GLYPH_FACE (sf, g));
1044 ch = FAST_GLYPH_CHAR (g);
1047 /* If the face of this glyph is different from the current
1048 screen face, update the screen attribute byte. */
1049 cf = str->face_id;
1050 if (cf != screen_face)
1051 IT_set_face (cf); /* handles invalid faces gracefully */
1053 if (glyph_not_in_table || GLYPH_SIMPLE_P (tbase, tlen, g))
1055 /* We generate the multi-byte form of CH in WORKBUF. */
1056 chlen = CHAR_STRING (ch, workbuf);
1057 buf = workbuf;
1059 else
1061 /* We have a string in Vglyph_table. */
1062 chlen = GLYPH_LENGTH (tbase, g);
1063 buf = GLYPH_STRING (tbase, g);
1066 /* If the character is not multibyte, don't bother converting it. */
1067 if (chlen == 1)
1069 *conversion_buffer = (unsigned char)ch;
1070 chlen = 0;
1071 enclen = 1;
1073 else
1075 coding->src_multibyte = 1;
1076 encode_coding (coding, buf, conversion_buffer, chlen,
1077 conversion_buffer_size);
1078 chlen -= coding->consumed;
1079 enclen = coding->produced;
1081 /* Replace glyph codes that cannot be converted by
1082 terminal_coding with Vdos_unsupported_char_glyph. */
1083 if (*conversion_buffer == '?')
1085 unsigned char *cbp = conversion_buffer;
1087 while (cbp < conversion_buffer + enclen && *cbp == '?')
1088 *cbp++ = unsupported_char;
1089 if (unsupported_face != screen_face)
1090 IT_set_face (unsupported_face);
1094 if (enclen + chlen > screen_buf_end - screen_bp)
1096 /* The allocated buffer for screen writes is too small.
1097 Flush it and loop again without incrementing STR, so
1098 that the next loop will begin with the same glyph. */
1099 int nbytes = screen_bp - screen_buf;
1101 mouse_off_maybe ();
1102 dosmemput (screen_buf, nbytes, (int)ScreenPrimary + offset);
1103 if (screen_virtual_segment)
1104 dosv_refresh_virtual_screen (offset, nbytes / 2);
1105 new_pos_X += nbytes / 2;
1106 offset += nbytes;
1108 /* Prepare to reuse the same buffer again. */
1109 screen_bp = screen_buf;
1111 else
1113 /* There's enough place in the allocated buffer to add
1114 the encoding of this glyph. */
1116 /* First, copy the encoded bytes. */
1117 for (bp = conversion_buffer; enclen--; bp++)
1119 *screen_bp++ = (unsigned char)*bp;
1120 *screen_bp++ = ScreenAttrib;
1121 if (termscript)
1122 fputc (*bp, termscript);
1125 /* Now copy the bytes not consumed by the encoding. */
1126 if (chlen > 0)
1128 buf += coding->consumed;
1129 while (chlen--)
1131 if (termscript)
1132 fputc (*buf, termscript);
1133 *screen_bp++ = (unsigned char)*buf++;
1134 *screen_bp++ = ScreenAttrib;
1138 /* Update STR and its remaining length. */
1139 str++;
1140 sl--;
1145 /* Dump whatever is left in the screen buffer. */
1146 mouse_off_maybe ();
1147 dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
1148 if (screen_virtual_segment)
1149 dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
1150 new_pos_X += (screen_bp - screen_buf) / 2;
1152 /* We may have to output some codes to terminate the writing. */
1153 if (CODING_REQUIRE_FLUSHING (coding))
1155 coding->mode |= CODING_MODE_LAST_BLOCK;
1156 encode_coding (coding, "", conversion_buffer, 0, conversion_buffer_size);
1157 if (coding->produced > 0)
1159 screen_buf = alloca (coding->produced * 2);
1160 for (screen_bp = screen_buf, bp = conversion_buffer;
1161 coding->produced--; bp++)
1163 *screen_bp++ = (unsigned char)*bp;
1164 *screen_bp++ = ScreenAttrib;
1165 if (termscript)
1166 fputc (*bp, termscript);
1168 offset += screen_bp - screen_buf;
1169 mouse_off_maybe ();
1170 dosmemput (screen_buf, screen_bp - screen_buf,
1171 (int)ScreenPrimary + offset);
1172 if (screen_virtual_segment)
1173 dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
1174 new_pos_X += (screen_bp - screen_buf) / 2;
1179 /************************************************************************
1180 Mouse Highlight (and friends..)
1181 ************************************************************************/
1183 /* Last window where we saw the mouse. Used by mouse-autoselect-window. */
1184 static Lisp_Object last_mouse_window;
1186 static int mouse_preempted = 0; /* non-zero when XMenu gobbles mouse events */
1188 /* Set the mouse pointer shape according to whether it is in the
1189 area where the mouse highlight is in effect. */
1190 static void
1191 IT_set_mouse_pointer (int mode)
1193 /* A no-op for now. DOS text-mode mouse pointer doesn't offer too
1194 many possibilities to change its shape, and the available
1195 functionality pretty much sucks (e.g., almost every reasonable
1196 shape will conceal the character it is on). Since the color of
1197 the pointer changes in the highlighted area, it is not clear to
1198 me whether anything else is required, anyway. */
1201 /* Display the active region described by mouse_face_*
1202 in its mouse-face if HL > 0, in its normal face if HL = 0. */
1203 static void
1204 show_mouse_face (struct display_info *dpyinfo, int hl)
1206 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
1207 struct frame *f = XFRAME (WINDOW_FRAME (w));
1208 int i;
1209 struct face *fp;
1212 /* If window is in the process of being destroyed, don't bother
1213 doing anything. */
1214 if (w->current_matrix == NULL)
1215 goto set_cursor_shape;
1217 /* Recognize when we are called to operate on rows that don't exist
1218 anymore. This can happen when a window is split. */
1219 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
1220 goto set_cursor_shape;
1222 /* There's no sense to do anything if the mouse face isn't realized. */
1223 if (hl > 0)
1225 if (dpyinfo->mouse_face_hidden)
1226 goto set_cursor_shape;
1228 fp = FACE_FROM_ID (SELECTED_FRAME(), dpyinfo->mouse_face_face_id);
1229 if (!fp)
1230 goto set_cursor_shape;
1233 /* Note that mouse_face_beg_row etc. are window relative. */
1234 for (i = dpyinfo->mouse_face_beg_row;
1235 i <= dpyinfo->mouse_face_end_row;
1236 i++)
1238 int start_hpos, end_hpos;
1239 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
1241 /* Don't do anything if row doesn't have valid contents. */
1242 if (!row->enabled_p)
1243 continue;
1245 /* For all but the first row, the highlight starts at column 0. */
1246 if (i == dpyinfo->mouse_face_beg_row)
1247 start_hpos = dpyinfo->mouse_face_beg_col;
1248 else
1249 start_hpos = 0;
1251 if (i == dpyinfo->mouse_face_end_row)
1252 end_hpos = dpyinfo->mouse_face_end_col;
1253 else
1254 end_hpos = row->used[TEXT_AREA];
1256 if (end_hpos <= start_hpos)
1257 continue;
1258 /* Record that some glyphs of this row are displayed in
1259 mouse-face. */
1260 row->mouse_face_p = hl > 0;
1261 if (hl > 0)
1263 int vpos = row->y + WINDOW_TOP_EDGE_Y (w);
1264 int kstart = start_hpos + WINDOW_LEFT_EDGE_X (w);
1265 int nglyphs = end_hpos - start_hpos;
1266 int offset = ScreenPrimary + 2*(vpos*screen_size_X + kstart) + 1;
1267 int start_offset = offset;
1269 if (termscript)
1270 fprintf (termscript, "\n<MH+ %d-%d:%d>",
1271 kstart, kstart + nglyphs - 1, vpos);
1273 mouse_off ();
1274 IT_set_face (dpyinfo->mouse_face_face_id);
1275 /* Since we are going to change only the _colors_ of the
1276 displayed text, there's no need to go through all the
1277 pain of generating and encoding the text from the glyphs.
1278 Instead, we simply poke the attribute byte of each
1279 affected position in video memory with the colors
1280 computed by IT_set_face! */
1281 _farsetsel (_dos_ds);
1282 while (nglyphs--)
1284 _farnspokeb (offset, ScreenAttrib);
1285 offset += 2;
1287 if (screen_virtual_segment)
1288 dosv_refresh_virtual_screen (start_offset, end_hpos - start_hpos);
1289 mouse_on ();
1291 else
1293 /* We are removing a previously-drawn mouse highlight. The
1294 safest way to do so is to redraw the glyphs anew, since
1295 all kinds of faces and display tables could have changed
1296 behind our back. */
1297 int nglyphs = end_hpos - start_hpos;
1298 int save_x = new_pos_X, save_y = new_pos_Y;
1300 if (end_hpos >= row->used[TEXT_AREA])
1301 nglyphs = row->used[TEXT_AREA] - start_hpos;
1303 /* IT_write_glyphs writes at cursor position, so we need to
1304 temporarily move cursor coordinates to the beginning of
1305 the highlight region. */
1306 new_pos_X = start_hpos + WINDOW_LEFT_EDGE_X (w);
1307 new_pos_Y = row->y + WINDOW_TOP_EDGE_Y (w);
1309 if (termscript)
1310 fprintf (termscript, "<MH- %d-%d:%d>",
1311 new_pos_X, new_pos_X + nglyphs - 1, new_pos_Y);
1312 IT_write_glyphs (row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
1313 if (termscript)
1314 fputs ("\n", termscript);
1315 new_pos_X = save_x;
1316 new_pos_Y = save_y;
1320 set_cursor_shape:
1321 /* Change the mouse pointer shape. */
1322 IT_set_mouse_pointer (hl);
1325 /* Clear out the mouse-highlighted active region.
1326 Redraw it un-highlighted first. */
1327 static void
1328 clear_mouse_face (struct display_info *dpyinfo)
1330 if (! NILP (dpyinfo->mouse_face_window))
1331 show_mouse_face (dpyinfo, 0);
1333 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
1334 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
1335 dpyinfo->mouse_face_window = Qnil;
1338 /* Find the glyph matrix position of buffer position POS in window W.
1339 *HPOS and *VPOS are set to the positions found. W's current glyphs
1340 must be up to date. If POS is above window start return (0, 0).
1341 If POS is after end of W, return end of last line in W. */
1342 static int
1343 fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
1345 int i, lastcol, line_start_position, maybe_next_line_p = 0;
1346 int yb = window_text_bottom_y (w);
1347 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0), *best_row = row;
1349 while (row->y < yb)
1351 if (row->used[TEXT_AREA])
1352 line_start_position = row->glyphs[TEXT_AREA]->charpos;
1353 else
1354 line_start_position = 0;
1356 if (line_start_position > pos)
1357 break;
1358 /* If the position sought is the end of the buffer,
1359 don't include the blank lines at the bottom of the window. */
1360 else if (line_start_position == pos
1361 && pos == BUF_ZV (XBUFFER (w->buffer)))
1363 maybe_next_line_p = 1;
1364 break;
1366 else if (line_start_position > 0)
1367 best_row = row;
1369 /* Don't overstep the last matrix row, lest we get into the
1370 never-never land... */
1371 if (row->y + 1 >= yb)
1372 break;
1374 ++row;
1377 /* Find the right column within BEST_ROW. */
1378 lastcol = 0;
1379 row = best_row;
1380 for (i = 0; i < row->used[TEXT_AREA]; i++)
1382 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
1383 int charpos;
1385 charpos = glyph->charpos;
1386 if (charpos == pos)
1388 *hpos = i;
1389 *vpos = row->y;
1390 return 1;
1392 else if (charpos > pos)
1393 break;
1394 else if (charpos > 0)
1395 lastcol = i;
1398 /* If we're looking for the end of the buffer,
1399 and we didn't find it in the line we scanned,
1400 use the start of the following line. */
1401 if (maybe_next_line_p)
1403 ++row;
1404 lastcol = 0;
1407 *vpos = row->y;
1408 *hpos = lastcol + 1;
1409 return 0;
1412 /* Take proper action when mouse has moved to the mode or top line of
1413 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
1414 mode line. X is relative to the start of the text display area of
1415 W, so the width of fringes and scroll bars must be subtracted
1416 to get a position relative to the start of the mode line. */
1417 static void
1418 IT_note_mode_line_highlight (struct window *w, int x, int mode_line_p)
1420 struct frame *f = XFRAME (w->frame);
1421 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1422 struct glyph_row *row;
1424 if (mode_line_p)
1425 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1426 else
1427 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1429 if (row->enabled_p)
1431 extern Lisp_Object Qhelp_echo;
1432 struct glyph *glyph, *end;
1433 Lisp_Object help, map;
1435 /* Find the glyph under X. */
1436 glyph = (row->glyphs[TEXT_AREA]
1438 /* Does MS-DOG really support scroll-bars?? ++KFS */
1439 - WINDOW_LEFT_SCROLL_BAR_AREA_WIDTH (w));
1440 end = glyph + row->used[TEXT_AREA];
1441 if (glyph < end
1442 && STRINGP (glyph->object)
1443 && STRING_INTERVALS (glyph->object)
1444 && glyph->charpos >= 0
1445 && glyph->charpos < SCHARS (glyph->object))
1447 /* If we're on a string with `help-echo' text property,
1448 arrange for the help to be displayed. This is done by
1449 setting the global variable help_echo to the help string. */
1450 help = Fget_text_property (make_number (glyph->charpos),
1451 Qhelp_echo, glyph->object);
1452 if (!NILP (help))
1454 help_echo_string = help;
1455 XSETWINDOW (help_echo_window, w);
1456 help_echo_object = glyph->object;
1457 help_echo_pos = glyph->charpos;
1463 /* Take proper action when the mouse has moved to position X, Y on
1464 frame F as regards highlighting characters that have mouse-face
1465 properties. Also de-highlighting chars where the mouse was before.
1466 X and Y can be negative or out of range. */
1467 static void
1468 IT_note_mouse_highlight (struct frame *f, int x, int y)
1470 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1471 enum window_part part = ON_NOTHING;
1472 Lisp_Object window;
1473 struct window *w;
1475 /* When a menu is active, don't highlight because this looks odd. */
1476 if (mouse_preempted)
1477 return;
1479 if (NILP (Vmouse_highlight)
1480 || !f->glyphs_initialized_p)
1481 return;
1483 dpyinfo->mouse_face_mouse_x = x;
1484 dpyinfo->mouse_face_mouse_y = y;
1485 dpyinfo->mouse_face_mouse_frame = f;
1487 if (dpyinfo->mouse_face_defer)
1488 return;
1490 if (gc_in_progress)
1492 dpyinfo->mouse_face_deferred_gc = 1;
1493 return;
1496 /* Which window is that in? */
1497 window = window_from_coordinates (f, x, y, &part, &x, &y, 0);
1499 /* If we were displaying active text in another window, clear that. */
1500 if (! EQ (window, dpyinfo->mouse_face_window))
1501 clear_mouse_face (dpyinfo);
1503 /* Not on a window -> return. */
1504 if (!WINDOWP (window))
1505 return;
1507 /* Convert to window-relative coordinates. */
1508 w = XWINDOW (window);
1510 if (part == ON_MODE_LINE || part == ON_HEADER_LINE)
1512 /* Mouse is on the mode or top line. */
1513 IT_note_mode_line_highlight (w, x, part == ON_MODE_LINE);
1514 return;
1517 IT_set_mouse_pointer (0);
1519 /* Are we in a window whose display is up to date?
1520 And verify the buffer's text has not changed. */
1521 if (part == ON_TEXT
1522 && EQ (w->window_end_valid, w->buffer)
1523 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
1524 && (XFASTINT (w->last_overlay_modified)
1525 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
1527 int pos, i, nrows = w->current_matrix->nrows;
1528 struct glyph_row *row;
1529 struct glyph *glyph;
1531 /* Find the glyph under X/Y. */
1532 glyph = NULL;
1533 if (y >= 0 && y < nrows)
1535 row = MATRIX_ROW (w->current_matrix, y);
1536 /* Give up if some row before the one we are looking for is
1537 not enabled. */
1538 for (i = 0; i <= y; i++)
1539 if (!MATRIX_ROW (w->current_matrix, i)->enabled_p)
1540 break;
1541 if (i > y /* all rows upto and including the one at Y are enabled */
1542 && row->displays_text_p
1543 && x < window_box_width (w, TEXT_AREA))
1545 glyph = row->glyphs[TEXT_AREA];
1546 if (x >= row->used[TEXT_AREA])
1547 glyph = NULL;
1548 else
1550 glyph += x;
1551 if (!BUFFERP (glyph->object))
1552 glyph = NULL;
1557 /* Clear mouse face if X/Y not over text. */
1558 if (glyph == NULL)
1560 clear_mouse_face (dpyinfo);
1561 return;
1564 if (!BUFFERP (glyph->object))
1565 abort ();
1566 pos = glyph->charpos;
1568 /* Check for mouse-face and help-echo. */
1570 extern Lisp_Object Qmouse_face;
1571 Lisp_Object mouse_face, overlay, position, *overlay_vec;
1572 int len, noverlays, obegv, ozv;;
1573 struct buffer *obuf;
1575 /* If we get an out-of-range value, return now; avoid an error. */
1576 if (pos > BUF_Z (XBUFFER (w->buffer)))
1577 return;
1579 /* Make the window's buffer temporarily current for
1580 overlays_at and compute_char_face. */
1581 obuf = current_buffer;
1582 current_buffer = XBUFFER (w->buffer);
1583 obegv = BEGV;
1584 ozv = ZV;
1585 BEGV = BEG;
1586 ZV = Z;
1588 /* Is this char mouse-active or does it have help-echo? */
1589 XSETINT (position, pos);
1591 /* Put all the overlays we want in a vector in overlay_vec.
1592 Store the length in len. If there are more than 10, make
1593 enough space for all, and try again. */
1594 len = 10;
1595 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1596 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL, 0);
1597 if (noverlays > len)
1599 len = noverlays;
1600 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1601 noverlays = overlays_at (pos,
1602 0, &overlay_vec, &len, NULL, NULL, 0);
1605 /* Sort overlays into increasing priority order. */
1606 noverlays = sort_overlays (overlay_vec, noverlays, w);
1608 /* Check mouse-face highlighting. */
1609 if (! (EQ (window, dpyinfo->mouse_face_window)
1610 && y >= dpyinfo->mouse_face_beg_row
1611 && y <= dpyinfo->mouse_face_end_row
1612 && (y > dpyinfo->mouse_face_beg_row
1613 || x >= dpyinfo->mouse_face_beg_col)
1614 && (y < dpyinfo->mouse_face_end_row
1615 || x < dpyinfo->mouse_face_end_col
1616 || dpyinfo->mouse_face_past_end)))
1618 /* Clear the display of the old active region, if any. */
1619 clear_mouse_face (dpyinfo);
1621 /* Find highest priority overlay that has a mouse-face prop. */
1622 overlay = Qnil;
1623 for (i = noverlays - 1; i >= 0; --i)
1625 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
1626 if (!NILP (mouse_face))
1628 overlay = overlay_vec[i];
1629 break;
1633 /* If no overlay applies, get a text property. */
1634 if (NILP (overlay))
1635 mouse_face = Fget_text_property (position, Qmouse_face,
1636 w->buffer);
1638 /* Handle the overlay case. */
1639 if (! NILP (overlay))
1641 /* Find the range of text around this char that
1642 should be active. */
1643 Lisp_Object before, after;
1644 int ignore;
1646 before = Foverlay_start (overlay);
1647 after = Foverlay_end (overlay);
1648 /* Record this as the current active region. */
1649 fast_find_position (w, XFASTINT (before),
1650 &dpyinfo->mouse_face_beg_col,
1651 &dpyinfo->mouse_face_beg_row);
1652 dpyinfo->mouse_face_past_end
1653 = !fast_find_position (w, XFASTINT (after),
1654 &dpyinfo->mouse_face_end_col,
1655 &dpyinfo->mouse_face_end_row);
1656 dpyinfo->mouse_face_window = window;
1657 dpyinfo->mouse_face_face_id
1658 = face_at_buffer_position (w, pos, 0, 0,
1659 &ignore, pos + 1,
1660 !dpyinfo->mouse_face_hidden);
1662 /* Display it as active. */
1663 show_mouse_face (dpyinfo, 1);
1665 /* Handle the text property case. */
1666 else if (! NILP (mouse_face))
1668 /* Find the range of text around this char that
1669 should be active. */
1670 Lisp_Object before, after, beginning, end;
1671 int ignore;
1673 beginning = Fmarker_position (w->start);
1674 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
1675 - XFASTINT (w->window_end_pos)));
1676 before
1677 = Fprevious_single_property_change (make_number (pos + 1),
1678 Qmouse_face,
1679 w->buffer, beginning);
1680 after
1681 = Fnext_single_property_change (position, Qmouse_face,
1682 w->buffer, end);
1683 /* Record this as the current active region. */
1684 fast_find_position (w, XFASTINT (before),
1685 &dpyinfo->mouse_face_beg_col,
1686 &dpyinfo->mouse_face_beg_row);
1687 dpyinfo->mouse_face_past_end
1688 = !fast_find_position (w, XFASTINT (after),
1689 &dpyinfo->mouse_face_end_col,
1690 &dpyinfo->mouse_face_end_row);
1691 dpyinfo->mouse_face_window = window;
1692 dpyinfo->mouse_face_face_id
1693 = face_at_buffer_position (w, pos, 0, 0,
1694 &ignore, pos + 1,
1695 !dpyinfo->mouse_face_hidden);
1697 /* Display it as active. */
1698 show_mouse_face (dpyinfo, 1);
1702 /* Look for a `help-echo' property. */
1704 Lisp_Object help;
1705 extern Lisp_Object Qhelp_echo;
1707 /* Check overlays first. */
1708 help = Qnil;
1709 for (i = noverlays - 1; i >= 0 && NILP (help); --i)
1711 overlay = overlay_vec[i];
1712 help = Foverlay_get (overlay, Qhelp_echo);
1715 if (!NILP (help))
1717 help_echo_string = help;
1718 help_echo_window = window;
1719 help_echo_object = overlay;
1720 help_echo_pos = pos;
1722 /* Try text properties. */
1723 else if (NILP (help)
1724 && ((STRINGP (glyph->object)
1725 && glyph->charpos >= 0
1726 && glyph->charpos < SCHARS (glyph->object))
1727 || (BUFFERP (glyph->object)
1728 && glyph->charpos >= BEGV
1729 && glyph->charpos < ZV)))
1731 help = Fget_text_property (make_number (glyph->charpos),
1732 Qhelp_echo, glyph->object);
1733 if (!NILP (help))
1735 help_echo_string = help;
1736 help_echo_window = window;
1737 help_echo_object = glyph->object;
1738 help_echo_pos = glyph->charpos;
1743 BEGV = obegv;
1744 ZV = ozv;
1745 current_buffer = obuf;
1750 static void
1751 IT_clear_end_of_line (int first_unused)
1753 char *spaces, *sp;
1754 int i, j, offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
1755 extern int fatal_error_in_progress;
1757 if (new_pos_X >= first_unused || fatal_error_in_progress)
1758 return;
1760 IT_set_face (0);
1761 i = (j = first_unused - new_pos_X) * 2;
1762 if (termscript)
1763 fprintf (termscript, "<CLR:EOL[%d..%d)>", new_pos_X, first_unused);
1764 spaces = sp = alloca (i);
1766 while (--j >= 0)
1768 *sp++ = ' ';
1769 *sp++ = ScreenAttrib;
1772 mouse_off_maybe ();
1773 dosmemput (spaces, i, (int)ScreenPrimary + offset);
1774 if (screen_virtual_segment)
1775 dosv_refresh_virtual_screen (offset, i / 2);
1777 /* clear_end_of_line_raw on term.c leaves the cursor at first_unused.
1778 Let's follow their lead, in case someone relies on this. */
1779 new_pos_X = first_unused;
1782 static void
1783 IT_clear_screen (void)
1785 if (termscript)
1786 fprintf (termscript, "<CLR:SCR>");
1787 /* We are sometimes called (from clear_garbaged_frames) when a new
1788 frame is being created, but its faces are not yet realized. In
1789 such a case we cannot call IT_set_face, since it will fail to find
1790 any valid faces and will abort. Instead, use the initial screen
1791 colors; that should mimic what a Unix tty does, which simply clears
1792 the screen with whatever default colors are in use. */
1793 if (FACE_FROM_ID (SELECTED_FRAME (), DEFAULT_FACE_ID) == NULL)
1794 ScreenAttrib = (initial_screen_colors[0] << 4) | initial_screen_colors[1];
1795 else
1796 IT_set_face (0);
1797 mouse_off ();
1798 ScreenClear ();
1799 if (screen_virtual_segment)
1800 dosv_refresh_virtual_screen (0, screen_size);
1801 new_pos_X = new_pos_Y = 0;
1804 static void
1805 IT_clear_to_end (void)
1807 if (termscript)
1808 fprintf (termscript, "<CLR:EOS>");
1810 while (new_pos_Y < screen_size_Y) {
1811 new_pos_X = 0;
1812 IT_clear_end_of_line (screen_size_X);
1813 new_pos_Y++;
1817 static void
1818 IT_cursor_to (int y, int x)
1820 if (termscript)
1821 fprintf (termscript, "\n<XY=%dx%d>", x, y);
1822 new_pos_X = x;
1823 new_pos_Y = y;
1826 static int cursor_cleared;
1828 static void
1829 IT_display_cursor (int on)
1831 if (on && cursor_cleared)
1833 ScreenSetCursor (current_pos_Y, current_pos_X);
1834 cursor_cleared = 0;
1836 else if (!on && !cursor_cleared)
1838 ScreenSetCursor (-1, -1);
1839 cursor_cleared = 1;
1843 /* Emacs calls cursor-movement functions a lot when it updates the
1844 display (probably a legacy of old terminals where you cannot
1845 update a screen line without first moving the cursor there).
1846 However, cursor movement is expensive on MSDOS (it calls a slow
1847 BIOS function and requires 2 mode switches), while actual screen
1848 updates access the video memory directly and don't depend on
1849 cursor position. To avoid slowing down the redisplay, we cheat:
1850 all functions that move the cursor only set internal variables
1851 which record the cursor position, whereas the cursor is only
1852 moved to its final position whenever screen update is complete.
1854 `IT_cmgoto' is called from the keyboard reading loop and when the
1855 frame update is complete. This means that we are ready for user
1856 input, so we update the cursor position to show where the point is,
1857 and also make the mouse pointer visible.
1859 Special treatment is required when the cursor is in the echo area,
1860 to put the cursor at the end of the text displayed there. */
1862 static void
1863 IT_cmgoto (FRAME_PTR f)
1865 /* Only set the cursor to where it should be if the display is
1866 already in sync with the window contents. */
1867 int update_cursor_pos = 1; /* MODIFF == unchanged_modified; */
1869 /* FIXME: This needs to be rewritten for the new redisplay, or
1870 removed. */
1871 #if 0
1872 static int previous_pos_X = -1;
1874 update_cursor_pos = 1; /* temporary!!! */
1876 /* If the display is in sync, forget any previous knowledge about
1877 cursor position. This is primarily for unexpected events like
1878 C-g in the minibuffer. */
1879 if (update_cursor_pos && previous_pos_X >= 0)
1880 previous_pos_X = -1;
1881 /* If we are in the echo area, put the cursor at the
1882 end of the echo area message. */
1883 if (!update_cursor_pos
1884 && WINDOW_TOP_EDGE_LINE (XWINDOW (FRAME_MINIBUF_WINDOW (f))) <= new_pos_Y)
1886 int tem_X = current_pos_X, dummy;
1888 if (echo_area_glyphs)
1890 tem_X = echo_area_glyphs_length;
1891 /* Save current cursor position, to be restored after the
1892 echo area message is erased. Only remember one level
1893 of previous cursor position. */
1894 if (previous_pos_X == -1)
1895 ScreenGetCursor (&dummy, &previous_pos_X);
1897 else if (previous_pos_X >= 0)
1899 /* We wind up here after the echo area message is erased.
1900 Restore the cursor position we remembered above. */
1901 tem_X = previous_pos_X;
1902 previous_pos_X = -1;
1905 if (current_pos_X != tem_X)
1907 new_pos_X = tem_X;
1908 update_cursor_pos = 1;
1911 #endif
1913 if (update_cursor_pos
1914 && (current_pos_X != new_pos_X || current_pos_Y != new_pos_Y))
1916 ScreenSetCursor (current_pos_Y = new_pos_Y, current_pos_X = new_pos_X);
1917 if (termscript)
1918 fprintf (termscript, "\n<CURSOR:%dx%d>", current_pos_X, current_pos_Y);
1921 /* Maybe cursor is invisible, so make it visible. */
1922 IT_display_cursor (1);
1924 /* Mouse pointer should be always visible if we are waiting for
1925 keyboard input. */
1926 if (!mouse_visible)
1927 mouse_on ();
1930 static void
1931 IT_update_begin (struct frame *f)
1933 struct display_info *display_info = FRAME_X_DISPLAY_INFO (f);
1934 struct frame *mouse_face_frame = display_info->mouse_face_mouse_frame;
1936 BLOCK_INPUT;
1938 if (f && f == mouse_face_frame)
1940 /* Don't do highlighting for mouse motion during the update. */
1941 display_info->mouse_face_defer = 1;
1943 /* If F needs to be redrawn, simply forget about any prior mouse
1944 highlighting. */
1945 if (FRAME_GARBAGED_P (f))
1946 display_info->mouse_face_window = Qnil;
1948 /* Can we tell that this update does not affect the window
1949 where the mouse highlight is? If so, no need to turn off.
1950 Likewise, don't do anything if none of the enabled rows
1951 contains glyphs highlighted in mouse face. */
1952 if (!NILP (display_info->mouse_face_window)
1953 && WINDOWP (display_info->mouse_face_window))
1955 struct window *w = XWINDOW (display_info->mouse_face_window);
1956 int i;
1958 /* If the mouse highlight is in the window that was deleted
1959 (e.g., if it was popped by completion), clear highlight
1960 unconditionally. */
1961 if (NILP (w->buffer))
1962 display_info->mouse_face_window = Qnil;
1963 else
1965 for (i = 0; i < w->desired_matrix->nrows; ++i)
1966 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i)
1967 && MATRIX_ROW (w->current_matrix, i)->mouse_face_p)
1968 break;
1971 if (NILP (w->buffer) || i < w->desired_matrix->nrows)
1972 clear_mouse_face (display_info);
1975 else if (mouse_face_frame && !FRAME_LIVE_P (mouse_face_frame))
1977 /* If the frame with mouse highlight was deleted, invalidate the
1978 highlight info. */
1979 display_info->mouse_face_beg_row = display_info->mouse_face_beg_col = -1;
1980 display_info->mouse_face_end_row = display_info->mouse_face_end_col = -1;
1981 display_info->mouse_face_window = Qnil;
1982 display_info->mouse_face_deferred_gc = 0;
1983 display_info->mouse_face_mouse_frame = NULL;
1986 UNBLOCK_INPUT;
1989 static void
1990 IT_update_end (struct frame *f)
1992 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
1995 Lisp_Object Qcursor_type;
1997 static void
1998 IT_frame_up_to_date (struct frame *f)
2000 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
2001 Lisp_Object new_cursor, frame_desired_cursor;
2002 struct window *sw;
2004 if (dpyinfo->mouse_face_deferred_gc
2005 || (f && f == dpyinfo->mouse_face_mouse_frame))
2007 BLOCK_INPUT;
2008 if (dpyinfo->mouse_face_mouse_frame)
2009 IT_note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
2010 dpyinfo->mouse_face_mouse_x,
2011 dpyinfo->mouse_face_mouse_y);
2012 dpyinfo->mouse_face_deferred_gc = 0;
2013 UNBLOCK_INPUT;
2016 /* Set the cursor type to whatever they wanted. In a minibuffer
2017 window, we want the cursor to appear only if we are reading input
2018 from this window, and we want the cursor to be taken from the
2019 frame parameters. For the selected window, we use either its
2020 buffer-local value or the value from the frame parameters if the
2021 buffer doesn't define its local value for the cursor type. */
2022 sw = XWINDOW (f->selected_window);
2023 frame_desired_cursor = Fcdr (Fassq (Qcursor_type, f->param_alist));
2024 if (cursor_in_echo_area
2025 && FRAME_HAS_MINIBUF_P (f)
2026 && EQ (FRAME_MINIBUF_WINDOW (f), echo_area_window)
2027 && sw == XWINDOW (echo_area_window))
2028 new_cursor = frame_desired_cursor;
2029 else
2031 struct buffer *b = XBUFFER (sw->buffer);
2033 if (EQ (b->cursor_type, Qt))
2034 new_cursor = frame_desired_cursor;
2035 else if (NILP (b->cursor_type)) /* nil means no cursor */
2036 new_cursor = Fcons (Qbar, make_number (0));
2037 else
2038 new_cursor = b->cursor_type;
2041 IT_set_cursor_type (f, new_cursor);
2043 IT_cmgoto (f); /* position cursor when update is done */
2046 /* Copy LEN glyphs displayed on a single line whose vertical position
2047 is YPOS, beginning at horizontal position XFROM to horizontal
2048 position XTO, by moving blocks in the video memory. Used by
2049 functions that insert and delete glyphs. */
2050 static void
2051 IT_copy_glyphs (int xfrom, int xto, size_t len, int ypos)
2053 /* The offsets of source and destination relative to the
2054 conventional memorty selector. */
2055 int from = 2 * (xfrom + screen_size_X * ypos) + ScreenPrimary;
2056 int to = 2 * (xto + screen_size_X * ypos) + ScreenPrimary;
2058 if (from == to || len <= 0)
2059 return;
2061 _farsetsel (_dos_ds);
2063 /* The source and destination might overlap, so we need to move
2064 glyphs non-destructively. */
2065 if (from > to)
2067 for ( ; len; from += 2, to += 2, len--)
2068 _farnspokew (to, _farnspeekw (from));
2070 else
2072 from += (len - 1) * 2;
2073 to += (len - 1) * 2;
2074 for ( ; len; from -= 2, to -= 2, len--)
2075 _farnspokew (to, _farnspeekw (from));
2077 if (screen_virtual_segment)
2078 dosv_refresh_virtual_screen (ypos * screen_size_X * 2, screen_size_X);
2081 /* Insert and delete glyphs. */
2082 static void
2083 IT_insert_glyphs (start, len)
2084 register struct glyph *start;
2085 register int len;
2087 int shift_by_width = screen_size_X - (new_pos_X + len);
2089 /* Shift right the glyphs from the nominal cursor position to the
2090 end of this line. */
2091 IT_copy_glyphs (new_pos_X, new_pos_X + len, shift_by_width, new_pos_Y);
2093 /* Now write the glyphs to be inserted. */
2094 IT_write_glyphs (start, len);
2097 static void
2098 IT_delete_glyphs (n)
2099 register int n;
2101 abort ();
2104 /* set-window-configuration on window.c needs this. */
2105 void
2106 x_set_menu_bar_lines (f, value, oldval)
2107 struct frame *f;
2108 Lisp_Object value, oldval;
2110 set_menu_bar_lines (f, value, oldval);
2113 /* This was copied from xfaces.c */
2115 extern Lisp_Object Qbackground_color;
2116 extern Lisp_Object Qforeground_color;
2117 Lisp_Object Qreverse;
2118 extern Lisp_Object Qtitle;
2120 /* IT_set_terminal_modes is called when emacs is started,
2121 resumed, and whenever the screen is redrawn! */
2123 static void
2124 IT_set_terminal_modes (void)
2126 if (termscript)
2127 fprintf (termscript, "\n<SET_TERM>");
2129 screen_size_X = ScreenCols ();
2130 screen_size_Y = ScreenRows ();
2131 screen_size = screen_size_X * screen_size_Y;
2133 new_pos_X = new_pos_Y = 0;
2134 current_pos_X = current_pos_Y = -1;
2136 if (term_setup_done)
2137 return;
2138 term_setup_done = 1;
2140 startup_screen_size_X = screen_size_X;
2141 startup_screen_size_Y = screen_size_Y;
2142 startup_screen_attrib = ScreenAttrib;
2144 #if __DJGPP__ > 1
2145 /* Is DOS/V (or any other RSIS software which relocates
2146 the screen) installed? */
2148 unsigned short es_value;
2149 __dpmi_regs regs;
2151 regs.h.ah = 0xfe; /* get relocated screen address */
2152 if (ScreenPrimary == 0xb0000UL || ScreenPrimary == 0xb8000UL)
2153 regs.x.es = (ScreenPrimary >> 4) & 0xffff;
2154 else if (screen_old_address) /* already switched to Japanese mode once */
2155 regs.x.es = (screen_old_address >> 4) & 0xffff;
2156 else
2157 regs.x.es = ScreenMode () == 7 ? 0xb000 : 0xb800;
2158 regs.x.di = 0;
2159 es_value = regs.x.es;
2160 __dpmi_int (0x10, &regs);
2162 if (regs.x.es != es_value)
2164 /* screen_old_address is only set if ScreenPrimary does NOT
2165 already point to the relocated buffer address returned by
2166 the Int 10h/AX=FEh call above. DJGPP v2.02 and later sets
2167 ScreenPrimary to that address at startup under DOS/V. */
2168 if (regs.x.es != (ScreenPrimary >> 4) & 0xffff)
2169 screen_old_address = ScreenPrimary;
2170 screen_virtual_segment = regs.x.es;
2171 screen_virtual_offset = regs.x.di;
2172 ScreenPrimary = (screen_virtual_segment << 4) + screen_virtual_offset;
2175 #endif /* __DJGPP__ > 1 */
2177 ScreenGetCursor (&startup_pos_Y, &startup_pos_X);
2178 ScreenRetrieve (startup_screen_buffer = xmalloc (screen_size * 2));
2180 if (termscript)
2181 fprintf (termscript, "<SCREEN SAVED (dimensions=%dx%d)>\n",
2182 screen_size_X, screen_size_Y);
2184 bright_bg ();
2187 /* IT_reset_terminal_modes is called when emacs is
2188 suspended or killed. */
2190 static void
2191 IT_reset_terminal_modes (void)
2193 int display_row_start = (int) ScreenPrimary;
2194 int saved_row_len = startup_screen_size_X * 2;
2195 int update_row_len = ScreenCols () * 2, current_rows = ScreenRows ();
2196 int to_next_row = update_row_len;
2197 unsigned char *saved_row = startup_screen_buffer;
2198 int cursor_pos_X = ScreenCols () - 1, cursor_pos_Y = ScreenRows () - 1;
2200 if (termscript)
2201 fprintf (termscript, "\n<RESET_TERM>");
2203 if (!term_setup_done)
2204 return;
2206 mouse_off ();
2208 /* Leave the video system in the same state as we found it,
2209 as far as the blink/bright-background bit is concerned. */
2210 maybe_enable_blinking ();
2212 /* We have a situation here.
2213 We cannot just do ScreenUpdate(startup_screen_buffer) because
2214 the luser could have changed screen dimensions inside Emacs
2215 and failed (or didn't want) to restore them before killing
2216 Emacs. ScreenUpdate() uses the *current* screen dimensions and
2217 thus will happily use memory outside what was allocated for
2218 `startup_screen_buffer'.
2219 Thus we only restore as much as the current screen dimensions
2220 can hold, and clear the rest (if the saved screen is smaller than
2221 the current) with the color attribute saved at startup. The cursor
2222 is also restored within the visible dimensions. */
2224 ScreenAttrib = startup_screen_attrib;
2226 /* Don't restore the screen if we are exiting less than 2 seconds
2227 after startup: we might be crashing, and the screen might show
2228 some vital clues to what's wrong. */
2229 if (clock () - startup_time >= 2*CLOCKS_PER_SEC)
2231 ScreenClear ();
2232 if (screen_virtual_segment)
2233 dosv_refresh_virtual_screen (0, screen_size);
2235 if (update_row_len > saved_row_len)
2236 update_row_len = saved_row_len;
2237 if (current_rows > startup_screen_size_Y)
2238 current_rows = startup_screen_size_Y;
2240 if (termscript)
2241 fprintf (termscript, "<SCREEN RESTORED (dimensions=%dx%d)>\n",
2242 update_row_len / 2, current_rows);
2244 while (current_rows--)
2246 dosmemput (saved_row, update_row_len, display_row_start);
2247 if (screen_virtual_segment)
2248 dosv_refresh_virtual_screen (display_row_start - ScreenPrimary,
2249 update_row_len / 2);
2250 saved_row += saved_row_len;
2251 display_row_start += to_next_row;
2254 if (startup_pos_X < cursor_pos_X)
2255 cursor_pos_X = startup_pos_X;
2256 if (startup_pos_Y < cursor_pos_Y)
2257 cursor_pos_Y = startup_pos_Y;
2259 ScreenSetCursor (cursor_pos_Y, cursor_pos_X);
2260 xfree (startup_screen_buffer);
2262 term_setup_done = 0;
2265 static void
2266 IT_set_terminal_window (int foo)
2270 /* Remember the screen colors of the curent frame, to serve as the
2271 default colors for newly-created frames. */
2272 DEFUN ("msdos-remember-default-colors", Fmsdos_remember_default_colors,
2273 Smsdos_remember_default_colors, 1, 1, 0,
2274 doc: /* Remember the screen colors of the current frame. */)
2275 (frame)
2276 Lisp_Object frame;
2278 struct frame *f;
2280 CHECK_FRAME (frame);
2281 f= XFRAME (frame);
2283 /* This function is called after applying default-frame-alist to the
2284 initial frame. At that time, if reverse-colors option was
2285 specified in default-frame-alist, it was already applied, and
2286 frame colors are reversed. We need to account for that. */
2287 if (EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt))
2289 initial_screen_colors[0] = FRAME_BACKGROUND_PIXEL (f);
2290 initial_screen_colors[1] = FRAME_FOREGROUND_PIXEL (f);
2292 else
2294 initial_screen_colors[0] = FRAME_FOREGROUND_PIXEL (f);
2295 initial_screen_colors[1] = FRAME_BACKGROUND_PIXEL (f);
2299 void
2300 IT_set_frame_parameters (f, alist)
2301 struct frame *f;
2302 Lisp_Object alist;
2304 Lisp_Object tail;
2305 int i, j, length = XINT (Flength (alist));
2306 Lisp_Object *parms
2307 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2308 Lisp_Object *values
2309 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2310 /* Do we have to reverse the foreground and background colors? */
2311 int reverse = EQ (Fcdr (Fassq (Qreverse, f->param_alist)), Qt);
2312 int need_to_reverse, was_reverse = reverse;
2313 int redraw = 0, fg_set = 0, bg_set = 0;
2314 unsigned long orig_fg, orig_bg;
2315 Lisp_Object frame_bg, frame_fg;
2316 extern Lisp_Object Qdefault, QCforeground, QCbackground;
2318 /* If we are creating a new frame, begin with the original screen colors
2319 used for the initial frame. */
2320 if (alist == Vdefault_frame_alist
2321 && initial_screen_colors[0] != -1 && initial_screen_colors[1] != -1)
2323 FRAME_FOREGROUND_PIXEL (f) = initial_screen_colors[0];
2324 FRAME_BACKGROUND_PIXEL (f) = initial_screen_colors[1];
2326 orig_fg = FRAME_FOREGROUND_PIXEL (f);
2327 orig_bg = FRAME_BACKGROUND_PIXEL (f);
2328 frame_fg = Fcdr (Fassq (Qforeground_color, f->param_alist));
2329 frame_bg = Fcdr (Fassq (Qbackground_color, f->param_alist));
2330 /* frame_fg and frame_bg could be nil if, for example,
2331 f->param_alist is nil, e.g. if we are called from
2332 Fmake_terminal_frame. */
2333 if (NILP (frame_fg))
2334 frame_fg = build_string (unspecified_fg);
2335 if (NILP (frame_bg))
2336 frame_bg = build_string (unspecified_bg);
2338 /* Extract parm names and values into those vectors. */
2339 i = 0;
2340 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
2342 Lisp_Object elt;
2344 elt = Fcar (tail);
2345 parms[i] = Fcar (elt);
2346 CHECK_SYMBOL (parms[i]);
2347 values[i] = Fcdr (elt);
2348 i++;
2351 j = i;
2353 for (i = 0; i < j; i++)
2355 Lisp_Object prop, val;
2357 prop = parms[i];
2358 val = values[i];
2360 if (EQ (prop, Qreverse))
2361 reverse = EQ (val, Qt);
2364 need_to_reverse = reverse && !was_reverse;
2365 if (termscript && need_to_reverse)
2366 fprintf (termscript, "<INVERSE-VIDEO>\n");
2368 /* Now process the alist elements in reverse of specified order. */
2369 for (i--; i >= 0; i--)
2371 Lisp_Object prop, val, frame;
2373 prop = parms[i];
2374 val = values[i];
2376 if (EQ (prop, Qforeground_color))
2378 unsigned long new_color = load_color (f, NULL, val, need_to_reverse
2379 ? LFACE_BACKGROUND_INDEX
2380 : LFACE_FOREGROUND_INDEX);
2381 if (new_color != FACE_TTY_DEFAULT_COLOR
2382 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2383 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
2385 FRAME_FOREGROUND_PIXEL (f) = new_color;
2386 /* Make sure the foreground of the default face for this
2387 frame is changed as well. */
2388 XSETFRAME (frame, f);
2389 if (need_to_reverse)
2391 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2392 val, frame);
2393 prop = Qbackground_color;
2394 bg_set = 1;
2396 else
2398 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2399 val, frame);
2400 fg_set = 1;
2402 redraw = 1;
2403 if (termscript)
2404 fprintf (termscript, "<FGCOLOR %lu>\n", new_color);
2407 else if (EQ (prop, Qbackground_color))
2409 unsigned long new_color = load_color (f, NULL, val, need_to_reverse
2410 ? LFACE_FOREGROUND_INDEX
2411 : LFACE_BACKGROUND_INDEX);
2412 if (new_color != FACE_TTY_DEFAULT_COLOR
2413 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2414 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
2416 FRAME_BACKGROUND_PIXEL (f) = new_color;
2417 /* Make sure the background of the default face for this
2418 frame is changed as well. */
2419 XSETFRAME (frame, f);
2420 if (need_to_reverse)
2422 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2423 val, frame);
2424 prop = Qforeground_color;
2425 fg_set = 1;
2427 else
2429 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2430 val, frame);
2431 bg_set = 1;
2433 redraw = 1;
2434 if (termscript)
2435 fprintf (termscript, "<BGCOLOR %lu>\n", new_color);
2438 else if (EQ (prop, Qtitle))
2440 x_set_title (f, val);
2441 if (termscript)
2442 fprintf (termscript, "<TITLE: %s>\n", SDATA (val));
2444 else if (EQ (prop, Qcursor_type))
2446 IT_set_cursor_type (f, val);
2447 if (termscript)
2448 fprintf (termscript, "<CTYPE: %s>\n",
2449 EQ (val, Qbar) || EQ (val, Qhbar)
2450 || CONSP (val) && (EQ (XCAR (val), Qbar)
2451 || EQ (XCAR (val), Qhbar))
2452 ? "bar" : "box");
2454 store_frame_param (f, prop, val);
2457 /* If they specified "reverse", but not the colors, we need to swap
2458 the current frame colors. */
2459 if (need_to_reverse)
2461 Lisp_Object frame;
2463 if (!fg_set)
2465 XSETFRAME (frame, f);
2466 Finternal_set_lisp_face_attribute (Qdefault, QCforeground,
2467 tty_color_name (f, orig_bg),
2468 frame);
2469 redraw = 1;
2471 if (!bg_set)
2473 XSETFRAME (frame, f);
2474 Finternal_set_lisp_face_attribute (Qdefault, QCbackground,
2475 tty_color_name (f, orig_fg),
2476 frame);
2477 redraw = 1;
2481 if (redraw)
2483 face_change_count++; /* forces xdisp.c to recompute basic faces */
2484 if (f == SELECTED_FRAME())
2485 redraw_frame (f);
2489 extern void init_frame_faces (FRAME_PTR);
2491 #endif /* !HAVE_X_WINDOWS */
2494 /* Do we need the internal terminal? */
2496 void
2497 internal_terminal_init ()
2499 char *term = getenv ("TERM"), *colors;
2500 struct frame *sf = SELECTED_FRAME();
2502 #ifdef HAVE_X_WINDOWS
2503 if (!inhibit_window_system)
2504 return;
2505 #endif
2507 internal_terminal
2508 = (!noninteractive) && term && !strcmp (term, "internal");
2510 if (getenv ("EMACSTEST"))
2511 termscript = fopen (getenv ("EMACSTEST"), "wt");
2513 #ifndef HAVE_X_WINDOWS
2514 if (!internal_terminal || inhibit_window_system)
2516 sf->output_method = output_termcap;
2517 return;
2520 Vwindow_system = intern ("pc");
2521 Vwindow_system_version = make_number (1);
2522 sf->output_method = output_msdos_raw;
2524 /* If Emacs was dumped on DOS/V machine, forget the stale VRAM address. */
2525 screen_old_address = 0;
2527 /* Forget the stale screen colors as well. */
2528 initial_screen_colors[0] = initial_screen_colors[1] = -1;
2530 bzero (&the_only_x_display, sizeof the_only_x_display);
2531 the_only_x_display.background_pixel = 7; /* White */
2532 the_only_x_display.foreground_pixel = 0; /* Black */
2533 bright_bg ();
2534 colors = getenv ("EMACSCOLORS");
2535 if (colors && strlen (colors) >= 2)
2537 /* The colors use 4 bits each (we enable bright background). */
2538 if (isdigit (colors[0]))
2539 colors[0] -= '0';
2540 else if (isxdigit (colors[0]))
2541 colors[0] -= (isupper (colors[0]) ? 'A' : 'a') - 10;
2542 if (colors[0] >= 0 && colors[0] < 16)
2543 the_only_x_display.foreground_pixel = colors[0];
2544 if (isdigit (colors[1]))
2545 colors[1] -= '0';
2546 else if (isxdigit (colors[1]))
2547 colors[1] -= (isupper (colors[1]) ? 'A' : 'a') - 10;
2548 if (colors[1] >= 0 && colors[1] < 16)
2549 the_only_x_display.background_pixel = colors[1];
2551 the_only_x_display.font = (XFontStruct *)1; /* must *not* be zero */
2552 the_only_x_display.display_info.mouse_face_mouse_frame = NULL;
2553 the_only_x_display.display_info.mouse_face_deferred_gc = 0;
2554 the_only_x_display.display_info.mouse_face_beg_row =
2555 the_only_x_display.display_info.mouse_face_beg_col = -1;
2556 the_only_x_display.display_info.mouse_face_end_row =
2557 the_only_x_display.display_info.mouse_face_end_col = -1;
2558 the_only_x_display.display_info.mouse_face_face_id = DEFAULT_FACE_ID;
2559 the_only_x_display.display_info.mouse_face_window = Qnil;
2560 the_only_x_display.display_info.mouse_face_mouse_x =
2561 the_only_x_display.display_info.mouse_face_mouse_y = 0;
2562 the_only_x_display.display_info.mouse_face_defer = 0;
2563 the_only_x_display.display_info.mouse_face_hidden = 0;
2565 init_frame_faces (sf);
2567 ring_bell_hook = IT_ring_bell;
2568 insert_glyphs_hook = IT_insert_glyphs;
2569 delete_glyphs_hook = IT_delete_glyphs;
2570 write_glyphs_hook = IT_write_glyphs;
2571 cursor_to_hook = raw_cursor_to_hook = IT_cursor_to;
2572 clear_to_end_hook = IT_clear_to_end;
2573 clear_end_of_line_hook = IT_clear_end_of_line;
2574 clear_frame_hook = IT_clear_screen;
2575 update_begin_hook = IT_update_begin;
2576 update_end_hook = IT_update_end;
2577 frame_up_to_date_hook = IT_frame_up_to_date;
2579 /* These hooks are called by term.c without being checked. */
2580 set_terminal_modes_hook = IT_set_terminal_modes;
2581 reset_terminal_modes_hook = IT_reset_terminal_modes;
2582 set_terminal_window_hook = IT_set_terminal_window;
2583 char_ins_del_ok = 0;
2584 #endif
2587 dos_get_saved_screen (screen, rows, cols)
2588 char **screen;
2589 int *rows;
2590 int *cols;
2592 #ifndef HAVE_X_WINDOWS
2593 *screen = startup_screen_buffer;
2594 *cols = startup_screen_size_X;
2595 *rows = startup_screen_size_Y;
2596 return *screen != (char *)0;
2597 #else
2598 return 0;
2599 #endif
2602 #ifndef HAVE_X_WINDOWS
2604 /* We are not X, but we can emulate it well enough for our needs... */
2605 void
2606 check_x (void)
2608 if (! FRAME_MSDOS_P (SELECTED_FRAME()))
2609 error ("Not running under a window system");
2612 #endif
2615 /* ----------------------- Keyboard control ----------------------
2617 * Keymaps reflect the following keyboard layout:
2619 * 0 1 2 3 4 5 6 7 8 9 10 11 12 BS
2620 * TAB 15 16 17 18 19 20 21 22 23 24 25 26 (41)
2621 * CLOK 30 31 32 33 34 35 36 37 38 39 40 (41) RET
2622 * SH () 45 46 47 48 49 50 51 52 53 54 SHIFT
2623 * SPACE
2626 #define Ignore 0x0000
2627 #define Normal 0x0000 /* normal key - alt changes scan-code */
2628 #define FctKey 0x1000 /* func key if c == 0, else c */
2629 #define Special 0x2000 /* func key even if c != 0 */
2630 #define ModFct 0x3000 /* special if mod-keys, else 'c' */
2631 #define Map 0x4000 /* alt scan-code, map to unshift/shift key */
2632 #define KeyPad 0x5000 /* map to insert/kp-0 depending on c == 0xe0 */
2633 #define Grey 0x6000 /* Grey keypad key */
2635 #define Alt 0x0100 /* alt scan-code */
2636 #define Ctrl 0x0200 /* ctrl scan-code */
2637 #define Shift 0x0400 /* shift scan-code */
2639 static int extended_kbd; /* 101 (102) keyboard present. */
2641 struct kbd_translate {
2642 unsigned char sc;
2643 unsigned char ch;
2644 unsigned short code;
2647 struct dos_keyboard_map
2649 char *unshifted;
2650 char *shifted;
2651 char *alt_gr;
2652 struct kbd_translate *translate_table;
2656 static struct dos_keyboard_map us_keyboard = {
2657 /* 0 1 2 3 4 5 */
2658 /* 01234567890123456789012345678901234567890 12345678901234 */
2659 "`1234567890-= qwertyuiop[] asdfghjkl;'\\ zxcvbnm,./ ",
2660 /* 0123456789012345678901234567890123456789 012345678901234 */
2661 "~!@#$%^&*()_+ QWERTYUIOP{} ASDFGHJKL:\"| ZXCVBNM<>? ",
2662 0, /* no Alt-Gr key */
2663 0 /* no translate table */
2666 static struct dos_keyboard_map fr_keyboard = {
2667 /* 0 1 2 3 4 5 */
2668 /* 012 3456789012345678901234567890123456789012345678901234 */
2669 "ý&‚\",(-Š_€…)= azertyuiop^$ qsdfghjklm—* wxcvbnm;:! ",
2670 /* 0123456789012345678901234567890123456789012345678901234 */
2671 " 1234567890ø+ AZERTYUIOPùœ QSDFGHJKLM%æ WXCVBN?./õ ",
2672 /* 01234567 89012345678901234567890123456789012345678901234 */
2673 " ~#{[|`\\^@]} Ï ",
2674 0 /* no translate table */
2678 * Italian keyboard support, country code 39.
2679 * '<' 56:3c*0000
2680 * '>' 56:3e*0000
2681 * added also {,},` as, respectively, AltGr-8, AltGr-9, AltGr-'
2682 * Donated by Stefano Brozzi <brozzis@mag00.cedi.unipr.it>
2685 static struct kbd_translate it_kbd_translate_table[] = {
2686 { 0x56, 0x3c, Normal | 13 },
2687 { 0x56, 0x3e, Normal | 27 },
2688 { 0, 0, 0 }
2690 static struct dos_keyboard_map it_keyboard = {
2691 /* 0 1 2 3 4 5 */
2692 /* 0 123456789012345678901234567890123456789012345678901234 */
2693 "\\1234567890'�< qwertyuiopŠ+> asdfghjkl•…— zxcvbnm,.- ",
2694 /* 01 23456789012345678901234567890123456789012345678901234 */
2695 "|!\"œ$%&/()=?^> QWERTYUIOP‚* ASDFGHJKL‡øõ ZXCVBNM;:_ ",
2696 /* 0123456789012345678901234567890123456789012345678901234 */
2697 " {}~` [] @# ",
2698 it_kbd_translate_table
2701 static struct dos_keyboard_map dk_keyboard = {
2702 /* 0 1 2 3 4 5 */
2703 /* 0123456789012345678901234567890123456789012345678901234 */
2704 "«1234567890+| qwertyuiop†~ asdfghjkl‘›' zxcvbnm,.- ",
2705 /* 01 23456789012345678901234567890123456789012345678901234 */
2706 "õ!\"#$%&/()=?` QWERTYUIOP�^ ASDFGHJKL’�* ZXCVBNM;:_ ",
2707 /* 0123456789012345678901234567890123456789012345678901234 */
2708 " @œ$ {[]} | ",
2709 0 /* no translate table */
2712 static struct kbd_translate jp_kbd_translate_table[] = {
2713 { 0x73, 0x5c, Normal | 0 },
2714 { 0x73, 0x5f, Normal | 0 },
2715 { 0x73, 0x1c, Map | 0 },
2716 { 0x7d, 0x5c, Normal | 13 },
2717 { 0x7d, 0x7c, Normal | 13 },
2718 { 0x7d, 0x1c, Map | 13 },
2719 { 0, 0, 0 }
2721 static struct dos_keyboard_map jp_keyboard = {
2722 /* 0 1 2 3 4 5 */
2723 /* 0123456789012 345678901234567890123456789012345678901234 */
2724 "\\1234567890-^\\ qwertyuiop@[ asdfghjkl;:] zxcvbnm,./ ",
2725 /* 01 23456789012345678901234567890123456789012345678901234 */
2726 "_!\"#$%&'()~=~| QWERTYUIOP`{ ASDFGHJKL+*} ZXCVBNM<>? ",
2727 0, /* no Alt-Gr key */
2728 jp_kbd_translate_table
2731 static struct keyboard_layout_list
2733 int country_code;
2734 struct dos_keyboard_map *keyboard_map;
2735 } keyboard_layout_list[] =
2737 1, &us_keyboard,
2738 33, &fr_keyboard,
2739 39, &it_keyboard,
2740 45, &dk_keyboard,
2741 81, &jp_keyboard
2744 static struct dos_keyboard_map *keyboard;
2745 static int keyboard_map_all;
2746 static int international_keyboard;
2749 dos_set_keyboard (code, always)
2750 int code;
2751 int always;
2753 int i;
2754 _go32_dpmi_registers regs;
2756 /* See if Keyb.Com is installed (for international keyboard support).
2757 Note: calling Int 2Fh via int86 wedges the DOS box on some versions
2758 of Windows 9X! So don't do that! */
2759 regs.x.ax = 0xad80;
2760 regs.x.ss = regs.x.sp = regs.x.flags = 0;
2761 _go32_dpmi_simulate_int (0x2f, &regs);
2762 if (regs.h.al == 0xff)
2763 international_keyboard = 1;
2765 /* Initialize to US settings, for countries that don't have their own. */
2766 keyboard = keyboard_layout_list[0].keyboard_map;
2767 keyboard_map_all = always;
2768 dos_keyboard_layout = 1;
2770 for (i = 0; i < (sizeof (keyboard_layout_list)/sizeof (struct keyboard_layout_list)); i++)
2771 if (code == keyboard_layout_list[i].country_code)
2773 keyboard = keyboard_layout_list[i].keyboard_map;
2774 keyboard_map_all = always;
2775 dos_keyboard_layout = code;
2776 return 1;
2778 return 0;
2781 static struct
2783 unsigned char char_code; /* normal code */
2784 unsigned char meta_code; /* M- code */
2785 unsigned char keypad_code; /* keypad code */
2786 unsigned char editkey_code; /* edit key */
2787 } keypad_translate_map[] = {
2788 '0', '0', 0xb0, /* kp-0 */ 0x63, /* insert */
2789 '1', '1', 0xb1, /* kp-1 */ 0x57, /* end */
2790 '2', '2', 0xb2, /* kp-2 */ 0x54, /* down */
2791 '3', '3', 0xb3, /* kp-3 */ 0x56, /* next */
2792 '4', '4', 0xb4, /* kp-4 */ 0x51, /* left */
2793 '5', '5', 0xb5, /* kp-5 */ 0xb5, /* kp-5 */
2794 '6', '6', 0xb6, /* kp-6 */ 0x53, /* right */
2795 '7', '7', 0xb7, /* kp-7 */ 0x50, /* home */
2796 '8', '8', 0xb8, /* kp-8 */ 0x52, /* up */
2797 '9', '9', 0xb9, /* kp-9 */ 0x55, /* prior */
2798 '.', '-', 0xae, /* kp-decimal */ 0xff /* delete */
2801 static struct
2803 unsigned char char_code; /* normal code */
2804 unsigned char keypad_code; /* keypad code */
2805 } grey_key_translate_map[] = {
2806 '/', 0xaf, /* kp-decimal */
2807 '*', 0xaa, /* kp-multiply */
2808 '-', 0xad, /* kp-subtract */
2809 '+', 0xab, /* kp-add */
2810 '\r', 0x8d /* kp-enter */
2813 static unsigned short
2814 ibmpc_translate_map[] =
2816 /* --------------- 00 to 0f --------------- */
2817 Normal | 0xff, /* Ctrl Break + Alt-NNN */
2818 Alt | ModFct | 0x1b, /* Escape */
2819 Normal | 1, /* '1' */
2820 Normal | 2, /* '2' */
2821 Normal | 3, /* '3' */
2822 Normal | 4, /* '4' */
2823 Normal | 5, /* '5' */
2824 Normal | 6, /* '6' */
2825 Normal | 7, /* '7' */
2826 Normal | 8, /* '8' */
2827 Normal | 9, /* '9' */
2828 Normal | 10, /* '0' */
2829 Normal | 11, /* '-' */
2830 Normal | 12, /* '=' */
2831 Special | 0x08, /* Backspace */
2832 ModFct | 0x74, /* Tab/Backtab */
2834 /* --------------- 10 to 1f --------------- */
2835 Map | 15, /* 'q' */
2836 Map | 16, /* 'w' */
2837 Map | 17, /* 'e' */
2838 Map | 18, /* 'r' */
2839 Map | 19, /* 't' */
2840 Map | 20, /* 'y' */
2841 Map | 21, /* 'u' */
2842 Map | 22, /* 'i' */
2843 Map | 23, /* 'o' */
2844 Map | 24, /* 'p' */
2845 Map | 25, /* '[' */
2846 Map | 26, /* ']' */
2847 ModFct | 0x0d, /* Return */
2848 Ignore, /* Ctrl */
2849 Map | 30, /* 'a' */
2850 Map | 31, /* 's' */
2852 /* --------------- 20 to 2f --------------- */
2853 Map | 32, /* 'd' */
2854 Map | 33, /* 'f' */
2855 Map | 34, /* 'g' */
2856 Map | 35, /* 'h' */
2857 Map | 36, /* 'j' */
2858 Map | 37, /* 'k' */
2859 Map | 38, /* 'l' */
2860 Map | 39, /* ';' */
2861 Map | 40, /* '\'' */
2862 Map | 0, /* '`' */
2863 Ignore, /* Left shift */
2864 Map | 41, /* '\\' */
2865 Map | 45, /* 'z' */
2866 Map | 46, /* 'x' */
2867 Map | 47, /* 'c' */
2868 Map | 48, /* 'v' */
2870 /* --------------- 30 to 3f --------------- */
2871 Map | 49, /* 'b' */
2872 Map | 50, /* 'n' */
2873 Map | 51, /* 'm' */
2874 Map | 52, /* ',' */
2875 Map | 53, /* '.' */
2876 Map | 54, /* '/' */
2877 Ignore, /* Right shift */
2878 Grey | 1, /* Grey * */
2879 Ignore, /* Alt */
2880 Normal | 55, /* ' ' */
2881 Ignore, /* Caps Lock */
2882 FctKey | 0xbe, /* F1 */
2883 FctKey | 0xbf, /* F2 */
2884 FctKey | 0xc0, /* F3 */
2885 FctKey | 0xc1, /* F4 */
2886 FctKey | 0xc2, /* F5 */
2888 /* --------------- 40 to 4f --------------- */
2889 FctKey | 0xc3, /* F6 */
2890 FctKey | 0xc4, /* F7 */
2891 FctKey | 0xc5, /* F8 */
2892 FctKey | 0xc6, /* F9 */
2893 FctKey | 0xc7, /* F10 */
2894 Ignore, /* Num Lock */
2895 Ignore, /* Scroll Lock */
2896 KeyPad | 7, /* Home */
2897 KeyPad | 8, /* Up */
2898 KeyPad | 9, /* Page Up */
2899 Grey | 2, /* Grey - */
2900 KeyPad | 4, /* Left */
2901 KeyPad | 5, /* Keypad 5 */
2902 KeyPad | 6, /* Right */
2903 Grey | 3, /* Grey + */
2904 KeyPad | 1, /* End */
2906 /* --------------- 50 to 5f --------------- */
2907 KeyPad | 2, /* Down */
2908 KeyPad | 3, /* Page Down */
2909 KeyPad | 0, /* Insert */
2910 KeyPad | 10, /* Delete */
2911 Shift | FctKey | 0xbe, /* (Shift) F1 */
2912 Shift | FctKey | 0xbf, /* (Shift) F2 */
2913 Shift | FctKey | 0xc0, /* (Shift) F3 */
2914 Shift | FctKey | 0xc1, /* (Shift) F4 */
2915 Shift | FctKey | 0xc2, /* (Shift) F5 */
2916 Shift | FctKey | 0xc3, /* (Shift) F6 */
2917 Shift | FctKey | 0xc4, /* (Shift) F7 */
2918 Shift | FctKey | 0xc5, /* (Shift) F8 */
2919 Shift | FctKey | 0xc6, /* (Shift) F9 */
2920 Shift | FctKey | 0xc7, /* (Shift) F10 */
2921 Ctrl | FctKey | 0xbe, /* (Ctrl) F1 */
2922 Ctrl | FctKey | 0xbf, /* (Ctrl) F2 */
2924 /* --------------- 60 to 6f --------------- */
2925 Ctrl | FctKey | 0xc0, /* (Ctrl) F3 */
2926 Ctrl | FctKey | 0xc1, /* (Ctrl) F4 */
2927 Ctrl | FctKey | 0xc2, /* (Ctrl) F5 */
2928 Ctrl | FctKey | 0xc3, /* (Ctrl) F6 */
2929 Ctrl | FctKey | 0xc4, /* (Ctrl) F7 */
2930 Ctrl | FctKey | 0xc5, /* (Ctrl) F8 */
2931 Ctrl | FctKey | 0xc6, /* (Ctrl) F9 */
2932 Ctrl | FctKey | 0xc7, /* (Ctrl) F10 */
2933 Alt | FctKey | 0xbe, /* (Alt) F1 */
2934 Alt | FctKey | 0xbf, /* (Alt) F2 */
2935 Alt | FctKey | 0xc0, /* (Alt) F3 */
2936 Alt | FctKey | 0xc1, /* (Alt) F4 */
2937 Alt | FctKey | 0xc2, /* (Alt) F5 */
2938 Alt | FctKey | 0xc3, /* (Alt) F6 */
2939 Alt | FctKey | 0xc4, /* (Alt) F7 */
2940 Alt | FctKey | 0xc5, /* (Alt) F8 */
2942 /* --------------- 70 to 7f --------------- */
2943 Alt | FctKey | 0xc6, /* (Alt) F9 */
2944 Alt | FctKey | 0xc7, /* (Alt) F10 */
2945 Ctrl | FctKey | 0x6d, /* (Ctrl) Sys Rq */
2946 Ctrl | KeyPad | 4, /* (Ctrl) Left */
2947 Ctrl | KeyPad | 6, /* (Ctrl) Right */
2948 Ctrl | KeyPad | 1, /* (Ctrl) End */
2949 Ctrl | KeyPad | 3, /* (Ctrl) Page Down */
2950 Ctrl | KeyPad | 7, /* (Ctrl) Home */
2951 Alt | Map | 1, /* '1' */
2952 Alt | Map | 2, /* '2' */
2953 Alt | Map | 3, /* '3' */
2954 Alt | Map | 4, /* '4' */
2955 Alt | Map | 5, /* '5' */
2956 Alt | Map | 6, /* '6' */
2957 Alt | Map | 7, /* '7' */
2958 Alt | Map | 8, /* '8' */
2960 /* --------------- 80 to 8f --------------- */
2961 Alt | Map | 9, /* '9' */
2962 Alt | Map | 10, /* '0' */
2963 Alt | Map | 11, /* '-' */
2964 Alt | Map | 12, /* '=' */
2965 Ctrl | KeyPad | 9, /* (Ctrl) Page Up */
2966 FctKey | 0xc8, /* F11 */
2967 FctKey | 0xc9, /* F12 */
2968 Shift | FctKey | 0xc8, /* (Shift) F11 */
2969 Shift | FctKey | 0xc9, /* (Shift) F12 */
2970 Ctrl | FctKey | 0xc8, /* (Ctrl) F11 */
2971 Ctrl | FctKey | 0xc9, /* (Ctrl) F12 */
2972 Alt | FctKey | 0xc8, /* (Alt) F11 */
2973 Alt | FctKey | 0xc9, /* (Alt) F12 */
2974 Ctrl | KeyPad | 8, /* (Ctrl) Up */
2975 Ctrl | Grey | 2, /* (Ctrl) Grey - */
2976 Ctrl | KeyPad | 5, /* (Ctrl) Keypad 5 */
2978 /* --------------- 90 to 9f --------------- */
2979 Ctrl | Grey | 3, /* (Ctrl) Grey + */
2980 Ctrl | KeyPad | 2, /* (Ctrl) Down */
2981 Ctrl | KeyPad | 0, /* (Ctrl) Insert */
2982 Ctrl | KeyPad | 10, /* (Ctrl) Delete */
2983 Ctrl | FctKey | 0x09, /* (Ctrl) Tab */
2984 Ctrl | Grey | 0, /* (Ctrl) Grey / */
2985 Ctrl | Grey | 1, /* (Ctrl) Grey * */
2986 Alt | FctKey | 0x50, /* (Alt) Home */
2987 Alt | FctKey | 0x52, /* (Alt) Up */
2988 Alt | FctKey | 0x55, /* (Alt) Page Up */
2989 Ignore, /* NO KEY */
2990 Alt | FctKey | 0x51, /* (Alt) Left */
2991 Ignore, /* NO KEY */
2992 Alt | FctKey | 0x53, /* (Alt) Right */
2993 Ignore, /* NO KEY */
2994 Alt | FctKey | 0x57, /* (Alt) End */
2996 /* --------------- a0 to af --------------- */
2997 Alt | KeyPad | 2, /* (Alt) Down */
2998 Alt | KeyPad | 3, /* (Alt) Page Down */
2999 Alt | KeyPad | 0, /* (Alt) Insert */
3000 Alt | KeyPad | 10, /* (Alt) Delete */
3001 Alt | Grey | 0, /* (Alt) Grey / */
3002 Alt | FctKey | 0x09, /* (Alt) Tab */
3003 Alt | Grey | 4 /* (Alt) Keypad Enter */
3006 /* These bit-positions corresponds to values returned by BIOS */
3007 #define SHIFT_P 0x0003 /* two bits! */
3008 #define CTRL_P 0x0004
3009 #define ALT_P 0x0008
3010 #define SCRLOCK_P 0x0010
3011 #define NUMLOCK_P 0x0020
3012 #define CAPSLOCK_P 0x0040
3013 #define ALT_GR_P 0x0800
3014 #define SUPER_P 0x4000 /* pseudo */
3015 #define HYPER_P 0x8000 /* pseudo */
3017 static int
3018 dos_get_modifiers (keymask)
3019 int *keymask;
3021 union REGS regs;
3022 int mask, modifiers = 0;
3024 /* Calculate modifier bits */
3025 regs.h.ah = extended_kbd ? 0x12 : 0x02;
3026 int86 (0x16, &regs, &regs);
3028 if (!extended_kbd)
3030 mask = regs.h.al & (SHIFT_P | CTRL_P | ALT_P |
3031 SCRLOCK_P | NUMLOCK_P | CAPSLOCK_P);
3033 else
3035 mask = regs.h.al & (SHIFT_P |
3036 SCRLOCK_P | NUMLOCK_P | CAPSLOCK_P);
3038 /* Do not break international keyboard support. */
3039 /* When Keyb.Com is loaded, the right Alt key is */
3040 /* used for accessing characters like { and } */
3041 if (regs.h.ah & 2) /* Left ALT pressed ? */
3042 mask |= ALT_P;
3044 if ((regs.h.ah & 8) != 0) /* Right ALT pressed ? */
3046 mask |= ALT_GR_P;
3047 if (dos_hyper_key == 1)
3049 mask |= HYPER_P;
3050 modifiers |= hyper_modifier;
3052 else if (dos_super_key == 1)
3054 mask |= SUPER_P;
3055 modifiers |= super_modifier;
3057 else if (!international_keyboard)
3059 /* If Keyb.Com is NOT installed, let Right Alt behave
3060 like the Left Alt. */
3061 mask &= ~ALT_GR_P;
3062 mask |= ALT_P;
3066 if (regs.h.ah & 1) /* Left CTRL pressed ? */
3067 mask |= CTRL_P;
3069 if (regs.h.ah & 4) /* Right CTRL pressed ? */
3071 if (dos_hyper_key == 2)
3073 mask |= HYPER_P;
3074 modifiers |= hyper_modifier;
3076 else if (dos_super_key == 2)
3078 mask |= SUPER_P;
3079 modifiers |= super_modifier;
3081 else
3082 mask |= CTRL_P;
3086 if (mask & SHIFT_P)
3087 modifiers |= shift_modifier;
3088 if (mask & CTRL_P)
3089 modifiers |= ctrl_modifier;
3090 if (mask & ALT_P)
3091 modifiers |= meta_modifier;
3093 if (keymask)
3094 *keymask = mask;
3095 return modifiers;
3098 #define NUM_RECENT_DOSKEYS (100)
3099 int recent_doskeys_index; /* Index for storing next element into recent_doskeys */
3100 int total_doskeys; /* Total number of elements stored into recent_doskeys */
3101 Lisp_Object recent_doskeys; /* A vector, holding the last 100 keystrokes */
3103 DEFUN ("recent-doskeys", Frecent_doskeys, Srecent_doskeys, 0, 0, 0,
3104 doc: /* Return vector of last 100 keyboard input values seen in dos_rawgetc.
3105 Each input key receives two values in this vector: first the ASCII code,
3106 and then the scan code. */)
3109 Lisp_Object val, *keys = XVECTOR (recent_doskeys)->contents;
3111 if (total_doskeys < NUM_RECENT_DOSKEYS)
3112 return Fvector (total_doskeys, keys);
3113 else
3115 val = Fvector (NUM_RECENT_DOSKEYS, keys);
3116 bcopy (keys + recent_doskeys_index,
3117 XVECTOR (val)->contents,
3118 (NUM_RECENT_DOSKEYS - recent_doskeys_index) * sizeof (Lisp_Object));
3119 bcopy (keys,
3120 XVECTOR (val)->contents + NUM_RECENT_DOSKEYS - recent_doskeys_index,
3121 recent_doskeys_index * sizeof (Lisp_Object));
3122 return val;
3126 /* Get a char from keyboard. Function keys are put into the event queue. */
3127 static int
3128 dos_rawgetc ()
3130 struct input_event event;
3131 union REGS regs;
3132 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (SELECTED_FRAME());
3134 #ifndef HAVE_X_WINDOWS
3135 /* Maybe put the cursor where it should be. */
3136 IT_cmgoto (SELECTED_FRAME());
3137 #endif
3139 /* The following condition is equivalent to `kbhit ()', except that
3140 it uses the bios to do its job. This pleases DESQview/X. */
3141 while ((regs.h.ah = extended_kbd ? 0x11 : 0x01),
3142 int86 (0x16, &regs, &regs),
3143 (regs.x.flags & 0x40) == 0)
3145 union REGS regs;
3146 register unsigned char c;
3147 int modifiers, sc, code = -1, mask, kp_mode;
3149 regs.h.ah = extended_kbd ? 0x10 : 0x00;
3150 int86 (0x16, &regs, &regs);
3151 c = regs.h.al;
3152 sc = regs.h.ah;
3154 total_doskeys += 2;
3155 XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
3156 = make_number (c);
3157 if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
3158 recent_doskeys_index = 0;
3159 XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
3160 = make_number (sc);
3161 if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
3162 recent_doskeys_index = 0;
3164 modifiers = dos_get_modifiers (&mask);
3166 #ifndef HAVE_X_WINDOWS
3167 if (!NILP (Vdos_display_scancodes))
3169 char buf[11];
3170 sprintf (buf, "%02x:%02x*%04x",
3171 (unsigned) (sc&0xff), (unsigned) c, mask);
3172 dos_direct_output (screen_size_Y - 2, screen_size_X - 12, buf, 10);
3174 #endif
3176 if (sc == 0xe0)
3178 switch (c)
3180 case 10: /* Ctrl Grey Enter */
3181 code = Ctrl | Grey | 4;
3182 break;
3183 case 13: /* Grey Enter */
3184 code = Grey | 4;
3185 break;
3186 case '/': /* Grey / */
3187 code = Grey | 0;
3188 break;
3189 default:
3190 continue;
3192 c = 0;
3194 else
3196 /* Try the keyboard-private translation table first. */
3197 if (keyboard->translate_table)
3199 struct kbd_translate *p = keyboard->translate_table;
3201 while (p->sc)
3203 if (p->sc == sc && p->ch == c)
3205 code = p->code;
3206 break;
3208 p++;
3211 /* If the private table didn't translate it, use the general
3212 one. */
3213 if (code == -1)
3215 if (sc >= (sizeof (ibmpc_translate_map) / sizeof (short)))
3216 continue;
3217 if ((code = ibmpc_translate_map[sc]) == Ignore)
3218 continue;
3222 if (c == 0)
3224 /* We only look at the keyboard Ctrl/Shift/Alt keys when
3225 Emacs is ready to read a key. Therefore, if they press
3226 `Alt-x' when Emacs is busy, by the time we get to
3227 `dos_get_modifiers', they might have already released the
3228 Alt key, and Emacs gets just `x', which is BAD.
3229 However, for keys with the `Map' property set, the ASCII
3230 code returns zero iff Alt is pressed. So, when we DON'T
3231 have to support international_keyboard, we don't have to
3232 distinguish between the left and right Alt keys, and we
3233 can set the META modifier for any keys with the `Map'
3234 property if they return zero ASCII code (c = 0). */
3235 if ( (code & Alt)
3236 || ( (code & 0xf000) == Map && !international_keyboard))
3237 modifiers |= meta_modifier;
3238 if (code & Ctrl)
3239 modifiers |= ctrl_modifier;
3240 if (code & Shift)
3241 modifiers |= shift_modifier;
3244 switch (code & 0xf000)
3246 case ModFct:
3247 if (c && !(mask & (SHIFT_P | ALT_P | CTRL_P | HYPER_P | SUPER_P)))
3248 return c;
3249 c = 0; /* Special */
3251 case FctKey:
3252 if (c != 0)
3253 return c;
3255 case Special:
3256 code |= 0xff00;
3257 break;
3259 case Normal:
3260 if (sc == 0)
3262 if (c == 0) /* ctrl-break */
3263 continue;
3264 return c; /* ALT-nnn */
3266 if (!keyboard_map_all)
3268 if (c != ' ')
3269 return c;
3270 code = c;
3271 break;
3274 case Map:
3275 if (c && !(mask & ALT_P) && !((mask & SHIFT_P) && (mask & CTRL_P)))
3276 if (!keyboard_map_all)
3277 return c;
3279 code &= 0xff;
3280 if (mask & ALT_P && code <= 10 && code > 0 && dos_keypad_mode & 0x200)
3281 mask |= SHIFT_P; /* ALT-1 => M-! etc. */
3283 if (mask & SHIFT_P)
3285 code = keyboard->shifted[code];
3286 mask -= SHIFT_P;
3287 modifiers &= ~shift_modifier;
3289 else
3290 if ((mask & ALT_GR_P) && keyboard->alt_gr && keyboard->alt_gr[code] != ' ')
3291 code = keyboard->alt_gr[code];
3292 else
3293 code = keyboard->unshifted[code];
3294 break;
3296 case KeyPad:
3297 code &= 0xff;
3298 if (c == 0xe0) /* edit key */
3299 kp_mode = 3;
3300 else
3301 if ((mask & (NUMLOCK_P|CTRL_P|SHIFT_P|ALT_P)) == NUMLOCK_P) /* numlock on */
3302 kp_mode = dos_keypad_mode & 0x03;
3303 else
3304 kp_mode = (dos_keypad_mode >> 4) & 0x03;
3306 switch (kp_mode)
3308 case 0:
3309 if (code == 10 && dos_decimal_point)
3310 return dos_decimal_point;
3311 return keypad_translate_map[code].char_code;
3313 case 1:
3314 code = 0xff00 | keypad_translate_map[code].keypad_code;
3315 break;
3317 case 2:
3318 code = keypad_translate_map[code].meta_code;
3319 modifiers = meta_modifier;
3320 break;
3322 case 3:
3323 code = 0xff00 | keypad_translate_map[code].editkey_code;
3324 break;
3326 break;
3328 case Grey:
3329 code &= 0xff;
3330 kp_mode = ((mask & (NUMLOCK_P|CTRL_P|SHIFT_P|ALT_P)) == NUMLOCK_P) ? 0x04 : 0x40;
3331 if (dos_keypad_mode & kp_mode)
3332 code = 0xff00 | grey_key_translate_map[code].keypad_code;
3333 else
3334 code = grey_key_translate_map[code].char_code;
3335 break;
3338 make_event:
3339 if (code == 0)
3340 continue;
3342 if (!dpyinfo->mouse_face_hidden && INTEGERP (Vmouse_highlight))
3344 dpyinfo->mouse_face_hidden = 1;
3345 clear_mouse_face (dpyinfo);
3348 if (code >= 0x100)
3349 event.kind = NON_ASCII_KEYSTROKE_EVENT;
3350 else
3351 event.kind = ASCII_KEYSTROKE_EVENT;
3352 event.code = code;
3353 event.modifiers = modifiers;
3354 event.frame_or_window = selected_frame;
3355 event.arg = Qnil;
3356 event.timestamp = event_timestamp ();
3357 kbd_buffer_store_event (&event);
3360 if (have_mouse > 0 && !mouse_preempted)
3362 int but, press, x, y, ok;
3363 int mouse_prev_x = mouse_last_x, mouse_prev_y = mouse_last_y;
3364 Lisp_Object mouse_window = Qnil;
3366 /* Check for mouse movement *before* buttons. */
3367 mouse_check_moved ();
3369 /* If the mouse moved from the spot of its last sighting, we
3370 might need to update mouse highlight. */
3371 if (mouse_last_x != mouse_prev_x || mouse_last_y != mouse_prev_y)
3373 if (dpyinfo->mouse_face_hidden)
3375 dpyinfo->mouse_face_hidden = 0;
3376 clear_mouse_face (dpyinfo);
3379 /* Generate SELECT_WINDOW_EVENTs when needed. */
3380 if (mouse_autoselect_window)
3382 mouse_window = window_from_coordinates (SELECTED_FRAME(),
3383 mouse_last_x,
3384 mouse_last_y,
3385 0, 0, 0, 0);
3386 /* A window will be selected only when it is not
3387 selected now, and the last mouse movement event was
3388 not in it. A minibuffer window will be selected iff
3389 it is active. */
3390 if (WINDOWP (mouse_window)
3391 && !EQ (mouse_window, last_mouse_window)
3392 && !EQ (mouse_window, selected_window))
3394 event.kind = SELECT_WINDOW_EVENT;
3395 event.frame_or_window = mouse_window;
3396 event.arg = Qnil;
3397 event.timestamp = event_timestamp ();
3398 kbd_buffer_store_event (&event);
3400 last_mouse_window = mouse_window;
3402 else
3403 last_mouse_window = Qnil;
3405 previous_help_echo_string = help_echo_string;
3406 help_echo_string = help_echo_object = help_echo_window = Qnil;
3407 help_echo_pos = -1;
3408 IT_note_mouse_highlight (SELECTED_FRAME(),
3409 mouse_last_x, mouse_last_y);
3410 /* If the contents of the global variable help_echo has
3411 changed, generate a HELP_EVENT. */
3412 if (!NILP (help_echo_string) || !NILP (previous_help_echo_string))
3414 event.kind = HELP_EVENT;
3415 event.frame_or_window = selected_frame;
3416 event.arg = help_echo_object;
3417 event.x = WINDOWP (help_echo_window)
3418 ? help_echo_window : selected_frame;
3419 event.y = help_echo_string;
3420 event.timestamp = event_timestamp ();
3421 event.code = help_echo_pos;
3422 kbd_buffer_store_event (&event);
3426 for (but = 0; but < NUM_MOUSE_BUTTONS; but++)
3427 for (press = 0; press < 2; press++)
3429 int button_num = but;
3431 if (press)
3432 ok = mouse_pressed (but, &x, &y);
3433 else
3434 ok = mouse_released (but, &x, &y);
3435 if (ok)
3437 /* Allow a simultaneous press/release of Mouse-1 and
3438 Mouse-2 to simulate Mouse-3 on two-button mice. */
3439 if (mouse_button_count == 2 && but < 2)
3441 int x2, y2; /* don't clobber original coordinates */
3443 /* If only one button is pressed, wait 100 msec and
3444 check again. This way, Speedy Gonzales isn't
3445 punished, while the slow get their chance. */
3446 if (press && mouse_pressed (1-but, &x2, &y2)
3447 || !press && mouse_released (1-but, &x2, &y2))
3448 button_num = 2;
3449 else
3451 delay (100);
3452 if (press && mouse_pressed (1-but, &x2, &y2)
3453 || !press && mouse_released (1-but, &x2, &y2))
3454 button_num = 2;
3458 event.kind = MOUSE_CLICK_EVENT;
3459 event.code = button_num;
3460 event.modifiers = dos_get_modifiers (0)
3461 | (press ? down_modifier : up_modifier);
3462 event.x = x;
3463 event.y = y;
3464 event.frame_or_window = selected_frame;
3465 event.arg = Qnil;
3466 event.timestamp = event_timestamp ();
3467 kbd_buffer_store_event (&event);
3472 return -1;
3475 static int prev_get_char = -1;
3477 /* Return 1 if a key is ready to be read without suspending execution. */
3479 dos_keysns ()
3481 if (prev_get_char != -1)
3482 return 1;
3483 else
3484 return ((prev_get_char = dos_rawgetc ()) != -1);
3487 /* Read a key. Return -1 if no key is ready. */
3489 dos_keyread ()
3491 if (prev_get_char != -1)
3493 int c = prev_get_char;
3494 prev_get_char = -1;
3495 return c;
3497 else
3498 return dos_rawgetc ();
3501 #ifndef HAVE_X_WINDOWS
3503 /* Simulation of X's menus. Nothing too fancy here -- just make it work
3504 for now.
3506 Actually, I don't know the meaning of all the parameters of the functions
3507 here -- I only know how they are called by xmenu.c. I could of course
3508 grab the nearest Xlib manual (down the hall, second-to-last door on the
3509 left), but I don't think it's worth the effort. */
3511 /* These hold text of the current and the previous menu help messages. */
3512 static char *menu_help_message, *prev_menu_help_message;
3513 /* Pane number and item number of the menu item which generated the
3514 last menu help message. */
3515 static int menu_help_paneno, menu_help_itemno;
3517 static XMenu *
3518 IT_menu_create ()
3520 XMenu *menu;
3522 menu = (XMenu *) xmalloc (sizeof (XMenu));
3523 menu->allocated = menu->count = menu->panecount = menu->width = 0;
3524 return menu;
3527 /* Allocate some (more) memory for MENU ensuring that there is room for one
3528 for item. */
3530 static void
3531 IT_menu_make_room (XMenu *menu)
3533 if (menu->allocated == 0)
3535 int count = menu->allocated = 10;
3536 menu->text = (char **) xmalloc (count * sizeof (char *));
3537 menu->submenu = (XMenu **) xmalloc (count * sizeof (XMenu *));
3538 menu->panenumber = (int *) xmalloc (count * sizeof (int));
3539 menu->help_text = (char **) xmalloc (count * sizeof (char *));
3541 else if (menu->allocated == menu->count)
3543 int count = menu->allocated = menu->allocated + 10;
3544 menu->text
3545 = (char **) xrealloc (menu->text, count * sizeof (char *));
3546 menu->submenu
3547 = (XMenu **) xrealloc (menu->submenu, count * sizeof (XMenu *));
3548 menu->panenumber
3549 = (int *) xrealloc (menu->panenumber, count * sizeof (int));
3550 menu->help_text
3551 = (char **) xrealloc (menu->help_text, count * sizeof (char *));
3555 /* Search the given menu structure for a given pane number. */
3557 static XMenu *
3558 IT_menu_search_pane (XMenu *menu, int pane)
3560 int i;
3561 XMenu *try;
3563 for (i = 0; i < menu->count; i++)
3564 if (menu->submenu[i])
3566 if (pane == menu->panenumber[i])
3567 return menu->submenu[i];
3568 if ((try = IT_menu_search_pane (menu->submenu[i], pane)))
3569 return try;
3571 return (XMenu *) 0;
3574 /* Determine how much screen space a given menu needs. */
3576 static void
3577 IT_menu_calc_size (XMenu *menu, int *width, int *height)
3579 int i, h2, w2, maxsubwidth, maxheight;
3581 maxsubwidth = 0;
3582 maxheight = menu->count;
3583 for (i = 0; i < menu->count; i++)
3585 if (menu->submenu[i])
3587 IT_menu_calc_size (menu->submenu[i], &w2, &h2);
3588 if (w2 > maxsubwidth) maxsubwidth = w2;
3589 if (i + h2 > maxheight) maxheight = i + h2;
3592 *width = menu->width + maxsubwidth;
3593 *height = maxheight;
3596 /* Display MENU at (X,Y) using FACES. */
3598 static void
3599 IT_menu_display (XMenu *menu, int y, int x, int pn, int *faces, int disp_help)
3601 int i, j, face, width, mx, my, enabled, mousehere, row, col;
3602 struct glyph *text, *p;
3603 char *q;
3604 struct frame *sf = SELECTED_FRAME();
3606 menu_help_message = NULL;
3608 width = menu->width;
3609 text = (struct glyph *) xmalloc ((width + 2) * sizeof (struct glyph));
3610 ScreenGetCursor (&row, &col);
3611 mouse_get_xy (&mx, &my);
3612 IT_update_begin (sf);
3613 for (i = 0; i < menu->count; i++)
3615 int max_width = width + 2;
3617 IT_cursor_to (y + i, x);
3618 enabled
3619 = (!menu->submenu[i] && menu->panenumber[i]) || (menu->submenu[i]);
3620 mousehere = (y + i == my && x <= mx && mx < x + width + 2);
3621 face = faces[enabled + mousehere * 2];
3622 /* The following if clause means that we display the menu help
3623 strings even if the menu item is currently disabled. */
3624 if (disp_help && enabled + mousehere * 2 >= 2)
3626 menu_help_message = menu->help_text[i];
3627 menu_help_paneno = pn - 1;
3628 menu_help_itemno = i;
3630 p = text;
3631 SET_CHAR_GLYPH (*p, ' ', face, 0);
3632 p++;
3633 for (j = 0, q = menu->text[i]; *q; j++)
3635 if (*q > 26)
3637 SET_CHAR_GLYPH (*p, *q++, face, 0);
3638 p++;
3640 else /* make '^x' */
3642 SET_CHAR_GLYPH (*p, '^', face, 0);
3643 p++;
3644 j++;
3645 SET_CHAR_GLYPH (*p, *q++ + 64, face, 0);
3646 p++;
3649 /* Don't let the menu text overflow into the next screen row. */
3650 if (x + max_width > screen_size_X)
3652 max_width = screen_size_X - x;
3653 text[max_width - 1].u.ch = '$'; /* indicate it's truncated */
3655 for (; j < max_width - 2; j++, p++)
3656 SET_CHAR_GLYPH (*p, ' ', face, 0);
3658 SET_CHAR_GLYPH (*p, menu->submenu[i] ? 16 : ' ', face, 0);
3659 p++;
3660 IT_write_glyphs (text, max_width);
3662 IT_update_end (sf);
3663 IT_cursor_to (row, col);
3664 xfree (text);
3667 /* --------------------------- X Menu emulation ---------------------- */
3669 /* Report availability of menus. */
3672 have_menus_p () { return 1; }
3674 /* Create a brand new menu structure. */
3676 XMenu *
3677 XMenuCreate (Display *foo1, Window foo2, char *foo3)
3679 return IT_menu_create ();
3682 /* Create a new pane and place it on the outer-most level. It is not
3683 clear that it should be placed out there, but I don't know what else
3684 to do. */
3687 XMenuAddPane (Display *foo, XMenu *menu, char *txt, int enable)
3689 int len;
3690 char *p;
3692 if (!enable)
3693 abort ();
3695 IT_menu_make_room (menu);
3696 menu->submenu[menu->count] = IT_menu_create ();
3697 menu->text[menu->count] = txt;
3698 menu->panenumber[menu->count] = ++menu->panecount;
3699 menu->help_text[menu->count] = NULL;
3700 menu->count++;
3702 /* Adjust length for possible control characters (which will
3703 be written as ^x). */
3704 for (len = strlen (txt), p = txt; *p; p++)
3705 if (*p < 27)
3706 len++;
3708 if (len > menu->width)
3709 menu->width = len;
3711 return menu->panecount;
3714 /* Create a new item in a menu pane. */
3717 XMenuAddSelection (Display *bar, XMenu *menu, int pane,
3718 int foo, char *txt, int enable, char *help_text)
3720 int len;
3721 char *p;
3723 if (pane)
3724 if (!(menu = IT_menu_search_pane (menu, pane)))
3725 return XM_FAILURE;
3726 IT_menu_make_room (menu);
3727 menu->submenu[menu->count] = (XMenu *) 0;
3728 menu->text[menu->count] = txt;
3729 menu->panenumber[menu->count] = enable;
3730 menu->help_text[menu->count] = help_text;
3731 menu->count++;
3733 /* Adjust length for possible control characters (which will
3734 be written as ^x). */
3735 for (len = strlen (txt), p = txt; *p; p++)
3736 if (*p < 27)
3737 len++;
3739 if (len > menu->width)
3740 menu->width = len;
3742 return XM_SUCCESS;
3745 /* Decide where the menu would be placed if requested at (X,Y). */
3747 void
3748 XMenuLocate (Display *foo0, XMenu *menu, int foo1, int foo2, int x, int y,
3749 int *ulx, int *uly, int *width, int *height)
3751 IT_menu_calc_size (menu, width, height);
3752 *ulx = x + 1;
3753 *uly = y;
3754 *width += 2;
3757 struct IT_menu_state
3759 void *screen_behind;
3760 XMenu *menu;
3761 int pane;
3762 int x, y;
3766 /* Display menu, wait for user's response, and return that response. */
3769 XMenuActivate (Display *foo, XMenu *menu, int *pane, int *selidx,
3770 int x0, int y0, unsigned ButtonMask, char **txt,
3771 void (*help_callback)(char *, int, int))
3773 struct IT_menu_state *state;
3774 int statecount, x, y, i, b, screensize, leave, result, onepane;
3775 int title_faces[4]; /* face to display the menu title */
3776 int faces[4], buffers_num_deleted = 0;
3777 struct frame *sf = SELECTED_FRAME();
3778 Lisp_Object saved_echo_area_message, selectface;
3780 /* Just in case we got here without a mouse present... */
3781 if (have_mouse <= 0)
3782 return XM_IA_SELECT;
3783 /* Don't allow non-positive x0 and y0, lest the menu will wrap
3784 around the display. */
3785 if (x0 <= 0)
3786 x0 = 1;
3787 if (y0 <= 0)
3788 y0 = 1;
3790 /* We will process all the mouse events directly, so we had
3791 better prevent dos_rawgetc from stealing them from us. */
3792 mouse_preempted++;
3794 state = alloca (menu->panecount * sizeof (struct IT_menu_state));
3795 screensize = screen_size * 2;
3796 faces[0]
3797 = lookup_derived_face (sf, intern ("msdos-menu-passive-face"),
3798 0, DEFAULT_FACE_ID);
3799 faces[1]
3800 = lookup_derived_face (sf, intern ("msdos-menu-active-face"),
3801 0, DEFAULT_FACE_ID);
3802 selectface = intern ("msdos-menu-select-face");
3803 faces[2] = lookup_derived_face (sf, selectface,
3804 0, faces[0]);
3805 faces[3] = lookup_derived_face (sf, selectface,
3806 0, faces[1]);
3808 /* Make sure the menu title is always displayed with
3809 `msdos-menu-active-face', no matter where the mouse pointer is. */
3810 for (i = 0; i < 4; i++)
3811 title_faces[i] = faces[3];
3813 statecount = 1;
3815 /* Don't let the title for the "Buffers" popup menu include a
3816 digit (which is ugly).
3818 This is a terrible kludge, but I think the "Buffers" case is
3819 the only one where the title includes a number, so it doesn't
3820 seem to be necessary to make this more general. */
3821 if (strncmp (menu->text[0], "Buffers 1", 9) == 0)
3823 menu->text[0][7] = '\0';
3824 buffers_num_deleted = 1;
3827 /* We need to save the current echo area message, so that we could
3828 restore it below, before we exit. See the commentary below,
3829 before the call to message_with_string. */
3830 saved_echo_area_message = Fcurrent_message ();
3831 state[0].menu = menu;
3832 mouse_off ();
3833 ScreenRetrieve (state[0].screen_behind = xmalloc (screensize));
3835 /* Turn off the cursor. Otherwise it shows through the menu
3836 panes, which is ugly. */
3837 IT_display_cursor (0);
3839 /* Display the menu title. */
3840 IT_menu_display (menu, y0 - 1, x0 - 1, 1, title_faces, 0);
3841 if (buffers_num_deleted)
3842 menu->text[0][7] = ' ';
3843 if ((onepane = menu->count == 1 && menu->submenu[0]))
3845 menu->width = menu->submenu[0]->width;
3846 state[0].menu = menu->submenu[0];
3848 else
3850 state[0].menu = menu;
3852 state[0].x = x0 - 1;
3853 state[0].y = y0;
3854 state[0].pane = onepane;
3856 mouse_last_x = -1; /* A hack that forces display. */
3857 leave = 0;
3858 while (!leave)
3860 if (!mouse_visible) mouse_on ();
3861 mouse_check_moved ();
3862 if (sf->mouse_moved)
3864 sf->mouse_moved = 0;
3865 result = XM_IA_SELECT;
3866 mouse_get_xy (&x, &y);
3867 for (i = 0; i < statecount; i++)
3868 if (state[i].x <= x && x < state[i].x + state[i].menu->width + 2)
3870 int dy = y - state[i].y;
3871 if (0 <= dy && dy < state[i].menu->count)
3873 if (!state[i].menu->submenu[dy])
3874 if (state[i].menu->panenumber[dy])
3875 result = XM_SUCCESS;
3876 else
3877 result = XM_IA_SELECT;
3878 *pane = state[i].pane - 1;
3879 *selidx = dy;
3880 /* We hit some part of a menu, so drop extra menus that
3881 have been opened. That does not include an open and
3882 active submenu. */
3883 if (i != statecount - 2
3884 || state[i].menu->submenu[dy] != state[i+1].menu)
3885 while (i != statecount - 1)
3887 statecount--;
3888 mouse_off ();
3889 ScreenUpdate (state[statecount].screen_behind);
3890 if (screen_virtual_segment)
3891 dosv_refresh_virtual_screen (0, screen_size);
3892 xfree (state[statecount].screen_behind);
3894 if (i == statecount - 1 && state[i].menu->submenu[dy])
3896 IT_menu_display (state[i].menu,
3897 state[i].y,
3898 state[i].x,
3899 state[i].pane,
3900 faces, 1);
3901 state[statecount].menu = state[i].menu->submenu[dy];
3902 state[statecount].pane = state[i].menu->panenumber[dy];
3903 mouse_off ();
3904 ScreenRetrieve (state[statecount].screen_behind
3905 = xmalloc (screensize));
3906 state[statecount].x
3907 = state[i].x + state[i].menu->width + 2;
3908 state[statecount].y = y;
3909 statecount++;
3913 IT_menu_display (state[statecount - 1].menu,
3914 state[statecount - 1].y,
3915 state[statecount - 1].x,
3916 state[statecount - 1].pane,
3917 faces, 1);
3919 else
3921 if ((menu_help_message || prev_menu_help_message)
3922 && menu_help_message != prev_menu_help_message)
3924 help_callback (menu_help_message,
3925 menu_help_paneno, menu_help_itemno);
3926 IT_display_cursor (0);
3927 prev_menu_help_message = menu_help_message;
3929 /* We are busy-waiting for the mouse to move, so let's be nice
3930 to other Windows applications by releasing our time slice. */
3931 __dpmi_yield ();
3933 for (b = 0; b < mouse_button_count && !leave; b++)
3935 /* Only leave if user both pressed and released the mouse, and in
3936 that order. This avoids popping down the menu pane unless
3937 the user is really done with it. */
3938 if (mouse_pressed (b, &x, &y))
3940 while (mouse_button_depressed (b, &x, &y))
3941 __dpmi_yield ();
3942 leave = 1;
3944 (void) mouse_released (b, &x, &y);
3948 mouse_off ();
3949 ScreenUpdate (state[0].screen_behind);
3950 if (screen_virtual_segment)
3951 dosv_refresh_virtual_screen (0, screen_size);
3953 /* We have a situation here. ScreenUpdate has just restored the
3954 screen contents as it was before we started drawing this menu.
3955 That includes any echo area message that could have been
3956 displayed back then. (In reality, that echo area message will
3957 almost always be the ``keystroke echo'' that echoes the sequence
3958 of menu items chosen by the user.) However, if the menu had some
3959 help messages, then displaying those messages caused Emacs to
3960 forget about the original echo area message. So when
3961 ScreenUpdate restored it, it created a discrepancy between the
3962 actual screen contents and what Emacs internal data structures
3963 know about it.
3965 To avoid this conflict, we force Emacs to restore the original
3966 echo area message as we found it when we entered this function.
3967 The irony of this is that we then erase the restored message
3968 right away, so the only purpose of restoring it is so that
3969 erasing it works correctly... */
3970 if (! NILP (saved_echo_area_message))
3971 message_with_string ("%s", saved_echo_area_message, 0);
3972 message (0);
3973 while (statecount--)
3974 xfree (state[statecount].screen_behind);
3975 IT_display_cursor (1); /* turn cursor back on */
3976 /* Clean up any mouse events that are waiting inside Emacs event queue.
3977 These events are likely to be generated before the menu was even
3978 displayed, probably because the user pressed and released the button
3979 (which invoked the menu) too quickly. If we don't remove these events,
3980 Emacs will process them after we return and surprise the user. */
3981 discard_mouse_events ();
3982 mouse_clear_clicks ();
3983 if (!kbd_buffer_events_waiting (1))
3984 clear_input_pending ();
3985 /* Allow mouse events generation by dos_rawgetc. */
3986 mouse_preempted--;
3987 return result;
3990 /* Dispose of a menu. */
3992 void
3993 XMenuDestroy (Display *foo, XMenu *menu)
3995 int i;
3996 if (menu->allocated)
3998 for (i = 0; i < menu->count; i++)
3999 if (menu->submenu[i])
4000 XMenuDestroy (foo, menu->submenu[i]);
4001 xfree (menu->text);
4002 xfree (menu->submenu);
4003 xfree (menu->panenumber);
4004 xfree (menu->help_text);
4006 xfree (menu);
4007 menu_help_message = prev_menu_help_message = NULL;
4011 x_pixel_width (struct frame *f)
4013 return FRAME_COLS (f);
4017 x_pixel_height (struct frame *f)
4019 return FRAME_LINES (f);
4021 #endif /* !HAVE_X_WINDOWS */
4023 /* ----------------------- DOS / UNIX conversion --------------------- */
4025 void msdos_downcase_filename (unsigned char *);
4027 /* Destructively turn backslashes into slashes. */
4029 void
4030 dostounix_filename (p)
4031 register char *p;
4033 msdos_downcase_filename (p);
4035 while (*p)
4037 if (*p == '\\')
4038 *p = '/';
4039 p++;
4043 /* Destructively turn slashes into backslashes. */
4045 void
4046 unixtodos_filename (p)
4047 register char *p;
4049 if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
4051 *p += 'a' - 'A';
4052 p += 2;
4055 while (*p)
4057 if (*p == '/')
4058 *p = '\\';
4059 p++;
4063 /* Get the default directory for a given drive. 0=def, 1=A, 2=B, ... */
4066 getdefdir (drive, dst)
4067 int drive;
4068 char *dst;
4070 char in_path[4], *p = in_path, e = errno;;
4072 /* Generate "X:." (when drive is X) or "." (when drive is 0). */
4073 if (drive != 0)
4075 *p++ = drive + 'A' - 1;
4076 *p++ = ':';
4079 *p++ = '.';
4080 *p = '\0';
4081 errno = 0;
4082 _fixpath (in_path, dst);
4083 /* _fixpath can set errno to ENOSYS on non-LFN systems because
4084 it queries the LFN support, so ignore that error. */
4085 if ((errno && errno != ENOSYS) || *dst == '\0')
4086 return 0;
4088 msdos_downcase_filename (dst);
4090 errno = e;
4091 return 1;
4094 char *
4095 emacs_root_dir (void)
4097 static char root_dir[4];
4099 sprintf (root_dir, "%c:/", 'A' + getdisk ());
4100 root_dir[0] = tolower (root_dir[0]);
4101 return root_dir;
4104 /* Remove all CR's that are followed by a LF. */
4107 crlf_to_lf (n, buf)
4108 register int n;
4109 register unsigned char *buf;
4111 unsigned char *np = buf, *startp = buf, *endp = buf + n;
4113 if (n == 0)
4114 return n;
4115 while (buf < endp - 1)
4117 if (*buf == 0x0d)
4119 if (*(++buf) != 0x0a)
4120 *np++ = 0x0d;
4122 else
4123 *np++ = *buf++;
4125 if (buf < endp)
4126 *np++ = *buf++;
4127 return np - startp;
4130 #if defined(__DJGPP__) && __DJGPP__ == 2 && __DJGPP_MINOR__ == 0
4132 /* In DJGPP v2.0, library `write' can call `malloc', which might
4133 cause relocation of the buffer whose address we get in ADDR.
4134 Here is a version of `write' that avoids calling `malloc',
4135 to serve us until such time as the library is fixed.
4136 Actually, what we define here is called `__write', because
4137 `write' is a stub that just jmp's to `__write' (to be
4138 POSIXLY-correct with respect to the global name-space). */
4140 #include <io.h> /* for _write */
4141 #include <libc/dosio.h> /* for __file_handle_modes[] */
4143 static char xbuf[64 * 1024]; /* DOS cannot write more in one chunk */
4145 #define XBUF_END (xbuf + sizeof (xbuf) - 1)
4148 __write (int handle, const void *buffer, size_t count)
4150 if (count == 0)
4151 return 0;
4153 if(__file_handle_modes[handle] & O_BINARY)
4154 return _write (handle, buffer, count);
4155 else
4157 char *xbp = xbuf;
4158 const char *bp = buffer;
4159 int total_written = 0;
4160 int nmoved = 0, ncr = 0;
4162 while (count)
4164 /* The next test makes sure there's space for at least 2 more
4165 characters in xbuf[], so both CR and LF can be put there. */
4166 if (xbp < XBUF_END)
4168 if (*bp == '\n')
4170 ncr++;
4171 *xbp++ = '\r';
4173 *xbp++ = *bp++;
4174 nmoved++;
4175 count--;
4177 if (xbp >= XBUF_END || !count)
4179 size_t to_write = nmoved + ncr;
4180 int written = _write (handle, xbuf, to_write);
4182 if (written == -1)
4183 return -1;
4184 else
4185 total_written += nmoved; /* CRs aren't counted in ret value */
4187 /* If some, but not all were written (disk full?), return
4188 an estimate of the total written bytes not counting CRs. */
4189 if (written < to_write)
4190 return total_written - (to_write - written) * nmoved/to_write;
4192 nmoved = 0;
4193 ncr = 0;
4194 xbp = xbuf;
4197 return total_written;
4201 /* A low-level file-renaming function which works around Windows 95 bug.
4202 This is pulled directly out of DJGPP v2.01 library sources, and only
4203 used when you compile with DJGPP v2.0. */
4205 #include <io.h>
4207 int _rename(const char *old, const char *new)
4209 __dpmi_regs r;
4210 int olen = strlen(old) + 1;
4211 int i;
4212 int use_lfn = _USE_LFN;
4213 char tempfile[FILENAME_MAX];
4214 const char *orig = old;
4215 int lfn_fd = -1;
4217 r.x.dx = __tb_offset;
4218 r.x.di = __tb_offset + olen;
4219 r.x.ds = r.x.es = __tb_segment;
4221 if (use_lfn)
4223 /* Windows 95 bug: for some filenames, when you rename
4224 file -> file~ (as in Emacs, to leave a backup), the
4225 short 8+3 alias doesn't change, which effectively
4226 makes OLD and NEW the same file. We must rename
4227 through a temporary file to work around this. */
4229 char *pbase = 0, *p;
4230 static char try_char[] = "abcdefghijklmnopqrstuvwxyz012345789";
4231 int idx = sizeof(try_char) - 1;
4233 /* Generate a temporary name. Can't use `tmpnam', since $TMPDIR
4234 might point to another drive, which will fail the DOS call. */
4235 strcpy(tempfile, old);
4236 for (p = tempfile; *p; p++) /* ensure temporary is on the same drive */
4237 if (*p == '/' || *p == '\\' || *p == ':')
4238 pbase = p;
4239 if (pbase)
4240 pbase++;
4241 else
4242 pbase = tempfile;
4243 strcpy(pbase, "X$$djren$$.$$temp$$");
4247 if (idx <= 0)
4248 return -1;
4249 *pbase = try_char[--idx];
4250 } while (_chmod(tempfile, 0) != -1);
4252 r.x.ax = 0x7156;
4253 _put_path2(tempfile, olen);
4254 _put_path(old);
4255 __dpmi_int(0x21, &r);
4256 if (r.x.flags & 1)
4258 errno = __doserr_to_errno(r.x.ax);
4259 return -1;
4262 /* Now create a file with the original name. This will
4263 ensure that NEW will always have a 8+3 alias
4264 different from that of OLD. (Seems to be required
4265 when NameNumericTail in the Registry is set to 0.) */
4266 lfn_fd = _creat(old, 0);
4268 olen = strlen(tempfile) + 1;
4269 old = tempfile;
4270 r.x.di = __tb_offset + olen;
4273 for (i=0; i<2; i++)
4275 if(use_lfn)
4276 r.x.ax = 0x7156;
4277 else
4278 r.h.ah = 0x56;
4279 _put_path2(new, olen);
4280 _put_path(old);
4281 __dpmi_int(0x21, &r);
4282 if(r.x.flags & 1)
4284 if (r.x.ax == 5 && i == 0) /* access denied */
4285 remove(new); /* and try again */
4286 else
4288 errno = __doserr_to_errno(r.x.ax);
4290 /* Restore to original name if we renamed it to temporary. */
4291 if (use_lfn)
4293 if (lfn_fd != -1)
4295 _close (lfn_fd);
4296 remove (orig);
4298 _put_path2(orig, olen);
4299 _put_path(tempfile);
4300 r.x.ax = 0x7156;
4301 __dpmi_int(0x21, &r);
4303 return -1;
4306 else
4307 break;
4310 /* Success. Delete the file possibly created to work
4311 around the Windows 95 bug. */
4312 if (lfn_fd != -1)
4313 return (_close (lfn_fd) == 0) ? remove (orig) : -1;
4314 return 0;
4317 #endif /* __DJGPP__ == 2 && __DJGPP_MINOR__ == 0 */
4319 DEFUN ("msdos-long-file-names", Fmsdos_long_file_names, Smsdos_long_file_names,
4320 0, 0, 0,
4321 doc: /* Return non-nil if long file names are supported on MSDOS. */)
4324 return (_USE_LFN ? Qt : Qnil);
4327 /* Convert alphabetic characters in a filename to lower-case. */
4329 void
4330 msdos_downcase_filename (p)
4331 register unsigned char *p;
4333 /* Always lower-case drive letters a-z, even if the filesystem
4334 preserves case in filenames.
4335 This is so MSDOS filenames could be compared by string comparison
4336 functions that are case-sensitive. Even case-preserving filesystems
4337 do not distinguish case in drive letters. */
4338 if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
4340 *p += 'a' - 'A';
4341 p += 2;
4344 /* Under LFN we expect to get pathnames in their true case. */
4345 if (NILP (Fmsdos_long_file_names ()))
4346 for ( ; *p; p++)
4347 if (*p >= 'A' && *p <= 'Z')
4348 *p += 'a' - 'A';
4351 DEFUN ("msdos-downcase-filename", Fmsdos_downcase_filename, Smsdos_downcase_filename,
4352 1, 1, 0,
4353 doc: /* Convert alphabetic characters in FILENAME to lower case and return that.
4354 When long filenames are supported, doesn't change FILENAME.
4355 If FILENAME is not a string, returns nil.
4356 The argument object is never altered--the value is a copy. */)
4357 (filename)
4358 Lisp_Object filename;
4360 Lisp_Object tem;
4362 if (! STRINGP (filename))
4363 return Qnil;
4365 tem = Fcopy_sequence (filename);
4366 msdos_downcase_filename (SDATA (tem));
4367 return tem;
4370 /* The Emacs root directory as determined by init_environment. */
4372 static char emacsroot[MAXPATHLEN];
4374 char *
4375 rootrelativepath (rel)
4376 char *rel;
4378 static char result[MAXPATHLEN + 10];
4380 strcpy (result, emacsroot);
4381 strcat (result, "/");
4382 strcat (result, rel);
4383 return result;
4386 /* Define a lot of environment variables if not already defined. Don't
4387 remove anything unless you know what you're doing -- lots of code will
4388 break if one or more of these are missing. */
4390 void
4391 init_environment (argc, argv, skip_args)
4392 int argc;
4393 char **argv;
4394 int skip_args;
4396 char *s, *t, *root;
4397 int len, i;
4398 static const char * const tempdirs[] = {
4399 "$TMPDIR", "$TEMP", "$TMP", "c:/"
4401 const int imax = sizeof (tempdirs) / sizeof (tempdirs[0]);
4403 /* Make sure they have a usable $TMPDIR. Many Emacs functions use
4404 temporary files and assume "/tmp" if $TMPDIR is unset, which
4405 will break on DOS/Windows. Refuse to work if we cannot find
4406 a directory, not even "c:/", usable for that purpose. */
4407 for (i = 0; i < imax ; i++)
4409 const char *tmp = tempdirs[i];
4411 if (*tmp == '$')
4412 tmp = getenv (tmp + 1);
4413 /* Note that `access' can lie to us if the directory resides on a
4414 read-only filesystem, like CD-ROM or a write-protected floppy.
4415 The only way to be really sure is to actually create a file and
4416 see if it succeeds. But I think that's too much to ask. */
4417 if (tmp && access (tmp, D_OK) == 0)
4419 setenv ("TMPDIR", tmp, 1);
4420 break;
4423 if (i >= imax)
4424 cmd_error_internal
4425 (Fcons (Qerror,
4426 Fcons (build_string ("no usable temporary directories found!!"),
4427 Qnil)),
4428 "While setting TMPDIR: ");
4430 /* Note the startup time, so we know not to clear the screen if we
4431 exit immediately; see IT_reset_terminal_modes.
4432 (Yes, I know `clock' returns zero the first time it's called, but
4433 I do this anyway, in case some wiseguy changes that at some point.) */
4434 startup_time = clock ();
4436 /* Find our root from argv[0]. Assuming argv[0] is, say,
4437 "c:/emacs/bin/emacs.exe" our root will be "c:/emacs". */
4438 root = alloca (MAXPATHLEN + 20);
4439 _fixpath (argv[0], root);
4440 msdos_downcase_filename (root);
4441 len = strlen (root);
4442 while (len > 0 && root[len] != '/' && root[len] != ':')
4443 len--;
4444 root[len] = '\0';
4445 if (len > 4
4446 && (strcmp (root + len - 4, "/bin") == 0
4447 || strcmp (root + len - 4, "/src") == 0)) /* under a debugger */
4448 root[len - 4] = '\0';
4449 else
4450 strcpy (root, "c:/emacs"); /* let's be defensive */
4451 len = strlen (root);
4452 strcpy (emacsroot, root);
4454 /* We default HOME to our root. */
4455 setenv ("HOME", root, 0);
4457 /* We default EMACSPATH to root + "/bin". */
4458 strcpy (root + len, "/bin");
4459 setenv ("EMACSPATH", root, 0);
4461 /* I don't expect anybody to ever use other terminals so the internal
4462 terminal is the default. */
4463 setenv ("TERM", "internal", 0);
4465 #ifdef HAVE_X_WINDOWS
4466 /* Emacs expects DISPLAY to be set. */
4467 setenv ("DISPLAY", "unix:0.0", 0);
4468 #endif
4470 /* SHELL is a bit tricky -- COMSPEC is the closest we come, but we must
4471 downcase it and mirror the backslashes. */
4472 s = getenv ("COMSPEC");
4473 if (!s) s = "c:/command.com";
4474 t = alloca (strlen (s) + 1);
4475 strcpy (t, s);
4476 dostounix_filename (t);
4477 setenv ("SHELL", t, 0);
4479 /* PATH is also downcased and backslashes mirrored. */
4480 s = getenv ("PATH");
4481 if (!s) s = "";
4482 t = alloca (strlen (s) + 3);
4483 /* Current directory is always considered part of MsDos's path but it is
4484 not normally mentioned. Now it is. */
4485 strcat (strcpy (t, ".;"), s);
4486 dostounix_filename (t); /* Not a single file name, but this should work. */
4487 setenv ("PATH", t, 1);
4489 /* In some sense all dos users have root privileges, so... */
4490 setenv ("USER", "root", 0);
4491 setenv ("NAME", getenv ("USER"), 0);
4493 /* Time zone determined from country code. To make this possible, the
4494 country code may not span more than one time zone. In other words,
4495 in the USA, you lose. */
4496 if (!getenv ("TZ"))
4497 switch (dos_country_code)
4499 case 31: /* Belgium */
4500 case 32: /* The Netherlands */
4501 case 33: /* France */
4502 case 34: /* Spain */
4503 case 36: /* Hungary */
4504 case 38: /* Yugoslavia (or what's left of it?) */
4505 case 39: /* Italy */
4506 case 41: /* Switzerland */
4507 case 42: /* Tjekia */
4508 case 45: /* Denmark */
4509 case 46: /* Sweden */
4510 case 47: /* Norway */
4511 case 48: /* Poland */
4512 case 49: /* Germany */
4513 /* Daylight saving from last Sunday in March to last Sunday in
4514 September, both at 2AM. */
4515 setenv ("TZ", "MET-01METDST-02,M3.5.0/02:00,M9.5.0/02:00", 0);
4516 break;
4517 case 44: /* United Kingdom */
4518 case 351: /* Portugal */
4519 case 354: /* Iceland */
4520 setenv ("TZ", "GMT+00", 0);
4521 break;
4522 case 81: /* Japan */
4523 case 82: /* Korea */
4524 setenv ("TZ", "JST-09", 0);
4525 break;
4526 case 90: /* Turkey */
4527 case 358: /* Finland */
4528 setenv ("TZ", "EET-02", 0);
4529 break;
4530 case 972: /* Israel */
4531 /* This is an approximation. (For exact rules, use the
4532 `zoneinfo/israel' file which comes with DJGPP, but you need
4533 to install it in `/usr/share/zoneinfo/' directory first.) */
4534 setenv ("TZ", "IST-02IDT-03,M4.1.6/00:00,M9.5.6/01:00", 0);
4535 break;
4537 tzset ();
4542 static int break_stat; /* BREAK check mode status. */
4543 static int stdin_stat; /* stdin IOCTL status. */
4545 #if __DJGPP__ < 2
4547 /* These must be global. */
4548 static _go32_dpmi_seginfo ctrl_break_vector;
4549 static _go32_dpmi_registers ctrl_break_regs;
4550 static int ctrlbreakinstalled = 0;
4552 /* Interrupt level detection of Ctrl-Break. Don't do anything fancy here! */
4554 void
4555 ctrl_break_func (regs)
4556 _go32_dpmi_registers *regs;
4558 Vquit_flag = Qt;
4561 void
4562 install_ctrl_break_check ()
4564 if (!ctrlbreakinstalled)
4566 /* Don't press Ctrl-Break if you don't have either DPMI or Emacs
4567 was compiler with Djgpp 1.11 maintenance level 5 or later! */
4568 ctrlbreakinstalled = 1;
4569 ctrl_break_vector.pm_offset = (int) ctrl_break_func;
4570 _go32_dpmi_allocate_real_mode_callback_iret (&ctrl_break_vector,
4571 &ctrl_break_regs);
4572 _go32_dpmi_set_real_mode_interrupt_vector (0x1b, &ctrl_break_vector);
4576 #endif /* __DJGPP__ < 2 */
4578 /* Turn off Dos' Ctrl-C checking and inhibit interpretation of
4579 control chars by DOS. Determine the keyboard type. */
4582 dos_ttraw ()
4584 union REGS inregs, outregs;
4585 static int first_time = 1;
4587 break_stat = getcbrk ();
4588 setcbrk (0);
4589 #if __DJGPP__ < 2
4590 install_ctrl_break_check ();
4591 #endif
4593 if (first_time)
4595 inregs.h.ah = 0xc0;
4596 int86 (0x15, &inregs, &outregs);
4597 extended_kbd = (!outregs.x.cflag) && (outregs.h.ah == 0);
4599 have_mouse = 0;
4601 if (internal_terminal
4602 #ifdef HAVE_X_WINDOWS
4603 && inhibit_window_system
4604 #endif
4607 inregs.x.ax = 0x0021;
4608 int86 (0x33, &inregs, &outregs);
4609 have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
4610 if (!have_mouse)
4612 /* Reportedly, the above doesn't work for some mouse drivers. There
4613 is an additional detection method that should work, but might be
4614 a little slower. Use that as an alternative. */
4615 inregs.x.ax = 0x0000;
4616 int86 (0x33, &inregs, &outregs);
4617 have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
4620 if (have_mouse)
4622 have_mouse = 1; /* enable mouse */
4623 mouse_visible = 0;
4624 mouse_setup_buttons (outregs.x.bx);
4625 mouse_position_hook = &mouse_get_pos;
4626 mouse_init ();
4629 #ifndef HAVE_X_WINDOWS
4630 #if __DJGPP__ >= 2
4631 /* Save the cursor shape used outside Emacs. */
4632 outside_cursor = _farpeekw (_dos_ds, 0x460);
4633 #endif
4634 #endif
4637 first_time = 0;
4639 #if __DJGPP__ >= 2
4641 stdin_stat = setmode (fileno (stdin), O_BINARY);
4642 return (stdin_stat != -1);
4644 else
4645 return (setmode (fileno (stdin), O_BINARY) != -1);
4647 #else /* __DJGPP__ < 2 */
4651 /* I think it is wrong to overwrite `stdin_stat' every time
4652 but the first one this function is called, but I don't
4653 want to change the way it used to work in v1.x.--EZ */
4655 inregs.x.ax = 0x4400; /* Get IOCTL status. */
4656 inregs.x.bx = 0x00; /* 0 = stdin. */
4657 intdos (&inregs, &outregs);
4658 stdin_stat = outregs.h.dl;
4660 inregs.x.dx = stdin_stat | 0x0020; /* raw mode */
4661 inregs.x.ax = 0x4401; /* Set IOCTL status */
4662 intdos (&inregs, &outregs);
4663 return !outregs.x.cflag;
4665 #endif /* __DJGPP__ < 2 */
4668 /* Restore status of standard input and Ctrl-C checking. */
4671 dos_ttcooked ()
4673 union REGS inregs, outregs;
4675 setcbrk (break_stat);
4676 mouse_off ();
4678 #if __DJGPP__ >= 2
4680 #ifndef HAVE_X_WINDOWS
4681 /* Restore the cursor shape we found on startup. */
4682 if (outside_cursor)
4684 inregs.h.ah = 1;
4685 inregs.x.cx = outside_cursor;
4686 int86 (0x10, &inregs, &outregs);
4688 #endif
4690 return (setmode (fileno (stdin), stdin_stat) != -1);
4692 #else /* not __DJGPP__ >= 2 */
4694 inregs.x.ax = 0x4401; /* Set IOCTL status. */
4695 inregs.x.bx = 0x00; /* 0 = stdin. */
4696 inregs.x.dx = stdin_stat;
4697 intdos (&inregs, &outregs);
4698 return !outregs.x.cflag;
4700 #endif /* not __DJGPP__ >= 2 */
4704 /* Run command as specified by ARGV in directory DIR.
4705 The command is run with input from TEMPIN, output to
4706 file TEMPOUT and stderr to TEMPERR. */
4709 run_msdos_command (argv, working_dir, tempin, tempout, temperr, envv)
4710 unsigned char **argv;
4711 const char *working_dir;
4712 int tempin, tempout, temperr;
4713 char **envv;
4715 char *saveargv1, *saveargv2, *lowcase_argv0, *pa, *pl;
4716 char oldwd[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
4717 int msshell, result = -1, inbak, outbak, errbak, x, y;
4718 Lisp_Object cmd;
4720 /* Get current directory as MSDOS cwd is not per-process. */
4721 getwd (oldwd);
4723 /* If argv[0] is the shell, it might come in any lettercase.
4724 Since `Fmember' is case-sensitive, we need to downcase
4725 argv[0], even if we are on case-preserving filesystems. */
4726 lowcase_argv0 = alloca (strlen (argv[0]) + 1);
4727 for (pa = argv[0], pl = lowcase_argv0; *pa; pl++)
4729 *pl = *pa++;
4730 if (*pl >= 'A' && *pl <= 'Z')
4731 *pl += 'a' - 'A';
4733 *pl = '\0';
4735 cmd = Ffile_name_nondirectory (build_string (lowcase_argv0));
4736 msshell = !NILP (Fmember (cmd, Fsymbol_value (intern ("msdos-shells"))))
4737 && !strcmp ("-c", argv[1]);
4738 if (msshell)
4740 saveargv1 = argv[1];
4741 saveargv2 = argv[2];
4742 argv[1] = "/c";
4743 /* We only need to mirror slashes if a DOS shell will be invoked
4744 not via `system' (which does the mirroring itself). Yes, that
4745 means DJGPP v1.x will lose here. */
4746 if (argv[2] && argv[3])
4748 char *p = alloca (strlen (argv[2]) + 1);
4750 strcpy (argv[2] = p, saveargv2);
4751 while (*p && isspace (*p))
4752 p++;
4753 while (*p)
4755 if (*p == '/')
4756 *p++ = '\\';
4757 else
4758 p++;
4763 chdir (working_dir);
4764 inbak = dup (0);
4765 outbak = dup (1);
4766 errbak = dup (2);
4767 if (inbak < 0 || outbak < 0 || errbak < 0)
4768 goto done; /* Allocation might fail due to lack of descriptors. */
4770 if (have_mouse > 0)
4771 mouse_get_xy (&x, &y);
4773 dos_ttcooked (); /* do it here while 0 = stdin */
4775 dup2 (tempin, 0);
4776 dup2 (tempout, 1);
4777 dup2 (temperr, 2);
4779 #if __DJGPP__ > 1
4781 if (msshell && !argv[3])
4783 /* MS-DOS native shells are too restrictive. For starters, they
4784 cannot grok commands longer than 126 characters. In DJGPP v2
4785 and later, `system' is much smarter, so we'll call it instead. */
4787 const char *cmnd;
4789 /* A shell gets a single argument--its full command
4790 line--whose original was saved in `saveargv2'. */
4792 /* Don't let them pass empty command lines to `system', since
4793 with some shells it will try to invoke an interactive shell,
4794 which will hang Emacs. */
4795 for (cmnd = saveargv2; *cmnd && isspace (*cmnd); cmnd++)
4797 if (*cmnd)
4799 extern char **environ;
4800 char **save_env = environ;
4801 int save_system_flags = __system_flags;
4803 /* Request the most powerful version of `system'. We need
4804 all the help we can get to avoid calling stock DOS shells. */
4805 __system_flags = (__system_redirect
4806 | __system_use_shell
4807 | __system_allow_multiple_cmds
4808 | __system_allow_long_cmds
4809 | __system_handle_null_commands
4810 | __system_emulate_chdir);
4812 environ = envv;
4813 result = system (cmnd);
4814 __system_flags = save_system_flags;
4815 environ = save_env;
4817 else
4818 result = 0; /* emulate Unixy shell behavior with empty cmd line */
4820 else
4822 #endif /* __DJGPP__ > 1 */
4824 result = spawnve (P_WAIT, argv[0], argv, envv);
4826 dup2 (inbak, 0);
4827 dup2 (outbak, 1);
4828 dup2 (errbak, 2);
4829 emacs_close (inbak);
4830 emacs_close (outbak);
4831 emacs_close (errbak);
4833 dos_ttraw ();
4834 if (have_mouse > 0)
4836 mouse_init ();
4837 mouse_moveto (x, y);
4840 /* Some programs might change the meaning of the highest bit of the
4841 text attribute byte, so we get blinking characters instead of the
4842 bright background colors. Restore that. */
4843 bright_bg ();
4845 done:
4846 chdir (oldwd);
4847 if (msshell)
4849 argv[1] = saveargv1;
4850 argv[2] = saveargv2;
4852 return result;
4855 void
4856 croak (badfunc)
4857 char *badfunc;
4859 fprintf (stderr, "%s not yet implemented\r\n", badfunc);
4860 reset_sys_modes ();
4861 exit (1);
4864 #if __DJGPP__ < 2
4866 /* ------------------------- Compatibility functions -------------------
4867 * gethostname
4868 * gettimeofday
4871 /* Hostnames for a pc are not really funny,
4872 but they are used in change log so we emulate the best we can. */
4874 gethostname (p, size)
4875 char *p;
4876 int size;
4878 char *q = egetenv ("HOSTNAME");
4880 if (!q) q = "pc";
4881 strcpy (p, q);
4882 return 0;
4885 /* When time zones are set from Ms-Dos too many C-libraries are playing
4886 tricks with time values. We solve this by defining our own version
4887 of `gettimeofday' bypassing GO32. Our version needs to be initialized
4888 once and after each call to `tzset' with TZ changed. That is
4889 accomplished by aliasing tzset to init_gettimeofday. */
4891 static struct tm time_rec;
4894 gettimeofday (struct timeval *tp, struct timezone *tzp)
4896 if (tp)
4898 struct time t;
4899 struct tm tm;
4901 gettime (&t);
4902 if (t.ti_hour < time_rec.tm_hour) /* midnight wrap */
4904 struct date d;
4905 getdate (&d);
4906 time_rec.tm_year = d.da_year - 1900;
4907 time_rec.tm_mon = d.da_mon - 1;
4908 time_rec.tm_mday = d.da_day;
4911 time_rec.tm_hour = t.ti_hour;
4912 time_rec.tm_min = t.ti_min;
4913 time_rec.tm_sec = t.ti_sec;
4915 tm = time_rec;
4916 tm.tm_gmtoff = dos_timezone_offset;
4918 tp->tv_sec = mktime (&tm); /* may modify tm */
4919 tp->tv_usec = t.ti_hund * (1000000 / 100);
4921 /* Ignore tzp; it's obsolescent. */
4922 return 0;
4925 #endif /* __DJGPP__ < 2 */
4928 * A list of unimplemented functions that we silently ignore.
4931 #if __DJGPP__ < 2
4932 unsigned alarm (s) unsigned s; {}
4933 fork () { return 0; }
4934 int kill (x, y) int x, y; { return -1; }
4935 nice (p) int p; {}
4936 void volatile pause () {}
4937 sigsetmask (x) int x; { return 0; }
4938 sigblock (mask) int mask; { return 0; }
4939 #endif
4941 void request_sigio (void) {}
4942 setpgrp () {return 0; }
4943 setpriority (x,y,z) int x,y,z; { return 0; }
4944 void unrequest_sigio (void) {}
4946 #if __DJGPP__ > 1
4947 #if __DJGPP_MINOR__ < 2
4949 #ifdef POSIX_SIGNALS
4951 /* Augment DJGPP library POSIX signal functions. This is needed
4952 as of DJGPP v2.01, but might be in the library in later releases. */
4954 #include <libc/bss.h>
4956 /* A counter to know when to re-initialize the static sets. */
4957 static int sigprocmask_count = -1;
4959 /* Which signals are currently blocked (initially none). */
4960 static sigset_t current_mask;
4962 /* Which signals are pending (initially none). */
4963 static sigset_t pending_signals;
4965 /* Previous handlers to restore when the blocked signals are unblocked. */
4966 typedef void (*sighandler_t)(int);
4967 static sighandler_t prev_handlers[320];
4969 /* A signal handler which just records that a signal occurred
4970 (it will be raised later, if and when the signal is unblocked). */
4971 static void
4972 sig_suspender (signo)
4973 int signo;
4975 sigaddset (&pending_signals, signo);
4979 sigprocmask (how, new_set, old_set)
4980 int how;
4981 const sigset_t *new_set;
4982 sigset_t *old_set;
4984 int signo;
4985 sigset_t new_mask;
4987 /* If called for the first time, initialize. */
4988 if (sigprocmask_count != __bss_count)
4990 sigprocmask_count = __bss_count;
4991 sigemptyset (&pending_signals);
4992 sigemptyset (&current_mask);
4993 for (signo = 0; signo < 320; signo++)
4994 prev_handlers[signo] = SIG_ERR;
4997 if (old_set)
4998 *old_set = current_mask;
5000 if (new_set == 0)
5001 return 0;
5003 if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK)
5005 errno = EINVAL;
5006 return -1;
5009 sigemptyset (&new_mask);
5011 /* DJGPP supports upto 320 signals. */
5012 for (signo = 0; signo < 320; signo++)
5014 if (sigismember (&current_mask, signo))
5015 sigaddset (&new_mask, signo);
5016 else if (sigismember (new_set, signo) && how != SIG_UNBLOCK)
5018 sigaddset (&new_mask, signo);
5020 /* SIGKILL is silently ignored, as on other platforms. */
5021 if (signo != SIGKILL && prev_handlers[signo] == SIG_ERR)
5022 prev_handlers[signo] = signal (signo, sig_suspender);
5024 if (( how == SIG_UNBLOCK
5025 && sigismember (&new_mask, signo)
5026 && sigismember (new_set, signo))
5027 || (how == SIG_SETMASK
5028 && sigismember (&new_mask, signo)
5029 && !sigismember (new_set, signo)))
5031 sigdelset (&new_mask, signo);
5032 if (prev_handlers[signo] != SIG_ERR)
5034 signal (signo, prev_handlers[signo]);
5035 prev_handlers[signo] = SIG_ERR;
5037 if (sigismember (&pending_signals, signo))
5039 sigdelset (&pending_signals, signo);
5040 raise (signo);
5044 current_mask = new_mask;
5045 return 0;
5048 #else /* not POSIX_SIGNALS */
5050 sigsetmask (x) int x; { return 0; }
5051 sigblock (mask) int mask; { return 0; }
5053 #endif /* not POSIX_SIGNALS */
5054 #endif /* not __DJGPP_MINOR__ < 2 */
5055 #endif /* __DJGPP__ > 1 */
5057 #ifndef HAVE_SELECT
5058 #include "sysselect.h"
5060 #ifndef EMACS_TIME_ZERO_OR_NEG_P
5061 #define EMACS_TIME_ZERO_OR_NEG_P(time) \
5062 ((long)(time).tv_sec < 0 \
5063 || ((time).tv_sec == 0 \
5064 && (long)(time).tv_usec <= 0))
5065 #endif
5067 /* This yields the rest of the current time slice to the task manager.
5068 It should be called by any code which knows that it has nothing
5069 useful to do except idle.
5071 I don't use __dpmi_yield here, since versions of library before 2.02
5072 called Int 2Fh/AX=1680h there in a way that would wedge the DOS box
5073 on some versions of Windows 9X. */
5075 void
5076 dos_yield_time_slice (void)
5078 _go32_dpmi_registers r;
5080 r.x.ax = 0x1680;
5081 r.x.ss = r.x.sp = r.x.flags = 0;
5082 _go32_dpmi_simulate_int (0x2f, &r);
5083 if (r.h.al == 0x80)
5084 errno = ENOSYS;
5087 /* Only event queue is checked. */
5088 /* We don't have to call timer_check here
5089 because wait_reading_process_input takes care of that. */
5091 sys_select (nfds, rfds, wfds, efds, timeout)
5092 int nfds;
5093 SELECT_TYPE *rfds, *wfds, *efds;
5094 EMACS_TIME *timeout;
5096 int check_input;
5097 struct time t;
5099 check_input = 0;
5100 if (rfds)
5102 check_input = FD_ISSET (0, rfds);
5103 FD_ZERO (rfds);
5105 if (wfds)
5106 FD_ZERO (wfds);
5107 if (efds)
5108 FD_ZERO (efds);
5110 if (nfds != 1)
5111 abort ();
5113 /* If we are looking only for the terminal, with no timeout,
5114 just read it and wait -- that's more efficient. */
5115 if (!timeout)
5117 while (!detect_input_pending ())
5119 dos_yield_time_slice ();
5122 else
5124 EMACS_TIME clnow, cllast, cldiff;
5126 gettime (&t);
5127 EMACS_SET_SECS_USECS (cllast, t.ti_sec, t.ti_hund * 10000L);
5129 while (!check_input || !detect_input_pending ())
5131 gettime (&t);
5132 EMACS_SET_SECS_USECS (clnow, t.ti_sec, t.ti_hund * 10000L);
5133 EMACS_SUB_TIME (cldiff, clnow, cllast);
5135 /* When seconds wrap around, we assume that no more than
5136 1 minute passed since last `gettime'. */
5137 if (EMACS_TIME_NEG_P (cldiff))
5138 EMACS_SET_SECS (cldiff, EMACS_SECS (cldiff) + 60);
5139 EMACS_SUB_TIME (*timeout, *timeout, cldiff);
5141 /* Stop when timeout value crosses zero. */
5142 if (EMACS_TIME_ZERO_OR_NEG_P (*timeout))
5143 return 0;
5144 cllast = clnow;
5145 dos_yield_time_slice ();
5149 FD_SET (0, rfds);
5150 return 1;
5152 #endif
5155 * Define overlaid functions:
5157 * chdir -> sys_chdir
5158 * tzset -> init_gettimeofday
5159 * abort -> dos_abort
5162 #ifdef chdir
5163 #undef chdir
5164 extern int chdir ();
5167 sys_chdir (path)
5168 const char* path;
5170 int len = strlen (path);
5171 char *tmp = (char *)path;
5173 if (*tmp && tmp[1] == ':')
5175 if (getdisk () != tolower (tmp[0]) - 'a')
5176 setdisk (tolower (tmp[0]) - 'a');
5177 tmp += 2; /* strip drive: KFS 1995-07-06 */
5178 len -= 2;
5181 if (len > 1 && (tmp[len - 1] == '/'))
5183 char *tmp1 = (char *) alloca (len + 1);
5184 strcpy (tmp1, tmp);
5185 tmp1[len - 1] = 0;
5186 tmp = tmp1;
5188 return chdir (tmp);
5190 #endif
5192 #ifdef tzset
5193 #undef tzset
5194 extern void tzset (void);
5196 void
5197 init_gettimeofday ()
5199 time_t ltm, gtm;
5200 struct tm *lstm;
5202 tzset ();
5203 ltm = gtm = time (NULL);
5204 ltm = mktime (lstm = localtime (&ltm));
5205 gtm = mktime (gmtime (&gtm));
5206 time_rec.tm_hour = 99; /* force gettimeofday to get date */
5207 time_rec.tm_isdst = lstm->tm_isdst;
5208 dos_timezone_offset = time_rec.tm_gmtoff = (int)(gtm - ltm) / 60;
5210 #endif
5212 #ifdef abort
5213 #undef abort
5214 void
5215 dos_abort (file, line)
5216 char *file;
5217 int line;
5219 char buffer1[200], buffer2[400];
5220 int i, j;
5222 sprintf (buffer1, "<EMACS FATAL ERROR IN %s LINE %d>", file, line);
5223 for (i = j = 0; buffer1[i]; i++) {
5224 buffer2[j++] = buffer1[i];
5225 buffer2[j++] = 0x70;
5227 dosmemput (buffer2, j, (int)ScreenPrimary);
5228 ScreenSetCursor (2, 0);
5229 abort ();
5231 #else
5232 void
5233 abort ()
5235 dos_ttcooked ();
5236 ScreenSetCursor (10, 0);
5237 cputs ("\r\n\nEmacs aborted!\r\n");
5238 #if __DJGPP__ > 1
5239 #if __DJGPP__ == 2 && __DJGPP_MINOR__ < 2
5240 if (screen_virtual_segment)
5241 dosv_refresh_virtual_screen (2 * 10 * screen_size_X, 4 * screen_size_X);
5242 /* Generate traceback, so we could tell whodunit. */
5243 signal (SIGINT, SIG_DFL);
5244 __asm__ __volatile__ ("movb $0x1b,%al;call ___djgpp_hw_exception");
5245 #else /* __DJGPP_MINOR__ >= 2 */
5246 raise (SIGABRT);
5247 #endif /* __DJGPP_MINOR__ >= 2 */
5248 #endif
5249 exit (2);
5251 #endif
5253 /* The following variables are required so that cus-start.el won't
5254 complain about unbound variables. */
5255 #ifndef subprocesses
5256 /* Nonzero means delete a process right away if it exits (process.c). */
5257 static int delete_exited_processes;
5258 #endif
5260 syms_of_msdos ()
5262 recent_doskeys = Fmake_vector (make_number (NUM_RECENT_DOSKEYS), Qnil);
5263 staticpro (&recent_doskeys);
5265 #ifndef HAVE_X_WINDOWS
5267 /* The following two are from xfns.c: */
5268 Qbar = intern ("bar");
5269 staticpro (&Qbar);
5270 Qhbar = intern ("hbar");
5271 staticpro (&Qhbar);
5272 Qcursor_type = intern ("cursor-type");
5273 staticpro (&Qcursor_type);
5274 Qreverse = intern ("reverse");
5275 staticpro (&Qreverse);
5277 DEFVAR_LISP ("dos-unsupported-char-glyph", &Vdos_unsupported_char_glyph,
5278 doc: /* *Glyph to display instead of chars not supported by current codepage.
5280 This variable is used only by MSDOS terminals. */);
5281 Vdos_unsupported_char_glyph = '\177';
5283 #endif
5284 #ifndef subprocesses
5285 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
5286 doc: /* *Non-nil means delete processes immediately when they exit.
5287 nil means don't delete them until `list-processes' is run. */);
5288 delete_exited_processes = 0;
5289 #endif
5291 defsubr (&Srecent_doskeys);
5292 defsubr (&Smsdos_long_file_names);
5293 defsubr (&Smsdos_downcase_filename);
5294 defsubr (&Smsdos_remember_default_colors);
5295 defsubr (&Smsdos_set_mouse_buttons);
5298 #endif /* MSDOS */