Coder Social home page Coder Social logo

meliache / bibtex-actions Goto Github PK

View Code? Open in Web Editor NEW

This project forked from emacs-citar/citar

0.0 1.0 0.0 3.11 MB

A package to quickly find and act on bibtex entries using default Emacs completion; especially selectrum with embark.

License: GNU General Public License v3.0

Emacs Lisp 58.59% TeX 41.09% Shell 0.32%

bibtex-actions's Introduction

https://melpa.org/packages/bibtex-actions-badge.svg

BibTeX Actions

What is it?

This package turns bibtex-completion functions into completing-read-based Emacs commands. When used with selectrum, embark, and marginalia, it provides similar functionality to helm-bibtex and ivy-bibtex: quick filtering and selecting of bibliographic entries from the minibuffer, and the option to run different commands against them.

Here’s a screenshot with vertico, embark. and consult-completing-read-multiple.

images/vertico.png

Installation

Bibtex-actions is available for installation from MELPA.

In addition, the following packages are strongly recommended for the best experience.

  1. Vertico or Selectrum (completion interface)
  2. Orderless (completion style)
  3. Embark (contextual actions)
  4. Marginalia (annotations, and also candidate classification for Embark)
  5. Consult (enhanced interface for multiple candidate selection)

Configuration

Basic

This is the minimal configuration, and will work with any completing-read compliant vertical completion UI, like Vertico, Selectrum, or the built-in icomplete-vertical, with actions available via M-x commands.

