3 Copyright (C) 2018-2019 Free Software Foundation, Inc.
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21 #include "cli/cli-cmds.h"
22 #include "cli/cli-style.h"
23 #include "source-cache.h"
24 #include "observable.h"
26 /* True if styling is enabled. */
28 #if defined (__MSDOS__) || defined (__CYGWIN__)
34 /* True if source styling is enabled. Note that this is only
35 consulted when cli_styling is true. */
37 int source_styling
= 1;
39 /* Name of colors; must correspond to ui_file_style::basic_color. */
40 static const char * const cli_colors
[] = {
53 /* Names of intensities; must correspond to
54 ui_file_style::intensity. */
55 static const char * const cli_intensities
[] = {
62 /* See cli-style.h. */
64 cli_style_option
file_name_style (ui_file_style::GREEN
);
66 /* See cli-style.h. */
68 cli_style_option
function_name_style (ui_file_style::YELLOW
);
70 /* See cli-style.h. */
72 cli_style_option
variable_name_style (ui_file_style::CYAN
);
74 /* See cli-style.h. */
76 cli_style_option
address_style (ui_file_style::BLUE
);
78 /* See cli-style.h. */
80 cli_style_option::cli_style_option (ui_file_style::basic_color fg
)
81 : m_foreground (cli_colors
[fg
- ui_file_style::NONE
]),
82 m_background (cli_colors
[0]),
83 m_intensity (cli_intensities
[ui_file_style::NORMAL
])
87 /* Return the color number corresponding to COLOR. */
90 color_number (const char *color
)
92 for (int i
= 0; i
< ARRAY_SIZE (cli_colors
); ++i
)
94 if (color
== cli_colors
[i
])
97 gdb_assert_not_reached ("color not found");
100 /* See cli-style.h. */
103 cli_style_option::style () const
105 int fg
= color_number (m_foreground
);
106 int bg
= color_number (m_background
);
107 ui_file_style::intensity intensity
= ui_file_style::NORMAL
;
109 for (int i
= 0; i
< ARRAY_SIZE (cli_intensities
); ++i
)
111 if (m_intensity
== cli_intensities
[i
])
113 intensity
= (ui_file_style::intensity
) i
;
118 return ui_file_style (fg
, bg
, intensity
);
121 /* See cli-style.h. */
124 cli_style_option::do_show_foreground (struct ui_file
*file
, int from_tty
,
125 struct cmd_list_element
*cmd
,
128 const char *name
= (const char *) get_cmd_context (cmd
);
129 fprintf_filtered (file
, _("The \"%s\" foreground color is: %s\n"),
133 /* See cli-style.h. */
136 cli_style_option::do_show_background (struct ui_file
*file
, int from_tty
,
137 struct cmd_list_element
*cmd
,
140 const char *name
= (const char *) get_cmd_context (cmd
);
141 fprintf_filtered (file
, _("The \"%s\" background color is: %s\n"),
145 /* See cli-style.h. */
148 cli_style_option::do_show_intensity (struct ui_file
*file
, int from_tty
,
149 struct cmd_list_element
*cmd
,
152 const char *name
= (const char *) get_cmd_context (cmd
);
153 fprintf_filtered (file
, _("The \"%s\" display intensity is: %s\n"),
157 /* See cli-style.h. */
160 cli_style_option::add_setshow_commands (const char *name
,
161 enum command_class theclass
,
162 const char *prefix_doc
,
163 struct cmd_list_element
**set_list
,
164 void (*do_set
) (const char *args
,
166 struct cmd_list_element
**show_list
,
167 void (*do_show
) (const char *args
,
170 m_set_prefix
= std::string ("set style ") + name
+ " ";
171 m_show_prefix
= std::string ("show style ") + name
+ " ";
173 add_prefix_cmd (name
, no_class
, do_set
, prefix_doc
, &m_set_list
,
174 m_set_prefix
.c_str (), 0, set_list
);
175 add_prefix_cmd (name
, no_class
, do_show
, prefix_doc
, &m_show_list
,
176 m_show_prefix
.c_str (), 0, show_list
);
178 add_setshow_enum_cmd ("foreground", theclass
, cli_colors
,
180 _("Set the foreground color for this property"),
181 _("Show the foreground color for this property"),
185 &m_set_list
, &m_show_list
, (void *) name
);
186 add_setshow_enum_cmd ("background", theclass
, cli_colors
,
188 _("Set the background color for this property"),
189 _("Show the background color for this property"),
193 &m_set_list
, &m_show_list
, (void *) name
);
194 add_setshow_enum_cmd ("intensity", theclass
, cli_intensities
,
196 _("Set the display intensity for this property"),
197 _("Show the display intensity for this property"),
201 &m_set_list
, &m_show_list
, (void *) name
);
204 static cmd_list_element
*style_set_list
;
205 static cmd_list_element
*style_show_list
;
208 set_style (const char *arg
, int from_tty
)
210 printf_unfiltered (_("\"set style\" must be followed "
211 "by an appropriate subcommand.\n"));
212 help_list (style_set_list
, "set style ", all_commands
, gdb_stdout
);
216 show_style (const char *arg
, int from_tty
)
218 cmd_show_list (style_show_list
, from_tty
, "");
222 set_style_enabled (const char *args
, int from_tty
, struct cmd_list_element
*c
)
224 g_source_cache
.clear ();
225 gdb::observers::source_styling_changed
.notify ();
229 show_style_enabled (struct ui_file
*file
, int from_tty
,
230 struct cmd_list_element
*c
, const char *value
)
233 fprintf_filtered (file
, _("CLI output styling is enabled.\n"));
235 fprintf_filtered (file
, _("CLI output styling is disabled.\n"));
239 show_style_sources (struct ui_file
*file
, int from_tty
,
240 struct cmd_list_element
*c
, const char *value
)
243 fprintf_filtered (file
, _("Source code styling is enabled.\n"));
245 fprintf_filtered (file
, _("Source code styling is disabled.\n"));
249 _initialize_cli_style ()
251 add_prefix_cmd ("style", no_class
, set_style
, _("\
252 Style-specific settings\n\
253 Configure various style-related variables, such as colors"),
254 &style_set_list
, "set style ", 0, &setlist
);
255 add_prefix_cmd ("style", no_class
, show_style
, _("\
256 Style-specific settings\n\
257 Configure various style-related variables, such as colors"),
258 &style_show_list
, "show style ", 0, &showlist
);
260 add_setshow_boolean_cmd ("enabled", no_class
, &cli_styling
, _("\
261 Set whether CLI styling is enabled."), _("\
262 Show whether CLI is enabled."), _("\
263 If enabled, output to the terminal is styled."),
264 set_style_enabled
, show_style_enabled
,
265 &style_set_list
, &style_show_list
);
267 add_setshow_boolean_cmd ("sources", no_class
, &source_styling
, _("\
268 Set whether source code styling is enabled."), _("\
269 Show whether source code styling is enabled."), _("\
270 If enabled, source code is styled.\n"
271 #ifdef HAVE_SOURCE_HIGHLIGHT
272 "Note that source styling only works if styling in general is enabled,\n\
273 see \"show style enabled\"."
275 "Source highlighting is disabled in this installation of gdb, because\n\
276 it was not linked against GNU Source Highlight."
278 ), set_style_enabled
, show_style_sources
,
279 &style_set_list
, &style_show_list
);
281 #define STYLE_ADD_SETSHOW_COMMANDS(STYLE, NAME, PREFIX_DOC) \
282 STYLE.add_setshow_commands (NAME, no_class, PREFIX_DOC, \
284 [] (const char *args, int from_tty) \
287 (STYLE.set_list (), \
288 "set style " NAME " ", \
293 [] (const char *args, int from_tty) \
296 (STYLE.show_list (), \
301 STYLE_ADD_SETSHOW_COMMANDS (file_name_style
, "filename",
303 Filename display styling\n\
304 Configure filename colors and display intensity."));
306 STYLE_ADD_SETSHOW_COMMANDS (function_name_style
, "function",
308 Function name display styling\n\
309 Configure function name colors and display intensity"));
311 STYLE_ADD_SETSHOW_COMMANDS (variable_name_style
, "variable",
313 Variable name display styling\n\
314 Configure variable name colors and display intensity"));
316 STYLE_ADD_SETSHOW_COMMANDS (address_style
, "address",
318 Address display styling\n\
319 Configure address colors and display intensity"));