Subject: bug#65462: 13.2; preview clears overlays when killing an indirect buffer

From: Paul Nelson <[email-masked]>
Date: Wed, 23 Aug 2023 01:17:07 +0200

Take any latex document with sufficiently many equations.  After cloning an
indirect buffer ("C-x 4 c") and killing that indirect buffer (e.g., "C-x 4
0"), many of the overlays in the original buffer become invisible.  Indeed,
one can check that the relevant subfolder of =_region_.prv= is rendered
empty, so the overlays that remain visible are just those that have been
cached.

This is undesirable: if one attempts to simultaneously use preview and
indirect buffers, one eventually has to regenerate all previews after
killing any indirect buffer.

The issue is that killing the indirect buffer calls
~preview-kill-buffer-cleanup~, which in turn calls ~preview-clearout~.

In my local version, I wrapped ~preview-kill-buffer-cleanup~ with ~(unless
(buffer-base-buffer) ...)~, as follows, so that it doesn't clearout
indirect buffers.  This has resolved the issue for me, but I imagine it
could be useful for others who use both AUCTeX and indirect buffers.

#+begin_src elisp
(defun preview-kill-buffer-cleanup (&optional buf)
  "This is a cleanup function just for use in hooks.
Cleans BUF or current buffer.  The difference to
`preview-clearout-buffer' is that previews
associated with the last buffer modification time are
kept."
  (with-current-buffer (or buf (current-buffer))
    (unless (buffer-base-buffer)    ; do not clearout indirect buffers
      (save-restriction
(widen)
(preview-clearout (point-min) (point-max) (visited-file-modtime))))))
#+end_src

Emacs  : GNU Emacs 30.0.50 (build 2, x86_64-apple-darwin21.6.0, NS
appkit-2113.60 Version 12.6 (Build 21G115))
 of 2023-08-11
Package: 13.2

current state:
==============
(setq
 AUCTeX-date "2023-08-18"
 window-system 'ns
 LaTeX-version "2e"
 TeX-style-path '("/usr/local/var/auctex"
"/Users/paulnelson/.emacs.d/elpaca/builds/auctex/auctex/style"
                  "/Users/paulnelson/.emacs.d/auctex/auto"
"/Users/paulnelson/.emacs.d/auctex/style" "auto" "style")
 TeX-auto-save t
 TeX-parse-self t
 TeX-master "~/doit/preview-master.tex"
 TeX-command-list '(("TeX" "%(PDF)%(tex) %(file-line-error) %`%(extraopts)
%S%(PDFout)%(mode)%' %(output-dir) %t"
                     TeX-run-TeX nil (plain-tex-mode ams-tex-mode
texinfo-mode) :help "Run plain TeX")
                    ("LaTeX" "%`%l%(mode)%' %T" TeX-run-TeX nil (latex-mode
doctex-mode) :help "Run LaTeX")
                    ("Makeinfo" "makeinfo %(extraopts) %(o-dir) %t"
TeX-run-compile nil (texinfo-mode) :help
                     "Run Makeinfo with Info output")
                    ("Makeinfo HTML" "makeinfo %(extraopts) %(o-dir) --html
%t" TeX-run-compile nil (texinfo-mode) :help
                     "Run Makeinfo with HTML output")
                    ("AmSTeX" "amstex %(PDFout) %`%(extraopts) %S%(mode)%'
%(output-dir) %t" TeX-run-TeX nil (ams-tex-mode)
                     :help "Run AMSTeX")
                    ("ConTeXt" "%(cntxcom) --once --texutil %(extraopts)
%(execopts)%t" TeX-run-TeX nil (context-mode)
                     :help "Run ConTeXt once")
                    ("ConTeXt Full" "%(cntxcom) %(extraopts) %(execopts)%t"
TeX-run-TeX nil (context-mode) :help
                     "Run ConTeXt until completion")
                    ("BibTeX" "bibtex %(O?aux)" TeX-run-BibTeX nil
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode context-mode) :help "Run BibTeX")
                    ("Biber" "biber %(output-dir) %s" TeX-run-Biber nil
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode) :help "Run Biber")
                    ("Texindex" "texindex %s.??" TeX-run-command nil
(texinfo-mode) :help "Run Texindex")
                    ("Texi2dvi" "%(PDF)texi2dvi %t" TeX-run-command nil
(texinfo-mode) :help "Run Texi2dvi or Texi2pdf")
                    ("View" "%V" TeX-run-discard-or-function t t :help "Run
Viewer")
                    ("Print" "%p" TeX-run-command t t :help "Print the
file")
                    ("Queue" "%q" TeX-run-background nil t :help "View the
printer queue" :visible TeX-queue-command)
                    ("File" "%(o?)dvips %d -o %f " TeX-run-dvips t
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode) :help "Generate PostScript file")
                    ("Dvips" "%(o?)dvips %d -o %f " TeX-run-dvips nil
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode) :help
                     "Convert DVI file to PostScript")
                    ("Dvipdfmx" "dvipdfmx -o %(O?pdf) %d" TeX-run-dvipdfmx
nil
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode) :help
                     "Convert DVI file to PDF with dvipdfmx")
                    ("Ps2pdf" "ps2pdf %f %(O?pdf)" TeX-run-ps2pdf nil
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode) :help
                     "Convert PostScript file to PDF")
                    ("Glossaries" "makeglossaries %(d-dir) %s"
TeX-run-command nil
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode) :help
                     "Run makeglossaries to create glossary file")
                    ("Index" "makeindex %(O?idx)" TeX-run-index nil
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode) :help
                     "Run makeindex to create index file")
                    ("upMendex" "upmendex %(O?idx)" TeX-run-index t
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode) :help
                     "Run upmendex to create index file")
                    ("Xindy" "texindy %s" TeX-run-command nil
                     (plain-tex-mode latex-mode doctex-mode ams-tex-mode
texinfo-mode) :help
                     "Run xindy to create index file")
                    ("Check" "lacheck %s" TeX-run-compile nil (latex-mode)
:help "Check LaTeX file for correctness")
                    ("ChkTeX" "chktex -v6 %s" TeX-run-compile nil
(latex-mode) :help "Check LaTeX file for common mistakes")
                    ("Spell" "(TeX-ispell-document \"\")" TeX-run-function
nil t :help "Spell-check the document")
                    ("Clean" "TeX-clean" TeX-run-function nil t :help
"Delete generated intermediate files")
                    ("Clean All" "(TeX-clean t)" TeX-run-function nil t
:help
                     "Delete generated intermediate and output files")
                    ("Other" "" TeX-run-command t t :help "Run an arbitrary
command"))
 )