4

I am writing on a large LaTeX Document using Emacs with AUCTeX, RefTeX and Company for autocompletion. RefTeX for citations is working fine and finds my bibliography entries really fast. However, when company searches for labels it has to scan every file used in the project, this takes several seconds and is done every time I move the point in a \ref{} command. Also while scanning for files, I cannot make additional keyboard input, making me wait until scanning is finished every time. Here is the relevant part of my Emacs configuration:

;;------------------------------------------------------------------------------
;; Company -> Autocomplete
;;------------------------------------------------------------------------------

(use-package company
  :config
  (setq company-idle-delay 0
        company-minimum-prefix-length 4
        company-selection-wrap-around t)
  :bind
  (("C-<tab>" . company-complete))
  )

(global-company-mode t)

(use-package company-math :ensure t)
(use-package company-reftex :ensure t)
(use-package company-bibtex :ensure t)

(use-package math-symbol-lists)
(use-package company-math :ensure t)
(use-package company-reftex :ensure t)
(use-package company-bibtex :ensure t)  

(add-hook 'LaTeX-mode-hook
       (lambda()
       (make-local-variable 'company-backends)
       (setq company-backends
                         (append '((:separate company-reftex-citations
                                              company-bibtex
                                              company-reftex-labels))
                                    company-backends))
       (setq company-backends
                         (append '((:separate company-math-symbols-latex
                                              company-math-symbols-unicode
                                              company-latex-commands
                                              company-ipa-symbols-unicode
                                              company-dabbrev))
                                    company-backends))))

;;------------------------------------------------------------------------------
;; LaTeX
;;------------------------------------------------------------------------------

(setq-default TeX-output-dir "build")
(setq-default TeX-master nil)

;; RefTex
(add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; Enable RefTex in AUCTex Latex mode
(add-hook 'latex-mode-hook 'turn-on-reftex) ; Enable RefTex in Emacs Latex mode

;; Enable LaTeX Preview Pane
;(add-hook 'LaTeX-mode-hook 'latex-preview-pane-mode)
;(add-hook 'latex-mode-hook 'latex-preview-pane-mode)

(setq-default reftex-plug-into-AUCTeX t)
(setq-default reftex-default-bibliography '("./bibliography/Masterthesis.bib"))
(setq-default reftex-bibliography-commands '("bibliography" "nobibliography" "addbibresource"))
(setq reftex-use-external-file-finders t)
(setq reftex-external-file-finders
      '(("tex". "kpsewhich -format=.tex %f")
                ("bib". "kpsewhich -format=.bib %f")))

Can I speed up the company process for scanning labels? Or is this there at least a way to disable the automatic scanning?

I am new to Emacs and think my problem might result from a faulty configuration.

Drew
  • 75,699
  • 9
  • 109
  • 225
ls.ptr
  • 61
  • 3
  • 1
    I think the culprit is https://github.com/TheBB/company-reftex/commit/0fdd561ad9e9baa2af6e3aab05db0e9a9870d247. If you revert this change in `company-reftex.el` and recompile the file with `emacs-lisp-byte-compile` you should be fine. The drawback is that you need to run `reftex-parse-all` manually when needed. You could also run `reftex-parse-all` once and afterwards watch the input files for changes. But that is more complicated. – Tobias Sep 22 '22 at 08:21
  • See also https://github.com/TheBB/company-reftex/issues/11. They already suspected that the change could cause some performance penalty. A shame that they didn't didn't put `reftex-parse-all` into a customizable hook such that it could be optionally removed or replaced by something better. – Tobias Sep 22 '22 at 08:37
  • We are discussing making `reftex-parse-all` in `company-reftex-label-candidates` optional. Could you please check whether my suggestion, removing `reftex-parse-all` in `company-reftex-label-candidates`, solves your problem and report back afterwards? Thanks in advance. You could also participate there: https://github.com/TheBB/company-reftex/issues/11 – Tobias Sep 26 '22 at 04:57
  • Hello Tobias, thank you for your answers! I looked at the issue and I think you might be correct. Sadly, I will not be able to test your suggestion until next tuesday. But I will test it as soon as I can and answer here again! If this helps I will also tune in on the Github Issue. – ls.ptr Sep 27 '22 at 06:57
  • https://github.com/atreyasha added an option `company-reftex-labels-parse-all` which can be set to nil. That change is still only in a pull request. Please test this planned changeset by **temporarily replacing `reftex-parse-all` with `reftex-parse-one` in `company-reftex-label-candidates`**. Please report the results of this test here or at https://github.com/TheBB/company-reftex/pull/13 Thank you in advance. – Tobias Oct 02 '22 at 06:42
  • I tried your proposed change. Sadly, company still scans all my files as soon as the autocomplete inside a `\ref{}` command is starts. Do I have to reload the package manually? What I did was exchange `reftex-parse-all` with `reftex-parse-one`, close Emacs (I have no Emacs server running) and restart Emacs. Then I tested the change in my Latex document. Did I miss any steps? I am pretty new to Emacs and I am not sure, if the changes I made worked. – ls.ptr Oct 04 '22 at 09:08
  • When `company-reftex.el` is open, there should be an `Emacs-Lisp` menu. Click on `Byte-compile and Load` from that menu. Afterwards run again your test case. (You do not need to re-start Emacs.) From what you wrote in your last comment it looks like you run the old byte-compiled file `company-reftex.elc`. If we need to investigate futher we should open a chat channel. https://chat.stackexchange.com/ – Tobias Oct 04 '22 at 11:10
  • Okay that seemed to be the problem. I recompiled the file and now the behavior is different: Company will still scan all files, but will do so only once. After that it works fine. This is a nice solution for my problem, thank you very much! I will update my comment on github. How exactly should we continue with this question here on stackexchange? I would mark it as resolved, but there is no answer to it. – ls.ptr Oct 04 '22 at 12:30

1 Answers1

2

I think my question has been answered successfully. There has been no answers but comments to my question; so I thought to answer it myself and point other readers at the solution. If a mod can edit this or explain the best way to answer/mark this question, I would be happy to do so.

The performance problem was caused by the function call (reftex-parse-all) inside company-reftex-label-candidates, which gets called as soon as a \ref{} command is entered. There is a pull request on GitHub, which solves this problem. It is currently discussed whether a customizable hook should be added in order to change the parsing behavior easier. This can be found here.

Special thanks to Tobias, who pointed me towards the pull request and effectively solved my problem.

ls.ptr
  • 61
  • 3