#586 set encoding <variable>


This patch implements "set encoding <variable-name>". The reason for this is that I wanted an easy way to reset the encoding to its previous setting within a script. Previously, my script used

evaluate("set encoding " . save_enc)

With this patch the last line simplifies to

set encoding save_enc

Since I wasn't sure if keywords should always take precedence over variable names or not, I put both implementation into the patch.


  • Changelog entry:

    2012-05-20 Bastian Maerkisch <bmaerkisch@web.de>

    \* src/set.c \(set\_encoding\): Implement "set encoding &lt;variable&gt;".
    Attention: currently includes code two cases: variable names take
    precedence over key words and vice versa.
    \* src/util.c|h \(almost\_equals\_str\): New function. Compares two strings
    and returns TRUE if they are identical up to the first $ in the second
    \* src/tables.c|h \(lookup\_table\_str\): New function. Works like 
    lookup\_table\(\), but accepts a string argument instead of a token.
  • Ethan Merritt
    Ethan Merritt

    That seems like a lot of code to support what should be a pretty simple change.

    I don't see a need to determine precedence between keywords and strings, since the syntax only allows a single option per invocation anyhow. Either you give it a keyword or you give it a string.

    Perhaps I misunderstand the intended use, but isn't the following patch sufficient?

    --- old/set.c 2012-05-21 16:15:18.000000000 -0700
    +++ new/set.c 2012-05-22 15:01:11.000000000 -0700
    @@ -1391,6 +1391,12 @@ set_encoding()
    encoding = S_ENC_SJIS;
    + } else if (isstringvalue(c_token)) {
    + int i;
    + char *senc = try_to_get_string();
    + for (i=0; encoding_names[i] != NULL; i++)
    + if (!strcmp(encoding_names[i],senc))
    + encoding = i;
    } else {
    int temp = lookup_table(&set_encoding_tbl[0],c_token);

  • You are absolutely right. That was way to much code. What was I thinking?

    Anyway, the question of precedence of keywords is illustrated by the following example:

    default = "sjis"
    set encoding default

    Do we expect to get expect to get SJIS or the platform's default?

    If we want the later, the following variant will do the job:

    --- old/set.c 2012-05-21 16:15:18.000000000 -0700
    +++ new/set.c 2012-05-22 15:01:11.000000000 -0700
    @@ -1391,6 +1391,14 @@
    } else {
    int temp = lookup_table(&set_encoding_tbl[0],c_token);

    + if ((temp == S_ENC_INVALID) && isstringvalue(c_token)) {
    + int i;
    + char *senc = try_to_get_string();
    + for (i = 0; encoding_names[i] != NULL; i++)
    + if (!strcmp(encoding_names[i], senc))
    + temp = i;
    + }
    if (temp == S_ENC_INVALID)
    int_error(c_token, "unrecognized encoding specification; see 'help encoding'.");
    encoding = temp;

  • Ethan Merritt
    Ethan Merritt

    Ah. That's what you meant by precedence. Other gnuplot commands check for legal keywords first and only check for variables or other syntax elements afterwards. So I think that's what this code should do also.

    Getting back to the intended use. Would the same purpose be served by instead adding two keywords push/pop?

  • > Getting back to the intended use. Would the same purpose be served by
    > instead adding two keywords push/pop?

    That would definitely be the most elegant solution. But using variables would be just fine by me.

    Btw. some of the demo scripts currently change the encoding and do not restore it. Once we have a solution, I opt to fix that.

  • New version of the patch. Implements
    set encoding push | pop
    set encoding <variable>.
    Includes an update to gnuplot.doc and adds push/pop commands to demos which change the encoding.

    I am uploading the patch here to discuss if adding the push_encoding variable to term.c / term_api.h is appropriate or if it should go somewhere else.

  • Petr Mikulik
    Petr Mikulik

    I think that set encoding save_enc is fine enough and there is no need for push/pop. The push/pop mechanism was invented by me for "set term" ages ago where no other possibilities like strings, macros etc. were available. I agree with the precedence strings first, variables next.

    (Hm, actually, set macros; set encoding @enc works even now.)

    • status: open --> closed-fixed
  • Now in CVS (without set encoding push|pop).