# HG changeset patch
# User Torsten <mttl@mailbox.org>
# Date 1494306340 -7200
#      Tue May 09 07:05:40 2017 +0200
# Node ID 4fd7a9f88e52b83633c59be665860cc2ed1e5ba4
# Parent  c8722519330af17b7c6f7c109fe1572d38ac7475
highlight error and warning messages in the gui console

* QTerminal.cc (default_colors): add default colors for errors and warnings;
  (color_names): add the related color names;
  (notice_settings): set the colors following the settings, using setColor

* QTerminal.h: remove virtual methods setForegroundColor and setBackgroundColor

* CharacterColor.h: define constants for error and warning colors, using
  the first two colors of the system color table

* QUnixTerminalImpl.cpp (setBackgroundColor, setForegroundColor): removed;
  (setColor): new method with the color index as additional parameter;

* QUnixTerminalImpl.h: remove setBackgroundColor, setForegroundColor,
  add setColor;

* resource-manager.h: extend the terminal color chars for error and warnings

* error.cc (verror, vwarning): use function set_color for formatting the
  message string;
  (set_color): new function for detecting error or warning at the beginning
  of a message string and inserting the related escape sequences

* error.h: new enum for possible terminal colors, new array of related
  escape sequences, new function set_color

diff -r c8722519330a -r 4fd7a9f88e52 libgui/qterminal/libqterminal/QTerminal.cc
--- a/libgui/qterminal/libqterminal/QTerminal.cc	Sun May 07 14:00:50 2017 +0200
+++ b/libgui/qterminal/libqterminal/QTerminal.cc	Tue May 09 07:05:40 2017 +0200
@@ -49,7 +49,9 @@ QTerminal::default_colors (void)
       colors << QColor(0,0,0)
              << QColor(255,255,255)
              << QColor(192,192,192)
-             << QColor(128,128,128);
+             << QColor(128,128,128)
+             << QColor(192,0,0)
+             << QColor(192,0,128);
     }
 
   return colors;
@@ -62,10 +64,12 @@ QTerminal::color_names (void)
 
   if (names.isEmpty ())
     {
-      names << QObject::tr ("foreground")
-            << QObject::tr ("background")
-            << QObject::tr ("selection")
-            << QObject::tr ("cursor");
+      names << QObject::tr ("Foreground")
+            << QObject::tr ("Background")
+            << QObject::tr ("Selection")
+            << QObject::tr ("Cursor")
+            << QObject::tr ("Error")
+            << QObject::tr ("Warning");
     }
 
   return names;
