Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use org-capture for bibtex-completion-notes? #260

Open
leezu opened this issue Sep 24, 2018 · 9 comments
Open

Use org-capture for bibtex-completion-notes? #260

leezu opened this issue Sep 24, 2018 · 9 comments

Comments

@leezu
Copy link

leezu commented Sep 24, 2018

org-capture is widely used and "more powerful" than the current bibtex-completion-edit-notes setup. For example, the new notes can be organized in a datetree and the template code supports more things (such as timestamps).

I wrote below replacement for bibtex-completion-edit-notes. I don't suggest merging it just like this, but it may be a good starting point in case you are interested in migrating to some org-capture based setup. Compared to the original bibtex-completion-edit-notes it also uses indirect buffers instead of modifying the outline and narrow state of the original notes buffer. Only the single notes file case is supported. If a note already exist, the behavior is similar to the existing function (but using indirect buffer). For new notes, org-capture-templates is temporarily overwritten based on bibtex-completion-notes-template-one-file and then org-capture is invoked.

(defun bibtex-completion-edit-notes (keys)
       "Open the notes associated with the selected entry or create new via org-capture."
       (require 'org-capture)
       (dolist (key keys)
         (let* ((entry (bibtex-completion-get-entry key))
                (year (or (bibtex-completion-get-value "year" entry)
                          (car (split-string (bibtex-completion-get-value "date" entry "") "-"))))
                (entry (push (cons "year" year) entry))
                (buffer (generate-new-buffer-name "bibtex-notes")))
           (with-current-buffer (make-indirect-buffer  ;; TODO: buffer is never deleted
                                 (find-file-noselect bibtex-completion-notes-path)
                                 buffer t)  ;; clone t
             (widen)
             (outline-show-all)
             (goto-char (point-min))
             (if (re-search-forward (format bibtex-completion-notes-key-pattern (regexp-quote key)) nil t)
                                        ; Existing entry found:
                 (when (eq major-mode 'org-mode)
                   (org-narrow-to-subtree)
                   (goto-char (point-min))
                   (outline-show-all)
                   (switch-to-buffer-other-window buffer))
                                        ; Create a new entry:
               (let ((org-capture-templates
                      '(("bibtex" "helm-bibtex"
                         entry
                         (file+olp+datetree bibtex-completion-notes-path)
                         "%(s-format bibtex-completion-notes-template-one-file 'bibtex-completion-apa-get-value entry)"))))
                 (org-capture nil "bibtex")))))))
@jagrg
Copy link
Contributor

jagrg commented Oct 5, 2018

I like the idea.

@tmalsburg
Copy link
Owner

tmalsburg commented Oct 5, 2018

Oh, I had written a comment earlier but apparently misclicked when I submitted. Sorry about this.

I like this, too, especially the use of indirect buffers. Wasn't aware they existed when I wrote the notes code. I had considered using org-capture but at the time I found some reason against that. Now I can't recall what that reason was.

What do you think needs to be changed before this feature can be merged? Would you mind making a PR?

@leezu
Copy link
Author

leezu commented Oct 7, 2018

Thanks for the feedback on this suggestion. I can think of the following points:

  • Support multi-file setup.
  • Improving the indirect buffer setup: The indirect buffer is used to search if the entry already exists. If yes, the buffer is shown (and should not be deleted automatically). Otherwise org-capture is started and the indirect buffer is not needed anymore. Further the naming of the indirect buffers could be improved.
  • Documentation
  • Changing default templates to include %? at the position where the cursor shall be placed by org-capture.
  • Integrate/Adapting helm-bibtex-notes-mode? When editing an existing note it may be useful. @jagrg as you contributed helm-bibtex-notes-mode, do you have some ideas what to do about it? If you have time you may try placing above code snippet in your init.el and point out any issues you currently see.

@tmalsburg What do you think? I can open a PR based on above code, but I may not be able to integrate all the points currently. An open PR may still be a better basis for discussion than a code-snippet in some issue.

@jagrg
Copy link
Contributor

jagrg commented Oct 8, 2018

Integrate/Adapting helm-bibtex-notes-mode?

I'd say keep the notes-mode for editing existing entries, and use org-capture for creating new ones. We should also make org-capture-templates customizable. Not everyone will want to organise their notes by date. The current code inserts new entries at the end of the file so maybe we should keep this as the default? Other than that, I would suggest adding bibtex-completion-clean-string when creating new entries.

@tmalsburg
Copy link
Owner

Hi both, I don't have time to work on this at this time. The term is starting and I have to prepare lectures. However if someone wants to make a PR, I'd be happy to give feedback.

@jagrg
Copy link
Contributor

jagrg commented Oct 17, 2018

I tried implementing this feature in #263.

@leezu
Copy link
Author

leezu commented Apr 5, 2019

Thanks @jagrg !

Regarding the multi-file setup: This could also be handled by org-capture if the target file of the entry is set dynamically. For example,

("bibtex" "helm-bibtex" entry (file new-zettel-file)
                         (file get-bibtex-completion-notes-path)
                         "%(s-format bibtex-completion-notes-template-multiple-files 'bibtex-completion-apa-get-value entry)")

where get-bibtex-completion-notes-path is a function returning the file name of the single file (to be) associated with the current bibtex key. If bibtex-completion-notes-template-multiple-files is not a valid org entry, ("bibtex" "helm-bibtex" plain ...) could be used instead.

@tmalsburg
Copy link
Owner

Neat idea!

If bibtex-completion-notes-template-multiple-files is not a valid org entry, ("bibtex" "helm-bibtex" plain ...) could be used instead.

I'm not sure I understand this bit. Could you explain?

@leezu
Copy link
Author

leezu commented Apr 18, 2019

The third argument in the capture template can be entry or plain. If it is entry, but the template does not represent a valid org-mode headline / entry, an error message will be shown. plain allows to use any kind of template as capture template.

The documentation of org-capture-templates:

Documentation:
Templates for the creation of new entries.

Each entry is a list with the following items:
[...]

type         The type of entry.  Valid types are:
               entry       an Org node, with a headline.  Will be filed
                           as the child of the target entry or as a
                           top-level entry.
               item        a plain list item, will be placed in the
                           first plain list at the target
                           location.
               checkitem   a checkbox item.  This differs from the
                           plain list item only is so far as it uses a
                           different default template.
               table-line  a new line in the first table at target location.
               plain       text to be inserted as it is.


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants