Subject: Re: bug#75310: Eliminate obsolete alias warning

From: Yuan Fu <[email-masked]>
Date: Fri, 3 Jan 2025 23:33:25 -0800

> On Jan 3, 2025, at 7:27 AM, Eli Zaretskii <[email-masked]> wrote:
> 
>> Date: Fri, 3 Jan 2025 14:47:53 +0000 (GMT)
>> From: Peter Oliver <[email-masked]>
>> cc: [email-masked]
>> 
>> On Fri, 3 Jan 2025, Eli Zaretskii wrote:
>> 
>>> IOW, can you show a minimal recipe for reproducing the problem,
>>> starting from "emacs -Q"?
>> 
>> $ src/emacs -Q --batch --eval "(progn (message emacs-version) (require 'cl) (require 'treesit))"
>> 31.0.50
>> Package cl is deprecated
>> lisp/treesit.el: Warning: ‘loop’ is an obsolete alias (as of 27.1); use ‘cl-loop’ instead.
> 
> Thanks.
> 
> Yuan, I think it would be good to rename 'loop' there to some other
> name, do you agree?

I would definitely agree. I’ve always used cl-loop (and now I try to not use it for new code). The only instance of “loop” I can find in treesit.el is in a named-let in treesit-transpose-sexps wriiten by Theo, and it’s not the CL loop but a local function defined by named-let. I wanted to rewrite it so it doesn’t use named-let because it’s not very readable, but tbh I haven’t find the time to understand that function well enough to rewrite it (because it’s not very readable :-)

It could be that only I can’t understand it, but code should be simple enough that even I can understand ;-)

Once I rewrite it, there shouldn't be any occurrence of loop in treesit.el.

treesit-transpose-sexps looks like this:

(defun treesit-transpose-sexps (&optional arg)
  "Tree-sitter `transpose-sexps' function.
ARG is the same as in `transpose-sexps'.

Locate the node closest to POINT, and transpose that node with
its sibling node ARG nodes away.

Return a pair of positions as described by
`transpose-sexps-function' for use in `transpose-subr' and
friends."
  ;; First arrive at the right level at where the node at point is
  ;; considered a sexp. If sexp isn't defined, or we can't find any
  ;; node that's a sexp, use the node at point.
  (let* ((node (or (treesit-thing-at-point 'sexp 'nested)
                   (treesit-node-at (point))))
         (parent (treesit-node-parent node))
         (child (treesit-node-child parent 0 t)))
    (named-let loop ((prev child)
                     (next (treesit-node-next-sibling child t)))
      (when (and prev next)
        (if (< (point) (treesit-node-end next))
            (if (= arg -1)
                (cons (treesit-node-start prev)
                      (treesit-node-end prev))
              (when-let* ((n (treesit-node-child
                              parent (+ arg (treesit-node-index prev t)) t)))
                (cons (treesit-node-end n)
                      (treesit-node-start n))))
          (loop (treesit-node-next-sibling prev t)
                (treesit-node-next-sibling next t)))))))

Yuan