[gnumeric] Help texts: allow a format for function examples.



commit 86747becb3bca069d4622ebf1b34e6618a15fa6a
Author: Morten Welinder <terra gnome org>
Date:   Sat Apr 21 22:18:35 2018 -0400

    Help texts: allow a format for function examples.

 plugins/fn-financial/ChangeLog       |    5 ++++
 plugins/fn-financial/functions.c     |    2 +-
 src/dialogs/ChangeLog                |    6 +++++
 src/dialogs/dialog-function-select.c |   41 ++++++++++++++++++++++++++++-----
 4 files changed, 46 insertions(+), 8 deletions(-)
---
diff --git a/plugins/fn-financial/ChangeLog b/plugins/fn-financial/ChangeLog
index 2a5b71e..91eea68 100644
--- a/plugins/fn-financial/ChangeLog
+++ b/plugins/fn-financial/ChangeLog
@@ -1,3 +1,8 @@
+2018-04-21  Morten Welinder  <terra gnome org>
+
+       * functions.c (help_dollarde): Format the sample expression value
+       to ensure we see four decimals.
+
 2018-03-13  Morten Welinder <terra gnome org>
 
        * Release 1.12.39
diff --git a/plugins/fn-financial/functions.c b/plugins/fn-financial/functions.c
index e5b477d..56c6dc5 100644
--- a/plugins/fn-financial/functions.c
+++ b/plugins/fn-financial/functions.c
@@ -935,7 +935,7 @@ static GnmFuncHelp const help_dollarde[] = {
         { GNM_FUNC_HELP_ARG, F_("fractional_dollar:amount to convert")},
         { GNM_FUNC_HELP_ARG, F_("fraction:denominator")},
        { GNM_FUNC_HELP_DESCRIPTION, F_("DOLLARDE converts a fractional dollar amount into a decimal amount.  
This is the inverse of the DOLLARFR function.") },
-       { GNM_FUNC_HELP_EXAMPLES, "=DOLLARDE(0.03,16)" },
+       { GNM_FUNC_HELP_EXAMPLES, "=TEXT(DOLLARDE(0.03,16),\"0.0000\")" },
         { GNM_FUNC_HELP_SEEALSO, "DOLLARFR" },
        { GNM_FUNC_HELP_END }
 };
diff --git a/src/dialogs/ChangeLog b/src/dialogs/ChangeLog
index 2f8b57f..c36e9fd 100644
--- a/src/dialogs/ChangeLog
+++ b/src/dialogs/ChangeLog
@@ -1,3 +1,9 @@
+2018-04-21  Morten Welinder  <terra gnome org>
+
+       * dialog-function-select.c (make_expr_example): Handle examples
+       with specified format.  (That is specified as an expression wrapped
+       in TEXT(...,"fmt").)
+
 2018-03-13  Morten Welinder <terra gnome org>
 
        * Release 1.12.39
diff --git a/src/dialogs/dialog-function-select.c b/src/dialogs/dialog-function-select.c
index 15a00e2..8d90816 100644
--- a/src/dialogs/dialog-function-select.c
+++ b/src/dialogs/dialog-function-select.c
@@ -641,7 +641,8 @@ cb_link_event (GtkTextTag *link, G_GNUC_UNUSED GObject *trigger,
 }
 
 static char *
-make_expr_example (Sheet *sheet, const char *text, gboolean localized)
+make_expr_example (Sheet *sheet, const char *text,
+                  gboolean localized, gboolean consider_format)
 {
        GnmLocale *oldlocale = NULL;
        GnmExprTop const *texpr;
@@ -649,6 +650,22 @@ make_expr_example (Sheet *sheet, const char *text, gboolean localized)
        GnmParsePos pp;
        GnmEvalPos ep;
        GnmConventions const *convs = gnm_conventions_default;
+       char *tmp_text = NULL;
+       GOFormat const *fmt = NULL;
+
+       if (consider_format &&
+           g_ascii_strncasecmp (text, "TEXT(", 5) == 0 &&
+           text[strlen(text) - 1] == ')') {
+               char *p;
+               tmp_text = g_strdup (text + 5);
+               p = tmp_text + strlen (tmp_text) - 1;
+               while (p >= tmp_text && p[0] != '"') p--;
+               p[0] = 0;
+               while (p >= tmp_text && p[0] != '"') p--;
+               fmt = go_format_new_from_XL (p + 1);
+               while (p >= tmp_text && p[0] != ',') p--;
+               *p = 0;
+       }
 
        eval_pos_init_sheet (&ep, sheet);
        parse_pos_init_evalpos (&pp, &ep);
@@ -666,12 +683,13 @@ make_expr_example (Sheet *sheet, const char *text, gboolean localized)
                char *etxt = gnm_expr_top_as_string (texpr, &pp, convs);
                GnmValue *val = gnm_expr_top_eval
                        (texpr, &ep, GNM_EXPR_EVAL_PERMIT_NON_SCALAR);
-               GOFormat const *format = gnm_auto_style_format_suggest (texpr, &ep);
-               char *vtxt = format_value (format, val, -1,
-                                          workbook_date_conv
-                                          (sheet->workbook));
+               char *vtxt;
+
+               if (!fmt)
+                       fmt = gnm_auto_style_format_suggest (texpr, &ep);
+               vtxt = format_value (fmt, val, -1,
+                                    workbook_date_conv (sheet->workbook));
 
-               go_format_unref (format);
                gnm_expr_top_unref (texpr);
                value_release (val);
 
@@ -684,6 +702,10 @@ make_expr_example (Sheet *sheet, const char *text, gboolean localized)
                res = g_strdup ("");
        }
 
+       g_free (tmp_text);
+       if (fmt)
+               go_format_unref (fmt);
+
        return res;
 }
 
@@ -722,6 +744,8 @@ describe_new_style (GtkTextBuffer *description,
        gboolean args_finished = FALSE;
        gboolean seen_examples = FALSE;
        gboolean seen_extref = FALSE;
+       gboolean is_TEXT =
+               g_ascii_strcasecmp (gnm_func_get_name (func, FALSE), "TEXT") == 0;
 
        gtk_text_buffer_get_end_iter (description, &ti);
 
@@ -786,7 +810,10 @@ describe_new_style (GtkTextBuffer *description,
                        }
 
                        if (text[0] == '=') {
-                               char *example = make_expr_example (sheet, text + 1, was_translated);
+                               char *example =
+                                       make_expr_example (sheet, text + 1,
+                                                          was_translated,
+                                                          !is_TEXT);
                                ADD_TEXT (example);
                                g_free (example);
                        } else {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]