mirrored from git://git.sv.gnu.org/emacs.git
/
keyboard.c
12674 lines (10770 loc) · 389 KB
/
keyboard.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Keyboard and mouse input; editor command loop.
Copyright (C) 1985-1989, 1993-1997, 1999-2022 Free Software Foundation,
Inc.
This file is part of GNU Emacs.
GNU Emacs is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at
your option) any later version.
GNU Emacs is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include <config.h>
#include <sys/stat.h>
#include "lisp.h"
#include "coding.h"
#include "termchar.h"
#include "termopts.h"
#include "frame.h"
#include "termhooks.h"
#include "macros.h"
#include "keyboard.h"
#include "window.h"
#include "commands.h"
#include "character.h"
#include "buffer.h"
#include "dispextern.h"
#include "syntax.h"
#include "intervals.h"
#include "keymap.h"
#include "blockinput.h"
#include "sysstdio.h"
#include "systime.h"
#include "atimer.h"
#include "process.h"
#include "menu.h"
#include <errno.h>
#ifdef HAVE_PTHREAD
#include <pthread.h>
#endif
#ifdef MSDOS
#include "msdos.h"
#include <time.h>
#else /* not MSDOS */
#include <sys/ioctl.h>
#endif /* not MSDOS */
#if defined USABLE_FIONREAD && defined USG5_4
# include <sys/filio.h>
#endif
#include "syssignal.h"
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <ignore-value.h>
#include "pdumper.h"
#ifdef HAVE_WINDOW_SYSTEM
#include TERM_HEADER
#endif /* HAVE_WINDOW_SYSTEM */
/* Work around GCC bug 54561. */
#if GNUC_PREREQ (4, 3, 0)
# pragma GCC diagnostic ignored "-Wclobbered"
#endif
#ifdef WINDOWSNT
char const DEV_TTY[] = "CONOUT$";
#else
char const DEV_TTY[] = "/dev/tty";
#endif
/* Variables for blockinput.h: */
/* Positive if interrupt input is blocked right now. */
volatile int interrupt_input_blocked;
/* True means an input interrupt or alarm signal has arrived.
The maybe_quit function checks this. */
volatile bool pending_signals;
enum { KBD_BUFFER_SIZE = 4096 };
KBOARD *initial_kboard;
KBOARD *current_kboard;
static KBOARD *all_kboards;
/* True in the single-kboard state, false in the any-kboard state. */
static bool single_kboard;
/* Minimum allowed size of the recent_keys vector. */
#define MIN_NUM_RECENT_KEYS (100)
/* Index for storing next element into recent_keys. */
static int recent_keys_index;
/* Total number of elements stored into recent_keys. */
static int total_keys;
/* Size of the recent_keys vector. */
static int lossage_limit = 3 * MIN_NUM_RECENT_KEYS;
/* This vector holds the last lossage_limit keystrokes. */
static Lisp_Object recent_keys;
/* Vector holding the key sequence that invoked the current command.
It is reused for each command, and it may be longer than the current
sequence; this_command_key_count indicates how many elements
actually mean something.
It's easier to staticpro a single Lisp_Object than an array. */
Lisp_Object this_command_keys;
ptrdiff_t this_command_key_count;
/* This vector is used as a buffer to record the events that were actually read
by read_key_sequence. */
static Lisp_Object raw_keybuf;
static int raw_keybuf_count;
#define GROW_RAW_KEYBUF \
if (raw_keybuf_count == ASIZE (raw_keybuf)) \
raw_keybuf = larger_vector (raw_keybuf, 1, -1)
/* Number of elements of this_command_keys
that precede this key sequence. */
static ptrdiff_t this_single_command_key_start;
#ifdef HAVE_STACK_OVERFLOW_HANDLING
/* For longjmp to recover from C stack overflow. */
sigjmp_buf return_to_command_loop;
/* Message displayed by Vtop_level when recovering from C stack overflow. */
static Lisp_Object recover_top_level_message;
#endif /* HAVE_STACK_OVERFLOW_HANDLING */
/* Message normally displayed by Vtop_level. */
static Lisp_Object regular_top_level_message;
/* True while displaying for echoing. Delays C-g throwing. */
static bool echoing;
/* Non-null means we can start echoing at the next input pause even
though there is something in the echo area. */
static struct kboard *ok_to_echo_at_next_pause;
/* The kboard last echoing, or null for none. Reset to 0 in
cancel_echoing. If non-null, and a current echo area message
exists, and echo_message_buffer is eq to the current message
buffer, we know that the message comes from echo_kboard. */
struct kboard *echo_kboard;
/* The buffer used for echoing. Set in echo_now, reset in
cancel_echoing. */
Lisp_Object echo_message_buffer;
/* Character that causes a quit. Normally C-g.
If we are running on an ordinary terminal, this must be an ordinary
ASCII char, since we want to make it our interrupt character.
If we are not running on an ordinary terminal, it still needs to be
an ordinary ASCII char. This character needs to be recognized in
the input interrupt handler. At this point, the keystroke is
represented as a struct input_event, while the desired quit
character is specified as a lispy event. The mapping from struct
input_events to lispy events cannot run in an interrupt handler,
and the reverse mapping is difficult for anything but ASCII
keystrokes.
FOR THESE ELABORATE AND UNSATISFYING REASONS, quit_char must be an
ASCII character. */
int quit_char;
/* Current depth in recursive edits. */
EMACS_INT command_loop_level;
/* If not Qnil, this is a switch-frame event which we decided to put
off until the end of a key sequence. This should be read as the
next command input, after any unread_command_events.
read_key_sequence uses this to delay switch-frame events until the
end of the key sequence; Fread_char uses it to put off switch-frame
events until a non-ASCII event is acceptable as input. */
Lisp_Object unread_switch_frame;
/* Last size recorded for a current buffer which is not a minibuffer. */
static ptrdiff_t last_non_minibuf_size;
uintmax_t num_input_events;
ptrdiff_t point_before_last_command_or_undo;
struct buffer *buffer_before_last_command_or_undo;
/* Value of num_nonmacro_input_events as of last auto save. */
static intmax_t last_auto_save;
/* The value of point when the last command was started. */
static ptrdiff_t last_point_position;
/* The frame in which the last input event occurred, or Qmacro if the
last event came from a macro. We use this to determine when to
generate switch-frame events. This may be cleared by functions
like Fselect_frame, to make sure that a switch-frame event is
generated by the next character.
FIXME: This is modified by a signal handler so it should be volatile.
It's exported to Lisp, though, so it can't simply be marked
'volatile' here. */
Lisp_Object internal_last_event_frame;
/* `read_key_sequence' stores here the command definition of the
key sequence that it reads. */
static Lisp_Object read_key_sequence_cmd;
static Lisp_Object read_key_sequence_remapped;
/* File in which we write all commands we read. */
static FILE *dribble;
/* True if input is available. */
bool input_pending;
/* True if more input was available last time we read an event.
Since redisplay can take a significant amount of time and is not
indispensable to perform the user's commands, when input arrives
"too fast", Emacs skips redisplay. More specifically, if the next
command has already been input when we finish the previous command,
we skip the intermediate redisplay.
This is useful to try and make sure Emacs keeps up with fast input
rates, such as auto-repeating keys. But in some cases, this proves
too conservative: we may end up disabling redisplay for the whole
duration of a key repetition, even though we could afford to
redisplay every once in a while.
So we "sample" the input_pending flag before running a command and
use *that* value after running the command to decide whether to
skip redisplay or not. This way, we only skip redisplay if we
really can't keep up with the repeat rate.
This only makes a difference if the next input arrives while running the
command, which is very unlikely if the command is executed quickly.
IOW this tends to avoid skipping redisplay after a long running command
(which is a case where skipping redisplay is not very useful since the
redisplay time is small compared to the time it took to run the command).
A typical use case is when scrolling. Scrolling time can be split into:
- Time to do jit-lock on the newly displayed portion of buffer.
- Time to run the actual scroll command.
- Time to perform the redisplay.
Jit-lock can happen either during the command or during the redisplay.
In the most painful cases, the jit-lock time is the one that dominates.
Also jit-lock can be tweaked (via jit-lock-defer) to delay its job, at the
cost of temporary inaccuracy in display and scrolling.
So without input_was_pending, what typically happens is the following:
- when the command starts, there's no pending input (yet).
- the scroll command triggers jit-lock.
- during the long jit-lock time the next input arrives.
- at the end of the command, we check input_pending and hence decide to
skip redisplay.
- we read the next input and start over.
End result: all the hard work of jit-locking is "wasted" since redisplay
doesn't actually happens (at least not before the input rate slows down).
With input_was_pending redisplay is still skipped if Emacs can't keep up
with the input rate, but if it can keep up just enough that there's no
input_pending when we begin the command, then redisplay is not skipped
which results in better feedback to the user. */
bool input_was_pending;
/* Circular buffer for pre-read keyboard input. */
static union buffered_input_event kbd_buffer[KBD_BUFFER_SIZE];
/* Pointer to next available character in kbd_buffer.
If kbd_fetch_ptr == kbd_store_ptr, the buffer is empty. */
static union buffered_input_event *kbd_fetch_ptr;
/* Pointer to next place to store character in kbd_buffer. */
static union buffered_input_event *kbd_store_ptr;
/* The above pair of variables forms a "queue empty" flag. When we
enqueue a non-hook event, we increment kbd_store_ptr. When we
dequeue a non-hook event, we increment kbd_fetch_ptr. We say that
there is input available if the two pointers are not equal.
Why not just have a flag set and cleared by the enqueuing and
dequeuing functions? The code is a bit simpler this way. */
static void recursive_edit_unwind (Lisp_Object buffer);
static Lisp_Object command_loop (void);
static void echo_now (void);
static ptrdiff_t echo_length (void);
/* Incremented whenever a timer is run. */
unsigned timers_run;
/* Address (if not 0) of struct timespec to zero out if a SIGIO interrupt
happens. */
struct timespec *input_available_clear_time;
/* True means use SIGIO interrupts; false means use CBREAK mode.
Default is true if INTERRUPT_INPUT is defined. */
bool interrupt_input;
/* Nonzero while interrupts are temporarily deferred during redisplay. */
bool interrupts_deferred;
/* The time when Emacs started being idle. */
static struct timespec timer_idleness_start_time;
/* After Emacs stops being idle, this saves the last value
of timer_idleness_start_time from when it was idle. */
static struct timespec timer_last_idleness_start_time;
/* Global variable declarations. */
/* Flags for readable_events. */
#define READABLE_EVENTS_DO_TIMERS_NOW (1 << 0)
#define READABLE_EVENTS_FILTER_EVENTS (1 << 1)
#define READABLE_EVENTS_IGNORE_SQUEEZABLES (1 << 2)
/* Function for init_keyboard to call with no args (if nonzero). */
static void (*keyboard_init_hook) (void);
static bool get_input_pending (int);
static bool readable_events (int);
static Lisp_Object read_char_x_menu_prompt (Lisp_Object,
Lisp_Object, bool *);
static Lisp_Object read_char_minibuf_menu_prompt (int, Lisp_Object);
static Lisp_Object make_lispy_event (struct input_event *);
static Lisp_Object make_lispy_movement (struct frame *, Lisp_Object,
enum scroll_bar_part,
Lisp_Object, Lisp_Object,
Time);
static Lisp_Object modify_event_symbol (ptrdiff_t, int, Lisp_Object,
Lisp_Object, const char *const *,
Lisp_Object *, ptrdiff_t);
static Lisp_Object make_lispy_switch_frame (Lisp_Object);
static Lisp_Object make_lispy_focus_in (Lisp_Object);
static Lisp_Object make_lispy_focus_out (Lisp_Object);
static bool help_char_p (Lisp_Object);
static void save_getcjmp (sys_jmp_buf);
static void restore_getcjmp (void *);
static Lisp_Object apply_modifiers (int, Lisp_Object);
static void restore_kboard_configuration (int);
static void handle_interrupt (bool);
static AVOID quit_throw_to_read_char (bool);
static void timer_start_idle (void);
static void timer_stop_idle (void);
static void timer_resume_idle (void);
static void deliver_user_signal (int);
static char *find_user_signal_name (int);
static void store_user_signal_events (void);
/* Advance or retreat a buffered input event pointer. */
static union buffered_input_event *
next_kbd_event (union buffered_input_event *ptr)
{
return ptr == kbd_buffer + KBD_BUFFER_SIZE - 1 ? kbd_buffer : ptr + 1;
}
#ifdef HAVE_X11
static union buffered_input_event *
prev_kbd_event (union buffered_input_event *ptr)
{
return ptr == kbd_buffer ? kbd_buffer + KBD_BUFFER_SIZE - 1 : ptr - 1;
}
#endif
/* Like EVENT_START, but assume EVENT is an event.
This pacifies gcc -Wnull-dereference, which might otherwise
complain about earlier checks that EVENT is indeed an event. */
static Lisp_Object
xevent_start (Lisp_Object event)
{
return XCAR (XCDR (event));
}
/* These setters are used only in this file, so they can be private. */
static void
kset_echo_string (struct kboard *kb, Lisp_Object val)
{
kb->echo_string_ = val;
}
static void
kset_echo_prompt (struct kboard *kb, Lisp_Object val)
{
kb->echo_prompt_ = val;
}
static void
kset_kbd_queue (struct kboard *kb, Lisp_Object val)
{
kb->kbd_queue_ = val;
}
static void
kset_keyboard_translate_table (struct kboard *kb, Lisp_Object val)
{
kb->Vkeyboard_translate_table_ = val;
}
static void
kset_last_prefix_arg (struct kboard *kb, Lisp_Object val)
{
kb->Vlast_prefix_arg_ = val;
}
static void
kset_last_repeatable_command (struct kboard *kb, Lisp_Object val)
{
kb->Vlast_repeatable_command_ = val;
}
static void
kset_local_function_key_map (struct kboard *kb, Lisp_Object val)
{
kb->Vlocal_function_key_map_ = val;
}
static void
kset_overriding_terminal_local_map (struct kboard *kb, Lisp_Object val)
{
kb->Voverriding_terminal_local_map_ = val;
}
static void
kset_real_last_command (struct kboard *kb, Lisp_Object val)
{
kb->Vreal_last_command_ = val;
}
static void
kset_system_key_syms (struct kboard *kb, Lisp_Object val)
{
kb->system_key_syms_ = val;
}
static bool
echo_keystrokes_p (void)
{
return (FLOATP (Vecho_keystrokes) ? XFLOAT_DATA (Vecho_keystrokes) > 0.0
: FIXNUMP (Vecho_keystrokes) ? XFIXNUM (Vecho_keystrokes) > 0
: false);
}
/* Add C to the echo string, without echoing it immediately. C can be
a character, which is pretty-printed, or a symbol, whose name is
printed. */
static void
echo_add_key (Lisp_Object c)
{
char initbuf[KEY_DESCRIPTION_SIZE + 100];
ptrdiff_t size = sizeof initbuf;
char *buffer = initbuf;
char *ptr = buffer;
Lisp_Object echo_string = KVAR (current_kboard, echo_string);
USE_SAFE_ALLOCA;
if (STRINGP (echo_string) && SCHARS (echo_string) > 0)
/* Add a space at the end as a separator between keys. */
ptr++[0] = ' ';
/* If someone has passed us a composite event, use its head symbol. */
c = EVENT_HEAD (c);
if (FIXNUMP (c))
ptr = push_key_description (XFIXNUM (c), ptr);
else if (SYMBOLP (c))
{
Lisp_Object name = SYMBOL_NAME (c);
ptrdiff_t nbytes = SBYTES (name);
if (size - (ptr - buffer) < nbytes)
{
ptrdiff_t offset = ptr - buffer;
size = max (2 * size, size + nbytes);
buffer = SAFE_ALLOCA (size);
ptr = buffer + offset;
}
ptr += copy_text (SDATA (name), (unsigned char *) ptr, nbytes,
STRING_MULTIBYTE (name), 1);
}
if ((NILP (echo_string) || SCHARS (echo_string) == 0)
&& help_char_p (c))
{
static const char text[] = " (Type ? for further options)";
int len = sizeof text - 1;
if (size - (ptr - buffer) < len)
{
ptrdiff_t offset = ptr - buffer;
size += len;
buffer = SAFE_ALLOCA (size);
ptr = buffer + offset;
}
memcpy (ptr, text, len);
ptr += len;
}
kset_echo_string
(current_kboard,
concat2 (echo_string, make_string (buffer, ptr - buffer)));
SAFE_FREE ();
}
/* Temporarily add a dash to the end of the echo string if it's not
empty, so that it serves as a mini-prompt for the very next
character. */
static void
echo_dash (void)
{
/* Do nothing if not echoing at all. */
if (NILP (KVAR (current_kboard, echo_string)))
return;
if (!current_kboard->immediate_echo
&& SCHARS (KVAR (current_kboard, echo_string)) == 0)
return;
/* Do nothing if we just printed a prompt. */
if (STRINGP (KVAR (current_kboard, echo_prompt))
&& (SCHARS (KVAR (current_kboard, echo_prompt))
== SCHARS (KVAR (current_kboard, echo_string))))
return;
/* Do nothing if we have already put a dash at the end. */
if (SCHARS (KVAR (current_kboard, echo_string)) > 1)
{
Lisp_Object last_char, prev_char, idx;
idx = make_fixnum (SCHARS (KVAR (current_kboard, echo_string)) - 2);
prev_char = Faref (KVAR (current_kboard, echo_string), idx);
idx = make_fixnum (SCHARS (KVAR (current_kboard, echo_string)) - 1);
last_char = Faref (KVAR (current_kboard, echo_string), idx);
if (XFIXNUM (last_char) == '-' && XFIXNUM (prev_char) != ' ')
return;
}
/* Put a dash at the end of the buffer temporarily,
but make it go away when the next character is added. */
AUTO_STRING (dash, "-");
kset_echo_string (current_kboard,
concat2 (KVAR (current_kboard, echo_string), dash));
echo_now ();
}
static void
echo_update (void)
{
if (current_kboard->immediate_echo)
{
ptrdiff_t i;
Lisp_Object prompt = KVAR (current_kboard, echo_prompt);
Lisp_Object prefix = call0 (Qinternal_echo_keystrokes_prefix);
kset_echo_string (current_kboard,
NILP (prompt) ? prefix
: NILP (prefix) ? prompt
: concat2 (prompt, prefix));
for (i = 0; i < this_command_key_count; i++)
{
Lisp_Object c;
c = AREF (this_command_keys, i);
if (! (EVENT_HAS_PARAMETERS (c)
&& EQ (EVENT_HEAD_KIND (EVENT_HEAD (c)), Qmouse_movement)))
echo_add_key (c);
}
echo_now ();
}
}
/* Display the current echo string, and begin echoing if not already
doing so. */
static void
echo_now (void)
{
if (!current_kboard->immediate_echo
/* This test breaks calls that use `echo_now' to display the echo_prompt.
&& echo_keystrokes_p () */)
{
current_kboard->immediate_echo = true;
echo_update ();
/* Put a dash at the end to invite the user to type more. */
echo_dash ();
}
echoing = true;
/* FIXME: Use call (Qmessage) so it can be advised (e.g. emacspeak). */
message3_nolog (KVAR (current_kboard, echo_string));
echoing = false;
/* Record in what buffer we echoed, and from which kboard. */
echo_message_buffer = echo_area_buffer[0];
echo_kboard = current_kboard;
if (waiting_for_input && !NILP (Vquit_flag))
quit_throw_to_read_char (0);
}
/* Turn off echoing, for the start of a new command. */
void
cancel_echoing (void)
{
current_kboard->immediate_echo = false;
kset_echo_prompt (current_kboard, Qnil);
kset_echo_string (current_kboard, Qnil);
ok_to_echo_at_next_pause = NULL;
echo_kboard = NULL;
echo_message_buffer = Qnil;
}
/* Return the length of the current echo string. */
static ptrdiff_t
echo_length (void)
{
return (STRINGP (KVAR (current_kboard, echo_string))
? SCHARS (KVAR (current_kboard, echo_string))
: 0);
}
/* Truncate the current echo message to its first LEN chars.
This and echo_char get used by read_key_sequence when the user
switches frames while entering a key sequence. */
static void
echo_truncate (ptrdiff_t nchars)
{
Lisp_Object es = KVAR (current_kboard, echo_string);
if (STRINGP (es) && SCHARS (es) > nchars)
kset_echo_string (current_kboard,
Fsubstring (KVAR (current_kboard, echo_string),
make_fixnum (0), make_fixnum (nchars)));
truncate_echo_area (nchars);
}
/* Functions for manipulating this_command_keys. */
static void
add_command_key (Lisp_Object key)
{
if (this_command_key_count >= ASIZE (this_command_keys))
this_command_keys = larger_vector (this_command_keys, 1, -1);
ASET (this_command_keys, this_command_key_count, key);
++this_command_key_count;
}
Lisp_Object
recursive_edit_1 (void)
{
ptrdiff_t count = SPECPDL_INDEX ();
Lisp_Object val;
if (command_loop_level > 0)
{
specbind (Qstandard_output, Qt);
specbind (Qstandard_input, Qt);
}
#ifdef HAVE_WINDOW_SYSTEM
/* The command loop has started an hourglass timer, so we have to
cancel it here, otherwise it will fire because the recursive edit
can take some time. Do not check for display_hourglass_p here,
because it could already be nil. */
cancel_hourglass ();
#endif
/* This function may have been called from a debugger called from
within redisplay, for instance by Edebugging a function called
from fontification-functions. We want to allow redisplay in
the debugging session.
The recursive edit is left with a `(throw exit ...)'. The `exit'
tag is not caught anywhere in redisplay, i.e. when we leave the
recursive edit, the original redisplay leading to the recursive
edit will be unwound. The outcome should therefore be safe. */
specbind (Qinhibit_redisplay, Qnil);
redisplaying_p = 0;
/* This variable stores buffers that have changed so that an undo
boundary can be added. specbind this so that changes in the
recursive edit will not result in undo boundaries in buffers
changed before we entered there recursive edit.
See Bug #23632.
*/
specbind (Qundo_auto__undoably_changed_buffers, Qnil);
val = command_loop ();
if (EQ (val, Qt))
quit ();
/* Handle throw from read_minibuf when using minibuffer
while it's active but we're in another window. */
if (STRINGP (val))
xsignal1 (Qerror, val);
if (FUNCTIONP (val))
call0 (val);
return unbind_to (count, Qnil);
}
/* When an auto-save happens, record the "time", and don't do again soon. */
void
record_auto_save (void)
{
last_auto_save = num_nonmacro_input_events;
}
/* Make an auto save happen as soon as possible at command level. */
#ifdef SIGDANGER
void
force_auto_save_soon (void)
{
last_auto_save = - auto_save_interval - 1;
}
#endif
DEFUN ("recursive-edit", Frecursive_edit, Srecursive_edit, 0, 0, "",
doc: /* Invoke the editor command loop recursively.
To get out of the recursive edit, a command can throw to `exit' -- for
instance (throw \\='exit nil).
The following values (last argument to `throw') can be used when
throwing to \\='exit:
- t causes `recursive-edit' to quit, so that control returns to the
command loop one level up.
- A string causes `recursive-edit' to signal an error, printing that
string as the error message.
- A function causes `recursive-edit' to call that function with no
arguments, and then return normally.
- Any other value causes `recursive-edit' to return normally to the
function that called it.
This function is called by the editor initialization to begin editing. */)
(void)
{
ptrdiff_t count = SPECPDL_INDEX ();
Lisp_Object buffer;
/* If we enter while input is blocked, don't lock up here.
This may happen through the debugger during redisplay. */
if (input_blocked_p ())
return Qnil;
if (command_loop_level >= 0
&& current_buffer != XBUFFER (XWINDOW (selected_window)->contents))
buffer = Fcurrent_buffer ();
else
buffer = Qnil;
/* Don't do anything interesting between the increment and the
record_unwind_protect! Otherwise, we could get distracted and
never decrement the counter again. */
command_loop_level++;
update_mode_lines = 17;
record_unwind_protect (recursive_edit_unwind, buffer);
/* If we leave recursive_edit_1 below with a `throw' for instance,
like it is done in the splash screen display, we have to
make sure that we restore single_kboard as command_loop_1
would have done if it were left normally. */
if (command_loop_level > 0)
temporarily_switch_to_single_kboard (SELECTED_FRAME ());
recursive_edit_1 ();
return unbind_to (count, Qnil);
}
void
recursive_edit_unwind (Lisp_Object buffer)
{
if (BUFFERP (buffer))
Fset_buffer (buffer);
command_loop_level--;
update_mode_lines = 18;
}
/* If we're in single_kboard state for kboard KBOARD,
get out of it. */
void
not_single_kboard_state (KBOARD *kboard)
{
if (kboard == current_kboard)
single_kboard = false;
}
/* Maintain a stack of kboards, so other parts of Emacs
can switch temporarily to the kboard of a given frame
and then revert to the previous status. */
struct kboard_stack
{
KBOARD *kboard;
struct kboard_stack *next;
};
static struct kboard_stack *kboard_stack;
void
push_kboard (struct kboard *k)
{
struct kboard_stack *p = xmalloc (sizeof *p);
p->next = kboard_stack;
p->kboard = current_kboard;
kboard_stack = p;
current_kboard = k;
}
void
pop_kboard (void)
{
struct terminal *t;
struct kboard_stack *p = kboard_stack;
bool found = false;
for (t = terminal_list; t; t = t->next_terminal)
{
if (t->kboard == p->kboard)
{
current_kboard = p->kboard;
found = true;
break;
}
}
if (!found)
{
/* The terminal we remembered has been deleted. */
current_kboard = FRAME_KBOARD (SELECTED_FRAME ());
single_kboard = false;
}
kboard_stack = p->next;
xfree (p);
}
/* Switch to single_kboard mode, making current_kboard the only KBOARD
from which further input is accepted. If F is non-nil, set its
KBOARD as the current keyboard.
This function uses record_unwind_protect_int to return to the previous
state later.
If Emacs is already in single_kboard mode, and F's keyboard is
locked, then this function will throw an error. */
void
temporarily_switch_to_single_kboard (struct frame *f)
{
bool was_locked = single_kboard;
if (was_locked)
{
if (f != NULL && FRAME_KBOARD (f) != current_kboard)
/* We can not switch keyboards while in single_kboard mode.
In rare cases, Lisp code may call `recursive-edit' (or
`read-minibuffer' or `y-or-n-p') after it switched to a
locked frame. For example, this is likely to happen
when server.el connects to a new terminal while Emacs is in
single_kboard mode. It is best to throw an error instead
of presenting the user with a frozen screen. */
error ("Terminal %d is locked, cannot read from it",
FRAME_TERMINAL (f)->id);
else
/* This call is unnecessary, but helps
`restore_kboard_configuration' discover if somebody changed
`current_kboard' behind our back. */
push_kboard (current_kboard);
}
else if (f != NULL)
current_kboard = FRAME_KBOARD (f);
single_kboard = true;
record_unwind_protect_int (restore_kboard_configuration, was_locked);
}
static void
restore_kboard_configuration (int was_locked)
{
single_kboard = was_locked;
if (was_locked)
{
struct kboard *prev = current_kboard;
pop_kboard ();
/* The pop should not change the kboard. */
if (single_kboard && current_kboard != prev)
emacs_abort ();
}
}
/* Handle errors that are not handled at inner levels
by printing an error message and returning to the editor command loop. */
static Lisp_Object
cmd_error (Lisp_Object data)
{
Lisp_Object old_level, old_length;
ptrdiff_t count = SPECPDL_INDEX ();
Lisp_Object conditions;
char macroerror[sizeof "After..kbd macro iterations: "
+ INT_STRLEN_BOUND (EMACS_INT)];
#ifdef HAVE_WINDOW_SYSTEM
if (display_hourglass_p)
cancel_hourglass ();
#endif
if (!NILP (executing_kbd_macro))
{
if (executing_kbd_macro_iterations == 1)
sprintf (macroerror, "After 1 kbd macro iteration: ");
else
sprintf (macroerror, "After %"pI"d kbd macro iterations: ",
executing_kbd_macro_iterations);
}
else
*macroerror = 0;
conditions = Fget (XCAR (data), Qerror_conditions);
if (NILP (Fmemq (Qminibuffer_quit, conditions)))
{
Vexecuting_kbd_macro = Qnil;
executing_kbd_macro = Qnil;
}
else if (!NILP (KVAR (current_kboard, defining_kbd_macro)))
/* An `M-x' command that signals a `minibuffer-quit' condition
that's part of a kbd macro. */
finalize_kbd_macro_chars ();
specbind (Qstandard_output, Qt);
specbind (Qstandard_input, Qt);
kset_prefix_arg (current_kboard, Qnil);
kset_last_prefix_arg (current_kboard, Qnil);
cancel_echoing ();
/* Avoid unquittable loop if data contains a circular list. */
old_level = Vprint_level;
old_length = Vprint_length;
XSETFASTINT (Vprint_level, 10);
XSETFASTINT (Vprint_length, 10);
cmd_error_internal (data, macroerror);
Vprint_level = old_level;
Vprint_length = old_length;
Vquit_flag = Qnil;
Vinhibit_quit = Qnil;
unbind_to (count, Qnil);
return make_fixnum (0);
}
/* Take actions on handling an error. DATA is the data that describes
the error.
CONTEXT is a C-string containing ASCII characters only which
describes the context in which the error happened. If we need to
generalize CONTEXT to allow multibyte characters, make it a Lisp
string. */
void