-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlisp.c
More file actions
3830 lines (3182 loc) · 138 KB
/
Copy pathlisp.c
File metadata and controls
3830 lines (3182 loc) · 138 KB
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
#include <libguile.h>
#include <obsidian/input.h>
#include <obsidian/theme.h>
#include <obsidian/window.h>
#include <stdbool.h>
#include "lisp.h"
#include "faces.h"
#include "libguile/scm.h"
#include "libguile/strings.h"
#include "rope.h"
#include "buffer.h"
#include "edit.h"
#include "textprop.h"
#include "theme.h"
#include "treesit.h"
#include "wm.h"
#include "minibuf.h"
#include "frame.h"
#include "fileio.h"
#include "glemax.h"
// Error handler that captures error message with full details
SCM error_handler(void *data, SCM key, SCM args) {
SCM port = scm_open_output_string();
// args structure: (subr message (arg ...) (extra ...))
if (scm_is_pair(args)) {
SCM rest = scm_cdr(args);
if (scm_is_pair(rest)) {
SCM message_template = scm_car(rest); // The error message template with ~A, ~S
SCM rest2 = scm_cdr(rest);
// Get the error arguments to fill into the template
SCM error_args = SCM_EOL;
if (scm_is_pair(rest2)) {
error_args = scm_car(rest2);
}
// Use scm_simple_format to properly format the message with arguments
// This will replace ~A, ~S with the actual values
if (!scm_is_null(error_args)) {
scm_simple_format(port, message_template, error_args);
} else {
scm_display(message_template, port);
}
} else {
// Fallback
scm_display(args, port);
}
} else {
// Simple fallback
scm_display(args, port);
}
SCM str = scm_get_output_string(port);
scm_close_port(port);
return str;
}
// Body function wrapper for scm_c_catch
SCM eval_string_body(void *data) {
const char *str = (const char *)data;
return scm_c_eval_string(str);
}
static SCM safe_eval_string(const char *str, bool *had_error) {
*had_error = false;
SCM result = scm_c_catch(SCM_BOOL_T,
eval_string_body, (void *)str,
error_handler, NULL,
NULL, NULL);
// Check if result is an error string (from our error_handler)
if (scm_is_string(result)) {
char *str_result = scm_to_locale_string(result);
// Check if it contains common error keys
if (strstr(str_result, "unbound-variable") ||
strstr(str_result, "wrong-type-arg") ||
strstr(str_result, "wrong-number-of-args") ||
strstr(str_result, "misc-error") ||
strstr(str_result, "out-of-range") ||
strstr(str_result, "system-error")) {
*had_error = true;
return result;
}
free(str_result);
}
return result;
}
// Helper to convert SCM to string for display
static char* scm_to_display_string(SCM obj) {
SCM str_port = scm_open_output_string();
if (scm_is_string(obj)) {
// For strings, add quotes manually but display the content
scm_display(scm_from_locale_string("\""), str_port);
scm_display(obj, str_port);
scm_display(scm_from_locale_string("\""), str_port);
} else {
// For other types, use write to get proper representation
scm_write(obj, str_port);
}
SCM str_scm = scm_get_output_string(str_port);
scm_close_port(str_port);
char *result = scm_to_locale_string(str_scm);
return result;
}
// Centralized function to display evaluation results
// Respects eval-display-prompt and eval-prompt variables
static void display_eval_result(SCM result, bool had_error) {
if (had_error) {
char *error_str = scm_to_locale_string(result);
message("%s", error_str);
free(error_str);
} else {
char *result_str = scm_to_display_string(result);
bool display_prompt = scm_get_bool("eval-display-prompt", true);
if (display_prompt) {
char *prompt = scm_get_string("eval-prompt", "=> ");
message("%s%s", prompt, result_str);
free(prompt);
} else {
message("%s", result_str);
}
free(result_str);
}
}
// Find the start of the last S-expression before point
static size_t find_sexp_start(Buffer *buf, size_t from_pos) {
if (from_pos == 0) return 0;
int paren_depth = 0;
size_t pos = from_pos;
// First, skip backwards over any whitespace
while (pos > 0) {
uint32_t ch = rope_char_at(buf->rope, pos - 1);
if (ch != ' ' && ch != '\n' && ch != '\t') {
break;
}
pos--;
}
if (pos == 0) return 0;
// Check what's immediately before point (after skipping whitespace)
uint32_t ch = rope_char_at(buf->rope, pos - 1);
if (ch == ')' || ch == ']' || ch == '}') {
// We're after a closing paren - find the matching opening paren
paren_depth = 1;
pos--;
while (pos > 0 && paren_depth > 0) {
ch = rope_char_at(buf->rope, pos - 1);
if (ch == ')' || ch == ']' || ch == '}') {
paren_depth++;
} else if (ch == '(' || ch == '[' || ch == '{') {
paren_depth--;
}
pos--;
}
// Now check for reader macros before the opening paren
while (pos > 0) {
ch = rope_char_at(buf->rope, pos - 1);
// Check for quote ', backquote `, comma , or comma-at ,@
if (ch == '\'' || ch == '`' || ch == ',') {
pos--;
// Handle ,@ (unquote-splicing)
if (ch == ',' && pos > 0 && rope_char_at(buf->rope, pos - 1) == '@') {
pos--;
}
} else if (ch == '#') {
// Could be #' or other syntax like #t, #f
// For safety, include the # if it's right before our sexp
pos--;
} else {
break;
}
}
return pos;
} else {
// We're after an atom (symbol, number, etc.) - find its start
pos--;
while (pos > 0) {
uint32_t prev = rope_char_at(buf->rope, pos - 1);
if (prev == ' ' || prev == '\n' || prev == '\t' ||
prev == '(' || prev == ')' || prev == '[' || prev == ']' ||
prev == '{' || prev == '}') {
break;
}
pos--;
}
// Check for reader macros before the atom
while (pos > 0) {
ch = rope_char_at(buf->rope, pos - 1);
if (ch == '\'' || ch == '`' || ch == ',') {
pos--;
if (ch == ',' && pos > 0 && rope_char_at(buf->rope, pos - 1) == '@') {
pos--;
}
} else if (ch == '#') {
pos--;
} else {
break;
}
}
return pos;
}
}
static char *eval_expression_print_format(SCM value) {
if (!scm_is_integer(value)) return NULL;
long n = scm_to_long(value);
// Get eval-expression-print-maximum-character from Scheme
long max_char = 127; // default fallback
SCM max_char_var = scm_c_lookup("eval-expression-print-maximum-character");
if (scm_is_true(scm_variable_bound_p(max_char_var))) {
SCM max_char_val = scm_variable_ref(max_char_var);
if (scm_is_integer(max_char_val))
max_char = scm_to_long(max_char_val);
}
if (n >= 0 && n <= max_char) {
char char_str[16] = {0};
if (n == 0) {
snprintf(char_str, sizeof(char_str), "#\\nul");
} else if (n < 32) {
// Guile named control characters
const char *ctrl_names[] = {
"nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel",
"bs", "tab", "newline", "vt", "page", "return", "so", "si",
"dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb",
"can", "em", "sub", "escape", "fs", "gs", "rs", "us"
};
snprintf(char_str, sizeof(char_str), "#\\%s", ctrl_names[n]);
} else if (n == 127) {
snprintf(char_str, sizeof(char_str), "#\\delete");
} else if (n >= 32 && n < 127) {
snprintf(char_str, sizeof(char_str), "#\\%c", (char)n);
}
if (char_str[0]) {
int len = snprintf(NULL, 0, " (#o%lo, #x%lx, %s)", n, n, char_str) + 1;
char *buf = malloc(len);
if (!buf) return NULL;
snprintf(buf, len, " (#o%lo, #x%lx, %s)", n, n, char_str);
return buf;
}
}
int len = snprintf(NULL, 0, " (#o%lo, #x%lx)", n, n) + 1;
char *buf = malloc(len);
if (!buf) return NULL;
snprintf(buf, len, " (#o%lo, #x%lx)", n, n);
return buf;
}
void eval_last_sexp(void) {
size_t point = current_buffer->pt;
if (point == 0) {
message("Beginning of buffer");
return;
}
size_t start = find_sexp_start(current_buffer, point);
if (start >= point) {
message("No expression before point");
return;
}
size_t len = point - start;
char *expr = malloc(len + 1);
if (!expr) {
message("Memory allocation failed");
return;
}
for (size_t i = 0; i < len; i++)
expr[i] = (char)rope_char_at(current_buffer->rope, start + i);
expr[len] = '\0';
bool had_error = false;
SCM result = safe_eval_string(expr, &had_error);
free(expr);
if (had_error) {
display_eval_result(result, had_error);
return;
}
int prefix = get_prefix_arg();
bool raw_prefix = false;
SCM raw_prefix_var = scm_c_lookup("raw-prefix-arg");
if (scm_is_true(scm_variable_bound_p(raw_prefix_var)))
raw_prefix = scm_is_true(scm_variable_ref(raw_prefix_var));
bool insert_into_buffer = (argument_manually_set && prefix != 0) || raw_prefix;
bool insert_full = (prefix < 0) || raw_prefix;
// Print the raw value (no prompt — used for both insert and message)
SCM port = scm_open_output_string();
scm_write(result, port);
char *str = scm_to_locale_string(scm_get_output_string(port));
if (insert_into_buffer) {
set_prefix_arg(1);
if (insert_full) {
char *full_suffix = eval_expression_print_format(result);
if (full_suffix) {
int total_len = strlen(str) + strlen(full_suffix) + 1;
char *combined = malloc(total_len);
if (combined) {
snprintf(combined, total_len, "%s%s", str, full_suffix);
insert(combined);
free(combined);
}
free(full_suffix);
} else {
insert(str);
}
} else {
insert(str);
}
} else {
// Show in message, respecting eval-display-prompt and eval-prompt
char *suffix = eval_expression_print_format(result);
char *value_with_suffix = NULL;
if (suffix) {
int total_len = strlen(str) + strlen(suffix) + 1;
value_with_suffix = malloc(total_len);
if (value_with_suffix)
snprintf(value_with_suffix, total_len, "%s%s", str, suffix);
free(suffix);
}
const char *display_str = value_with_suffix ? value_with_suffix : str;
bool display_prompt = scm_get_bool("eval-display-prompt", true);
if (display_prompt) {
char *prompt = scm_get_string("eval-prompt", "=> ");
message("%s%s", prompt, display_str);
free(prompt);
} else {
message("%s", display_str);
}
if (value_with_suffix) free(value_with_suffix);
}
free(str);
}
void eval_region() {
size_t start, end;
region_bounds(&start, &end);
if (start >= end) {
message("Empty region");
return;
}
size_t len = end - start;
char *code = malloc(len + 1);
if (!code) {
message("Memory allocation failed");
return;
}
for (size_t i = 0; i < len; i++) {
code[i] = (char)rope_char_at(current_buffer->rope, start + i);
}
code[len] = '\0';
bool had_error = false;
SCM result = safe_eval_string(code, &had_error);
display_eval_result(result, had_error);
free(code);
}
void eval_buffer() {
size_t len = rope_char_length(current_buffer->rope);
if (len == 0) {
message("Empty buffer");
return;
}
char *code = malloc(len + 1);
if (!code) {
message("Memory allocation failed");
return;
}
for (size_t i = 0; i < len; i++) {
code[i] = (char)rope_char_at(current_buffer->rope, i);
}
code[len] = '\0';
bool had_error = false;
SCM result = safe_eval_string(code, &had_error);
display_eval_result(result, had_error);
free(code);
}
// Helper to get procedure name as string
const char* scm_proc_name(SCM proc) {
if (scm_is_false(proc) || !scm_is_true(scm_procedure_p(proc))) {
return NULL;
}
SCM name = scm_procedure_name(proc);
if (scm_is_false(name)) {
return NULL;
}
return scm_to_locale_string(scm_symbol_to_string(name));
}
// Check if procedure matches a name
bool is_scm_proc(SCM proc, const char *name) {
const char *proc_name = scm_proc_name(proc);
if (!proc_name) return false;
return strcmp(proc_name, name) == 0;
}
inline int clip_to_bounds (int lower, int num, int upper) {
return max (lower, min (num, upper));
}
/// SCHEME BINDINGS
// NOT NEEDED
static SCM scm_char_or_string_p(SCM object) {
if (scm_is_string(object)) {
return SCM_BOOL_T;
}
if (scm_is_integer(object)) {
// Check if it's a valid character codepoint (0 to 0x10FFFF)
// but exclude surrogate pairs (0xD800 to 0xDFFF)
if (scm_is_unsigned_integer(object, 0, 0x10FFFF)) {
uint32_t cp = scm_to_uint32(object);
if (cp >= 0xD800 && cp <= 0xDFFF) {
return SCM_BOOL_F; // Surrogate pairs are not valid characters
}
return SCM_BOOL_T;
}
}
return SCM_BOOL_F;
}
static SCM scm_insert(SCM rest) {
// Iterate through all arguments
while (!scm_is_null(rest)) {
SCM arg = scm_car(rest);
if (scm_is_integer(arg)) {
// Single character (codepoint) - convert to string
uint32_t codepoint = scm_to_uint32(arg);
// Validate codepoint range
if (codepoint >= 0x110000 || (codepoint >= 0xD800 && codepoint <= 0xDFFF)) {
scm_wrong_type_arg("insert", 0, arg);
}
char utf8[5] = {0};
size_t len = utf8_encode(codepoint, utf8);
if (len > 0) {
utf8[len] = '\0';
insert(utf8);
}
} else if (scm_is_string(arg)) {
char *text = scm_to_utf8_string(arg);
insert(text);
free(text);
} else {
scm_wrong_type_arg("insert", 0, arg);
}
rest = scm_cdr(rest);
}
return SCM_UNSPECIFIED;
}
static SCM scm_quoted_insert(void) {
quoted_insert();
return SCM_UNSPECIFIED;
}
static SCM scm_delete_blank_lines(void) {
delete_blank_lines();
return SCM_UNSPECIFIED;
}
static SCM scm_back_to_indentation(void) {
back_to_indentation();
return SCM_UNSPECIFIED;
}
static SCM scm_set_mark(SCM pos) {
size_t position = scm_to_size_t(pos);
set_mark(position);
return SCM_UNSPECIFIED;
}
// TODO Whit positive ARG activate transient-mark-mode
static SCM scm_exchange_point_and_mark(void) {
exchange_point_and_mark();
return SCM_UNSPECIFIED;
}
static SCM scm_delete_region(void) {
delete_region();
return SCM_UNSPECIFIED;
}
static SCM scm_activate_mark(void) {
activate_mark();
return SCM_UNSPECIFIED;
}
static SCM scm_deactivate_mark(void) {
deactivate_mark();
return SCM_UNSPECIFIED;
}
/// Arg
static SCM sym_interactive_spec = SCM_BOOL_F;
void init_interactive_system(void) {
// Just a plain Scheme variable, not a parameter object
scm_c_define("current-interactive-proc", SCM_BOOL_F);
sym_interactive_spec = scm_from_utf8_symbol("interactive-spec");
scm_gc_protect_object(sym_interactive_spec);
}
static SCM read_interactive_args(const char *spec) {
if (!spec || *spec == '\0') return SCM_EOL;
SCM args = SCM_EOL;
const char *p = spec;
while (*p) {
switch (*p) {
case 'p': {
args = scm_append(scm_list_2(args,
scm_list_1(scm_from_int(get_prefix_arg()))));
p++;
break;
}
case 'P': {
SCM var = scm_c_lookup("prefix-arg");
SCM val = scm_is_false(var) ? SCM_BOOL_F : scm_variable_ref(var);
SCM raw = scm_is_integer(val) ? val : SCM_BOOL_F;
args = scm_append(scm_list_2(args, scm_list_1(raw)));
p++;
break;
}
case 'r': {
if (current_buffer->region.mark < 0) {
message("The mark is not set now, so there is no region");
return SCM_BOOL_F;
}
size_t pt = current_buffer->pt;
size_t mark = (size_t)current_buffer->region.mark;
size_t start = pt < mark ? pt : mark;
size_t end = pt < mark ? mark : pt;
args = scm_append(scm_list_2(args,
scm_list_2(scm_from_size_t(start),
scm_from_size_t(end))));
p++;
break;
}
/* case 'r': { */
/* if (!current_buffer->region.active || current_buffer->region.mark < 0) { */
/* message("The mark is not set now, so there is no region"); */
/* return SCM_BOOL_F; */
/* } */
/* size_t pt = current_buffer->pt; */
/* size_t mark = (size_t)current_buffer->region.mark; */
/* size_t start = pt < mark ? pt : mark; */
/* size_t end = pt < mark ? mark : pt; */
/* args = scm_append(scm_list_2(args, */
/* scm_list_2(scm_from_size_t(start), */
/* scm_from_size_t(end)))); */
/* p++; */
/* break; */
/* } */
case 's': {
p++;
char prompt[256] = "";
size_t i = 0;
while (*p && *p != '\n' && i < sizeof(prompt) - 1)
prompt[i++] = *p++;
prompt[i] = '\0';
if (*p == '\n') p++;
SCM hist = scm_from_locale_symbol("minibuffer-history");
char *input = read_from_minibuffer(prompt, NULL, hist);
if (!input) return SCM_BOOL_F;
args = scm_append(scm_list_2(args,
scm_list_1(scm_from_locale_string(input))));
free(input);
break;
}
case 'n': {
p++;
char prompt[256] = "Number: ";
size_t i = 0;
char tmp[256] = "";
while (*p && *p != '\n' && i < sizeof(tmp) - 1)
tmp[i++] = *p++;
tmp[i] = '\0';
if (*p == '\n') p++;
if (i > 0) strncpy(prompt, tmp, sizeof(prompt) - 1);
SCM hist = scm_from_locale_symbol("minibuffer-history");
char *input = read_from_minibuffer(prompt, NULL, hist);
if (!input) return SCM_BOOL_F;
int n = atoi(input);
free(input);
args = scm_append(scm_list_2(args, scm_list_1(scm_from_int(n))));
break;
}
case 'B':
case 'b': {
// 'b' — default is current buffer
// 'B' — default is other-buffer (like switch-to-buffer)
bool is_B = (*p == 'B');
p++;
char base_prompt[256] = "";
size_t i = 0;
char tmp[256] = "";
while (*p && *p != '\n' && i < sizeof(tmp) - 1)
tmp[i++] = *p++;
tmp[i] = '\0';
if (*p == '\n') p++;
if (i > 0)
strncpy(base_prompt, tmp, sizeof(base_prompt) - 1);
else
strncpy(base_prompt, is_B ? "Switch to buffer" : "Buffer",
sizeof(base_prompt) - 1);
Buffer *minibuf = selected_frame->wm.minibuffer_window->buffer;
Buffer *def_buf = is_B ? other_buffer() : current_buffer;
// Build completion list — 'B' excludes current buffer like Emacs
SCM buffer_names = SCM_EOL;
Buffer *buf = all_buffers;
do {
if (buf != minibuf && !(is_B && buf == current_buffer))
buffer_names = scm_cons(scm_from_locale_string(buf->name),
buffer_names);
buf = buf->next;
} while (buf != all_buffers);
// Build prompt with default, e.g. "Switch to buffer (default foo): "
char full_prompt[512];
snprintf(full_prompt, sizeof(full_prompt),
"%s (default %s): ", base_prompt,
def_buf ? def_buf->name : "none");
SCM hist = scm_from_locale_symbol("buffer-name-history");
char *input = read_from_minibuffer_with_completion(
full_prompt, NULL, buffer_names, SCM_BOOL_F, hist);
if (!input) return SCM_BOOL_F;
// Empty input -> use default
const char *chosen = (*input) ? input : (def_buf ? def_buf->name : "");
args = scm_append(scm_list_2(args,
scm_list_1(scm_from_locale_string(chosen))));
free(input);
break;
}
case 'f': {
p++;
char prompt[256] = "File: ";
size_t i = 0;
char tmp[256] = "";
while (*p && *p != '\n' && i < sizeof(tmp) - 1)
tmp[i++] = *p++;
tmp[i] = '\0';
if (*p == '\n') p++;
if (i > 0) strncpy(prompt, tmp, sizeof(prompt) - 1);
SCM read_file_name_func = scm_variable_ref(
scm_c_lookup("read-file-name"));
SCM result = scm_call_1(read_file_name_func,
scm_from_locale_string(prompt));
if (scm_is_false(result)) return SCM_BOOL_F;
args = scm_append(scm_list_2(args, scm_list_1(result)));
break;
}
default:
p++;
break;
}
}
return args;
}
typedef struct {
SCM proc;
SCM args;
} ApplyData;
static SCM apply_body(void *data) {
ApplyData *d = (ApplyData *)data;
return scm_apply_0(d->proc, d->args);
}
SCM call_interactively(SCM proc) {
if (!scm_is_true(scm_procedure_p(proc)))
return SCM_BOOL_F;
scm_c_define("current-interactive-proc", proc);
SCM spec_val = scm_procedure_property(proc, sym_interactive_spec);
SCM result = SCM_UNSPECIFIED;
if (scm_is_false(spec_val)) {
// No interactive spec — call with no args
result = scm_internal_catch(SCM_BOOL_T,
(scm_t_catch_body)scm_call_0, proc,
error_handler, NULL);
} else if (scm_is_string(spec_val)) {
char *spec = scm_to_locale_string(spec_val);
SCM args = read_interactive_args(spec);
free(spec);
if (scm_is_false(args) && !scm_is_null(args)) {
scm_c_define("current-interactive-proc", SCM_BOOL_F);
return SCM_UNSPECIFIED;
}
ApplyData data = { proc, args };
result = scm_internal_catch(SCM_BOOL_T,
apply_body, &data,
error_handler, NULL);
} else {
result = scm_internal_catch(SCM_BOOL_T,
(scm_t_catch_body)scm_call_0, proc,
error_handler, NULL);
}
scm_c_define("current-interactive-proc", SCM_BOOL_F);
return result;
}
static SCM scm_call_interactively(SCM proc) {
return call_interactively(proc);
}
static SCM scm_interactive(SCM spec) {
(void)spec;
return SCM_UNSPECIFIED;
}
static SCM scm_interactive_form(SCM proc) {
if (!scm_is_true(scm_procedure_p(proc))) return SCM_BOOL_F;
return scm_procedure_property(proc, sym_interactive_spec);
}
static SCM scm_commandp(SCM obj) {
if (!scm_is_true(scm_procedure_p(obj))) return SCM_BOOL_F;
SCM spec = scm_procedure_property(obj, sym_interactive_spec);
return scm_is_false(spec) ? SCM_BOOL_F : SCM_BOOL_T;
}
// NOTE This is our way to do (interactive) for C functions
// passed to scheme, so later M-x will have access
// to all commands registered trough this macro
#define DEFINE_SCM_COMMAND(scm_name, c_func, doc_string) \
static SCM scm_name(SCM arg) { \
if (!SCM_UNBNDP(arg)) { \
if (!scm_is_integer(arg)) { \
scm_wrong_type_arg(#scm_name, 1, arg); \
} \
set_prefix_arg(scm_to_int(arg)); \
} \
c_func(); \
return SCM_UNSPECIFIED; \
} \
static const char* scm_name##_doc = doc_string;
#include "theme.h"
DEFINE_SCM_COMMAND(scm_self_insert_command, self_insert_command,
"Insert the character you type."
"Whichever character C you type to run this command is inserted."
"The numeric prefix argument N says how many times to repeat the insertion."
"Before insertion, `expand-abbrev' is executed if the inserted character does"
"not have word syntax and the previous character in the buffer does."
"After insertion, `internal-auto-fill' is called if"
"`auto-fill-function' is non-nil and if the `auto-fill-chars' table has"
"a non-nil value for the inserted character. At the end, it runs"
"`post-self-insert-hook'.");
DEFINE_SCM_COMMAND(my_scm_newline, newline,
"Insert a newline, and move to left margin of the new line.\n"
"With prefix argument ARG, insert that many newlines.\n"
"\n"
"TODO If `electric-indent-mode' is enabled, this indents the final new line\n"
"that it adds, and reindents the preceding line. To just insert\n"
"a newline, use \\[electric-indent-just-newline].");
DEFINE_SCM_COMMAND(scm_open_line, open_line,
"Insert a newline and leave point before it.\n"
"TODO If there is a fill prefix and/or a `left-margin', insert them on\n"
"the new line if the line would have been blank.\n"
"With arg N, insert N newlines.");
DEFINE_SCM_COMMAND(scm_split_line, split_line,
"Split current line, moving portion beyond point vertically down.\n"
"TODO If the current line starts with `fill-prefix', insert it on the new\n"
"line as well. With prefix ARG, don't insert `fill-prefix' on new line.\n"
"\n"
"TODO When called from Lisp code, ARG may be a prefix string to copy.");
DEFINE_SCM_COMMAND(scm_kill_line, kill_line,
"Kill the rest of the current line; if no nonblanks there, kill thru newline.\n"
"With prefix argument ARG, kill that many lines from point.\n"
"Negative arguments kill lines backward.\n"
"With zero argument, kills the text before point on the current line.\n"
"\n"
"When calling from a program, a number counts as a prefix arg.\n"
"\n"
"To kill a whole line, when point is not at the beginning, type \\\n"
"\\[move-beginning-of-line] \\[kill-line] \\[kill-line].\n"
"\n"
"If option `kill-whole-line' is #t, then this command kills the whole line\n"
"including its terminating newline, when used at the beginning of a line\n"
"with no argument. As a consequence, you can always kill a whole line\n"
"by typing \\[move-beginning-of-line] \\[kill-line].\n"
"\n"
"If you want to append the killed line to the last killed text,\n"
"use \\[append-next-kill] before \\[kill-line].\n"
"\n"
"TODO If the buffer is read-only, Glemax will beep and refrain from deleting\n"
"the line, but put the line in the kill ring anyway. This means that\n"
"you can use this command to copy text from a read-only buffer.\n"
"\(If the variable `kill-read-only-ok' is #t, then this won't\n"
"even beep.\n");
DEFINE_SCM_COMMAND(scm_kill_word, kill_word,
"Kill characters forward until encountering the end of a word.\n"
"With argument ARG, do this that many times.");
DEFINE_SCM_COMMAND(scm_backward_kill_word, backward_kill_word,
"Kill characters backward until encountering the beginning of a word."
"With argument ARG, do this that many times.");
DEFINE_SCM_COMMAND(scm_kill_region, kill_region,
"Kill (\"cut\") text between point and mark.\n"
"This deletes the text from the buffer and saves it in the kill ring.\n"
"The command \\[yank] can retrieve it from there.\n"
"\(If you want to save the region without killing it, use \\[kill-ring-save].)\n"
"\n"
"TODO If you want to append the killed region to the last killed text,\n"
"use \\[append-next-kill] before \\[kill-region].\n"
"\n"
"TODO Any command that calls this function is a \"kill command\".\n"
"If the previous command was also a kill command,\n"
"the text killed this time appends to the text killed last time\n"
"to make one entry in the kill ring.\n"
"\n"
"TODO If the buffer is read-only, Glemax will beep and refrain from deleting\n"
"the text, but put the text in the kill ring anyway. This means that\n"
"you can use the killing commands to copy text from a read-only buffer.");
DEFINE_SCM_COMMAND(scm_copy_region_as_kill, copy_region_as_kill,
"Save the region as if killed, but don't kill it."
"\n"
"In Transient Mark mode, deactivate the mark.");
DEFINE_SCM_COMMAND(scm_yank, yank,
"Reinsert (\"paste\") the last stretch of killed text.\n"
"More precisely, reinsert the most recent kill, which is the stretch of\n"
"text most recently killed OR yanked, as returned by `current-kill' (which\n"
"see). Put point at the end, and set mark at the beginning without\n"
"activating it. With just \\[universal-argument] as argument, put point\n"
"at beginning, and mark at end.\n"
"TODO With argument N, reinsert the Nth most recent kill.\n"
"\n"
"TODO This command honors the `yank-handled-properties' and\n"
"`yank-excluded-properties' variables, and the `yank-handler' text\n"
"property, as described below.\n"
"\n"
"Properties listed in `yank-handled-properties' are processed,\n"
"then those listed in `yank-excluded-properties' are discarded.");
DEFINE_SCM_COMMAND(scm_duplicate_line, duplicate_line,
"Duplicate the current line N times."
"Interactively, N is the prefix numeric argument, and defaults to 1."
"The user option `duplicate-line-final-position' specifies where to"
"move point after duplicating the line."
"Also see the `copy-from-above-command' command.");
DEFINE_SCM_COMMAND(scm_duplicate_region, duplicate_region, NULL);
DEFINE_SCM_COMMAND(scm_duplicate_dwim, duplicate_dwim,
"Duplicate the current line or region N times."
"If the region is inactive, duplicate the current line (like `duplicate-line')."
"Otherwise, duplicate the region, which remains active afterwards."
"If the region is rectangular, duplicate on its right-hand side."
"Interactively, N is the prefix numeric argument, and defaults to 1."
"The variables `duplicate-line-final-position' and"
"`duplicate-region-final-position' control the position of point"
"and the region after the duplication.");
DEFINE_SCM_COMMAND(scm_read_only_mode, read_only_mode,
"Change whether the current buffer is read-only.");
// TODO Add Docstring for all those commands...
DEFINE_SCM_COMMAND(scm_save_buffer, save_buffer, NULL);
DEFINE_SCM_COMMAND(scm_set_mark_command, set_mark_command,
"Set the mark where point is, and activate it; or jump to the mark."
"Setting the mark also alters the region, which is the text"
"between point and mark; this is the closest equivalent in"
"Emacs to what some editors call the \"selection\"."
"\n"
"With no prefix argument, set the mark at point, and push the"
"old mark position on local mark ring. Also push the new mark on"
"global mark ring, if the previous mark was set in another buffer."
"\n"
"When Transient Mark Mode is off, immediately repeating this"
"command activates `transient-mark-mode' temporarily."
"\n"
"With prefix argument (e.g., \\[universal-argument] \\[set-mark-command]),"
"jump to the mark, and set the mark from"
"position popped off the local mark ring (this does not affect the global"
"mark ring). Use \\[pop-global-mark] to jump to a mark popped off the global"
"mark ring (see `pop-global-mark')."
"\n"
"If `set-mark-command-repeat-pop' is non-nil, repeating"
"the \\[set-mark-command] command with no prefix argument pops the next position"
"off the local (or global) mark ring and jumps there."
"\n"
"With \\[universal-argument] \\[universal-argument] as prefix"
"argument, unconditionally set mark where point is, even if"
"`set-mark-command-repeat-pop' is non-nil."
"\n"
"Novice Emacs Lisp programmers often try to use the mark for the wrong"
"purposes. See the documentation of `set-mark' for more information.");
DEFINE_SCM_COMMAND(scm_delete_indentation, delete_indentation,
"Join this line to previous and fix up whitespace at join."