reddit / emacs

Script works only when stepping in edebug

Deep-Fox6860 · 2022-02-18 09:19
Hi, I'm writing some functionality in elisp which only works when I step through the expressions using Edebug and "space" but it fails when running in a regular manner. I know where the script fails in terms of a specific line, I just don't know of a mechanism in which the outcome is different due to stepping vs. regular run. Here is the code: (defun df/cite () (interactive) (defvar df/cite-title "") (defvar df/cite-buffer (current-buffer)) (org-ref-insert-cite-link) (org-ref-open-citation-at-point) (setq df/cite-title (bibtex-autokey-get-field "title" bibtex-autokey-titleword-change-strings)) (switch-to-buffer df/cite-buffer) (end-of-visual-line) (backward-char) (insert (format "[%s]" df/cite-title)) (end-of-visual-line)) The code lets you insert an org-ref citation and uses the article's title (from the bibtex) entry as the org link description. The script works line by line in edebug but fails as a whole on the line running "open-citation-at-point". Not sure why.
nv-elisp · 2022-02-18 15:22
You have a syntax issue on line three
Deep-Fox6860 · 2022-02-18 16:03
What do you mean?
nv-elisp · 2022-02-18 16:26
I was going off the code you posted.
Deep-Fox6860 · 2022-02-18 16:43
Added the code; the post you mention is outdated.
nv-elisp · 2022-02-18 16:53
> The script works line by line in edebug but fails as a whole on the line running "open-citation-at-point". Be specific about what you expect to happen and what is actually happening. Is there an error, warning, nothing happens, etc.
Deep-Fox6860 · 2022-02-18 17:04
`org-ref-open-citation-at-point` can't find a link at the point. I'm printing the point and current buffer, they're correct but somehow it sees different text then what's displayed. Maybe it's org doing some overlays, text properties on the newly inserted link. When edebugging `org-ref-open-citation-at-point`, it works. Very strange.
arthurno1 · 2022-02-18 19:36
Are you really sure you wish to define global vars from within your function? (defvar df/cite-title "") (defvar df/cite-buffer (current-buffer)) Also, you don't need to switch to current-buffer, you are already in "current-buffer". Now I don't know if your intention is to switch to/from some other buffer, but the code does not have any effect as it is written now: (switch-to-buffer df/cite-buffer) For another thing, are you really sure you wish to insert at the end of a visual line? That can give you some surprising results. For example, make your window/frame smaller so that line wraps. Minimize frame horizontally so that the window ends at the end of the dash after the word autokey in the following line: (bibtex-autokey-get-field. Now run M-: (end-of-visual-line) and then restore your frame. The cursor should be at the beginning of the word 'get' after the dash. So you would insert your citation in the middle of the line. Is that really what you intended? The example with the above comment: (defun df/cite () (interactive) (org-ref-insert-cite-link) (org-ref-open-citation-at-point) (let ((df/cite-title (df/cite-title (bibtex-autokey-get-field "title" bibtex-autokey-titleword-change-strings)))) (end-of-line) (insert (format "[%s]" df/cite-title)) (end-of-line))) Also, are you really sure you wish to move the point? You probably wish to execute the last three line in a [save-excursion](https://www.gnu.org/software/emacs/manual/html_node/elisp/Excursions.html) form, but I don't know what is your intention, so I am just guessing here. (defun df/cite () (interactive) (org-ref-insert-cite-link) (org-ref-open-citation-at-point) (let ((df/cite-title (df/cite-title (bibtex-autokey-get-field "title" bibtex-autokey-titleword-change-strings)))) (save-excursion (end-of-line) (insert (format "[%s]" df/cite-title)))))
Deep-Fox6860 · 2022-02-18 19:56
Agree with everything. Variables were added because `save-excursion` didn't work, at least that's what I thought at first. Visual vs. logical, doesn't really matter. You do need to go back one character because you want to be here: `[[cite]|]` when you insert a description (org-link format): `[[cite][desc]]` Finally, you do need to go back to the original buffer because `org-ref-open-citation-at-point` jumps to the `bib` file's buffer containing the bibliography item details.
arthurno1 · 2022-02-18 22:27
> Variables were added because save-excursion didn't work save-excursion works very well. I promise. > Visual vs. logical, doesn't really matter. It does. Try what I wrote with frame. I don't know how your text look like, maybe you never have lines longer than what fits in a window width, but you are putting yourself at risk to mess up text in case you might perform the function on a wrapped line. It may be a rare case, but there is no reason to put yourself at risk. > You do need to go back one character because you want to be here ... Ah allright than. I didn't really run the code :-). > org-ref-open-citation-at-point jumps to the bib file's buffer containing the bibliography item details. I should have checked what it does, sorry for that one. But save-excursion should have you covered there, it restores both the point and the current buffer.