From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Juri Linkov Newsgroups: gmane.emacs.bugs Subject: bug#44155: Print integers as characters Date: Sat, 24 Oct 2020 22:53:44 +0300 Organization: LINKOV.NET Message-ID: <87d0172yev.fsf@mail.linkov.net> References: <877drh7unm.fsf@igel.home> <87lffxuwgn.fsf@mail.linkov.net> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="34247"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (x86_64-pc-linux-gnu) Cc: 44155@debbugs.gnu.org To: Andreas Schwab Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Oct 24 22:01:03 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kWPip-0008pI-8z for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 24 Oct 2020 22:01:03 +0200 Original-Received: from localhost ([::1]:46962 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kWPio-0007tU-7I for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 24 Oct 2020 16:01:02 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59076) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kWPgu-0005D4-LZ for bug-gnu-emacs@gnu.org; Sat, 24 Oct 2020 15:59:05 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:51924) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kWPgr-0000MH-JZ for bug-gnu-emacs@gnu.org; Sat, 24 Oct 2020 15:59:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kWPgr-0002pY-Jd for bug-gnu-emacs@gnu.org; Sat, 24 Oct 2020 15:59:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 24 Oct 2020 19:59:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 44155 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 44155-submit@debbugs.gnu.org id=B44155.160356950910834 (code B ref 44155); Sat, 24 Oct 2020 19:59:01 +0000 Original-Received: (at 44155) by debbugs.gnu.org; 24 Oct 2020 19:58:29 +0000 Original-Received: from localhost ([127.0.0.1]:35237 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kWPgK-0002of-Kq for submit@debbugs.gnu.org; Sat, 24 Oct 2020 15:58:28 -0400 Original-Received: from relay8-d.mail.gandi.net ([217.70.183.201]:49491) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kWPgG-0002oK-Sf for 44155@debbugs.gnu.org; Sat, 24 Oct 2020 15:58:27 -0400 X-Originating-IP: 91.129.102.160 Original-Received: from mail.gandi.net (m91-129-102-160.cust.tele2.ee [91.129.102.160]) (Authenticated sender: juri@linkov.net) by relay8-d.mail.gandi.net (Postfix) with ESMTPSA id AAB661BF207; Sat, 24 Oct 2020 19:58:15 +0000 (UTC) In-Reply-To: <87lffxuwgn.fsf@mail.linkov.net> (Juri Linkov's message of "Fri, 23 Oct 2020 11:32:52 +0300") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:191452 Archived-At: --=-=-= Content-Type: text/plain >>> + if (print_integers_as_characters && CHARACTERP (obj)) >>> + { >>> + printchar ('?', printcharfun); >>> + print_string (CALLN (Fstring, obj), printcharfun); >> >> That will create ambigous output. > > Or do you mean: > > (dotimes (i (max-char)) > (condition-case err > (unless (eq i (read (concat "?" (string i)))) > (message "%d ?%s" i (string i))) > (error (message "%d ?%s ;; %s" i (string i) (error-message-string err))))) > > 92 ?\ ;; End of file during parsing > 4194176 ?\200 > ... > 4194302 ?\376 Now the following patch on this code (let ((integer-output-format t)) (pp '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\ 4194176) (current-buffer))) outputs (?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\ 4194176) and no ambiguities found with (let ((integer-output-format t)) (dotimes (i (+ (max-char) 2)) (condition-case err (unless (eq i (read (format "%S" i))) (message "%d ?%s" i (string i))) (error (message "%d ?%s ;; %s" i (string i) (error-message-string err)))))) The list of escaped characters was taken from 'prin1-char', not from a similar list in 'print_object' in 'case Lisp_Symbol' branch. Also 'integer-output-format' prints integers in hex format when set to 16. (let ((integer-output-format 16)) (pp '(?\; ?\( ?\) ?\{ ?\} ?\[ ?\] ?\" ?\' ?\\ 4194176) (current-buffer))) => (#x3b #x28 #x29 #x7b #x7d #x5b #x5d #x22 #x27 #x5c #x3fff80) --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=integer-output-format.patch diff --git a/src/print.c b/src/print.c index 53aa353769..53c8c4c91a 100644 --- a/src/print.c +++ b/src/print.c @@ -1908,8 +1908,29 @@ print_object (Lisp_Object obj, Lisp_Object printcharfun, bool escapeflag) { case_Lisp_Int: { - int len = sprintf (buf, "%"pI"d", XFIXNUM (obj)); - strout (buf, len, len, printcharfun); + EMACS_INT c = XFIXNUM (obj); + + if (EQ (Vinteger_output_format, Qt) && CHARACTERP (obj) && c < 4194176) + { + printchar ('?', printcharfun); + + if (escapeflag + && (c == ';' || c == '(' || c == ')' || c == '{' || c == '}' + || c == '[' || c == ']' || c == '\"' || c == '\'' || c == '\\')) + printchar ('\\', printcharfun); + print_string (Fchar_to_string (obj), printcharfun); + } + else if (INTEGERP (Vinteger_output_format) + && XFIXNUM (Vinteger_output_format) == 16 && c >= 0) + { + int len = sprintf (buf, "#x%"pI"x", (EMACS_UINT) c); + strout (buf, len, len, printcharfun); + } + else + { + int len = sprintf (buf, "%"pI"d", c); + strout (buf, len, len, printcharfun); + } } break; @@ -2247,6 +2268,13 @@ syms_of_print (void) that represents the number without losing information. */); Vfloat_output_format = Qnil; + DEFVAR_LISP ("integer-output-format", Vinteger_output_format, + doc: /* The format used to print integers. +When 't', print integers as characters. +When a number 16, print numbers in hex format. +Otherwise, print integers in decimal format. */); + Vinteger_output_format = Qnil; + DEFVAR_LISP ("print-length", Vprint_length, doc: /* Maximum length of list to print before abbreviating. A value of nil means no limit. See also `eval-expression-print-length'. */); --=-=-=--