[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: hierarchy-print doesn't allow sending indent-string arg to hierarchy

From: Damien Cassou
Subject: Re: hierarchy-print doesn't allow sending indent-string arg to hierarchy-map
Date: Sun, 30 Jul 2023 08:16:20 +0200


mousebot <mousebot@riseup.net> 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\"."
      (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

> 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

> 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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]