Skip to content

Commit f59c6e8

Browse files
committed
patch 8.0.1685: can't set ANSI colors of a terminal window
Problem: Can't set ANSI colors of a terminal window. Solution: Add term_setansicolors(), term_getansicolors() and g:term_ansi_colors. (Andy Massimino, closes #2747)
1 parent 07b46af commit f59c6e8

9 files changed

Lines changed: 321 additions & 6 deletions

File tree

runtime/doc/eval.txt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,6 +2423,7 @@ term_dumpload({filename} [, {options}])
24232423
term_dumpwrite({buf}, {filename} [, {options}])
24242424
none dump terminal window contents
24252425
term_getaltscreen({buf}) Number get the alternate screen flag
2426+
term_getansicolors({buf}) List get ANSI palette in GUI color mode
24262427
term_getattr({attr}, {what}) Number get the value of attribute {what}
24272428
term_getcursor({buf}) List get the cursor position of a terminal
24282429
term_getjob({buf}) Job get the job associated with a terminal
@@ -2435,6 +2436,8 @@ term_gettty({buf}, [{input}]) String get the tty name of a terminal
24352436
term_list() List get the list of terminal buffers
24362437
term_scrape({buf}, {row}) List get row of a terminal screen
24372438
term_sendkeys({buf}, {keys}) none send keystrokes to a terminal
2439+
term_setansicolors({buf}, {colors})
2440+
none set ANSI palette in GUI color mode
24382441
term_setkill({buf}, {how}) none set signal to stop job in terminal
24392442
term_setrestore({buf}, {command}) none set command to restore terminal
24402443
term_start({cmd}, {options}) Job open a terminal window and run a job
@@ -8248,6 +8251,18 @@ term_getaltscreen({buf}) *term_getaltscreen()*
82488251
{buf} is used as with |term_getsize()|.
82498252
{only available when compiled with the |+terminal| feature}
82508253

8254+
term_getansicolors({buf}) *term_getansicolors()*
8255+
Get the ANSI color palette in use by terminal {buf}.
8256+
Returns a List of length 16 where each element is a String
8257+
representing a color in hexadecimal "#rrggbb" format.
8258+
Also see |term_setansicolors()| and |g:terminal_ansi_colors|.
8259+
If neither was used returns the default colors.
8260+
8261+
{buf} is used as with |term_getsize()|. If the buffer does not
8262+
exist or is not a terminal window, an empty list is returned.
8263+
{only available when compiled with the |+terminal| feature and
8264+
with GUI enabled and/or the |+termguicolors| feature}
8265+
82518266
term_getattr({attr}, {what}) *term_getattr()*
82528267
Given {attr}, a value returned by term_scrape() in the "attr"
82538268
item, return whether {what} is on. {what} can be one of:
@@ -8379,6 +8394,19 @@ term_sendkeys({buf}, {keys}) *term_sendkeys()*
83798394
means the character CTRL-X.
83808395
{only available when compiled with the |+terminal| feature}
83818396

8397+
term_setansicolors({buf}, {colors}) *term_setansicolors()*
8398+
Set the ANSI color palette used by terminal {buf}.
8399+
{colors} must be a List of 16 valid color names or hexadecimal
8400+
color codes, like those accepted by |highlight-guifg|.
8401+
Also see |term_getansicolors()| and |g:terminal_ansi_colors|.
8402+
8403+
These colors are used in the GUI and in the terminal when
8404+
'termguicolors' is set. When not using GUI colors (GUI mode
8405+
or |termguicolors|), the terminal window always uses the 16
8406+
ANSI colors of the underlying terminal.
8407+
{only available when compiled with the |+terminal| feature and
8408+
with GUI enabled and/or the |+termguicolors| feature}
8409+
83828410
term_setkill({buf}, {how}) *term_setkill()*
83838411
When exiting Vim or trying to close the terminal window in
83848412
another way, {how} defines whether the job in the terminal can
@@ -8463,6 +8491,9 @@ term_start({cmd}, {options}) *term_start()*
84638491
CTRL-D is used on MS-Windows. For Python
84648492
use CTRL-Z or "exit()". For a shell use
84658493
"exit". A CR is always added.
8494+
"ansi_colors" A list of 16 color names or hex codes
8495+
defining the ANSI palette used in GUI
8496+
color modes. See |g:terminal_ansi_colors|.
84668497

84678498
{only available when compiled with the |+terminal| feature}
84688499

runtime/doc/terminal.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,16 @@ terminal window will start with a white or black background.
136136
To use a different color the Terminal highlight group can be used, for
137137
example: >
138138
hi Terminal ctermbg=lightgrey ctermfg=blue guibg=lightgrey guifg=blue
139+
<
140+
*g:terminal_ansi_colors*
141+
In GUI mode or with |termguicolors|, the 16 ANSI colors used by default in new
142+
terminal windows may be configured using the variable
143+
`g:terminal_ansi_colors`, which should be a list of 16 color names or
144+
hexadecimal color codes, similar to those accepted by |highlight-guifg|. When
145+
not using GUI colors, the terminal window always uses the 16 ANSI colors of
146+
the underlying terminal.
147+
The |term_setansicolors()| function can be used to change the colors, and
148+
|term_getansicolors()| to get the currently used colors.
139149

140150

141151
Syntax ~

