Do not allow resizing scrollback buffer in copy mode.
[screen-lua.git] / src / mark.c
blob08f49b83269e2fe937ae39313bf6ae4dc12a2176
1 /* Copyright (c) 1993-2002
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Copyright (c) 1987 Oliver Laumann
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (see the file COPYING); if not, write to the
18 * Free Software Foundation, Inc.,
19 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
21 ****************************************************************
24 #include <sys/types.h>
26 #include "config.h"
27 #include "screen.h"
28 #include "mark.h"
29 #include "extern.h"
31 #ifdef COPY_PASTE
34 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
36 * WARNING: these routines use the global variables "fore" and
37 * "flayer" to make things easier.
39 * !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
42 static int is_letter __P((int));
43 static void nextword __P((int *, int *, int, int));
44 static int linestart __P((int));
45 static int lineend __P((int));
46 static int rem __P((int, int , int , int , int , char *, int));
47 static int eq __P((int, int ));
48 static int MarkScrollDownDisplay __P((int));
49 static int MarkScrollUpDisplay __P((int));
51 static void MarkProcess __P((char **, int *));
52 static void MarkAbort __P((void));
53 static void MarkRedisplayLine __P((int, int, int, int));
54 static int MarkRewrite __P((int, int, int, struct mchar *, int));
56 extern struct layer *flayer;
57 extern struct display *display, *displays;
58 extern struct win *fore;
59 extern struct mline mline_blank, mline_null;
60 extern struct mchar mchar_so;
62 #ifdef FONT
63 int pastefont = 1;
64 #endif
66 struct LayFuncs MarkLf =
68 MarkProcess,
69 MarkAbort,
70 MarkRedisplayLine,
71 DefClearLine,
72 MarkRewrite,
73 DefResize,
74 DefRestore
77 int join_with_cr = 0;
78 int compacthist = 0;
80 unsigned char mark_key_tab[256]; /* this array must be initialised first! */
82 static struct markdata *markdata;
86 * VI like is_letter: 0 - whitespace
87 * 1 - letter
88 * 2 - other
90 static int is_letter(c)
91 char c;
93 if ((c >= 'a' && c <= 'z') ||
94 (c >= 'A' && c <= 'Z') ||
95 (c >= '0' && c <= '9') ||
96 c == '_' || c == '.' ||
97 c == '@' || c == ':' ||
98 c == '%' || c == '!' ||
99 c == '-' || c == '+')
100 /* thus we can catch email-addresses as a word :-) */
101 return 1;
102 else if (c != ' ')
103 return 2;
104 return 0;
107 static int
108 linestart(y)
109 int y;
111 register int x;
112 register unsigned char *i;
114 for (x = markdata->left_mar, i = WIN(y)->image + x; x < fore->w_width - 1; x++)
115 if (*i++ != ' ')
116 break;
117 if (x == fore->w_width - 1)
118 x = markdata->left_mar;
119 return x;
122 static int
123 lineend(y)
124 int y;
126 register int x;
127 register unsigned char *i;
129 for (x = markdata->right_mar, i = WIN(y)->image + x; x >= 0; x--)
130 if (*i-- != ' ')
131 break;
132 if (x < 0)
133 x = markdata->left_mar;
134 return x;
138 * nextchar sets *xp to the num-th occurrence of the target in the line.
140 * Returns -1 if the target doesn't appear num times, 0 otherwise.
142 static int
143 nextchar(int *xp, int *yp, int direction, char target, int num)
145 int width; /* width of the current window. */
146 int x; /* x coordinate of the current cursor position. */
147 int step; /* amount to increment x (+1 or -1) */
148 int adjust; /* Final adjustment of cursor position. */
149 char *displayed_line; /* Line in which search takes place. */
151 debug("nextchar\n");
153 x = *xp;
154 adjust = 0;
155 width = fore->w_width;
156 displayed_line = WIN(*yp) -> image;
158 switch(direction) {
159 case 't': adjust = -1; /* fall through */
160 case 'f': step = 1; /* fall through */
161 break;
162 case 'T': adjust = 1; /* fall through */
163 case 'F': step = -1; /* fall through */
164 break;
165 default:
166 ASSERT(0);
169 x += step;
171 debug1("ml->image = %s\n", displayed_line);
172 debug2("num = %d, width = %d\n",num, width);
173 debug2("x = %d targe = %c\n", x, target );
175 for ( ;x>=0 && x <= width; x += step) {
176 if (displayed_line[x] == target) {
177 if (--num == 0) {
178 *xp = x + adjust;
179 return 0;
183 return -1;
187 * nextword calculates the cursor position of the num'th word.
188 * If the cursor is on a word, it counts as the first.
189 * NW_BACK: search backward
190 * NW_ENDOFWORD: find the end of the word
191 * NW_MUSTMOVE: move at least one char
192 * NW_BIG: match WORDs not words
195 #define NW_BACK (1<<0)
196 #define NW_ENDOFWORD (1<<1)
197 #define NW_MUSTMOVE (1<<2)
198 #define NW_BIG (1<<3)
202 static void
203 nextword(xp, yp, flags, num)
204 int *xp, *yp, flags, num;
206 int xx = fore->w_width, yy = fore->w_histheight + fore->w_height;
207 register int sx, oq, q, x, y;
208 struct mline *ml;
210 x = *xp;
211 y = *yp;
212 sx = (flags & NW_BACK) ? -1 : 1;
213 if ((flags & NW_ENDOFWORD) && (flags & NW_MUSTMOVE))
214 x += sx;
215 ml = WIN(y);
216 for (oq = -1; ; x += sx, oq = q)
218 if (x >= xx || x < 0)
219 q = 0;
220 else if (flags & NW_BIG)
221 q = ml->image[x] == ' ';
222 else
223 q = is_letter(ml->image[x]);
224 if (oq >= 0 && oq != q)
226 if (oq == 0 || !(flags & NW_ENDOFWORD))
227 *xp = x;
228 else
229 *xp = x-sx;
230 *yp = y;
231 if ((!(flags & NW_ENDOFWORD) && q) ||
232 ((flags & NW_ENDOFWORD) && oq))
234 if (--num <= 0)
235 return;
238 if (x == xx)
240 x = -1;
241 if (++y >= yy)
242 return;
243 ml = WIN(y);
245 else if (x < 0)
247 x = xx;
248 if (--y < 0)
249 return;
250 ml = WIN(y);
257 * y1, y2 are WIN coordinates
259 * redisplay: 0 - just copy
260 * 1 - redisplay + copy
261 * 2 - count + copy, don't redisplay
264 static int
265 rem(x1, y1, x2, y2, redisplay, pt, yend)
266 int x1, y1, x2, y2, redisplay, yend;
267 char *pt;
269 int i, j, from, to, ry, c;
270 int l = 0;
271 unsigned char *im;
272 struct mline *ml;
273 #ifdef FONT
274 int cf, font;
275 unsigned char *fo;
276 #endif
278 markdata->second = 0;
279 if (y2 < y1 || ((y2 == y1) && (x2 < x1)))
281 i = y2;
282 y2 = y1;
283 y1 = i;
284 i = x2;
285 x2 = x1;
286 x1 = i;
288 ry = y1 - markdata->hist_offset;
290 i = y1;
291 if (redisplay != 2 && pt == 0 && ry <0)
293 i -= ry;
294 ry = 0;
296 for (; i <= y2; i++, ry++)
298 if (redisplay != 2 && pt == 0 && ry > yend)
299 break;
300 ml = WIN(i);
301 from = (i == y1) ? x1 : 0;
302 if (from < markdata->left_mar)
303 from = markdata->left_mar;
304 for (to = fore->w_width, im = ml->image + to; to >= 0; to--)
305 if (*im-- != ' ')
306 break;
307 if (i == y2 && x2 < to)
308 to = x2;
309 if (to > markdata->right_mar)
310 to = markdata->right_mar;
311 if (redisplay == 1 && from <= to && ry >=0 && ry <= yend)
312 MarkRedisplayLine(ry, from, to, 0);
313 if (redisplay != 2 && pt == 0) /* don't count/copy */
314 continue;
315 j = from;
316 #ifdef DW_CHARS
317 if (dw_right(ml, j, fore->w_encoding))
318 j--;
319 #endif
320 im = ml->image + j;
321 #ifdef FONT
322 fo = ml->font + j;
323 font = ASCII;
324 #endif
325 for (; j <= to; j++)
327 c = (unsigned char)*im++;
328 #ifdef FONT
329 cf = (unsigned char)*fo++;
330 # ifdef UTF8
331 if (fore->w_encoding == UTF8)
333 c |= cf << 8;
334 if (c == UCS_HIDDEN)
335 continue;
336 c = ToUtf8_comb(pt, c);
337 l += c;
338 if (pt)
339 pt += c;
340 continue;
342 # endif
343 # ifdef DW_CHARS
344 if (is_dw_font(cf))
346 c = c << 8 | (unsigned char)*im++;
347 fo++;
348 j++;
350 # endif
351 if (pastefont)
353 c = EncodeChar(pt, c | cf << 16, fore->w_encoding, &font);
354 l += c;
355 if (pt)
356 pt += c;
357 continue;
359 #endif /* FONT */
360 if (pt)
361 *pt++ = c;
362 l++;
364 #ifdef FONT
365 if (pastefont && font != ASCII)
367 if (pt)
369 strcpy(pt, "\033(B");
370 pt += 3;
372 l += 3;
374 #endif
375 if (i != y2 && (to != fore->w_width - 1 || ml->image[to + 1] == ' '))
378 * this code defines, what glues lines together
380 switch (markdata->nonl)
382 case 0: /* lines separated by newlines */
383 if (pt)
384 *pt++ = '\r';
385 l++;
386 if (join_with_cr)
388 if (pt)
389 *pt++ = '\n';
390 l++;
392 break;
393 case 1: /* nothing to separate lines */
394 break;
395 case 2: /* lines separated by blanks */
396 if (pt)
397 *pt++ = ' ';
398 l++;
399 break;
400 case 3: /* seperate by comma, for csh junkies */
401 if (pt)
402 *pt++ = ',';
403 l++;
404 break;
408 return l;
411 /* Check if two chars are identical. All digits are treated
412 * as same. Used for GetHistory()
415 static int
416 eq(a, b)
417 int a, b;
419 if (a == b)
420 return 1;
421 if (a == 0 || b == 0)
422 return 1;
423 if (a <= '9' && a >= '0' && b <= '9' && b >= '0')
424 return 1;
425 return 0;
429 /**********************************************************************/
432 GetHistory() /* return value 1 if copybuffer changed */
434 int i = 0, q = 0, xx, yy, x, y;
435 unsigned char *linep;
436 struct mline *ml;
438 ASSERT(display && fore);
439 x = fore->w_x;
440 if (x >= fore->w_width)
441 x = fore->w_width - 1;
442 y = fore->w_y + fore->w_histheight;
443 debug2("cursor is at x=%d, y=%d\n", x, y);
444 ml = WIN(y);
445 for (xx = x - 1, linep = ml->image + xx; xx >= 0; xx--)
446 if ((q = *linep--) != ' ' )
447 break;
448 debug3("%c at (%d,%d)\n", q, xx, y);
449 for (yy = y - 1; yy >= 0; yy--)
451 ml = WIN(yy);
452 linep = ml->image;
453 if (xx < 0 || eq(linep[xx], q))
454 { /* line is matching... */
455 for (i = fore->w_width - 1, linep += i; i >= x; i--)
456 if (*linep-- != ' ')
457 break;
458 if (i >= x)
459 break;
462 if (yy < 0)
463 return 0;
464 if (D_user->u_plop.buf)
465 UserFreeCopyBuffer(D_user);
466 if ((D_user->u_plop.buf = (char *)malloc((unsigned) (i - x + 2))) == NULL)
468 LMsg(0, "Not enough memory... Sorry.");
469 return 0;
471 bcopy((char *)linep - i + x + 1, D_user->u_plop.buf, i - x + 1);
472 D_user->u_plop.len = i - x + 1;
473 #ifdef ENCODINGS
474 D_user->u_plop.enc = fore->w_encoding;
475 #endif
476 return 1;
479 /**********************************************************************/
482 void
483 MarkRoutine()
485 int x, y;
487 ASSERT(fore && display && D_user);
489 debug2("MarkRoutine called: fore nr %d, display %s\n",
490 fore->w_number, D_usertty);
492 if (InitOverlayPage(sizeof(*markdata), &MarkLf, 1))
493 return;
494 flayer->l_encoding = fore->w_encoding;
495 markdata = (struct markdata *)flayer->l_data;
496 markdata->md_user = D_user; /* XXX: Correct? */
497 markdata->md_window = fore;
498 markdata->second = 0;
499 markdata->rep_cnt = 0;
500 markdata->append_mode = 0;
501 markdata->write_buffer = 0;
502 markdata->nonl = 0;
503 markdata->left_mar = 0;
504 markdata->right_mar = fore->w_width - 1;
505 markdata->hist_offset = fore->w_histheight;
506 x = fore->w_x;
507 y = D2W(fore->w_y);
508 if (x >= fore->w_width)
509 x = fore->w_width - 1;
511 LGotoPos(flayer, x, W2D(y));
512 LMsg(0, "Copy mode - Column %d Line %d(+%d) (%d,%d)",
513 x + 1, W2D(y + 1), fore->w_histheight, fore->w_width, fore->w_height);
514 markdata->cx = markdata->x1 = x;
515 markdata->cy = markdata->y1 = y;
516 flayer->l_x = x;
517 flayer->l_y = W2D(y);
520 static void
521 MarkProcess(inbufp,inlenp)
522 char **inbufp;
523 int *inlenp;
525 char *inbuf, *pt;
526 int inlen;
527 int cx, cy, x2, y2, j, yend;
528 int newcopylen = 0, od;
529 int in_mark;
530 int rep_cnt;
531 struct acluser *md_user;
534 char *extrap = 0, extrabuf[100];
537 markdata = (struct markdata *)flayer->l_data;
538 fore = markdata->md_window;
539 md_user = markdata->md_user;
540 if (inbufp == 0)
542 MarkAbort();
543 return;
546 LGotoPos(flayer, markdata->cx, W2D(markdata->cy));
547 inbuf= *inbufp;
548 inlen= *inlenp;
549 pt = inbuf;
550 in_mark = 1;
551 while (in_mark && (inlen /* || extrap */))
554 if (extrap)
556 od = *extrap++;
557 if (*extrap == 0)
558 extrap = 0;
560 else
563 od = mark_key_tab[(int)(unsigned char)*pt++];
564 inlen--;
566 rep_cnt = markdata->rep_cnt;
567 if (od >= '0' && od <= '9' && !markdata->f_cmd.flag)
569 if (rep_cnt < 1001 && (od != '0' || rep_cnt != 0))
571 markdata->rep_cnt = 10 * rep_cnt + od - '0';
572 continue;
574 * Now what is that 1001 here? Well, we have a screen with
575 * 25 * 80 = 2000 characters. Movement is at most across the full
576 * screen. This we do with word by word movement, as character by
577 * character movement never steps over line boundaries. The most words
578 * we can place on the screen are 1000 single letter words. Thus 1001
579 * is sufficient. Users with bigger screens never write in single letter
580 * words, as they should be more advanced. jw.
581 * Oh, wrong. We still give even the experienced user a factor of ten.
585 cx = markdata->cx;
586 cy = markdata->cy;
588 if (markdata -> f_cmd.flag) {
589 debug2("searching for %c:%d\n",od,rep_cnt);
590 markdata->f_cmd.flag = 0;
591 markdata->rep_cnt = 0;
593 if (isgraph (od)) {
594 markdata->f_cmd.target = od;
595 rep_cnt = (rep_cnt) ? rep_cnt : 1;
596 nextchar(&cx, &cy, markdata->f_cmd.direction, od, rep_cnt );
597 revto(cx, cy);
598 continue;
602 switch (od)
604 case 'f': /* fall through */
605 case 'F': /* fall through */
606 case 't': /* fall through */
607 case 'T': /* fall through */
609 * Set f_cmd to do a search on the next key stroke.
610 * If we break, rep_cnt will be reset, so we
611 * continue instead. It might be cleaner to
612 * store the rep_count in f_cmd and
613 * break here so later followon code will be
614 * hit.
616 markdata->f_cmd.flag = 1;
617 markdata->f_cmd.direction = od;
618 debug("entering char search\n");
619 continue;
620 case ';':
621 if (!rep_cnt)
622 rep_cnt = 1;
623 nextchar(&cx, &cy, markdata->f_cmd.direction, markdata->f_cmd.target, rep_cnt );
624 revto(cx, cy);
625 break;
626 case ',': {
627 int search_dir;
628 if (!rep_cnt)
629 rep_cnt = 1;
630 switch (markdata->f_cmd.direction) {
631 case 't': search_dir = 'T'; break;
632 case 'T': search_dir = 't'; break;
633 case 'f': search_dir = 'F'; break;
634 case 'F': search_dir = 'f'; break;
636 nextchar(&cx, &cy, search_dir, markdata->f_cmd.target, rep_cnt );
638 revto(cx, cy);
639 break;
642 case 'o':
643 case 'x':
644 if (!markdata->second)
645 break;
646 markdata->cx = markdata->x1;
647 markdata->cy = markdata->y1;
648 markdata->x1 = cx;
649 markdata->y1 = cy;
650 revto(markdata->cx, markdata->cy);
651 break;
652 case '\014': /* CTRL-L Redisplay */
653 Redisplay(0);
654 LGotoPos(flayer, cx, W2D(cy));
655 break;
656 case 0202: /* M-C-b */
657 case '\010': /* CTRL-H Backspace */
658 case 'h':
659 if (rep_cnt == 0)
660 rep_cnt = 1;
661 revto(cx - rep_cnt, cy);
662 break;
663 case 0216: /* M-C-p */
664 case '\016': /* CTRL-N */
665 case 'j':
666 if (rep_cnt == 0)
667 rep_cnt = 1;
668 revto(cx, cy + rep_cnt);
669 break;
670 case '+':
671 if (rep_cnt == 0)
672 rep_cnt = 1;
673 j = cy + rep_cnt;
674 if (j > fore->w_histheight + fore->w_height - 1)
675 j = fore->w_histheight + fore->w_height - 1;
676 revto(linestart(j), j);
677 break;
678 case '-':
679 if (rep_cnt == 0)
680 rep_cnt = 1;
681 cy -= rep_cnt;
682 if (cy < 0)
683 cy = 0;
684 revto(linestart(cy), cy);
685 break;
686 case '^':
687 revto(linestart(cy), cy);
688 break;
689 case '\n':
690 revto(markdata->left_mar, cy + 1);
691 break;
692 case 0220: /* M-C-p */
693 case '\020': /* CTRL-P */
694 case 'k':
695 if (rep_cnt == 0)
696 rep_cnt = 1;
697 revto(cx, cy - rep_cnt);
698 break;
699 case 0206: /* M-C-f */
700 case 'l':
701 if (rep_cnt == 0)
702 rep_cnt = 1;
703 revto(cx + rep_cnt, cy);
704 break;
705 case '\001': /* CTRL-A from tcsh/emacs */
706 case '0':
707 revto(markdata->left_mar, cy);
708 break;
709 case '\004': /* CTRL-D down half screen */
710 if (rep_cnt == 0)
711 rep_cnt = (fore->w_height + 1) >> 1;
712 revto_line(cx, cy + rep_cnt, W2D(cy));
713 break;
714 case '$':
715 revto(lineend(cy), cy);
716 break;
717 case '\022': /* CTRL-R emacs style backwards search */
718 ISearch(-1);
719 in_mark = 0;
720 break;
721 case '\023': /* CTRL-S emacs style search */
722 ISearch(1);
723 in_mark = 0;
724 break;
725 case '\025': /* CTRL-U up half screen */
726 if (rep_cnt == 0)
727 rep_cnt = (fore->w_height + 1) >> 1;
728 revto_line(cx, cy - rep_cnt, W2D(cy));
729 break;
730 case '\007': /* CTRL-G show cursorpos */
731 if (markdata->left_mar == 0 && markdata->right_mar == fore->w_width - 1)
732 LMsg(0, "Column %d Line %d(+%d)", cx+1, W2D(cy)+1,
733 markdata->hist_offset);
734 else
735 LMsg(0, "Column %d(%d..%d) Line %d(+%d)", cx+1,
736 markdata->left_mar+1, markdata->right_mar+1, W2D(cy)+1, markdata->hist_offset);
737 break;
738 case '\002': /* CTRL-B back one page */
739 if (rep_cnt == 0)
740 rep_cnt = 1;
741 rep_cnt *= fore->w_height;
742 revto(cx, cy - rep_cnt);
743 break;
744 case '\006': /* CTRL-F forward one page */
745 if (rep_cnt == 0)
746 rep_cnt = 1;
747 rep_cnt *= fore->w_height;
748 revto(cx, cy + rep_cnt);
749 break;
750 case '\005': /* CTRL-E scroll up */
751 if (rep_cnt == 0)
752 rep_cnt = 1;
753 rep_cnt = MarkScrollUpDisplay(rep_cnt);
754 if (cy < D2W(0))
755 revto(cx, D2W(0));
756 else
757 LGotoPos(flayer, cx, W2D(cy));
758 break;
759 case '\031': /* CTRL-Y scroll down */
760 if (rep_cnt == 0)
761 rep_cnt = 1;
762 rep_cnt = MarkScrollDownDisplay(rep_cnt);
763 if (cy > D2W(fore->w_height-1))
764 revto(cx, D2W(fore->w_height-1));
765 else
766 LGotoPos(flayer, cx, W2D(cy));
767 break;
768 case '@':
769 /* it may be usefull to have a key that does nothing */
770 break;
771 case '%':
772 rep_cnt--;
773 /* rep_cnt is a percentage for the history buffer */
774 if (rep_cnt < 0)
775 rep_cnt = 0;
776 if (rep_cnt > 100)
777 rep_cnt = 100;
778 revto_line(markdata->left_mar, (rep_cnt * (fore->w_histheight + fore->w_height)) / 100, (fore->w_height - 1) / 2);
779 break;
780 case 0201:
781 case 'g':
782 rep_cnt = 1;
783 /* FALLTHROUGH */
784 case 0205:
785 case 'G':
786 /* rep_cnt is here the WIN line number */
787 if (rep_cnt == 0)
788 rep_cnt = fore->w_histheight + fore->w_height;
789 revto_line(markdata->left_mar, --rep_cnt, (fore->w_height - 1) / 2);
790 break;
791 case 'H':
792 revto(markdata->left_mar, D2W(0));
793 break;
794 case 'M':
795 revto(markdata->left_mar, D2W((fore->w_height - 1) / 2));
796 break;
797 case 'L':
798 revto(markdata->left_mar, D2W(fore->w_height - 1));
799 break;
800 case '|':
801 revto(--rep_cnt, cy);
802 break;
803 case 'w':
804 if (rep_cnt == 0)
805 rep_cnt = 1;
806 nextword(&cx, &cy, NW_MUSTMOVE, rep_cnt);
807 revto(cx, cy);
808 break;
809 case 'e':
810 case 'E':
811 if (rep_cnt == 0)
812 rep_cnt = 1;
813 nextword(&cx, &cy, NW_ENDOFWORD|NW_MUSTMOVE | (od == 'E' ? NW_BIG : 0), rep_cnt);
814 revto(cx, cy);
815 break;
816 case 'b':
817 case 'B':
818 if (rep_cnt == 0)
819 rep_cnt = 1;
820 nextword(&cx, &cy, NW_BACK|NW_ENDOFWORD|NW_MUSTMOVE | (od == 'B' ? NW_BIG : 0), rep_cnt);
821 revto(cx, cy);
822 break;
823 case 'a':
824 markdata->append_mode = 1 - markdata->append_mode;
825 debug1("append mode %d--\n", markdata->append_mode);
826 LMsg(0, (markdata->append_mode) ? ":set append" : ":set noappend");
827 break;
828 case 'v':
829 case 'V':
830 /* this sets start column to column 9 for VI :set nu users */
831 if (markdata->left_mar == 8)
832 rep_cnt = 1;
833 else
834 rep_cnt = 9;
835 /* FALLTHROUGH */
836 case 'c':
837 case 'C':
838 /* set start column (c) and end column (C) */
839 if (markdata->second)
841 rem(markdata->x1, markdata->y1, cx, cy, 1, (char *)0, fore->w_height-1); /* Hack */
842 markdata->second = 1; /* rem turns off second */
844 rep_cnt--;
845 if (rep_cnt < 0)
846 rep_cnt = cx;
847 if (od != 'C')
849 markdata->left_mar = rep_cnt;
850 if (markdata->left_mar > markdata->right_mar)
851 markdata->left_mar = markdata->right_mar;
853 else
855 markdata->right_mar = rep_cnt;
856 if (markdata->left_mar > markdata->right_mar)
857 markdata->right_mar = markdata->left_mar;
859 if (markdata->second)
861 markdata->cx = markdata->x1; markdata->cy = markdata->y1;
862 revto(cx, cy);
864 if (od == 'v' || od == 'V')
865 LMsg(0, (markdata->left_mar != 8) ? ":set nonu" : ":set nu");
866 break;
867 case 'J':
868 /* how do you join lines in VI ? */
869 markdata->nonl = (markdata->nonl + 1) % 4;
870 switch (markdata->nonl)
872 case 0:
873 if (join_with_cr)
874 LMsg(0, "Multiple lines (CR/LF)");
875 else
876 LMsg(0, "Multiple lines (LF)");
877 break;
878 case 1:
879 LMsg(0, "Lines joined");
880 break;
881 case 2:
882 LMsg(0, "Lines joined with blanks");
883 break;
884 case 3:
885 LMsg(0, "Lines joined with comma");
886 break;
888 break;
889 case '/':
890 Search(1);
891 in_mark = 0;
892 break;
893 case '?':
894 Search(-1);
895 in_mark = 0;
896 break;
897 case 'n':
898 Search(0);
899 break;
900 case 'N':
901 markdata->isdir = -markdata->isdir;
902 Search(0);
903 markdata->isdir = -markdata->isdir;
904 break;
905 case 'y':
906 case 'Y':
907 if (markdata->second == 0)
909 revto(linestart(cy), cy);
910 markdata->second++;
911 cx = markdata->x1 = markdata->cx;
912 cy = markdata->y1 = markdata->cy;
914 if (--rep_cnt > 0)
915 revto(cx, cy + rep_cnt);
916 revto(lineend(markdata->cy), markdata->cy);
917 if (od == 'y')
918 break;
919 /* FALLTHROUGH */
920 case 'W':
921 if (od == 'W')
923 if (rep_cnt == 0)
924 rep_cnt = 1;
925 if (!markdata->second)
927 nextword(&cx, &cy, NW_BACK|NW_ENDOFWORD, 1);
928 revto(cx, cy);
929 markdata->second++;
930 cx = markdata->x1 = markdata->cx;
931 cy = markdata->y1 = markdata->cy;
933 nextword(&cx, &cy, NW_ENDOFWORD, rep_cnt);
934 revto(cx, cy);
936 cx = markdata->cx;
937 cy = markdata->cy;
938 /* FALLTHROUGH */
939 case 'A':
940 if (od == 'A')
941 markdata->append_mode = 1;
942 /* FALLTHROUGH */
943 case '>':
944 if (od == '>')
945 markdata->write_buffer = 1;
946 /* FALLTHROUGH */
947 case ' ':
948 case '\r':
949 if (!markdata->second)
951 markdata->second++;
952 markdata->x1 = cx;
953 markdata->y1 = cy;
954 revto(cx, cy);
955 LMsg(0, "First mark set - Column %d Line %d", cx+1, W2D(cy)+1);
956 break;
958 else
960 int append_mode = markdata->append_mode;
961 int write_buffer = markdata->write_buffer;
963 x2 = cx;
964 y2 = cy;
965 newcopylen = rem(markdata->x1, markdata->y1, x2, y2, 2, (char *)0, 0); /* count */
966 if (md_user->u_plop.buf && !append_mode)
967 UserFreeCopyBuffer(md_user);
968 yend = fore->w_height - 1;
969 if (fore->w_histheight - markdata->hist_offset < fore->w_height)
971 markdata->second = 0;
972 yend -= MarkScrollUpDisplay(fore->w_histheight - markdata->hist_offset);
974 if (newcopylen > 0)
976 /* the +3 below is for : cr + lf + \0 */
977 if (md_user->u_plop.buf)
978 md_user->u_plop.buf = realloc(md_user->u_plop.buf,
979 (unsigned) (md_user->u_plop.len + newcopylen + 3));
980 else
982 md_user->u_plop.len = 0;
983 md_user->u_plop.buf = malloc((unsigned) (newcopylen + 3));
985 if (!md_user->u_plop.buf)
987 MarkAbort();
988 in_mark = 0;
989 LMsg(0, "Not enough memory... Sorry.");
990 md_user->u_plop.len = 0;
991 md_user->u_plop.buf = 0;
992 break;
994 if (append_mode)
996 switch (markdata->nonl)
999 * this code defines, what glues lines together
1001 case 0:
1002 if (join_with_cr)
1004 md_user->u_plop.buf[md_user->u_plop.len] = '\r';
1005 md_user->u_plop.len++;
1007 md_user->u_plop.buf[md_user->u_plop.len] = '\n';
1008 md_user->u_plop.len++;
1009 break;
1010 case 1:
1011 break;
1012 case 2:
1013 md_user->u_plop.buf[md_user->u_plop.len] = ' ';
1014 md_user->u_plop.len++;
1015 break;
1016 case 3:
1017 md_user->u_plop.buf[md_user->u_plop.len] = ',';
1018 md_user->u_plop.len++;
1019 break;
1022 md_user->u_plop.len += rem(markdata->x1, markdata->y1, x2, y2,
1023 markdata->hist_offset == fore->w_histheight,
1024 md_user->u_plop.buf + md_user->u_plop.len, yend);
1025 #ifdef ENCODINGS
1026 md_user->u_plop.enc = fore->w_encoding;
1027 #endif
1029 if (markdata->hist_offset != fore->w_histheight)
1031 LAY_CALL_UP(LRefreshAll(flayer, 0));
1033 ExitOverlayPage();
1034 if (append_mode)
1035 LMsg(0, "Appended %d characters to buffer",
1036 newcopylen);
1037 else
1038 LMsg(0, "Copied %d characters into buffer", md_user->u_plop.len);
1039 if (write_buffer)
1040 WriteFile(md_user, (char *)0, DUMP_EXCHANGE);
1041 in_mark = 0;
1042 break;
1044 default:
1045 MarkAbort();
1046 LMsg(0, "Copy mode aborted");
1047 in_mark = 0;
1048 break;
1050 if (in_mark) /* markdata may be freed */
1051 markdata->rep_cnt = 0;
1053 if (in_mark)
1055 flayer->l_x = markdata->cx;
1056 flayer->l_y = W2D(markdata->cy);
1058 *inbufp = pt;
1059 *inlenp = inlen;
1062 void revto(tx, ty)
1063 int tx, ty;
1065 revto_line(tx, ty, -1);
1068 /* tx, ty: WINDOW, line: DISPLAY */
1069 void revto_line(tx, ty, line)
1070 int tx, ty, line;
1072 int fx, fy;
1073 int x, y, t, revst, reven, qq, ff, tt, st, en, ce = 0;
1074 int ystart = 0, yend = fore->w_height-1;
1075 int i, ry;
1076 unsigned char *wi;
1077 struct mline *ml;
1078 struct mchar mc;
1080 if (tx < 0)
1081 tx = 0;
1082 else if (tx > fore->w_width - 1)
1083 tx = fore->w_width -1;
1084 if (ty < 0)
1085 ty = 0;
1086 else if (ty > fore->w_histheight + fore->w_height - 1)
1087 ty = fore->w_histheight + fore->w_height - 1;
1089 fx = markdata->cx; fy = markdata->cy;
1091 #ifdef DW_CHARS
1092 /* don't just move inside of a kanji, the user wants to see something */
1093 ml = WIN(ty);
1094 if (ty == fy && fx + 1 == tx && dw_right(ml, tx, fore->w_encoding) && tx < D_width - 1)
1095 tx++;
1096 if (ty == fy && fx - 1 == tx && dw_right(ml, fx, fore->w_encoding) && tx)
1097 tx--;
1098 #endif
1100 markdata->cx = tx; markdata->cy = ty;
1103 * if we go to a position that is currently offscreen
1104 * then scroll the screen
1106 i = 0;
1107 if (line >= 0 && line < fore->w_height)
1108 i = W2D(ty) - line;
1109 else if (ty < markdata->hist_offset)
1110 i = ty - markdata->hist_offset;
1111 else if (ty > markdata->hist_offset + (fore->w_height - 1))
1112 i = ty - markdata->hist_offset - (fore->w_height - 1);
1113 if (i > 0)
1114 yend -= MarkScrollUpDisplay(i);
1115 else if (i < 0)
1116 ystart += MarkScrollDownDisplay(-i);
1118 if (markdata->second == 0)
1120 LGotoPos(flayer, tx, W2D(ty));
1121 return;
1124 qq = markdata->x1 + markdata->y1 * fore->w_width;
1125 ff = fx + fy * fore->w_width; /* "from" offset in WIN coords */
1126 tt = tx + ty * fore->w_width; /* "to" offset in WIN coords*/
1128 if (ff > tt)
1130 st = tt; en = ff;
1131 x = tx; y = ty;
1133 else
1135 st = ff; en = tt;
1136 x = fx; y = fy;
1138 if (st > qq)
1140 st++;
1141 x++;
1143 if (en < qq)
1144 en--;
1145 if (tt > qq)
1147 revst = qq; reven = tt;
1149 else
1151 revst = tt; reven = qq;
1153 ry = y - markdata->hist_offset;
1154 if (ry < ystart)
1156 y += (ystart - ry);
1157 x = 0;
1158 st = y * fore->w_width;
1159 ry = ystart;
1161 ml = WIN(y);
1162 for (t = st; t <= en; t++, x++)
1164 if (x >= fore->w_width)
1166 x = 0;
1167 y++, ry++;
1168 ml = WIN(y);
1170 if (ry > yend)
1171 break;
1172 if (t == st || x == 0)
1174 wi = ml->image + fore->w_width;
1175 for (ce = fore->w_width; ce >= 0; ce--, wi--)
1176 if (*wi != ' ')
1177 break;
1179 if (x <= ce && x >= markdata->left_mar && x <= markdata->right_mar)
1181 #ifdef DW_CHARS
1182 if (dw_right(ml, x, fore->w_encoding))
1184 if (t == revst)
1185 revst--;
1186 t--;
1187 x--;
1189 #endif
1190 if (t >= revst && t <= reven)
1192 mc = mchar_so;
1193 #ifdef FONT
1194 if (pastefont)
1195 mc.font = ml->font[x];
1196 #endif
1197 mc.image = ml->image[x];
1199 else
1200 copy_mline2mchar(&mc, ml, x);
1201 #ifdef DW_CHARS
1202 if (dw_left(ml, x, fore->w_encoding))
1204 mc.mbcs = ml->image[x + 1];
1205 LPutChar(flayer, &mc, x, W2D(y));
1206 t++;
1208 #endif
1209 LPutChar(flayer, &mc, x, W2D(y));
1210 #ifdef DW_CHARS
1211 if (dw_left(ml, x, fore->w_encoding))
1212 x++;
1213 #endif
1216 LGotoPos(flayer, tx, W2D(ty));
1219 static void
1220 MarkAbort()
1222 int yend, redisp;
1224 debug("MarkAbort\n");
1225 markdata = (struct markdata *)flayer->l_data;
1226 fore = markdata->md_window;
1227 yend = fore->w_height - 1;
1228 redisp = markdata->second;
1229 if (fore->w_histheight - markdata->hist_offset < fore->w_height)
1231 markdata->second = 0;
1232 yend -= MarkScrollUpDisplay(fore->w_histheight - markdata->hist_offset);
1234 if (markdata->hist_offset != fore->w_histheight)
1236 LAY_CALL_UP(LRefreshAll(flayer, 0));
1238 else
1240 rem(markdata->x1, markdata->y1, markdata->cx, markdata->cy, redisp, (char *)0, yend);
1242 ExitOverlayPage();
1246 static void
1247 MarkRedisplayLine(y, xs, xe, isblank)
1248 int y; /* NOTE: y is in DISPLAY coords system! */
1249 int xs, xe;
1250 int isblank;
1252 int wy, x, i, rm;
1253 int sta, sto, cp; /* NOTE: these 3 are in WINDOW coords system */
1254 unsigned char *wi;
1255 struct mline *ml;
1256 struct mchar mchar_marked;
1258 if (y < 0) /* No special full page handling */
1259 return;
1261 markdata = (struct markdata *)flayer->l_data;
1262 fore = markdata->md_window;
1264 mchar_marked = mchar_so;
1266 wy = D2W(y);
1267 ml = WIN(wy);
1269 if (markdata->second == 0)
1271 if (dw_right(ml, xs, fore->w_encoding) && xs > 0)
1272 xs--;
1273 if (dw_left(ml, xe, fore->w_encoding) && xe < fore->w_width - 1)
1274 xe++;
1275 if (xs == 0 && y > 0 && wy > 0 && WIN(wy - 1)->image[flayer->l_width] == 0)
1276 LCDisplayLineWrap(flayer, ml, y, xs, xe, isblank);
1277 else
1278 LCDisplayLine(flayer, ml, y, xs, xe, isblank);
1279 return;
1282 sta = markdata->y1 * fore->w_width + markdata->x1;
1283 sto = markdata->cy * fore->w_width + markdata->cx;
1284 if (sta > sto)
1286 i=sta; sta=sto; sto=i;
1288 cp = wy * fore->w_width + xs;
1290 rm = markdata->right_mar;
1291 for (x = fore->w_width, wi = ml->image + fore->w_width; x >= 0; x--, wi--)
1292 if (*wi != ' ')
1293 break;
1294 if (x < rm)
1295 rm = x;
1297 for (x = xs; x <= xe; x++, cp++)
1298 if (cp >= sta && x >= markdata->left_mar)
1299 break;
1300 #ifdef DW_CHARS
1301 if (dw_right(ml, x, fore->w_encoding))
1302 x--;
1303 #endif
1304 if (x > xs)
1305 LCDisplayLine(flayer, ml, y, xs, x - 1, isblank);
1306 for (; x <= xe; x++, cp++)
1308 if (cp > sto || x > rm)
1309 break;
1310 #ifdef FONT
1311 if (pastefont)
1312 mchar_marked.font = ml->font[x];
1313 #endif
1314 mchar_marked.image = ml->image[x];
1315 #ifdef DW_CHARS
1316 mchar_marked.mbcs = 0;
1317 if (dw_left(ml, x, fore->w_encoding))
1319 mchar_marked.mbcs = ml->image[x + 1];
1320 cp++;
1322 #endif
1323 LPutChar(flayer, &mchar_marked, x, y);
1324 #ifdef DW_CHARS
1325 if (dw_left(ml, x, fore->w_encoding))
1326 x++;
1327 #endif
1329 if (x <= xe)
1330 LCDisplayLine(flayer, ml, y, x, xe, isblank);
1335 * This ugly routine is to speed up GotoPos()
1337 static int
1338 MarkRewrite(ry, xs, xe, rend, doit)
1339 int ry, xs, xe, doit;
1340 struct mchar *rend;
1342 int dx, x, y, st, en, t, rm;
1343 unsigned char *i;
1344 struct mline *ml;
1345 struct mchar mchar_marked;
1347 mchar_marked = mchar_so;
1349 debug3("MarkRewrite %d, %d-%d\n", ry, xs, xe);
1350 markdata = (struct markdata *)flayer->l_data;
1351 fore = markdata->md_window;
1352 y = D2W(ry);
1353 ml = WIN(y);
1354 #ifdef UTF8
1355 if (fore->w_encoding && fore->w_encoding != UTF8 && D_encoding == UTF8 && ContainsSpecialDeffont(ml, xs, xe, fore->w_encoding))
1356 return EXPENSIVE;
1357 #endif
1358 dx = xe - xs + 1;
1359 if (doit)
1361 i = ml->image + xs;
1362 while (dx--)
1363 PUTCHAR(*i++);
1364 return 0;
1367 if (markdata->second == 0)
1368 st = en = -1;
1369 else
1371 st = markdata->y1 * fore->w_width + markdata->x1;
1372 en = markdata->cy * fore->w_width + markdata->cx;
1373 if (st > en)
1375 t = st; st = en; en = t;
1378 t = y * fore->w_width + xs;
1379 for (rm = fore->w_width, i = ml->image + fore->w_width; rm >= 0; rm--)
1380 if (*i-- != ' ')
1381 break;
1382 if (rm > markdata->right_mar)
1383 rm = markdata->right_mar;
1384 x = xs;
1385 while (dx--)
1387 if (t >= st && t <= en && x >= markdata->left_mar && x <= rm)
1389 #ifdef FONT
1390 if (pastefont)
1391 mchar_marked.font = ml->font[x];
1392 #endif
1393 rend->image = mchar_marked.image;
1394 if (!cmp_mchar(rend, &mchar_marked))
1395 return EXPENSIVE;
1397 else
1399 rend->image = ml->image[x];
1400 if (!cmp_mchar_mline(rend, ml, x))
1401 return EXPENSIVE;
1403 x++;
1405 return xe - xs + 1;
1410 * scroll the screen contents up/down.
1412 static int MarkScrollUpDisplay(n)
1413 int n;
1415 int i;
1417 debug1("MarkScrollUpDisplay(%d)\n", n);
1418 if (n <= 0)
1419 return 0;
1420 if (n > fore->w_histheight - markdata->hist_offset)
1421 n = fore->w_histheight - markdata->hist_offset;
1422 markdata->hist_offset += n;
1423 i = (n < flayer->l_height) ? n : (flayer->l_height);
1424 LScrollV(flayer, i, 0, flayer->l_height - 1, 0);
1425 while (i-- > 0)
1426 MarkRedisplayLine(flayer->l_height - i - 1, 0, flayer->l_width - 1, 1);
1427 return n;
1430 static int
1431 MarkScrollDownDisplay(n)
1432 int n;
1434 int i;
1436 debug1("MarkScrollDownDisplay(%d)\n", n);
1437 if (n <= 0)
1438 return 0;
1439 if (n > markdata->hist_offset)
1440 n = markdata->hist_offset;
1441 markdata->hist_offset -= n;
1442 i = (n < flayer->l_height) ? n : (flayer->l_height);
1443 LScrollV(flayer, -i, 0, fore->w_height - 1, 0);
1444 while (i-- > 0)
1445 MarkRedisplayLine(i, 0, flayer->l_width - 1, 1);
1446 return n;
1450 InMark()
1452 if (flayer && flayer->l_layfn == &MarkLf)
1453 return 1;
1454 return 0;
1457 void
1458 MakePaster(pa, buf, len, bufiscopy)
1459 struct paster *pa;
1460 char *buf;
1461 int len;
1462 int bufiscopy;
1464 FreePaster(pa);
1465 pa->pa_pasteptr = buf;
1466 pa->pa_pastelen = len;
1467 if (bufiscopy)
1468 pa->pa_pastebuf = buf;
1469 pa->pa_pastelayer = flayer;
1470 DoProcess(Layer2Window(flayer), &pa->pa_pasteptr, &pa->pa_pastelen, pa);
1473 void
1474 FreePaster(pa)
1475 struct paster *pa;
1477 if (pa->pa_pastebuf)
1478 free(pa->pa_pastebuf);
1479 pa->pa_pastebuf = 0;
1480 pa->pa_pasteptr = 0;
1481 pa->pa_pastelen = 0;
1482 pa->pa_pastelayer = 0;
1483 evdeq(&pa->pa_slowev);
1486 #endif /* COPY_PASTE */