Minor issue saving '\' in keymap in nightlies

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Max Stats
    Swordsman
    • Jun 2010
    • 316

    Minor issue saving '\' in keymap in nightlies

    I have encountered a very minor issue in the nightlies. I have a keymap for '^\', and when I save keymaps, the next time I load the game I receive an error that the line in the pref file with this keymap is too long. I assume that this is because the '\' character escapes the newline after it, and this messes everything up. I can fix it by editing the pref file and changing the trigger to '^\\'. This fixes it until the next time I save keymaps.

    The fix seems to be writing two '\' characters anytime one is encountered in a keymap. I don't care too much, because once I get my keymaps set, I won't be saving them much anymore.
    If beauty is in the eye of the beholder, then why are beholders so freaking ugly?
  • myshkin
    Angband Devteam member
    • Apr 2007
    • 327

    #2
    Confirmed, and entered as bug 1389 on trac. Thanks for the report.

    Comment

    • Max Stats
      Swordsman
      • Jun 2010
      • 316

      #3
      For awhile, I thought that once I had my keymaps set, I wouldn't need to change them much, and this bug wouldn't be that annoying. Turns out, it is more annoying than I thought, as I still tweak my keymaps from time to time. In fact, it was annoying enough that I decided to take a swing at it and see if I could fix it on my copy. I think I have done it. Here is my patch:

      Code:
      diff --git a/src/keymap.c b/src/keymap.c
      index 35a3304..d1a0818 100644
      --- a/src/keymap.c
      +++ b/src/keymap.c
      @@ -174,12 +174,12 @@ void keymap_dump(ang_file *fff)
       		struct keypress key[2] = { { 0 }, { 0 } };
       
       		/* Encode the action */
      -		keypress_to_text(buf, sizeof(buf), k->actions);
      +		keypress_to_text(buf, sizeof(buf), k->actions, FALSE);
       		file_putf(fff, "A:%s\n", buf);
       
       		/* Convert the key into a string */
       		key[0] = k->key;
      -		keypress_to_text(buf, sizeof(buf), key);
      +		keypress_to_text(buf, sizeof(buf), key, TRUE);
       		file_putf(fff, "C:%d:%s\n", mode, buf);
       
       		file_putf(fff, "\n");
      diff --git a/src/ui-event.c b/src/ui-event.c
      index c9e9601..44412ef 100644
      --- a/src/ui-event.c
      +++ b/src/ui-event.c
      @@ -221,7 +221,7 @@ void keypress_from_text(struct keypress *buf, size_t len, const char *str)
       /*
        * Convert a string of keypresses into their textual equivalent.
        */
      -void keypress_to_text(char *buf, size_t len, const struct keypress *src)
      +void keypress_to_text(char *buf, size_t len, const struct keypress *src, bool expand_backslash)
       {
       	size_t cur = 0;
       	size_t end = 0;
      @@ -258,7 +258,13 @@ void keypress_to_text(char *buf, size_t len, const struct keypress *src)
       		} else {
       			switch (i) {
       				case '\a': strnfcat(buf, len, &end, "\a"); break;
      -				case '\\': strnfcat(buf, len, &end, "\\"); break;
      +				case '\\': {
      +					if (expand_backslash)
      +						   strnfcat(buf, len, &end, "\\\\");
      +					else
      +						   strnfcat(buf, len, &end, "\\");
      +					break;
      +				}
       				case '^': strnfcat(buf, len, &end, "\\^"); break;
       				case '[': strnfcat(buf, len, &end, "\\["); break;
       				default: {
      diff --git a/src/ui-event.h b/src/ui-event.h
      index 5becb8e..0942fe5 100644
      --- a/src/ui-event.h
      +++ b/src/ui-event.h
      @@ -179,7 +179,7 @@ keycode_t keycode_find_code(const char *str, size_t len);
       const char *keycode_find_desc(keycode_t kc);
       
       /** Convert a string of keypresses into their textual representation */
      -void keypress_to_text(char *buf, size_t len, const struct keypress *src);
      +void keypress_to_text(char *buf, size_t len, const struct keypress *src, bool expand_backslash);
       
       /** Covert a textual representation of keypresses into actual keypresses */
       void keypress_from_text(struct keypress *buf, size_t len, const char *str);
      diff --git a/src/ui-options.c b/src/ui-options.c
      index ce5a849..7bc5e2a 100644
      --- a/src/ui-options.c
      +++ b/src/ui-options.c
      @@ -367,7 +367,7 @@ static struct keypress keymap_get_trigger(void)
       	buf[0] = inkey();
       
       	/* Convert to ascii */
      -	keypress_to_text(tmp, sizeof(tmp), buf);
      +	keypress_to_text(tmp, sizeof(tmp), buf, FALSE);
       
       	/* Hack -- display the trigger */
       	Term_addstr(-1, TERM_WHITE, tmp);
      @@ -420,7 +420,7 @@ static void ui_keymap_query(const char *title, int row)
       	else
       	{
       		/* Analyze the current action */
      -		keypress_to_text(tmp, sizeof(tmp), act);
      +		keypress_to_text(tmp, sizeof(tmp), act, FALSE);
       	
       		/* Display the current action */
       		prt("Found: ", 15, 0);
      @@ -449,7 +449,7 @@ static void ui_keymap_create(const char *title, int row)
       	while (!done) {
       		struct keypress kp;
       
      -		keypress_to_text(tmp, sizeof(tmp), keymap_buffer);
      +		keypress_to_text(tmp, sizeof(tmp), keymap_buffer, FALSE);
       		c_prt(first ? TERM_YELLOW : TERM_WHITE,
       				format("Action: %s", tmp), 15, 0);
       
      @@ -515,7 +515,7 @@ static void keymap_browse_hook(int oid, void *db, const region *loc)
       
       	/* Show current action */
       	prt("Current action (if any) shown below:", 13, 0);
      -	keypress_to_text(tmp, sizeof(tmp), keymap_buffer);
      +	keypress_to_text(tmp, sizeof(tmp), keymap_buffer, FALSE);
       	prt(tmp, 14, 0);
       }
      Caveat: I poke around the code from time to time, but I am far from familiar with it, so a more elegant solution might be possible from someone more learned than I am. The gist of this is to add a flag to keypress_to_text that causes it to double the backslash character when it is TRUE. Everyone sets the flag to FALSE except keymap_dump, which sets it to true when outputting the "C:" lines.

      I have tested this on my own pref files, which seem to work correctly, but I naturally can't guarantee that this won't introduce bugs of its own.
      If beauty is in the eye of the beholder, then why are beholders so freaking ugly?

      Comment

      • Magnate
        Angband Devteam member
        • May 2007
        • 4916

        #4
        Originally posted by Max Stats
        For awhile, I thought that once I had my keymaps set, I wouldn't need to change them much, and this bug wouldn't be that annoying. Turns out, it is more annoying than I thought, as I still tweak my keymaps from time to time. In fact, it was annoying enough that I decided to take a swing at it and see if I could fix it on my copy. I think I have done it. Here is my patch:
        Awesome - thank you. This looks fine at first glance, but the input layer is not my specialty so I will wait for one of the others to cast an eye over it before committing.
        "Been away so long I hardly knew the place, gee it's good to be back home" - The Beatles

        Comment

        • myshkin
          Angband Devteam member
          • Apr 2007
          • 327

          #5
          Originally posted by Max Stats
          Here is my patch:
          Thanks! I have committed this change to staging, and it should be in a dev release shortly. Note that the keymap module does not actually map a bare '\', as that is needed for command escaping, but your change fixes behavior for keypresses like ^\.

          Comment

          Working...
          😀
          😂
          🥰
          😘
          🤢
          😎
          😞
          😡
          👍
          👎