This repository was archived by the owner on Feb 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 528
/
Copy pathedit.c
4117 lines (3568 loc) · 117 KB
/
edit.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
/*
Editor low level data handling and cursor fundamentals.
Copyright (C) 1996-2015
Free Software Foundation, Inc.
Written by:
Paul Sheer 1996, 1997
Ilia Maslakov <il.smind@gmail.com> 2009, 2010, 2011
Andrew Borodin <aborodin@vmail.ru> 2012, 2013
This file is part of the Midnight Commander.
The Midnight Commander 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.
The Midnight Commander 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file
* \brief Source: editor low level data handling and cursor fundamentals
* \author Paul Sheer
* \date 1996, 1997
*/
#include <config.h>
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <sys/stat.h>
#include <stdint.h> /* UINTMAX_MAX */
#include <stdlib.h>
#include "lib/global.h"
#include "lib/tty/color.h"
#include "lib/tty/tty.h" /* attrset() */
#include "lib/tty/key.h" /* is_idle() */
#include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
#include "lib/vfs/vfs.h"
#include "lib/strutil.h" /* utf string functions */
#include "lib/util.h" /* load_file_position(), save_file_position() */
#include "lib/timefmt.h" /* time formatting */
#include "lib/lock.h"
#include "lib/widget.h"
#ifdef HAVE_CHARSET
#include "lib/charsets.h" /* get_codepage_id */
#endif
#include "src/filemanager/usermenu.h" /* user_menu_cmd() */
#include "src/setup.h" /* option_tab_spacing */
#include "src/learn.h" /* learn_keys */
#include "src/keybind-defaults.h"
#include "edit-impl.h"
#include "editwidget.h"
#ifdef HAVE_ASPELL
#include "spell.h"
#endif
/*** global variables ****************************************************************************/
int option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
int option_typewriter_wrap = 0;
int option_auto_para_formatting = 0;
int option_fill_tabs_with_spaces = 0;
int option_return_does_auto_indent = 1;
int option_backspace_through_tabs = 0;
int option_fake_half_tabs = 1;
int option_save_mode = EDIT_QUICK_SAVE;
int option_save_position = 1;
int option_max_undo = 32768;
int option_persistent_selections = 1;
int option_cursor_beyond_eol = 0;
int option_line_state = 0;
int option_line_state_width = 0;
gboolean option_cursor_after_inserted_block = FALSE;
int option_state_full_filename = 0;
int option_autodetect_lb = 0;
int option_edit_right_extreme = 0;
int option_edit_left_extreme = 0;
int option_edit_top_extreme = 0;
int option_edit_bottom_extreme = 0;
int enable_show_tabs_tws = 1;
int option_check_nl_at_eof = 0;
int option_group_undo = 0;
int show_right_margin = 0;
char *option_backup_ext = NULL;
char *option_filesize_threshold = NULL;
unsigned int edit_stack_iterator = 0;
edit_stack_type edit_history_moveto[MAX_HISTORY_MOVETO];
/* magic sequense for say than block is vertical */
const char VERTICAL_MAGIC[] = { '\1', '\1', '\1', '\1', '\n' };
/*** file scope macro definitions ****************************************************************/
#define TEMP_BUF_LEN 1024
#define space_width 1
#define DETECT_LB_TYPE_BUFLEN BUF_MEDIUM
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/* detecting an error on save is easy: just check if every byte has been written. */
/* detecting an error on read, is not so easy 'cos there is not way to tell
whether you read everything or not. */
/* FIXME: add proper 'triple_pipe_open' to read, write and check errors. */
static const struct edit_filters
{
const char *read, *write, *extension;
} all_filters[] =
{
/* *INDENT-OFF* */
{ "xz -cd %s 2>&1", "xz > %s", ".xz"},
{ "lzma -cd %s 2>&1", "lzma > %s", ".lzma" },
{ "bzip2 -cd %s 2>&1", "bzip2 > %s", ".bz2" },
{ "gzip -cd %s 2>&1", "gzip > %s", ".gz" },
{ "gzip -cd %s 2>&1", "gzip > %s", ".Z" }
/* *INDENT-ON* */
};
static const off_t option_filesize_default_threshold = 64 * 1024 * 1024; /* 64 MB */
/* --------------------------------------------------------------------------------------------- */
/*** file scope functions ************************************************************************/
/* --------------------------------------------------------------------------------------------- */
static int
edit_load_status_update_cb (status_msg_t * sm)
{
simple_status_msg_t *ssm = SIMPLE_STATUS_MSG (sm);
edit_buffer_read_file_status_msg_t *rsm = (edit_buffer_read_file_status_msg_t *) sm;
Widget *wd = WIDGET (sm->dlg);
if (verbose)
label_set_textv (ssm->label, _("Loading: %3d%%"),
edit_buffer_calc_percent (rsm->buf, rsm->loaded));
else
label_set_text (ssm->label, _("Loading..."));
if (rsm->first)
{
int wd_width;
Widget *lw = WIDGET (ssm->label);
wd_width = max (wd->cols, lw->cols + 6);
widget_set_size (wd, wd->y, wd->x, wd->lines, wd_width);
widget_set_size (lw, lw->y, wd->x + (wd->cols - lw->cols) / 2, lw->lines, lw->cols);
rsm->first = FALSE;
}
return status_msg_common_update (sm);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Load file OR text into buffers. Set cursor to the beginning of file.
*
* @return FALSE on error.
*/
static gboolean
edit_load_file_fast (edit_buffer_t * buf, const vfs_path_t * filename_vpath)
{
int file;
gboolean ret;
edit_buffer_read_file_status_msg_t rsm;
gboolean aborted;
file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
if (file < 0)
{
gchar *errmsg;
errmsg =
g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
edit_error_dialog (_("Error"), errmsg);
g_free (errmsg);
return FALSE;
}
rsm.first = TRUE;
rsm.buf = buf;
rsm.loaded = 0;
status_msg_init (STATUS_MSG (&rsm), _("Load file"), 1.0, simple_status_msg_init_cb,
edit_load_status_update_cb, NULL);
ret = (edit_buffer_read_file (buf, file, buf->size, &rsm, &aborted) == buf->size);
status_msg_deinit (STATUS_MSG (&rsm));
if (!ret && !aborted)
{
gchar *errmsg;
errmsg = g_strdup_printf (_("Error reading %s"), vfs_path_as_str (filename_vpath));
edit_error_dialog (_("Error"), errmsg);
g_free (errmsg);
}
mc_close (file);
return ret;
}
/* --------------------------------------------------------------------------------------------- */
/** Return index of the filter or -1 is there is no appropriate filter */
static int
edit_find_filter (const vfs_path_t * filename_vpath)
{
size_t i, l;
if (filename_vpath == NULL)
return -1;
l = strlen (vfs_path_as_str (filename_vpath));
for (i = 0; i < G_N_ELEMENTS (all_filters); i++)
{
size_t e;
e = strlen (all_filters[i].extension);
if (l > e)
if (!strcmp (all_filters[i].extension, vfs_path_as_str (filename_vpath) + l - e))
return i;
}
return -1;
}
/* --------------------------------------------------------------------------------------------- */
static char *
edit_get_filter (const vfs_path_t * filename_vpath)
{
int i;
char *p, *quoted_name;
i = edit_find_filter (filename_vpath);
if (i < 0)
return NULL;
quoted_name = name_quote (vfs_path_as_str (filename_vpath), FALSE);
p = g_strdup_printf (all_filters[i].read, quoted_name);
g_free (quoted_name);
return p;
}
/* --------------------------------------------------------------------------------------------- */
static off_t
edit_insert_stream (WEdit * edit, FILE * f)
{
int c;
off_t i = 0;
while ((c = fgetc (f)) >= 0)
{
edit_insert (edit, c);
i++;
}
return i;
}
/* --------------------------------------------------------------------------------------------- */
/**
* Open file and create it if necessary.
*
* @param edit editor object
* @param filename_vpath file name
* @param st buffer for store stat info
* @return TRUE for success, FALSE for error.
*/
static gboolean
check_file_access (WEdit * edit, const vfs_path_t * filename_vpath, struct stat *st)
{
static uintmax_t threshold = UINTMAX_MAX;
int file;
gchar *errmsg = NULL;
gboolean ret = TRUE;
/* Try opening an existing file */
file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY, 0666);
if (file < 0)
{
/*
* Try creating the file. O_EXCL prevents following broken links
* and opening existing files.
*/
file = mc_open (filename_vpath, O_NONBLOCK | O_RDONLY | O_BINARY | O_CREAT | O_EXCL, 0666);
if (file < 0)
{
errmsg =
g_strdup_printf (_("Cannot open %s for reading"), vfs_path_as_str (filename_vpath));
goto cleanup;
}
/* New file, delete it if it's not modified or saved */
edit->delete_file = 1;
}
/* Check what we have opened */
if (mc_fstat (file, st) < 0)
{
errmsg =
g_strdup_printf (_("Cannot get size/permissions for %s"),
vfs_path_as_str (filename_vpath));
goto cleanup;
}
/* We want to open regular files only */
if (!S_ISREG (st->st_mode))
{
errmsg =
g_strdup_printf (_("\"%s\" is not a regular file"), vfs_path_as_str (filename_vpath));
goto cleanup;
}
/* get file size threshold for alarm */
if (threshold == UINTMAX_MAX)
{
gboolean err = FALSE;
threshold = parse_integer (option_filesize_threshold, &err);
if (err)
threshold = option_filesize_default_threshold;
}
/*
* Don't delete non-empty files.
* O_EXCL should prevent it, but let's be on the safe side.
*/
if (st->st_size > 0)
edit->delete_file = 0;
if ((uintmax_t) st->st_size > threshold)
{
int act;
errmsg = g_strdup_printf (_("File \"%s\" is too large.\nOpen it anyway?"),
vfs_path_as_str (filename_vpath));
act = edit_query_dialog2 (_("Warning"), errmsg, _("&Yes"), _("&No"));
MC_PTR_FREE (errmsg);
if (act != 0)
ret = FALSE;
}
cleanup:
(void) mc_close (file);
if (errmsg != NULL)
{
edit_error_dialog (_("Error"), errmsg);
g_free (errmsg);
ret = FALSE;
}
return ret;
}
/* --------------------------------------------------------------------------------------------- */
/**
* detect type of line breaks
*
*/
/* --------------------------------------------------------------------------------------------- */
static LineBreaks
detect_lb_type_buf (unsigned char *p, ssize_t sz)
{
LineBreaks detected_lb = LB_ASIS;
/* If there was error or file too short, give up */
if (sz <= 2)
return LB_ASIS;
p[(size_t) sz] = '\0';
/* Avoid ambiguity of our buffer breaking CR LF sequence */
if (p[sz - 1] == '\r') {
p[--sz] = '\0';
}
for (; sz--; p++) {
LineBreaks new_lb = LB_ASIS;
if (*p == '\r') {
if (p[1] == '\n') {
sz--; p++;
new_lb = LB_WIN;
} else {
new_lb = LB_MAC;
}
} else if (*p == '\n') {
/* LF CR is anomaly for text file, give up */
if (p[1] == '\r')
return LB_ASIS;
new_lb = LB_UNIX;
} else if (*p < 0x20 && *p != '\t' && *p != '\f') {
/* The only common special char in text files is tab, much
less commonly - form feed. Anything else - give up. */
return LB_ASIS;
}
/* If we detected a new lb, and it doesn't match previously
detected, give up */
if (new_lb != LB_ASIS) {
if (detected_lb != LB_ASIS && detected_lb != new_lb) {
return LB_ASIS;
}
detected_lb = new_lb;
}
}
/* LB_UNIX means that within buffer, we saw only LF breaks, but
we cannot be sure about entire file. So, go conservative route
and don't report to user in UI that this file has unix line
breaks. */
return detected_lb == LB_UNIX ? LB_ASIS : detected_lb;
}
static LineBreaks
detect_lb_type (const vfs_path_t *filename_vpath)
{
unsigned char buf[BUF_LARGE];
ssize_t file, sz;
file = mc_open (filename_vpath, O_RDONLY | O_BINARY);
if (file == -1)
return LB_ASIS;
sz = mc_read (file, buf, sizeof (buf) - 1);
mc_close (file);
return detect_lb_type_buf (buf, sz);
}
/* --------------------------------------------------------------------------------------------- */
/**
* Open the file and load it into the buffers, either directly or using
* a filter. Return TRUE on success, FALSE on error.
*
* Fast loading (edit_load_file_fast) is used when the file size is
* known. In this case the data is read into the buffers by blocks.
* If the file size is not known, the data is loaded byte by byte in
* edit_insert_file.
*
* @param edit editor object
* @return TRUE if file was successfully opened and loaded to buffers, FALSE otherwise
*/
static gboolean
edit_load_file (WEdit * edit)
{
gboolean fast_load = TRUE;
LineBreaks lb_type = LB_ASIS;
/* Cannot do fast load if a filter is used */
if (edit_find_filter (edit->filename_vpath) >= 0)
fast_load = FALSE;
/*
* FIXME: line end translation should disable fast loading as well
* Consider doing fseek() to the end and ftell() for the real size.
*/
if (edit->filename_vpath != NULL)
{
/*
* VFS may report file size incorrectly, and slow load is not a big
* deal considering overhead in VFS.
*/
if (!vfs_file_is_local (edit->filename_vpath))
fast_load = FALSE;
/* If we are dealing with a real file, check that it exists */
if (!check_file_access (edit, edit->filename_vpath, &edit->stat1))
{
edit_clean (edit);
return FALSE;
}
if (option_autodetect_lb)
lb_type = detect_lb_type (edit->filename_vpath);
if (lb_type != LB_ASIS && lb_type != LB_UNIX)
fast_load = FALSE;
}
else
{
/* nothing to load */
fast_load = FALSE;
}
if (fast_load)
{
edit_buffer_init (&edit->buffer, edit->stat1.st_size);
if (!edit_load_file_fast (&edit->buffer, edit->filename_vpath))
{
edit_clean (edit);
return FALSE;
}
}
else
{
edit_buffer_init (&edit->buffer, 0);
if (edit->filename_vpath != NULL
&& *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) != '\0')
{
edit->undo_stack_disable = 1;
if (edit_insert_file (edit, edit->filename_vpath, lb_type) < 0)
{
edit_clean (edit);
return FALSE;
}
edit->undo_stack_disable = 0;
}
}
edit->lb = lb_type;
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/** Restore saved cursor position in the file */
static void
edit_load_position (WEdit * edit)
{
long line, column;
off_t offset;
if (edit->filename_vpath == NULL
|| *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
return;
load_file_position (edit->filename_vpath, &line, &column, &offset, &edit->serialized_bookmarks);
if (line > 0)
{
edit_move_to_line (edit, line - 1);
edit->prev_col = column;
}
else if (offset > 0)
{
edit_cursor_move (edit, offset);
line = edit->buffer.curs_line;
edit->search_start = edit->buffer.curs1;
}
book_mark_restore (edit, BOOK_MARK_COLOR);
edit_move_to_prev_col (edit, edit_buffer_get_current_bol (&edit->buffer));
edit_move_display (edit, line - (WIDGET (edit)->lines / 2));
}
/* --------------------------------------------------------------------------------------------- */
/** Save cursor position in the file */
static void
edit_save_position (WEdit * edit)
{
if (edit->filename_vpath == NULL
|| *(vfs_path_get_by_index (edit->filename_vpath, 0)->path) == '\0')
return;
book_mark_serialize (edit, BOOK_MARK_COLOR);
save_file_position (edit->filename_vpath, edit->buffer.curs_line + 1, edit->curs_col,
edit->buffer.curs1, edit->serialized_bookmarks);
edit->serialized_bookmarks = NULL;
}
/* --------------------------------------------------------------------------------------------- */
/** Clean the WEdit stricture except the widget part */
static void
edit_purge_widget (WEdit * edit)
{
size_t len = sizeof (WEdit) - sizeof (Widget);
char *start = (char *) edit + sizeof (Widget);
memset (start, 0, len);
}
/* --------------------------------------------------------------------------------------------- */
/*
TODO: if the user undos until the stack bottom, and the stack has not wrapped,
then the file should be as it was when he loaded up. Then set edit->modified to 0.
*/
static long
edit_pop_undo_action (WEdit * edit)
{
long c;
unsigned long sp = edit->undo_stack_pointer;
if (sp == edit->undo_stack_bottom)
return STACK_BOTTOM;
sp = (sp - 1) & edit->undo_stack_size_mask;
c = edit->undo_stack[sp];
if (c >= 0)
{
/* edit->undo_stack[sp] = '@'; */
edit->undo_stack_pointer = (edit->undo_stack_pointer - 1) & edit->undo_stack_size_mask;
return c;
}
if (sp == edit->undo_stack_bottom)
return STACK_BOTTOM;
c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
if (edit->undo_stack[sp] == -2)
{
/* edit->undo_stack[sp] = '@'; */
edit->undo_stack_pointer = sp;
}
else
edit->undo_stack[sp]++;
return c;
}
static long
edit_pop_redo_action (WEdit * edit)
{
long c;
unsigned long sp = edit->redo_stack_pointer;
if (sp == edit->redo_stack_bottom)
return STACK_BOTTOM;
sp = (sp - 1) & edit->redo_stack_size_mask;
c = edit->redo_stack[sp];
if (c >= 0)
{
edit->redo_stack_pointer = (edit->redo_stack_pointer - 1) & edit->redo_stack_size_mask;
return c;
}
if (sp == edit->redo_stack_bottom)
return STACK_BOTTOM;
c = edit->redo_stack[(sp - 1) & edit->redo_stack_size_mask];
if (edit->redo_stack[sp] == -2)
edit->redo_stack_pointer = sp;
else
edit->redo_stack[sp]++;
return c;
}
static long
get_prev_undo_action (WEdit * edit)
{
long c;
unsigned long sp = edit->undo_stack_pointer;
if (sp == edit->undo_stack_bottom)
return STACK_BOTTOM;
sp = (sp - 1) & edit->undo_stack_size_mask;
c = edit->undo_stack[sp];
if (c >= 0)
return c;
if (sp == edit->undo_stack_bottom)
return STACK_BOTTOM;
c = edit->undo_stack[(sp - 1) & edit->undo_stack_size_mask];
return c;
}
/* --------------------------------------------------------------------------------------------- */
/** is called whenever a modification is made by one of the four routines below */
static void
edit_modification (WEdit * edit)
{
edit->caches_valid = FALSE;
/* raise lock when file modified */
if (!edit->modified && !edit->delete_file)
edit->locked = lock_file (edit->filename_vpath);
edit->modified = 1;
}
/* --------------------------------------------------------------------------------------------- */
/* high level cursor movement commands */
/* --------------------------------------------------------------------------------------------- */
/** check whether cursor is in indent part of line
*
* @param edit editor object
*
* @return TRUE if cursor is in indent, FALSE otherwise
*/
static gboolean
is_in_indent (const edit_buffer_t * buf)
{
off_t p;
for (p = edit_buffer_get_current_bol (buf); p < buf->curs1; p++)
if (strchr (" \t", edit_buffer_get_byte (buf, p)) == NULL)
return FALSE;
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/** check whether line in editor is blank or not
*
* @param edit editor object
* @param offset position in file
*
* @return TRUE if line in blank, FALSE otherwise
*/
static gboolean
is_blank (const edit_buffer_t * buf, off_t offset)
{
off_t s, f;
s = edit_buffer_get_bol (buf, offset);
f = edit_buffer_get_eol (buf, offset) - 1;
while (s <= f)
{
int c;
c = edit_buffer_get_byte (buf, s++);
if (!isspace (c))
return FALSE;
}
return TRUE;
}
/* --------------------------------------------------------------------------------------------- */
/** returns the offset of line i */
static off_t
edit_find_line (WEdit * edit, long line)
{
long i, j = 0;
long m = 2000000000; /* what is the magic number? */
if (!edit->caches_valid)
{
memset (edit->line_numbers, 0, sizeof (edit->line_numbers));
memset (edit->line_offsets, 0, sizeof (edit->line_offsets));
/* three offsets that we *know* are line 0 at 0 and these two: */
edit->line_numbers[1] = edit->buffer.curs_line;
edit->line_offsets[1] = edit_buffer_get_current_bol (&edit->buffer);
edit->line_numbers[2] = edit->buffer.lines;
edit->line_offsets[2] = edit_buffer_get_bol (&edit->buffer, edit->buffer.size);
edit->caches_valid = TRUE;
}
if (line >= edit->buffer.lines)
return edit->line_offsets[2];
if (line <= 0)
return 0;
/* find the closest known point */
for (i = 0; i < N_LINE_CACHES; i++)
{
long n;
n = labs (edit->line_numbers[i] - line);
if (n < m)
{
m = n;
j = i;
}
}
if (m == 0)
return edit->line_offsets[j]; /* know the offset exactly */
if (m == 1 && j >= 3)
i = j; /* one line different - caller might be looping, so stay in this cache */
else
i = 3 + (rand () % (N_LINE_CACHES - 3));
if (line > edit->line_numbers[j])
edit->line_offsets[i] =
edit_buffer_move_forward (&edit->buffer, edit->line_offsets[j],
line - edit->line_numbers[j], 0);
else
edit->line_offsets[i] =
edit_buffer_move_backward (&edit->buffer, edit->line_offsets[j],
edit->line_numbers[j] - line);
edit->line_numbers[i] = line;
return edit->line_offsets[i];
}
/* --------------------------------------------------------------------------------------------- */
/** moves up until a blank line is reached, or until just
before a non-blank line is reached */
static void
edit_move_up_paragraph (WEdit * edit, gboolean do_scroll)
{
long i = 0;
if (edit->buffer.curs_line > 1)
{
if (!edit_line_is_blank (edit, edit->buffer.curs_line))
{
for (i = edit->buffer.curs_line - 1; i != 0; i--)
if (edit_line_is_blank (edit, i))
break;
}
else if (edit_line_is_blank (edit, edit->buffer.curs_line - 1))
{
for (i = edit->buffer.curs_line - 1; i != 0; i--)
if (!edit_line_is_blank (edit, i))
{
i++;
break;
}
}
else
{
for (i = edit->buffer.curs_line - 1; i != 0; i--)
if (edit_line_is_blank (edit, i))
break;
}
}
edit_move_up (edit, edit->buffer.curs_line - i, do_scroll);
}
/* --------------------------------------------------------------------------------------------- */
/** moves down until a blank line is reached, or until just
before a non-blank line is reached */
static void
edit_move_down_paragraph (WEdit * edit, gboolean do_scroll)
{
long i;
if (edit->buffer.curs_line >= edit->buffer.lines - 1)
i = edit->buffer.lines;
else if (!edit_line_is_blank (edit, edit->buffer.curs_line))
{
for (i = edit->buffer.curs_line + 1; i != 0; i++)
if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
break;
}
else if (edit_line_is_blank (edit, edit->buffer.curs_line + 1))
{
for (i = edit->buffer.curs_line + 1; i != 0; i++)
if (!edit_line_is_blank (edit, i) || i > edit->buffer.lines)
{
i--;
break;
}
}
else
{
for (i = edit->buffer.curs_line + 1; i != 0; i++)
if (edit_line_is_blank (edit, i) || i >= edit->buffer.lines)
break;
}
edit_move_down (edit, i - edit->buffer.curs_line, do_scroll);
}
/* --------------------------------------------------------------------------------------------- */
static void
edit_begin_page (WEdit * edit)
{
edit_update_curs_row (edit);
edit_move_up (edit, edit->curs_row, 0);
}
/* --------------------------------------------------------------------------------------------- */
static void
edit_end_page (WEdit * edit)
{
edit_update_curs_row (edit);
edit_move_down (edit, WIDGET (edit)->lines - edit->curs_row - 1, 0);
}
/* --------------------------------------------------------------------------------------------- */
/** goto beginning of text */
static void
edit_move_to_top (WEdit * edit)
{
if (edit->buffer.curs_line != 0)
{
edit_cursor_move (edit, -edit->buffer.curs1);
edit_move_to_prev_col (edit, 0);
edit->force |= REDRAW_PAGE;
edit->search_start = 0;
edit_update_curs_row (edit);
}
}
/* --------------------------------------------------------------------------------------------- */
/** goto end of text */
static void
edit_move_to_bottom (WEdit * edit)
{
if (edit->buffer.curs_line < edit->buffer.lines)
{
edit_move_down (edit, edit->buffer.lines - edit->curs_row, 0);
edit->start_display = edit->buffer.size;
edit->start_line = edit->buffer.lines;
edit_scroll_upward (edit, WIDGET (edit)->lines - 1);
edit->force |= REDRAW_PAGE;
}
}
/* --------------------------------------------------------------------------------------------- */
/** goto beginning of line */
static void
edit_cursor_to_bol (WEdit * edit)
{
edit_cursor_move (edit, edit_buffer_get_current_bol (&edit->buffer) - edit->buffer.curs1);
edit->search_start = edit->buffer.curs1;
edit->prev_col = edit_get_col (edit);
edit->over_col = 0;
}
/* --------------------------------------------------------------------------------------------- */
/** goto end of line */
static void
edit_cursor_to_eol (WEdit * edit)
{
edit_cursor_move (edit, edit_buffer_get_current_eol (&edit->buffer) - edit->buffer.curs1);
edit->search_start = edit->buffer.curs1;
edit->prev_col = edit_get_col (edit);
edit->over_col = 0;
}
/* --------------------------------------------------------------------------------------------- */
static unsigned long
my_type_of (int c)
{
int x, r = 0;
const char *p, *q;
const char option_chars_move_whole_word[] =
"!=&|<>^~ !:;, !'!`!.?!\"!( !) !{ !} !Aa0 !+-*/= |<> ![ !] !\\#! ";
if (c == 0)
return 0;
if (c == '!')
{
if (*option_chars_move_whole_word == '!')
return 2;
return 0x80000000UL;
}
if (g_ascii_isupper ((gchar) c))
c = 'A';
else if (g_ascii_islower ((gchar) c))
c = 'a';
else if (g_ascii_isalpha (c))
c = 'a';
else if (isdigit (c))
c = '0';
else if (isspace (c))
c = ' ';
q = strchr (option_chars_move_whole_word, c);
if (!q)
return 0xFFFFFFFFUL;
do
{
for (x = 1, p = option_chars_move_whole_word; p < q; p++)
if (*p == '!')
x <<= 1;
r |= x;
}
while ((q = strchr (q + 1, c)));
return r;
}
/* --------------------------------------------------------------------------------------------- */
static void