view examples/dsl/edit.h @ 22:5581ef01f993

Regen all the AG output. This also fixes the line number output in the tools (cgbigen/helpgen/help2html), apparently because those files weren't regenerated last go.
author David A. Holland
date Mon, 13 Jun 2022 00:06:39 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*****

 AnaGram Programming Examples

 A Dos Script Language
 Line Edit Class
 The Edit Class is completely defined in this file.
 There is no supporting CPP file.

 Copyright 1993 Parsifal Software. All Rights Reserved.

 This software is provided 'as-is', without any express or implied
 warranty.  In no event will the authors be held liable for any damages
 arising from the use of this software.

 Permission is granted to anyone to use this software for any purpose,
 including commercial applications, and to alter it and redistribute it
 freely, subject to the following restrictions:

 1. The origin of this software must not be misrepresented; you must not
    claim that you wrote the original software. If you use this software
    in a product, an acknowledgment in the product documentation would be
    appreciated but is not required.
 2. Altered source versions must be plainly marked as such, and must not be
    misrepresented as being the original software.
 3. This notice may not be removed or altered from any source distribution.

*****/


#ifndef EDIT_H
#define EDIT_H

#include <string.h>
#include "charsink.h"
#include "util.h"
#include "screen.h"

class line_edit_buffer {
  char *text;                               // text of line being edited
  int x;                                    // position of cursor in line
  int length;                               // length of line
  int buf_size;                             // does not include final zero
public:
  int changes;                              // if non-zero, line changed


// Constructors

  line_edit_buffer(int n) {                 // Init with no initial string
    buf_size = n;
    text = new char[buf_size + 1];
    memset(text,0, buf_size + 1);
    x = length = changes = 0;
  }

  line_edit_buffer(char *s, int n) {        // Init with initial string
    if (s != NULL) {
      length = strlen(s);
      if (length > n) n = length;           // Make buf at least as long
    }
    else length = 0;
    buf_size = n;
    text = new char[buf_size + 1];
    memset(text,0, buf_size + 1);
    if (s != NULL) strcpy(text, s);
    x = changes = 0;
  }


// Destructor

  ~line_edit_buffer() {
    delete [] text;
  }


// Insert char in buffer

  line_edit_buffer &operator << (const int c) {
    if (length >= buf_size) return *this;
    if (length < x) {
      memset(&text[length], ' ', x - length);
      length = x;
      text[length] = 0;
    }
    memmove(&text[x+1], &text[x], length++ - x);
    text[x++] = (char )c;
    changes++;
    return *this;
  }


// Increment cursor position

  line_edit_buffer &operator ++() {
    if (x < buf_size) x++;
    return *this;
  }


// Move to end of line

  friend line_edit_buffer &end(line_edit_buffer &b) {
    b.x = b.length;
    return b;
  }
  line_edit_buffer &operator --() {
    if (x) x--;
    return *this;
  }

// Move to beginning of line

  friend line_edit_buffer &home(line_edit_buffer &b) {
    b.x = 0;
    return b;
  }

// Delete a character

  line_edit_buffer &operator ~() {
    if (x >= length) return *this;
    memmove(&text[x], &text[x+1], --length - x);
    text[length] = 0;
    changes++;
    return *this;
  }

  operator char *() {
    return text;
  }

  int index() {                       // return index in line
    return x;
  }


// Display edit buffer in a a screen rectangle

  friend screen_rect &operator << ( screen_rect &r
                                  , const line_edit_buffer &b
                                  ) {
    int offset = b.x - r.size.x + 1;
    if (offset < 0) offset = 0;

    r.set_cursor(b.x - offset) << (b.text + offset);
    return r;
  }
};

#endif