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