src/channel.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4802,6 +4802,50 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
48024802
opt->jo_set2 |= JO2_TERM_KILL;
48034803
opt->jo_term_kill = get_tv_string_chk(item);
48044804
}
4805+
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
4806+
else if (STRCMP(hi->hi_key, "ansi_colors") == 0)
4807+
{
4808+
int n = 0;
4809+
listitem_T *li;
4810+
long_u rgb[16];
4811+
4812+
if (!(supported2 & JO2_ANSI_COLORS))
4813+
break;
4814+
4815+
if (item == NULL || item->v_type != VAR_LIST
4816+
|| item->vval.v_list == NULL)
4817+
{
4818+
EMSG2(_(e_invargval), "ansi_colors");
4819+
return FAIL;
4820+
}
4821+
4822+
li = item->vval.v_list->lv_first;
4823+
for (; li != NULL && n < 16; li = li->li_next, n++)
4824+
{
4825+
char_u *color_name;
4826+
guicolor_T guicolor;
4827+
4828+
color_name = get_tv_string_chk(&li->li_tv);
4829+
if (color_name == NULL)
4830+
return FAIL;
4831+
4832+
guicolor = GUI_GET_COLOR(color_name);
4833+
if (guicolor == INVALCOLOR)
4834+
return FAIL;
4835+
4836+
rgb[n] = GUI_MCH_GET_RGB(guicolor);
4837+
}
4838+
4839+
if (n != 16 || li != NULL)
4840+
{
4841+
EMSG2(_(e_invargval), "ansi_colors");
4842+
return FAIL;
4843+
}
4844+
4845+
opt->jo_set2 |= JO2_ANSI_COLORS;
4846+
memcpy(opt->jo_ansi_colors, rgb, sizeof(rgb));
4847+
}
4848+
# endif
48054849
#endif
48064850
else if (STRCMP(hi->hi_key, "env") == 0)
48074851
{

src/evalfunc.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,9 @@ static struct fst
856856
{"term_dumpload", 1, 2, f_term_dumpload},
857857
{"term_dumpwrite", 2, 3, f_term_dumpwrite},
858858
{"term_getaltscreen", 1, 1, f_term_getaltscreen},
859+
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
860+
{"term_getansicolors", 1, 1, f_term_getansicolors},
861+
# endif
859862
{"term_getattr", 2, 2, f_term_getattr},
860863
{"term_getcursor", 1, 1, f_term_getcursor},
861864
{"term_getjob", 1, 1, f_term_getjob},
@@ -868,6 +871,9 @@ static struct fst
868871
{"term_list", 0, 0, f_term_list},
869872
{"term_scrape", 2, 2, f_term_scrape},
870873
{"term_sendkeys", 2, 2, f_term_sendkeys},
874+
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
875+
{"term_setansicolors", 2, 2, f_term_setansicolors},
876+
# endif
871877
{"term_setkill", 2, 2, f_term_setkill},
872878
{"term_setrestore", 2, 2, f_term_setrestore},
873879
{"term_start", 1, 2, f_term_start},

src/proto/terminal.pro

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ int term_swap_diff(void);
3232
void f_term_dumpdiff(typval_T *argvars, typval_T *rettv);
3333
void f_term_dumpload(typval_T *argvars, typval_T *rettv);
3434
void f_term_getaltscreen(typval_T *argvars, typval_T *rettv);
35+
void f_term_getansicolors(typval_T *argvars, typval_T *rettv);
3536
void f_term_getattr(typval_T *argvars, typval_T *rettv);
3637
void f_term_getcursor(typval_T *argvars, typval_T *rettv);
3738
void f_term_getjob(typval_T *argvars, typval_T *rettv);
@@ -44,6 +45,7 @@ void f_term_gettty(typval_T *argvars, typval_T *rettv);
4445
void f_term_list(typval_T *argvars, typval_T *rettv);
4546
void f_term_scrape(typval_T *argvars, typval_T *rettv);
4647
void f_term_sendkeys(typval_T *argvars, typval_T *rettv);
48+
void f_term_setansicolors(typval_T *argvars, typval_T *rettv);
4749
void f_term_setrestore(typval_T *argvars, typval_T *rettv);
4850
void f_term_setkill(typval_T *argvars, typval_T *rettv);
4951
void f_term_start(typval_T *argvars, typval_T *rettv);

src/structs.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1708,7 +1708,7 @@ struct channel_S {
17081708
#define JO2_EOF_CHARS 0x1000 /* "eof_chars" */
17091709
#define JO2_NORESTORE 0x2000 /* "norestore" */
17101710
#define JO2_TERM_KILL 0x4000 /* "term_kill" */
1711-
#define JO2_ALL 0x7FFF
1711+
#define JO2_ANSI_COLORS 0x8000 /* "ansi_colors" */
17121712

17131713
#define JO_MODE_ALL (JO_MODE + JO_IN_MODE + JO_OUT_MODE + JO_ERR_MODE)
17141714
#define JO_CB_ALL \
@@ -1777,6 +1777,9 @@ typedef struct
17771777
int jo_term_finish;
17781778
char_u *jo_eof_chars;
17791779
char_u *jo_term_kill;
1780+
# if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
1781+
long_u jo_ansi_colors[16];
1782+
# endif
17801783
#endif
17811784
} jobopt_T;
17821785

0 commit comments

Comments
 (0)