NeoMutt  2021-10-29-225-gb9986f
Teaching an old dog new tricks
DOXYGEN
color.c File Reference

Color and attribute parsing. More...

#include "config.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mutt/lib.h"
#include "config/lib.h"
#include "email/lib.h"
#include "core/lib.h"
#include "gui/lib.h"
#include "mutt.h"
#include "lib.h"
#include "pattern/lib.h"
#include "context.h"
#include "init.h"
#include "mutt_globals.h"
#include "options.h"
+ Include dependency graph for color.c:

Go to the source code of this file.

Data Structures

struct  ColorList
 A set of colors. More...
 

Functions

static void color_list_free (struct ColorList **ptr)
 Free the list of curses colours. More...
 
void mutt_color_free (uint32_t fg, uint32_t bg)
 Free a colour. More...
 
void colors_clear (void)
 Reset all the colours. More...
 
void mutt_colors_cleanup (void)
 Cleanup all the colours. More...
 
void mutt_colors_init (void)
 Initialize colours. More...
 
int mutt_color_alloc (uint32_t fg, uint32_t bg)
 Allocate a colour pair. More...
 
static int mutt_lookup_color (short pair, uint32_t *fg, uint32_t *bg)
 Get the colours from a colour pair. More...
 
int mutt_color_combine (uint32_t fg_attr, uint32_t bg_attr)
 Combine two colours. More...
 

Variables

struct ColorListUserColors
 Array of user colours. More...
 
int NumUserColors
 Number of user colours. More...
 
const struct Mapping ColorNames []
 

Detailed Description

Color and attribute parsing.

Authors
  • Michael R. Elkins
  • R Primus

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file color.c.

Function Documentation

◆ color_list_free()

static void color_list_free ( struct ColorList **  ptr)
static

Free the list of curses colours.

Parameters
ptrColours

Definition at line 85 of file color.c.

86 {
87  if (!ptr || !*ptr)
88  return;
89 
90  struct ColorList *cl = *ptr;
91  struct ColorList *next = NULL;
92 
93  while (cl)
94  {
95  next = cl->next;
96  FREE(&cl);
97  cl = next;
98  }
99  *ptr = NULL;
100 }
#define FREE(x)
Definition: memory.h:40
A set of colors.
Definition: color.c:56
struct ColorList * next
Linked list.
Definition: color.c:63
+ Here is the caller graph for this function:

◆ mutt_color_free()

void mutt_color_free ( uint32_t  fg,
uint32_t  bg 
)

Free a colour.

Parameters
fgForeground colour ID
bgBackground colour ID

If there are no more users, the resource will be freed.

Definition at line 109 of file color.c.

110 {
111  struct ColorList *q = NULL;
112 
113  struct ColorList *p = UserColors;
114  while (p)
115  {
116  if ((p->fg == fg) && (p->bg == bg))
117  {
118  (p->ref_count)--;
119  if (p->ref_count > 0)
120  return;
121 
122  NumUserColors--;
123  mutt_debug(LL_DEBUG1, "Color pairs used so far: %d\n", NumUserColors);
124 
125  if (p == UserColors)
126  {
128  FREE(&p);
129  return;
130  }
131  q = UserColors;
132  while (q)
133  {
134  if (q->next == p)
135  {
136  q->next = p->next;
137  FREE(&p);
138  return;
139  }
140  q = q->next;
141  }
142  /* can't get here */
143  }
144  p = p->next;
145  }
146 }
struct ColorList * UserColors
Array of user colours.
Definition: color.c:49
int NumUserColors
Number of user colours.
Definition: color.c:50
#define mutt_debug(LEVEL,...)
Definition: logging.h:84
@ LL_DEBUG1
Log at debug level 1.
Definition: logging.h:40
uint32_t fg
Foreground colour.
Definition: color.c:59
uint32_t bg
Background colour.
Definition: color.c:60
short ref_count
Number of users.
Definition: color.c:62
+ Here is the caller graph for this function:

◆ colors_clear()

void colors_clear ( void  )

Reset all the colours.

Definition at line 151 of file color.c.

152 {
156 
158 }
void regex_colors_clear(void)
Clear the Regex colours.
Definition: regex.c:112
void simple_colors_clear(void)
Reset the simple colour definitions.
Definition: simple.c:58
static void color_list_free(struct ColorList **ptr)
Free the list of curses colours.
Definition: color.c:85
void quoted_colors_clear(void)
Reset the quoted-email colours.
Definition: quoted.c:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_colors_cleanup()

void mutt_colors_cleanup ( void  )

Cleanup all the colours.

Definition at line 163 of file color.c.

164 {
165  colors_clear();
167 }
void color_notify_free(void)
Free the Colour notification.
Definition: notify.c:47
void colors_clear(void)
Reset all the colours.
Definition: color.c:151
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_colors_init()

void mutt_colors_init ( void  )

Initialize colours.

Definition at line 172 of file color.c.

