]> code.delx.au - gnu-emacs/blob - src/msdos.c
(IT_write_glyphs): Allocate a larger screen_buf as data
[gnu-emacs] / src / msdos.c
1 /* MS-DOS specific C utilities. -*- coding: raw-text -*-
2 Copyright (C) 1993, 94, 95, 96, 97, 1999 Free Software Foundation, Inc.
3
4 This file is part of GNU Emacs.
5
6 GNU Emacs is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU Emacs is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
20
21 /* Contributed by Morten Welinder */
22 /* New display, keyboard, and mouse control by Kim F. Storm */
23
24 /* Note: some of the stuff here was taken from end of sysdep.c in demacs. */
25
26 #include <config.h>
27
28 #ifdef MSDOS
29 #include "lisp.h"
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <time.h>
33 #include <sys/param.h>
34 #include <sys/time.h>
35 #include <dos.h>
36 #include <errno.h>
37 #include <string.h> /* for bzero and string functions */
38 #include <sys/stat.h> /* for _fixpath */
39 #include <unistd.h> /* for chdir, dup, dup2, etc. */
40 #if __DJGPP__ >= 2
41 #include <fcntl.h>
42 #include <io.h> /* for setmode */
43 #include <dpmi.h> /* for __dpmi_xxx stuff */
44 #include <sys/farptr.h> /* for _farsetsel, _farnspokeb */
45 #include <libc/dosio.h> /* for _USE_LFN */
46 #include <conio.h> /* for cputs */
47 #endif
48
49 #include "msdos.h"
50 #include "systime.h"
51 #include "termhooks.h"
52 #include "termchar.h"
53 #include "dispextern.h"
54 #include "dosfns.h"
55 #include "termopts.h"
56 #include "charset.h"
57 #include "coding.h"
58 #include "disptab.h"
59 #include "frame.h"
60 #include "window.h"
61 #include "buffer.h"
62 #include "commands.h"
63 #include "blockinput.h"
64 #include <go32.h>
65 #include <pc.h>
66 #include <ctype.h>
67 /* #include <process.h> */
68 /* Damn that local process.h! Instead we can define P_WAIT ourselves. */
69 #define P_WAIT 1
70
71 #ifndef _USE_LFN
72 #define _USE_LFN 0
73 #endif
74
75 #ifndef _dos_ds
76 #define _dos_ds _go32_info_block.selector_for_linear_memory
77 #endif
78
79 #if __DJGPP__ > 1
80
81 #include <signal.h>
82 #include "syssignal.h"
83
84 #ifndef SYSTEM_MALLOC
85
86 #ifdef GNU_MALLOC
87
88 /* If other `malloc' than ours is used, force our `sbrk' behave like
89 Unix programs expect (resize memory blocks to keep them contiguous).
90 If `sbrk' from `ralloc.c' is NOT used, also zero-out sbrk'ed memory,
91 because that's what `gmalloc' expects to get. */
92 #include <crt0.h>
93
94 #ifdef REL_ALLOC
95 int _crt0_startup_flags = _CRT0_FLAG_UNIX_SBRK;
96 #else /* not REL_ALLOC */
97 int _crt0_startup_flags = (_CRT0_FLAG_UNIX_SBRK | _CRT0_FLAG_FILL_SBRK_MEMORY);
98 #endif /* not REL_ALLOC */
99 #endif /* GNU_MALLOC */
100
101 #endif /* not SYSTEM_MALLOC */
102 #endif /* __DJGPP__ > 1 */
103
104 static unsigned long
105 event_timestamp ()
106 {
107 struct time t;
108 unsigned long s;
109
110 gettime (&t);
111 s = t.ti_min;
112 s *= 60;
113 s += t.ti_sec;
114 s *= 1000;
115 s += t.ti_hund * 10;
116
117 return s;
118 }
119
120 \f
121 /* ------------------------ Mouse control ---------------------------
122 *
123 * Coordinates are in screen positions and zero based.
124 * Mouse buttons are numbered from left to right and also zero based.
125 */
126
127 int have_mouse; /* 0: no, 1: enabled, -1: disabled */
128 static int mouse_visible;
129
130 static int mouse_last_x;
131 static int mouse_last_y;
132
133 static int mouse_button_translate[NUM_MOUSE_BUTTONS];
134 static int mouse_button_count;
135
136 void
137 mouse_on ()
138 {
139 union REGS regs;
140
141 if (have_mouse > 0 && !mouse_visible)
142 {
143 if (termscript)
144 fprintf (termscript, "<M_ON>");
145 regs.x.ax = 0x0001;
146 int86 (0x33, &regs, &regs);
147 mouse_visible = 1;
148 }
149 }
150
151 void
152 mouse_off ()
153 {
154 union REGS regs;
155
156 if (have_mouse > 0 && mouse_visible)
157 {
158 if (termscript)
159 fprintf (termscript, "<M_OFF>");
160 regs.x.ax = 0x0002;
161 int86 (0x33, &regs, &regs);
162 mouse_visible = 0;
163 }
164 }
165
166 static void
167 mouse_get_xy (int *x, int *y)
168 {
169 union REGS regs;
170
171 regs.x.ax = 0x0003;
172 int86 (0x33, &regs, &regs);
173 *x = regs.x.cx / 8;
174 *y = regs.x.dx / 8;
175 }
176
177 void
178 mouse_moveto (x, y)
179 int x, y;
180 {
181 union REGS regs;
182
183 if (termscript)
184 fprintf (termscript, "<M_XY=%dx%d>", x, y);
185 regs.x.ax = 0x0004;
186 mouse_last_x = regs.x.cx = x * 8;
187 mouse_last_y = regs.x.dx = y * 8;
188 int86 (0x33, &regs, &regs);
189 }
190
191 static int
192 mouse_pressed (b, xp, yp)
193 int b, *xp, *yp;
194 {
195 union REGS regs;
196
197 if (b >= mouse_button_count)
198 return 0;
199 regs.x.ax = 0x0005;
200 regs.x.bx = mouse_button_translate[b];
201 int86 (0x33, &regs, &regs);
202 if (regs.x.bx)
203 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
204 return (regs.x.bx != 0);
205 }
206
207 static int
208 mouse_released (b, xp, yp)
209 int b, *xp, *yp;
210 {
211 union REGS regs;
212
213 if (b >= mouse_button_count)
214 return 0;
215 regs.x.ax = 0x0006;
216 regs.x.bx = mouse_button_translate[b];
217 int86 (0x33, &regs, &regs);
218 if (regs.x.bx)
219 *xp = regs.x.cx / 8, *yp = regs.x.dx / 8;
220 return (regs.x.bx != 0);
221 }
222
223 static int
224 mouse_button_depressed (b, xp, yp)
225 int b, *xp, *yp;
226 {
227 union REGS regs;
228
229 if (b >= mouse_button_count)
230 return 0;
231 regs.x.ax = 0x0003;
232 int86 (0x33, &regs, &regs);
233 if ((regs.x.bx & (1 << mouse_button_translate[b])) != 0)
234 {
235 *xp = regs.x.cx / 8;
236 *yp = regs.x.dx / 8;
237 return 1;
238 }
239 return 0;
240 }
241
242 void
243 mouse_get_pos (f, insist, bar_window, part, x, y, time)
244 FRAME_PTR *f;
245 int insist;
246 Lisp_Object *bar_window, *x, *y;
247 enum scroll_bar_part *part;
248 unsigned long *time;
249 {
250 int ix, iy;
251 Lisp_Object frame, tail;
252
253 /* Clear the mouse-moved flag for every frame on this display. */
254 FOR_EACH_FRAME (tail, frame)
255 XFRAME (frame)->mouse_moved = 0;
256
257 *f = SELECTED_FRAME();
258 *bar_window = Qnil;
259 mouse_get_xy (&ix, &iy);
260 *time = event_timestamp ();
261 *x = make_number (mouse_last_x = ix);
262 *y = make_number (mouse_last_y = iy);
263 }
264
265 static void
266 mouse_check_moved ()
267 {
268 int x, y;
269
270 mouse_get_xy (&x, &y);
271 SELECTED_FRAME()->mouse_moved |= (x != mouse_last_x || y != mouse_last_y);
272 mouse_last_x = x;
273 mouse_last_y = y;
274 }
275
276 void
277 mouse_init ()
278 {
279 union REGS regs;
280 int b;
281
282 if (termscript)
283 fprintf (termscript, "<M_INIT>");
284
285 regs.x.ax = 0x0021;
286 int86 (0x33, &regs, &regs);
287
288 /* Reset the mouse last press/release info. It seems that Windows
289 doesn't do that automatically when function 21h is called, which
290 causes Emacs to ``remember'' the click that switched focus to the
291 window just before Emacs was started from that window. */
292 for (b = 0; b < mouse_button_count; b++)
293 {
294 int dummy_x, dummy_y;
295
296 (void) mouse_pressed (b, &dummy_x, &dummy_y);
297 (void) mouse_released (b, &dummy_x, &dummy_y);
298 }
299
300 regs.x.ax = 0x0007;
301 regs.x.cx = 0;
302 regs.x.dx = 8 * (ScreenCols () - 1);
303 int86 (0x33, &regs, &regs);
304
305 regs.x.ax = 0x0008;
306 regs.x.cx = 0;
307 regs.x.dx = 8 * (ScreenRows () - 1);
308 int86 (0x33, &regs, &regs);
309
310 mouse_moveto (0, 0);
311 mouse_visible = 0;
312 }
313 \f
314 /* ------------------------- Screen control ----------------------
315 *
316 */
317
318 static int internal_terminal = 0;
319
320 #ifndef HAVE_X_WINDOWS
321 extern unsigned char ScreenAttrib;
322 static int screen_face;
323 static int highlight;
324
325 static int screen_size_X;
326 static int screen_size_Y;
327 static int screen_size;
328
329 static int current_pos_X;
330 static int current_pos_Y;
331 static int new_pos_X;
332 static int new_pos_Y;
333
334 static void *startup_screen_buffer;
335 static int startup_screen_size_X;
336 static int startup_screen_size_Y;
337 static int startup_pos_X;
338 static int startup_pos_Y;
339 static unsigned char startup_screen_attrib;
340
341 static clock_t startup_time;
342
343 static int term_setup_done;
344
345 /* Similar to the_only_frame. */
346 struct x_output the_only_x_display;
347
348 /* Support for DOS/V (allows Japanese characters to be displayed on
349 standard, non-Japanese, ATs). Only supported for DJGPP v2 and later. */
350
351 /* Holds the address of the text-mode screen buffer. */
352 static unsigned long screen_old_address = 0;
353 /* Segment and offset of the virtual screen. If 0, DOS/V is NOT loaded. */
354 static unsigned short screen_virtual_segment = 0;
355 static unsigned short screen_virtual_offset = 0;
356 /* A flag to control how to display unibyte 8-bit characters. */
357 extern int unibyte_display_via_language_environment;
358
359 #if __DJGPP__ > 1
360 /* Update the screen from a part of relocated DOS/V screen buffer which
361 begins at OFFSET and includes COUNT characters. */
362 static void
363 dosv_refresh_virtual_screen (int offset, int count)
364 {
365 __dpmi_regs regs;
366
367 if (offset < 0 || count < 0) /* paranoia; illegal values crash DOS/V */
368 return;
369
370 regs.h.ah = 0xff; /* update relocated screen */
371 regs.x.es = screen_virtual_segment;
372 regs.x.di = screen_virtual_offset + offset;
373 regs.x.cx = count;
374 __dpmi_int (0x10, &regs);
375 }
376 #endif
377
378 static void
379 dos_direct_output (y, x, buf, len)
380 int y;
381 int x;
382 char *buf;
383 int len;
384 {
385 int t0 = 2 * (x + y * screen_size_X);
386 int t = t0 + (int) ScreenPrimary;
387 int l0 = len;
388
389 #if (__DJGPP__ < 2)
390 while (--len >= 0) {
391 dosmemput (buf++, 1, t);
392 t += 2;
393 }
394 #else
395 /* This is faster. */
396 for (_farsetsel (_dos_ds); --len >= 0; t += 2, buf++)
397 _farnspokeb (t, *buf);
398
399 if (screen_virtual_segment)
400 dosv_refresh_virtual_screen (t0, l0);
401 #endif
402 }
403 #endif
404
405 /* Flash the screen as a substitute for BEEPs. */
406
407 #if (__DJGPP__ < 2)
408 static void
409 do_visible_bell (xorattr)
410 unsigned char xorattr;
411 {
412 asm volatile
413 (" movb $1,%%dl
414 visible_bell_0:
415 movl _ScreenPrimary,%%eax
416 call dosmemsetup
417 movl %%eax,%%ebx
418 movl %1,%%ecx
419 movb %0,%%al
420 incl %%ebx
421 visible_bell_1:
422 xorb %%al,%%gs:(%%ebx)
423 addl $2,%%ebx
424 decl %%ecx
425 jne visible_bell_1
426 decb %%dl
427 jne visible_bell_3
428 visible_bell_2:
429 movzwl %%ax,%%eax
430 movzwl %%ax,%%eax
431 movzwl %%ax,%%eax
432 movzwl %%ax,%%eax
433 decw %%cx
434 jne visible_bell_2
435 jmp visible_bell_0
436 visible_bell_3:"
437 : /* no output */
438 : "m" (xorattr), "g" (screen_size)
439 : "%eax", "%ebx", /* "%gs",*/ "%ecx", "%edx");
440 }
441
442 static void
443 ScreenVisualBell (void)
444 {
445 /* This creates an xor-mask that will swap the default fore- and
446 background colors. */
447 do_visible_bell (((the_only_x_display.foreground_pixel
448 ^ the_only_x_display.background_pixel)
449 * 0x11) & 0x7f);
450 }
451 #endif
452
453 #ifndef HAVE_X_WINDOWS
454
455 static int blink_bit = -1; /* the state of the blink bit at startup */
456
457 /* Enable bright background colors. */
458 static void
459 bright_bg (void)
460 {
461 union REGS regs;
462
463 /* Remember the original state of the blink/bright-background bit.
464 It is stored at 0040:0065h in the BIOS data area. */
465 if (blink_bit == -1)
466 blink_bit = (_farpeekb (_dos_ds, 0x465) & 0x20) == 0x20;
467
468 regs.h.bl = 0;
469 regs.x.ax = 0x1003;
470 int86 (0x10, &regs, &regs);
471 }
472
473 /* Disable bright background colors (and enable blinking) if we found
474 the video system in that state at startup. */
475 static void
476 maybe_enable_blinking (void)
477 {
478 if (blink_bit == 1)
479 {
480 union REGS regs;
481
482 regs.h.bl = 1;
483 regs.x.ax = 0x1003;
484 int86 (0x10, &regs, &regs);
485 }
486 }
487
488 /* Set the screen dimensions so that it can show no less than
489 ROWS x COLS frame. */
490
491 void
492 dos_set_window_size (rows, cols)
493 int *rows, *cols;
494 {
495 char video_name[30];
496 Lisp_Object video_mode;
497 int video_mode_value;
498 int have_vga = 0;
499 union REGS regs;
500 int current_rows = ScreenRows (), current_cols = ScreenCols ();
501
502 if (*rows == current_rows && *cols == current_cols)
503 return;
504
505 /* Do we have a VGA? */
506 regs.x.ax = 0x1a00;
507 int86 (0x10, &regs, &regs);
508 if (regs.h.al == 0x1a && regs.h.bl > 5 && regs.h.bl < 13)
509 have_vga = 1;
510
511 mouse_off ();
512
513 /* If the user specified a special video mode for these dimensions,
514 use that mode. */
515 sprintf (video_name, "screen-dimensions-%dx%d", *rows, *cols);
516 video_mode = XSYMBOL (Fintern_soft (build_string (video_name),
517 Qnil))-> value;
518
519 if (INTEGERP (video_mode)
520 && (video_mode_value = XINT (video_mode)) > 0)
521 {
522 regs.x.ax = video_mode_value;
523 int86 (0x10, &regs, &regs);
524
525 if (have_mouse)
526 {
527 /* Must hardware-reset the mouse, or else it won't update
528 its notion of screen dimensions for some non-standard
529 video modes. This is *painfully* slow... */
530 regs.x.ax = 0;
531 int86 (0x33, &regs, &regs);
532 }
533 }
534
535 /* Find one of the dimensions supported by standard EGA/VGA
536 which gives us at least the required dimensions. */
537
538 #if __DJGPP__ > 1
539
540 else
541 {
542 static struct {
543 int rows;
544 int need_vga;
545 } std_dimension[] = {
546 {25, 0},
547 {28, 1},
548 {35, 0},
549 {40, 1},
550 {43, 0},
551 {50, 1}
552 };
553 int i = 0;
554
555 while (i < sizeof (std_dimension) / sizeof (std_dimension[0]))
556 {
557 if (std_dimension[i].need_vga <= have_vga
558 && std_dimension[i].rows >= *rows)
559 {
560 if (std_dimension[i].rows != current_rows
561 || *cols != current_cols)
562 _set_screen_lines (std_dimension[i].rows);
563 break;
564 }
565 i++;
566 }
567 }
568
569 #else /* not __DJGPP__ > 1 */
570
571 else if (*rows <= 25)
572 {
573 if (current_rows != 25 || current_cols != 80)
574 {
575 regs.x.ax = 3;
576 int86 (0x10, &regs, &regs);
577 regs.x.ax = 0x1101;
578 regs.h.bl = 0;
579 int86 (0x10, &regs, &regs);
580 regs.x.ax = 0x1200;
581 regs.h.bl = 32;
582 int86 (0x10, &regs, &regs);
583 regs.x.ax = 3;
584 int86 (0x10, &regs, &regs);
585 }
586 }
587 else if (*rows <= 50)
588 if (have_vga && (current_rows != 50 || current_cols != 80)
589 || *rows <= 43 && (current_rows != 43 || current_cols != 80))
590 {
591 regs.x.ax = 3;
592 int86 (0x10, &regs, &regs);
593 regs.x.ax = 0x1112;
594 regs.h.bl = 0;
595 int86 (0x10, &regs, &regs);
596 regs.x.ax = 0x1200;
597 regs.h.bl = 32;
598 int86 (0x10, &regs, &regs);
599 regs.x.ax = 0x0100;
600 regs.x.cx = 7;
601 int86 (0x10, &regs, &regs);
602 }
603 #endif /* not __DJGPP__ > 1 */
604
605 if (have_mouse)
606 {
607 mouse_init ();
608 mouse_on ();
609 }
610
611 /* Tell the caller what dimensions have been REALLY set. */
612 *rows = ScreenRows ();
613 *cols = ScreenCols ();
614
615 #if __DJGPP__ > 1
616 /* If the dimensions changed, the mouse highlight info is invalid. */
617 if (current_rows != *rows || current_cols != *cols)
618 {
619 struct frame *f = SELECTED_FRAME();
620 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
621 Lisp_Object window = dpyinfo->mouse_face_window;
622
623 if (! NILP (window) && XFRAME (XWINDOW (window)->frame) == f)
624 {
625 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
626 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
627 dpyinfo->mouse_face_window = Qnil;
628 }
629 }
630 #endif
631
632 /* Enable bright background colors. */
633 bright_bg ();
634
635 /* FIXME: I'm not sure the above will run at all on DOS/V. But let's
636 be defensive anyway. */
637 if (screen_virtual_segment)
638 dosv_refresh_virtual_screen (0, *cols * *rows);
639 }
640
641 /* If we write a character in the position where the mouse is,
642 the mouse cursor may need to be refreshed. */
643
644 static void
645 mouse_off_maybe ()
646 {
647 int x, y;
648
649 if (!mouse_visible)
650 return;
651
652 mouse_get_xy (&x, &y);
653 if (y != new_pos_Y || x < new_pos_X)
654 return;
655
656 mouse_off ();
657 }
658
659 static void
660 IT_ring_bell (void)
661 {
662 if (visible_bell)
663 {
664 mouse_off ();
665 ScreenVisualBell ();
666 }
667 else
668 {
669 union REGS inregs, outregs;
670 inregs.h.ah = 2;
671 inregs.h.dl = 7;
672 intdos (&inregs, &outregs);
673 }
674 }
675
676 /* Given a face id FACE, extract the face parameters to be used for
677 display until the face changes. The face parameters (actually, its
678 color) are used to construct the video attribute byte for each
679 glyph during the construction of the buffer that is then blitted to
680 the video RAM. */
681 static void
682 IT_set_face (int face)
683 {
684 struct frame *sf = SELECTED_FRAME();
685 struct face *fp = FACE_FROM_ID (sf, face);
686 unsigned long fg, bg;
687
688 if (!fp)
689 {
690 fp = FACE_FROM_ID (sf, DEFAULT_FACE_ID);
691 /* The default face for the frame should always be realized and
692 cached. */
693 if (!fp)
694 abort ();
695 }
696 screen_face = face;
697 fg = fp->foreground;
698 bg = fp->background;
699
700 /* Don't use invalid colors. In particular, FACE_TTY_DEFAULT_*
701 colors mean use the colors of the default face, except that if
702 highlight is on, invert the foreground and the background. Note
703 that we assume all 16 colors to be available for the background,
704 since Emacs switches on this mode (and loses the blinking
705 attribute) at startup. */
706 if (fg == FACE_TTY_DEFAULT_COLOR || fg == FACE_TTY_DEFAULT_FG_COLOR)
707 fg = FRAME_FOREGROUND_PIXEL (sf);
708 else if (fg == FACE_TTY_DEFAULT_BG_COLOR)
709 fg = FRAME_BACKGROUND_PIXEL (sf);
710 if (bg == FACE_TTY_DEFAULT_COLOR || bg == FACE_TTY_DEFAULT_BG_COLOR)
711 bg = FRAME_BACKGROUND_PIXEL (sf);
712 else if (bg == FACE_TTY_DEFAULT_FG_COLOR)
713 bg = FRAME_FOREGROUND_PIXEL (sf);
714
715 /* Make sure highlighted lines really stand out, come what may. */
716 if ((highlight || fp->tty_reverse_p)
717 && (fg == FRAME_FOREGROUND_PIXEL (sf)
718 && bg == FRAME_BACKGROUND_PIXEL (sf)))
719 {
720 unsigned long tem = fg;
721
722 fg = bg;
723 bg = tem;
724 }
725 if (termscript)
726 fprintf (termscript, "<FACE %d%s: %d/%d[FG:%d/BG:%d]>", face,
727 highlight ? "H" : "", fp->foreground, fp->background, fg, bg);
728 if (fg >= 0 && fg < 16)
729 {
730 ScreenAttrib &= 0xf0;
731 ScreenAttrib |= fg;
732 }
733 if (bg >= 0 && bg < 16)
734 {
735 ScreenAttrib &= 0x0f;
736 ScreenAttrib |= ((bg & 0x0f) << 4);
737 }
738 }
739
740 Lisp_Object Vdos_unsupported_char_glyph;
741
742 static void
743 IT_write_glyphs (struct glyph *str, int str_len)
744 {
745 unsigned char *screen_buf, *screen_bp, *screen_buf_end, *bp;
746 int unsupported_face = FAST_GLYPH_FACE (Vdos_unsupported_char_glyph);
747 unsigned unsupported_char= FAST_GLYPH_CHAR (Vdos_unsupported_char_glyph);
748 int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
749 register int sl = str_len;
750 register int tlen = GLYPH_TABLE_LENGTH;
751 register Lisp_Object *tbase = GLYPH_TABLE_BASE;
752
753 struct coding_system *coding = (CODING_REQUIRE_ENCODING (&terminal_coding)
754 ? &terminal_coding
755 : &safe_terminal_coding);
756 struct frame *sf;
757
758 /* Do we need to consider conversion of unibyte characters to
759 multibyte? */
760 int convert_unibyte_characters
761 = (NILP (current_buffer->enable_multibyte_characters)
762 && unibyte_display_via_language_environment);
763
764 if (str_len <= 0) return;
765
766 screen_buf = screen_bp = alloca (str_len * 2);
767 screen_buf_end = screen_buf + str_len * 2;
768 sf = SELECTED_FRAME();
769
770 /* Since faces get cached and uncached behind our back, we can't
771 rely on their indices in the cache being consistent across
772 invocations. So always reset the screen face to the default
773 face of the frame, before writing glyphs, and let the glyphs
774 set the right face if it's different from the default. */
775 IT_set_face (DEFAULT_FACE_ID);
776
777 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
778 the tail. */
779 terminal_coding.mode &= ~CODING_MODE_LAST_BLOCK;
780 while (sl)
781 {
782 int cf, chlen, enclen;
783 unsigned char workbuf[MAX_MULTIBYTE_LENGTH], *buf;
784 unsigned ch;
785
786 /* Glyphs with GLYPH_MASK_PADDING bit set are actually there
787 only for the redisplay code to know how many columns does
788 this character occupy on the screen. Skip padding glyphs. */
789 if (CHAR_GLYPH_PADDING_P (*str))
790 {
791 str++;
792 sl--;
793 }
794 else
795 {
796 register GLYPH g = GLYPH_FROM_CHAR_GLYPH (*str);
797 int glyph_not_in_table = 0;
798
799 if (g < 0 || g >= tlen)
800 {
801 /* This glyph doesn't have an entry in Vglyph_table. */
802 ch = str->u.ch;
803 glyph_not_in_table = 1;
804 }
805 else
806 {
807 /* This glyph has an entry in Vglyph_table, so process
808 any aliases before testing for simpleness. */
809 GLYPH_FOLLOW_ALIASES (tbase, tlen, g);
810 ch = FAST_GLYPH_CHAR (g);
811 }
812
813 /* Convert the character code to multibyte, if they
814 requested display via language environment. We only want
815 to convert unibyte characters to multibyte in unibyte
816 buffers! Otherwise, the 8-bit value in CH came from the
817 display table set up to display foreign characters. */
818 if (SINGLE_BYTE_CHAR_P (ch) && convert_unibyte_characters
819 && (ch >= 0240
820 || (ch >= 0200 && !NILP (Vnonascii_translation_table))))
821 ch = unibyte_char_to_multibyte (ch);
822
823 /* Invalid characters are displayed with a special glyph. */
824 if (! CHAR_VALID_P (ch, 0))
825 {
826 g = !NILP (Vdos_unsupported_char_glyph)
827 ? Vdos_unsupported_char_glyph
828 : MAKE_GLYPH (sf, '\177', GLYPH_FACE (sf, g));
829 ch = FAST_GLYPH_CHAR (g);
830 }
831
832 /* If the face of this glyph is different from the current
833 screen face, update the screen attribute byte. */
834 cf = FAST_GLYPH_FACE (g);
835 if (cf != screen_face)
836 IT_set_face (cf); /* handles invalid faces gracefully */
837
838 if (glyph_not_in_table || GLYPH_SIMPLE_P (tbase, tlen, g))
839 {
840 /* We generate the multi-byte form of CH in WORKBUF. */
841 chlen = CHAR_STRING (ch, workbuf);
842 buf = workbuf;
843 }
844 else
845 {
846 /* We have a string in Vglyph_table. */
847 chlen = GLYPH_LENGTH (tbase, g);
848 buf = GLYPH_STRING (tbase, g);
849 }
850
851 /* If the character is not multibyte, don't bother converting it. */
852 if (chlen == 1)
853 {
854 *conversion_buffer = (unsigned char)ch;
855 chlen = 0;
856 enclen = 1;
857 }
858 else
859 {
860 encode_coding (coding, buf, conversion_buffer, chlen,
861 conversion_buffer_size);
862 chlen -= coding->consumed;
863 enclen = coding->produced;
864
865 /* Replace glyph codes that cannot be converted by
866 terminal_coding with Vdos_unsupported_char_glyph. */
867 if (*conversion_buffer == '?')
868 {
869 char *cbp = conversion_buffer;
870
871 while (cbp < conversion_buffer + enclen && *cbp == '?')
872 *cbp++ = unsupported_char;
873 if (unsupported_face != screen_face)
874 IT_set_face (unsupported_face);
875 }
876 }
877
878 if (enclen + chlen > screen_buf_end - screen_bp)
879 {
880 /* The allocated buffer for screen writes is too small.
881 Flush it and loop again without incrementing STR, so
882 that the next loop will begin with the same glyph. */
883 int nbytes = screen_bp - screen_buf;
884
885 mouse_off_maybe ();
886 dosmemput (screen_buf, nbytes, (int)ScreenPrimary + offset);
887 if (screen_virtual_segment)
888 dosv_refresh_virtual_screen (offset, nbytes / 2);
889 new_pos_X += nbytes / 2;
890 offset += nbytes;
891
892 /* Prepare to reuse the same buffer again. */
893 screen_bp = screen_buf;
894 }
895 else
896 {
897 /* There's enough place in the allocated buffer to add
898 the encoding of this glyph. */
899
900 /* First, copy the encoded bytes. */
901 for (bp = conversion_buffer; enclen--; bp++)
902 {
903 *screen_bp++ = (unsigned char)*bp;
904 *screen_bp++ = ScreenAttrib;
905 if (termscript)
906 fputc (*bp, termscript);
907 }
908
909 /* Now copy the bytes not consumed by the encoding. */
910 if (chlen > 0)
911 {
912 buf += coding->consumed;
913 while (chlen--)
914 {
915 if (termscript)
916 fputc (*buf, termscript);
917 *screen_bp++ = (unsigned char)*buf++;
918 *screen_bp++ = ScreenAttrib;
919 }
920 }
921
922 /* Update STR and its remaining length. */
923 str++;
924 sl--;
925 }
926 }
927 }
928
929 /* Dump whatever is left in the screen buffer. */
930 mouse_off_maybe ();
931 dosmemput (screen_buf, screen_bp - screen_buf, (int)ScreenPrimary + offset);
932 if (screen_virtual_segment)
933 dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
934 new_pos_X += (screen_bp - screen_buf) / 2;
935
936 /* We may have to output some codes to terminate the writing. */
937 if (CODING_REQUIRE_FLUSHING (coding))
938 {
939 coding->mode |= CODING_MODE_LAST_BLOCK;
940 encode_coding (coding, "", conversion_buffer, 0, conversion_buffer_size);
941 if (coding->produced > 0)
942 {
943 screen_buf = alloca (coding->produced * 2);
944 for (screen_bp = screen_buf, bp = conversion_buffer;
945 coding->produced--; bp++)
946 {
947 *screen_bp++ = (unsigned char)*bp;
948 *screen_bp++ = ScreenAttrib;
949 if (termscript)
950 fputc (*bp, termscript);
951 }
952 offset += screen_bp - screen_buf;
953 mouse_off_maybe ();
954 dosmemput (screen_buf, screen_bp - screen_buf,
955 (int)ScreenPrimary + offset);
956 if (screen_virtual_segment)
957 dosv_refresh_virtual_screen (offset, (screen_bp - screen_buf) / 2);
958 new_pos_X += (screen_bp - screen_buf) / 2;
959 }
960 }
961 }
962
963 /************************************************************************
964 Mouse Highlight (and friends..)
965 ************************************************************************/
966
967 /* This is used for debugging, to turn off note_mouse_highlight. */
968 int disable_mouse_highlight;
969
970 /* If a string, dos_rawgetc generates an event to display that string.
971 (The display is done in keyboard.c:read_char.) */
972 static Lisp_Object help_echo;
973 static Lisp_Object previous_help_echo; /* a helper temporary variable */
974
975 static int mouse_preempted = 0; /* non-zero when XMenu gobbles mouse events */
976
977 /* Set the mouse pointer shape according to whether it is in the
978 area where the mouse highlight is in effect. */
979 static void
980 IT_set_mouse_pointer (int mode)
981 {
982 /* A no-op for now. DOS text-mode mouse pointer doesn't offer too
983 many possibilities to change its shape, and the available
984 functionality pretty much sucks (e.g., almost every reasonable
985 shape will conceal the character it is on). Since the color of
986 the pointer changes in the highlighted area, it is not clear to
987 me whether anything else is required, anyway. */
988 }
989
990 /* Display the active region described by mouse_face_*
991 in its mouse-face if HL > 0, in its normal face if HL = 0. */
992 static void
993 show_mouse_face (struct display_info *dpyinfo, int hl)
994 {
995 struct window *w = XWINDOW (dpyinfo->mouse_face_window);
996 struct frame *f = XFRAME (WINDOW_FRAME (w));
997 int i;
998 struct face *fp;
999
1000
1001 /* If window is in the process of being destroyed, don't bother
1002 doing anything. */
1003 if (w->current_matrix == NULL)
1004 goto set_cursor_shape;
1005
1006 /* Recognize when we are called to operate on rows that don't exist
1007 anymore. This can happen when a window is split. */
1008 if (dpyinfo->mouse_face_end_row >= w->current_matrix->nrows)
1009 goto set_cursor_shape;
1010
1011 /* There's no sense to do anything if the mouse face isn't realized. */
1012 if (hl > 0)
1013 {
1014 fp = FACE_FROM_ID (SELECTED_FRAME(), dpyinfo->mouse_face_face_id);
1015 if (!fp)
1016 goto set_cursor_shape;
1017 }
1018
1019 /* Note that mouse_face_beg_row etc. are window relative. */
1020 for (i = dpyinfo->mouse_face_beg_row;
1021 i <= dpyinfo->mouse_face_end_row;
1022 i++)
1023 {
1024 int start_hpos, end_hpos;
1025 struct glyph_row *row = MATRIX_ROW (w->current_matrix, i);
1026
1027 /* Don't do anything if row doesn't have valid contents. */
1028 if (!row->enabled_p)
1029 continue;
1030
1031 /* For all but the first row, the highlight starts at column 0. */
1032 if (i == dpyinfo->mouse_face_beg_row)
1033 start_hpos = dpyinfo->mouse_face_beg_col;
1034 else
1035 start_hpos = 0;
1036
1037 if (i == dpyinfo->mouse_face_end_row)
1038 end_hpos = dpyinfo->mouse_face_end_col;
1039 else
1040 end_hpos = row->used[TEXT_AREA];
1041
1042 if (end_hpos <= start_hpos)
1043 continue;
1044 if (hl > 0)
1045 {
1046 int vpos = row->y + WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
1047 int kstart = start_hpos + WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w);
1048 int nglyphs = end_hpos - start_hpos;
1049 int offset = ScreenPrimary + 2*(vpos*screen_size_X + kstart) + 1;
1050 int start_offset = offset;
1051
1052 if (termscript)
1053 fprintf (termscript, "\n<MH+ %d-%d:%d>",
1054 kstart, kstart + nglyphs - 1, vpos);
1055
1056 mouse_off ();
1057 IT_set_face (dpyinfo->mouse_face_face_id);
1058 /* Since we are going to change only the _colors_ of the
1059 displayed text, there's no need to go through all the
1060 pain of generating and encoding the text from the glyphs.
1061 Instead, we simply poke the attribute byte of each
1062 affected position in video memory with the colors
1063 computed by IT_set_face! */
1064 _farsetsel (_dos_ds);
1065 while (nglyphs--)
1066 {
1067 _farnspokeb (offset, ScreenAttrib);
1068 offset += 2;
1069 }
1070 if (screen_virtual_segment)
1071 dosv_refresh_virtual_screen (start_offset, end_hpos - start_hpos);
1072 mouse_on ();
1073 }
1074 else
1075 {
1076 /* We are removing a previously-drawn mouse highlight. The
1077 safest way to do so is to redraw the glyphs anew, since
1078 all kinds of faces and display tables could have changed
1079 behind our back. */
1080 int nglyphs = end_hpos - start_hpos;
1081 int save_x = new_pos_X, save_y = new_pos_Y;
1082
1083 if (end_hpos >= row->used[TEXT_AREA])
1084 nglyphs = row->used[TEXT_AREA] - start_hpos;
1085
1086 /* IT_write_glyphs writes at cursor position, so we need to
1087 temporarily move cursor coordinates to the beginning of
1088 the highlight region. */
1089 new_pos_X = start_hpos + WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w);
1090 new_pos_Y = row->y + WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
1091
1092 if (termscript)
1093 fprintf (termscript, "<MH- %d-%d:%d>",
1094 new_pos_X, new_pos_X + nglyphs - 1, new_pos_Y);
1095 IT_write_glyphs (row->glyphs[TEXT_AREA] + start_hpos, nglyphs);
1096 if (termscript)
1097 fputs ("\n", termscript);
1098 new_pos_X = save_x;
1099 new_pos_Y = save_y;
1100 }
1101 }
1102
1103 set_cursor_shape:
1104
1105 /* Change the mouse pointer shape. */
1106 IT_set_mouse_pointer (hl);
1107 }
1108
1109 /* Clear out the mouse-highlighted active region.
1110 Redraw it un-highlighted first. */
1111 static void
1112 clear_mouse_face (struct display_info *dpyinfo)
1113 {
1114 if (! NILP (dpyinfo->mouse_face_window))
1115 show_mouse_face (dpyinfo, 0);
1116
1117 dpyinfo->mouse_face_beg_row = dpyinfo->mouse_face_beg_col = -1;
1118 dpyinfo->mouse_face_end_row = dpyinfo->mouse_face_end_col = -1;
1119 dpyinfo->mouse_face_window = Qnil;
1120 }
1121
1122 /* Find the glyph matrix position of buffer position POS in window W.
1123 *HPOS and *VPOS are set to the positions found. W's current glyphs
1124 must be up to date. If POS is above window start return (0, 0).
1125 If POS is after end of W, return end of last line in W. */
1126 static int
1127 fast_find_position (struct window *w, int pos, int *hpos, int *vpos)
1128 {
1129 int i;
1130 int lastcol;
1131 int maybe_next_line_p = 0;
1132 int line_start_position;
1133 int yb = window_text_bottom_y (w);
1134 struct glyph_row *row = MATRIX_ROW (w->current_matrix, 0);
1135 struct glyph_row *best_row = row;
1136
1137 while (row->y < yb)
1138 {
1139 if (row->used[TEXT_AREA])
1140 line_start_position = row->glyphs[TEXT_AREA]->charpos;
1141 else
1142 line_start_position = 0;
1143
1144 if (line_start_position > pos)
1145 break;
1146 /* If the position sought is the end of the buffer,
1147 don't include the blank lines at the bottom of the window. */
1148 else if (line_start_position == pos
1149 && pos == BUF_ZV (XBUFFER (w->buffer)))
1150 {
1151 maybe_next_line_p = 1;
1152 break;
1153 }
1154 else if (line_start_position > 0)
1155 best_row = row;
1156
1157 ++row;
1158 }
1159
1160 /* Find the right column within BEST_ROW. */
1161 lastcol = 0;
1162 row = best_row;
1163 for (i = 0; i < row->used[TEXT_AREA]; i++)
1164 {
1165 struct glyph *glyph = row->glyphs[TEXT_AREA] + i;
1166 int charpos;
1167
1168 charpos = glyph->charpos;
1169 if (charpos == pos)
1170 {
1171 *hpos = i;
1172 *vpos = row->y;
1173 return 1;
1174 }
1175 else if (charpos > pos)
1176 break;
1177 else if (charpos > 0)
1178 lastcol = i;
1179 }
1180
1181 /* If we're looking for the end of the buffer,
1182 and we didn't find it in the line we scanned,
1183 use the start of the following line. */
1184 if (maybe_next_line_p)
1185 {
1186 ++row;
1187 lastcol = 0;
1188 }
1189
1190 *vpos = row->y;
1191 *hpos = lastcol + 1;
1192 return 0;
1193 }
1194
1195 /* Take proper action when mouse has moved to the mode or top line of
1196 window W, x-position X. MODE_LINE_P non-zero means mouse is on the
1197 mode line. X is relative to the start of the text display area of
1198 W, so the width of bitmap areas and scroll bars must be subtracted
1199 to get a position relative to the start of the mode line. */
1200 static void
1201 IT_note_mode_line_highlight (struct window *w, int x, int mode_line_p)
1202 {
1203 struct frame *f = XFRAME (w->frame);
1204 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1205 struct glyph_row *row;
1206
1207 if (mode_line_p)
1208 row = MATRIX_MODE_LINE_ROW (w->current_matrix);
1209 else
1210 row = MATRIX_HEADER_LINE_ROW (w->current_matrix);
1211
1212 if (row->enabled_p)
1213 {
1214 extern Lisp_Object Qhelp_echo;
1215 struct glyph *glyph, *end;
1216 Lisp_Object help, map;
1217
1218 /* Find the glyph under X. */
1219 glyph = row->glyphs[TEXT_AREA]
1220 + x - FRAME_LEFT_SCROLL_BAR_WIDTH (f) * CANON_X_UNIT (f);
1221 end = glyph + row->used[TEXT_AREA];
1222 if (glyph < end
1223 && STRINGP (glyph->object)
1224 && XSTRING (glyph->object)->intervals
1225 && glyph->charpos >= 0
1226 && glyph->charpos < XSTRING (glyph->object)->size)
1227 {
1228 /* If we're on a string with `help-echo' text property,
1229 arrange for the help to be displayed. This is done by
1230 setting the global variable help_echo to the help string. */
1231 help = Fget_text_property (make_number (glyph->charpos),
1232 Qhelp_echo, glyph->object);
1233 if (STRINGP (help))
1234 help_echo = help;
1235 }
1236 }
1237 }
1238
1239 /* Take proper action when the mouse has moved to position X, Y on
1240 frame F as regards highlighting characters that have mouse-face
1241 properties. Also de-highlighting chars where the mouse was before.
1242 X and Y can be negative or out of range. */
1243 static void
1244 IT_note_mouse_highlight (struct frame *f, int x, int y)
1245 {
1246 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1247 int portion;
1248 Lisp_Object window;
1249 struct window *w;
1250
1251 /* When a menu is active, don't highlight because this looks odd. */
1252 if (mouse_preempted)
1253 return;
1254
1255 if (disable_mouse_highlight)
1256 return;
1257
1258 dpyinfo->mouse_face_mouse_x = x;
1259 dpyinfo->mouse_face_mouse_y = y;
1260 dpyinfo->mouse_face_mouse_frame = f;
1261
1262 if (dpyinfo->mouse_face_defer)
1263 return;
1264
1265 if (gc_in_progress)
1266 {
1267 dpyinfo->mouse_face_deferred_gc = 1;
1268 return;
1269 }
1270
1271 /* Which window is that in? */
1272 window = window_from_coordinates (f, x, y, &portion, 0);
1273
1274 /* If we were displaying active text in another window, clear that. */
1275 if (! EQ (window, dpyinfo->mouse_face_window))
1276 clear_mouse_face (dpyinfo);
1277
1278 /* Not on a window -> return. */
1279 if (!WINDOWP (window))
1280 return;
1281
1282 /* Convert to window-relative coordinates. */
1283 w = XWINDOW (window);
1284 x -= WINDOW_DISPLAY_LEFT_EDGE_PIXEL_X (w);
1285 y -= WINDOW_DISPLAY_TOP_EDGE_PIXEL_Y (w);
1286
1287 if (portion == 1 || portion == 3)
1288 {
1289 /* Mouse is on the mode or top line. */
1290 IT_note_mode_line_highlight (w, x, portion == 1);
1291 return;
1292 }
1293 else
1294 IT_set_mouse_pointer (0);
1295
1296 /* Are we in a window whose display is up to date?
1297 And verify the buffer's text has not changed. */
1298 if (/* Within text portion of the window. */
1299 portion == 0
1300 && EQ (w->window_end_valid, w->buffer)
1301 && XFASTINT (w->last_modified) == BUF_MODIFF (XBUFFER (w->buffer))
1302 && (XFASTINT (w->last_overlay_modified)
1303 == BUF_OVERLAY_MODIFF (XBUFFER (w->buffer))))
1304 {
1305 int pos, i, area;
1306 struct glyph_row *row;
1307 struct glyph *glyph;
1308
1309 /* Find the glyph under X/Y. */
1310 glyph = NULL;
1311 if (y < w->current_matrix->nrows)
1312 {
1313 row = MATRIX_ROW (w->current_matrix, y);
1314 if (row->enabled_p
1315 && row->displays_text_p
1316 && x < window_box_width (w, TEXT_AREA))
1317 {
1318 glyph = row->glyphs[TEXT_AREA];
1319 if (x >= row->used[TEXT_AREA])
1320 glyph = NULL;
1321 else
1322 {
1323 glyph += x;
1324 if (!BUFFERP (glyph->object))
1325 glyph = NULL;
1326 }
1327 }
1328 }
1329
1330 /* Clear mouse face if X/Y not over text. */
1331 if (glyph == NULL)
1332 {
1333 clear_mouse_face (dpyinfo);
1334 return;
1335 }
1336
1337 if (!BUFFERP (glyph->object))
1338 abort ();
1339 pos = glyph->charpos;
1340
1341 /* Check for mouse-face and help-echo. */
1342 {
1343 extern Lisp_Object Qmouse_face;
1344 Lisp_Object mouse_face, overlay, position;
1345 Lisp_Object *overlay_vec;
1346 int len, noverlays;
1347 struct buffer *obuf;
1348 int obegv, ozv;
1349
1350 /* If we get an out-of-range value, return now; avoid an error. */
1351 if (pos > BUF_Z (XBUFFER (w->buffer)))
1352 return;
1353
1354 /* Make the window's buffer temporarily current for
1355 overlays_at and compute_char_face. */
1356 obuf = current_buffer;
1357 current_buffer = XBUFFER (w->buffer);
1358 obegv = BEGV;
1359 ozv = ZV;
1360 BEGV = BEG;
1361 ZV = Z;
1362
1363 /* Is this char mouse-active or does it have help-echo? */
1364 XSETINT (position, pos);
1365
1366 /* Put all the overlays we want in a vector in overlay_vec.
1367 Store the length in len. If there are more than 10, make
1368 enough space for all, and try again. */
1369 len = 10;
1370 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1371 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
1372 if (noverlays > len)
1373 {
1374 len = noverlays;
1375 overlay_vec = (Lisp_Object *) alloca (len * sizeof (Lisp_Object));
1376 noverlays = overlays_at (pos, 0, &overlay_vec, &len, NULL, NULL);
1377 }
1378
1379 noverlays = sort_overlays (overlay_vec, noverlays, w);
1380
1381 /* Check mouse-face highlighting. */
1382 if (! (EQ (window, dpyinfo->mouse_face_window)
1383 && y >= dpyinfo->mouse_face_beg_row
1384 && y <= dpyinfo->mouse_face_end_row
1385 && (y > dpyinfo->mouse_face_beg_row
1386 || x >= dpyinfo->mouse_face_beg_col)
1387 && (y < dpyinfo->mouse_face_end_row
1388 || x < dpyinfo->mouse_face_end_col
1389 || dpyinfo->mouse_face_past_end)))
1390 {
1391 /* Clear the display of the old active region, if any. */
1392 clear_mouse_face (dpyinfo);
1393
1394 /* Find highest priority overlay that has a mouse-face prop. */
1395 overlay = Qnil;
1396 for (i = 0; i < noverlays; i++)
1397 {
1398 mouse_face = Foverlay_get (overlay_vec[i], Qmouse_face);
1399 if (!NILP (mouse_face))
1400 {
1401 overlay = overlay_vec[i];
1402 break;
1403 }
1404 }
1405
1406 /* If no overlay applies, get a text property. */
1407 if (NILP (overlay))
1408 mouse_face = Fget_text_property (position, Qmouse_face,
1409 w->buffer);
1410
1411 /* Handle the overlay case. */
1412 if (! NILP (overlay))
1413 {
1414 /* Find the range of text around this char that
1415 should be active. */
1416 Lisp_Object before, after;
1417 int ignore;
1418
1419 before = Foverlay_start (overlay);
1420 after = Foverlay_end (overlay);
1421 /* Record this as the current active region. */
1422 fast_find_position (w, XFASTINT (before),
1423 &dpyinfo->mouse_face_beg_col,
1424 &dpyinfo->mouse_face_beg_row);
1425 dpyinfo->mouse_face_past_end
1426 = !fast_find_position (w, XFASTINT (after),
1427 &dpyinfo->mouse_face_end_col,
1428 &dpyinfo->mouse_face_end_row);
1429 dpyinfo->mouse_face_window = window;
1430 dpyinfo->mouse_face_face_id
1431 = face_at_buffer_position (w, pos, 0, 0,
1432 &ignore, pos + 1, 1);
1433
1434 /* Display it as active. */
1435 show_mouse_face (dpyinfo, 1);
1436 }
1437 /* Handle the text property case. */
1438 else if (! NILP (mouse_face))
1439 {
1440 /* Find the range of text around this char that
1441 should be active. */
1442 Lisp_Object before, after, beginning, end;
1443 int ignore;
1444
1445 beginning = Fmarker_position (w->start);
1446 XSETINT (end, (BUF_Z (XBUFFER (w->buffer))
1447 - XFASTINT (w->window_end_pos)));
1448 before
1449 = Fprevious_single_property_change (make_number (pos + 1),
1450 Qmouse_face,
1451 w->buffer, beginning);
1452 after
1453 = Fnext_single_property_change (position, Qmouse_face,
1454 w->buffer, end);
1455 /* Record this as the current active region. */
1456 fast_find_position (w, XFASTINT (before),
1457 &dpyinfo->mouse_face_beg_col,
1458 &dpyinfo->mouse_face_beg_row);
1459 dpyinfo->mouse_face_past_end
1460 = !fast_find_position (w, XFASTINT (after),
1461 &dpyinfo->mouse_face_end_col,
1462 &dpyinfo->mouse_face_end_row);
1463 dpyinfo->mouse_face_window = window;
1464 dpyinfo->mouse_face_face_id
1465 = face_at_buffer_position (w, pos, 0, 0,
1466 &ignore, pos + 1, 1);
1467
1468 /* Display it as active. */
1469 show_mouse_face (dpyinfo, 1);
1470 }
1471 }
1472
1473 /* Look for a `help-echo' property. */
1474 {
1475 Lisp_Object help;
1476 extern Lisp_Object Qhelp_echo;
1477
1478 /* Check overlays first. */
1479 help = Qnil;
1480 for (i = 0; i < noverlays && !STRINGP (help); ++i)
1481 help = Foverlay_get (overlay_vec[i], Qhelp_echo);
1482
1483 /* Try text properties. */
1484 if (!STRINGP (help)
1485 && ((STRINGP (glyph->object)
1486 && glyph->charpos >= 0
1487 && glyph->charpos < XSTRING (glyph->object)->size)
1488 || (BUFFERP (glyph->object)
1489 && glyph->charpos >= BEGV
1490 && glyph->charpos < ZV)))
1491 help = Fget_text_property (make_number (glyph->charpos),
1492 Qhelp_echo, glyph->object);
1493
1494 if (STRINGP (help))
1495 help_echo = help;
1496 }
1497
1498 BEGV = obegv;
1499 ZV = ozv;
1500 current_buffer = obuf;
1501 }
1502 }
1503 }
1504
1505 static void
1506 IT_clear_end_of_line (int first_unused)
1507 {
1508 char *spaces, *sp;
1509 int i, j;
1510 int offset = 2 * (new_pos_X + screen_size_X * new_pos_Y);
1511 extern int fatal_error_in_progress;
1512
1513 if (new_pos_X >= first_unused || fatal_error_in_progress)
1514 return;
1515
1516 IT_set_face (0);
1517 i = (j = first_unused - new_pos_X) * 2;
1518 if (termscript)
1519 fprintf (termscript, "<CLR:EOL[%d..%d)>", new_pos_X, first_unused);
1520 spaces = sp = alloca (i);
1521
1522 while (--j >= 0)
1523 {
1524 *sp++ = ' ';
1525 *sp++ = ScreenAttrib;
1526 }
1527
1528 mouse_off_maybe ();
1529 dosmemput (spaces, i, (int)ScreenPrimary + offset);
1530 if (screen_virtual_segment)
1531 dosv_refresh_virtual_screen (offset, i / 2);
1532
1533 /* clear_end_of_line_raw on term.c leaves the cursor at first_unused.
1534 Let's follow their lead, in case someone relies on this. */
1535 new_pos_X = first_unused;
1536 }
1537
1538 static void
1539 IT_clear_screen (void)
1540 {
1541 if (termscript)
1542 fprintf (termscript, "<CLR:SCR>");
1543 IT_set_face (0);
1544 mouse_off ();
1545 ScreenClear ();
1546 if (screen_virtual_segment)
1547 dosv_refresh_virtual_screen (0, screen_size);
1548 new_pos_X = new_pos_Y = 0;
1549 }
1550
1551 static void
1552 IT_clear_to_end (void)
1553 {
1554 if (termscript)
1555 fprintf (termscript, "<CLR:EOS>");
1556
1557 while (new_pos_Y < screen_size_Y) {
1558 new_pos_X = 0;
1559 IT_clear_end_of_line (screen_size_X);
1560 new_pos_Y++;
1561 }
1562 }
1563
1564 static void
1565 IT_cursor_to (int y, int x)
1566 {
1567 if (termscript)
1568 fprintf (termscript, "\n<XY=%dx%d>", x, y);
1569 new_pos_X = x;
1570 new_pos_Y = y;
1571 }
1572
1573 static int cursor_cleared;
1574
1575 static void
1576 IT_display_cursor (int on)
1577 {
1578 if (on && cursor_cleared)
1579 {
1580 ScreenSetCursor (current_pos_Y, current_pos_X);
1581 cursor_cleared = 0;
1582 }
1583 else if (!on && !cursor_cleared)
1584 {
1585 ScreenSetCursor (-1, -1);
1586 cursor_cleared = 1;
1587 }
1588 }
1589
1590 /* Emacs calls cursor-movement functions a lot when it updates the
1591 display (probably a legacy of old terminals where you cannot
1592 update a screen line without first moving the cursor there).
1593 However, cursor movement is expensive on MSDOS (it calls a slow
1594 BIOS function and requires 2 mode switches), while actual screen
1595 updates access the video memory directly and don't depend on
1596 cursor position. To avoid slowing down the redisplay, we cheat:
1597 all functions that move the cursor only set internal variables
1598 which record the cursor position, whereas the cursor is only
1599 moved to its final position whenever screen update is complete.
1600
1601 `IT_cmgoto' is called from the keyboard reading loop and when the
1602 frame update is complete. This means that we are ready for user
1603 input, so we update the cursor position to show where the point is,
1604 and also make the mouse pointer visible.
1605
1606 Special treatment is required when the cursor is in the echo area,
1607 to put the cursor at the end of the text displayed there. */
1608
1609 static void
1610 IT_cmgoto (FRAME_PTR f)
1611 {
1612 /* Only set the cursor to where it should be if the display is
1613 already in sync with the window contents. */
1614 int update_cursor_pos = 1; /* MODIFF == unchanged_modified; */
1615
1616 /* FIXME: This needs to be rewritten for the new redisplay, or
1617 removed. */
1618 #if 0
1619 static int previous_pos_X = -1;
1620
1621 update_cursor_pos = 1; /* temporary!!! */
1622
1623 /* If the display is in sync, forget any previous knowledge about
1624 cursor position. This is primarily for unexpected events like
1625 C-g in the minibuffer. */
1626 if (update_cursor_pos && previous_pos_X >= 0)
1627 previous_pos_X = -1;
1628 /* If we are in the echo area, put the cursor at the
1629 end of the echo area message. */
1630 if (!update_cursor_pos
1631 && XFASTINT (XWINDOW (FRAME_MINIBUF_WINDOW (f))->top) <= new_pos_Y)
1632 {
1633 int tem_X = current_pos_X, dummy;
1634
1635 if (echo_area_glyphs)
1636 {
1637 tem_X = echo_area_glyphs_length;
1638 /* Save current cursor position, to be restored after the
1639 echo area message is erased. Only remember one level
1640 of previous cursor position. */
1641 if (previous_pos_X == -1)
1642 ScreenGetCursor (&dummy, &previous_pos_X);
1643 }
1644 else if (previous_pos_X >= 0)
1645 {
1646 /* We wind up here after the echo area message is erased.
1647 Restore the cursor position we remembered above. */
1648 tem_X = previous_pos_X;
1649 previous_pos_X = -1;
1650 }
1651
1652 if (current_pos_X != tem_X)
1653 {
1654 new_pos_X = tem_X;
1655 update_cursor_pos = 1;
1656 }
1657 }
1658 #endif
1659
1660 if (update_cursor_pos
1661 && (current_pos_X != new_pos_X || current_pos_Y != new_pos_Y))
1662 {
1663 ScreenSetCursor (current_pos_Y = new_pos_Y, current_pos_X = new_pos_X);
1664 if (termscript)
1665 fprintf (termscript, "\n<CURSOR:%dx%d>", current_pos_X, current_pos_Y);
1666 }
1667
1668 /* Maybe cursor is invisible, so make it visible. */
1669 IT_display_cursor (1);
1670
1671 /* Mouse pointer should be always visible if we are waiting for
1672 keyboard input. */
1673 if (!mouse_visible)
1674 mouse_on ();
1675 }
1676
1677 static void
1678 IT_reassert_line_highlight (int new, int vpos)
1679 {
1680 highlight = new;
1681 }
1682
1683 static void
1684 IT_change_line_highlight (int new_highlight, int y, int vpos, int first_unused_hpos)
1685 {
1686 highlight = new_highlight;
1687 IT_cursor_to (vpos, 0);
1688 IT_clear_end_of_line (first_unused_hpos);
1689 }
1690
1691 static void
1692 IT_update_begin (struct frame *f)
1693 {
1694 struct display_info *display_info = FRAME_X_DISPLAY_INFO (f);
1695
1696 highlight = 0;
1697
1698 BLOCK_INPUT;
1699
1700 if (f == display_info->mouse_face_mouse_frame)
1701 {
1702 /* Don't do highlighting for mouse motion during the update. */
1703 display_info->mouse_face_defer = 1;
1704
1705 /* If F needs to be redrawn, simply forget about any prior mouse
1706 highlighting. */
1707 if (FRAME_GARBAGED_P (f))
1708 display_info->mouse_face_window = Qnil;
1709
1710 /* Can we tell that this update does not affect the window
1711 where the mouse highlight is? If so, no need to turn off.
1712 Likewise, don't do anything if the frame is garbaged;
1713 in that case, the frame's current matrix that we would use
1714 is all wrong, and we will redisplay that line anyway. */
1715 if (!NILP (display_info->mouse_face_window)
1716 && WINDOWP (display_info->mouse_face_window))
1717 {
1718 struct window *w = XWINDOW (display_info->mouse_face_window);
1719 int i;
1720
1721 /* If the mouse highlight is in the window that was deleted
1722 (e.g., if it was popped by completion), clear highlight
1723 unconditionally. */
1724 if (NILP (w->buffer))
1725 display_info->mouse_face_window = Qnil;
1726 else
1727 {
1728 for (i = 0; i < w->desired_matrix->nrows; ++i)
1729 if (MATRIX_ROW_ENABLED_P (w->desired_matrix, i))
1730 break;
1731 }
1732
1733 if (NILP (w->buffer) || i < w->desired_matrix->nrows)
1734 clear_mouse_face (display_info);
1735 }
1736 }
1737 else if (!FRAME_LIVE_P (display_info->mouse_face_mouse_frame))
1738 {
1739 /* If the frame with mouse highlight was deleted, invalidate the
1740 highlight info. */
1741 display_info->mouse_face_beg_row = display_info->mouse_face_beg_col = -1;
1742 display_info->mouse_face_end_row = display_info->mouse_face_end_col = -1;
1743 display_info->mouse_face_window = Qnil;
1744 display_info->mouse_face_deferred_gc = 0;
1745 display_info->mouse_face_mouse_frame = NULL;
1746 }
1747
1748 UNBLOCK_INPUT;
1749 }
1750
1751 static void
1752 IT_update_end (struct frame *f)
1753 {
1754 highlight = 0;
1755 FRAME_X_DISPLAY_INFO (f)->mouse_face_defer = 0;
1756 }
1757
1758 static void
1759 IT_frame_up_to_date (struct frame *f)
1760 {
1761 struct display_info *dpyinfo = FRAME_X_DISPLAY_INFO (f);
1762
1763 if (dpyinfo->mouse_face_deferred_gc
1764 || f == dpyinfo->mouse_face_mouse_frame)
1765 {
1766 BLOCK_INPUT;
1767 if (dpyinfo->mouse_face_mouse_frame)
1768 IT_note_mouse_highlight (dpyinfo->mouse_face_mouse_frame,
1769 dpyinfo->mouse_face_mouse_x,
1770 dpyinfo->mouse_face_mouse_y);
1771 dpyinfo->mouse_face_deferred_gc = 0;
1772 UNBLOCK_INPUT;
1773 }
1774
1775 IT_cmgoto (f); /* position cursor when update is done */
1776 }
1777
1778 /* Copy LEN glyphs displayed on a single line whose vertical position
1779 is YPOS, beginning at horizontal position XFROM to horizontal
1780 position XTO, by moving blocks in the video memory. Used by
1781 functions that insert and delete glyphs. */
1782 static void
1783 IT_copy_glyphs (int xfrom, int xto, size_t len, int ypos)
1784 {
1785 /* The offsets of source and destination relative to the
1786 conventional memorty selector. */
1787 int from = 2 * (xfrom + screen_size_X * ypos) + ScreenPrimary;
1788 int to = 2 * (xto + screen_size_X * ypos) + ScreenPrimary;
1789
1790 if (from == to || len <= 0)
1791 return;
1792
1793 _farsetsel (_dos_ds);
1794
1795 /* The source and destination might overlap, so we need to move
1796 glyphs non-destructively. */
1797 if (from > to)
1798 {
1799 for ( ; len; from += 2, to += 2, len--)
1800 _farnspokew (to, _farnspeekw (from));
1801 }
1802 else
1803 {
1804 from += (len - 1) * 2;
1805 to += (len - 1) * 2;
1806 for ( ; len; from -= 2, to -= 2, len--)
1807 _farnspokew (to, _farnspeekw (from));
1808 }
1809 if (screen_virtual_segment)
1810 dosv_refresh_virtual_screen (ypos * screen_size_X * 2, screen_size_X);
1811 }
1812
1813 /* Insert and delete glyphs. */
1814 static void
1815 IT_insert_glyphs (start, len)
1816 register struct glyph *start;
1817 register int len;
1818 {
1819 int shift_by_width = screen_size_X - (new_pos_X + len);
1820
1821 /* Shift right the glyphs from the nominal cursor position to the
1822 end of this line. */
1823 IT_copy_glyphs (new_pos_X, new_pos_X + len, shift_by_width, new_pos_Y);
1824
1825 /* Now write the glyphs to be inserted. */
1826 IT_write_glyphs (start, len);
1827 }
1828
1829 static void
1830 IT_delete_glyphs (n)
1831 register int n;
1832 {
1833 abort ();
1834 }
1835
1836 /* set-window-configuration on window.c needs this. */
1837 void
1838 x_set_menu_bar_lines (f, value, oldval)
1839 struct frame *f;
1840 Lisp_Object value, oldval;
1841 {
1842 set_menu_bar_lines (f, value, oldval);
1843 }
1844
1845 /* This was copied from xfns.c */
1846
1847 Lisp_Object Qbackground_color;
1848 Lisp_Object Qforeground_color;
1849 extern Lisp_Object Qtitle;
1850
1851 /* IT_set_terminal_modes is called when emacs is started,
1852 resumed, and whenever the screen is redrawn! */
1853
1854 static void
1855 IT_set_terminal_modes (void)
1856 {
1857 if (termscript)
1858 fprintf (termscript, "\n<SET_TERM>");
1859 highlight = 0;
1860
1861 screen_size_X = ScreenCols ();
1862 screen_size_Y = ScreenRows ();
1863 screen_size = screen_size_X * screen_size_Y;
1864
1865 new_pos_X = new_pos_Y = 0;
1866 current_pos_X = current_pos_Y = -1;
1867
1868 if (term_setup_done)
1869 return;
1870 term_setup_done = 1;
1871
1872 startup_screen_size_X = screen_size_X;
1873 startup_screen_size_Y = screen_size_Y;
1874 startup_screen_attrib = ScreenAttrib;
1875
1876 #if __DJGPP__ > 1
1877 /* Is DOS/V (or any other RSIS software which relocates
1878 the screen) installed? */
1879 {
1880 unsigned short es_value;
1881 __dpmi_regs regs;
1882
1883 regs.h.ah = 0xfe; /* get relocated screen address */
1884 if (ScreenPrimary == 0xb0000UL || ScreenPrimary == 0xb8000UL)
1885 regs.x.es = (ScreenPrimary >> 4) & 0xffff;
1886 else if (screen_old_address) /* already switched to Japanese mode once */
1887 regs.x.es = (screen_old_address >> 4) & 0xffff;
1888 else
1889 regs.x.es = ScreenMode () == 7 ? 0xb000 : 0xb800;
1890 regs.x.di = 0;
1891 es_value = regs.x.es;
1892 __dpmi_int (0x10, &regs);
1893
1894 if (regs.x.es != es_value)
1895 {
1896 /* screen_old_address is only set if ScreenPrimary does NOT
1897 already point to the relocated buffer address returned by
1898 the Int 10h/AX=FEh call above. DJGPP v2.02 and later sets
1899 ScreenPrimary to that address at startup under DOS/V. */
1900 if (regs.x.es != (ScreenPrimary >> 4) & 0xffff)
1901 screen_old_address = ScreenPrimary;
1902 screen_virtual_segment = regs.x.es;
1903 screen_virtual_offset = regs.x.di;
1904 ScreenPrimary = (screen_virtual_segment << 4) + screen_virtual_offset;
1905 }
1906 }
1907 #endif /* __DJGPP__ > 1 */
1908
1909 ScreenGetCursor (&startup_pos_Y, &startup_pos_X);
1910 ScreenRetrieve (startup_screen_buffer = xmalloc (screen_size * 2));
1911
1912 if (termscript)
1913 fprintf (termscript, "<SCREEN SAVED (dimensions=%dx%d)>\n",
1914 screen_size_X, screen_size_Y);
1915
1916 bright_bg ();
1917 }
1918
1919 /* IT_reset_terminal_modes is called when emacs is
1920 suspended or killed. */
1921
1922 static void
1923 IT_reset_terminal_modes (void)
1924 {
1925 int display_row_start = (int) ScreenPrimary;
1926 int saved_row_len = startup_screen_size_X * 2;
1927 int update_row_len = ScreenCols () * 2;
1928 int current_rows = ScreenRows ();
1929 int to_next_row = update_row_len;
1930 unsigned char *saved_row = startup_screen_buffer;
1931 int cursor_pos_X = ScreenCols () - 1;
1932 int cursor_pos_Y = ScreenRows () - 1;
1933
1934 if (termscript)
1935 fprintf (termscript, "\n<RESET_TERM>");
1936
1937 highlight = 0;
1938
1939 if (!term_setup_done)
1940 return;
1941
1942 mouse_off ();
1943
1944 /* Leave the video system in the same state as we found it,
1945 as far as the blink/bright-background bit is concerned. */
1946 maybe_enable_blinking ();
1947
1948 /* We have a situation here.
1949 We cannot just do ScreenUpdate(startup_screen_buffer) because
1950 the luser could have changed screen dimensions inside Emacs
1951 and failed (or didn't want) to restore them before killing
1952 Emacs. ScreenUpdate() uses the *current* screen dimensions and
1953 thus will happily use memory outside what was allocated for
1954 `startup_screen_buffer'.
1955 Thus we only restore as much as the current screen dimensions
1956 can hold, and clear the rest (if the saved screen is smaller than
1957 the current) with the color attribute saved at startup. The cursor
1958 is also restored within the visible dimensions. */
1959
1960 ScreenAttrib = startup_screen_attrib;
1961
1962 /* Don't restore the screen if we are exiting less than 2 seconds
1963 after startup: we might be crashing, and the screen might show
1964 some vital clues to what's wrong. */
1965 if (clock () - startup_time >= 2*CLOCKS_PER_SEC)
1966 {
1967 ScreenClear ();
1968 if (screen_virtual_segment)
1969 dosv_refresh_virtual_screen (0, screen_size);
1970
1971 if (update_row_len > saved_row_len)
1972 update_row_len = saved_row_len;
1973 if (current_rows > startup_screen_size_Y)
1974 current_rows = startup_screen_size_Y;
1975
1976 if (termscript)
1977 fprintf (termscript, "<SCREEN RESTORED (dimensions=%dx%d)>\n",
1978 update_row_len / 2, current_rows);
1979
1980 while (current_rows--)
1981 {
1982 dosmemput (saved_row, update_row_len, display_row_start);
1983 if (screen_virtual_segment)
1984 dosv_refresh_virtual_screen (display_row_start - ScreenPrimary,
1985 update_row_len / 2);
1986 saved_row += saved_row_len;
1987 display_row_start += to_next_row;
1988 }
1989 }
1990 if (startup_pos_X < cursor_pos_X)
1991 cursor_pos_X = startup_pos_X;
1992 if (startup_pos_Y < cursor_pos_Y)
1993 cursor_pos_Y = startup_pos_Y;
1994
1995 ScreenSetCursor (cursor_pos_Y, cursor_pos_X);
1996 xfree (startup_screen_buffer);
1997
1998 term_setup_done = 0;
1999 }
2000
2001 static void
2002 IT_set_terminal_window (int foo)
2003 {
2004 }
2005
2006 /* Remember the screen colors of the curent frame, to serve as the
2007 default colors for newly-created frames. */
2008
2009 static int initial_screen_colors[2];
2010
2011 DEFUN ("msdos-remember-default-colors", Fmsdos_remember_default_colors,
2012 Smsdos_remember_default_colors, 1, 1, 0,
2013 "Remember the screen colors of the current frame.")
2014 (frame)
2015 Lisp_Object frame;
2016 {
2017 int reverse;
2018 struct frame *f;
2019
2020 CHECK_FRAME (frame, 0);
2021 f= XFRAME (frame);
2022 reverse = EQ (Fcdr (Fassq (intern ("reverse"), f->param_alist)), Qt);
2023
2024 initial_screen_colors[0]
2025 = reverse ? FRAME_BACKGROUND_PIXEL (f) : FRAME_FOREGROUND_PIXEL (f);
2026 initial_screen_colors[1]
2027 = reverse ? FRAME_FOREGROUND_PIXEL (f) : FRAME_BACKGROUND_PIXEL (f);
2028 }
2029
2030 void
2031 IT_set_frame_parameters (f, alist)
2032 struct frame *f;
2033 Lisp_Object alist;
2034 {
2035 Lisp_Object tail;
2036 int length = XINT (Flength (alist));
2037 int i, j;
2038 Lisp_Object *parms
2039 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2040 Lisp_Object *values
2041 = (Lisp_Object *) alloca (length * sizeof (Lisp_Object));
2042 Lisp_Object qreverse = intern ("reverse");
2043 /* Do we have to reverse the foreground and background colors? */
2044 int reverse = EQ (Fcdr (Fassq (qreverse, f->param_alist)), Qt);
2045 int was_reverse = reverse;
2046 int redraw = 0, fg_set = 0, bg_set = 0;
2047 unsigned long orig_fg;
2048 unsigned long orig_bg;
2049
2050 /* If we are creating a new frame, begin with the original screen colors
2051 used for the initial frame. */
2052 if (alist == Vdefault_frame_alist
2053 && initial_screen_colors[0] != -1 && initial_screen_colors[1] != -1)
2054 {
2055 FRAME_FOREGROUND_PIXEL (f) = initial_screen_colors[0];
2056 FRAME_BACKGROUND_PIXEL (f) = initial_screen_colors[1];
2057 }
2058 orig_fg = FRAME_FOREGROUND_PIXEL (f);
2059 orig_bg = FRAME_BACKGROUND_PIXEL (f);
2060
2061 /* Extract parm names and values into those vectors. */
2062 i = 0;
2063 for (tail = alist; CONSP (tail); tail = Fcdr (tail))
2064 {
2065 Lisp_Object elt;
2066
2067 elt = Fcar (tail);
2068 parms[i] = Fcar (elt);
2069 CHECK_SYMBOL (parms[i], 1);
2070 values[i] = Fcdr (elt);
2071 i++;
2072 }
2073
2074 j = i;
2075
2076 for (i = 0; i < j; i++)
2077 {
2078 Lisp_Object prop = parms[i];
2079 Lisp_Object val = values[i];
2080
2081 if (EQ (prop, qreverse))
2082 reverse = EQ (val, Qt);
2083 }
2084
2085 if (termscript && reverse && !was_reverse)
2086 fprintf (termscript, "<INVERSE-VIDEO>\n");
2087
2088 /* Now process the alist elements in reverse of specified order. */
2089 for (i--; i >= 0; i--)
2090 {
2091 Lisp_Object prop = parms[i];
2092 Lisp_Object val = values[i];
2093
2094 if (EQ (prop, Qforeground_color))
2095 {
2096 unsigned long new_color = load_color (f, NULL, val, reverse
2097 ? LFACE_BACKGROUND_INDEX
2098 : LFACE_FOREGROUND_INDEX);
2099 if (new_color != FACE_TTY_DEFAULT_COLOR
2100 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2101 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
2102 {
2103 if (reverse)
2104 /* FIXME: should the fore-/background of the default
2105 face change here as well? */
2106 FRAME_BACKGROUND_PIXEL (f) = new_color;
2107 else
2108 FRAME_FOREGROUND_PIXEL (f) = new_color;
2109 redraw = 1;
2110 fg_set = 1;
2111 if (termscript)
2112 fprintf (termscript, "<FGCOLOR %lu>\n", new_color);
2113 }
2114 }
2115 else if (EQ (prop, Qbackground_color))
2116 {
2117 unsigned long new_color = load_color (f, NULL, val, reverse
2118 ? LFACE_FOREGROUND_INDEX
2119 : LFACE_BACKGROUND_INDEX);
2120 if (new_color != FACE_TTY_DEFAULT_COLOR
2121 && new_color != FACE_TTY_DEFAULT_FG_COLOR
2122 && new_color != FACE_TTY_DEFAULT_BG_COLOR)
2123 {
2124 if (reverse)
2125 FRAME_FOREGROUND_PIXEL (f) = new_color;
2126 else
2127 FRAME_BACKGROUND_PIXEL (f) = new_color;
2128 redraw = 1;
2129 bg_set = 1;
2130 if (termscript)
2131 fprintf (termscript, "<BGCOLOR %lu>\n", new_color);
2132 }
2133 }
2134 else if (EQ (prop, Qtitle))
2135 {
2136 x_set_title (f, val);
2137 if (termscript)
2138 fprintf (termscript, "<TITLE: %s>\n", XSTRING (val)->data);
2139 }
2140 store_frame_param (f, prop, val);
2141 }
2142
2143 /* If they specified "reverse", but not the colors, we need to swap
2144 the current frame colors. */
2145 if (reverse && !was_reverse)
2146 {
2147 if (!fg_set)
2148 {
2149 FRAME_BACKGROUND_PIXEL (f) = orig_fg;
2150 redraw = 1;
2151 }
2152 if (!bg_set)
2153 {
2154 FRAME_FOREGROUND_PIXEL (f) = orig_bg;
2155 redraw = 1;
2156 }
2157 }
2158
2159 if (redraw)
2160 {
2161 face_change_count++; /* forces xdisp.c to recompute basic faces */
2162 if (f == SELECTED_FRAME())
2163 redraw_frame (f);
2164 }
2165 }
2166
2167 extern void init_frame_faces (FRAME_PTR);
2168
2169 #endif /* !HAVE_X_WINDOWS */
2170
2171
2172 /* Do we need the internal terminal? */
2173
2174 void
2175 internal_terminal_init ()
2176 {
2177 char *term = getenv ("TERM");
2178 char *colors;
2179 struct frame *sf = SELECTED_FRAME();
2180
2181 #ifdef HAVE_X_WINDOWS
2182 if (!inhibit_window_system)
2183 return;
2184 #endif
2185
2186 internal_terminal
2187 = (!noninteractive) && term && !strcmp (term, "internal");
2188
2189 if (getenv ("EMACSTEST"))
2190 termscript = fopen (getenv ("EMACSTEST"), "wt");
2191
2192 #ifndef HAVE_X_WINDOWS
2193 if (!internal_terminal || inhibit_window_system)
2194 {
2195 sf->output_method = output_termcap;
2196 return;
2197 }
2198
2199 Vwindow_system = intern ("pc");
2200 Vwindow_system_version = make_number (1);
2201 sf->output_method = output_msdos_raw;
2202
2203 /* If Emacs was dumped on DOS/V machine, forget the stale VRAM address. */
2204 screen_old_address = 0;
2205
2206 /* Forget the stale screen colors as well. */
2207 initial_screen_colors[0] = initial_screen_colors[1] = -1;
2208
2209 bzero (&the_only_x_display, sizeof the_only_x_display);
2210 the_only_x_display.background_pixel = 7; /* White */
2211 the_only_x_display.foreground_pixel = 0; /* Black */
2212 bright_bg ();
2213 colors = getenv ("EMACSCOLORS");
2214 if (colors && strlen (colors) >= 2)
2215 {
2216 /* The colors use 4 bits each (we enable bright background). */
2217 if (isdigit (colors[0]))
2218 colors[0] -= '0';
2219 else if (isxdigit (colors[0]))
2220 colors[0] -= (isupper (colors[0]) ? 'A' : 'a') - 10;
2221 if (colors[0] >= 0 && colors[0] < 16)
2222 the_only_x_display.foreground_pixel = colors[0];
2223 if (isdigit (colors[1]))
2224 colors[1] -= '0';
2225 else if (isxdigit (colors[1]))
2226 colors[1] -= (isupper (colors[1]) ? 'A' : 'a') - 10;
2227 if (colors[1] >= 0 && colors[1] < 16)
2228 the_only_x_display.background_pixel = colors[1];
2229 }
2230 the_only_x_display.line_height = 1;
2231 the_only_x_display.font = (XFontStruct *)1; /* must *not* be zero */
2232 the_only_x_display.display_info.mouse_face_mouse_frame = NULL;
2233 the_only_x_display.display_info.mouse_face_deferred_gc = 0;
2234 the_only_x_display.display_info.mouse_face_beg_row =
2235 the_only_x_display.display_info.mouse_face_beg_col = -1;
2236 the_only_x_display.display_info.mouse_face_end_row =
2237 the_only_x_display.display_info.mouse_face_end_col = -1;
2238 the_only_x_display.display_info.mouse_face_face_id = DEFAULT_FACE_ID;
2239 the_only_x_display.display_info.mouse_face_window = Qnil;
2240 the_only_x_display.display_info.mouse_face_mouse_x =
2241 the_only_x_display.display_info.mouse_face_mouse_y = 0;
2242 the_only_x_display.display_info.mouse_face_defer = 0;
2243
2244 init_frame_faces (sf);
2245
2246 ring_bell_hook = IT_ring_bell;
2247 insert_glyphs_hook = IT_insert_glyphs;
2248 delete_glyphs_hook = IT_delete_glyphs;
2249 write_glyphs_hook = IT_write_glyphs;
2250 cursor_to_hook = raw_cursor_to_hook = IT_cursor_to;
2251 clear_to_end_hook = IT_clear_to_end;
2252 clear_end_of_line_hook = IT_clear_end_of_line;
2253 clear_frame_hook = IT_clear_screen;
2254 change_line_highlight_hook = IT_change_line_highlight;
2255 update_begin_hook = IT_update_begin;
2256 update_end_hook = IT_update_end;
2257 reassert_line_highlight_hook = IT_reassert_line_highlight;
2258 frame_up_to_date_hook = IT_frame_up_to_date;
2259
2260 /* These hooks are called by term.c without being checked. */
2261 set_terminal_modes_hook = IT_set_terminal_modes;
2262 reset_terminal_modes_hook = IT_reset_terminal_modes;
2263 set_terminal_window_hook = IT_set_terminal_window;
2264 char_ins_del_ok = 0;
2265 #endif
2266 }
2267
2268 dos_get_saved_screen (screen, rows, cols)
2269 char **screen;
2270 int *rows;
2271 int *cols;
2272 {
2273 #ifndef HAVE_X_WINDOWS
2274 *screen = startup_screen_buffer;
2275 *cols = startup_screen_size_X;
2276 *rows = startup_screen_size_Y;
2277 return *screen != (char *)0;
2278 #else
2279 return 0;
2280 #endif
2281 }
2282
2283 #ifndef HAVE_X_WINDOWS
2284
2285 /* We are not X, but we can emulate it well enough for our needs... */
2286 void
2287 check_x (void)
2288 {
2289 if (! FRAME_MSDOS_P (SELECTED_FRAME()))
2290 error ("Not running under a window system");
2291 }
2292
2293 #endif
2294
2295 \f
2296 /* ----------------------- Keyboard control ----------------------
2297 *
2298 * Keymaps reflect the following keyboard layout:
2299 *
2300 * 0 1 2 3 4 5 6 7 8 9 10 11 12 BS
2301 * TAB 15 16 17 18 19 20 21 22 23 24 25 26 (41)
2302 * CLOK 30 31 32 33 34 35 36 37 38 39 40 (41) RET
2303 * SH () 45 46 47 48 49 50 51 52 53 54 SHIFT
2304 * SPACE
2305 */
2306
2307 #define Ignore 0x0000
2308 #define Normal 0x0000 /* normal key - alt changes scan-code */
2309 #define FctKey 0x1000 /* func key if c == 0, else c */
2310 #define Special 0x2000 /* func key even if c != 0 */
2311 #define ModFct 0x3000 /* special if mod-keys, else 'c' */
2312 #define Map 0x4000 /* alt scan-code, map to unshift/shift key */
2313 #define KeyPad 0x5000 /* map to insert/kp-0 depending on c == 0xe0 */
2314 #define Grey 0x6000 /* Grey keypad key */
2315
2316 #define Alt 0x0100 /* alt scan-code */
2317 #define Ctrl 0x0200 /* ctrl scan-code */
2318 #define Shift 0x0400 /* shift scan-code */
2319
2320 static int extended_kbd; /* 101 (102) keyboard present. */
2321
2322 struct kbd_translate {
2323 unsigned char sc;
2324 unsigned char ch;
2325 unsigned short code;
2326 };
2327
2328 struct dos_keyboard_map
2329 {
2330 char *unshifted;
2331 char *shifted;
2332 char *alt_gr;
2333 struct kbd_translate *translate_table;
2334 };
2335
2336
2337 static struct dos_keyboard_map us_keyboard = {
2338 /* 0 1 2 3 4 5 */
2339 /* 01234567890123456789012345678901234567890 12345678901234 */
2340 "`1234567890-= qwertyuiop[] asdfghjkl;'\\ zxcvbnm,./ ",
2341 /* 0123456789012345678901234567890123456789 012345678901234 */
2342 "~!@#$%^&*()_+ QWERTYUIOP{} ASDFGHJKL:\"| ZXCVBNM<>? ",
2343 0, /* no Alt-Gr key */
2344 0 /* no translate table */
2345 };
2346
2347 static struct dos_keyboard_map fr_keyboard = {
2348 /* 0 1 2 3 4 5 */
2349 /* 012 3456789012345678901234567890123456789012345678901234 */
2350 "ý&\82\",(-\8a_\80\85)= azertyuiop^$ qsdfghjklm\97* wxcvbnm;:! ",
2351 /* 0123456789012345678901234567890123456789012345678901234 */
2352 " 1234567890ø+ AZERTYUIOPù\9c QSDFGHJKLM%æ WXCVBN?./õ ",
2353 /* 01234567 89012345678901234567890123456789012345678901234 */
2354 " ~#{[|`\\^@]} Ï ",
2355 0 /* no translate table */
2356 };
2357
2358 /*
2359 * Italian keyboard support, country code 39.
2360 * '<' 56:3c*0000
2361 * '>' 56:3e*0000
2362 * added also {,},` as, respectively, AltGr-8, AltGr-9, AltGr-'
2363 * Donated by Stefano Brozzi <brozzis@mag00.cedi.unipr.it>
2364 */
2365
2366 static struct kbd_translate it_kbd_translate_table[] = {
2367 { 0x56, 0x3c, Normal | 13 },
2368 { 0x56, 0x3e, Normal | 27 },
2369 { 0, 0, 0 }
2370 };
2371 static struct dos_keyboard_map it_keyboard = {
2372 /* 0 1 2 3 4 5 */
2373 /* 0 123456789012345678901234567890123456789012345678901234 */
2374 "\\1234567890'\8d< qwertyuiop\8a+> asdfghjkl\95\85\97 zxcvbnm,.- ",
2375 /* 01 23456789012345678901234567890123456789012345678901234 */
2376 "|!\"\9c$%&/()=?^> QWERTYUIOP\82* ASDFGHJKL\87øõ ZXCVBNM;:_ ",
2377 /* 0123456789012345678901234567890123456789012345678901234 */
2378 " {}~` [] @# ",
2379 it_kbd_translate_table
2380 };
2381
2382 static struct dos_keyboard_map dk_keyboard = {
2383 /* 0 1 2 3 4 5 */
2384 /* 0123456789012345678901234567890123456789012345678901234 */
2385 "«1234567890+| qwertyuiop\86~ asdfghjkl\91\9b' zxcvbnm,.- ",
2386 /* 01 23456789012345678901234567890123456789012345678901234 */
2387 "õ!\"#$%&/()=?` QWERTYUIOP\8f^ ASDFGHJKL\92\9d* ZXCVBNM;:_ ",
2388 /* 0123456789012345678901234567890123456789012345678901234 */
2389 " @\9c$ {[]} | ",
2390 0 /* no translate table */
2391 };
2392
2393 static struct kbd_translate jp_kbd_translate_table[] = {
2394 { 0x73, 0x5c, Normal | 0 },
2395 { 0x73, 0x5f, Normal | 0 },
2396 { 0x73, 0x1c, Map | 0 },
2397 { 0x7d, 0x5c, Normal | 13 },
2398 { 0x7d, 0x7c, Normal | 13 },
2399 { 0x7d, 0x1c, Map | 13 },
2400 { 0, 0, 0 }
2401 };
2402 static struct dos_keyboard_map jp_keyboard = {
2403 /* 0 1 2 3 4 5 */
2404 /* 0123456789012 345678901234567890123456789012345678901234 */
2405 "\\1234567890-^\\ qwertyuiop@[ asdfghjkl;:] zxcvbnm,./ ",
2406 /* 01 23456789012345678901234567890123456789012345678901234 */
2407 "_!\"#$%&'()~=~| QWERTYUIOP`{ ASDFGHJKL+*} ZXCVBNM<>? ",
2408 0, /* no Alt-Gr key */
2409 jp_kbd_translate_table
2410 };
2411
2412 static struct keyboard_layout_list
2413 {
2414 int country_code;
2415 struct dos_keyboard_map *keyboard_map;
2416 } keyboard_layout_list[] =
2417 {
2418 1, &us_keyboard,
2419 33, &fr_keyboard,
2420 39, &it_keyboard,
2421 45, &dk_keyboard,
2422 81, &jp_keyboard
2423 };
2424
2425 static struct dos_keyboard_map *keyboard;
2426 static int keyboard_map_all;
2427 static int international_keyboard;
2428
2429 int
2430 dos_set_keyboard (code, always)
2431 int code;
2432 int always;
2433 {
2434 int i;
2435 _go32_dpmi_registers regs;
2436
2437 /* See if Keyb.Com is installed (for international keyboard support).
2438 Note: calling Int 2Fh via int86 wedges the DOS box on some versions
2439 of Windows 9X! So don't do that! */
2440 regs.x.ax = 0xad80;
2441 regs.x.ss = regs.x.sp = regs.x.flags = 0;
2442 _go32_dpmi_simulate_int (0x2f, &regs);
2443 if (regs.h.al == 0xff)
2444 international_keyboard = 1;
2445
2446 /* Initialize to US settings, for countries that don't have their own. */
2447 keyboard = keyboard_layout_list[0].keyboard_map;
2448 keyboard_map_all = always;
2449 dos_keyboard_layout = 1;
2450
2451 for (i = 0; i < (sizeof (keyboard_layout_list)/sizeof (struct keyboard_layout_list)); i++)
2452 if (code == keyboard_layout_list[i].country_code)
2453 {
2454 keyboard = keyboard_layout_list[i].keyboard_map;
2455 keyboard_map_all = always;
2456 dos_keyboard_layout = code;
2457 return 1;
2458 }
2459 return 0;
2460 }
2461 \f
2462 static struct
2463 {
2464 unsigned char char_code; /* normal code */
2465 unsigned char meta_code; /* M- code */
2466 unsigned char keypad_code; /* keypad code */
2467 unsigned char editkey_code; /* edit key */
2468 } keypad_translate_map[] = {
2469 '0', '0', 0xb0, /* kp-0 */ 0x63, /* insert */
2470 '1', '1', 0xb1, /* kp-1 */ 0x57, /* end */
2471 '2', '2', 0xb2, /* kp-2 */ 0x54, /* down */
2472 '3', '3', 0xb3, /* kp-3 */ 0x56, /* next */
2473 '4', '4', 0xb4, /* kp-4 */ 0x51, /* left */
2474 '5', '5', 0xb5, /* kp-5 */ 0xb5, /* kp-5 */
2475 '6', '6', 0xb6, /* kp-6 */ 0x53, /* right */
2476 '7', '7', 0xb7, /* kp-7 */ 0x50, /* home */
2477 '8', '8', 0xb8, /* kp-8 */ 0x52, /* up */
2478 '9', '9', 0xb9, /* kp-9 */ 0x55, /* prior */
2479 '.', '-', 0xae, /* kp-decimal */ 0xff /* delete */
2480 };
2481
2482 static struct
2483 {
2484 unsigned char char_code; /* normal code */
2485 unsigned char keypad_code; /* keypad code */
2486 } grey_key_translate_map[] = {
2487 '/', 0xaf, /* kp-decimal */
2488 '*', 0xaa, /* kp-multiply */
2489 '-', 0xad, /* kp-subtract */
2490 '+', 0xab, /* kp-add */
2491 '\r', 0x8d /* kp-enter */
2492 };
2493
2494 static unsigned short
2495 ibmpc_translate_map[] =
2496 {
2497 /* --------------- 00 to 0f --------------- */
2498 Normal | 0xff, /* Ctrl Break + Alt-NNN */
2499 Alt | ModFct | 0x1b, /* Escape */
2500 Normal | 1, /* '1' */
2501 Normal | 2, /* '2' */
2502 Normal | 3, /* '3' */
2503 Normal | 4, /* '4' */
2504 Normal | 5, /* '5' */
2505 Normal | 6, /* '6' */
2506 Normal | 7, /* '7' */
2507 Normal | 8, /* '8' */
2508 Normal | 9, /* '9' */
2509 Normal | 10, /* '0' */
2510 Normal | 11, /* '-' */
2511 Normal | 12, /* '=' */
2512 Special | 0x08, /* Backspace */
2513 ModFct | 0x74, /* Tab/Backtab */
2514
2515 /* --------------- 10 to 1f --------------- */
2516 Map | 15, /* 'q' */
2517 Map | 16, /* 'w' */
2518 Map | 17, /* 'e' */
2519 Map | 18, /* 'r' */
2520 Map | 19, /* 't' */
2521 Map | 20, /* 'y' */
2522 Map | 21, /* 'u' */
2523 Map | 22, /* 'i' */
2524 Map | 23, /* 'o' */
2525 Map | 24, /* 'p' */
2526 Map | 25, /* '[' */
2527 Map | 26, /* ']' */
2528 ModFct | 0x0d, /* Return */
2529 Ignore, /* Ctrl */
2530 Map | 30, /* 'a' */
2531 Map | 31, /* 's' */
2532
2533 /* --------------- 20 to 2f --------------- */
2534 Map | 32, /* 'd' */
2535 Map | 33, /* 'f' */
2536 Map | 34, /* 'g' */
2537 Map | 35, /* 'h' */
2538 Map | 36, /* 'j' */
2539 Map | 37, /* 'k' */
2540 Map | 38, /* 'l' */
2541 Map | 39, /* ';' */
2542 Map | 40, /* '\'' */
2543 Map | 0, /* '`' */
2544 Ignore, /* Left shift */
2545 Map | 41, /* '\\' */
2546 Map | 45, /* 'z' */
2547 Map | 46, /* 'x' */
2548 Map | 47, /* 'c' */
2549 Map | 48, /* 'v' */
2550
2551 /* --------------- 30 to 3f --------------- */
2552 Map | 49, /* 'b' */
2553 Map | 50, /* 'n' */
2554 Map | 51, /* 'm' */
2555 Map | 52, /* ',' */
2556 Map | 53, /* '.' */
2557 Map | 54, /* '/' */
2558 Ignore, /* Right shift */
2559 Grey | 1, /* Grey * */
2560 Ignore, /* Alt */
2561 Normal | 55, /* ' ' */
2562 Ignore, /* Caps Lock */
2563 FctKey | 0xbe, /* F1 */
2564 FctKey | 0xbf, /* F2 */
2565 FctKey | 0xc0, /* F3 */
2566 FctKey | 0xc1, /* F4 */
2567 FctKey | 0xc2, /* F5 */
2568
2569 /* --------------- 40 to 4f --------------- */
2570 FctKey | 0xc3, /* F6 */
2571 FctKey | 0xc4, /* F7 */
2572 FctKey | 0xc5, /* F8 */
2573 FctKey | 0xc6, /* F9 */
2574 FctKey | 0xc7, /* F10 */
2575 Ignore, /* Num Lock */
2576 Ignore, /* Scroll Lock */
2577 KeyPad | 7, /* Home */
2578 KeyPad | 8, /* Up */
2579 KeyPad | 9, /* Page Up */
2580 Grey | 2, /* Grey - */
2581 KeyPad | 4, /* Left */
2582 KeyPad | 5, /* Keypad 5 */
2583 KeyPad | 6, /* Right */
2584 Grey | 3, /* Grey + */
2585 KeyPad | 1, /* End */
2586
2587 /* --------------- 50 to 5f --------------- */
2588 KeyPad | 2, /* Down */
2589 KeyPad | 3, /* Page Down */
2590 KeyPad | 0, /* Insert */
2591 KeyPad | 10, /* Delete */
2592 Shift | FctKey | 0xbe, /* (Shift) F1 */
2593 Shift | FctKey | 0xbf, /* (Shift) F2 */
2594 Shift | FctKey | 0xc0, /* (Shift) F3 */
2595 Shift | FctKey | 0xc1, /* (Shift) F4 */
2596 Shift | FctKey | 0xc2, /* (Shift) F5 */
2597 Shift | FctKey | 0xc3, /* (Shift) F6 */
2598 Shift | FctKey | 0xc4, /* (Shift) F7 */
2599 Shift | FctKey | 0xc5, /* (Shift) F8 */
2600 Shift | FctKey | 0xc6, /* (Shift) F9 */
2601 Shift | FctKey | 0xc7, /* (Shift) F10 */
2602 Ctrl | FctKey | 0xbe, /* (Ctrl) F1 */
2603 Ctrl | FctKey | 0xbf, /* (Ctrl) F2 */
2604
2605 /* --------------- 60 to 6f --------------- */
2606 Ctrl | FctKey | 0xc0, /* (Ctrl) F3 */
2607 Ctrl | FctKey | 0xc1, /* (Ctrl) F4 */
2608 Ctrl | FctKey | 0xc2, /* (Ctrl) F5 */
2609 Ctrl | FctKey | 0xc3, /* (Ctrl) F6 */
2610 Ctrl | FctKey | 0xc4, /* (Ctrl) F7 */
2611 Ctrl | FctKey | 0xc5, /* (Ctrl) F8 */
2612 Ctrl | FctKey | 0xc6, /* (Ctrl) F9 */
2613 Ctrl | FctKey | 0xc7, /* (Ctrl) F10 */
2614 Alt | FctKey | 0xbe, /* (Alt) F1 */
2615 Alt | FctKey | 0xbf, /* (Alt) F2 */
2616 Alt | FctKey | 0xc0, /* (Alt) F3 */
2617 Alt | FctKey | 0xc1, /* (Alt) F4 */
2618 Alt | FctKey | 0xc2, /* (Alt) F5 */
2619 Alt | FctKey | 0xc3, /* (Alt) F6 */
2620 Alt | FctKey | 0xc4, /* (Alt) F7 */
2621 Alt | FctKey | 0xc5, /* (Alt) F8 */
2622
2623 /* --------------- 70 to 7f --------------- */
2624 Alt | FctKey | 0xc6, /* (Alt) F9 */
2625 Alt | FctKey | 0xc7, /* (Alt) F10 */
2626 Ctrl | FctKey | 0x6d, /* (Ctrl) Sys Rq */
2627 Ctrl | KeyPad | 4, /* (Ctrl) Left */
2628 Ctrl | KeyPad | 6, /* (Ctrl) Right */
2629 Ctrl | KeyPad | 1, /* (Ctrl) End */
2630 Ctrl | KeyPad | 3, /* (Ctrl) Page Down */
2631 Ctrl | KeyPad | 7, /* (Ctrl) Home */
2632 Alt | Map | 1, /* '1' */
2633 Alt | Map | 2, /* '2' */
2634 Alt | Map | 3, /* '3' */
2635 Alt | Map | 4, /* '4' */
2636 Alt | Map | 5, /* '5' */
2637 Alt | Map | 6, /* '6' */
2638 Alt | Map | 7, /* '7' */
2639 Alt | Map | 8, /* '8' */
2640
2641 /* --------------- 80 to 8f --------------- */
2642 Alt | Map | 9, /* '9' */
2643 Alt | Map | 10, /* '0' */
2644 Alt | Map | 11, /* '-' */
2645 Alt | Map | 12, /* '=' */
2646 Ctrl | KeyPad | 9, /* (Ctrl) Page Up */
2647 FctKey | 0xc8, /* F11 */
2648 FctKey | 0xc9, /* F12 */
2649 Shift | FctKey | 0xc8, /* (Shift) F11 */
2650 Shift | FctKey | 0xc9, /* (Shift) F12 */
2651 Ctrl | FctKey | 0xc8, /* (Ctrl) F11 */
2652 Ctrl | FctKey | 0xc9, /* (Ctrl) F12 */
2653 Alt | FctKey | 0xc8, /* (Alt) F11 */
2654 Alt | FctKey | 0xc9, /* (Alt) F12 */
2655 Ctrl | KeyPad | 8, /* (Ctrl) Up */
2656 Ctrl | Grey | 2, /* (Ctrl) Grey - */
2657 Ctrl | KeyPad | 5, /* (Ctrl) Keypad 5 */
2658
2659 /* --------------- 90 to 9f --------------- */
2660 Ctrl | Grey | 3, /* (Ctrl) Grey + */
2661 Ctrl | KeyPad | 2, /* (Ctrl) Down */
2662 Ctrl | KeyPad | 0, /* (Ctrl) Insert */
2663 Ctrl | KeyPad | 10, /* (Ctrl) Delete */
2664 Ctrl | FctKey | 0x09, /* (Ctrl) Tab */
2665 Ctrl | Grey | 0, /* (Ctrl) Grey / */
2666 Ctrl | Grey | 1, /* (Ctrl) Grey * */
2667 Alt | FctKey | 0x50, /* (Alt) Home */
2668 Alt | FctKey | 0x52, /* (Alt) Up */
2669 Alt | FctKey | 0x55, /* (Alt) Page Up */
2670 Ignore, /* NO KEY */
2671 Alt | FctKey | 0x51, /* (Alt) Left */
2672 Ignore, /* NO KEY */
2673 Alt | FctKey | 0x53, /* (Alt) Right */
2674 Ignore, /* NO KEY */
2675 Alt | FctKey | 0x57, /* (Alt) End */
2676
2677 /* --------------- a0 to af --------------- */
2678 Alt | KeyPad | 2, /* (Alt) Down */
2679 Alt | KeyPad | 3, /* (Alt) Page Down */
2680 Alt | KeyPad | 0, /* (Alt) Insert */
2681 Alt | KeyPad | 10, /* (Alt) Delete */
2682 Alt | Grey | 0, /* (Alt) Grey / */
2683 Alt | FctKey | 0x09, /* (Alt) Tab */
2684 Alt | Grey | 4 /* (Alt) Keypad Enter */
2685 };
2686 \f
2687 /* These bit-positions corresponds to values returned by BIOS */
2688 #define SHIFT_P 0x0003 /* two bits! */
2689 #define CTRL_P 0x0004
2690 #define ALT_P 0x0008
2691 #define SCRLOCK_P 0x0010
2692 #define NUMLOCK_P 0x0020
2693 #define CAPSLOCK_P 0x0040
2694 #define ALT_GR_P 0x0800
2695 #define SUPER_P 0x4000 /* pseudo */
2696 #define HYPER_P 0x8000 /* pseudo */
2697
2698 static int
2699 dos_get_modifiers (keymask)
2700 int *keymask;
2701 {
2702 union REGS regs;
2703 int mask;
2704 int modifiers = 0;
2705
2706 /* Calculate modifier bits */
2707 regs.h.ah = extended_kbd ? 0x12 : 0x02;
2708 int86 (0x16, &regs, &regs);
2709
2710 if (!extended_kbd)
2711 {
2712 mask = regs.h.al & (SHIFT_P | CTRL_P | ALT_P |
2713 SCRLOCK_P | NUMLOCK_P | CAPSLOCK_P);
2714 }
2715 else
2716 {
2717 mask = regs.h.al & (SHIFT_P |
2718 SCRLOCK_P | NUMLOCK_P | CAPSLOCK_P);
2719
2720 /* Do not break international keyboard support. */
2721 /* When Keyb.Com is loaded, the right Alt key is */
2722 /* used for accessing characters like { and } */
2723 if (regs.h.ah & 2) /* Left ALT pressed ? */
2724 mask |= ALT_P;
2725
2726 if ((regs.h.ah & 8) != 0) /* Right ALT pressed ? */
2727 {
2728 mask |= ALT_GR_P;
2729 if (dos_hyper_key == 1)
2730 {
2731 mask |= HYPER_P;
2732 modifiers |= hyper_modifier;
2733 }
2734 else if (dos_super_key == 1)
2735 {
2736 mask |= SUPER_P;
2737 modifiers |= super_modifier;
2738 }
2739 else if (!international_keyboard)
2740 {
2741 /* If Keyb.Com is NOT installed, let Right Alt behave
2742 like the Left Alt. */
2743 mask &= ~ALT_GR_P;
2744 mask |= ALT_P;
2745 }
2746 }
2747
2748 if (regs.h.ah & 1) /* Left CTRL pressed ? */
2749 mask |= CTRL_P;
2750
2751 if (regs.h.ah & 4) /* Right CTRL pressed ? */
2752 {
2753 if (dos_hyper_key == 2)
2754 {
2755 mask |= HYPER_P;
2756 modifiers |= hyper_modifier;
2757 }
2758 else if (dos_super_key == 2)
2759 {
2760 mask |= SUPER_P;
2761 modifiers |= super_modifier;
2762 }
2763 else
2764 mask |= CTRL_P;
2765 }
2766 }
2767
2768 if (mask & SHIFT_P)
2769 modifiers |= shift_modifier;
2770 if (mask & CTRL_P)
2771 modifiers |= ctrl_modifier;
2772 if (mask & ALT_P)
2773 modifiers |= meta_modifier;
2774
2775 if (keymask)
2776 *keymask = mask;
2777 return modifiers;
2778 }
2779
2780 #define NUM_RECENT_DOSKEYS (100)
2781 int recent_doskeys_index; /* Index for storing next element into recent_doskeys */
2782 int total_doskeys; /* Total number of elements stored into recent_doskeys */
2783 Lisp_Object recent_doskeys; /* A vector, holding the last 100 keystrokes */
2784
2785 DEFUN ("recent-doskeys", Frecent_doskeys, Srecent_doskeys, 0, 0, 0,
2786 "Return vector of last 100 keyboard input values seen in dos_rawgetc.\n\
2787 Each input key receives two values in this vector: first the ASCII code,\n\
2788 and then the scan code.")
2789 ()
2790 {
2791 Lisp_Object *keys = XVECTOR (recent_doskeys)->contents;
2792 Lisp_Object val;
2793
2794 if (total_doskeys < NUM_RECENT_DOSKEYS)
2795 return Fvector (total_doskeys, keys);
2796 else
2797 {
2798 val = Fvector (NUM_RECENT_DOSKEYS, keys);
2799 bcopy (keys + recent_doskeys_index,
2800 XVECTOR (val)->contents,
2801 (NUM_RECENT_DOSKEYS - recent_doskeys_index) * sizeof (Lisp_Object));
2802 bcopy (keys,
2803 XVECTOR (val)->contents + NUM_RECENT_DOSKEYS - recent_doskeys_index,
2804 recent_doskeys_index * sizeof (Lisp_Object));
2805 return val;
2806 }
2807 }
2808
2809 /* Get a char from keyboard. Function keys are put into the event queue. */
2810
2811 extern void kbd_buffer_store_event (struct input_event *);
2812
2813 static int
2814 dos_rawgetc ()
2815 {
2816 struct input_event event;
2817 union REGS regs;
2818
2819 #ifndef HAVE_X_WINDOWS
2820 /* Maybe put the cursor where it should be. */
2821 IT_cmgoto (SELECTED_FRAME());
2822 #endif
2823
2824 /* The following condition is equivalent to `kbhit ()', except that
2825 it uses the bios to do its job. This pleases DESQview/X. */
2826 while ((regs.h.ah = extended_kbd ? 0x11 : 0x01),
2827 int86 (0x16, &regs, &regs),
2828 (regs.x.flags & 0x40) == 0)
2829 {
2830 union REGS regs;
2831 register unsigned char c;
2832 int sc, code = -1, mask, kp_mode;
2833 int modifiers;
2834
2835 regs.h.ah = extended_kbd ? 0x10 : 0x00;
2836 int86 (0x16, &regs, &regs);
2837 c = regs.h.al;
2838 sc = regs.h.ah;
2839
2840 total_doskeys += 2;
2841 XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
2842 = make_number (c);
2843 if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
2844 recent_doskeys_index = 0;
2845 XVECTOR (recent_doskeys)->contents[recent_doskeys_index++]
2846 = make_number (sc);
2847 if (recent_doskeys_index == NUM_RECENT_DOSKEYS)
2848 recent_doskeys_index = 0;
2849
2850 modifiers = dos_get_modifiers (&mask);
2851
2852 #ifndef HAVE_X_WINDOWS
2853 if (!NILP (Vdos_display_scancodes))
2854 {
2855 char buf[11];
2856 sprintf (buf, "%02x:%02x*%04x",
2857 (unsigned) (sc&0xff), (unsigned) c, mask);
2858 dos_direct_output (screen_size_Y - 2, screen_size_X - 12, buf, 10);
2859 }
2860 #endif
2861
2862 if (sc == 0xe0)
2863 {
2864 switch (c)
2865 {
2866 case 10: /* Ctrl Grey Enter */
2867 code = Ctrl | Grey | 4;
2868 break;
2869 case 13: /* Grey Enter */
2870 code = Grey | 4;
2871 break;
2872 case '/': /* Grey / */
2873 code = Grey | 0;
2874 break;
2875 default:
2876 continue;
2877 };
2878 c = 0;
2879 }
2880 else
2881 {
2882 /* Try the keyboard-private translation table first. */
2883 if (keyboard->translate_table)
2884 {
2885 struct kbd_translate *p = keyboard->translate_table;
2886
2887 while (p->sc)
2888 {
2889 if (p->sc == sc && p->ch == c)
2890 {
2891 code = p->code;
2892 break;
2893 }
2894 p++;
2895 }
2896 }
2897 /* If the private table didn't translate it, use the general
2898 one. */
2899 if (code == -1)
2900 {
2901 if (sc >= (sizeof (ibmpc_translate_map) / sizeof (short)))
2902 continue;
2903 if ((code = ibmpc_translate_map[sc]) == Ignore)
2904 continue;
2905 }
2906 }
2907
2908 if (c == 0)
2909 {
2910 /* We only look at the keyboard Ctrl/Shift/Alt keys when
2911 Emacs is ready to read a key. Therefore, if they press
2912 `Alt-x' when Emacs is busy, by the time we get to
2913 `dos_get_modifiers', they might have already released the
2914 Alt key, and Emacs gets just `x', which is BAD.
2915 However, for keys with the `Map' property set, the ASCII
2916 code returns zero iff Alt is pressed. So, when we DON'T
2917 have to support international_keyboard, we don't have to
2918 distinguish between the left and right Alt keys, and we
2919 can set the META modifier for any keys with the `Map'
2920 property if they return zero ASCII code (c = 0). */
2921 if ( (code & Alt)
2922 || ( (code & 0xf000) == Map && !international_keyboard))
2923 modifiers |= meta_modifier;
2924 if (code & Ctrl)
2925 modifiers |= ctrl_modifier;
2926 if (code & Shift)
2927 modifiers |= shift_modifier;
2928 }
2929
2930 switch (code & 0xf000)
2931 {
2932 case ModFct:
2933 if (c && !(mask & (SHIFT_P | ALT_P | CTRL_P | HYPER_P | SUPER_P)))
2934 return c;
2935 c = 0; /* Special */
2936
2937 case FctKey:
2938 if (c != 0)
2939 return c;
2940
2941 case Special:
2942 code |= 0xff00;
2943 break;
2944
2945 case Normal:
2946 if (sc == 0)
2947 {
2948 if (c == 0) /* ctrl-break */
2949 continue;
2950 return c; /* ALT-nnn */
2951 }
2952 if (!keyboard_map_all)
2953 {
2954 if (c != ' ')
2955 return c;
2956 code = c;
2957 break;
2958 }
2959
2960 case Map:
2961 if (c && !(mask & ALT_P) && !((mask & SHIFT_P) && (mask & CTRL_P)))
2962 if (!keyboard_map_all)
2963 return c;
2964
2965 code &= 0xff;
2966 if (mask & ALT_P && code <= 10 && code > 0 && dos_keypad_mode & 0x200)
2967 mask |= SHIFT_P; /* ALT-1 => M-! etc. */
2968
2969 if (mask & SHIFT_P)
2970 {
2971 code = keyboard->shifted[code];
2972 mask -= SHIFT_P;
2973 modifiers &= ~shift_modifier;
2974 }
2975 else
2976 if ((mask & ALT_GR_P) && keyboard->alt_gr && keyboard->alt_gr[code] != ' ')
2977 code = keyboard->alt_gr[code];
2978 else
2979 code = keyboard->unshifted[code];
2980 break;
2981
2982 case KeyPad:
2983 code &= 0xff;
2984 if (c == 0xe0) /* edit key */
2985 kp_mode = 3;
2986 else
2987 if ((mask & (NUMLOCK_P|CTRL_P|SHIFT_P|ALT_P)) == NUMLOCK_P) /* numlock on */
2988 kp_mode = dos_keypad_mode & 0x03;
2989 else
2990 kp_mode = (dos_keypad_mode >> 4) & 0x03;
2991
2992 switch (kp_mode)
2993 {
2994 case 0:
2995 if (code == 10 && dos_decimal_point)
2996 return dos_decimal_point;
2997 return keypad_translate_map[code].char_code;
2998
2999 case 1:
3000 code = 0xff00 | keypad_translate_map[code].keypad_code;
3001 break;
3002
3003 case 2:
3004 code = keypad_translate_map[code].meta_code;
3005 modifiers = meta_modifier;
3006 break;
3007
3008 case 3:
3009 code = 0xff00 | keypad_translate_map[code].editkey_code;
3010 break;
3011 }
3012 break;
3013
3014 case Grey:
3015 code &= 0xff;
3016 kp_mode = ((mask & (NUMLOCK_P|CTRL_P|SHIFT_P|ALT_P)) == NUMLOCK_P) ? 0x04 : 0x40;
3017 if (dos_keypad_mode & kp_mode)
3018 code = 0xff00 | grey_key_translate_map[code].keypad_code;
3019 else
3020 code = grey_key_translate_map[code].char_code;
3021 break;
3022 }
3023
3024 make_event:
3025 if (code == 0)
3026 continue;
3027
3028 if (code >= 0x100)
3029 event.kind = non_ascii_keystroke;
3030 else
3031 event.kind = ascii_keystroke;
3032 event.code = code;
3033 event.modifiers = modifiers;
3034 event.frame_or_window = selected_frame;
3035 event.timestamp = event_timestamp ();
3036 kbd_buffer_store_event (&event);
3037 }
3038
3039 if (have_mouse > 0 && !mouse_preempted)
3040 {
3041 int but, press, x, y, ok;
3042 int mouse_prev_x = mouse_last_x, mouse_prev_y = mouse_last_y;
3043
3044 /* Check for mouse movement *before* buttons. */
3045 mouse_check_moved ();
3046
3047 /* If the mouse moved from the spot of its last sighting, we
3048 might need to update mouse highlight. */
3049 if (mouse_last_x != mouse_prev_x || mouse_last_y != mouse_prev_y)
3050 {
3051 previous_help_echo = help_echo;
3052 help_echo = Qnil;
3053 IT_note_mouse_highlight (SELECTED_FRAME(),
3054 mouse_last_x, mouse_last_y);
3055 /* If the contents of the global variable help_echo has
3056 changed, generate a HELP_EVENT. */
3057 if (STRINGP (help_echo) || STRINGP (previous_help_echo))
3058 {
3059 event.kind = HELP_EVENT;
3060 event.frame_or_window = Fcons (selected_frame, help_echo);
3061 event.timestamp = event_timestamp ();
3062 kbd_buffer_store_event (&event);
3063 }
3064 }
3065
3066 for (but = 0; but < NUM_MOUSE_BUTTONS; but++)
3067 for (press = 0; press < 2; press++)
3068 {
3069 int button_num = but;
3070
3071 if (press)
3072 ok = mouse_pressed (but, &x, &y);
3073 else
3074 ok = mouse_released (but, &x, &y);
3075 if (ok)
3076 {
3077 /* Allow a simultaneous press/release of Mouse-1 and
3078 Mouse-2 to simulate Mouse-3 on two-button mice. */
3079 if (mouse_button_count == 2 && but < 2)
3080 {
3081 int x2, y2; /* don't clobber original coordinates */
3082
3083 /* If only one button is pressed, wait 100 msec and
3084 check again. This way, Speedy Gonzales isn't
3085 punished, while the slow get their chance. */
3086 if (press && mouse_pressed (1-but, &x2, &y2)
3087 || !press && mouse_released (1-but, &x2, &y2))
3088 button_num = 2;
3089 else
3090 {
3091 delay (100);
3092 if (press && mouse_pressed (1-but, &x2, &y2)
3093 || !press && mouse_released (1-but, &x2, &y2))
3094 button_num = 2;
3095 }
3096 }
3097
3098 event.kind = mouse_click;
3099 event.code = button_num;
3100 event.modifiers = dos_get_modifiers (0)
3101 | (press ? down_modifier : up_modifier);
3102 event.x = x;
3103 event.y = y;
3104 event.frame_or_window = selected_frame;
3105 event.timestamp = event_timestamp ();
3106 kbd_buffer_store_event (&event);
3107 }
3108 }
3109 }
3110
3111 return -1;
3112 }
3113
3114 static int prev_get_char = -1;
3115
3116 /* Return 1 if a key is ready to be read without suspending execution. */
3117
3118 dos_keysns ()
3119 {
3120 if (prev_get_char != -1)
3121 return 1;
3122 else
3123 return ((prev_get_char = dos_rawgetc ()) != -1);
3124 }
3125
3126 /* Read a key. Return -1 if no key is ready. */
3127
3128 dos_keyread ()
3129 {
3130 if (prev_get_char != -1)
3131 {
3132 int c = prev_get_char;
3133 prev_get_char = -1;
3134 return c;
3135 }
3136 else
3137 return dos_rawgetc ();
3138 }
3139 \f
3140 #ifndef HAVE_X_WINDOWS
3141 /* See xterm.c for more info. */
3142 void
3143 pixel_to_glyph_coords (f, pix_x, pix_y, x, y, bounds, noclip)
3144 FRAME_PTR f;
3145 register int pix_x, pix_y;
3146 register int *x, *y;
3147 XRectangle *bounds;
3148 int noclip;
3149 {
3150 if (bounds) abort ();
3151
3152 /* Ignore clipping. */
3153
3154 *x = pix_x;
3155 *y = pix_y;
3156 }
3157
3158 void
3159 glyph_to_pixel_coords (f, x, y, pix_x, pix_y)
3160 FRAME_PTR f;
3161 register int x, y;
3162 register int *pix_x, *pix_y;
3163 {
3164 *pix_x = x;
3165 *pix_y = y;
3166 }
3167 \f
3168 /* Simulation of X's menus. Nothing too fancy here -- just make it work
3169 for now.
3170
3171 Actually, I don't know the meaning of all the parameters of the functions
3172 here -- I only know how they are called by xmenu.c. I could of course
3173 grab the nearest Xlib manual (down the hall, second-to-last door on the
3174 left), but I don't think it's worth the effort. */
3175
3176 static char *menu_help_message, *prev_menu_help_message;
3177
3178 static XMenu *
3179 IT_menu_create ()
3180 {
3181 XMenu *menu;
3182
3183 menu = (XMenu *) xmalloc (sizeof (XMenu));
3184 menu->allocated = menu->count = menu->panecount = menu->width = 0;
3185 return menu;
3186 }
3187
3188 /* Allocate some (more) memory for MENU ensuring that there is room for one
3189 for item. */
3190
3191 static void
3192 IT_menu_make_room (XMenu *menu)
3193 {
3194 if (menu->allocated == 0)
3195 {
3196 int count = menu->allocated = 10;
3197 menu->text = (char **) xmalloc (count * sizeof (char *));
3198 menu->submenu = (XMenu **) xmalloc (count * sizeof (XMenu *));
3199 menu->panenumber = (int *) xmalloc (count * sizeof (int));
3200 menu->help_text = (char **) xmalloc (count * sizeof (char *));
3201 }
3202 else if (menu->allocated == menu->count)
3203 {
3204 int count = menu->allocated = menu->allocated + 10;
3205 menu->text
3206 = (char **) xrealloc (menu->text, count * sizeof (char *));
3207 menu->submenu
3208 = (XMenu **) xrealloc (menu->submenu, count * sizeof (XMenu *));
3209 menu->panenumber
3210 = (int *) xrealloc (menu->panenumber, count * sizeof (int));
3211 menu->help_text
3212 = (char **) xrealloc (menu->help_text, count * sizeof (char *));
3213 }
3214 }
3215
3216 /* Search the given menu structure for a given pane number. */
3217
3218 static XMenu *
3219 IT_menu_search_pane (XMenu *menu, int pane)
3220 {
3221 int i;
3222 XMenu *try;
3223
3224 for (i = 0; i < menu->count; i++)
3225 if (menu->submenu[i])
3226 {
3227 if (pane == menu->panenumber[i])
3228 return menu->submenu[i];
3229 if ((try = IT_menu_search_pane (menu->submenu[i], pane)))
3230 return try;
3231 }
3232 return (XMenu *) 0;
3233 }
3234
3235 /* Determine how much screen space a given menu needs. */
3236
3237 static void
3238 IT_menu_calc_size (XMenu *menu, int *width, int *height)
3239 {
3240 int i, h2, w2, maxsubwidth, maxheight;
3241
3242 maxsubwidth = 0;
3243 maxheight = menu->count;
3244 for (i = 0; i < menu->count; i++)
3245 {
3246 if (menu->submenu[i])
3247 {
3248 IT_menu_calc_size (menu->submenu[i], &w2, &h2);
3249 if (w2 > maxsubwidth) maxsubwidth = w2;
3250 if (i + h2 > maxheight) maxheight = i + h2;
3251 }
3252 }
3253 *width = menu->width + maxsubwidth;
3254 *height = maxheight;
3255 }
3256
3257 /* Display MENU at (X,Y) using FACES. */
3258
3259 static void
3260 IT_menu_display (XMenu *menu, int y, int x, int *faces, int disp_help)
3261 {
3262 int i, j, face, width;
3263 struct glyph *text, *p;
3264 char *q;
3265 int mx, my;
3266 int enabled, mousehere;
3267 int row, col;
3268 struct frame *sf = SELECTED_FRAME();
3269
3270 menu_help_message = NULL;
3271
3272 width = menu->width;
3273 text = (struct glyph *) xmalloc ((width + 2) * sizeof (struct glyph));
3274 ScreenGetCursor (&row, &col);
3275 mouse_get_xy (&mx, &my);
3276 IT_update_begin (sf);
3277 for (i = 0; i < menu->count; i++)
3278 {
3279 int max_width = width + 2;
3280
3281 IT_cursor_to (y + i, x);
3282 enabled
3283 = (!menu->submenu[i] && menu->panenumber[i]) || (menu->submenu[i]);
3284 mousehere = (y + i == my && x <= mx && mx < x + width + 2);
3285 face = faces[enabled + mousehere * 2];
3286 if (disp_help && enabled + mousehere * 2 >= 2)
3287 menu_help_message = menu->help_text[i];
3288 p = text;
3289 SET_CHAR_GLYPH (*p, ' ', face, 0);
3290 p++;
3291 for (j = 0, q = menu->text[i]; *q; j++)
3292 {
3293 if (*q > 26)
3294 {
3295 SET_CHAR_GLYPH (*p, *q++, face, 0);
3296 p++;
3297 }
3298 else /* make '^x' */
3299 {
3300 SET_CHAR_GLYPH (*p, '^', face, 0);
3301 p++;
3302 j++;
3303 SET_CHAR_GLYPH (*p, *q++ + 64, face, 0);
3304 p++;
3305 }
3306 }
3307 /* Don't let the menu text overflow into the next screen row. */
3308 if (x + max_width > screen_size_X)
3309 {
3310 max_width = screen_size_X - x;
3311 text[max_width - 1].u.ch = '$'; /* indicate it's truncated */
3312 }
3313 for (; j < max_width - 2; j++, p++)
3314 SET_CHAR_GLYPH (*p, ' ', face, 0);
3315
3316 SET_CHAR_GLYPH (*p, menu->submenu[i] ? 16 : ' ', face, 0);
3317 p++;
3318 IT_write_glyphs (text, max_width);
3319 }
3320 IT_update_end (sf);
3321 IT_cursor_to (row, col);
3322 xfree (text);
3323 }
3324 \f
3325 /* --------------------------- X Menu emulation ---------------------- */
3326
3327 /* Report availability of menus. */
3328
3329 int
3330 have_menus_p ()
3331 {
3332 return 1;
3333 }
3334
3335 /* Create a brand new menu structure. */
3336
3337 XMenu *
3338 XMenuCreate (Display *foo1, Window foo2, char *foo3)
3339 {
3340 return IT_menu_create ();
3341 }
3342
3343 /* Create a new pane and place it on the outer-most level. It is not
3344 clear that it should be placed out there, but I don't know what else
3345 to do. */
3346
3347 int
3348 XMenuAddPane (Display *foo, XMenu *menu, char *txt, int enable)
3349 {
3350 int len;
3351 char *p;
3352
3353 if (!enable)
3354 abort ();
3355
3356 IT_menu_make_room (menu);
3357 menu->submenu[menu->count] = IT_menu_create ();
3358 menu->text[menu->count] = txt;
3359 menu->panenumber[menu->count] = ++menu->panecount;
3360 menu->help_text[menu->count] = NULL;
3361 menu->count++;
3362
3363 /* Adjust length for possible control characters (which will
3364 be written as ^x). */
3365 for (len = strlen (txt), p = txt; *p; p++)
3366 if (*p < 27)
3367 len++;
3368
3369 if (len > menu->width)
3370 menu->width = len;
3371
3372 return menu->panecount;
3373 }
3374
3375 /* Create a new item in a menu pane. */
3376
3377 int
3378 XMenuAddSelection (Display *bar, XMenu *menu, int pane,
3379 int foo, char *txt, int enable, char *help_text)
3380 {
3381 int len;
3382 char *p;
3383
3384 if (pane)
3385 if (!(menu = IT_menu_search_pane (menu, pane)))
3386 return XM_FAILURE;
3387 IT_menu_make_room (menu);
3388 menu->submenu[menu->count] = (XMenu *) 0;
3389 menu->text[menu->count] = txt;
3390 menu->panenumber[menu->count] = enable;
3391 menu->help_text[menu->count] = help_text;
3392 menu->count++;
3393
3394 /* Adjust length for possible control characters (which will
3395 be written as ^x). */
3396 for (len = strlen (txt), p = txt; *p; p++)
3397 if (*p < 27)
3398 len++;
3399
3400 if (len > menu->width)
3401 menu->width = len;
3402
3403 return XM_SUCCESS;
3404 }
3405
3406 /* Decide where the menu would be placed if requested at (X,Y). */
3407
3408 void
3409 XMenuLocate (Display *foo0, XMenu *menu, int foo1, int foo2, int x, int y,
3410 int *ulx, int *uly, int *width, int *height)
3411 {
3412 IT_menu_calc_size (menu, width, height);
3413 *ulx = x + 1;
3414 *uly = y;
3415 *width += 2;
3416 }
3417
3418 struct IT_menu_state
3419 {
3420 void *screen_behind;
3421 XMenu *menu;
3422 int pane;
3423 int x, y;
3424 };
3425
3426
3427 /* Display menu, wait for user's response, and return that response. */
3428
3429 int
3430 XMenuActivate (Display *foo, XMenu *menu, int *pane, int *selidx,
3431 int x0, int y0, unsigned ButtonMask, char **txt,
3432 void (*help_callback)(char *))
3433 {
3434 struct IT_menu_state *state;
3435 int statecount;
3436 int x, y, i, b;
3437 int screensize;
3438 int faces[4];
3439 Lisp_Object selectface;
3440 int leave, result, onepane;
3441 int title_faces[4]; /* face to display the menu title */
3442 int buffers_num_deleted = 0;
3443 struct frame *sf = SELECTED_FRAME();
3444
3445 /* Just in case we got here without a mouse present... */
3446 if (have_mouse <= 0)
3447 return XM_IA_SELECT;
3448 /* Don't allow non-positive x0 and y0, lest the menu will wrap
3449 around the display. */
3450 if (x0 <= 0)
3451 x0 = 1;
3452 if (y0 <= 0)
3453 y0 = 1;
3454
3455 /* We will process all the mouse events directly, so we had
3456 better prevented dos_rawgetc from stealing them from us. */
3457 mouse_preempted++;
3458
3459 state = alloca (menu->panecount * sizeof (struct IT_menu_state));
3460 screensize = screen_size * 2;
3461 faces[0]
3462 = lookup_derived_face (sf, intern ("msdos-menu-passive-face"),
3463 CHARSET_ASCII, DEFAULT_FACE_ID);
3464 faces[1]
3465 = lookup_derived_face (sf, intern ("msdos-menu-active-face"),
3466 CHARSET_ASCII, DEFAULT_FACE_ID);
3467 selectface = intern ("msdos-menu-select-face");
3468 faces[2] = lookup_derived_face (sf, selectface,
3469 CHARSET_ASCII, faces[0]);
3470 faces[3] = lookup_derived_face (sf, selectface,
3471 CHARSET_ASCII, faces[1]);
3472
3473 /* Make sure the menu title is always displayed with
3474 `msdos-menu-active-face', no matter where the mouse pointer is. */
3475 for (i = 0; i < 4; i++)
3476 title_faces[i] = faces[3];
3477
3478 statecount = 1;
3479
3480 /* Don't let the title for the "Buffers" popup menu include a
3481 digit (which is ugly).
3482
3483 This is a terrible kludge, but I think the "Buffers" case is
3484 the only one where the title includes a number, so it doesn't
3485 seem to be necessary to make this more general. */
3486 if (strncmp (menu->text[0], "Buffers 1", 9) == 0)
3487 {
3488 menu->text[0][7] = '\0';
3489 buffers_num_deleted = 1;
3490 }
3491 state[0].menu = menu;
3492 mouse_off ();
3493 ScreenRetrieve (state[0].screen_behind = xmalloc (screensize));
3494
3495 /* Turn off the cursor. Otherwise it shows through the menu
3496 panes, which is ugly. */
3497 IT_display_cursor (0);
3498
3499 /* Display the menu title. */
3500 IT_menu_display (menu, y0 - 1, x0 - 1, title_faces, 0);
3501 if (buffers_num_deleted)
3502 menu->text[0][7] = ' ';
3503 if ((onepane = menu->count == 1 && menu->submenu[0]))
3504 {
3505 menu->width = menu->submenu[0]->width;
3506 state[0].menu = menu->submenu[0];
3507 }
3508 else
3509 {
3510 state[0].menu = menu;
3511 }
3512 state[0].x = x0 - 1;
3513 state[0].y = y0;
3514 state[0].pane = onepane;
3515
3516 mouse_last_x = -1; /* A hack that forces display. */
3517 leave = 0;
3518 while (!leave)
3519 {
3520 if (!mouse_visible) mouse_on ();
3521 mouse_check_moved ();
3522 if (sf->mouse_moved)
3523 {
3524 sf->mouse_moved = 0;
3525 result = XM_IA_SELECT;
3526 mouse_get_xy (&x, &y);
3527 for (i = 0; i < statecount; i++)
3528 if (state[i].x <= x && x < state[i].x + state[i].menu->width + 2)
3529 {
3530 int dy = y - state[i].y;
3531 if (0 <= dy && dy < state[i].menu->count)
3532 {
3533 if (!state[i].menu->submenu[dy])
3534 if (state[i].menu->panenumber[dy])
3535 result = XM_SUCCESS;
3536 else
3537 result = XM_IA_SELECT;
3538 *pane = state[i].pane - 1;
3539 *selidx = dy;
3540 /* We hit some part of a menu, so drop extra menus that
3541 have been opened. That does not include an open and
3542 active submenu. */
3543 if (i != statecount - 2
3544 || state[i].menu->submenu[dy] != state[i+1].menu)
3545 while (i != statecount - 1)
3546 {
3547 statecount--;
3548 mouse_off ();
3549 ScreenUpdate (state[statecount].screen_behind);
3550 if (screen_virtual_segment)
3551 dosv_refresh_virtual_screen (0, screen_size);
3552 xfree (state[statecount].screen_behind);
3553 }
3554 if (i == statecount - 1 && state[i].menu->submenu[dy])
3555 {
3556 IT_menu_display (state[i].menu,
3557 state[i].y,
3558 state[i].x,
3559 faces, 1);
3560 state[statecount].menu = state[i].menu->submenu[dy];
3561 state[statecount].pane = state[i].menu->panenumber[dy];
3562 mouse_off ();
3563 ScreenRetrieve (state[statecount].screen_behind
3564 = xmalloc (screensize));
3565 state[statecount].x
3566 = state[i].x + state[i].menu->width + 2;
3567 state[statecount].y = y;
3568 statecount++;
3569 }
3570 }
3571 }
3572 IT_menu_display (state[statecount - 1].menu,
3573 state[statecount - 1].y,
3574 state[statecount - 1].x,
3575 faces, 1);
3576 }
3577 else
3578 {
3579 if ((menu_help_message || prev_menu_help_message)
3580 && menu_help_message != prev_menu_help_message)
3581 {
3582 help_callback (menu_help_message);
3583 IT_display_cursor (0);
3584 prev_menu_help_message = menu_help_message;
3585 }
3586 /* We are busy-waiting for the mouse to move, so let's be nice
3587 to other Windows applications by releasing our time slice. */
3588 __dpmi_yield ();
3589 }
3590 for (b = 0; b < mouse_button_count && !leave; b++)
3591 {
3592 /* Only leave if user both pressed and released the mouse, and in
3593 that order. This avoids popping down the menu pane unless
3594 the user is really done with it. */
3595 if (mouse_pressed (b, &x, &y))
3596 {
3597 while (mouse_button_depressed (b, &x, &y))
3598 __dpmi_yield ();
3599 leave = 1;
3600 }
3601 (void) mouse_released (b, &x, &y);
3602 }
3603 }
3604
3605 mouse_off ();
3606 ScreenUpdate (state[0].screen_behind);
3607 if (screen_virtual_segment)
3608 dosv_refresh_virtual_screen (0, screen_size);
3609 message (0);
3610 while (statecount--)
3611 xfree (state[statecount].screen_behind);
3612 IT_display_cursor (1); /* turn cursor back on */
3613 /* Clean up any mouse events that are waiting inside Emacs event queue.
3614 These events are likely to be generated before the menu was even
3615 displayed, probably because the user pressed and released the button
3616 (which invoked the menu) too quickly. If we don't remove these events,
3617 Emacs will process them after we return and surprise the user. */
3618 discard_mouse_events ();
3619 /* Allow mouse events generation by dos_rawgetc. */
3620 mouse_preempted--;
3621 return result;
3622 }
3623
3624 /* Dispose of a menu. */
3625
3626 void
3627 XMenuDestroy (Display *foo, XMenu *menu)
3628 {
3629 int i;
3630 if (menu->allocated)
3631 {
3632 for (i = 0; i < menu->count; i++)
3633 if (menu->submenu[i])
3634 XMenuDestroy (foo, menu->submenu[i]);
3635 xfree (menu->text);
3636 xfree (menu->submenu);
3637 xfree (menu->panenumber);
3638 xfree (menu->help_text);
3639 }
3640 xfree (menu);
3641 menu_help_message = prev_menu_help_message = NULL;
3642 }
3643
3644 int
3645 x_pixel_width (struct frame *f)
3646 {
3647 return FRAME_WIDTH (f);
3648 }
3649
3650 int
3651 x_pixel_height (struct frame *f)
3652 {
3653 return FRAME_HEIGHT (f);
3654 }
3655 #endif /* !HAVE_X_WINDOWS */
3656 \f
3657 /* ----------------------- DOS / UNIX conversion --------------------- */
3658
3659 void msdos_downcase_filename (unsigned char *);
3660
3661 /* Destructively turn backslashes into slashes. */
3662
3663 void
3664 dostounix_filename (p)
3665 register char *p;
3666 {
3667 msdos_downcase_filename (p);
3668
3669 while (*p)
3670 {
3671 if (*p == '\\')
3672 *p = '/';
3673 p++;
3674 }
3675 }
3676
3677 /* Destructively turn slashes into backslashes. */
3678
3679 void
3680 unixtodos_filename (p)
3681 register char *p;
3682 {
3683 if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
3684 {
3685 *p += 'a' - 'A';
3686 p += 2;
3687 }
3688
3689 while (*p)
3690 {
3691 if (*p == '/')
3692 *p = '\\';
3693 p++;
3694 }
3695 }
3696
3697 /* Get the default directory for a given drive. 0=def, 1=A, 2=B, ... */
3698
3699 int
3700 getdefdir (drive, dst)
3701 int drive;
3702 char *dst;
3703 {
3704 char in_path[4], *p = in_path;
3705 int e = errno;
3706
3707 /* Generate "X:." (when drive is X) or "." (when drive is 0). */
3708 if (drive != 0)
3709 {
3710 *p++ = drive + 'A' - 1;
3711 *p++ = ':';
3712 }
3713
3714 *p++ = '.';
3715 *p = '\0';
3716 errno = 0;
3717 _fixpath (in_path, dst);
3718 /* _fixpath can set errno to ENOSYS on non-LFN systems because
3719 it queries the LFN support, so ignore that error. */
3720 if ((errno && errno != ENOSYS) || *dst == '\0')
3721 return 0;
3722
3723 msdos_downcase_filename (dst);
3724
3725 errno = e;
3726 return 1;
3727 }
3728
3729 /* Remove all CR's that are followed by a LF. */
3730
3731 int
3732 crlf_to_lf (n, buf)
3733 register int n;
3734 register unsigned char *buf;
3735 {
3736 unsigned char *np = buf;
3737 unsigned char *startp = buf;
3738 unsigned char *endp = buf + n;
3739
3740 if (n == 0)
3741 return n;
3742 while (buf < endp - 1)
3743 {
3744 if (*buf == 0x0d)
3745 {
3746 if (*(++buf) != 0x0a)
3747 *np++ = 0x0d;
3748 }
3749 else
3750 *np++ = *buf++;
3751 }
3752 if (buf < endp)
3753 *np++ = *buf++;
3754 return np - startp;
3755 }
3756
3757 #if defined(__DJGPP__) && __DJGPP__ == 2 && __DJGPP_MINOR__ == 0
3758
3759 /* In DJGPP v2.0, library `write' can call `malloc', which might
3760 cause relocation of the buffer whose address we get in ADDR.
3761 Here is a version of `write' that avoids calling `malloc',
3762 to serve us until such time as the library is fixed.
3763 Actually, what we define here is called `__write', because
3764 `write' is a stub that just jmp's to `__write' (to be
3765 POSIXLY-correct with respect to the global name-space). */
3766
3767 #include <io.h> /* for _write */
3768 #include <libc/dosio.h> /* for __file_handle_modes[] */
3769
3770 static char xbuf[64 * 1024]; /* DOS cannot write more in one chunk */
3771
3772 #define XBUF_END (xbuf + sizeof (xbuf) - 1)
3773
3774 int
3775 __write (int handle, const void *buffer, size_t count)
3776 {
3777 if (count == 0)
3778 return 0;
3779
3780 if(__file_handle_modes[handle] & O_BINARY)
3781 return _write (handle, buffer, count);
3782 else
3783 {
3784 char *xbp = xbuf;
3785 const char *bp = buffer;
3786 int total_written = 0;
3787 int nmoved = 0, ncr = 0;
3788
3789 while (count)
3790 {
3791 /* The next test makes sure there's space for at least 2 more
3792 characters in xbuf[], so both CR and LF can be put there. */
3793 if (xbp < XBUF_END)
3794 {
3795 if (*bp == '\n')
3796 {
3797 ncr++;
3798 *xbp++ = '\r';
3799 }
3800 *xbp++ = *bp++;
3801 nmoved++;
3802 count--;
3803 }
3804 if (xbp >= XBUF_END || !count)
3805 {
3806 size_t to_write = nmoved + ncr;
3807 int written = _write (handle, xbuf, to_write);
3808
3809 if (written == -1)
3810 return -1;
3811 else
3812 total_written += nmoved; /* CRs aren't counted in ret value */
3813
3814 /* If some, but not all were written (disk full?), return
3815 an estimate of the total written bytes not counting CRs. */
3816 if (written < to_write)
3817 return total_written - (to_write - written) * nmoved/to_write;
3818
3819 nmoved = 0;
3820 ncr = 0;
3821 xbp = xbuf;
3822 }
3823 }
3824 return total_written;
3825 }
3826 }
3827
3828 /* A low-level file-renaming function which works around Windows 95 bug.
3829 This is pulled directly out of DJGPP v2.01 library sources, and only
3830 used when you compile with DJGPP v2.0. */
3831
3832 #include <io.h>
3833
3834 int _rename(const char *old, const char *new)
3835 {
3836 __dpmi_regs r;
3837 int olen = strlen(old) + 1;
3838 int i;
3839 int use_lfn = _USE_LFN;
3840 char tempfile[FILENAME_MAX];
3841 const char *orig = old;
3842 int lfn_fd = -1;
3843
3844 r.x.dx = __tb_offset;
3845 r.x.di = __tb_offset + olen;
3846 r.x.ds = r.x.es = __tb_segment;
3847
3848 if (use_lfn)
3849 {
3850 /* Windows 95 bug: for some filenames, when you rename
3851 file -> file~ (as in Emacs, to leave a backup), the
3852 short 8+3 alias doesn't change, which effectively
3853 makes OLD and NEW the same file. We must rename
3854 through a temporary file to work around this. */
3855
3856 char *pbase = 0, *p;
3857 static char try_char[] = "abcdefghijklmnopqrstuvwxyz012345789";
3858 int idx = sizeof(try_char) - 1;
3859
3860 /* Generate a temporary name. Can't use `tmpnam', since $TMPDIR
3861 might point to another drive, which will fail the DOS call. */
3862 strcpy(tempfile, old);
3863 for (p = tempfile; *p; p++) /* ensure temporary is on the same drive */
3864 if (*p == '/' || *p == '\\' || *p == ':')
3865 pbase = p;
3866 if (pbase)
3867 pbase++;
3868 else
3869 pbase = tempfile;
3870 strcpy(pbase, "X$$djren$$.$$temp$$");
3871
3872 do
3873 {
3874 if (idx <= 0)
3875 return -1;
3876 *pbase = try_char[--idx];
3877 } while (_chmod(tempfile, 0) != -1);
3878
3879 r.x.ax = 0x7156;
3880 _put_path2(tempfile, olen);
3881 _put_path(old);
3882 __dpmi_int(0x21, &r);
3883 if (r.x.flags & 1)
3884 {
3885 errno = __doserr_to_errno(r.x.ax);
3886 return -1;
3887 }
3888
3889 /* Now create a file with the original name. This will
3890 ensure that NEW will always have a 8+3 alias
3891 different from that of OLD. (Seems to be required
3892 when NameNumericTail in the Registry is set to 0.) */
3893 lfn_fd = _creat(old, 0);
3894
3895 olen = strlen(tempfile) + 1;
3896 old = tempfile;
3897 r.x.di = __tb_offset + olen;
3898 }
3899
3900 for (i=0; i<2; i++)
3901 {
3902 if(use_lfn)
3903 r.x.ax = 0x7156;
3904 else
3905 r.h.ah = 0x56;
3906 _put_path2(new, olen);
3907 _put_path(old);
3908 __dpmi_int(0x21, &r);
3909 if(r.x.flags & 1)
3910 {
3911 if (r.x.ax == 5 && i == 0) /* access denied */
3912 remove(new); /* and try again */
3913 else
3914 {
3915 errno = __doserr_to_errno(r.x.ax);
3916
3917 /* Restore to original name if we renamed it to temporary. */
3918 if (use_lfn)
3919 {
3920 if (lfn_fd != -1)
3921 {
3922 _close (lfn_fd);
3923 remove (orig);
3924 }
3925 _put_path2(orig, olen);
3926 _put_path(tempfile);
3927 r.x.ax = 0x7156;
3928 __dpmi_int(0x21, &r);
3929 }
3930 return -1;
3931 }
3932 }
3933 else
3934 break;
3935 }
3936
3937 /* Success. Delete the file possibly created to work
3938 around the Windows 95 bug. */
3939 if (lfn_fd != -1)
3940 return (_close (lfn_fd) == 0) ? remove (orig) : -1;
3941 return 0;
3942 }
3943
3944 #endif /* __DJGPP__ == 2 && __DJGPP_MINOR__ == 0 */
3945
3946 DEFUN ("msdos-long-file-names", Fmsdos_long_file_names, Smsdos_long_file_names,
3947 0, 0, 0,
3948 "Return non-nil if long file names are supported on MSDOS.")
3949 ()
3950 {
3951 return (_USE_LFN ? Qt : Qnil);
3952 }
3953
3954 /* Convert alphabetic characters in a filename to lower-case. */
3955
3956 void
3957 msdos_downcase_filename (p)
3958 register unsigned char *p;
3959 {
3960 /* Always lower-case drive letters a-z, even if the filesystem
3961 preserves case in filenames.
3962 This is so MSDOS filenames could be compared by string comparison
3963 functions that are case-sensitive. Even case-preserving filesystems
3964 do not distinguish case in drive letters. */
3965 if (p[1] == ':' && *p >= 'A' && *p <= 'Z')
3966 {
3967 *p += 'a' - 'A';
3968 p += 2;
3969 }
3970
3971 /* Under LFN we expect to get pathnames in their true case. */
3972 if (NILP (Fmsdos_long_file_names ()))
3973 for ( ; *p; p++)
3974 if (*p >= 'A' && *p <= 'Z')
3975 *p += 'a' - 'A';
3976 }
3977
3978 DEFUN ("msdos-downcase-filename", Fmsdos_downcase_filename, Smsdos_downcase_filename,
3979 1, 1, 0,
3980 "Convert alphabetic characters in FILENAME to lower case and return that.\n\
3981 When long filenames are supported, doesn't change FILENAME.\n\
3982 If FILENAME is not a string, returns nil.\n\
3983 The argument object is never altered--the value is a copy.")
3984 (filename)
3985 Lisp_Object filename;
3986 {
3987 Lisp_Object tem;
3988
3989 if (! STRINGP (filename))
3990 return Qnil;
3991
3992 tem = Fcopy_sequence (filename);
3993 msdos_downcase_filename (XSTRING (tem)->data);
3994 return tem;
3995 }
3996 \f
3997 /* The Emacs root directory as determined by init_environment. */
3998
3999 static char emacsroot[MAXPATHLEN];
4000
4001 char *
4002 rootrelativepath (rel)
4003 char *rel;
4004 {
4005 static char result[MAXPATHLEN + 10];
4006
4007 strcpy (result, emacsroot);
4008 strcat (result, "/");
4009 strcat (result, rel);
4010 return result;
4011 }
4012
4013 /* Define a lot of environment variables if not already defined. Don't
4014 remove anything unless you know what you're doing -- lots of code will
4015 break if one or more of these are missing. */
4016
4017 void
4018 init_environment (argc, argv, skip_args)
4019 int argc;
4020 char **argv;
4021 int skip_args;
4022 {
4023 char *s, *t, *root;
4024 int len;
4025 static const char * const tempdirs[] = {
4026 "$TMPDIR", "$TEMP", "$TMP", "c:/"
4027 };
4028 int i;
4029 const int imax = sizeof (tempdirs) / sizeof (tempdirs[0]);
4030
4031 /* Make sure they have a usable $TMPDIR. Many Emacs functions use
4032 temporary files and assume "/tmp" if $TMPDIR is unset, which
4033 will break on DOS/Windows. Refuse to work if we cannot find
4034 a directory, not even "c:/", usable for that purpose. */
4035 for (i = 0; i < imax ; i++)
4036 {
4037 const char *tmp = tempdirs[i];
4038
4039 if (*tmp == '$')
4040 tmp = getenv (tmp + 1);
4041 /* Note that `access' can lie to us if the directory resides on a
4042 read-only filesystem, like CD-ROM or a write-protected floppy.
4043 The only way to be really sure is to actually create a file and
4044 see if it succeeds. But I think that's too much to ask. */
4045 if (tmp && access (tmp, D_OK) == 0)
4046 {
4047 setenv ("TMPDIR", tmp, 1);
4048 break;
4049 }
4050 }
4051 if (i >= imax)
4052 cmd_error_internal
4053 (Fcons (Qerror,
4054 Fcons (build_string ("no usable temporary directories found!!"),
4055 Qnil)),
4056 "While setting TMPDIR: ");
4057
4058 /* Note the startup time, so we know not to clear the screen if we
4059 exit immediately; see IT_reset_terminal_modes.
4060 (Yes, I know `clock' returns zero the first time it's called, but
4061 I do this anyway, in case some wiseguy changes that at some point.) */
4062 startup_time = clock ();
4063
4064 /* Find our root from argv[0]. Assuming argv[0] is, say,
4065 "c:/emacs/bin/emacs.exe" our root will be "c:/emacs". */
4066 root = alloca (MAXPATHLEN + 20);
4067 _fixpath (argv[0], root);
4068 msdos_downcase_filename (root);
4069 len = strlen (root);
4070 while (len > 0 && root[len] != '/' && root[len] != ':')
4071 len--;
4072 root[len] = '\0';
4073 if (len > 4
4074 && (strcmp (root + len - 4, "/bin") == 0
4075 || strcmp (root + len - 4, "/src") == 0)) /* under a debugger */
4076 root[len - 4] = '\0';
4077 else
4078 strcpy (root, "c:/emacs"); /* let's be defensive */
4079 len = strlen (root);
4080 strcpy (emacsroot, root);
4081
4082 /* We default HOME to our root. */
4083 setenv ("HOME", root, 0);
4084
4085 /* We default EMACSPATH to root + "/bin". */
4086 strcpy (root + len, "/bin");
4087 setenv ("EMACSPATH", root, 0);
4088
4089 /* I don't expect anybody to ever use other terminals so the internal
4090 terminal is the default. */
4091 setenv ("TERM", "internal", 0);
4092
4093 #ifdef HAVE_X_WINDOWS
4094 /* Emacs expects DISPLAY to be set. */
4095 setenv ("DISPLAY", "unix:0.0", 0);
4096 #endif
4097
4098 /* SHELL is a bit tricky -- COMSPEC is the closest we come, but we must
4099 downcase it and mirror the backslashes. */
4100 s = getenv ("COMSPEC");
4101 if (!s) s = "c:/command.com";
4102 t = alloca (strlen (s) + 1);
4103 strcpy (t, s);
4104 dostounix_filename (t);
4105 setenv ("SHELL", t, 0);
4106
4107 /* PATH is also downcased and backslashes mirrored. */
4108 s = getenv ("PATH");
4109 if (!s) s = "";
4110 t = alloca (strlen (s) + 3);
4111 /* Current directory is always considered part of MsDos's path but it is
4112 not normally mentioned. Now it is. */
4113 strcat (strcpy (t, ".;"), s);
4114 dostounix_filename (t); /* Not a single file name, but this should work. */
4115 setenv ("PATH", t, 1);
4116
4117 /* In some sense all dos users have root privileges, so... */
4118 setenv ("USER", "root", 0);
4119 setenv ("NAME", getenv ("USER"), 0);
4120
4121 /* Time zone determined from country code. To make this possible, the
4122 country code may not span more than one time zone. In other words,
4123 in the USA, you lose. */
4124 if (!getenv ("TZ"))
4125 switch (dos_country_code)
4126 {
4127 case 31: /* Belgium */
4128 case 32: /* The Netherlands */
4129 case 33: /* France */
4130 case 34: /* Spain */
4131 case 36: /* Hungary */
4132 case 38: /* Yugoslavia (or what's left of it?) */
4133 case 39: /* Italy */
4134 case 41: /* Switzerland */
4135 case 42: /* Tjekia */
4136 case 45: /* Denmark */
4137 case 46: /* Sweden */
4138 case 47: /* Norway */
4139 case 48: /* Poland */
4140 case 49: /* Germany */
4141 /* Daylight saving from last Sunday in March to last Sunday in
4142 September, both at 2AM. */
4143 setenv ("TZ", "MET-01METDST-02,M3.5.0/02:00,M9.5.0/02:00", 0);
4144 break;
4145 case 44: /* United Kingdom */
4146 case 351: /* Portugal */
4147 case 354: /* Iceland */
4148 setenv ("TZ", "GMT+00", 0);
4149 break;
4150 case 81: /* Japan */
4151 case 82: /* Korea */
4152 setenv ("TZ", "JST-09", 0);
4153 break;
4154 case 90: /* Turkey */
4155 case 358: /* Finland */
4156 setenv ("TZ", "EET-02", 0);
4157 break;
4158 case 972: /* Israel */
4159 /* This is an approximation. (For exact rules, use the
4160 `zoneinfo/israel' file which comes with DJGPP, but you need
4161 to install it in `/usr/share/zoneinfo/' directory first.) */
4162 setenv ("TZ", "IST-02IDT-03,M4.1.6/00:00,M9.5.6/01:00", 0);
4163 break;
4164 }
4165 tzset ();
4166 }
4167
4168 \f
4169
4170 static int break_stat; /* BREAK check mode status. */
4171 static int stdin_stat; /* stdin IOCTL status. */
4172
4173 #if __DJGPP__ < 2
4174
4175 /* These must be global. */
4176 static _go32_dpmi_seginfo ctrl_break_vector;
4177 static _go32_dpmi_registers ctrl_break_regs;
4178 static int ctrlbreakinstalled = 0;
4179
4180 /* Interrupt level detection of Ctrl-Break. Don't do anything fancy here! */
4181
4182 void
4183 ctrl_break_func (regs)
4184 _go32_dpmi_registers *regs;
4185 {
4186 Vquit_flag = Qt;
4187 }
4188
4189 void
4190 install_ctrl_break_check ()
4191 {
4192 if (!ctrlbreakinstalled)
4193 {
4194 /* Don't press Ctrl-Break if you don't have either DPMI or Emacs
4195 was compiler with Djgpp 1.11 maintenance level 5 or later! */
4196 ctrlbreakinstalled = 1;
4197 ctrl_break_vector.pm_offset = (int) ctrl_break_func;
4198 _go32_dpmi_allocate_real_mode_callback_iret (&ctrl_break_vector,
4199 &ctrl_break_regs);
4200 _go32_dpmi_set_real_mode_interrupt_vector (0x1b, &ctrl_break_vector);
4201 }
4202 }
4203
4204 #endif /* __DJGPP__ < 2 */
4205
4206 /* Turn off Dos' Ctrl-C checking and inhibit interpretation of
4207 control chars by DOS. Determine the keyboard type. */
4208
4209 int
4210 dos_ttraw ()
4211 {
4212 union REGS inregs, outregs;
4213 static int first_time = 1;
4214
4215 break_stat = getcbrk ();
4216 setcbrk (0);
4217 #if __DJGPP__ < 2
4218 install_ctrl_break_check ();
4219 #endif
4220
4221 if (first_time)
4222 {
4223 inregs.h.ah = 0xc0;
4224 int86 (0x15, &inregs, &outregs);
4225 extended_kbd = (!outregs.x.cflag) && (outregs.h.ah == 0);
4226
4227 have_mouse = 0;
4228
4229 if (internal_terminal
4230 #ifdef HAVE_X_WINDOWS
4231 && inhibit_window_system
4232 #endif
4233 )
4234 {
4235 inregs.x.ax = 0x0021;
4236 int86 (0x33, &inregs, &outregs);
4237 have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
4238 if (!have_mouse)
4239 {
4240 /* Reportedly, the above doesn't work for some mouse drivers. There
4241 is an additional detection method that should work, but might be
4242 a little slower. Use that as an alternative. */
4243 inregs.x.ax = 0x0000;
4244 int86 (0x33, &inregs, &outregs);
4245 have_mouse = (outregs.x.ax & 0xffff) == 0xffff;
4246 }
4247
4248 if (have_mouse)
4249 {
4250 have_mouse = 1; /* enable mouse */
4251 mouse_visible = 0;
4252
4253 if (outregs.x.bx == 3)
4254 {
4255 mouse_button_count = 3;
4256 mouse_button_translate[0] = 0; /* Left */
4257 mouse_button_translate[1] = 2; /* Middle */
4258 mouse_button_translate[2] = 1; /* Right */
4259 }
4260 else
4261 {
4262 mouse_button_count = 2;
4263 mouse_button_translate[0] = 0;
4264 mouse_button_translate[1] = 1;
4265 }
4266 mouse_position_hook = &mouse_get_pos;
4267 mouse_init ();
4268 }
4269 }
4270
4271 first_time = 0;
4272
4273 #if __DJGPP__ >= 2
4274
4275 stdin_stat = setmode (fileno (stdin), O_BINARY);
4276 return (stdin_stat != -1);
4277 }
4278 else
4279 return (setmode (fileno (stdin), O_BINARY) != -1);
4280
4281 #else /* __DJGPP__ < 2 */
4282
4283 }
4284
4285 /* I think it is wrong to overwrite `stdin_stat' every time
4286 but the first one this function is called, but I don't
4287 want to change the way it used to work in v1.x.--EZ */
4288
4289 inregs.x.ax = 0x4400; /* Get IOCTL status. */
4290 inregs.x.bx = 0x00; /* 0 = stdin. */
4291 intdos (&inregs, &outregs);
4292 stdin_stat = outregs.h.dl;
4293
4294 inregs.x.dx = stdin_stat | 0x0020; /* raw mode */
4295 inregs.x.ax = 0x4401; /* Set IOCTL status */
4296 intdos (&inregs, &outregs);
4297 return !outregs.x.cflag;
4298
4299 #endif /* __DJGPP__ < 2 */
4300 }
4301
4302 /* Restore status of standard input and Ctrl-C checking. */
4303
4304 int
4305 dos_ttcooked ()
4306 {
4307 union REGS inregs, outregs;
4308
4309 setcbrk (break_stat);
4310 mouse_off ();
4311
4312 #if __DJGPP__ >= 2
4313
4314 return (setmode (fileno (stdin), stdin_stat) != -1);
4315
4316 #else /* not __DJGPP__ >= 2 */
4317
4318 inregs.x.ax = 0x4401; /* Set IOCTL status. */
4319 inregs.x.bx = 0x00; /* 0 = stdin. */
4320 inregs.x.dx = stdin_stat;
4321 intdos (&inregs, &outregs);
4322 return !outregs.x.cflag;
4323
4324 #endif /* not __DJGPP__ >= 2 */
4325 }
4326
4327 \f
4328 /* Run command as specified by ARGV in directory DIR.
4329 The command is run with input from TEMPIN, output to
4330 file TEMPOUT and stderr to TEMPERR. */
4331
4332 int
4333 run_msdos_command (argv, working_dir, tempin, tempout, temperr, envv)
4334 unsigned char **argv;
4335 const char *working_dir;
4336 int tempin, tempout, temperr;
4337 char **envv;
4338 {
4339 char *saveargv1, *saveargv2, *lowcase_argv0, *pa, *pl;
4340 char oldwd[MAXPATHLEN + 1]; /* Fixed size is safe on MSDOS. */
4341 int msshell, result = -1;
4342 int inbak, outbak, errbak;
4343 int x, y;
4344 Lisp_Object cmd;
4345
4346 /* Get current directory as MSDOS cwd is not per-process. */
4347 getwd (oldwd);
4348
4349 /* If argv[0] is the shell, it might come in any lettercase.
4350 Since `Fmember' is case-sensitive, we need to downcase
4351 argv[0], even if we are on case-preserving filesystems. */
4352 lowcase_argv0 = alloca (strlen (argv[0]) + 1);
4353 for (pa = argv[0], pl = lowcase_argv0; *pa; pl++)
4354 {
4355 *pl = *pa++;
4356 if (*pl >= 'A' && *pl <= 'Z')
4357 *pl += 'a' - 'A';
4358 }
4359 *pl = '\0';
4360
4361 cmd = Ffile_name_nondirectory (build_string (lowcase_argv0));
4362 msshell = !NILP (Fmember (cmd, Fsymbol_value (intern ("msdos-shells"))))
4363 && !strcmp ("-c", argv[1]);
4364 if (msshell)
4365 {
4366 saveargv1 = argv[1];
4367 saveargv2 = argv[2];
4368 argv[1] = "/c";
4369 if (argv[2])
4370 {
4371 char *p = alloca (strlen (argv[2]) + 1);
4372
4373 strcpy (argv[2] = p, saveargv2);
4374 while (*p && isspace (*p))
4375 p++;
4376 while (*p && !isspace (*p))
4377 if (*p == '/')
4378 *p++ = '\\';
4379 else
4380 p++;
4381 }
4382 }
4383
4384 chdir (working_dir);
4385 inbak = dup (0);
4386 outbak = dup (1);
4387 errbak = dup (2);
4388 if (inbak < 0 || outbak < 0 || errbak < 0)
4389 goto done; /* Allocation might fail due to lack of descriptors. */
4390
4391 if (have_mouse > 0)
4392 mouse_get_xy (&x, &y);
4393
4394 dos_ttcooked (); /* do it here while 0 = stdin */
4395
4396 dup2 (tempin, 0);
4397 dup2 (tempout, 1);
4398 dup2 (temperr, 2);
4399
4400 #if __DJGPP__ > 1
4401
4402 if (msshell && !argv[3])
4403 {
4404 /* MS-DOS native shells are too restrictive. For starters, they
4405 cannot grok commands longer than 126 characters. In DJGPP v2
4406 and later, `system' is much smarter, so we'll call it instead. */
4407
4408 const char *cmnd;
4409
4410 /* A shell gets a single argument--its full command
4411 line--whose original was saved in `saveargv2'. */
4412
4413 /* Don't let them pass empty command lines to `system', since
4414 with some shells it will try to invoke an interactive shell,
4415 which will hang Emacs. */
4416 for (cmnd = saveargv2; *cmnd && isspace (*cmnd); cmnd++)
4417 ;
4418 if (*cmnd)
4419 {
4420 extern char **environ;
4421 int save_system_flags = __system_flags;
4422
4423 /* Request the most powerful version of `system'. We need
4424 all the help we can get to avoid calling stock DOS shells. */
4425 __system_flags = (__system_redirect
4426 | __system_use_shell
4427 | __system_allow_multiple_cmds
4428 | __system_allow_long_cmds
4429 | __system_handle_null_commands
4430 | __system_emulate_chdir);
4431
4432 environ = envv;
4433 result = system (cmnd);
4434 __system_flags = save_system_flags;
4435 }
4436 else
4437 result = 0; /* emulate Unixy shell behavior with empty cmd line */
4438 }
4439 else
4440
4441 #endif /* __DJGPP__ > 1 */
4442
4443 result = spawnve (P_WAIT, argv[0], argv, envv);
4444
4445 dup2 (inbak, 0);
4446 dup2 (outbak, 1);
4447 dup2 (errbak, 2);
4448 emacs_close (inbak);
4449 emacs_close (outbak);
4450 emacs_close (errbak);
4451
4452 dos_ttraw ();
4453 if (have_mouse > 0)
4454 {
4455 mouse_init ();
4456 mouse_moveto (x, y);
4457 }
4458
4459 /* Some programs might change the meaning of the highest bit of the
4460 text attribute byte, so we get blinking characters instead of the
4461 bright background colors. Restore that. */
4462 bright_bg ();
4463
4464 done:
4465 chdir (oldwd);
4466 if (msshell)
4467 {
4468 argv[1] = saveargv1;
4469 argv[2] = saveargv2;
4470 }
4471 return result;
4472 }
4473
4474 croak (badfunc)
4475 char *badfunc;
4476 {
4477 fprintf (stderr, "%s not yet implemented\r\n", badfunc);
4478 reset_sys_modes ();
4479 exit (1);
4480 }
4481 \f
4482 #if __DJGPP__ < 2
4483
4484 /* ------------------------- Compatibility functions -------------------
4485 * gethostname
4486 * gettimeofday
4487 */
4488
4489 /* Hostnames for a pc are not really funny,
4490 but they are used in change log so we emulate the best we can. */
4491
4492 gethostname (p, size)
4493 char *p;
4494 int size;
4495 {
4496 char *q = egetenv ("HOSTNAME");
4497
4498 if (!q) q = "pc";
4499 strcpy (p, q);
4500 return 0;
4501 }
4502
4503 /* When time zones are set from Ms-Dos too many C-libraries are playing
4504 tricks with time values. We solve this by defining our own version
4505 of `gettimeofday' bypassing GO32. Our version needs to be initialized
4506 once and after each call to `tzset' with TZ changed. That is
4507 accomplished by aliasing tzset to init_gettimeofday. */
4508
4509 static struct tm time_rec;
4510
4511 int
4512 gettimeofday (struct timeval *tp, struct timezone *tzp)
4513 {
4514 if (tp)
4515 {
4516 struct time t;
4517 struct tm tm;
4518
4519 gettime (&t);
4520 if (t.ti_hour < time_rec.tm_hour) /* midnight wrap */
4521 {
4522 struct date d;
4523 getdate (&d);
4524 time_rec.tm_year = d.da_year - 1900;
4525 time_rec.tm_mon = d.da_mon - 1;
4526 time_rec.tm_mday = d.da_day;
4527 }
4528
4529 time_rec.tm_hour = t.ti_hour;
4530 time_rec.tm_min = t.ti_min;
4531 time_rec.tm_sec = t.ti_sec;
4532
4533 tm = time_rec;
4534 tm.tm_gmtoff = dos_timezone_offset;
4535
4536 tp->tv_sec = mktime (&tm); /* may modify tm */
4537 tp->tv_usec = t.ti_hund * (1000000 / 100);
4538 }
4539 /* Ignore tzp; it's obsolescent. */
4540 return 0;
4541 }
4542
4543 #endif /* __DJGPP__ < 2 */
4544
4545 /*
4546 * A list of unimplemented functions that we silently ignore.
4547 */
4548
4549 #if __DJGPP__ < 2
4550 unsigned alarm (s) unsigned s; {}
4551 fork () { return 0; }
4552 int kill (x, y) int x, y; { return -1; }
4553 nice (p) int p; {}
4554 void volatile pause () {}
4555 sigsetmask (x) int x; { return 0; }
4556 sigblock (mask) int mask; { return 0; }
4557 #endif
4558
4559 void request_sigio (void) {}
4560 setpgrp () {return 0; }
4561 setpriority (x,y,z) int x,y,z; { return 0; }
4562 void unrequest_sigio (void) {}
4563
4564 #if __DJGPP__ > 1
4565
4566 #ifdef POSIX_SIGNALS
4567
4568 /* Augment DJGPP library POSIX signal functions. This is needed
4569 as of DJGPP v2.01, but might be in the library in later releases. */
4570
4571 #include <libc/bss.h>
4572
4573 /* A counter to know when to re-initialize the static sets. */
4574 static int sigprocmask_count = -1;
4575
4576 /* Which signals are currently blocked (initially none). */
4577 static sigset_t current_mask;
4578
4579 /* Which signals are pending (initially none). */
4580 static sigset_t pending_signals;
4581
4582 /* Previous handlers to restore when the blocked signals are unblocked. */
4583 typedef void (*sighandler_t)(int);
4584 static sighandler_t prev_handlers[320];
4585
4586 /* A signal handler which just records that a signal occured
4587 (it will be raised later, if and when the signal is unblocked). */
4588 static void
4589 sig_suspender (signo)
4590 int signo;
4591 {
4592 sigaddset (&pending_signals, signo);
4593 }
4594
4595 int
4596 sigprocmask (how, new_set, old_set)
4597 int how;
4598 const sigset_t *new_set;
4599 sigset_t *old_set;
4600 {
4601 int signo;
4602 sigset_t new_mask;
4603
4604 /* If called for the first time, initialize. */
4605 if (sigprocmask_count != __bss_count)
4606 {
4607 sigprocmask_count = __bss_count;
4608 sigemptyset (&pending_signals);
4609 sigemptyset (&current_mask);
4610 for (signo = 0; signo < 320; signo++)
4611 prev_handlers[signo] = SIG_ERR;
4612 }
4613
4614 if (old_set)
4615 *old_set = current_mask;
4616
4617 if (new_set == 0)
4618 return 0;
4619
4620 if (how != SIG_BLOCK && how != SIG_UNBLOCK && how != SIG_SETMASK)
4621 {
4622 errno = EINVAL;
4623 return -1;
4624 }
4625
4626 sigemptyset (&new_mask);
4627
4628 /* DJGPP supports upto 320 signals. */
4629 for (signo = 0; signo < 320; signo++)
4630 {
4631 if (sigismember (&current_mask, signo))
4632 sigaddset (&new_mask, signo);
4633 else if (sigismember (new_set, signo) && how != SIG_UNBLOCK)
4634 {
4635 sigaddset (&new_mask, signo);
4636
4637 /* SIGKILL is silently ignored, as on other platforms. */
4638 if (signo != SIGKILL && prev_handlers[signo] == SIG_ERR)
4639 prev_handlers[signo] = signal (signo, sig_suspender);
4640 }
4641 if (( how == SIG_UNBLOCK
4642 && sigismember (&new_mask, signo)
4643 && sigismember (new_set, signo))
4644 || (how == SIG_SETMASK
4645 && sigismember (&new_mask, signo)
4646 && !sigismember (new_set, signo)))
4647 {
4648 sigdelset (&new_mask, signo);
4649 if (prev_handlers[signo] != SIG_ERR)
4650 {
4651 signal (signo, prev_handlers[signo]);
4652 prev_handlers[signo] = SIG_ERR;
4653 }
4654 if (sigismember (&pending_signals, signo))
4655 {
4656 sigdelset (&pending_signals, signo);
4657 raise (signo);
4658 }
4659 }
4660 }
4661 current_mask = new_mask;
4662 return 0;
4663 }
4664
4665 #else /* not POSIX_SIGNALS */
4666
4667 sigsetmask (x) int x; { return 0; }
4668 sigblock (mask) int mask; { return 0; }
4669
4670 #endif /* not POSIX_SIGNALS */
4671 #endif /* __DJGPP__ > 1 */
4672
4673 #ifndef HAVE_SELECT
4674 #include "sysselect.h"
4675
4676 #ifndef EMACS_TIME_ZERO_OR_NEG_P
4677 #define EMACS_TIME_ZERO_OR_NEG_P(time) \
4678 ((long)(time).tv_sec < 0 \
4679 || ((time).tv_sec == 0 \
4680 && (long)(time).tv_usec <= 0))
4681 #endif
4682
4683 /* This yields the rest of the current time slice to the task manager.
4684 It should be called by any code which knows that it has nothing
4685 useful to do except idle.
4686
4687 I don't use __dpmi_yield here, since versions of library before 2.02
4688 called Int 2Fh/AX=1680h there in a way that would wedge the DOS box
4689 on some versions of Windows 9X. */
4690
4691 void
4692 dos_yield_time_slice (void)
4693 {
4694 _go32_dpmi_registers r;
4695
4696 r.x.ax = 0x1680;
4697 r.x.ss = r.x.sp = r.x.flags = 0;
4698 _go32_dpmi_simulate_int (0x2f, &r);
4699 if (r.h.al == 0x80)
4700 errno = ENOSYS;
4701 }
4702
4703 /* Only event queue is checked. */
4704 /* We don't have to call timer_check here
4705 because wait_reading_process_input takes care of that. */
4706 int
4707 sys_select (nfds, rfds, wfds, efds, timeout)
4708 int nfds;
4709 SELECT_TYPE *rfds, *wfds, *efds;
4710 EMACS_TIME *timeout;
4711 {
4712 int check_input;
4713 struct time t;
4714
4715 check_input = 0;
4716 if (rfds)
4717 {
4718 check_input = FD_ISSET (0, rfds);
4719 FD_ZERO (rfds);
4720 }
4721 if (wfds)
4722 FD_ZERO (wfds);
4723 if (efds)
4724 FD_ZERO (efds);
4725
4726 if (nfds != 1)
4727 abort ();
4728
4729 /* If we are looking only for the terminal, with no timeout,
4730 just read it and wait -- that's more efficient. */
4731 if (!timeout)
4732 {
4733 while (!detect_input_pending ())
4734 {
4735 dos_yield_time_slice ();
4736 }
4737 }
4738 else
4739 {
4740 EMACS_TIME clnow, cllast, cldiff;
4741
4742 gettime (&t);
4743 EMACS_SET_SECS_USECS (cllast, t.ti_sec, t.ti_hund * 10000L);
4744
4745 while (!check_input || !detect_input_pending ())
4746 {
4747 gettime (&t);
4748 EMACS_SET_SECS_USECS (clnow, t.ti_sec, t.ti_hund * 10000L);
4749 EMACS_SUB_TIME (cldiff, clnow, cllast);
4750
4751 /* When seconds wrap around, we assume that no more than
4752 1 minute passed since last `gettime'. */
4753 if (EMACS_TIME_NEG_P (cldiff))
4754 EMACS_SET_SECS (cldiff, EMACS_SECS (cldiff) + 60);
4755 EMACS_SUB_TIME (*timeout, *timeout, cldiff);
4756
4757 /* Stop when timeout value crosses zero. */
4758 if (EMACS_TIME_ZERO_OR_NEG_P (*timeout))
4759 return 0;
4760 cllast = clnow;
4761 dos_yield_time_slice ();
4762 }
4763 }
4764
4765 FD_SET (0, rfds);
4766 return 1;
4767 }
4768 #endif
4769
4770 /*
4771 * Define overlaid functions:
4772 *
4773 * chdir -> sys_chdir
4774 * tzset -> init_gettimeofday
4775 * abort -> dos_abort
4776 */
4777
4778 #ifdef chdir
4779 #undef chdir
4780 extern int chdir ();
4781
4782 int
4783 sys_chdir (path)
4784 const char* path;
4785 {
4786 int len = strlen (path);
4787 char *tmp = (char *)path;
4788
4789 if (*tmp && tmp[1] == ':')
4790 {
4791 if (getdisk () != tolower (tmp[0]) - 'a')
4792 setdisk (tolower (tmp[0]) - 'a');
4793 tmp += 2; /* strip drive: KFS 1995-07-06 */
4794 len -= 2;
4795 }
4796
4797 if (len > 1 && (tmp[len - 1] == '/'))
4798 {
4799 char *tmp1 = (char *) alloca (len + 1);
4800 strcpy (tmp1, tmp);
4801 tmp1[len - 1] = 0;
4802 tmp = tmp1;
4803 }
4804 return chdir (tmp);
4805 }
4806 #endif
4807
4808 #ifdef tzset
4809 #undef tzset
4810 extern void tzset (void);
4811
4812 void
4813 init_gettimeofday ()
4814 {
4815 time_t ltm, gtm;
4816 struct tm *lstm;
4817
4818 tzset ();
4819 ltm = gtm = time (NULL);
4820 ltm = mktime (lstm = localtime (&ltm));
4821 gtm = mktime (gmtime (&gtm));
4822 time_rec.tm_hour = 99; /* force gettimeofday to get date */
4823 time_rec.tm_isdst = lstm->tm_isdst;
4824 dos_timezone_offset = time_rec.tm_gmtoff = (int)(gtm - ltm) / 60;
4825 }
4826 #endif
4827
4828 #ifdef abort
4829 #undef abort
4830 void
4831 dos_abort (file, line)
4832 char *file;
4833 int line;
4834 {
4835 char buffer1[200], buffer2[400];
4836 int i, j;
4837
4838 sprintf (buffer1, "<EMACS FATAL ERROR IN %s LINE %d>", file, line);
4839 for (i = j = 0; buffer1[i]; i++) {
4840 buffer2[j++] = buffer1[i];
4841 buffer2[j++] = 0x70;
4842 }
4843 dosmemput (buffer2, j, (int)ScreenPrimary);
4844 ScreenSetCursor (2, 0);
4845 abort ();
4846 }
4847 #else
4848 void
4849 abort ()
4850 {
4851 dos_ttcooked ();
4852 ScreenSetCursor (10, 0);
4853 cputs ("\r\n\nEmacs aborted!\r\n");
4854 #if __DJGPP__ > 1
4855 #if __DJGPP__ == 2 && __DJGPP_MINOR__ < 2
4856 if (screen_virtual_segment)
4857 dosv_refresh_virtual_screen (2 * 10 * screen_size_X, 4 * screen_size_X);
4858 /* Generate traceback, so we could tell whodunit. */
4859 signal (SIGINT, SIG_DFL);
4860 __asm__ __volatile__ ("movb $0x1b,%al;call ___djgpp_hw_exception");
4861 #else /* __DJGPP_MINOR__ >= 2 */
4862 raise (SIGABRT);
4863 #endif /* __DJGPP_MINOR__ >= 2 */
4864 #endif
4865 exit (2);
4866 }
4867 #endif
4868
4869 /* The following variables are required so that cus-start.el won't
4870 complain about unbound variables. */
4871 #ifndef HAVE_X_WINDOWS
4872 /* Search path for bitmap files (xfns.c). */
4873 Lisp_Object Vx_bitmap_file_path;
4874 int x_stretch_cursor_p;
4875 #endif
4876 #ifndef subprocesses
4877 /* Nonzero means delete a process right away if it exits (process.c). */
4878 static int delete_exited_processes;
4879 #endif
4880
4881 syms_of_msdos ()
4882 {
4883 recent_doskeys = Fmake_vector (make_number (NUM_RECENT_DOSKEYS), Qnil);
4884 staticpro (&recent_doskeys);
4885 #ifndef HAVE_X_WINDOWS
4886 staticpro (&help_echo);
4887 help_echo = Qnil;
4888 staticpro (&previous_help_echo);
4889 previous_help_echo = Qnil;
4890
4891 DEFVAR_LISP ("x-bitmap-file-path", &Vx_bitmap_file_path,
4892 "List of directories to search for bitmap files for X.");
4893 Vx_bitmap_file_path = decode_env_path ((char *) 0, ".");
4894
4895 DEFVAR_BOOL ("x-stretch-cursor", &x_stretch_cursor_p,
4896 "*Non-nil means draw block cursor as wide as the glyph under it.\n\
4897 For example, if a block cursor is over a tab, it will be drawn as\n\
4898 wide as that tab on the display. (No effect on MS-DOS.)");
4899 x_stretch_cursor_p = 0;
4900
4901 /* The following three are from xfns.c: */
4902 Qbackground_color = intern ("background-color");
4903 staticpro (&Qbackground_color);
4904 Qforeground_color = intern ("foreground-color");
4905 staticpro (&Qforeground_color);
4906
4907 DEFVAR_LISP ("dos-unsupported-char-glyph", &Vdos_unsupported_char_glyph,
4908 "*Glyph to display instead of chars not supported by current codepage.\n\
4909
4910 This variable is used only by MSDOS terminals.");
4911 Vdos_unsupported_char_glyph = '\177';
4912 #endif
4913 #ifndef subprocesses
4914 DEFVAR_BOOL ("delete-exited-processes", &delete_exited_processes,
4915 "*Non-nil means delete processes immediately when they exit.\n\
4916 nil means don't delete them until `list-processes' is run.");
4917 delete_exited_processes = 0;
4918 #endif
4919
4920 defsubr (&Srecent_doskeys);
4921 defsubr (&Smsdos_long_file_names);
4922 defsubr (&Smsdos_downcase_filename);
4923 defsubr (&Smsdos_remember_default_colors);
4924 }
4925
4926 #endif /* MSDOS */
4927