Changeset a60edf2


Ignore:
Timestamp:
Jul 11, 2009, 1:14:35 PM (15 years ago)
Author:
Nelson Elhage <nelhage@mit.edu>
Branches:
master, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
2da7348
Parents:
7f0c26f
git-author:
Nelson Elhage <nelhage@mit.edu> (07/07/09 22:49:21)
git-committer:
Nelson Elhage <nelhage@mit.edu> (07/11/09 13:14:35)
Message:
Implement kill-region, copy-region-as-kill, and yank.
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • commands.c

    r7f0c26f ra60edf2  
    889889                  OWL_CTX_EDIT,
    890890                  "exchanges the point and the mark",
     891                  "", ""),
     892
     893  OWLCMD_VOID_CTX("edit:copy-region-as-kill", owl_editwin_copy_region_as_kill,
     894                  OWL_CTX_EDIT,
     895                  "copy the text between the point and the mark",
     896                  "", ""),
     897
     898  OWLCMD_VOID_CTX("edit:kill-region", owl_editwin_kill_region,
     899                  OWL_CTX_EDIT,
     900                  "kill text between the point and the mark",
     901                  "", ""),
     902
     903  OWLCMD_VOID_CTX("edit:yank", owl_editwin_yank,
     904                  OWL_CTX_EDIT,
     905                  "insert the current text from the kill buffer",
    891906                  "", ""),
    892907
  • editwin.c

    r7f0c26f ra60edf2  
    2525  int index;
    2626  int mark;
     27  char *killbuf;
    2728  int goal_column;
    2829  int topindex;
     
    4142};
    4243
    43 
    4444static void oe_reframe(owl_editwin *e);
    4545static void oe_save_excursion(owl_editwin *e, oe_excursion *x);
    4646static void oe_release_excursion(owl_editwin *e, oe_excursion *x);
    4747static void oe_restore_excursion(owl_editwin *e, oe_excursion *x);
     48static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x);
    4849static int oe_count_glyphs(char *s);
    4950static int oe_char_width(gunichar c, int column);
     
    5657static gunichar owl_editwin_get_char_at_point(owl_editwin *e);
    5758static int owl_editwin_replace(owl_editwin *e, int count, char *s);
     59static char *oe_copy_buf(owl_editwin *e, char *buf, int len);
     60static int oe_copy_region(owl_editwin *e);
    5861
    5962#define INCR 4096
     
    113116  oe_set_index(e, 0);
    114117  oe_set_mark(e, -1);
     118  if (e->killbuf != NULL)
     119    free(e->killbuf);
     120  e->killbuf = NULL;
    115121  e->goal_column = -1;
    116122  e->cursorx = -1;
     
    345351    e->mark = x->mark;
    346352    e->lock = x->lock;
     353
     354    oe_release_excursion(e, x);
     355  }
     356}
     357
     358static void oe_restore_mark_only(owl_editwin *e, oe_excursion *x)
     359{
     360  if (x->valid == VALID_EXCURSION) {
     361    e->mark = x->mark;
    347362
    348363    oe_release_excursion(e, x);
     
    544559  if (need > 0) {
    545560    size = e->allocated + need + INCR - (need % INCR);
    546     p = realloc(e->buff, size);
     561    p = owl_realloc(e->buff, size);
    547562    if (p == NULL) {
    548563      /* XXX signal impending doom somehow and don't do anything */
     
    915930{
    916931  oe_excursion x;
    917   int distance;
    918932
    919933  oe_save_excursion(e, &x);
    920   distance = owl_editwin_forward_word(e);
    921   oe_restore_excursion(e, &x);
    922   owl_editwin_replace(e, distance, "");
     934  oe_set_mark(e, e->index);
     935  owl_editwin_forward_word(e);
     936  owl_editwin_kill_region(e);
     937  oe_restore_mark_only(e, &x);
    923938}
    924939
    925940void owl_editwin_delete_previousword(owl_editwin *e)
    926941{
    927   /* go backwards to the last non-space character, then delete chars */
    928   int distance;
    929 
    930   distance = owl_editwin_backward_word(e);
    931   owl_editwin_replace(e, -distance, "");
     942  oe_excursion x;
     943
     944  oe_save_excursion(e, &x);
     945  oe_set_mark(e, e->index);
     946  owl_editwin_backward_word(e);
     947  owl_editwin_kill_region(e);
     948  oe_restore_mark_only(e, &x);
    932949}
    933950
     
    943960
    944961  oe_save_excursion(e, &x);
     962  owl_editwin_set_mark(e);
    945963  distance = owl_editwin_move_to_end_of_line(e);
     964  if (distance)
     965    owl_editwin_kill_region(e);
     966  else
     967    owl_editwin_replace(e, 1, "");
    946968  oe_restore_excursion(e, &x);
    947   owl_editwin_replace(e, distance ? distance : 1, "");
     969}
     970
     971void owl_editwin_yank(owl_editwin *e)
     972{
     973  if (e->killbuf != NULL)
     974    owl_editwin_replace(e, 0, e->killbuf);
     975}
     976
     977static char *oe_copy_buf(owl_editwin *e, char *buf, int len)
     978{
     979  char *p;
     980
     981  p = owl_malloc(len + 1);
     982
     983  if (p != NULL) {
     984    owl_free(e->killbuf);
     985    e->killbuf = p;
     986    memcpy(e->killbuf, buf, len);
     987    e->killbuf[len] = 0;
     988  }
     989
     990  return p;
     991}
     992
     993static int oe_copy_region(owl_editwin *e)
     994{
     995  char *p;
     996  int start, end;
     997
     998  if (e->mark == -1)
     999    return 0;
     1000
     1001  start = MIN(e->index, e->mark);
     1002  end = MAX(e->index, e->mark);
     1003
     1004  p = oe_copy_buf(e, e->buff + start, end - start);
     1005  if (p != NULL)
     1006    return end - start;
     1007  return 0;
     1008}
     1009
     1010void owl_editwin_copy_region_as_kill(owl_editwin *e)
     1011{
     1012  oe_copy_region(e);
     1013}
     1014
     1015void owl_editwin_kill_region(owl_editwin *e)
     1016{
     1017  if (e->index > e->mark)
     1018    owl_editwin_exchange_point_and_mark(e);
     1019
     1020  owl_editwin_replace(e, oe_copy_region(e), "");
    9481021}
    9491022
  • keys.c

    r7f0c26f ra60edf2  
    8686  BIND_CMD("C-@",         "edit:set-mark", "");
    8787  BIND_CMD("C-x C-x",     "edit:exchange-point-and-mark", "");
     88
     89  BIND_CMD("M-w",         "edit:copy-region-as-kill", "");
     90  BIND_CMD("C-w",         "edit:kill-region", "");
     91  BIND_CMD("C-y",         "edit:yank", "");
    8892
    8993  BIND_CMD("C-l",         "( edit:recenter ; redisplay )", "");
Note: See TracChangeset for help on using the changeset viewer.