* lisp/emacs-lisp/edebug.el (edebug-instrument-function): Check a marker is
[emacs.git] / src / w32console.c
blob31c4a7c4e5a7b5e4e551c780ff0ad97851a5be36
1 /* Terminal hooks for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1992, 1999, 2001-2011 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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 3 of the License, or
9 (at your option) any later version.
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.
16 You should have received a copy of the GNU General Public License
17 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
20 Tim Fleehart (apollo@online.com) 1-17-92
21 Geoff Voelker (voelker@cs.washington.edu) 9-12-93
25 #include <config.h>
27 #include <stdio.h>
28 #include <windows.h>
29 #include <setjmp.h>
31 #include "lisp.h"
32 #include "character.h"
33 #include "coding.h"
34 #include "disptab.h"
35 #include "frame.h"
36 #include "termhooks.h"
37 #include "termchar.h"
38 #include "dispextern.h"
39 #include "w32inevt.h"
41 /* from window.c */
42 extern Lisp_Object Frecenter (Lisp_Object);
44 /* from keyboard.c */
45 extern int detect_input_pending (void);
47 /* from sysdep.c */
48 extern int read_input_pending (void);
50 static void w32con_move_cursor (struct frame *f, int row, int col);
51 static void w32con_clear_to_end (struct frame *f);
52 static void w32con_clear_frame (struct frame *f);
53 static void w32con_clear_end_of_line (struct frame *f, int);
54 static void w32con_ins_del_lines (struct frame *f, int vpos, int n);
55 static void w32con_insert_glyphs (struct frame *f, struct glyph *start, int len);
56 static void w32con_write_glyphs (struct frame *f, struct glyph *string, int len);
57 static void w32con_delete_glyphs (struct frame *f, int n);
58 static void w32con_reset_terminal_modes (struct terminal *t);
59 static void w32con_set_terminal_modes (struct terminal *t);
60 static void w32con_set_terminal_window (struct frame *f, int size);
61 static void w32con_update_begin (struct frame * f);
62 static void w32con_update_end (struct frame * f);
63 static WORD w32_face_attributes (struct frame *f, int face_id);
65 static COORD cursor_coords;
66 static HANDLE prev_screen, cur_screen;
67 static WORD char_attr_normal;
68 static DWORD prev_console_mode;
70 #ifndef USE_SEPARATE_SCREEN
71 static CONSOLE_CURSOR_INFO prev_console_cursor;
72 #endif
74 HANDLE keyboard_handle;
77 /* Setting this as the ctrl handler prevents emacs from being killed when
78 someone hits ^C in a 'suspended' session (child shell).
79 Also ignore Ctrl-Break signals. */
81 BOOL
82 ctrl_c_handler (unsigned long type)
84 /* Only ignore "interrupt" events when running interactively. */
85 return (!noninteractive
86 && (type == CTRL_C_EVENT || type == CTRL_BREAK_EVENT));
90 /* Move the cursor to (ROW, COL) on FRAME. */
91 static void
92 w32con_move_cursor (struct frame *f, int row, int col)
94 cursor_coords.X = col;
95 cursor_coords.Y = row;
97 /* TODO: for multi-tty support, cur_screen should be replaced with a
98 reference to the terminal for this frame. */
99 SetConsoleCursorPosition (cur_screen, cursor_coords);
102 /* Clear from cursor to end of screen. */
103 static void
104 w32con_clear_to_end (struct frame *f)
106 w32con_clear_end_of_line (f, FRAME_COLS (f) - 1);
107 w32con_ins_del_lines (f, cursor_coords.Y, FRAME_LINES (f) - cursor_coords.Y - 1);
110 /* Clear the frame. */
111 static void
112 w32con_clear_frame (struct frame *f)
114 COORD dest;
115 int n;
116 DWORD r;
117 CONSOLE_SCREEN_BUFFER_INFO info;
119 GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &info);
121 /* Remember that the screen buffer might be wider than the window. */
122 n = FRAME_LINES (f) * info.dwSize.X;
123 dest.X = dest.Y = 0;
125 FillConsoleOutputAttribute (cur_screen, char_attr_normal, n, dest, &r);
126 FillConsoleOutputCharacter (cur_screen, ' ', n, dest, &r);
128 w32con_move_cursor (f, 0, 0);
132 static struct glyph glyph_base[256];
133 static BOOL ceol_initialized = FALSE;
135 /* Clear from Cursor to end (what's "standout marker"?). */
136 static void
137 w32con_clear_end_of_line (struct frame *f, int end)
139 if (!ceol_initialized)
141 int i;
142 for (i = 0; i < 256; i++)
144 memcpy (&glyph_base[i], &space_glyph, sizeof (struct glyph));
146 ceol_initialized = TRUE;
148 w32con_write_glyphs (f, glyph_base, end - cursor_coords.X); /* fencepost ? */
151 /* Insert n lines at vpos. if n is negative delete -n lines. */
152 static void
153 w32con_ins_del_lines (struct frame *f, int vpos, int n)
155 int i, nb;
156 SMALL_RECT scroll;
157 SMALL_RECT clip;
158 COORD dest;
159 CHAR_INFO fill;
161 if (n < 0)
163 scroll.Top = vpos - n;
164 scroll.Bottom = FRAME_LINES (f);
165 dest.Y = vpos;
167 else
169 scroll.Top = vpos;
170 scroll.Bottom = FRAME_LINES (f) - n;
171 dest.Y = vpos + n;
173 clip.Top = clip.Left = scroll.Left = 0;
174 clip.Right = scroll.Right = FRAME_COLS (f);
175 clip.Bottom = FRAME_LINES (f);
177 dest.X = 0;
179 fill.Char.AsciiChar = 0x20;
180 fill.Attributes = char_attr_normal;
182 ScrollConsoleScreenBuffer (cur_screen, &scroll, &clip, dest, &fill);
184 /* Here we have to deal with a w32 console flake: If the scroll
185 region looks like abc and we scroll c to a and fill with d we get
186 cbd... if we scroll block c one line at a time to a, we get cdd...
187 Emacs expects cdd consistently... So we have to deal with that
188 here... (this also occurs scrolling the same way in the other
189 direction. */
191 if (n > 0)
193 if (scroll.Bottom < dest.Y)
195 for (i = scroll.Bottom; i < dest.Y; i++)
197 w32con_move_cursor (f, i, 0);
198 w32con_clear_end_of_line (f, FRAME_COLS (f));
202 else
204 nb = dest.Y + (scroll.Bottom - scroll.Top) + 1;
206 if (nb < scroll.Top)
208 for (i = nb; i < scroll.Top; i++)
210 w32con_move_cursor (f, i, 0);
211 w32con_clear_end_of_line (f, FRAME_COLS (f));
216 cursor_coords.X = 0;
217 cursor_coords.Y = vpos;
220 #undef LEFT
221 #undef RIGHT
222 #define LEFT 1
223 #define RIGHT 0
225 static void
226 scroll_line (struct frame *f, int dist, int direction)
228 /* The idea here is to implement a horizontal scroll in one line to
229 implement delete and half of insert. */
230 SMALL_RECT scroll, clip;
231 COORD dest;
232 CHAR_INFO fill;
234 clip.Top = scroll.Top = clip.Bottom = scroll.Bottom = cursor_coords.Y;
235 clip.Left = 0;
236 clip.Right = FRAME_COLS (f);
238 if (direction == LEFT)
240 scroll.Left = cursor_coords.X + dist;
241 scroll.Right = FRAME_COLS (f) - 1;
243 else
245 scroll.Left = cursor_coords.X;
246 scroll.Right = FRAME_COLS (f) - dist - 1;
249 dest.X = cursor_coords.X;
250 dest.Y = cursor_coords.Y;
252 fill.Char.AsciiChar = 0x20;
253 fill.Attributes = char_attr_normal;
255 ScrollConsoleScreenBuffer (cur_screen, &scroll, &clip, dest, &fill);
259 /* If start is zero insert blanks instead of a string at start ?. */
260 static void
261 w32con_insert_glyphs (struct frame *f, register struct glyph *start,
262 register int len)
264 scroll_line (f, len, RIGHT);
266 /* Move len chars to the right starting at cursor_coords, fill with blanks */
267 if (start)
269 /* Print the first len characters of start, cursor_coords.X adjusted
270 by write_glyphs. */
272 w32con_write_glyphs (f, start, len);
274 else
276 w32con_clear_end_of_line (f, cursor_coords.X + len);
280 extern unsigned char *encode_terminal_code (struct glyph *, int,
281 struct coding_system *);
283 static void
284 w32con_write_glyphs (struct frame *f, register struct glyph *string,
285 register int len)
287 DWORD r;
288 WORD char_attr;
289 unsigned char *conversion_buffer;
290 struct coding_system *coding;
292 if (len <= 0)
293 return;
295 /* If terminal_coding does any conversion, use it, otherwise use
296 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
297 because it always return 1 if the member src_multibyte is 1. */
298 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
299 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
300 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
301 the tail. */
302 coding->mode &= ~CODING_MODE_LAST_BLOCK;
304 while (len > 0)
306 /* Identify a run of glyphs with the same face. */
307 int face_id = string->face_id;
308 int n;
310 for (n = 1; n < len; ++n)
311 if (string[n].face_id != face_id)
312 break;
314 /* Turn appearance modes of the face of the run on. */
315 char_attr = w32_face_attributes (f, face_id);
317 if (n == len)
318 /* This is the last run. */
319 coding->mode |= CODING_MODE_LAST_BLOCK;
320 conversion_buffer = encode_terminal_code (string, n, coding);
321 if (coding->produced > 0)
323 /* Set the attribute for these characters. */
324 if (!FillConsoleOutputAttribute (cur_screen, char_attr,
325 coding->produced, cursor_coords,
326 &r))
328 printf ("Failed writing console attributes: %d\n",
329 GetLastError ());
330 fflush (stdout);
333 /* Write the characters. */
334 if (!WriteConsoleOutputCharacter (cur_screen, conversion_buffer,
335 coding->produced, cursor_coords,
336 &r))
338 printf ("Failed writing console characters: %d\n",
339 GetLastError ());
340 fflush (stdout);
343 cursor_coords.X += coding->produced;
344 w32con_move_cursor (f, cursor_coords.Y, cursor_coords.X);
346 len -= n;
347 string += n;
352 static void
353 w32con_delete_glyphs (struct frame *f, int n)
355 /* delete chars means scroll chars from cursor_coords.X + n to
356 cursor_coords.X, anything beyond the edge of the screen should
357 come out empty... */
359 scroll_line (f, n, LEFT);
362 static unsigned int sound_type = 0xFFFFFFFF;
363 #define MB_EMACS_SILENT (0xFFFFFFFF - 1)
365 void
366 w32_sys_ring_bell (struct frame *f)
368 if (sound_type == 0xFFFFFFFF)
370 Beep (666, 100);
372 else if (sound_type == MB_EMACS_SILENT)
374 /* Do nothing. */
376 else
377 MessageBeep (sound_type);
380 DEFUN ("set-message-beep", Fset_message_beep, Sset_message_beep, 1, 1, 0,
381 doc: /* Set the sound generated when the bell is rung.
382 SOUND is 'asterisk, 'exclamation, 'hand, 'question, 'ok, or 'silent
383 to use the corresponding system sound for the bell. The 'silent sound
384 prevents Emacs from making any sound at all.
385 SOUND is nil to use the normal beep. */)
386 (Lisp_Object sound)
388 CHECK_SYMBOL (sound);
390 if (NILP (sound))
391 sound_type = 0xFFFFFFFF;
392 else if (EQ (sound, intern ("asterisk")))
393 sound_type = MB_ICONASTERISK;
394 else if (EQ (sound, intern ("exclamation")))
395 sound_type = MB_ICONEXCLAMATION;
396 else if (EQ (sound, intern ("hand")))
397 sound_type = MB_ICONHAND;
398 else if (EQ (sound, intern ("question")))
399 sound_type = MB_ICONQUESTION;
400 else if (EQ (sound, intern ("ok")))
401 sound_type = MB_OK;
402 else if (EQ (sound, intern ("silent")))
403 sound_type = MB_EMACS_SILENT;
404 else
405 sound_type = 0xFFFFFFFF;
407 return sound;
410 static void
411 w32con_reset_terminal_modes (struct terminal *t)
413 COORD dest;
414 CONSOLE_SCREEN_BUFFER_INFO info;
415 int n;
416 DWORD r;
418 /* Clear the complete screen buffer. This is required because Emacs
419 sets the cursor position to the top of the buffer, but there might
420 be other output below the bottom of the Emacs frame if the screen buffer
421 is larger than the window size. */
422 GetConsoleScreenBufferInfo (cur_screen, &info);
423 dest.X = 0;
424 dest.Y = 0;
425 n = info.dwSize.X * info.dwSize.Y;
427 FillConsoleOutputAttribute (cur_screen, char_attr_normal, n, dest, &r);
428 FillConsoleOutputCharacter (cur_screen, ' ', n, dest, &r);
429 /* Now that the screen is clear, put the cursor at the top. */
430 SetConsoleCursorPosition (cur_screen, dest);
432 #ifdef USE_SEPARATE_SCREEN
433 SetConsoleActiveScreenBuffer (prev_screen);
434 #else
435 SetConsoleCursorInfo (prev_screen, &prev_console_cursor);
436 #endif
438 SetConsoleMode (keyboard_handle, prev_console_mode);
441 static void
442 w32con_set_terminal_modes (struct terminal *t)
444 CONSOLE_CURSOR_INFO cci;
446 /* make cursor big and visible (100 on Win95 makes it disappear) */
447 cci.dwSize = 99;
448 cci.bVisible = TRUE;
449 (void) SetConsoleCursorInfo (cur_screen, &cci);
451 SetConsoleActiveScreenBuffer (cur_screen);
453 SetConsoleMode (keyboard_handle, ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
455 /* Initialize input mode: interrupt_input off, no flow control, allow
456 8 bit character input, standard quit char. */
457 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
460 /* hmmm... perhaps these let us bracket screen changes so that we can flush
461 clumps rather than one-character-at-a-time...
463 we'll start with not moving the cursor while an update is in progress. */
464 static void
465 w32con_update_begin (struct frame * f)
469 static void
470 w32con_update_end (struct frame * f)
472 SetConsoleCursorPosition (cur_screen, cursor_coords);
475 static void
476 w32con_set_terminal_window (struct frame *f, int size)
480 /***********************************************************************
481 stubs from termcap.c
482 ***********************************************************************/
484 void
485 sys_tputs (char *str, int nlines, int (*outfun) (int))
489 char *
490 sys_tgetstr (char *cap, char **area)
492 return NULL;
496 /***********************************************************************
497 stubs from cm.c
498 ***********************************************************************/
500 struct tty_display_info *current_tty = NULL;
501 int cost = 0;
504 evalcost (int c)
506 return c;
510 cmputc (int c)
512 return c;
515 void
516 cmcheckmagic (struct tty_display_info *tty)
520 void
521 cmcostinit (struct tty_display_info *tty)
525 void
526 cmgoto (struct tty_display_info *tty, int row, int col)
530 void
531 Wcm_clear (struct tty_display_info *tty)
536 /***********************************************************************
537 Faces
538 ***********************************************************************/
541 /* Turn appearances of face FACE_ID on tty frame F on. */
543 static WORD
544 w32_face_attributes (struct frame *f, int face_id)
546 WORD char_attr;
547 struct face *face = FACE_FROM_ID (f, face_id);
549 xassert (face != NULL);
551 char_attr = char_attr_normal;
553 /* Reverse the default color if requested. If background and
554 foreground are specified, then they have been reversed already. */
555 if (face->tty_reverse_p)
556 char_attr = (char_attr & 0xff00) + ((char_attr & 0x000f) << 4)
557 + ((char_attr & 0x00f0) >> 4);
559 /* Before the terminal is properly initialized, all colors map to 0.
560 Don't try to resolve them. */
561 if (NILP (Vtty_defined_color_alist))
562 return char_attr;
564 /* Colors should be in the range 0...15 unless they are one of
565 FACE_TTY_DEFAULT_COLOR, FACE_TTY_DEFAULT_FG_COLOR or
566 FACE_TTY_DEFAULT_BG_COLOR. Other out of range colors are
567 invalid, so it is better to use the default color if they ever
568 get through to here. */
569 if (face->foreground >= 0 && face->foreground < 16)
570 char_attr = (char_attr & 0xfff0) + face->foreground;
572 if (face->background >= 0 && face->background < 16)
573 char_attr = (char_attr & 0xff0f) + (face->background << 4);
575 return char_attr;
578 void
579 initialize_w32_display (struct terminal *term)
581 CONSOLE_SCREEN_BUFFER_INFO info;
583 term->rif = 0; /* No window based redisplay on the console. */
584 term->cursor_to_hook = w32con_move_cursor;
585 term->raw_cursor_to_hook = w32con_move_cursor;
586 term->clear_to_end_hook = w32con_clear_to_end;
587 term->clear_frame_hook = w32con_clear_frame;
588 term->clear_end_of_line_hook = w32con_clear_end_of_line;
589 term->ins_del_lines_hook = w32con_ins_del_lines;
590 term->insert_glyphs_hook = w32con_insert_glyphs;
591 term->write_glyphs_hook = w32con_write_glyphs;
592 term->delete_glyphs_hook = w32con_delete_glyphs;
593 term->ring_bell_hook = w32_sys_ring_bell;
594 term->reset_terminal_modes_hook = w32con_reset_terminal_modes;
595 term->set_terminal_modes_hook = w32con_set_terminal_modes;
596 term->set_terminal_window_hook = w32con_set_terminal_window;
597 term->update_begin_hook = w32con_update_begin;
598 term->update_end_hook = w32con_update_end;
600 term->read_socket_hook = w32_console_read_socket;
601 term->mouse_position_hook = w32_console_mouse_position;
603 /* The following are not used on the console. */
604 term->frame_rehighlight_hook = 0;
605 term->frame_raise_lower_hook = 0;
606 term->set_vertical_scroll_bar_hook = 0;
607 term->condemn_scroll_bars_hook = 0;
608 term->redeem_scroll_bar_hook = 0;
609 term->judge_scroll_bars_hook = 0;
610 term->frame_up_to_date_hook = 0;
612 /* Initialize interrupt_handle. */
613 init_crit ();
615 /* Remember original console settings. */
616 keyboard_handle = GetStdHandle (STD_INPUT_HANDLE);
617 GetConsoleMode (keyboard_handle, &prev_console_mode);
619 prev_screen = GetStdHandle (STD_OUTPUT_HANDLE);
621 #ifdef USE_SEPARATE_SCREEN
622 cur_screen = CreateConsoleScreenBuffer (GENERIC_READ | GENERIC_WRITE,
623 0, NULL,
624 CONSOLE_TEXTMODE_BUFFER,
625 NULL);
627 if (cur_screen == INVALID_HANDLE_VALUE)
629 printf ("CreateConsoleScreenBuffer failed in ResetTerm\n");
630 printf ("LastError = 0x%lx\n", GetLastError ());
631 fflush (stdout);
632 exit (0);
634 #else
635 cur_screen = prev_screen;
636 GetConsoleCursorInfo (prev_screen, &prev_console_cursor);
637 #endif
639 /* Respect setting of LINES and COLUMNS environment variables. */
641 char * lines = getenv ("LINES");
642 char * columns = getenv ("COLUMNS");
644 if (lines != NULL && columns != NULL)
646 SMALL_RECT new_win_dims;
647 COORD new_size;
649 new_size.X = atoi (columns);
650 new_size.Y = atoi (lines);
652 GetConsoleScreenBufferInfo (cur_screen, &info);
654 /* Shrink the window first, so the buffer dimensions can be
655 reduced if necessary. */
656 new_win_dims.Top = 0;
657 new_win_dims.Left = 0;
658 new_win_dims.Bottom = min (new_size.Y, info.dwSize.Y) - 1;
659 new_win_dims.Right = min (new_size.X, info.dwSize.X) - 1;
660 SetConsoleWindowInfo (cur_screen, TRUE, &new_win_dims);
662 SetConsoleScreenBufferSize (cur_screen, new_size);
664 /* Set the window size to match the buffer dimension. */
665 new_win_dims.Top = 0;
666 new_win_dims.Left = 0;
667 new_win_dims.Bottom = new_size.Y - 1;
668 new_win_dims.Right = new_size.X - 1;
669 SetConsoleWindowInfo (cur_screen, TRUE, &new_win_dims);
673 GetConsoleScreenBufferInfo (cur_screen, &info);
675 char_attr_normal = info.wAttributes;
677 /* Determine if the info returned by GetConsoleScreenBufferInfo
678 is realistic. Old MS Telnet servers used to only fill out
679 the dwSize portion, even modern one fill the whole struct with
680 garbage when using non-MS telnet clients. */
681 if ((w32_use_full_screen_buffer
682 && (info.dwSize.Y < 20 || info.dwSize.Y > 100
683 || info.dwSize.X < 40 || info.dwSize.X > 200))
684 || (!w32_use_full_screen_buffer
685 && (info.srWindow.Bottom - info.srWindow.Top < 20
686 || info.srWindow.Bottom - info.srWindow.Top > 100
687 || info.srWindow.Right - info.srWindow.Left < 40
688 || info.srWindow.Right - info.srWindow.Left > 100)))
690 FRAME_LINES (SELECTED_FRAME ()) = 25;
691 SET_FRAME_COLS (SELECTED_FRAME (), 80);
694 else if (w32_use_full_screen_buffer)
696 FRAME_LINES (SELECTED_FRAME ()) = info.dwSize.Y; /* lines per page */
697 SET_FRAME_COLS (SELECTED_FRAME (), info.dwSize.X); /* characters per line */
699 else
701 /* Lines per page. Use buffer coords instead of buffer size. */
702 FRAME_LINES (SELECTED_FRAME ()) = 1 + info.srWindow.Bottom -
703 info.srWindow.Top;
704 /* Characters per line. Use buffer coords instead of buffer size. */
705 SET_FRAME_COLS (SELECTED_FRAME (), 1 + info.srWindow.Right -
706 info.srWindow.Left);
709 /* Setup w32_display_info structure for this frame. */
711 w32_initialize_display_info (build_string ("Console"));
716 DEFUN ("set-screen-color", Fset_screen_color, Sset_screen_color, 2, 2, 0,
717 doc: /* Set screen colors. */)
718 (Lisp_Object foreground, Lisp_Object background)
720 char_attr_normal = XFASTINT (foreground) + (XFASTINT (background) << 4);
722 Frecenter (Qnil);
723 return Qt;
726 DEFUN ("set-cursor-size", Fset_cursor_size, Sset_cursor_size, 1, 1, 0,
727 doc: /* Set cursor size. */)
728 (Lisp_Object size)
730 CONSOLE_CURSOR_INFO cci;
731 cci.dwSize = XFASTINT (size);
732 cci.bVisible = TRUE;
733 (void) SetConsoleCursorInfo (cur_screen, &cci);
735 return Qt;
738 void
739 syms_of_ntterm (void)
741 DEFVAR_BOOL ("w32-use-full-screen-buffer",
742 w32_use_full_screen_buffer,
743 doc: /* Non-nil means make terminal frames use the full screen buffer dimensions.
744 This is desirable when running Emacs over telnet.
745 A value of nil means use the current console window dimensions; this
746 may be preferrable when working directly at the console with a large
747 scroll-back buffer. */);
748 w32_use_full_screen_buffer = 0;
750 defsubr (&Sset_screen_color);
751 defsubr (&Sset_cursor_size);
752 defsubr (&Sset_message_beep);