home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / new / util / edit / jade / src / edit.c < prev    next >
C/C++ Source or Header  |  1994-10-04  |  19KB  |  783 lines

  1. /* edit.c -- Editing buffers
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4.    This file is part of Jade.
  5.  
  6.    Jade is free software; you can redistribute it and/or modify it
  7.    under the terms of the GNU General Public License as published by
  8.    the Free Software Foundation; either version 2, or (at your option)
  9.    any later version.
  10.  
  11.    Jade is distributed in the hope that it will be useful, but
  12.    WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.     See the
  14.    GNU General Public License for more details.
  15.  
  16.    You should have received a copy of the GNU General Public License
  17.    along with Jade; see the file COPYING.    If not, write to
  18.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "jade.h"
  21. #include "jade_protos.h"
  22.  
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <ctype.h>
  26.  
  27. #ifdef NEED_MEMORY_H
  28. # include <memory.h>
  29. #endif
  30.  
  31. _PR bool clear_line_list(TX *);
  32. _PR void kill_line_list(TX *);
  33. _PR LINE *resize_line_list(TX *, long, long);
  34. _PR bool insert_gap(TX *, long, const POS *);
  35. _PR bool insert_str(TX *, const u_char *, POS *);
  36. _PR bool insert_str_n(TX *, const u_char *, long, POS *);
  37. _PR bool insert_string(TX *, const u_char *, long, POS *);
  38. _PR bool delete_chars(TX *, const POS *, long);
  39. _PR bool delete_lines(TX *, long, long);
  40. _PR bool delete_section(TX *, POS *, POS *);
  41. _PR bool split_line(TX *, POS *);
  42. static bool join_lines(TX *, POS *);
  43. _PR bool pad_pos(TX *, POS *);
  44. _PR bool pad_cursor(VW *);
  45. _PR void order_pos(POS *, POS *);
  46. _PR bool check_section(TX *, POS *, POS *);
  47. _PR void check_pos(TX *, POS *);
  48. _PR bool check_line(TX *, POS *);
  49. _PR long section_length(TX *, POS *, POS *);
  50. _PR void copy_section(TX *, POS *, POS *, u_char *);
  51. _PR bool pos_in_block(VW *, long, long);
  52. _PR bool cursor_in_block(VW *);
  53. _PR bool page_in_block(VW *);
  54. _PR short line_in_block(VW *, long);
  55. _PR void order_block(VW *);
  56. _PR void set_block_refresh(VW *);
  57. _PR bool read_only(TX *);
  58.  
  59. #define ALLOC_LL(n)   mymalloc(sizeof(LINE) * (n))
  60. #define FREE_LL(l)    myfree(l)
  61.  
  62. /*
  63.  * This copies a line list (or part of one).
  64.  * d = destination
  65.  * s = source
  66.  * n = number of LINE's to copy.
  67.  */
  68. #define MOV_LL(d,s,n) memcpy((d), (s), (n) * sizeof(LINE))
  69. /*
  70.  * void
  71.  * MOV_LL(LINE *dst, LINE *src, long number)
  72.  * {
  73.  *     memcpy(dst, src, number * sizeof(LINE));
  74.  * }
  75.  */
  76.  
  77. /*
  78.  * Makes file empty (null string in first line)
  79.  */
  80. bool
  81. clear_line_list(TX *tx)
  82. {
  83.     if(tx->tx_Lines)
  84.     kill_line_list(tx);
  85.     tx->tx_Lines = ALLOC_LL(1);
  86.     if(tx->tx_Lines)
  87.     {
  88.     tx->tx_Lines[0].ln_Line = str_dupn("", 0);
  89.     if(tx->tx_Lines[0].ln_Line)
  90.         tx->tx_Lines[0].ln_Strlen = 1;
  91.     else
  92.         tx->tx_Lines[0].ln_Strlen = 0;
  93.     tx->tx_NumLines = 1;
  94.     return(TRUE);
  95.     }
  96.     return(FALSE);
  97. }
  98.  
  99. /*
  100.  * deallocates all lines and their list
  101.  */
  102. void
  103. kill_line_list(TX *tx)
  104. {
  105.     if(tx->tx_Lines)
  106.     {
  107.     LINE *line;
  108.     long i;
  109.     for(i = 0, line = tx->tx_Lines; i < tx->tx_NumLines; i++, line++)
  110.     {
  111.         if(line->ln_Strlen)
  112.         str_free(line->ln_Line);
  113.     }
  114.     FREE_LL(tx->tx_Lines);
  115.     tx->tx_Lines = NULL;
  116.     tx->tx_NumLines = 0;
  117.     }
  118. }
  119.  
  120. /*
  121.  * deallocates some lines (but not the list)
  122.  */
  123. static void
  124. kill_some_lines(TX *tx, long start, long number)
  125. {
  126.     LINE *line = tx->tx_Lines + start;
  127.     long i;
  128.     for(i = 0; i < number; i++, line++)
  129.     {
  130.     if(line->ln_Strlen)
  131.     {
  132.         str_free(line->ln_Line);
  133.         line->ln_Strlen = 0;
  134.         line->ln_Line = NULL;
  135.     }
  136.     }
  137. }
  138.  
  139. /*
  140.  * Creates blank entries or removes existing lines starting from line 'where'
  141.  * 'change' is the number of lines to insert, negative numbers mean delete
  142.  * that number of lines starting at the cursor line.
  143.  * If lines are deleted the actual text is also deleted.
  144.  * NOTE: You can't have a line list of zero lines.
  145.  */
  146. LINE *
  147. resize_line_list(TX *tx, long change, long where)
  148. {
  149.     LINE *newlines;
  150.     long newsize = tx->tx_NumLines + change;
  151.     if((newsize > 0) && (newlines = ALLOC_LL(newsize)))
  152.     {
  153.     if(tx->tx_Lines)
  154.     {
  155.         if(change > 0)
  156.         {
  157.         MOV_LL(newlines, tx->tx_Lines, where);
  158.         MOV_LL(newlines + where + change, tx->tx_Lines + where, tx->tx_NumLines - where);
  159.         }
  160.         else
  161.         {
  162.         MOV_LL(newlines, tx->tx_Lines, where);
  163.         MOV_LL(newlines + where, tx->tx_Lines + where - change, tx->tx_NumLines - where + change);
  164.         kill_some_lines(tx, where, -change);
  165.         }
  166.         FREE_LL(tx->tx_Lines);
  167.     }
  168.     if(change > 0)
  169.         bzero(newlines + where, change * sizeof(LINE));
  170.     tx->tx_Lines = newlines;
  171.     tx->tx_NumLines = newsize;
  172.     return(newlines);
  173.     }
  174.     return(FALSE);
  175. }
  176.  
  177. #if 0
  178. /*
  179.  * Pastes a line into the current view at line num.
  180.  * a LINE should have been made if it is wanted.
  181.  * text is not copied, it should have been mymalloc()'ed (or savestring()'ed)
  182.  */
  183. bool
  184. stuffline(TX *tx, u_char *text, long lineNum)
  185. {
  186.     LINE *line = tx->tx_Lines + lineNum;
  187.     if(line->ln_Strlen)
  188.     str_free(line->ln_Line);
  189.     line->ln_Strlen = strlen(text) + 1;
  190.     line->ln_Line = text;
  191.     return(TRUE);
  192. }
  193. #endif
  194.  
  195. /*
  196.  * Inserts some `space' at pos. The gap will be filled with random garbage.
  197.  * pos is *not* altered.
  198.  */
  199. bool
  200. insert_gap(TX *tx, long len, const POS *pos)
  201. {
  202.     LINE *line = tx->tx_Lines + pos->pos_Line;
  203.     u_char *newline = str_alloc(len + line->ln_Strlen);
  204.     if(newline)
  205.     {
  206.     if(line->ln_Strlen)
  207.     {
  208.         memcpy(newline, line->ln_Line, pos->pos_Col);
  209.         memcpy(newline + pos->pos_Col + len, line->ln_Line + pos->pos_Col,
  210.            line->ln_Strlen - pos->pos_Col);
  211.         str_free(line->ln_Line);
  212.     }
  213.     else
  214.         newline[len] = 0;
  215.     line->ln_Line = newline;
  216.     line->ln_Strlen += len;
  217.     adjust_marks_add_x(tx, len, pos->pos_Col, pos->pos_Line);
  218.     return(TRUE);
  219.     }
  220.     mem_error();
  221.     return(FALSE);
  222. }
  223.  
  224. /*
  225.  * Inserts a string into the current line at the cursor pos
  226.  *
  227.  * IMPORTANT: For any of the next functions which insert text the pos
  228.  * argument must not be one which will be fiddled with by the keeppos*()
  229.  * functions (specifically a direct pointer to vw_CursorPos).
  230.  */
  231. bool
  232. insert_str(TX *tx, const u_char *text, POS *pos)
  233. {
  234.     return(insert_str_n(tx, text, strlen(text), pos));
  235. }
  236.  
  237. bool
  238. insert_str_n(TX *tx, const u_char *text, long textLen, POS *pos)
  239. {
  240.     LINE *line = tx->tx_Lines + pos->pos_Line;
  241.     if(insert_gap(tx, textLen, pos))
  242.     {
  243.     memcpy(line->ln_Line + pos->pos_Col, text, textLen);
  244.     pos->pos_Col += textLen;
  245.     return(TRUE);
  246.     }
  247.     return(FALSE);
  248. }
  249.  
  250. /*
  251.  * Inserts a null teminated string, this routine acts on any '\n' characters
  252.  * that it finds. I expect that this routine will be incredibly slow.
  253.  */
  254. bool
  255. insert_string(TX *tx, const u_char *text, long textLen, POS *pos)
  256. {
  257.     const u_char *eol;
  258.     POS orig = *pos;
  259.     while((eol = memchr(text, '\n', textLen)))
  260.     {
  261.     long len = eol - text;
  262.     if(pos->pos_Col)
  263.     {
  264.         if(len > 0)
  265.         {
  266.         if(!insert_str_n(tx, text, len, pos))
  267.             goto abort;
  268.         }
  269.         if(!split_line(tx, pos))
  270.         goto abort;
  271.         pos->pos_Col = 0;
  272.     }
  273.     else
  274.     {
  275.         u_char *copy;
  276.         if(!resize_line_list(tx, +1, pos->pos_Line))
  277.         goto abort;
  278.         if(!(copy = str_dupn(text, len)))
  279.         goto abort;
  280.         tx->tx_Lines[pos->pos_Line].ln_Strlen = len + 1;
  281.         tx->tx_Lines[pos->pos_Line].ln_Line = copy;
  282.         adjust_marks_add_y(tx, +1, pos->pos_Line);
  283.     }
  284.     textLen -= len + 1;
  285.     text = eol + 1;
  286.     pos->pos_Line++;
  287.     }
  288.     if(textLen > 0)
  289.     {
  290.     if(!insert_str_n(tx, text, textLen, pos))
  291.     {
  292.     abort:
  293.         return(FALSE);
  294.     }
  295.     }
  296.     undo_record_insertion(tx, &orig, pos);
  297.     flag_insertion(tx, &orig, pos);
  298.     return(TRUE);
  299. }
  300.  
  301. /*
  302.  * Deletes some text (this line only)
  303.  */
  304. bool
  305. delete_chars(TX *tx, const POS *pos, long size)
  306. {
  307.     LINE *line = tx->tx_Lines + pos->pos_Line;
  308.     if(line->ln_Strlen)
  309.     {
  310.     u_char *newline;
  311.     if(size >= line->ln_Strlen)
  312.         size = line->ln_Strlen - 1;
  313.     newline = str_alloc(line->ln_Strlen - size);
  314.     if(newline)
  315.     {
  316. #if 1
  317.             memcpy(newline, line->ln_Line, pos->pos_Col);
  318.             memcpy(newline + pos->pos_Col, line->ln_Line + pos->pos_Col + size,
  319.                    line->ln_Strlen - pos->pos_Col - size);
  320. #else
  321.         strncpy(newline, line->ln_Line, pos->pos_Col);
  322.         strcpy(newline + pos->pos_Col, line->ln_Line + pos->pos_Col + size);
  323. #endif
  324.         str_free(line->ln_Line);
  325.         line->ln_Strlen -= size;
  326.         line->ln_Line = newline;
  327.         ad