@@ -183,14 +187,22 @@ QTerminal::notice_settings (const QSetti
 
   QList<QColor> colors = default_colors ();
 
-  setForegroundColor
-    (settings->value ("terminal/color_f",
+  setColor (DEFAULT_FORE_COLOR,
+    settings->value ("terminal/color_f",
                       QVariant (colors.at (0))).value<QColor> ());
 
-  setBackgroundColor
-    (settings->value ("terminal/color_b",
+  setColor (DEFAULT_BACK_COLOR,
+    settings->value ("terminal/color_b",
                       QVariant (colors.at (1))).value<QColor> ());
 
+  setColor (DEFAULT_ERROR_COLOR,
+    settings->value ("terminal/color_e",
+                      QVariant (colors.at (4))).value<QColor> ());
+
+  setColor (DEFAULT_WARNING_COLOR,
+    settings->value ("terminal/color_w",
+                      QVariant (colors.at (5))).value<QColor> ());
+
   setSelectionColor
     (settings->value ("terminal/color_s",
                       QVariant (colors.at (2))).value<QColor> ());
@@ -199,6 +211,7 @@ QTerminal::notice_settings (const QSetti
     (cursorUseForegroundColor,
      settings->value ("terminal/color_c",
                       QVariant (colors.at (3))).value<QColor> ());
+
   setScrollBufferSize (settings->value ("terminal/history_buffer",1000).toInt () );
 
   // check whether Copy shortcut is Ctrl-C
diff -r c8722519330a -r 4fd7a9f88e52 libgui/qterminal/libqterminal/QTerminal.h
--- a/libgui/qterminal/libqterminal/QTerminal.h	Sun May 07 14:00:50 2017 +0200
+++ b/libgui/qterminal/libqterminal/QTerminal.h	Tue May 09 07:05:40 2017 +0200
@@ -80,9 +80,7 @@ public:
     Q_UNUSED (blinking);
   }
 
-  virtual void setBackgroundColor (const QColor& color) = 0;
-
-  virtual void setForegroundColor (const QColor& color) = 0;
+  virtual void setColor (int color_index, const QColor& color) = 0;
 
   virtual void setSelectionColor (const QColor& color) = 0;
 
diff -r c8722519330a -r 4fd7a9f88e52 libgui/qterminal/libqterminal/unix/CharacterColor.h
--- a/libgui/qterminal/libqterminal/unix/CharacterColor.h	Sun May 07 14:00:50 2017 +0200
+++ b/libgui/qterminal/libqterminal/unix/CharacterColor.h	Tue May 09 07:05:40 2017 +0200
@@ -95,6 +95,8 @@ public:
 
 #define DEFAULT_FORE_COLOR 0
 #define DEFAULT_BACK_COLOR 1
+#define DEFAULT_ERROR_COLOR 2
+#define DEFAULT_WARNING_COLOR 3
 
 //a standard set of colors using black text on a white background.
 //defined in TerminalDisplay.cpp
diff -r c8722519330a -r 4fd7a9f88e52 libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.cpp
--- a/libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.cpp	Sun May 07 14:00:50 2017 +0200
+++ b/libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.cpp	Tue May 09 07:05:40 2017 +0200
@@ -172,7 +172,7 @@ void QUnixTerminalImpl::setCursorType(Cu
 
 // FIXME -- not sure how to make these work properly given the way the
 // Unix terminal handles colors.
-void QUnixTerminalImpl::setBackgroundColor (const QColor& color)
+void QUnixTerminalImpl::setColor (int color_index, const QColor& color)
   {
     ColorEntry cols[TABLE_COLORS];
 
@@ -182,27 +182,11 @@ void QUnixTerminalImpl::setBackgroundCol
      cols[i] = curr_cols[i];
     }
 
-    cols[DEFAULT_BACK_COLOR].color = color;
+    cols[color_index].color = color;
 
     m_terminalView->setColorTable(cols);
+  }
 
-  }
-void QUnixTerminalImpl::setForegroundColor (const QColor& color)
-{
-    ColorEntry cols[TABLE_COLORS];
-
-    const ColorEntry * curr_cols = m_terminalView->colorTable();
-    for(int i=0;i<TABLE_COLORS;i++)
-    {
-     cols[i] = curr_cols[i];
-    }
-
-    cols[DEFAULT_FORE_COLOR].color = color;
-
-    m_terminalView->setColorTable(cols);
-
-
-}
 void QUnixTerminalImpl::setSelectionColor (const QColor& color) { }
 
 void QUnixTerminalImpl::setCursorColor (bool useForegroundColor,
diff -r c8722519330a -r 4fd7a9f88e52 libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.h
--- a/libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.h	Sun May 07 14:00:50 2017 +0200
+++ b/libgui/qterminal/libqterminal/unix/QUnixTerminalImpl.h	Tue May 09 07:05:40 2017 +0200
@@ -43,8 +43,7 @@ public:
 
     void setCursorType(CursorType type, bool blinking);
 
-    void setBackgroundColor (const QColor& color);
-    void setForegroundColor (const QColor& color);
+    void setColor (int color_index, const QColor& color);
     void setSelectionColor (const QColor& color);
     void setCursorColor (bool useForegroundColor, const QColor& color);
     void setScrollBufferSize(int value);
diff -r c8722519330a -r 4fd7a9f88e52 libgui/src/resource-manager.h
--- a/libgui/src/resource-manager.h	Sun May 07 14:00:50 2017 +0200
+++ b/libgui/src/resource-manager.h	Tue May 09 07:05:40 2017 +0200
@@ -108,7 +108,7 @@ public:
   static QStringList storage_class_names (void);
   static QList<QColor> storage_class_default_colors (void);
 
-  static QString terminal_color_chars (void) { return "fbsc"; }
+  static QString terminal_color_chars (void) { return "fbscew"; }
   static QStringList terminal_color_names (void);
   static QList<QColor> terminal_default_colors (void);
 
diff -r c8722519330a -r 4fd7a9f88e52 libinterp/corefcn/error.cc
--- a/libinterp/corefcn/error.cc	Sun May 07 14:00:50 2017 +0200
+++ b/libinterp/corefcn/error.cc	Tue May 09 07:05:40 2017 +0200
@@ -50,6 +50,7 @@ along with Octave; see the file COPYING.
 #include "interpreter.h"
 #include "unwind-prot.h"
 #include "variables.h"
+#include "builtins.h"
 
 // TRUE means that Octave will try to beep obnoxiously before printing
 // error messages.
@@ -227,6 +228,8 @@ verror (bool save_last_error, std::ostre
   if (! buffer_error_messages || Vdebug_on_caught)
     {
       octave_diary << msg_string;
+
+      set_color (&msg_string);
       os << msg_string;
     }
 }
@@ -410,6 +413,7 @@ vwarning (const char *name, const char *
     {
       octave_diary << msg_string;
 
+      set_color (&msg_string);
       std::cerr << msg_string;
     }
 }
@@ -2181,3 +2185,27 @@ interpreter_try (octave::unwind_protect&
   Vdebug_on_warning = false;
   // leave Vdebug_on_caught as it was, so errors in try/catch are still caught
 }
+
+// color a message string if it is an error or warning message
+void
+set_color (std::string *msg_string)
+{
+#if defined (Q_OS_WIN32)
+  // FIXME - windows terminal does not support these esc sequences yet
+  return;
+#else
+  if (! octave::application::is_gui_running ())
+    return;
+
+  if (! strncmp ("warning: ", msg_string->c_str (), 9))
+    {
+      msg_string->insert (9,ESC_SEQ_COL[DEFAULT_COLOR]);
+      msg_string->insert (0,ESC_SEQ_COL[WARNING_COLOR]);
+    }
+  else if (! strncmp ("error: ", msg_string->c_str (), 7))
+    {
+      msg_string->insert (7,ESC_SEQ_COL[DEFAULT_COLOR]);
+      msg_string->insert (0,ESC_SEQ_COL[ERROR_COLOR]);
+    }
+ #endif
+}
\ No newline at end of file
diff -r c8722519330a -r 4fd7a9f88e52 libinterp/corefcn/error.h
--- a/libinterp/corefcn/error.h	Sun May 07 14:00:50 2017 +0200
+++ b/libinterp/corefcn/error.h	Tue May 09 07:05:40 2017 +0200
@@ -179,4 +179,14 @@ extern OCTINTERP_API std::string last_wa
 
 extern OCTINTERP_API void interpreter_try (octave::unwind_protect&);
 
+enum terminal_color
+{
+  DEFAULT_COLOR = 0,
+  ERROR_COLOR,
+  WARNING_COLOR
+};
+const char * const ESC_SEQ_COL [] = {"\x1b[39m", "\x1b[30m", "\x1b[31m"};
+
+void set_color (std::string *msg_stirng);
+
 #endif
