#define ESCAPE_HEX             BIT(5)
 #define ESCAPE_NA              BIT(6)
 #define ESCAPE_NAP             BIT(7)
+#define ESCAPE_APPEND          BIT(8)
 
 int string_escape_mem(const char *src, size_t isz, char *dst, size_t osz,
                unsigned int flags, const char *only);
 
  *             escape only non-ascii characters, checked by isascii()
  *     %ESCAPE_NAP:
  *             escape only non-printable or non-ascii characters
+ *     %ESCAPE_APPEND:
+ *             append characters from @only to be escaped by the given classes
+ *
+ * %ESCAPE_APPEND would help to pass additional characters to the escaped, when
+ * one of %ESCAPE_NP, %ESCAPE_NA, or %ESCAPE_NAP is provided.
  *
  * One notable caveat, the %ESCAPE_NAP, %ESCAPE_NP and %ESCAPE_NA have the
  * higher priority than the rest of the flags (%ESCAPE_NAP is the highest).
        char *p = dst;
        char *end = p + osz;
        bool is_dict = only && *only;
+       bool is_append = flags & ESCAPE_APPEND;
 
        while (isz--) {
                unsigned char c = *src++;
+               bool in_dict = is_dict && strchr(only, c);
 
                /*
                 * Apply rules in the following sequence:
                 *        defined by given @flags
                 * In these cases we just pass through a character to the
                 * output buffer.
+                *
+                * When %ESCAPE_APPEND is passed, the characters from @only
+                * have been excluded from the %ESCAPE_NAP, %ESCAPE_NP, and
+                * %ESCAPE_NA cases.
                 */
-               if (is_dict && !strchr(only, c) &&
+               if (!(is_append || in_dict) && is_dict &&
                                          escape_passthrough(c, &p, end))
                        continue;
 
-               if (isascii(c) && isprint(c) &&
+               if (!(is_append && in_dict) && isascii(c) && isprint(c) &&
                    flags & ESCAPE_NAP && escape_passthrough(c, &p, end))
                        continue;
 
-               if (isprint(c) &&
+               if (!(is_append && in_dict) && isprint(c) &&
                    flags & ESCAPE_NP && escape_passthrough(c, &p, end))
                        continue;
 
-               if (isascii(c) &&
+               if (!(is_append && in_dict) && isascii(c) &&
                    flags & ESCAPE_NA && escape_passthrough(c, &p, end))
                        continue;