source: editwin.c @ 7f0c26f

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