aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarc André Tanner <mat@brain-dump.org>2014-09-10 12:16:26 +0200
committerMarc André Tanner <mat@brain-dump.org>2014-09-10 12:16:26 +0200
commite46158f31369b8b16ed9ea3885b9def08afc4a55 (patch)
treec4d3583b7d503212cbe06d4f9b54f1fad0fedea4
parent18aa993466ab857b2a56612dabd243992e987998 (diff)
downloadvis-e46158f31369b8b16ed9ea3885b9def08afc4a55.tar.gz
vis-e46158f31369b8b16ed9ea3885b9def08afc4a55.tar.xz
Simplify cursor color management
-rw-r--r--Makefile2
-rw-r--r--colors.c106
-rw-r--r--editor.c72
-rw-r--r--editor.h6
4 files changed, 72 insertions, 114 deletions
diff --git a/Makefile b/Makefile
index 601bb94..92c7a81 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@ include config.mk
SRC = editor.c window.c text.c text-motions.c text-objects.c register.c
HDR := ${SRC:.c=.h} syntax.h util.h config.def.h
-SRC += vis.c colors.c
+SRC += vis.c
OBJ = ${SRC:.c=.o}
ALL = ${SRC} ${HDR} config.mk Makefile LICENSE README vis.1
diff --git a/colors.c b/colors.c
deleted file mode 100644
index d1a0357..0000000
--- a/colors.c
+++ /dev/null
@@ -1,106 +0,0 @@
-#include <stdbool.h>
-#include <stdlib.h>
-#include <curses.h>
-
-#include "editor.h"
-#include "util.h"
-
-#ifdef NCURSES_VERSION
-# ifndef NCURSES_EXT_COLORS
-# define NCURSES_EXT_COLORS 0
-# endif
-# if !NCURSES_EXT_COLORS
-# define MAX_COLOR_PAIRS 256
-# endif
-#endif
-#ifndef MAX_COLOR_PAIRS
-# define MAX_COLOR_PAIRS COLOR_PAIRS
-#endif
-
-static bool has_default_colors;
-static short *color2palette, default_fg, default_bg;
-static short color_pairs_reserved, color_pairs_max, color_pair_current;
-
-static unsigned int color_hash(short fg, short bg)
-{
- if (fg == -1)
- fg = COLORS;
- if (bg == -1)
- bg = COLORS + 1;
- return fg * (COLORS + 2) + bg;
-}
-
-short editor_color_get(short fg, short bg)
-{
- if (fg >= COLORS)
- fg = default_fg;
- if (bg >= COLORS)
- bg = default_bg;
-
- if (!has_default_colors) {
- if (fg == -1)
- fg = default_fg;
- if (bg == -1)
- bg = default_bg;
- }
-
- if (!color2palette || (fg == -1 && bg == -1))
- return 0;
- unsigned int index = color_hash(fg, bg);
- if (color2palette[index] == 0) {
- short oldfg, oldbg;
- for (;;) {
- if (++color_pair_current >= color_pairs_max)
- color_pair_current = color_pairs_reserved + 1;
- pair_content(color_pair_current, &oldfg, &oldbg);
- unsigned int old_index = color_hash(oldfg, oldbg);
- if (color2palette[old_index] >= 0) {
- if (init_pair(color_pair_current, fg, bg) == OK) {
- color2palette[old_index] = 0;
- color2palette[index] = color_pair_current;
- }
- break;
- }
- }
- }
-
- short color_pair = color2palette[index];
- return color_pair >= 0 ? color_pair : -color_pair;
-}
-
-short editor_color_reserve(short fg, short bg)
-{
- if (!color2palette)
- editor_init();
- if (!color2palette || fg >= COLORS || bg >= COLORS)
- return 0;
- if (!has_default_colors && fg == -1)
- fg = default_fg;
- if (!has_default_colors && bg == -1)
- bg = default_bg;
- if (fg == -1 && bg == -1)
- return 0;
- unsigned int index = color_hash(fg, bg);
- if (color2palette[index] >= 0) {
- if (init_pair(++color_pairs_reserved, fg, bg) == OK)
- color2palette[index] = -color_pairs_reserved;
- }
- short color_pair = color2palette[index];
- return color_pair >= 0 ? color_pair : -color_pair;
-}
-
-void editor_init(void)
-{
- if (color2palette)
- return;
- pair_content(0, &default_fg, &default_bg);
- if (default_fg == -1)
- default_fg = COLOR_WHITE;
- if (default_bg == -1)
- default_bg = COLOR_BLACK;
- has_default_colors = (use_default_colors() == OK);
- color_pairs_max = MIN(COLOR_PAIRS, MAX_COLOR_PAIRS);
- if (COLORS)
- color2palette = calloc((COLORS + 2) * (COLORS + 2), sizeof(short));
- editor_color_reserve(COLOR_WHITE, COLOR_BLACK);
-}
diff --git a/editor.c b/editor.c
index e497f77..1371812 100644
--- a/editor.c
+++ b/editor.c
@@ -4,6 +4,18 @@
#include "editor.h"
#include "util.h"
+#ifdef NCURSES_VERSION
+# ifndef NCURSES_EXT_COLORS
+# define NCURSES_EXT_COLORS 0
+# endif
+# if !NCURSES_EXT_COLORS
+# define MAX_COLOR_PAIRS 256
+# endif
+#endif
+#ifndef MAX_COLOR_PAIRS
+# define MAX_COLOR_PAIRS COLOR_PAIRS
+#endif
+
static EditorWin *editor_window_new_text(Editor *ed, Text *text);
static void editor_window_free(Editor *ed, EditorWin *win);
static void editor_window_split_internal(Editor *ed, const char *filename);
@@ -175,7 +187,7 @@ bool editor_syntax_load(Editor *ed, Syntax *syntaxes, Color *colors) {
if (rule->color.attr == 0)
rule->color.attr = A_NORMAL;
if (rule->color.fg != 0)
- rule->color.attr |= COLOR_PAIR(editor_color_reserve(rule->color.fg, rule->color.bg));
+ rule->color.attr |= COLOR_PAIR(editor_color_get(rule->color.fg, rule->color.bg));
if (regcomp(&rule->regex, rule->rule, REG_EXTENDED|rule->cflags))
success = false;
}
@@ -481,3 +493,61 @@ char *editor_prompt_get(Editor *ed) {
buf[len] = '\0';
return buf;
}
+
+static unsigned int color_hash(short fg, short bg)
+{
+ if (fg == -1)
+ fg = COLORS;
+ if (bg == -1)
+ bg = COLORS + 1;
+ return fg * (COLORS + 2) + bg;
+}
+
+short editor_color_get(short fg, short bg)
+{
+ static bool has_default_colors;
+ static short *color2palette, default_fg, default_bg;
+ static short color_pairs_max, color_pair_current;
+
+ if (!color2palette) {
+ pair_content(0, &default_fg, &default_bg);
+ if (default_fg == -1)
+ default_fg = COLOR_WHITE;
+ if (default_bg == -1)
+ default_bg = COLOR_BLACK;
+ has_default_colors = (use_default_colors() == OK);
+ color_pairs_max = MIN(COLOR_PAIRS, MAX_COLOR_PAIRS);
+ if (COLORS)
+ color2palette = calloc((COLORS + 2) * (COLORS + 2), sizeof(short));
+ }
+
+ if (fg >= COLORS)
+ fg = default_fg;
+ if (bg >= COLORS)
+ bg = default_bg;
+
+ if (!has_default_colors) {
+ if (fg == -1)
+ fg = default_fg;
+ if (bg == -1)
+ bg = default_bg;
+ }
+
+ if (!color2palette || (fg == -1 && bg == -1))
+ return 0;
+
+ unsigned int index = color_hash(fg, bg);
+ if (color2palette[index] == 0) {
+ short oldfg, oldbg;
+ if (++color_pair_current >= color_pairs_max)
+ color_pair_current = 1;
+ pair_content(color_pair_current, &oldfg, &oldbg);
+ unsigned int old_index = color_hash(oldfg, oldbg);
+ if (init_pair(color_pair_current, fg, bg) == OK) {
+ color2palette[old_index] = 0;
+ color2palette[index] = color_pair_current;
+ }
+ }
+
+ return color2palette[index];
+}
diff --git a/editor.h b/editor.h
index 109ea32..388f8d5 100644
--- a/editor.h
+++ b/editor.h
@@ -149,14 +149,8 @@ void editor_prompt_set(Editor *vis, const char *line);
void editor_prompt_show(Editor *vis, const char *title);
void editor_prompt_hide(Editor *vis);
-
void editor_statusbar_set(Editor*, editor_statusbar_t);
-// TODO cleanup this mess only 1 function should suffice? move perform
-// lazy initialization on first call
-/* library initialization code, should be run at startup */
-void editor_init(void);
-short editor_color_reserve(short fg, short bg);
/* look up a curses color pair for the given combination of fore and
* background color */
short editor_color_get(short fg, short bg);