emacs-diffs
[Top][All Lists]
Advanced

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

[Emacs-diffs] master eb9d3ec 3/7: Escape control characters in backtrace


From: Noam Postavsky
Subject: [Emacs-diffs] master eb9d3ec 3/7: Escape control characters in backtraces (Bug#6991)
Date: Thu, 29 Jun 2017 19:47:46 -0400 (EDT)

branch: master
commit eb9d3eca801c1ea847956a96fafd29eef9bbe5d1
Author: Noam Postavsky <address@hidden>
Commit: Noam Postavsky <address@hidden>

    Escape control characters in backtraces (Bug#6991)
    
    * src/print.c (syms_of_print): Add new variable,
    print-escape-control-characters.
    (print_object): Print control characters with octal escape codes when
    print-escape-control-characters is true.
    * lisp/subr.el (backtrace):
    * lisp/emacs-lisp/debug.el (debugger-setup-buffer): Bind
    `print-escape-control-characters' to t.
---
 lisp/subr.el |  3 ++-
 src/print.c  | 45 +++++++++++++++++++++++++++++++++------------
 2 files changed, 35 insertions(+), 13 deletions(-)

diff --git a/lisp/subr.el b/lisp/subr.el
index d0c8517..a9edff6 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -4514,7 +4514,8 @@ EVALD, FUNC, ARGS, FLAGS are as in `mapbacktrace'."
 (defun backtrace ()
   "Print a trace of Lisp function calls currently active.
 Output stream used is value of `standard-output'."
-  (let ((print-level (or print-level 8)))
+  (let ((print-level (or print-level 8))
+        (print-escape-control-characters t))
     (mapbacktrace #'backtrace--print-frame 'backtrace)))
 
 (defun backtrace-frames (&optional base)
diff --git a/src/print.c b/src/print.c
index 6bf8af9..50c75d7 100644
--- a/src/print.c
+++ b/src/print.c
@@ -1870,21 +1870,36 @@ print_object (Lisp_Object obj, Lisp_Object 
printcharfun, bool escapeflag)
                }
              else
                {
+                  bool still_need_nonhex = false;
                  /* If we just had a hex escape, and this character
                     could be taken as part of it,
                     output `\ ' to prevent that.  */
-                 if (need_nonhex && c_isxdigit (c))
-                   print_c_string ("\\ ", printcharfun);
-
-                 if (c == '\n' && print_escape_newlines
-                     ? (c = 'n', true)
-                     : c == '\f' && print_escape_newlines
-                     ? (c = 'f', true)
-                     : c == '\"' || c == '\\')
-                   printchar ('\\', printcharfun);
-
-                 printchar (c, printcharfun);
-                 need_nonhex = false;
+                  if (c_isxdigit (c))
+                    {
+                      if (need_nonhex)
+                        print_c_string ("\\ ", printcharfun);
+                      printchar (c, printcharfun);
+                    }
+                  else if (c == '\n' && print_escape_newlines
+                           ? (c = 'n', true)
+                           : c == '\f' && print_escape_newlines
+                           ? (c = 'f', true)
+                           : c == '\0' && print_escape_control_characters
+                           ? (c = '0', still_need_nonhex = true)
+                           : c == '\"' || c == '\\')
+                    {
+                      printchar ('\\', printcharfun);
+                      printchar (c, printcharfun);
+                    }
+                  else if (print_escape_control_characters && c_iscntrl (c))
+                    {
+                      char outbuf[1 + 3 + 1];
+                      int len = sprintf (outbuf, "\\%03o", c + 0u);
+                      strout (outbuf, len, len, printcharfun);
+                    }
+                  else
+                    printchar (c, printcharfun);
+                 need_nonhex = still_need_nonhex;
                }
            }
          printchar ('\"', printcharfun);
@@ -2329,6 +2344,11 @@ A value of nil means no limit.  See also 
`eval-expression-print-level'.  */);
 Also print formfeeds as `\\f'.  */);
   print_escape_newlines = 0;
 
+  DEFVAR_BOOL ("print-escape-control-characters", 
print_escape_control_characters,
+              doc: /* Non-nil means print control characters in strings as 
`\\OOO'.
+\(OOO is the octal representation of the character code.)*/);
+  print_escape_control_characters = 0;
+
   DEFVAR_BOOL ("print-escape-nonascii", print_escape_nonascii,
               doc: /* Non-nil means print unibyte non-ASCII chars in strings 
as \\OOO.
 \(OOO is the octal representation of the character code.)
@@ -2418,6 +2438,7 @@ priorities.  */);
   DEFSYM (Qprint_escape_newlines, "print-escape-newlines");
   DEFSYM (Qprint_escape_multibyte, "print-escape-multibyte");
   DEFSYM (Qprint_escape_nonascii, "print-escape-nonascii");
+  DEFSYM (Qprint_escape_control_characters, "print-escape-control-characters");
 
   print_prune_charset_plist = Qnil;
   staticpro (&print_prune_charset_plist);



reply via email to

[Prev in Thread] Current Thread [Next in Thread]