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: Thu, 22 Oct 2020 23:56:21 +0300 Organization: LINKOV.NET Message-ID: <87mu0e0ykq.fsf_-_@mail.linkov.net> References: <83tuv47vkn.fsf@gnu.org> <87ft6igdzi.fsf@mail.linkov.net> <837drt1qta.fsf@gnu.org> <874kmx443u.fsf@mail.linkov.net> <83k0vszw0e.fsf@gnu.org> <87d01kvbnj.fsf@mail.linkov.net> <83wnzsxln9.fsf@gnu.org> <87wnzmgd1f.fsf@mail.linkov.net> <83mu0hng0d.fsf@gnu.org> <87r1ps8xh4.fsf@mail.linkov.net> <831rhroexo.fsf@gnu.org> <87blgv60ug.fsf@mail.linkov.net> <83d01bmol5.fsf@gnu.org> <871rhr3057.fsf@mail.linkov.net> <83blgvmnpu.fsf@gnu.org> <87h7qn1i8z.fsf@mail.linkov.net> <834kmmmn5j.fsf@gnu.org> 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="17135"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (x86_64-pc-linux-gnu) To: 44155@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Oct 22 23:13:53 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 1kVhuC-0004Mh-S7 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 22 Oct 2020 23:13:52 +0200 Original-Received: from localhost ([::1]:39010 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kVhuB-0002Sg-Sz for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 22 Oct 2020 17:13:51 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57236) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kVhsQ-0001GE-Jf for bug-gnu-emacs@gnu.org; Thu, 22 Oct 2020 17:12:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:42518) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kVhsQ-00076m-92 for bug-gnu-emacs@gnu.org; Thu, 22 Oct 2020 17:12:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kVhsQ-0003jJ-4a for bug-gnu-emacs@gnu.org; Thu, 22 Oct 2020 17:12:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Juri Linkov Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 22 Oct 2020 21:12:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 44155 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.160340109014291 (code B ref -1); Thu, 22 Oct 2020 21:12:01 +0000 Original-Received: (at submit) by debbugs.gnu.org; 22 Oct 2020 21:11:30 +0000 Original-Received: from localhost ([127.0.0.1]:54064 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kVhru-0003iQ-8K for submit@debbugs.gnu.org; Thu, 22 Oct 2020 17:11:30 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:52984) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kVhrs-0003iG-O6 for submit@debbugs.gnu.org; Thu, 22 Oct 2020 17:11:29 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57080) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kVhrs-00017L-Ip for bug-gnu-emacs@gnu.org; Thu, 22 Oct 2020 17:11:28 -0400 Original-Received: from relay5-d.mail.gandi.net ([217.70.183.197]:43309) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kVhrp-0006ys-Q2 for bug-gnu-emacs@gnu.org; Thu, 22 Oct 2020 17:11: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 relay5-d.mail.gandi.net (Postfix) with ESMTPSA id 9210A1C0005 for ; Thu, 22 Oct 2020 21:11:22 +0000 (UTC) In-Reply-To: <834kmmmn5j.fsf@gnu.org> (Eli Zaretskii's message of "Thu, 22 Oct 2020 15:59:52 +0300") Received-SPF: pass client-ip=217.70.183.197; envelope-from=juri@linkov.net; helo=relay5-d.mail.gandi.net X-detected-operating-system: by eggs.gnu.org: First seen = 2020/10/22 17:11:23 X-ACL-Warn: Detected OS = Linux 3.11 and newer [fuzzy] X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:191318 Archived-At: --=-=-= Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Tags: patch [Creating a separate feature request from bug#43866] >> Let-binding a new variable 'print-integers-as-chars' to t: >> >> (let ((print-integers-as-chars t)) >> (pp '(("'A" . [?Á]) >> ("'E" . [?É]) >> ("'I" . [?Í]) >> ("'O" . [?Ó]) >> ("'U" . [?Ú]) >> ("'Y" . [?Ý])) >> (current-buffer))) >> >> prints integers as characters: >> >> (("'A" . [?Á]) >> ("'E" . [?É]) >> ("'I" . [?Í]) >> ("'O" . [?Ó]) >> ("'U" . [?Ú]) >> ("'Y" . [?Ý])) >> >> with this patch: > > The idea is fine, but I have a few comments about implementation: > >> case_Lisp_Int: >> { >> - int len = sprintf (buf, "%"pI"d", XFIXNUM (obj)); >> - strout (buf, len, len, printcharfun); >> + if (!NILP (Vprint_integers_as_chars) && CHARACTERP (obj)) > ^^^^^^^^^^^^^^^^^^^^^^^^ > If this is supposed to be a boolean variable, please use DEFVAR_BOOL, > with all the consequences. Fixed in the next patch. >> + int len = sprintf (buf, "%s", SDATA (call1 (intern ("prin1-char"), obj))); > > Do we really need to call Lisp? I thought we were quite capable of > printing characters from C, aren't we? Thanks for the hint. Now the patch uses only C functions. (My initial idea was to use eval-expression-print-format as a base that has (let ((char-string (and (characterp value) (<= value eval-expression-print-maximum-character) (char-displayable-p value) (prin1-char value)))) but it seems only the condition 'characterp' is needed in C implementation.) >> @@ -2247,6 +2255,10 @@ syms_of_print (void) >> that represents the number without losing information. */); >> Vfloat_output_format = Qnil; >> >> + DEFVAR_LISP ("print-integers-as-chars", Vprint_integers_as_chars, >> + doc: /* Print integers as characters. */); >> + Vprint_integers_as_chars = Qnil; > > I wonder whether it wouldn't be cleaner to add another optional > argument to prin1, and let it bind some internal variable so that > print_object does this, instead of exposing this knob to Lisp. > Because print_object is used all over the place, and who knows what > will this do to other callers? The variable 'print-integers-as-chars' is modeled after many similar variables that affect the prin1 output: - print-escape-control-characters - print-escape-newlines - print-escape-nonascii - print-escape-multibyte - print-length - print-level - print-quoted - print-circle - float-output-format But now this leads me to think that maybe the new variable should be like 'float-output-format', so it could be named 'integer-output-format' and support options for different integer formats: - 'character': print integers as characters; - 'decimal': the default format; - 'binary': print integers as e.g. #b010101; - 'octal': print integers as e.g. #o777; - 'hex': print integers as e.g. #x00ff; --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=print-integers-as-characters.patch diff --git a/src/print.c b/src/print.c index dca095f281..909c55efed 100644 --- a/src/print.c +++ b/src/print.c @@ -1908,8 +1908,16 @@ 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); + if (print_integers_as_characters && CHARACTERP (obj)) + { + printchar ('?', printcharfun); + print_string (CALLN (Fstring, obj), printcharfun); + } + else + { + int len = sprintf (buf, "%"pI"d", XFIXNUM (obj)); + strout (buf, len, len, printcharfun); + } } break; @@ -2247,6 +2255,10 @@ syms_of_print (void) that represents the number without losing information. */); Vfloat_output_format = Qnil; + DEFVAR_BOOL ("print-integers-as-characters", print_integers_as_characters, + doc: /* Print integers as characters. */); + print_integers_as_characters = 0; + 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'. */); --=-=-=--