source: editwin.c @ 23e1110

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