From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Damien Cassou Newsgroups: gmane.emacs.devel Subject: Re: hierarchy-print doesn't allow sending indent-string arg to hierarchy-map Date: Sun, 30 Jul 2023 08:16:20 +0200 Message-ID: <87jzuh2abv.fsf@cassou.me> References: <87a5vi4piz.fsf@cassou.me> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13590"; mail-complaints-to="usenet@ciao.gmane.io" To: mousebot , emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Jul 30 12:18:49 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 1qQ3Vg-0003KP-Cv for ged-emacs-devel@m.gmane-mx.org; Sun, 30 Jul 2023 12:18:48 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qQ2er-0003gc-2O; Sun, 30 Jul 2023 05:24:13 -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 1qPzjL-0006Qb-Dk for emacs-devel@gnu.org; Sun, 30 Jul 2023 02:16:39 -0400 Original-Received: from mail.choca.pics ([80.67.172.235]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qPzjI-0004QP-P5 for emacs-devel@gnu.org; Sun, 30 Jul 2023 02:16:38 -0400 Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id F1DD318193F40; Sun, 30 Jul 2023 08:16:24 +0200 (CEST) Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10032) with ESMTP id gJX77yhYCxaI; Sun, 30 Jul 2023 08:16:24 +0200 (CEST) Original-Received: from localhost (localhost.localdomain [IPv6:::1]) by mail.choca.pics (Postfix) with ESMTP id 009D118193F41; Sun, 30 Jul 2023 08:16:23 +0200 (CEST) X-Virus-Scanned: amavisd-new at choca.pics Original-Received: from mail.choca.pics ([IPv6:::1]) by localhost (mail.choca.pics [IPv6:::1]) (amavisd-new, port 10026) with ESMTP id tIeCL5jQadxa; Sun, 30 Jul 2023 08:16:23 +0200 (CEST) Original-Received: from localhost (unknown [IPv6:2a01:cb19:ca5:9400:e443:3f0d:ed50:c0fa]) by mail.choca.pics (Postfix) with ESMTPSA id 1B07D18193F40; Sun, 30 Jul 2023 08:16:23 +0200 (CEST) In-Reply-To: Received-SPF: pass client-ip=80.67.172.235; envelope-from=damien@cassou.me; helo=mail.choca.pics 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, SPF_HELO_NONE=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: Sun, 30 Jul 2023 05:24:11 -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:308209 Archived-At: 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. -- Damien Cassou "Success is the ability to go from one failure to another without losing enthusiasm." --Winston Churchill