home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / STVI369G.ZIP / MARK.C < prev    next >
C/C++ Source or Header  |  1990-05-01  |  2KB  |  122 lines

  1. /* $Header: /nw/tony/src/stevie/src/RCS/mark.c,v 1.3 89/03/11 22:42:39 tony Exp $
  2.  *
  3.  * Routines to save and retrieve marks.
  4.  */
  5.  
  6. #include "stevie.h"
  7.  
  8. #define    NMARKS    10        /* max. # of marks that can be saved */
  9.  
  10. struct    mark {
  11.     char    name;
  12.     LPTR    pos;
  13. };
  14.  
  15. static    struct    mark    mlist[NMARKS];
  16. static    struct    mark    pcmark;        /* previous context mark */
  17. static    bool_t    pcvalid = FALSE;    /* true if pcmark is valid */
  18.  
  19. /*
  20.  * setmark(c) - set mark 'c' at current cursor position
  21.  *
  22.  * Returns TRUE on success, FALSE if no room for mark or bad name given.
  23.  */
  24. bool_t
  25. setmark(c)
  26. register char    c;
  27. {
  28.     register int    i;
  29.  
  30.     if (!isalpha(c))
  31.         return FALSE;
  32.  
  33.     /*
  34.      * If there is already a mark of this name, then just use the
  35.      * existing mark entry.
  36.      */
  37.     for (i=0; i < NMARKS ;i++) {
  38.         if (mlist[i].name == c) {
  39.             mlist[i].pos = *Curschar;
  40.             return TRUE;
  41.         }
  42.     }
  43.  
  44.     /*
  45.      * There wasn't a mark of the given name, so find a free slot
  46.      */
  47.     for (i=0; i < NMARKS ;i++) {
  48.         if (mlist[i].name == NUL) {    /* got a free one */
  49.             mlist[i].name = c;
  50.             mlist[i].pos = *Curschar;
  51.             return TRUE;
  52.         }
  53.     }
  54.     return FALSE;
  55. }
  56.  
  57. /*
  58.  * setpcmark() - set the previous context mark to the current position
  59.  */
  60. void
  61. setpcmark()
  62. {
  63.     pcmark.pos = *Curschar;
  64.     pcvalid = TRUE;
  65. }
  66.  
  67. /*
  68.  * getmark(c) - find mark for char 'c'
  69.  *
  70.  * Return pointer to LPTR or NULL if no such mark.
  71.  */
  72. LPTR *
  73. getmark(c)
  74. register char    c;
  75. {
  76.     register int    i;
  77.  
  78.     if (c == '\'' || c == '`')    /* previous context mark */
  79.         return pcvalid ? &(pcmark.pos) : (LPTR *) NULL;
  80.  
  81.     for (i=0; i < NMARKS ;i++) {
  82.         if (mlist[i].name == c)
  83.             return &(mlist[i].pos);
  84.     }
  85.     return (LPTR *) NULL;
  86. }
  87.  
  88. /*
  89.  * clrall() - clear all marks
  90.  *
  91.  * Used mainly when trashing the entire buffer during ":e" type commands
  92.  */
  93. void
  94. clrall()
  95. {
  96.     register int    i;
  97.  
  98.     for (i=0; i < NMARKS ;i++)
  99.         mlist[i].name = NUL;
  100.     pcvalid = FALSE;
  101. }
  102.  
  103. /*
  104.  * clrmark(line) - clear any marks for 'line'
  105.  *
  106.  * Used any time a line is deleted so we don't have marks pointing to
  107.  * non-existent lines.
  108.  */
  109. void
  110. clrmark(line)
  111. register LINE    *line;
  112. {
  113.     register int    i;
  114.  
  115.     for (i=0; i < NMARKS ;i++) {
  116.         if (mlist[i].pos.linep == line)
  117.             mlist[i].name = NUL;
  118.     }
  119.     if (pcvalid && (pcmark.pos.linep == line))
  120.         pcvalid = FALSE;
  121. }
  122.