Iranipride · 2022-11-25 14:10
ALLCAPSNOBRAKES · 2022-11-25 17:46
interesting article. standard practice in Lisp for scope-local variables would be to use a `let` binding instead of `setq-local` or `setq`. in fact, `setq-local` is actually intended to set a variable's value within a specific *buffer*, not within the scope of a function.
Iranipride · 2022-11-26 00:43
Reading the documentation on let shows me that it was exactly what I should have used. Thank you for letting me know!
arthurno1 · 2022-11-26 18:59
> (directory-files dir nil
> (concat "\." filetype "$"))
I think you can just type in many cases:
(directory-files dir nil filetype)
If you pass in type like "jpeg" "pdf" etc, but if you have some cases where suffix can appear elsewhere in a filename, and need to be sure you are looking at the end of the string, then (concat filetype "$") is enough.
I think you can also just use directory-files directly, without mapcar-ing and creating intermediate list of prefixes. You are throwing away all but the last prefix, so why not just take the last prefix, then? :). Something like this:
(substring
(car
(nreverse
(directory-files "./" nil (concat "pdf" "$")))) 0 5)
I am not sure, I don't have a directory with such names, and am too lazy to test it, but I think it should work. I have also switched last for the nreverse. Since you need just one last element, you can just reverse the list.
> First I set a local variable using setq-local to avoid polluting the global space.
This is not local *that* way. You are still "polluting" the namespace with the symbol name, but the value for the variable will have [buffer-local binding](https://www.gnu.org/software/emacs/manual/html_node/elisp/Buffer_002dLocal-Variables.html), which means each buffer will have its own value for the variable, but the name is still globally visible, and there is also a default value which is used in the case where local value is not set. So buffer local binding is a bit more expensive in computational terms. If you type C-h f setq-local RET, you can read the docs:
> Make each VARIABLE buffer-local and assign to it the corresponding VALUE.
You just probably want here is to [let-bind your variable](https://www.gnu.org/software/emacs/manual/html_node/elisp/Local-Variables.html), something like this:
(defun my/get-next-jd-number-in-jd-dir (dir filetype)
"Returns the next JD number for a given filetype in a JD directory."
(let ((jd-number
(substring
(car
(nreverse
(directory-files "./" nil (concat "pdf" "$")))) 0 5)))
(concat
(substring jd-number 0 3)
(format "%02d" (1+ (string-to-number
(substring jd-number 3 nil)))))))
> Since I am not at all proficient in Emacs Lisp
Me neither, so test everythign first, I have probably made some mistake since I have just copy-pasted in the browser.
Iranipride · 2022-11-27 12:21
Thank you so much for taking the time, I appreciate it. I always struggle with the fear that my writing and coding is *not good enough*, then I get feedback like this which immediately points out the fallacy in those fears!
arthurno1 · 2022-11-27 12:29
> I get feedback like this which immediately points out the fallacy in those fears
You mean I'm worse than you? :-) Probably true! Anyhow, I am glad if the feedback helps :).
Iranipride · 2022-11-27 13:49
Haha, not at all! Rather, that people want to offer their advice instead of being bringing you down. Your feedback is testament to the good in strangers and that sharing can inspire someone to give something back, like your thoughts on the matter.
arthurno1 · 2022-11-27 14:32
I was just kidding; I understand what you mean. There is no reason to bring you down. I am myself learning Emacs Lisp, and I understand there is *a lot* to get into, so it is just normal to net be super informed about all the parts. If you are not a programmer, then some things are easy to overlook; I make myself lots of stupid, simple mistakes all the time. You shared your thoughts, and you will probably share more useful thoughts in the future, so it is just normal to help you if possible.