173 {
178 
179  start_color();
180 }
void color_notify_init(void)
Initialise the Colour notification.
Definition: notify.c:38
void regex_colors_init(void)
Initialise the Regex colours.
Definition: regex.c:96
void simple_colors_init(void)
Initialise the simple colour definitions.
Definition: simple.c:41
void quoted_colors_init(void)
Initialise the quoted-email colours.
Definition: quoted.c:53
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_color_alloc()

int mutt_color_alloc ( uint32_t  fg,
uint32_t  bg 
)

Allocate a colour pair.

Parameters
fgForeground colour ID
bgBackground colour ID
Return values
numCombined colour pair

Definition at line 188 of file color.c.

189 {
190  struct ColorList *p = UserColors;
191 
192  /* check to see if this color is already allocated to save space */
193  while (p)
194  {
195  if ((p->fg == fg) && (p->bg == bg))
196  {
197  (p->ref_count)++;
198  return COLOR_PAIR(p->index);
199  }
200  p = p->next;
201  }
202 
203  /* check to see if there are colors left */
204  if (++NumUserColors > COLOR_PAIRS)
205  return A_NORMAL;
206 
207  /* find the smallest available index (object) */
208  int i = 1;
209  while (true)
210  {
211  p = UserColors;
212  while (p)
213  {
214  if (p->index == i)
215  break;
216  p = p->next;
217  }
218  if (!p)
219  break;
220  i++;
221  }
222 
223  /* Check for pair overflow too.
224  * We are currently using init_pair(), which only accepts size short. */
225  if (i > SHRT_MAX)
226  return (0);
227 
228  p = mutt_mem_malloc(sizeof(struct ColorList));
229  p->next = UserColors;
230  UserColors = p;
231 
232  p->index = i;
233  p->ref_count = 1;
234  p->bg = bg;
235  p->fg = fg;
236 
237  if (fg == COLOR_DEFAULT)
238  fg = COLOR_UNSET;
239  if (bg == COLOR_DEFAULT)
240  bg = COLOR_UNSET;
241  init_pair(i, fg, bg);
242 
243  mutt_debug(LL_DEBUG3, "Color pairs used so far: %d\n", NumUserColors);
244 
245  return COLOR_PAIR(p->index);
246 }
#define COLOR_DEFAULT
Definition: color.h:103
#define COLOR_UNSET
Definition: color.h:104
@ LL_DEBUG3
Log at debug level 3.
Definition: logging.h:42
void * mutt_mem_malloc(size_t size)
Allocate memory on the heap.
Definition: memory.c:90
short index
Index number.
Definition: color.c:61
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ mutt_lookup_color()

static int mutt_lookup_color ( short  pair,
uint32_t *  fg,
uint32_t *  bg 
)
static

Get the colours from a colour pair.

Parameters
[in]pairColour pair
[out]fgForeground colour (OPTIONAL)
[out]bgBackground colour (OPTIONAL)
Return values
0Success
-1Error

Definition at line 256 of file color.c.

257 {
258  struct ColorList *p = UserColors;
259 
260  while (p)
261  {
262  if (COLOR_PAIR(p->index) == pair)
263  {
264  if (fg)
265  *fg = p->fg;
266  if (bg)
267  *bg = p->bg;
268  return 0;
269  }
270  p = p->next;
271  }
272  return -1;
273 }
+ Here is the caller graph for this function:

◆ mutt_color_combine()

int mutt_color_combine ( uint32_t  fg_attr,
uint32_t  bg_attr 
)

Combine two colours.

Parameters
fg_attrColour pair of foreground to use
bg_attrColour pair of background to use
Return values
numColour pair of combined colour

Definition at line 281 of file color.c.

282 {
283  uint32_t fg = COLOR_DEFAULT;
284  uint32_t bg = COLOR_DEFAULT;
285 
286  mutt_lookup_color(fg_attr, &fg, NULL);
287  mutt_lookup_color(bg_attr, NULL, &bg);
288 
289  if ((fg == COLOR_DEFAULT) && (bg == COLOR_DEFAULT))
290  return A_NORMAL;
291  return mutt_color_alloc(fg, bg);
292 }
static int mutt_lookup_color(short pair, uint32_t *fg, uint32_t *bg)
Get the colours from a colour pair.
Definition: color.c:256
int mutt_color_alloc(uint32_t fg, uint32_t bg)
Allocate a colour pair.
Definition: color.c:188
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Variable Documentation

◆ UserColors

struct ColorList* UserColors

Array of user colours.

Definition at line 49 of file color.c.

◆ NumUserColors

int NumUserColors

Number of user colours.

Definition at line 50 of file color.c.

◆ ColorNames

const struct Mapping ColorNames[]
Initial value:
= {
{ "black", COLOR_BLACK },
{ "blue", COLOR_BLUE },
{ "cyan", COLOR_CYAN },
{ "green", COLOR_GREEN },
{ "magenta", COLOR_MAGENTA },
{ "red", COLOR_RED },
{ "white", COLOR_WHITE },
{ "yellow", COLOR_YELLOW },
{ "default", COLOR_DEFAULT },
{ 0, 0 },
}

Definition at line 50 of file color.c.