source: editwin.c @ 428834d

barnowl_perlaimdebianrelease-1.10release-1.4release-1.5release-1.6release-1.7release-1.8release-1.9
Last change on this file since 428834d was 830c36e, checked in by Alejandro R. Sedeño <asedeno@mit.edu>, 17 years ago
fixing bugs in editwin bufflen calculations.
  • Property mode set to 100644
File size: 26.9 KB
Line 
1#include "owl.h"
2#include <stdlib.h>
3#include <unistd.h>
4#include <string.h>
5#include <ctype.h>
6
7static const char fileIdent[] = "$Id$";
8
9#define INCR 5000
10
11/* initialize the editwin e.
12 * 'win' is an already initialzed curses window that will be used by editwin
13 */
14void owl_editwin_init(owl_editwin *e, WINDOW *win, int winlines, int wincols, int style, owl_history *hist)
15{
16  e->buff=owl_malloc(INCR); 
17  e->buff[0]='\0';
18  e->bufflen=0;
19  e->hist=hist;
20  e->allocated=INCR;
21  e->buffx=0;
22  e->buffy=0;
23  e->topline=0;
24  e->winlines=winlines;
25  e->wincols=wincols;
26  e->fillcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxfillcols(&g));
27  e->wrapcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxwrapcols(&g));
28  e->curswin=win;
29  e->style=style;
30  if ((style!=OWL_EDITWIN_STYLE_MULTILINE) &&
31      (style!=OWL_EDITWIN_STYLE_ONELINE)) {
32    e->style=OWL_EDITWIN_STYLE_MULTILINE;
33  }
34  e->lock=0;
35  e->dotsend=0;
36  e->echochar='\0';
37
38  /*
39    // We get initialized multiple times, but we need to hold on to
40    // the callbacks, so we can't NULL them here.
41    e->command = NULL;
42    e->callback = NULL;
43    e->cbdata = NULL;
44  */
45  if (win) werase(win);
46}
47
48void owl_editwin_set_curswin(owl_editwin *e, WINDOW *w, int winlines, int wincols)
49{
50  e->curswin=w;
51  e->winlines=winlines;
52  e->wincols=wincols;
53  e->fillcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxfillcols(&g));
54  e->wrapcol=owl_editwin_limit_maxcols(wincols-7, owl_global_get_edit_maxwrapcols(&g));
55}
56
57/* echo the character 'ch' for each normal character keystroke,
58 * excepting locktext.  This is useful for entering passwords etc.  If
59 * ch=='\0' characters are echo'd normally
60 */
61void owl_editwin_set_echochar(owl_editwin *e, int ch)
62{
63  e->echochar=ch;
64}
65
66WINDOW *owl_editwin_get_curswin(owl_editwin *e)
67{
68  return(e->curswin);
69}
70
71void owl_editwin_set_history(owl_editwin *e, owl_history *h)
72{
73  e->hist=h;
74}
75
76owl_history *owl_editwin_get_history(owl_editwin *e)
77{
78  return(e->hist);
79}
80
81void owl_editwin_set_dotsend(owl_editwin *e)
82{
83  e->dotsend=1;
84}
85
86void owl_editwin_set_command(owl_editwin *e, char *command) {
87  if(e->command) owl_free(e->command);
88  e->command = owl_strdup(command);
89}
90
91char *owl_editwin_get_command(owl_editwin *e) {
92  if(e->command) return e->command;
93  return "";
94}
95
96void owl_editwin_set_callback(owl_editwin *e, void (*cb)(owl_editwin*)) {
97  e->callback = cb;
98}
99
100void (*owl_editwin_get_callback(owl_editwin *e))(owl_editwin*) {
101  return e->callback;
102}
103
104void owl_editwin_set_cbdata(owl_editwin *e, void *data) {
105  e->cbdata = data;
106}
107
108void* owl_editwin_get_cbdata(owl_editwin *e) {
109  return e->cbdata;
110}
111
112void owl_editwin_do_callback(owl_editwin *e) {
113  void (*cb)(owl_editwin*);
114  cb=owl_editwin_get_callback(e);
115  if(!cb) {
116    owl_function_error("Internal error: No editwin callback!");
117  } else {
118    // owl_function_error("text: |%s|", owl_editwin_get_text(e));
119    cb(e);
120  }
121}
122
123int owl_editwin_limit_maxcols(int v, int maxv)
124{
125  if (maxv > 5 && v > maxv) {
126    return(maxv);
127  } else {
128    return(v);
129  }
130}
131
132/* set text to be 'locked in' at the beginning of the buffer, any
133 * previous text (including locked text) will be overwritten
134 */
135void owl_editwin_set_locktext(owl_editwin *e, char *text)
136{
137 
138  int x, y;
139
140  x=e->buffx;
141  y=e->buffy;
142  e->buffx=0;
143  e->buffy=0;
144  owl_editwin_overwrite_string(e, text);
145  owl_editwin_overwrite_char(e, '\0');
146  e->lock=strlen(text);
147  /* if (text[e->lock-1]=='\n') e->lock--; */
148  /*  e->buffx=x; */
149  /*  e->buffy=y; */
150  owl_editwin_adjust_for_locktext(e);
151  owl_editwin_redisplay(e, 0);
152}
153
154int owl_editwin_get_style(owl_editwin *e)
155{
156  return(e->style);
157}
158
159void owl_editwin_new_style(owl_editwin *e, int newstyle, owl_history *h)
160{
161  char *ptr;
162
163  owl_editwin_set_history(e, h);
164  if (e->style==newstyle) return;
165
166  if (newstyle==OWL_EDITWIN_STYLE_MULTILINE) {
167    e->style=newstyle;
168  } else if (newstyle==OWL_EDITWIN_STYLE_ONELINE) {
169    e->style=newstyle;
170
171    /* nuke everything after the first line */
172    if (e->bufflen > 0) {
173      ptr=strchr(e->buff, '\n')-1;
174      if (ptr) {
175        e->bufflen=ptr - e->buff;
176        e->buff[e->bufflen]='\0';
177        e->buffx=0;
178        e->buffy=0;
179      }
180    }
181  }
182}
183
184/* completly reinitialize the buffer */
185void owl_editwin_fullclear(owl_editwin *e)
186{
187  owl_free(e->buff);
188  owl_editwin_init(e, e->curswin, e->winlines, e->wincols, e->style, e->hist);
189}
190
191/* clear all text except for locktext and put the cursor at the
192 * beginning
193 */
194void owl_editwin_clear(owl_editwin *e)
195{
196
197  int lock;
198  int dotsend=e->dotsend;
199  char *locktext=NULL;
200
201  lock=0;
202  if (e->lock > 0) {
203    lock=1;
204
205    locktext=owl_malloc(e->lock+20);
206    strncpy(locktext, e->buff, e->lock);
207    locktext[e->lock]='\0';
208  }
209
210  owl_free(e->buff);
211  owl_editwin_init(e, e->curswin, e->winlines, e->wincols, e->style, e->hist);
212
213  if (lock > 0) {
214    owl_editwin_set_locktext(e, locktext);
215  }
216  if (dotsend) {
217    owl_editwin_set_dotsend(e);
218  }
219
220  if (locktext) owl_free(locktext);
221  owl_editwin_adjust_for_locktext(e);
222}
223
224/* malloc more space for the buffer */
225void _owl_editwin_addspace(owl_editwin *e)
226{
227  e->buff=owl_realloc(e->buff, e->allocated+INCR);
228  if (!e->buff) {
229    /* error */
230    return;
231  }
232  e->allocated+=INCR;
233}
234
235void owl_editwin_recenter(owl_editwin *e)
236{
237  e->topline=e->buffy-(e->winlines/2);
238  if (e->topline<0) e->topline=0;
239  if (e->topline>owl_editwin_get_numlines(e)) e->topline=owl_editwin_get_numlines(e);
240}
241
242/* regenerate the text on the curses window */
243/* if update == 1 then do a doupdate(), otherwise do not */
244void owl_editwin_redisplay(owl_editwin *e, int update)
245{
246 
247  char *ptr1, *ptr2, *ptr3, *buff;
248  int i;
249
250  werase(e->curswin);
251  wmove(e->curswin, 0, 0);
252
253  /* start at topline */
254  ptr1 = e->buff;
255  for (i = 0; i < e->topline; i++) {
256    ptr2 = strchr(ptr1, '\n');
257    if (!ptr2) {
258      /* we're already on the last line */
259      break;
260    }
261    ptr1 = ptr2 + 1;
262  }
263  /* ptr1 now stores the starting point */
264
265  /* find the ending point and store it in ptr3 */
266  ptr2 = ptr1;
267  ptr3 = ptr1;
268  for (i = 0; i < e->winlines; i++) {
269    ptr3 = strchr(ptr2, '\n');
270    if (!ptr3) {
271      /* we've hit the last line */
272      /* print everything to the end */
273      ptr3 = e->buff + e->bufflen - 1;
274      ptr3--;
275      break;
276    }
277    ptr2 = ptr3 + 1;
278  }
279  ptr3 += 2;
280
281  buff = owl_malloc(ptr3 - ptr1 + 50);
282  strncpy(buff, ptr1, ptr3 - ptr1);
283  buff[ptr3 - ptr1] = '\0';
284  if (e->echochar == '\0') {
285    waddstr(e->curswin, buff);
286  } else {
287    /* translate to echochar, *except* for the locktext */
288    int len;
289    int dolocklen = e->lock - (ptr1 - e->buff);
290
291    for (i = 0; i < dolocklen; i++) {
292      waddch(e->curswin, buff[i]);
293    }
294    len = strlen(buff);
295    for (i = 0; i < len-dolocklen; i++) {
296      waddch(e->curswin, e->echochar);
297    }
298  }
299  wmove(e->curswin, e->buffy-e->topline, e->buffx + _owl_editwin_cursor_adjustment(e));
300  wnoutrefresh(e->curswin);
301  if (update == 1) {
302    doupdate();
303  }
304  owl_free(buff);
305}
306
307/* Remove n bytes at cursor. */
308void _owl_editwin_remove_bytes(owl_editwin *e, int n) /*noproto*/
309{
310  int i = _owl_editwin_get_index_from_xy(e) + n;
311  for (; i < e->bufflen; i++) {
312    e->buff[i-n] = e->buff[i];
313  }
314 
315  e->bufflen -= n;
316  e->buff[e->bufflen] = '\0';
317}
318
319/* Insert n bytes at cursor.*/
320void _owl_editwin_insert_bytes(owl_editwin *e, int n) /*noproto*/
321{
322  int i, z;
323 
324  if ((e->bufflen + n) > (e->allocated - 5)) {
325    _owl_editwin_addspace(e);
326  }
327
328  e->bufflen += n;
329  e->buff[e->bufflen] = '\0';
330 
331  z = _owl_editwin_get_index_from_xy(e);
332  for (i = e->bufflen - 1; i > z; i--) {
333    e->buff[i] = e->buff[i - n];
334  }
335}
336
337
338/* linewrap the word just before the cursor.
339 * returns 0 on success
340 * returns -1 if we could not wrap.
341 */
342int _owl_editwin_linewrap_word(owl_editwin *e)
343{
344  int i, z;
345
346  z = _owl_editwin_get_index_from_xy(e);
347  /* move back and line wrap the previous word */
348  for (i = z - 1; ; i--) {
349    /* move back until you find a space or hit the beginning of the line */
350    if (e->buff[i] == ' ') {
351      /* replace the space with a newline */
352      e->buff[i] = '\n';
353      e->buffy++;
354      e->buffx = z - i - 1;
355      /* were we on the last line */
356      return(0);
357    } else if (e->buff[i] == '\n' || i <= e->lock) {
358      /* we hit the beginning of the line or the buffer, we cannot
359       * wrap.
360       */
361      return(-1);
362    }
363  }
364}
365
366/* insert a character at the current point (shift later
367 * characters over)
368 */
369void owl_editwin_insert_char(owl_editwin *e, gunichar c)
370{
371  int z, i, ret, len;
372  char tmp[6];
373  memset(tmp, '\0', 6);
374
375  /* \r is \n */
376  if (c == '\r') {
377    c = '\n';
378  }
379
380  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
381    /* perhaps later this will change some state that allows the string
382       to be read */
383    return;
384  }
385
386  g_unichar_to_utf8(c, tmp);
387  len = strlen(tmp);
388
389  /* make sure there is enough memory for the new text */
390  if ((e->bufflen + len) > (e->allocated - 5)) {
391    _owl_editwin_addspace(e);
392  }
393
394  /* get the insertion point */
395  z = _owl_editwin_get_index_from_xy(e);
396
397  /* If we're going to insert at the last column do word wrapping, unless it's a \n */
398  if ((e->buffx + 1 == e->wrapcol) && (c != '\n')) {
399    ret = _owl_editwin_linewrap_word(e);
400    if (ret == -1) {
401      /* we couldn't wrap, insert a hard newline instead */
402      owl_editwin_insert_char(e, '\n');
403    }
404  }
405
406  /* shift all the other characters right */
407  if (z != e->bufflen) {
408    _owl_editwin_insert_bytes(e, len);
409  }
410
411  /* insert the new character */
412  for(i = 0; i < len; i++) {
413    e->buff[z + i] = tmp[i];
414  }
415
416  /* housekeeping */
417  e->bufflen += len;
418  e->buff[e->bufflen] = '\0';
419 
420  /* advance the cursor */
421  z += len;
422  _owl_editwin_set_xy_by_index(e, z);
423}
424
425/* overwrite the character at the current point with 'c' */
426void owl_editwin_overwrite_char(owl_editwin *e, gunichar c)
427{
428  int z, oldlen, newlen, i;
429  char tmp[6];
430  memset(tmp, '\0', 6);
431
432  /* \r is \n */
433  if (c == '\r') {
434    c = '\n';
435  }
436 
437  if (c == '\n' && e->style == OWL_EDITWIN_STYLE_ONELINE) {
438    /* perhaps later this will change some state that allows the string
439       to be read */
440    return;
441  }
442
443  g_unichar_to_utf8(c, tmp);
444  newlen = strlen(tmp);
445
446  z = _owl_editwin_get_index_from_xy(e);
447  {
448    char *t = g_utf8_find_next_char(e->buff + z, NULL);
449    oldlen = (t ? (t - (e->buff + z)) : 0);
450  }
451
452  /* only if we are at the end of the buffer do we create new space here */
453  if (z == e->bufflen) {
454    if ((e->bufflen+newlen) > (e->allocated-5)) {
455      _owl_editwin_addspace(e);
456    }
457  }
458  /* if not at the end of the buffer, adjust based in char size difference. */ 
459  else if (oldlen > newlen) {
460    _owl_editwin_remove_bytes(e, oldlen-newlen);
461  }
462  else /* oldlen < newlen */ {
463    _owl_editwin_insert_bytes(e, newlen-oldlen);
464  }
465  /* Overwrite the old char*/
466  for (i = 0; i < newlen; i++) {
467    e->buff[z+i] = tmp[i];
468  }
469       
470  /* housekeeping */
471  if (z == e->bufflen) {
472    e->bufflen += newlen;
473    e->buff[e->bufflen] = '\0';
474  }
475 
476  /* advance the cursor */
477  z += newlen;
478  _owl_editwin_set_xy_by_index(e, z);
479}
480
481/* delete the character at the current point, following chars
482 * shift left.
483 */ 
484void owl_editwin_delete_char(owl_editwin *e)
485{
486  int z;
487  char *p1, *p2;
488  gunichar c;
489
490  if (e->bufflen == 0) return;
491 
492  /* get the deletion point */
493  z = _owl_editwin_get_index_from_xy(e);
494
495  if (z == e->bufflen) return;
496
497  p1 = e->buff + z;
498  p2 = g_utf8_next_char(p1);
499  c = g_utf8_get_char(p2);
500  while (g_unichar_ismark(c)) {
501    p2 = g_utf8_next_char(p2);
502    c = g_utf8_get_char(p2);
503  }
504  _owl_editwin_remove_bytes(e, p2-p1);
505}
506
507/* Swap the character at point with the character at point-1 and
508 * advance the pointer.  If point is at beginning of buffer do
509 * nothing.  If point is after the last character swap point-1 with
510 * point-2.  (Behaves as observed in tcsh and emacs). 
511 */
512void owl_editwin_transpose_chars(owl_editwin *e)
513{
514  int z;
515  char *p1, *p2, *p3, *tmp;
516
517  if (e->bufflen == 0) return;
518 
519  /* get the cursor point */
520  z = _owl_editwin_get_index_from_xy(e);
521
522  if (z == e->bufflen) {
523    /* point is after last character */
524    z--;
525  } 
526
527  if (z - 1 < e->lock) {
528    /* point is at beginning of buffer, do nothing */
529    return;
530  }
531
532  /* Transpose two utf-8 unicode glyphs. */
533  p1 = e->buff + z;
534
535  p2 = g_utf8_find_next_char(p1, NULL);
536  while (p2 != NULL && g_unichar_ismark(g_utf8_get_char(p2))) {
537    p2 = g_utf8_find_next_char(p2, NULL);
538  }
539  if (p2 == NULL) return;
540
541  p3 = g_utf8_find_prev_char(e->buff, p1);
542  while (p3 != NULL && g_unichar_ismark(g_utf8_get_char(p3))) {
543    p3 = g_utf8_find_prev_char(p3, NULL);
544  }
545  if (p3 == NULL) return;
546
547  tmp = owl_malloc(p2 - p3 + 5);
548  *tmp = '\0';
549  strncat(tmp, p1, p2 - p1);
550  strncat(tmp, p3, p1 - p3);
551  strncpy(p3, tmp, p2 - p3);
552  owl_free(tmp);
553  _owl_editwin_set_xy_by_index(e, p3 - e->buff);
554}
555
556/* insert 'string' at the current point, later text is shifted
557 * right
558 */
559void owl_editwin_insert_string(owl_editwin *e, char *string)
560{
561  char *p;
562  gunichar c;
563  if (!g_utf8_validate(string, -1, NULL)) {
564    owl_function_debugmsg("owl_editwin_insert_string: received non-utf-8 string.");
565    return;
566  }
567  p = string;
568  c = g_utf8_get_char(p);
569  while (c) {
570    owl_editwin_process_char(e, c);
571    p = g_utf8_next_char(p);
572    c = g_utf8_get_char(p);
573  }
574}
575
576/* write 'string' at the current point, overwriting text that is
577 * already there
578 */
579
580void owl_editwin_overwrite_string(owl_editwin *e, char *string)
581{
582  char *p;
583  gunichar c;
584
585  if (!g_utf8_validate(string, -1, NULL)) {
586    owl_function_debugmsg("owl_editwin_overwrite_string: received non-utf-8 string.");
587    return;
588  }
589  p = string;
590  c = g_utf8_get_char(p);
591  while (c) {
592    owl_editwin_overwrite_char(e, c);
593    p = g_utf8_next_char(p);
594    c = g_utf8_get_char(p);
595  }
596}
597
598/* get the index into e->buff for the current cursor
599 * position.
600 */
601int _owl_editwin_get_index_from_xy(owl_editwin *e)
602{
603  int i;
604  char *ptr1, *ptr2;
605  gunichar c;
606
607  if (e->bufflen == 0) return(0);
608 
609  /* first go to the yth line */
610  ptr1 = e->buff;
611  for (i = 0; i < e->buffy; i++) {
612    ptr2= strchr(ptr1, '\n');
613    if (!ptr2) {
614      /* we're already on the last line */
615      break;
616    }
617    ptr1 = ptr2 + 1;
618  }
619
620  /* now go to the xth cell */
621  ptr2 = ptr1;
622  i = 0;
623  while (ptr2 != NULL && i < e->buffx && (ptr2 - e->buff) < e->bufflen) {
624    c = g_utf8_get_char(ptr2);
625    i += (c == '\n' ? 1 : mk_wcwidth(c));
626    ptr2 = g_utf8_next_char(ptr2);
627  }
628  while(ptr2 != NULL && g_unichar_ismark(g_utf8_get_char(ptr2))) {
629    ptr2 = g_utf8_next_char(ptr2);
630  }
631  if (ptr2 == NULL) return e->bufflen;
632  return(ptr2 - e->buff);
633}
634
635void _owl_editwin_set_xy_by_index(owl_editwin *e, int index)
636{
637  char *ptr1, *ptr2, *target;
638  gunichar c;
639
640  e->buffx = 0;
641  e->buffy = 0;
642
643  ptr1 = e->buff;
644  target = ptr1 + index;
645  /* target sanitizing */
646  if ((target[0] & 0x80) && (~target[0] & 0x40)) {
647    /* middle of a utf-8 character, back up to previous character. */
648    target = g_utf8_find_prev_char(e->buff, target);
649  }
650  c = g_utf8_get_char(target);
651  while (g_unichar_ismark(c) && target > e->buff) {
652    /* Adjust the target off of combining characters and the like. */
653    target = g_utf8_find_prev_char(e->buff, target);
654    c = g_utf8_get_char(target);
655  }
656  /* If we start with a mark, something is wrong.*/
657  if (g_unichar_ismark(c)) return;
658
659  /* Now our target should be acceptable. */
660  ptr2 = strchr(ptr1, '\n');
661  while (ptr2 != NULL && ptr2 < target) {
662    e->buffy++;
663    ptr1 = ptr2 + 1;
664    ptr2 = strchr(ptr1, '\n');
665  }
666  ptr2 = ptr1;
667  while (ptr2 != NULL && ptr2 < target) {
668    c = g_utf8_get_char(ptr2);
669    e->buffx += mk_wcwidth(c);
670    ptr2 = g_utf8_next_char(ptr2);
671  }
672}
673
674int _owl_editwin_cursor_adjustment(owl_editwin *e)
675{
676  char *ptr1, *ptr2;
677  gunichar c;
678  int x, i;
679
680  ptr1 = e->buff;
681  ptr2 = strchr(ptr1, '\n');
682  for (i = 0; ptr2 != NULL && i < e->buffy; i++) {
683    ptr1 = ptr2 + 1;
684    ptr2 = strchr(ptr1, '\n');
685  }
686  ptr2 = ptr1;
687  x = 0;
688  while (ptr2 != NULL && x < e->buffx) {
689    if (*ptr2 == '\n') return 0;
690    c = g_utf8_get_char(ptr2);
691    x += mk_wcwidth(c);
692    ptr2 = g_utf8_next_char(ptr2);
693  }
694  return x - e->buffx;
695}
696
697void owl_editwin_adjust_for_locktext(owl_editwin *e)
698{
699  /* if we happen to have the cursor over locked text
700   * move it to be out of the locktext region */
701  if (_owl_editwin_get_index_from_xy(e) < e->lock) {
702    _owl_editwin_set_xy_by_index(e, e->lock);
703  }
704}
705
706void owl_editwin_backspace(owl_editwin *e)
707{
708  /* delete the char before the current one
709   * and shift later chars left
710   */
711  if (_owl_editwin_get_index_from_xy(e) > e->lock) {
712    owl_editwin_key_left(e);
713    owl_editwin_delete_char(e);
714  }
715  owl_editwin_adjust_for_locktext(e);
716}
717
718void owl_editwin_key_up(owl_editwin *e)
719{
720  if (e->buffy > 0) e->buffy--;
721  if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) {
722    e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
723  }
724
725  /* do we need to scroll? */
726  if (e->buffy-e->topline < 0) {
727    e->topline-=e->winlines/2;
728  }
729
730  owl_editwin_adjust_for_locktext(e);
731}
732
733void owl_editwin_key_down(owl_editwin *e)
734{
735  /* move down if we can */
736  if (e->buffy+1 < owl_editwin_get_numlines(e)) e->buffy++;
737
738  /* if we're past the last character move back */
739  if (e->buffx >= owl_editwin_get_numcells_on_line(e, e->buffy)) {
740    e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
741  }
742
743  /* do we need to scroll? */
744  if (e->buffy-e->topline > e->winlines) {
745    e->topline+=e->winlines/2;
746  }
747
748  /* adjust for locktext */
749  owl_editwin_adjust_for_locktext(e);
750}
751
752void owl_editwin_key_left(owl_editwin *e)
753{
754  int i;
755  char * p;
756  i = _owl_editwin_get_index_from_xy(e);
757  p = e->buff + i;
758  p = g_utf8_find_prev_char(e->buff, p);
759  while (p && g_unichar_ismark(g_utf8_get_char(p))) {
760    p = g_utf8_find_prev_char(e->buff, p);
761  }
762  if (p == NULL) p = e->buff;
763  _owl_editwin_set_xy_by_index(e, p - e->buff);
764
765  if (e->buffy - e->topline < 0) {
766    e->topline -= e->winlines / 2;
767  }
768
769  /* make sure to avoid locktext */
770  owl_editwin_adjust_for_locktext(e);
771}
772
773void owl_editwin_key_right(owl_editwin *e)
774{
775  int i;
776  char * p;
777  i = _owl_editwin_get_index_from_xy(e);
778  p = e->buff + i;
779  p = g_utf8_find_next_char(p, NULL);
780  while (p && g_unichar_ismark(g_utf8_get_char(p))) {
781    p = g_utf8_find_next_char(p, NULL);
782  }
783  if (p == NULL) {
784    _owl_editwin_set_xy_by_index(e, e->bufflen);
785  }
786  else {
787    _owl_editwin_set_xy_by_index(e, p - e->buff);
788  }
789
790  /* do we need to scroll down? */
791  if (e->buffy - e->topline >= e->winlines) {
792    e->topline += e->winlines / 2;
793  }
794}
795
796void owl_editwin_move_to_nextword(owl_editwin *e)
797{
798  /* asedeno: needs fixing for utf-8*/
799  int i, x;
800
801  /* if we're starting on a space, find the first non-space */
802  i=_owl_editwin_get_index_from_xy(e);
803  if (e->buff[i]==' ') {
804    for (x=i; x<e->bufflen; x++) {
805      if (e->buff[x]!=' ' && e->buff[x]!='\n') {
806        _owl_editwin_set_xy_by_index(e, x);
807        break;
808      }
809    }
810  }
811
812  /* find the next space, newline or end of line and go there, if
813     already at the end of the line, continue on to the next */
814  i=owl_editwin_get_numchars_on_line(e, e->buffy);
815  if (e->buffx < i) {
816    /* move right till end of line */
817    while (e->buffx < i) {
818      e->buffx++;
819      if (e->buff[_owl_editwin_get_index_from_xy(e)]==' ') return;
820      if (e->buffx == i) return;
821    }
822  } else if (e->buffx == i) {
823    /* try to move down */
824    if (e->style==OWL_EDITWIN_STYLE_MULTILINE) {
825      if (e->buffy+1 <  owl_editwin_get_numlines(e)) {
826        e->buffx=0;
827        e->buffy++;
828        owl_editwin_move_to_nextword(e);
829      }
830    }
831  }
832}
833
834/* go backwards to the last non-space character
835 */
836void owl_editwin_move_to_previousword(owl_editwin *e)
837{
838  /* asedeno: needs fixing for utf-8*/
839  int i, x;
840
841  /* are we already at the beginning of the word? */
842  i=_owl_editwin_get_index_from_xy(e);
843  if ( (e->buff[i]!=' ' && e->buff[i]!='\n' && e->buff[i]!='\0') &&
844       (e->buff[i-1]==' ' || e->buff[i-1]=='\n') ) {
845    owl_editwin_key_left(e);
846  }
847   
848  /* are we starting on a space character? */
849  i=_owl_editwin_get_index_from_xy(e);
850  if (e->buff[i]==' ' || e->buff[i]=='\n' || e->buff[i]=='\0') {
851    /* find the first non-space */
852    for (x=i; x>=e->lock; x--) {
853      if (e->buff[x]!=' ' && e->buff[x]!='\n' && e->buff[x]!='\0') {
854        _owl_editwin_set_xy_by_index(e, x);
855        break;
856      }
857    }
858  }
859
860  /* find the last non-space */
861  i=_owl_editwin_get_index_from_xy(e);
862  for (x=i; x>=e->lock; x--) {
863    if (e->buff[x-1]==' ' || e->buff[x-1]=='\n') {
864      _owl_editwin_set_xy_by_index(e, x);
865      break;
866    }
867  }
868  _owl_editwin_set_xy_by_index(e, x);
869}
870
871
872void owl_editwin_delete_nextword(owl_editwin *e)
873{
874  /* asedeno: needs fixing for utf-8*/
875  int z;
876
877  if (e->bufflen==0) return;
878
879  /* if we start out on a space character then gobble all the spaces
880     up first */
881  while (1) {
882    z=_owl_editwin_get_index_from_xy(e);
883    if (e->buff[z]==' ' || e->buff[z]=='\n') {
884      owl_editwin_delete_char(e);
885    } else {
886      break;
887    }
888  }
889
890  /* then nuke the next word */
891  while (1) {
892    z=_owl_editwin_get_index_from_xy(e);
893    /* z == e->bufflen check added to prevent a hang I (nelhage) have
894       seen repeatedly while using owl. I'm not sure precisely what
895       conditions lead to it. */
896    if (z == e->bufflen
897        || e->buff[z+1]==' ' || e->buff[z+1]=='\n' || e->buff[z+1]=='\0') break;
898    owl_editwin_delete_char(e);
899  }
900  owl_editwin_delete_char(e);
901}
902
903void owl_editwin_delete_previousword(owl_editwin *e)
904{
905  /* asedeno: needs fixing for utf-8*/
906  /* go backwards to the last non-space character, then delete chars */
907  int i, startpos, endpos;
908
909  startpos = _owl_editwin_get_index_from_xy(e);
910  owl_editwin_move_to_previousword(e);
911  endpos = _owl_editwin_get_index_from_xy(e);
912  for (i=0; i<startpos-endpos; i++) {
913    owl_editwin_delete_char(e);
914  }
915}
916
917void owl_editwin_delete_to_endofline(owl_editwin *e)
918{
919  /* asedeno: needs fixing for utf-8*/
920  int i;
921
922  if (owl_editwin_get_numchars_on_line(e, e->buffy) > e->buffx) {
923    /* normal line */
924    i=_owl_editwin_get_index_from_xy(e);
925    while(i < e->bufflen) {
926      if (e->buff[i]!='\n') {
927        owl_editwin_delete_char(e);
928      } else if ((e->buff[i]=='\n') && (i==e->bufflen-1)) {
929        owl_editwin_delete_char(e);
930      } else {
931        return;
932      }
933    }
934  } else if (e->buffy+1 < owl_editwin_get_numlines(e)) {
935    /* line with cursor at the end but not on very last line */
936    owl_editwin_key_right(e);
937    owl_editwin_backspace(e);
938  }
939}
940
941void owl_editwin_move_to_line_end(owl_editwin *e)
942{
943  e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
944}
945
946void owl_editwin_move_to_line_start(owl_editwin *e)
947{
948  e->buffx=0;
949  owl_editwin_adjust_for_locktext(e);
950}
951
952void owl_editwin_move_to_end(owl_editwin *e)
953{
954  /* go to last char */
955  e->buffy=owl_editwin_get_numlines(e)-1;
956  e->buffx=owl_editwin_get_numcells_on_line(e, e->buffy);
957  owl_editwin_key_right(e);
958
959  /* do we need to scroll? */
960  /*
961  if (e->buffy-e->topline > e->winlines) {
962    e->topline+=e->winlines/2;
963  }
964  */
965  owl_editwin_recenter(e);
966}
967
968void owl_editwin_move_to_top(owl_editwin *e)
969{
970  _owl_editwin_set_xy_by_index(e, 0);
971
972  /* do we need to scroll? */
973  e->topline=0;
974
975  owl_editwin_adjust_for_locktext(e);
976}
977
978void owl_editwin_fill_paragraph(owl_editwin *e)
979{
980  /* asedeno: needs fixing for utf-8*/
981  int i, save;
982
983  /* save our starting point */
984  save=_owl_editwin_get_index_from_xy(e);
985
986  /* scan back to the beginning of this paragraph */
987  for (i=save; i>=e->lock; i--) {
988    if ( (i<=e->lock) ||
989         ((e->buff[i]=='\n') && (e->buff[i-1]=='\n'))) {
990      _owl_editwin_set_xy_by_index(e, i+1);
991      break;
992    }
993  }
994
995  /* main loop */
996  while (1) {
997    i = _owl_editwin_get_index_from_xy(e);
998
999    /* bail if we hit the end of the buffer */
1000    if (i >= e->bufflen || e->buff[i] == '\0') break;
1001
1002    /* bail if we hit the end of the paragraph */
1003    if (e->buff[i] == '\n' && e->buff[i+1] == '\n') break;
1004
1005    /* if we've travelled too far, linewrap */
1006    if ((e->buffx) >= e->fillcol) {
1007      _owl_editwin_linewrap_word(e);
1008    }
1009
1010    /* did we hit the end of a line too soon? */
1011    i = _owl_editwin_get_index_from_xy(e);
1012    if (e->buff[i] == '\n' && e->buffx < e->fillcol - 1) {
1013      /* ********* we need to make sure we don't pull in a word that's too long ***********/
1014      e->buff[i]=' ';
1015    }
1016   
1017    /* fix spacing */
1018    i = _owl_editwin_get_index_from_xy(e);
1019    if (e->buff[i] == ' ' && e->buff[i+1] == ' ') {
1020      if (e->buff[i-1] == '.' || e->buff[i-1] == '!' || e->buff[i-1] == '?') {
1021        owl_editwin_key_right(e);
1022      } else {
1023        owl_editwin_delete_char(e);
1024        /* if we did this ahead of the save point, adjust it */
1025        if (i < save) save--;
1026      }
1027    } else {
1028      owl_editwin_key_right(e);
1029    }
1030
1031  }
1032
1033  /* put cursor back at starting point */
1034  _owl_editwin_set_xy_by_index(e, save);
1035
1036  /* do we need to scroll? */
1037  if (e->buffy-e->topline < 0) {
1038    e->topline-=e->winlines/2;
1039  }
1040}
1041
1042/* returns true if only whitespace remains */
1043int owl_editwin_is_at_end(owl_editwin *e)
1044{
1045  int cur=_owl_editwin_get_index_from_xy(e);
1046  return (only_whitespace(e->buff+cur));
1047}
1048
1049int owl_editwin_check_dotsend(owl_editwin *e)
1050{
1051  char *p, *p_n, *p_p;
1052  gunichar c;
1053
1054  if (!e->dotsend) return(0);
1055
1056  p = g_utf8_find_prev_char(e->buff, e->buff + e->bufflen);
1057  p_n = g_utf8_find_next_char(p, NULL);
1058  p_p = g_utf8_find_prev_char(e->buff, p);
1059  c = g_utf8_get_char(p);
1060  while (p != NULL) {
1061    if (*p == '.'
1062        && p_p != NULL && (*p_p == '\n' || *p_p == '\r')
1063        && p_n != NULL && (*p_n == '\n' || *p_n == '\r')) {
1064      e->bufflen = p - e->buff;
1065      e->buff[e->bufflen] = '\0';
1066      return(1);
1067    }
1068    if (c != '\0' && !g_unichar_isspace(c)) return(0);
1069    p_n = p;
1070    p = p_p;
1071    c = g_utf8_get_char(p);
1072    p_p = g_utf8_find_prev_char(e->buff, p);
1073  }
1074  return(0);
1075}
1076
1077void owl_editwin_post_process_char(owl_editwin *e, gunichar j)
1078{
1079  /* check if we need to scroll down */
1080  if (e->buffy-e->topline >= e->winlines) {
1081    e->topline+=e->winlines/2;
1082  }
1083  if ((j==13 || j==10) && owl_editwin_check_dotsend(e)) {
1084    owl_command_editmulti_done(e);
1085    return;
1086  }
1087  owl_editwin_redisplay(e, 0); 
1088}
1089
1090void owl_editwin_process_char(owl_editwin *e, gunichar j)
1091{
1092  if (j == ERR) return;
1093  if (g_unichar_iscntrl(j) && (j != 10) && (j != 13)) {
1094    return;
1095  }
1096  else {
1097    owl_editwin_insert_char(e, j);
1098  }
1099}
1100
1101char *owl_editwin_get_text(owl_editwin *e)
1102{
1103  return(e->buff+e->lock);
1104}
1105
1106int owl_editwin_get_numchars_on_line(owl_editwin *e, int line)
1107{
1108  int i;
1109  char *ptr1, *ptr2;
1110
1111  if (e->bufflen==0) return(0);
1112 
1113  /* first go to the yth line */
1114  ptr1=e->buff;
1115  for (i=0; i<line; i++) {
1116    ptr2=strchr(ptr1, '\n');
1117    if (!ptr2) {
1118      /* we're already on the last line */
1119      return(0);
1120    }
1121    ptr1=ptr2+1;
1122  }
1123
1124  /* now count characters */
1125  i = 0;
1126  ptr2 = ptr1;
1127  while (ptr2 - e->buff < e->bufflen
1128         && *ptr2 != '\n') {
1129    ++i;
1130    ptr2 = g_utf8_next_char(ptr2);
1131  }
1132  return i;
1133}
1134
1135int owl_editwin_get_numcells_on_line(owl_editwin *e, int line)
1136{
1137  int i;
1138  char *ptr1, *ptr2;
1139  gunichar c;
1140
1141  if (e->bufflen==0) return(0);
1142 
1143  /* first go to the yth line */
1144  ptr1=e->buff;
1145  for (i=0; i<line; i++) {
1146    ptr2=strchr(ptr1, '\n');
1147    if (!ptr2) {
1148      /* we're already on the last line */
1149      return(0);
1150    }
1151    ptr1=ptr2+1;
1152  }
1153
1154  /* now count cells */
1155  i = 0;
1156  ptr2 = ptr1;
1157  while (ptr2 - e->buff < e->bufflen
1158         && *ptr2 != '\n') {
1159    c = g_utf8_get_char(ptr2);
1160    i += mk_wcwidth(c);
1161    ptr2 = g_utf8_next_char(ptr2);
1162  }
1163  return i;
1164}
1165
1166int owl_editwin_get_numlines(owl_editwin *e)
1167{
1168  return(owl_text_num_lines(e->buff));
1169}
1170
Note: See TracBrowser for help on using the repository browser.