2 * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
13 * Author: Hans-J. Boehm (boehm@parc.xerox.com)
16 * A really simple-minded text editor based on cords.
17 * Things it does right:
20 * Shouldn't crash no matter what file you invoke it on (e.g. /vmunix)
21 * (Make sure /vmunix is not writable before you try this.)
22 * Scrolls horizontally.
23 * Things it does wrong:
24 * It doesn't handle tabs reasonably (use "expand" first).
25 * The command set is MUCH too small.
26 * The redisplay algorithm doesn't let curses do the scrolling.
27 * The rule for moving the window over the file is suboptimal.
29 /* Boehm, February 6, 1995 12:27 pm PST */
31 /* Boehm, May 19, 1994 2:20 pm PDT */
41 #if defined(__BORLANDC__) && !defined(WIN32)
42 /* If this is DOS or win16, we'll fail anyway. */
43 /* Might as well assume win32. */
50 #elif defined(MACINTOSH)
52 /* curses emulation. */
56 # define noecho() csetmode(C_NOECHO, stdout)
57 # define cbreak() csetmode(C_CBREAK, stdout)
59 # define addch(c) putchar(c)
60 # define standout() cinverse(1, stdout)
61 # define standend() cinverse(0, stdout)
62 # define move(line,col) cgotoxy(col + 1, line + 1, stdout)
63 # define clrtoeol() ccleol(stdout)
64 # define de_error(s) { fprintf(stderr, s); getchar(); }
69 # define de_error(s) { fprintf(stderr, s); sleep(2); }
73 /* List of line number to position mappings, in descending order. */
74 /* There may be holes. */
75 typedef struct LineMapRep
{
78 struct LineMapRep
* previous
;
81 /* List of file versions, one per edit operation */
82 typedef struct HistoryRep
{
84 struct HistoryRep
* previous
;
85 line_map map
; /* Invalid for first record "now" */
89 CORD current
; /* == now -> file_contents. */
90 size_t current_len
; /* Current file length. */
91 line_map current_map
= 0; /* Current line no. to pos. map */
92 size_t current_map_size
= 0; /* Number of current_map entries. */
93 /* Not always accurate, but reset */
95 # define MAX_MAP_SIZE 3000
97 /* Current display position */
103 int need_redisplay
= 0; /* Line that needs to be redisplayed. */
106 /* Current cursor position. Always within file. */
109 size_t file_pos
= 0; /* Character position corresponding to cursor. */
111 /* Invalidate line map for lines > i */
112 void invalidate_map(int i
)
114 while(current_map
-> line
> i
) {
115 current_map
= current_map
-> previous
;
120 /* Reduce the number of map entries to save space for huge files. */
121 /* This also affects maps in histories. */
124 line_map map
= current_map
;
125 int start_line
= map
-> line
;
127 current_map_size
= 0;
128 for(; map
!= 0; map
= map
-> previous
) {
130 if (map
-> line
< start_line
- LINES
&& map
-> previous
!= 0) {
131 map
-> previous
= map
-> previous
-> previous
;
135 /* Add mapping entry */
136 void add_map(int line
, size_t pos
)
138 line_map new_map
= GC_NEW(struct LineMapRep
);
140 if (current_map_size
>= MAX_MAP_SIZE
) prune_map();
141 new_map
-> line
= line
;
142 new_map
-> pos
= pos
;
143 new_map
-> previous
= current_map
;
144 current_map
= new_map
;
150 /* Return position of column *c of ith line in */
151 /* current file. Adjust *c to be within the line.*/
152 /* A 0 pointer is taken as 0 column. */
153 /* Returns CORD_NOT_FOUND if i is too big. */
154 /* Assumes i > dis_line. */
155 size_t line_pos(int i
, int *c
)
160 line_map map
= current_map
;
162 while (map
-> line
> i
) map
= map
-> previous
;
163 if (map
-> line
< i
- 2) /* rebuild */ invalidate_map(i
);
164 for (j
= map
-> line
, cur
= map
-> pos
; j
< i
;) {
165 cur
= CORD_chr(current
, cur
, '\n');
166 if (cur
== current_len
-1) return(CORD_NOT_FOUND
);
168 if (++j
> current_map
-> line
) add_map(j
, cur
);
171 next
= CORD_chr(current
, cur
, '\n');
172 if (next
== CORD_NOT_FOUND
) next
= current_len
- 1;
173 if (next
< cur
+ *c
) {
181 void add_hist(CORD s
)
183 history new_file
= GC_NEW(struct HistoryRep
);
185 new_file
-> file_contents
= current
= s
;
186 current_len
= CORD_len(s
);
187 new_file
-> previous
= now
;
188 if (now
!= 0) now
-> map
= current_map
;
194 now
= now
-> previous
;
195 current
= now
-> file_contents
;
196 current_map
= now
-> map
;
197 current_len
= CORD_len(current
);
200 /* Current screen_contents; a dynamically allocated array of CORDs */
205 /* Replace a line in the curses stdscr. All control characters are */
206 /* displayed as upper case characters in standout mode. This isn't */
207 /* terribly appropriate for tabs. */
208 void replace_line(int i
, CORD s
)
212 size_t len
= CORD_len(s
);
214 if (screen
== 0 || LINES
> screen_size
) {
216 screen
= (CORD
*)GC_MALLOC(screen_size
* sizeof(CORD
));
218 # if !defined(MACINTOSH)
219 /* A gross workaround for an apparent curses bug: */
220 if (i
== LINES
-1 && len
== COLS
) {
221 s
= CORD_substr(s
, 0, CORD_len(s
) - 1);
224 if (CORD_cmp(screen
[i
], s
) != 0) {
225 move(i
, 0); clrtoeol(); move(i
,0);
228 c
= CORD_pos_fetch(p
) & 0x7f;
230 standout(); addch(c
+ 0x40); standend();
239 # define replace_line(i,s) invalidate_line(i)
242 /* Return up to COLS characters of the line of s starting at pos, */
243 /* returning only characters after the given column. */
244 CORD
retrieve_line(CORD s
, size_t pos
, unsigned column
)
246 CORD candidate
= CORD_substr(s
, pos
, column
+ COLS
);
247 /* avoids scanning very long lines */
248 int eol
= CORD_chr(candidate
, 0, '\n');
251 if (eol
== CORD_NOT_FOUND
) eol
= CORD_len(candidate
);
252 len
= (int)eol
- (int)column
;
253 if (len
< 0) len
= 0;
254 return(CORD_substr(s
, pos
+ column
, len
));
260 CORD
retrieve_screen_line(int i
)
264 invalidate_map(dis_line
+ LINES
); /* Prune search */
265 pos
= line_pos(dis_line
+ i
, 0);
266 if (pos
== CORD_NOT_FOUND
) return(CORD_EMPTY
);
267 return(retrieve_line(current
, pos
, dis_col
));
271 /* Display the visible section of the current file */
276 invalidate_map(dis_line
+ LINES
); /* Prune search */
277 for (i
= 0; i
< LINES
; i
++) {
278 if (need_redisplay
== ALL
|| need_redisplay
== i
) {
279 register size_t pos
= line_pos(dis_line
+ i
, 0);
281 if (pos
== CORD_NOT_FOUND
) break;
282 replace_line(i
, retrieve_line(current
, pos
, dis_col
));
283 if (need_redisplay
== i
) goto done
;
286 for (; i
< LINES
; i
++) replace_line(i
, CORD_EMPTY
);
289 need_redisplay
= NONE
;
294 /* Update dis_line, dis_col, and dis_pos to make cursor visible. */
295 /* Assumes line, col, dis_line, dis_pos are in bounds. */
296 void normalize_display()
298 int old_line
= dis_line
;
299 int old_col
= dis_col
;
302 if (LINES
> 15 && COLS
> 15) dis_granularity
= 2;
303 while (dis_line
> line
) dis_line
-= dis_granularity
;
304 while (dis_col
> col
) dis_col
-= dis_granularity
;
305 while (line
>= dis_line
+ LINES
) dis_line
+= dis_granularity
;
306 while (col
>= dis_col
+ COLS
) dis_col
+= dis_granularity
;
307 if (old_line
!= dis_line
|| old_col
!= dis_col
) {
308 need_redisplay
= ALL
;
313 # elif defined(MACINTOSH)
314 # define move_cursor(x,y) cgotoxy(x + 1, y + 1, stdout)
316 # define move_cursor(x,y) move(y,x)
319 /* Adjust display so that cursor is visible; move cursor into position */
320 /* Update screen if necessary. */
321 void fix_cursor(void)
324 if (need_redisplay
!= NONE
) redisplay();
325 move_cursor(col
- dis_col
, line
- dis_line
);
332 /* Make sure line, col, and dis_pos are somewhere inside file. */
333 /* Recompute file_pos. Assumes dis_pos is accurate or past eof */
338 if ((size_t)line
> current_len
) line
= current_len
;
339 file_pos
= line_pos(line
, &my_col
);
340 if (file_pos
== CORD_NOT_FOUND
) {
341 for (line
= current_map
-> line
, file_pos
= current_map
-> pos
;
342 file_pos
< current_len
;
343 line
++, file_pos
= CORD_chr(current
, file_pos
, '\n') + 1);
345 file_pos
= line_pos(line
, &col
);
352 # define beep() Beep(1000 /* Hz */, 300 /* msecs */)
353 #elif defined(MACINTOSH)
354 # define beep() SysBeep(1)
357 * beep() is part of some curses packages and not others.
358 * We try to match the type of the builtin one, if any.
366 putc('\007', stderr
);
371 # define NO_PREFIX -1
372 # define BARE_PREFIX -2
373 int repeat_count
= NO_PREFIX
; /* Current command prefix. */
375 int locate_mode
= 0; /* Currently between 2 ^Ls */
376 CORD locate_string
= CORD_EMPTY
; /* Current search string. */
378 char * arg_file_name
;
381 /* Change the current position to whatever is currently displayed at */
382 /* the given SCREEN coordinates. */
383 void set_position(int c
, int l
)
388 move_cursor(col
- dis_col
, line
- dis_line
);
392 /* Perform the command associated with character c. C may be an */
393 /* integer > 256 denoting a windows command, one of the above control */
394 /* characters, or another ASCII character to be used as either a */
395 /* character to be inserted, a repeat count, or a search string, */
396 /* depending on the current state. */
397 void do_command(int c
)
403 if ( c
== '\r') c
= '\n';
409 locate_string
= CORD_EMPTY
;
412 locate_string
= CORD_cat_char(locate_string
, (char)c
);
413 new_pos
= CORD_str(current
, file_pos
- CORD_len(locate_string
) + 1,
415 if (new_pos
!= CORD_NOT_FOUND
) {
416 need_redisplay
= ALL
;
417 new_pos
+= CORD_len(locate_string
);
419 file_pos
= line_pos(line
+ 1, 0);
420 if (file_pos
> new_pos
) break;
423 col
= new_pos
- line_pos(line
, 0);
427 locate_string
= CORD_substr(locate_string
, 0,
428 CORD_len(locate_string
) - 1);
434 repeat_count
= BARE_PREFIX
; return;
435 } else if (c
< 0x100 && isdigit(c
)){
436 if (repeat_count
== BARE_PREFIX
) {
437 repeat_count
= c
- '0'; return;
438 } else if (repeat_count
!= NO_PREFIX
) {
439 repeat_count
= 10 * repeat_count
+ c
- '0'; return;
442 if (repeat_count
== NO_PREFIX
) repeat_count
= 1;
443 if (repeat_count
== BARE_PREFIX
&& (c
== UP
|| c
== DOWN
)) {
444 repeat_count
= LINES
- dis_granularity
;
446 if (repeat_count
== BARE_PREFIX
) repeat_count
= 8;
448 for (i
= 0; i
< repeat_count
; i
++) {
454 line
= col
= file_pos
= 0;
472 if (CORD_fetch(current
, file_pos
) == '\n') break;
477 need_redisplay
= ALL
; need_fix_pos
= 1;
487 if (file_pos
== current_len
-1) break;
488 /* Can't delete trailing newline */
489 if (CORD_fetch(current
, file_pos
) == '\n') {
490 need_redisplay
= ALL
; need_fix_pos
= 1;
492 need_redisplay
= line
- dis_line
;
495 CORD_substr(current
, 0, file_pos
),
496 CORD_substr(current
, file_pos
+1, current_len
)));
497 invalidate_map(line
);
501 CORD name
= CORD_cat(CORD_from_char_star(arg_file_name
),
504 if ((out
= fopen(CORD_to_const_char_star(name
), "wb")) == NULL
505 || CORD_put(current
, out
) == EOF
) {
506 de_error("Write failed\n");
507 need_redisplay
= ALL
;
515 CORD left_part
= CORD_substr(current
, 0, file_pos
);
516 CORD right_part
= CORD_substr(current
, file_pos
, current_len
);
518 add_hist(CORD_cat(CORD_cat_char(left_part
, (char)c
),
520 invalidate_map(line
);
522 col
= 0; line
++; file_pos
++;
523 need_redisplay
= ALL
;
526 need_redisplay
= line
- dis_line
;
532 if (need_fix_pos
) fix_pos();
534 repeat_count
= NO_PREFIX
;
537 /* OS independent initialization */
539 void generic_init(void)
544 if ((f
= fopen(arg_file_name
, "rb")) == NULL
) {
547 initial
= CORD_from_file(f
);
548 if (initial
== CORD_EMPTY
549 || CORD_fetch(initial
, CORD_len(initial
)-1) != '\n') {
550 initial
= CORD_cat(initial
, "\n");
555 now
-> map
= current_map
;
556 now
-> previous
= now
; /* Can't back up further: beginning of the world */
557 need_redisplay
= ALL
;
569 #if defined(MACINTOSH)
570 console_options
.title
= "\pDumb Editor";
572 argc
= ccommand(&argv
);
576 if (argc
!= 2) goto usage
;
577 arg_file_name
= argv
[1];
578 setvbuf(stdout
, GC_MALLOC_ATOMIC(8192), _IOFBF
, 8192);
580 noecho(); nonl(); cbreak();
582 while ((c
= getchar()) != QUIT
) {
595 fprintf(stderr
, "Usage: %s file\n", argv
[0]);
596 fprintf(stderr
, "Cursor keys: ^B(left) ^F(right) ^P(up) ^N(down)\n");
597 fprintf(stderr
, "Undo: ^U Write to <file>.new: ^W");
598 fprintf(stderr
, "Quit:^D Repeat count: ^R[n]\n");
599 fprintf(stderr
, "Top: ^T Locate (search, find): ^L text ^L\n");