emacs-citar / citar

Emacs package to quickly find and act on bibliographic references, and edit org, markdown, and latex academic documents.
GNU General Public License v3.0
502 stars 54 forks source link

Citar capf not working #727

Closed deb75 closed 1 year ago

deb75 commented 1 year ago

Describe the bug

Citar completion at point is not working.

To Reproduce

  1. Open an org file
  2. Type "cite:@"
  3. M-x completion-at-point
  4. Nothing happens

Expected behavior

Should open a menu with possible completions

Emacs version:

windows 11 / mingw64 / emacs 30.0.60

Additional context

I have citar, org-cite configured and working as expected as for inserting citations. When loading the emacs init file, I noticed the error :

run-hooks: Autoloading file c:/Users/xxxx/.emacs.d/elpa/citar-20230129.1922/citar-capf.elc failed to define function citar-capf-mode

This error can be reproduced by M-x citar-capdf-mode The citar configuration is :

(use-package citar
  :custom
  (citar-library-paths ... )
  (org-cite-global-bibliography ... )
  (org-cite-insert-processor 'citar)
  (org-cite-follow-processor 'citar)
  (org-cite-activate-processor 'citar)
  (citar-bibliography org-cite-global-bibliography)
  :config
  (add-to-list 'citar-file-open-functions (cons "pdf" #'org-pdftools-open))
  (setq citar-library-file-extensions (list "pdf" "jpg")
        citar-file-additional-files-separator "-"
        citar-notes-paths ... )
  :hook ((org-mode markdown-mode tex-mode latex-mode reftex-mode) . citar-capf-mode)
  :bind
  (:map org-mode-map :package org ("C-c b" . #'org-cite-insert)))
bdarcus commented 1 year ago

Well, the error is because there's no longer any citar-capf-mode.

Did you have it working earlier?

That code in general needs work (see #660), which is largely why it's not currently documented.

And I don't use it, so am actually not sure what the right configuration should be.

Perhaps you can figure out how to modify the config so you remove the error, and get it to load correctly, and see if you still have the same issue?

bdarcus commented 1 year ago

You can see what the mode did here , as a basis for the config?

@mclearc - are you using citar-capf these days? Any tips?

mclearc commented 1 year ago

@mclearc - are you using citar-capf these days? Any tips?

hi @bdarcus -- I'm actually just using a very basic capf I wrote that just uses parsebib. At least in my use case I've found this is more reliable and responsive (as per this comment) than using citar (though I do still use citar -- just more for note-related stuff).

bdarcus commented 1 year ago

hi @bdarcus -- I'm actually just using a very basic capf I wrote that just uses parsebib. At least in my use case I've found this is more reliable and responsive (as per this comment) than using citar (though I do still use citar -- just more for note-related stuff).

Have you changed anything in the bibtex-capf activation code, @mclearc, the lack of which would explain what the OP is seeing (that the capf doesn't appear to activate; I see the same), that we might apply here?

EDIT: reading the bibtex-capf function, it does seem the same. Maybe a config issue?

The candidates/caching is a separate issue that I'm more comfortable with figuring out; I actually have an idea on that.

bdarcus commented 1 year ago

EDIT: reading the bibtex-capf function, it does seem the same. Maybe a config issue?

I see the issue here.

I had used this to config, from IELM:

(add-hook 'completion-at-point-functions #'citar-capf -90 t)

That last argument makes the hook local, and so to IELM, which means it doesn't work in org-mode.

@deb75 - can you try this? It should work:

(use-package citar-capf
  :init
  (add-to-list 'completion-at-point-functions #'citar-capf -90))

Is that right for the README @mclearc?

I guess we could add back the minor-mode and use a hook there, but maybe premature until we address #660.

mclearc commented 1 year ago
(use-package citar-capf
  :init
  (add-to-list 'completion-at-point-functions #'citar-capf -90))

Is that right for the README @mclearc?

EDIT: the activation issue usually stems from some other completion function running before citar gets its try. Hence the "90" weight. But note you're using add-to-list instead of add-hook, so here you should just prepend. So something like this.

(use-package citar-capf
:init
(add-to-list 'completion-at-point-functions #'citar-capf)

EDIT2: Note though that for the list addition to work it has to be added in every mode, which is why I use hooks.

bdarcus commented 1 year ago

Note though that for the list addition to work it has to be added in every mode, which is why I use hooks.

Oops; just pushed the earlier iteration.

How would I change it to use hooks, without adding a minor-mode? Just change add-to-list to add-hook?

mclearc commented 1 year ago

How would I change it to use hooks, without adding a minor-mode?

I assume by adding one for each mode:

(add-hook 'org-mode-hook (lambda () (add-hook 'completion-at-point-functions #'citar-capf -90)))

etc...

bdarcus commented 1 year ago

@mclearc how do you find the UX with the simple complete-on-the-citekey?

I can see it may have some appeal in this (buffer) context?

If yes, it seems like we could solve #660 pretty easily:

(completing-read "Test: " (citar-get-entries)) ; cached data, accounts for local bib files

... and then an annotation function (which is the only piece of content dynamically-generated; though maybe even this is problematic for a capf?) like:

  (defun citar-capf-annotate (key)
    "Annotate a citation KEY."
    (concat
     (truncate-string-to-width
      (or (citar-get-value "author" key)
          (citar-get-value "editor" key)
          "")
          20 nil 32)
     (citar-get-value "title" key)))

I started to play with this idea on the capf-refactor branch, though can't yet get it to work!

mclearc commented 1 year ago

@bdarcus that seems like it should work since I do something similar in my bibtex-capf completion function: https://github.com/mclear-tools/bibtex-capf/blob/406d7232b1460c79e70e0f76ba52c102e14afbb9/bibtex-capf.el#L104

bdarcus commented 1 year ago

That worked!

I was more worried about the padding between the candidate and the annotation, but that turned to be a non-issue.

bdarcus commented 1 year ago

OK, I'm closing this, as I'm pretty certain this was a config issue to begin with (not a bug), and I just merged #728.

image

Feel free to test it out, and open a separate issue if you have problems, @deb75?

EDIT: I've also made some other minor changes; see the config in the README.

bdarcus commented 1 year ago

Any luck with this @deb75?

deb75 commented 1 year ago

šŸ‘ šŸ‘ šŸ‘

Regards

deb75 commented 1 year ago

Actually, it suddenly stops working, answering "No match" although I use the same word which was working before.

In the message buffer, I get some errors :

Search failed: there is an unmatched expression somewhere or we are at the beginning/end of file. [4 times]
Error in post-command-hook (cursor-sensor--detect): (wrong-type-argument integer-or-marker-p nil)
redisplay--pre-redisplay-functions: (wrong-type-argument integer-or-marker-p nil)
Beginning of buffer

but I am not sure if they are all related to citar-capf.

bdarcus commented 1 year ago

~Oddly enough, I also can't get it to work now, though I get no error.~ Was just a syntax error in my config.

~OTOH, I can't get it work in markdown or latex.~

Your error, however, does look like it's related to cursor-sensor; not the capf.