emacs-devel
[Top][All Lists]
Advanced

[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: mousebot
Subject: Re: hierarchy-print doesn't allow sending indent-string arg to hierarchy-map
Date: Tue, 1 Aug 2023 13:11:08 +0200

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 <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\"."
   (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

Attachment: hierarchy001.patch
Description: Text Data


reply via email to

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