reddit / emacs

Help understanding a specific `let` form in a function defined in `bibtex.el`

graduale · 2022-02-24 00:01
The built-in `bibtex-mode` provides a function `bibtex-next-field` which does what you'd expect: if we use "|" to represent point, calling `bibtex-next-field` from the following configuration: @article{key, author = {D|oe, Jane}, title = {A paper}, ... will take you to this configuration: @article{key, author = {D|oe, Jane}, title = {A paper|}, ... I'm trying to define a `bibtex-previous-field` function that does the reverse, so I decided to look into the definition of `bibtex-next-field`, which is this: (defun bibtex-next-field (begin &optional comma) "Move point to end of text of next BibTeX field or entry head. With prefix BEGIN non-nil, move point to its beginning. Optional arg COMMA is as in `bibtex-enclosing-field'. It is t for interactive calls." (interactive (list current-prefix-arg t)) (let ((bounds (bibtex-find-text-internal t nil comma)) end-of-entry) (if (not bounds) (setq end-of-entry t) (goto-char (nth 3 bounds)) (if (assoc-string (car bounds) '("@String" "@Preamble") t) (setq end-of-entry t) ;; BibTeX key or field (if (looking-at ",[ \t\n]*") (goto-char (match-end 0))) ;; end of entry (if (looking-at "[)}][ \t\n]*") (setq end-of-entry t)))) (if (and end-of-entry (re-search-forward bibtex-any-entry-maybe-empty-head nil t)) (goto-char (match-beginning 0))) (bibtex-find-text begin nil bibtex-help-message))) Unfortunately, I'm having trouble parsing this. What is the expression `end-of-entry` doing? I thought `let` expressions would take the following form: (let ((VAR-1 VALUE-1) (VAR-2 VALUE-2) ... (VAR-n VALUE-n)) BODY) but the first argument of `let` in the definition of `bibtex-next-field` seems to have a different form: ... (let ((bounds (bibtex-find-text-internal t nil comma)) end-of-entry) BODY ... Any help making sense of this would be much appreciated.
agrostis · 2022-02-24 00:19
(let (… foo …) <body>) is a shortcut for (let (… (foo nil) …) <body>) It's beside the point, but IMO, its use here is stylistically not quite elegant. This shortcut should only be used where the variable in question is explicitly assigned a value before it is ever used (to conceptually distinguish “unassigned” from “false”).
00-11 · 2022-02-24 00:25
You show this: (let ((bounds (bibtex-find-text-internal t nil comma)) end-of-entry) BODY ... (Presumably there's a closing paren after all that.) That binds `bounds` to the value of `(bibtex-find-text-internal t nil comma)`. And it binds `end-of-entry` to `nil`. The latter binding is equivalent to this one, which explicitly shows the `nil` value: `(end-of-entry nil)`. For better human readability (IMHO), I use the form with the explicit binding to `nil` when I want to make clear what the initial value of `end-of-entry` is. I use the form without the explicit binding to `nil` when I want to make clear that the initial value doesn't matter, because the `BODY` sets it. But the two have exactly the same behavior and meaning for Lisp, even if they look different, and so can be made to suggest different use cases to human readers.
graduale · 2022-02-24 00:41
I see. Thanks very much!
graduale · 2022-02-24 00:41
Thanks!
7890yuiop · 2022-02-25 09:49
~~The only code formatting method which works for all of reddit is to switch the reddit editor to markdown mode and indent the code by 4 spaces (you can use `M-4 C-x C-i` on a region followed by `M-x untabify` to achieve this in Emacs). Otherwise for lots of readers your message looks like this: https://old.reddit.com/r/emacs/comments/szw38z/help_understanding_a_specific_let_form_in_a/~~ Thanks!
graduale · 2022-02-25 18:27
Thanks for that. Fixed now, I hope!