Add pcomplete support for hosts defined in .ssh/config.
[emacs.git] / src / w32console.c
blobfaa6cbcc44316cc6524cd669222f7c6c7f6b4377
1 /* Terminal hooks for GNU Emacs on the Microsoft W32 API.
2 Copyright (C) 1992, 1999, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
3 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
21 Tim Fleehart (apollo@online.com) 1-17-92
22 Geoff Voelker (voelker@cs.washington.edu) 9-12-93
26 #include <config.h>
28 #include <stdio.h>
29 #include <windows.h>
30 #include <setjmp.h>
32 #include "lisp.h"
33 #include "character.h"
34 #include "coding.h"
35 #include "disptab.h"
36 #include "frame.h"
37 #include "termhooks.h"
38 #include "termchar.h"
39 #include "dispextern.h"
40 #include "w32inevt.h"
42 /* from window.c */
43 extern Lisp_Object Frecenter (Lisp_Object);
45 /* from keyboard.c */
46 extern int detect_input_pending (void);
48 /* from sysdep.c */
49 extern int read_input_pending (void);
51 static void w32con_move_cursor (struct frame *f, int row, int col);
52 static void w32con_clear_to_end (struct frame *f);
53 static void w32con_clear_frame (struct frame *f);
54 static void w32con_clear_end_of_line (struct frame *f, int);
55 static void w32con_ins_del_lines (struct frame *f, int vpos, int n);
56 static void w32con_insert_glyphs (struct frame *f, struct glyph *start, int len);
57 static void w32con_write_glyphs (struct frame *f, struct glyph *string, int len);
58 static void w32con_delete_glyphs (struct frame *f, int n);
59 static void w32con_reset_terminal_modes (struct terminal *t);
60 static void w32con_set_terminal_modes (struct terminal *t);
61 static void w32con_set_terminal_window (struct frame *f, int size);
62 static void w32con_update_begin (struct frame * f);
63 static void w32con_update_end (struct frame * f);
64 static WORD w32_face_attributes (struct frame *f, int face_id);
66 static COORD cursor_coords;
67 static HANDLE prev_screen, cur_screen;
68 static WORD char_attr_normal;
69 static DWORD prev_console_mode;
71 #ifndef USE_SEPARATE_SCREEN
72 static CONSOLE_CURSOR_INFO prev_console_cursor;
73 #endif
75 HANDLE keyboard_handle;
78 /* Setting this as the ctrl handler prevents emacs from being killed when
79 someone hits ^C in a 'suspended' session (child shell).
80 Also ignore Ctrl-Break signals. */
82 BOOL
83 ctrl_c_handler (unsigned long type)
85 /* Only ignore "interrupt" events when running interactively. */
86 return (!noninteractive
87 && (type == CTRL_C_EVENT || type == CTRL_BREAK_EVENT));
91 /* Move the cursor to (ROW, COL) on FRAME. */
92 static void
93 w32con_move_cursor (struct frame *f, int row, int col)
95 cursor_coords.X = col;
96 cursor_coords.Y = row;
98 /* TODO: for multi-tty support, cur_screen should be replaced with a
99 reference to the terminal for this frame. */
100 SetConsoleCursorPosition (cur_screen, cursor_coords);
103 /* Clear from cursor to end of screen. */
104 static void
105 w32con_clear_to_end (struct frame *f)
107 w32con_clear_end_of_line (f, FRAME_COLS (f) - 1);
108 w32con_ins_del_lines (f, cursor_coords.Y, FRAME_LINES (f) - cursor_coords.Y - 1);
111 /* Clear the frame. */
112 static void
113 w32con_clear_frame (struct frame *f)
115 COORD dest;
116 int n;
117 DWORD r;
118 CONSOLE_SCREEN_BUFFER_INFO info;
120 GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &info);
122 /* Remember that the screen buffer might be wider than the window. */
123 n = FRAME_LINES (f) * info.dwSize.X;
124 dest.X = dest.Y = 0;
126 FillConsoleOutputAttribute (cur_screen, char_attr_normal, n, dest, &r);
127 FillConsoleOutputCharacter (cur_screen, ' ', n, dest, &r);
129 w32con_move_cursor (f, 0, 0);
133 static struct glyph glyph_base[256];
134 static BOOL ceol_initialized = FALSE;
136 /* Clear from Cursor to end (what's "standout marker"?). */
137 static void
138 w32con_clear_end_of_line (struct frame *f, int end)
140 if (!ceol_initialized)
142 int i;
143 for (i = 0; i < 256; i++)
145 memcpy (&glyph_base[i], &space_glyph, sizeof (struct glyph));
147 ceol_initialized = TRUE;
149 w32con_write_glyphs (f, glyph_base, end - cursor_coords.X); /* fencepost ? */
152 /* Insert n lines at vpos. if n is negative delete -n lines. */
153 static void
154 w32con_ins_del_lines (struct frame *f, int vpos, int n)
156 int i, nb;
157 SMALL_RECT scroll;
158 SMALL_RECT clip;
159 COORD dest;
160 CHAR_INFO fill;
162 if (n < 0)
164 scroll.Top = vpos - n;
165 scroll.Bottom = FRAME_LINES (f);
166 dest.Y = vpos;
168 else
170 scroll.Top = vpos;
171 scroll.Bottom = FRAME_LINES (f) - n;
172 dest.Y = vpos + n;
174 clip.Top = clip.Left = scroll.Left = 0;
175 clip.Right = scroll.Right = FRAME_COLS (f);
176 clip.Bottom = FRAME_LINES (f);
178 dest.X = 0;
180 fill.Char.AsciiChar = 0x20;
181 fill.Attributes = char_attr_normal;
183 ScrollConsoleScreenBuffer (cur_screen, &scroll, &clip, dest, &fill);
185 /* Here we have to deal with a w32 console flake: If the scroll
186 region looks like abc and we scroll c to a and fill with d we get
187 cbd... if we scroll block c one line at a time to a, we get cdd...
188 Emacs expects cdd consistently... So we have to deal with that
189 here... (this also occurs scrolling the same way in the other
190 direction. */
192 if (n > 0)
194 if (scroll.Bottom < dest.Y)
196 for (i = scroll.Bottom; i < dest.Y; i++)
198 w32con_move_cursor (f, i, 0);
199 w32con_clear_end_of_line (f, FRAME_COLS (f));
203 else
205 nb = dest.Y + (scroll.Bottom - scroll.Top) + 1;
207 if (nb < scroll.Top)
209 for (i = nb; i < scroll.Top; i++)
211 w32con_move_cursor (f, i, 0);
212 w32con_clear_end_of_line (f, FRAME_COLS (f));
217 cursor_coords.X = 0;
218 cursor_coords.Y = vpos;
221 #undef LEFT
222 #undef RIGHT
223 #define LEFT 1
224 #define RIGHT 0
226 static void
227 scroll_line (struct frame *f, int dist, int direction)
229 /* The idea here is to implement a horizontal scroll in one line to
230 implement delete and half of insert. */
231 SMALL_RECT scroll, clip;
232 COORD dest;
233 CHAR_INFO fill;
235 clip.Top = scroll.Top = clip.Bottom = scroll.Bottom = cursor_coords.Y;
236 clip.Left = 0;
237 clip.Right = FRAME_COLS (f);
239 if (direction == LEFT)
241 scroll.Left = cursor_coords.X + dist;
242 scroll.Right = FRAME_COLS (f) - 1;
244 else
246 scroll.Left = cursor_coords.X;
247 scroll.Right = FRAME_COLS (f) - dist - 1;
250 dest.X = cursor_coords.X;
251 dest.Y = cursor_coords.Y;
253 fill.Char.AsciiChar = 0x20;
254 fill.Attributes = char_attr_normal;
256 ScrollConsoleScreenBuffer (cur_screen, &scroll, &clip, dest, &fill);
260 /* If start is zero insert blanks instead of a string at start ?. */
261 static void
262 w32con_insert_glyphs (struct frame *f, register struct glyph *start,
263 register int len)
265 scroll_line (f, len, RIGHT);
267 /* Move len chars to the right starting at cursor_coords, fill with blanks */
268 if (start)
270 /* Print the first len characters of start, cursor_coords.X adjusted
271 by write_glyphs. */
273 w32con_write_glyphs (f, start, len);
275 else
277 w32con_clear_end_of_line (f, cursor_coords.X + len);
281 extern unsigned char *encode_terminal_code (struct glyph *, int,
282 struct coding_system *);
284 static void
285 w32con_write_glyphs (struct frame *f, register struct glyph *string,
286 register int len)
288 DWORD r;
289 WORD char_attr;
290 unsigned char *conversion_buffer;
291 struct coding_system *coding;
293 if (len <= 0)
294 return;
296 /* If terminal_coding does any conversion, use it, otherwise use
297 safe_terminal_coding. We can't use CODING_REQUIRE_ENCODING here
298 because it always return 1 if the member src_multibyte is 1. */
299 coding = (FRAME_TERMINAL_CODING (f)->common_flags & CODING_REQUIRE_ENCODING_MASK
300 ? FRAME_TERMINAL_CODING (f) : &safe_terminal_coding);
301 /* The mode bit CODING_MODE_LAST_BLOCK should be set to 1 only at
302 the tail. */
303 coding->mode &= ~CODING_MODE_LAST_BLOCK;
305 while (len > 0)
307 /* Identify a run of glyphs with the same face. */
308 int face_id = string->face_id;
309 int n;
311 for (n = 1; n < len; ++n)
312 if (string[n].face_id != face_id)
313 break;
315 /* Turn appearance modes of the face of the run on. */
316 char_attr = w32_face_attributes (f, face_id);
318 if (n == len)
319 /* This is the last run. */
320 coding->mode |= CODING_MODE_LAST_BLOCK;
321 conversion_buffer = encode_terminal_code (string, n, coding);
322 if (coding->produced > 0)
324 /* Set the attribute for these characters. */
325 if (!FillConsoleOutputAttribute (cur_screen, char_attr,
326 coding->produced, cursor_coords,
327 &r))
329 printf ("Failed writing console attributes: %d\n",
330 GetLastError ());
331 fflush (stdout);
334 /* Write the characters. */
335 if (!WriteConsoleOutputCharacter (cur_screen, conversion_buffer,
336 coding->produced, cursor_coords,
337 &r))
339 printf ("Failed writing console characters: %d\n",
340 GetLastError ());
341 fflush (stdout);
344 cursor_coords.X += coding->produced;
345 w32con_move_cursor (f, cursor_coords.Y, cursor_coords.X);
347 len -= n;
348 string += n;
353 static void
354 w32con_delete_glyphs (struct frame *f, int n)
356 /* delete chars means scroll chars from cursor_coords.X + n to
357 cursor_coords.X, anything beyond the edge of the screen should
358 come out empty... */
360 scroll_line (f, n, LEFT);
363 static unsigned int sound_type = 0xFFFFFFFF;
364 #define MB_EMACS_SILENT (0xFFFFFFFF - 1)
366 void
367 w32_sys_ring_bell (struct frame *f)
369 if (sound_type == 0xFFFFFFFF)
371 Beep (666, 100);
373 else if (sound_type == MB_EMACS_SILENT)
375 /* Do nothing. */
377 else
378 MessageBeep (sound_type);
381 DEFUN ("set-message-beep", Fset_message_beep, Sset_message_beep, 1, 1, 0,
382 doc: /* Set the sound generated when the bell is rung.
383 SOUND is 'asterisk, 'exclamation, 'hand, 'question, 'ok, or 'silent
384 to use the corresponding system sound for the bell. The 'silent sound
385 prevents Emacs from making any sound at all.
386 SOUND is nil to use the normal beep. */)
387 (Lisp_Object sound)
389 CHECK_SYMBOL (sound);
391 if (NILP (sound))
392 sound_type = 0xFFFFFFFF;
393 else if (EQ (sound, intern ("asterisk")))
394 sound_type = MB_ICONASTERISK;
395 else if (EQ (sound, intern ("exclamation")))
396 sound_type = MB_ICONEXCLAMATION;
397 else if (EQ (sound, intern ("hand")))
398 sound_type = MB_ICONHAND;
399 else if (EQ (sound, intern ("question")))
400 sound_type = MB_ICONQUESTION;
401 else if (EQ (sound, intern ("ok")))
402 sound_type = MB_OK;
403 else if (EQ (sound, intern ("silent")))
404 sound_type = MB_EMACS_SILENT;
405 else
406 sound_type = 0xFFFFFFFF;
408 return sound;
411 static void
412 w32con_reset_terminal_modes (struct terminal *t)
414 COORD dest;
415 CONSOLE_SCREEN_BUFFER_INFO info;
416 int n;
417 DWORD r;
419 /* Clear the complete screen buffer. This is required because Emacs
420 sets the cursor position to the top of the buffer, but there might
421 be other output below the bottom of the Emacs frame if the screen buffer
422 is larger than the window size. */
423 GetConsoleScreenBufferInfo (cur_screen, &info);
424 dest.X = 0;
425 dest.Y = 0;
426 n = info.dwSize.X * info.dwSize.Y;
428 FillConsoleOutputAttribute (cur_screen, char_attr_normal, n, dest, &r);
429 FillConsoleOutputCharacter (cur_screen, ' ', n, dest, &r);
430 /* Now that the screen is clear, put the cursor at the top. */
431 SetConsoleCursorPosition (cur_screen, dest);
433 #ifdef USE_SEPARATE_SCREEN
434 SetConsoleActiveScreenBuffer (prev_screen);
435 #else
436 SetConsoleCursorInfo (prev_screen, &prev_console_cursor);
437 #endif
439 SetConsoleMode (keyboard_handle, prev_console_mode);
442 static void
443 w32con_set_terminal_modes (struct terminal *t)
445 CONSOLE_CURSOR_INFO cci;
447 /* make cursor big and visible (100 on Win95 makes it disappear) */
448 cci.dwSize = 99;
449 cci.bVisible = TRUE;
450 (void) SetConsoleCursorInfo (cur_screen, &cci);
452 SetConsoleActiveScreenBuffer (cur_screen);
454 SetConsoleMode (keyboard_handle, ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT);
456 /* Initialize input mode: interrupt_input off, no flow control, allow
457 8 bit character input, standard quit char. */
458 Fset_input_mode (Qnil, Qnil, make_number (2), Qnil);
461 /* hmmm... perhaps these let us bracket screen changes so that we can flush
462 clumps rather than one-character-at-a-time...
464 we'll start with not moving the cursor while an update is in progress. */
465 static void
466 w32con_update_begin (struct frame * f)
470 static void
471 w32con_update_end (struct frame * f)
473 SetConsoleCursorPosition (cur_screen, cursor_coords);
476 static void
477 w32con_set_terminal_window (struct frame *f, int size)
481 /***********************************************************************
482 stubs from termcap.c
483 ***********************************************************************/
485 void
486 sys_tputs (char *str, int nlines, int (*outfun) (int))
490 char *
491 sys_tgetstr (char *cap, char **area)
493 return NULL;
497 /***********************************************************************
498 stubs from cm.c
499 ***********************************************************************/
501 struct tty_display_info *current_tty = NULL;
502 int cost = 0;
505 evalcost (int c)
507 return c;
511 cmputc (int c)
513 return c;
516 void
517 cmcheckmagic (struct tty_display_info *tty)
521 void
522 cmcostinit (struct tty_display_info *tty)
526 void
527 cmgoto (struct tty_display_info *tty, int row, int col)
531 void
532 Wcm_clear (struct tty_display_info *tty)
537 /***********************************************************************
538 Faces
539 ***********************************************************************/
542 /* Turn appearances of face FACE_ID on tty frame F on. */
544 static WORD
545 w32_face_attributes (struct frame *f, int face_id)
547 WORD char_attr;
548 struct face *face = FACE_FROM_ID (f, face_id);
550 xassert (face != NULL);
552 char_attr = char_attr_normal;
554 /* Reverse the default color if requested. If background and
555 foreground are specified, then they have been reversed already. */
556 if (face->tty_reverse_p)
557 char_attr = (char_attr & 0xff00) + ((char_attr & 0x000f) << 4)
558 + ((char_attr & 0x00f0) >> 4);
560 /* Before the terminal is properly initialized, all colors map to 0.
561 Don't try to resolve them. */
562 if (NILP (Vtty_defined_color_alist))
563 return char_attr;
565 /* Colors should be in the range 0...15 unless they are one of
566 FACE_TTY_DEFAULT_COLOR, FACE_TTY_DEFAULT_FG_COLOR or
567 FACE_TTY_DEFAULT_BG_COLOR. Other out of range colors are
568 invalid, so it is better to use the default color if they ever
569 get through to here. */
570 if (face->foreground >= 0 && face->foreground < 16)
571 char_attr = (char_attr & 0xfff0) + face->foreground;
573 if (face->background >= 0 && face->background < 16)
574 char_attr = (char_attr & 0xff0f) + (face->background << 4);
576 return char_attr;
579 void
580 initialize_w32_display (struct terminal *term)
582 CONSOLE_SCREEN_BUFFER_INFO info;
584 term->rif = 0; /* No window based redisplay on the console. */
585 term->cursor_to_hook = w32con_move_cursor;
586 term->raw_cursor_to_hook = w32con_move_cursor;
587 term->clear_to_end_hook = w32con_clear_to_end;
588 term->clear_frame_hook = w32con_clear_frame;
589 term->clear_end_of_line_hook = w32con_clear_end_of_line;
590 term->ins_del_lines_hook = w32con_ins_del_lines;
591 term->insert_glyphs_hook = w32con_insert_glyphs;
592 term->write_glyphs_hook = w32con_write_glyphs;
593 term->delete_glyphs_hook = w32con_delete_glyphs;
594 term->ring_bell_hook = w32_sys_ring_bell;
595 term->reset_terminal_modes_hook = w32con_reset_terminal_modes;
596 term->set_terminal_modes_hook = w32con_set_terminal_modes;
597 term->set_terminal_window_hook = w32con_set_terminal_window;
598 term->update_begin_hook = w32con_update_begin;
599 term->update_end_hook = w32con_update_end;
601 term->read_socket_hook = w32_console_read_socket;
602 term->mouse_position_hook = w32_console_mouse_position;
604 /* The following are not used on the console. */
605 term->frame_rehighlight_hook = 0;
606 term->frame_raise_lower_hook = 0;
607 term->set_vertical_scroll_bar_hook = 0;
608 term->condemn_scroll_bars_hook = 0;
609 term->redeem_scroll_bar_hook = 0;
610 term->judge_scroll_bars_hook = 0;
611 term->frame_up_to_date_hook = 0;
613 /* Initialize interrupt_handle. */
614 init_crit ();
616 /* Remember original console settings. */
617 keyboard_handle = GetStdHandle (STD_INPUT_HANDLE);
618 GetConsoleMode (keyboard_handle, &prev_console_mode);
620 prev_screen = GetStdHandle (STD_OUTPUT_HANDLE);
622 #ifdef USE_SEPARATE_SCREEN
623 cur_screen = CreateConsoleScreenBuffer (GENERIC_READ | GENERIC_WRITE,
624 0, NULL,
625 CONSOLE_TEXTMODE_BUFFER,
626 NULL);
628 if (cur_screen == INVALID_HANDLE_VALUE)
630 printf ("CreateConsoleScreenBuffer failed in ResetTerm\n");
631 printf ("LastError = 0x%lx\n", GetLastError ());
632 fflush (stdout);
633 exit (0);
635 #else
636 cur_screen = prev_screen;
637 GetConsoleCursorInfo (prev_screen, &prev_console_cursor);
638 #endif
640 /* Respect setting of LINES and COLUMNS environment variables. */
642 char * lines = getenv ("LINES");
643 char * columns = getenv ("COLUMNS");
645 if (lines != NULL && columns != NULL)
647 SMALL_RECT new_win_dims;
648 COORD new_size;
650 new_size.X = atoi (columns);
651 new_size.Y = atoi (lines);
653 GetConsoleScreenBufferInfo (cur_screen, &info);
655 /* Shrink the window first, so the buffer dimensions can be
656 reduced if necessary. */
657 new_win_dims.Top = 0;
658 new_win_dims.Left = 0;
659 new_win_dims.Bottom = min (new_size.Y, info.dwSize.Y) - 1;
660 new_win_dims.Right = min (new_size.X, info.dwSize.X) - 1;
661 SetConsoleWindowInfo (cur_screen, TRUE, &new_win_dims);
663 SetConsoleScreenBufferSize (cur_screen, new_size);
665 /* Set the window size to match the buffer dimension. */
666 new_win_dims.Top = 0;
667 new_win_dims.Left = 0;
668 new_win_dims.Bottom = new_size.Y - 1;
669 new_win_dims.Right = new_size.X - 1;
670 SetConsoleWindowInfo (cur_screen, TRUE, &new_win_dims);
674 GetConsoleScreenBufferInfo (cur_screen, &info);
676 char_attr_normal = info.wAttributes;
678 /* Determine if the info returned by GetConsoleScreenBufferInfo
679 is realistic. Old MS Telnet servers used to only fill out
680 the dwSize portion, even modern one fill the whole struct with
681 garbage when using non-MS telnet clients. */
682 if ((w32_use_full_screen_buffer
683 && (info.dwSize.Y < 20 || info.dwSize.Y > 100
684 || info.dwSize.X < 40 || info.dwSize.X > 200))
685 || (!w32_use_full_screen_buffer
686 && (info.srWindow.Bottom - info.srWindow.Top < 20
687 || info.srWindow.Bottom - info.srWindow.Top > 100
688 || info.srWindow.Right - info.srWindow.Left < 40
689 || info.srWindow.Right - info.srWindow.Left > 100)))
691 FRAME_LINES (SELECTED_FRAME ()) = 25;
692 SET_FRAME_COLS (SELECTED_FRAME (), 80);
695 else if (w32_use_full_screen_buffer)
697 FRAME_LINES (SELECTED_FRAME ()) = info.dwSize.Y; /* lines per page */
698 SET_FRAME_COLS (SELECTED_FRAME (), info.dwSize.X); /* characters per line */
700 else
702 /* Lines per page. Use buffer coords instead of buffer size. */
703 FRAME_LINES (SELECTED_FRAME ()) = 1 + info.srWindow.Bottom -
704 info.srWindow.Top;
705 /* Characters per line. Use buffer coords instead of buffer size. */
706 SET_FRAME_COLS (SELECTED_FRAME (), 1 + info.srWindow.Right -
707 info.srWindow.Left);
710 /* Setup w32_display_info structure for this frame. */
712 w32_initialize_display_info (build_string ("Console"));
717 DEFUN ("set-screen-color", Fset_screen_color, Sset_screen_color, 2, 2, 0,
718 doc: /* Set screen colors. */)
719 (Lisp_Object foreground, Lisp_Object background)
721 char_attr_normal = XFASTINT (foreground) + (XFASTINT (background) << 4);
723 Frecenter (Qnil);
724 return Qt;
727 DEFUN ("set-cursor-size", Fset_cursor_size, Sset_cursor_size, 1, 1, 0,
728 doc: /* Set cursor size. */)
729 (Lisp_Object size)
731 CONSOLE_CURSOR_INFO cci;
732 cci.dwSize = XFASTINT (size);
733 cci.bVisible = TRUE;
734 (void) SetConsoleCursorInfo (cur_screen, &cci);
736 return Qt;
739 void
740 syms_of_ntterm (void)
742 DEFVAR_BOOL ("w32-use-full-screen-buffer",
743 w32_use_full_screen_buffer,
744 doc: /* Non-nil means make terminal frames use the full screen buffer dimensions.
745 This is desirable when running Emacs over telnet.
746 A value of nil means use the current console window dimensions; this
747 may be preferrable when working directly at the console with a large
748 scroll-back buffer. */);
749 w32_use_full_screen_buffer = 0;
751 defsubr (&Sset_screen_color);
752 defsubr (&Sset_cursor_size);
753 defsubr (&Sset_message_beep);