unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* hierarchy-print doesn't allow sending indent-string arg to hierarchy-map
@ 2023-07-26 16:16 Damien Cassou
  2023-07-29 13:33 ` mousebot
  0 siblings, 1 reply; 7+ messages in thread
From: Damien Cassou @ 2023-07-26 16:16 UTC (permalink / raw)
  To: mousebot, emacs-devel

Hi,

you are right that something isn't good enough. The fix you suggest
fixes the problem. Unfortunately, it is backward incompatible: all
usages of `hierarchy-print' passing a custom value for TO-STRING will
now fail because TO-STRING should now read a second argument.
Additionally, I think it shouldn't be the responsibility of
`hierarchy-print' to take care of the indentation.

Had I been smart enough, I would probably have written `hierarchy-print'
this way instead:

  (defun hierarchy-print (hierarchy &optional to-string)
    (let ((to-string (or to-string (lambda (item) (format "%s" item)))))
      (hierarchy-map
       (lambda (item indent)
         (insert (funcall to-string item ident) "\n"))
       hierarchy)))

That is:
- TO-STRING takes 2 parameters

- indentation should be decided by the user through a dedicated value
  TO-STRING value, e.g., with
  (hierarchy-labelfn-indent (lambda (item) (format "%s" item)))

But this implementation also breaks backward compatibility. What I
suggest:

1. Implement `hierarchy-print-line' with the implementation above (this
   way we have a good implementation anyone can use)

2. Refactor and simplify `hierarchy-print` to avoid duplication with
   `hierarchy-print-line' but keeping backward compatibility.

What do you think? Would you mind sending a patch?

(please keep me on CC)

Best

-- 
Damien Cassou

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill



^ permalink raw reply	[flat|nested] 7+ messages in thread
* hierarchy-print doesn't allow sending indent-string arg to hierarchy-map
@ 2023-07-12 13:43 mousebot
  2023-07-13  3:38 ` Michael Heerdegen
  0 siblings, 1 reply; 7+ messages in thread
From: mousebot @ 2023-07-12 13:43 UTC (permalink / raw)
  To: emacs-devel

hi emacs,

`hierarchy-labelfn-indent' takes an optional second argument `indent-string'.

`hierarchy-print' calls `hierarchy-labelfn-indent', but doesn't allow providing it with an `indent-string' arg.

it also provides no second argument to the labelfn arg of `hierarchy-labelfn-indent'.
  
hierarchy-print looks like this:

```
(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\"."
   (let ((to-string (or to-string (lambda (item) (format "%s" item)))))
     (hierarchy-map
      (hierarchy-labelfn-indent (lambda (item _)
                                  (insert (funcall to-string item) "\n")))
      hierarchy)))
```

i'm working on a client and i'm using a copy of `hierarchy-print' that looks like this:

```
(defun hierarchy-print (hierarchy &optional to-string indent-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\"."
   (let ((to-string (or to-string (lambda (item) (format "%s" item)))))
     (hierarchy-map
      (hierarchy-labelfn-indent (lambda (item indent) ; add indent
                                  (insert (funcall to-string item indent) "\n")) ; add indent
                                indent-string) ; add indent-string
      hierarchy)))
```

i provide optional arg `indent-string' and hand it to `hierarchy-map'. i also provide a second 'indent' arg to the lambda that is the first arg for `hierarchy-labelfn-indent', indent is then given as the last arg of `funcall'. this allows me to print indented tree structures, which i was unable to achieve otherwise.

is there a reason this isn't the way `hierarchy-print' is coded? is there perhaps another way to achieve the same result? i have little experience with `hierarchy.el'.

regards,
m



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2023-08-02 15:48 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-26 16:16 hierarchy-print doesn't allow sending indent-string arg to hierarchy-map Damien Cassou
2023-07-29 13:33 ` mousebot
2023-07-30  6:16   ` Damien Cassou
2023-08-01 11:11     ` mousebot
2023-08-02 15:48       ` Damien Cassou
  -- strict thread matches above, loose matches on Subject: below --
2023-07-12 13:43 mousebot
2023-07-13  3:38 ` Michael Heerdegen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).