From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: mousebot Newsgroups: gmane.emacs.devel Subject: Re: hierarchy-print doesn't allow sending indent-string arg to hierarchy-map Date: Tue, 1 Aug 2023 13:11:08 +0200 Message-ID: <4a9152a4-3aff-3550-e967-e77a1c9307dc@riseup.net> References: <87a5vi4piz.fsf@cassou.me> <87jzuh2abv.fsf@cassou.me> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------Mb0BByTxA8QJXpE0eodXYAUq" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24329"; mail-complaints-to="usenet@ciao.gmane.io" To: Damien Cassou , emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Aug 01 13:26:02 2023 Return-path: Envelope-to: ged-emacs-devel@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 1qQnVp-00066V-ND for ged-emacs-devel@m.gmane-mx.org; Tue, 01 Aug 2023 13:26:02 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQnV9-0002C2-I1; Tue, 01 Aug 2023 07:25:19 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQnHZ-0004Zz-1W for emacs-devel@gnu.org; Tue, 01 Aug 2023 07:11:17 -0400 Original-Received: from mx0.riseup.net ([198.252.153.6]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qQnHW-0002Rb-RW for emacs-devel@gnu.org; Tue, 01 Aug 2023 07:11:16 -0400 Original-Received: from fews02-sea.riseup.net (fews02-sea-pn.riseup.net [10.0.1.112]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx0.riseup.net (Postfix) with ESMTPS id 4RFXV80DqLz9s50; Tue, 1 Aug 2023 11:11:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=riseup.net; s=squak; t=1690888272; bh=eRdP71C36l7nkGjLo4nIKgEk7hoBkyBEwuwAGOBQdjw=; h=Date:Subject:To:References:From:In-Reply-To:From; b=GY5tR3TW5KIoo2iCSaD+71T4pjvidozmGL3RPl4St2RvvfemYDQQiFznq0sTN8N88 70W0zBIYa0o4NIkGkLyUOxi0ip45QP1dVW83Ti/xUUJO7SgTIThVQaN0+gbVszGNEb Gy95kB6C/Pz8rOjwBA3dNUukPw7bc2c7boOgUVN4= X-Riseup-User-ID: 0D1B131E84150D41620B99AF9160AF462DEF218861B4EAEEA203A480B47DDC48 Original-Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews02-sea.riseup.net (Postfix) with ESMTPSA id 4RFXV71fWWzFpb1; Tue, 1 Aug 2023 11:11:10 +0000 (UTC) Content-Language: en-US In-Reply-To: <87jzuh2abv.fsf@cassou.me> Received-SPF: pass client-ip=198.252.153.6; envelope-from=mousebot@riseup.net; helo=mx0.riseup.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 01 Aug 2023 07:25:16 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:308235 Archived-At: This is a multi-part message in MIME format. --------------Mb0BByTxA8QJXpE0eodXYAUq Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit hi again, thanks for clarifying, i was confused by to-string/labelfn args. i came up with something based on what you suggested. with this the code in my pkg works fine, and the hierarchy.el tests all pass. to fix the tests for 'hierarchy-print', i found i had to also remove 'insert' inside the lambda that is the LABELFN arg for 'hierarchy-labelfn-indent', as insert returns nil and we need to return a string. hierarchy-print-line how handles 'insert'. i'm unsure about whether the let binding of to-string in -print is needed, and whether it should be the same as the let binding of labelfn in -print-line. perhaps there is some other code it run it against also? a patch is attached, formatted with magit from a magit diff. is that applicable, or is a different format required? kind regards, m On 7/30/23 08:16, Damien Cassou wrote: > Hi, > > mousebot writes: >> My initial query was also a way of asking if there is any (other) way >> in hierarchy.el of printing while respecting indentation. > > I think `hierarchy-print' is what you need. As you can see, the > implementation is very short so you can also copy/paste/adapt the code > of the function to your needs. > > >> I'm unsure as to how to refactor hierarchy-print, as I don't >> understand what you think it ought to do differently once >> hierarchy-print-line is added. > > The goal is to have hierarchy-print keep its current behavior but > leverage `hierarchy-print-line' for its implementation. I have something > like this in mind (not tested): > > (defun hierarchy-print (hierarchy &optional to-string) > "Insert HIERARCHY in current buffer as plain text. > > Use TO-STRING to convert each element to a string. TO-STRING is > a function taking an item of HIERARCHY as input and returning a > string. If nil, TO-STRING defaults to a call to `format' with \"%s\"." > (hierarchy-print-line > hierarchy > (hierarchy-labelfn-indent > (lambda (item _) (insert (funcall to-string item) "\n"))))) > >> I also wonder if it's perhaps a little confusing to have an arg >> 'to-string' that is now likely to be a call to >> `hierarchy-labelfn-indent`, whereas in `hierarchy-print` `to-string` >> is a function handed to hierarchy-labelfn-indent as an argument. > > > hierarchy has a notion of "labeling function" (parameters of this type > are called LABELFN). A labeling function is one that takes an item > object and an indentation level number as arguments and returns a > string. The `hierarchy-labelfn-indent' function takes a "labeling > function" as argument and returns another one. > > The function that `hierarchy-print' takes as parameter is called > TO-STRING instead of LABELFN because it only accepts an item object as > argument. > > >> In that case, perhaps -print-line should have the same call to >> `hierarchy-labelfn-indent` in its body, so that the to-string arg is >> of a similar form to -print? > > > I disagree. In `hierarchy-print-line', the second parameter should be > named LABELFN instead of TO-STRING (sorry for not saying that > earlier). The docstring should say that the function is not responsible > for indentation and that the user is free to call > `hierarchy-labelfn-indent' or `hierarchy-print' if s/he cares about > indentation. > > >> Finally, is there anything wrong with simply making the indent arg in >> the lambda in my original suggestion '&optional'? >> >> i.e. >> >> (defun hierarchy-print (hierarchy &optional to-string indent-string) >> (let ((to-string (or to-string (lambda (item) (format "%s" item))))) >> (hierarchy-map >> (hierarchy-labelfn-indent (lambda (item &optional indent) >> (insert (funcall to-string item indent) "\n")) >> indent-string) >> hierarchy))) > > The problem is not this lambda as `hierarchy-labelfn-indent' will always > call it with 2 arguments anyway. The problem is with the TO-STRING > parameter which used to be called with 1 argument but now requires > 2. For example, the code below works before the change you suggest but > triggers an error after: > > (hierarchy-print hierarchy (lambda (item) (format "-%s-"))) > > This is because the lambda here will now be called with 2 arguments but > it only declares 1 parameter. > > To go in the direction you suggest, we could use `func-arity' on > TO-STRING to detect how many arguments we have to pass and then call it > with either both ITEM and INDENT or only with ITEM. > >> Maybe sending 'nil' as an argument to funcall is bad news? > > > it's not the problem > > >> I don't really understand why the -print function shouldn't take an >> indent-string argument, but it's your pkg not mine. > > > this is not the problem either. The problem is passing this argument to > TO-STRING. I hope the above would have make this clearer. > -- some writing: https://anarchive.mooo.com an internets: https://pleasantlybabykid.tumblr.com/ . xmpp: mousebot@ghost.noho.st . gpg pub key: 0x582C8EAF0B0D77C9 fingerprint: DA24 B943 36EF C491 E22F A70B 582C 8EAF 0B0D 77C9 --------------Mb0BByTxA8QJXpE0eodXYAUq Content-Type: text/x-patch; charset=UTF-8; name="hierarchy001.patch" Content-Disposition: attachment; filename="hierarchy001.patch" Content-Transfer-Encoding: base64 ZGlmZiAtLWdpdCBhL2hpZXJhcmNoeS5lbCBiL2hpZXJhcmNoeS5lbAppbmRleCAyOGZhYjU1 Li5mYmZmZjA0IDEwMDY0NAotLS0gYS9oaWVyYXJjaHkuZWwKKysrIGIvaGllcmFyY2h5LmVs CkBAIC00NzUsMTAgKzQ3NSwzMSBAQCBpbmRlbnRhdGlvbiB2YWx1ZSAoYSBudW1iZXIpIGFz IGlucHV0LiIKIAogVXNlIFRPLVNUUklORyB0byBjb252ZXJ0IGVhY2ggZWxlbWVudCB0byBh IHN0cmluZy4gIFRPLVNUUklORyBpcwogYSBmdW5jdGlvbiB0YWtpbmcgYW4gaXRlbSBvZiBI SUVSQVJDSFkgYXMgaW5wdXQgYW5kIHJldHVybmluZyBhCi1zdHJpbmcuICBJZiBuaWwsIFRP LVNUUklORyBkZWZhdWx0cyB0byBhIGNhbGwgdG8gYGZvcm1hdCcgd2l0aCBcIiVzXCIuIgor c3RyaW5nLgorCitDYWxscyBgaGllcmFyY2h5LXByaW50LWxpbmUnIHdpdGggYGhpZXJhcmNo eS1sYWJlbGZuLWluZGVudCcgYXMKK3NlY29uZCBhcmd1bWVudC4iCiAgIChsZXQgKCh0by1z dHJpbmcgKG9yIHRvLXN0cmluZyAobGFtYmRhIChpdGVtKSAoZm9ybWF0ICIlcyIgaXRlbSkp KSkpCisgICAgKGhpZXJhcmNoeS1wcmludC1saW5lCisgICAgIGhpZXJhcmNoeQorICAgICAo aGllcmFyY2h5LWxhYmVsZm4taW5kZW50CisgICAgICAobGFtYmRhIChpdGVtIF8pCisgICAg ICAgIChmdW5jYWxsIHRvLXN0cmluZyBpdGVtKSkpKSkpCisKKyhkZWZ1biBoaWVyYX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