(use-package bibtex-actions
  :bind (("C-c b" . bibtex-actions-insert-citation)
         :map minibuffer-local-map
         ("M-b" . bibtex-actions-insert-preset))
  :config
  ;; make sure to set this to ensure open commands work correctly
  (bibtex-completion-additional-search-fields '(doi url))
  (bibtex-completion-bibliography '("~/bib/references.bib")))

Since most of the command logic resides in bibtex-completion, that is where to look for different configuration options.

Embark

Highly recommended, this option adds embark, for contextual access to actions in the minibuffer and at-point.

(use-package bibtex-actions
  :bind (("C-c b" . bibtex-actions-insert-citation)
         :map minibuffer-local-map
         ("M-b" . bibtex-actions-insert-preset))
  :after embark
  :config
  ;; Make the 'bibtex-actions' bindings and targets available to `embark'.
  (add-to-list 'embark-target-finders 'bibtex-actions-citation-key-at-point)
  (add-to-list 'embark-keymap-alist '(bibtex . bibtex-actions-map))
  (add-to-list 'embark-keymap-alist '(citation-key . bibtex-actions-buffer-map))
  ;; Make sure to set this to ensure 'bibtex-actions-open-link' command works correctly.
  (bibtex-completion-additional-search-fields '(doi url))
  (bibtex-completion-bibliography '("~/bib/references.bib")))

;; use consult-completing-read for enhanced interface
(advice-add #'completing-read-multiple :override #'consult-completing-read-multiple)

When using this option, these actions are generic, and work the same across org, markdown, and latex modes.

Org-Cite

images/org-cite-embark-point.png

If you use org-mode, this is the best option. It includes the embark functionality above, but customizes menus and user-experience for org-cite. In other supported modes, it will work the same as the embark option above.

;; Set bibliography paths so they are the same.
(my/bibs '("~/bib/references.bib"))

(use-package bibtex-actions-org-cite
  :bind (("C-c b" . org-cite-insert)
         ("M-o" . org-open-at-point)
         :map minibuffer-local-map
         ("M-b" . bibtex-actions-insert-preset))
  :after (embark org oc bibtex-actions)
  :config
  ;; make sure to set this to ensure open commands work correctly
  (bibtex-completion-additional-search-fields '(doi url))
  (bibtex-completion-bibliography my/bibs)
  (org-cite-global-bibliography my/bibs))

;; Use consult-completing-read for enhanced interface.
(advice-add #'completing-read-multiple :override #'consult-completing-read-multiple)

If you prefer to have the embark menu open with org-open-at-point, you should set this variable.

(setq bibtex-actions-at-point-function 'embark-act)

You can invoke both embark-act and embark-dwim, however, independently of org-at-point, and in other modes such as latex-mode.

Oc-bibtex-actions does not provide activate functionality. For that, try org-ref-cite. Once installed and loaded, you can configure that like so:

(setq org-cite-activate-processor 'org-ref-cite-activate)

If you prefer to use a hydra-based in-buffer interface, you can also use that project’s “follow processor”:

(setq org-cite-activate-processor 'org-ref-cite-follow)

Test Script

The repository test directory also includes a script you can use to run this and associated packages in the emacs -Q sandbox. To do that, simply run ./run.sh from the test directory. By default, this will use selectrum as the completion system. If you would like to try vertico instead, just do M-x vertico-mode.

Rich UI

There are three sections of the browsing UI.

  1. The prefix, exploiting the affixation feature only available starting with Emacs 28, and holding the symbols to indicate the presence of PDFs or notes associated with the entries.
  2. The main display, which by default shows author, title, and date.
  3. The suffix, which by default shows citekey, reference type, and (if present) tags or keywords.

You can search against all of the above content. For the prefix, you can filter for associated PDFs or notes using has:pdf or has:note respectively (and at least with my setup, even the :p or :n shorthand).

images/ui-segments.png

You can configure both of the last two just as you do with bibtex-completion.

(setq bibtex-actions-template '((t . " ${title:*}")))
(setq bibtex-actions-template-suffix '((t . "          ${=key=:15}")))

Note: the asterisk signals to the formatter to use available space for the column. You should only use this on one field total, across the two templates, for the formatting to work correctly.

By default, this UI is plain text, but you can configure it to use icons instead.

images/rich-ui-icons.png

Here’s how to configure it to use all-the-icons:

(setq bibtex-actions-symbols
  `((pdf . (,(all-the-icons-icon-for-file "foo.pdf" :face 'all-the-icons-dred) .
            ,(all-the-icons-icon-for-file "foo.pdf" :face 'bibtex-actions-icon-dim)))
    (note . (,(all-the-icons-icon-for-file "foo.txt") .
            ,(all-the-icons-icon-for-file "foo.txt" :face 'bibtex-actions-icon-dim)))        
    (link . 
        (,(all-the-icons-faicon "external-link-square" :v-adjust 0.02 :face 'all-the-icons-dpurple) .
        ,(all-the-icons-faicon "external-link-square" :v-adjust 0.02 :face 'bibtex-actions-icon-dim)))))
;; Here we define a face to dim non 'active' icons, but preserve alignment
(defface bibtex-actions-icon-dim
    '((((background dark)) :foreground "#282c34")
     (((background light)) :foreground "#fafafa"))
     "Face for obscuring/dimming icons"
     :group 'all-the-icons-faces)

History and predefined searches

Bibtex-actions has functionality similar to the predefined search functionality in helm-bibtex and ivy-bibtex, but with a different implementation. Rather than create a new command with the search terms as argument, you just set the bibtex-actions-presets variable, and add the strings you want to access:

(setq bibtex-actions-presets '("one search string" "another search string"))

You then have two ways to access these strings from the completion prompt:

  1. by using M-n from the prompt, which will cycle through the strings
  2. by calling bibtex-actions-insert-preset with a keybinding, and then selecting the string

Bibtex-actions also preserves the history of your selections (see caveat below about multiple candidate selection though), which are also accessible in your completion UI, but by using M-p. You can save this history across sessions by adding bibtex-actions-history to savehist-additional-variables.

Pre-filtering entries

By default, bibtex-actions will, assuming you are using orderless or prescient to filter candidates, pre-filter entries for the following commands.

  1. bibtex-actions-open: pre-narrows the list to those which have associated pdf or links
  2. bibtex-actions-open-link: pre-narrows the list to those which have associated links
  3. bibtex-actions-open-pdf: -pre-narrows the list to those which have associated pdf(s)

That is, upon running the command, an initial-input value will be inserted to narrow the results. You can also delete that if you prefer to see the full list of candidates.

By default, pre-filtering of bibtex-actions-open-notes is off, because the command by default will create a new note if none is available, and therefore it makes sense to have access to your full library. But you can customize this to pre-filter if you prefer.

If you want to modify those values, or remove them entirely, you can set bibtex-actions-initial-inputs like so; in this case turning off pre-filtering for bibtex-actions-open-pdf:

(setq bibtex-actions-initial-inputs
  '((pdf    . nil)
    (note   . nil)
    (link   . "has:link")
    (source . "has:link\\|has:pdf"))

Refreshing the library display

Bibtex-actions uses a cache to speed up library display. This is great for performance, but means the data can become stale if you modify it.

The bibtex-actions-refresh command will reload the cache, and you can call this manually. You can also call any of the bibtex-actions commands with a prefix argument: C-u M-x bibtex-actions-insert-key.

Finally, another option is to add bibtex-completion-style proactive loading externally by using filenotify something like this:

;; Of course, you could also use `bibtex-completion-bibliography` here, but would need 
;; to adapt this if you specify multiple files.
(file-notify-add-watch 
  "/path/to/file.bib" '(change) 'bibtex-actions-refresh)

You can also extend this to do the same thing for your PDF files, or notes:

(file-notify-add-watch 
  bibtex-completion-library-path '(change) 'bibtex-actions-refresh)

(file-notify-add-watch 
  bibtex-completion-note-path '(change) 'bibtex-actions-refresh)

For additional configuration options on this, see the wiki.

Finding citation keys at point

bibtex-actions-at-point can find citation keys at point in org-mode buffer, latex-mode buffer, etc. To add support for other major modes or citation syntax, you can write a function (below is an example for org-cite) and add it to bibtex-completion-key-at-point-functions.

(defun bibtex-actions-get-key-org-cite ()
  "Return key at point for org-cite citation-reference."
  (when-let (((eq major-mode 'org-mode))
             (elt (org-element-context)))
    (pcase (org-element-type elt)
      ('citation-reference
       (org-element-property :key elt))
      ('citation
       (org-cite-get-references elt t)))))

Usage

You have a few different ways to interact with these commands.

Org-cite

Bibtex-actions includes org-cite integration in bibtex-actions-org-cite, which includes a processor with “follow” and “insert” capabilities.

The “insert processor” will use bibtex-actions-read to browse your library to insert and edit citations and citation references using the org-cite-insert command.

The “follow processor” provides at-point functionality accessible via the org-open-at-point command. By default, in org-mode with org-cite support, when point is on a citation or citation-reference, and you invoke org-open-at-point, it will run the default command, which is bibtex-actions-open.

M-x

Simply do M-x and select the command that you want, enter the terms to find the item you are looking for, and hit return. This runs the default action: the command you invoked.

Here’s the view, using marginalia for annotations.

images/m-x.png

A note on multiple candidate selection:

These commands do allow you to select multiple items, with two caveats:

  1. For this to work correctly, you must use the ampersand (&) as crm-separator to separate the candidates.
  2. We use long candidate strings, so if you use a completion system that requires you to TAB-complete, the experience is less-than-ideal.

Access an alternate action via embark-act

If while browsing you instead would rather edit that record, and you have embark installed and configured, this is where embark-act comes in. Simply input the keybinding for embark-act (in my case C-o), and select the alternate action.

Use embark-collect-snapshot

A final option, that can be useful: run embark-collect-snapshot (S) from embark-act. This will select the candidate subset, and open it in a separate buffer. From there, you can run the same options discussed above using embark-act (which is also bound to a in the collect buffer).

So, for example, say you are working on a paper. You hold the complete super-set of items you are interested in citing at some point in that buffer. From there, you can run different actions on the candidates at will, rather than search individually for each item you want to cite.

Use bibtex-actions-dwim

M-x bibtex-actions-dwim will run the default action on citation keys found at point directly. If you have embark installed, you use can embark-dwim instead for the same behavior, and embark-act for additional actions at-point.

If no citation key is found, the minibuffer will open for selection. You can disable this behavior by setting bibtex-actions-at-point-fallback to nil.

Comparisons

This is inspired by helm-bibtex and ivy-bibtex, but is based on completing-read. In comparison:

  • like helm-bibtex, but unlike ivy-bibtex, bibtex-actions has support for multi-selection of candidates
  • helm-bibtex and ivy-bibtex provide a single command, and the actions accessed from there; bibtex-actions provides all of its actions as standard commands, available from M-x, without a single entry point.
  • bibtex-actions is based on completing-read-multiple, with a single dependency, and works with different completion systems (though in practice is best supported in selectrum) and supporting packages that are completing-read compliant; helm-bibtex and ivy-bibtex are based on helm and ivy respectively.

Acknowledgements

The ideas in this project were initially worked out in a conversation with Maxime Tréca and Daniel Mendler. Daniel, author of consult and marginalia, helped us understand the possibilities of the new suite of completing-read packages, while Maxime came up with an initial prototype.

This code takes those ideas and re-implements them to fill out the feature set, and also optimize the code clarity and performance.

Along the way, Clemens Radermacher and Omar Antolín helped with some of the intricacies of completing-read and elisp.

And, of course, thanks to Titus von der Malburg for creating and maintaining bibtex-completion and helm-bibtex and ivy-bibtex.

bibtex-actions's People

Contributors

bdarcus avatar denismaier avatar ilupin avatar publicimageltd avatar titaniumbones avatar wenjie2wang avatar yevgnen avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.