From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Mark H Weaver Newsgroups: gmane.lisp.guile.user Subject: Re: Change interpreter from octets to hex? Date: Tue, 21 Aug 2018 00:32:16 -0400 Message-ID: <87a7pg9w6n.fsf@netris.org> References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1534825959 20526 195.159.176.226 (21 Aug 2018 04:32:39 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Tue, 21 Aug 2018 04:32:39 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) Cc: guile-user@gnu.org To: Joshua Datko Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Tue Aug 21 06:32:35 2018 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1fryLJ-0005DP-Vt for guile-user@m.gmane.org; Tue, 21 Aug 2018 06:32:34 +0200 Original-Received: from localhost ([::1]:50635 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fryNQ-0003xq-0q for guile-user@m.gmane.org; Tue, 21 Aug 2018 00:34:44 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:56243) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fryMb-0003wZ-Ge for guile-user@gnu.org; Tue, 21 Aug 2018 00:33:54 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fryMY-00008w-BP for guile-user@gnu.org; Tue, 21 Aug 2018 00:33:53 -0400 Original-Received: from world.peace.net ([64.112.178.59]:51274) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1fryMY-00008H-7Z for guile-user@gnu.org; Tue, 21 Aug 2018 00:33:50 -0400 Original-Received: from mhw by world.peace.net with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1fryMX-00069o-3H; Tue, 21 Aug 2018 00:33:49 -0400 In-Reply-To: (Joshua Datko's message of "Sun, 19 Aug 2018 23:54:17 -0600") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 64.112.178.59 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.org gmane.lisp.guile.user:14747 Archived-At: Hi Joshua, Joshua Datko writes: > Is it possible to change the default interpreter behavior to display > hex instead of octets for bytevectors and u8-lists? And if so, how to > do so? > > The default, as y'all know is: > > scheme@(guile-user)> #vu8(#x0a #xaa) > $6 = #vu8(10 170) > > But I would like instead the output to be: > $6 = #vu8(#x0a #xaa) The printer used by the Guile REPL can be customized using the ",option" REPL command. For example: --8<---------------cut here---------------start------------->8--- mhw@jojen ~$ guile GNU Guile 2.2.3 Copyright (C) 1995-2017 Free Software Foundation, Inc. Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'. This program is free software, and you are welcome to redistribute it under certain conditions; type `,show c' for details. Enter `,help' for help. scheme@(guile-user)> ,help system System Commands [abbrev]: ,gc - Garbage collection. ,statistics [,stat] - Display statistics. ,option [NAME] [EXP] [,o] - List/show/set options. ,quit [,q ,continue ,cont] - Quit this session. scheme@(guile-user)> ,option compile-options (#:warnings (unbound-variable macro-use-before-definition arity-mismatch format duplicate-case-datum bad-case-datum)) trace #f interp #f prompt #f print #f value-history #t on-error debug scheme@(guile-user)> (use-modules (rnrs bytevectors) (ice-9 format)) scheme@(guile-user)> (define (format-octet octet) (format #f "#x~2,'0x" octet)) scheme@(guile-user)> (define (print-bytevector bv) (display "#vu8(") (display (string-join (map format-octet (bytevector->u8-list bv)))) (display ")")) scheme@(guile-user)> (define (my-repl-print repl val) (if (bytevector? val) (print-bytevector val) (write val)) (newline)) scheme@(guile-user)> ,o print my-repl-print scheme@(guile-user)> #vu8(0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20) $1 = #vu8(#x00 #x01 #x02 #x03 #x04 #x05 #x06 #x07 #x08 #x09 #x0a #x0b #x0c #x0d #x0e #x0f #x10 #x11 #x12 #x13 #x14) scheme@(guile-user)> --8<---------------cut here---------------end--------------->8--- This simple strategy won't work for bytevectors nested within another data structure. However, you could handle some specific cases by supporting more data structures in your custom printer. For example, if you enhanced the custom printer to print ordinary scheme lists and to call itself recursively for the elements of those lists, then you could support printing bytevectors in your preferred format as long as the surrounding data structure is composed of normal list structure only. Guile's built-in printer does not support customizing its formatting of core data structures, so you cannot handle the general case here without modifying Guile itself. If you wanted to do that, 'scm_i_print_bytevector' in bytevectors.c is the function to modify. Another option would be to wrap your bytevectors within your own record type, in which case you could install your own custom printer for that type using 'set-record-type-printer!'. Mark