path: root/common.h
blob: 7d7b165d10896cf5ebbd398f8ef293ead757e9fe (plain) (tree)

















/* Copyright (c) 2021 nytpu <alex@nytpu.com>
 * SPDX-License-Identifier: GPL-3.0-only
 * For more license details, see LICENSE or <https://www.gnu.org/licenses/gpl-3.0.html>.

#pragma once
#ifndef _COMMON_H
#define _COMMON_H

#include <stdbool.h>
#include <stddef.h>

// ensure that regex.h and other headers follow POSIX 2008/2017
#define _POSIX_C_SOURCE 200809L
// XXX: possibly stop mandating C11/C17.  AFAIK the only C11 features I'm using
//      are anonymous structs/unions and noreturn, which seem to be supported
//      in GCC's & Clang's extensions to C99?
#define _ISOC11_SOURCE

// should be set to proper number by preprocessor
#ifndef VERSION
#define VERSION " Unknown"
#endif // VERSION

// default to 80 columns
#define DEFAULT_WIDTH 80

typedef long RowNum;

struct Row {
	RowNum idx;    // Row number (zero-based)
	size_t size;   // Size of row (excluding trailing \0)
	char *chars;   // Row content (no trailing \n) -- MUST FREE

// doubles as an editor state/editor config struct as well
struct Editor {
	/* actual buffer stuff */
	char *filename;   // -- MUST FREE
	RowNum numrows;
	struct Row *rows; // -- MUST FREE

	/* command line options */
	bool prompt_enabled;
	char *prompt; // don't free - usually pointer to argv
	bool quiet;
	bool verbose;
	bool loose;

	/* misc state information */
	bool dirty;           // has the buffer been modified since last write?
	int scols;            // number of columns we can display on screen
	RowNum crow;          // current row address - ONE INDEXED
	const char *last_err; // verbose error message for previous error

extern struct Editor E;

// print the current perror then exit failure
void die(const char *m);

// initialize global struct Buffer to null/zero values
void editor_init(void);
void editor_deinit(void);

// print the standard "?\n", and also print a given msg if verbosity is enabled
// the msg will be saved for a future "h" command.
void error(const char *msg);

#endif // _COMMON_H