2

This is my first time trying to write my own elisp code, so bear with me. I have the following cl-loop:

data contains the following:

((refs . [((file . "api/user.js") (start . 266) (end . 269)) ((file . "api/user.js") (start . 292) (end . 295)) ((file . "api/user.js") (start . 304) (end . 307)) ((file . "api/user.js") (start . 320) (end . 323)) ((file . "api/user.js") (start . 554) (end . 557)) ((file . "api/user.js") (start . 575) (end . 578))]) (type . "local") (name . "req"))
  (cl-loop for ref across (cdr (assq 'refs data))
           for start = (1+ (cdr (assq 'start ref))) and
           file = (expand-file-name (cdr (assq 'file ref)) (tern-project-dir))
           if (string= buffer-file-name file)
           if (let ((a 1))
                (print ref)
                (print start)
                (< start (point)))
           collect start into prev-refs
           else collect start into next-refs
           finally return (list file (sort prev-refs '>) (sort next-refs '<)))

which currently outputs the following:

((file . "api/user.js") (start . 266) (end . 269))

267

((file . "api/user.js") (start . 292) (end . 295))

267

((file . "api/user.js") (start . 304) (end . 307))

293

((file . "api/user.js") (start . 320) (end . 323))

305

((file . "api/user.js") (start . 554) (end . 557))

321

((file . "api/user.js") (start . 575) (end . 578))

555

This seems to be close, but I don't understand why start stays the same for the first 2 iterations of the loop (causing it to miss the start value in the last iteration). What I'm hoping to do is to define the variables to be used within the loop.

I've tried using with instead of the second for, but that didn't work. I've gotten it to work by using the full code for start and file in the various places where they're used, but that produces some quite ugly code.

btw, I just added that let statement only so I could execute those print statements.

Any help on getting the desired outcome would be greatly appreciated!

1 Answer 1

2

Thanks to bpalmer in #emacs for helping me solve this. The issue was with the and after for start...; instead, it needed to be another for (or as). I chose to use as instead of the other fors since it seems a bit more intuitive, but either will work.

The correct code is:

  (cl-loop for ref across (cdr (assq 'refs data))
           as start = (1+ (cdr (assq 'start ref)))
           as file = (expand-file-name (cdr (assq 'file ref)) (tern-project-dir))
           if (string= buffer-file-name file)
           if (let ((a 1))
                (print ref)
                (print start)
                (< start (point)))
           collect start into prev-refs
           else collect start into next-refs
           finally return (list file (sort prev-refs '>) (sort next-refs '<)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.