aboutsummaryrefslogtreecommitdiff
path: root/gdb/source-cache.c
diff options
context:
space:
mode:
authorTom Tromey <tromey@adacore.com>2020-01-03 13:59:27 -0700
committerTom Tromey <tromey@adacore.com>2020-01-21 12:39:17 -0700
commitf6474de9aacec990d44d2d65a337668b389efd99 (patch)
tree4d70c7c91fba9b49b5b0c62baa7759b6e1d5b13e /gdb/source-cache.c
parentb4654b109bd023d0a22f445db7d4e27f769593f4 (diff)
downloadgdb-f6474de9aacec990d44d2d65a337668b389efd99.zip
gdb-f6474de9aacec990d44d2d65a337668b389efd99.tar.gz
gdb-f6474de9aacec990d44d2d65a337668b389efd99.tar.bz2
Allow use of Pygments to colorize source code
While GNU Source Highlight is good, it's also difficult to build and distribute. For one thing, it needs Boost. For another, it has an unusual configuration and installation setup. Pygments, a Python library, doesn't suffer from these issues, and so I thought it would be a reasonable fallback. This patch implements this idea. GNU Source Highlight is preferred, but if it is unavailable (or fails), the extension languages are tried. This patch also implements support for Pygments. Something similar could be done for Guile, using: https://dthompson.us/projects/guile-syntax-highlight.html However, I don't know enough about Guile internals to make this happen, so I have not done it here. gdb/ChangeLog 2020-01-21 Tom Tromey <tromey@adacore.com> * source-cache.c (source_cache::ensure): Call ext_lang_colorize. * python/python.c (python_extension_ops): Update. (gdbpy_colorize): New function. * python/lib/gdb/__init__.py (colorize): New function. * extension.h (ext_lang_colorize): Declare. * extension.c (ext_lang_colorize): New function. * extension-priv.h (struct extension_language_ops) <colorize>: New member. * cli/cli-style.c (_initialize_cli_style): Update help text. Change-Id: I5e21623ee05f1f66baaa6deaeca78b578c031bf4
Diffstat (limited to 'gdb/source-cache.c')
-rw-r--r--gdb/source-cache.c14
1 files changed, 12 insertions, 2 deletions
diff --git a/gdb/source-cache.c b/gdb/source-cache.c
index d546ae5..71277ec 100644
--- a/gdb/source-cache.c
+++ b/gdb/source-cache.c
@@ -178,9 +178,10 @@ source_cache::ensure (struct symtab *s)
std::string contents = get_plain_source_lines (s, fullname);
-#ifdef HAVE_SOURCE_HIGHLIGHT
if (source_styling && gdb_stdout->can_emit_style_escape ())
{
+#ifdef HAVE_SOURCE_HIGHLIGHT
+ bool already_styled = false;
const char *lang_name = get_language_name (SYMTAB_LANGUAGE (s));
if (lang_name != nullptr)
{
@@ -202,6 +203,7 @@ source_cache::ensure (struct symtab *s)
std::ostringstream output;
highlighter->highlight (input, output, lang_name, fullname);
contents = output.str ();
+ already_styled = true;
}
catch (...)
{
@@ -213,8 +215,16 @@ source_cache::ensure (struct symtab *s)
un-highlighted text. */
}
}
- }
+
+ if (!already_styled)
#endif /* HAVE_SOURCE_HIGHLIGHT */
+ {
+ gdb::optional<std::string> ext_contents;
+ ext_contents = ext_lang_colorize (fullname, contents);
+ if (ext_contents.has_value ())
+ contents = std::move (*ext_contents);
+ }
+ }
source_text result = { std::move (fullname), std::move (contents) };
m_source_map.push_back (std::move (result));