Al Haji-Ali <abdo.haji....@gmail.com> writes: > On 27/08/2025, Uwe Brauer wrote: >> BTW have tried it out together with preview-auto (by Paul >> Nelson)? > > I have. I found preview-auto to be overly complicated and too prone to > fail in my tests, so I implemented my own automatic previewing > mechanism which turned out to be much simpler. The code above enables > this.
1. Any feedback on failure cases for preview-auto would be very welcome. 2. I noticed that most of the "automatic previewing" code in preview-point is derived or copied verbatim from preview-auto (see the excerpts below). That code is FSF copyrighted and GPLv3 licensed, so if I understand correctly, you're free to modify and use it, but must preserve copyright and license notices in the source. I understand that your package is new and you may have just not had the chance to do this. I would also be happy to work together on any part of this. Indeed, I inquired some time ago about the possibility of getting this code into AUCTeX itself, given that it seems like functionality everyone could benefit from. I see that you've stripped out the preview-auto code that finds new regions to preview, which is useful for those of us who prefer previews to generate automatically, and replaced it with a simple loop through existing overlays, which suffices for those who prefer keeping the LaTeX visible by default. A common approach would be to leave how to determine which regions to preview automatically as a user knob. In any event, as Uwe brings up in his responses, the stripped down approach means that new LaTeX code (created via ordinary typing, abbrevs, LaTeX-environment, copy/pasting from other files, ...) will not be previewed until the user runs one of the ordinary commands. To give further context, I often use preview-auto in large files (say 300+ pages compiled) for which commands like preview-buffer would take some time. One point of the complicated region-finding code is that I want to jump around in those files (via references, outline commands, cross-references between files, ...) and have whatever I'm looking at previewed automatically and quickly. I would welcome any suggestions on better ways to do this, and understand that this feature is not relevant for everyone's use cases. >From preview-point.el: --8<---------------cut here---------------start------------->8--- ;;; Auto-preview (defcustom preview-point-auto-delay 0.1 "Delay in seconds for automatic preview timer." :type 'number) (defun preview-point@around@write-region (orig-fun &rest args) "Advice around `write-region' to suppress messages. ORIG-FUN is the original function. ARGS are its arguments." (let ((noninteractive t) (inhibit-message t) message-log-max) (apply orig-fun args))) (buframe--defun-debounced preview-point--preview-at-point (pt buffer &delay preview-point-auto-delay) "Debounced preview at point PT in BUFFER." (interactive (list (current-buffer))) (when (buffer-live-p buffer) (with-current-buffer buffer (if-let* ((cur-process (or (get-buffer-process (TeX-process-buffer-name (TeX-region-file))) (get-buffer-process (TeX-process-buffer-name (TeX-master-file)))))) (let ((preview-point-auto-delay (if (> preview-point-auto-delay 0) preview-point-auto-delay 0.5))) ;; Force de-bouncing (when (and preview-current-region (not preview-abort-flag) ;; (< beg (cdr preview-current-region)) ) (progn (ignore-errors (TeX-kill-job)) (setq preview-abort-flag t))) (with-local-quit (accept-process-output cur-process)) (preview-point--preview-at-point pt buffer)) (let ((TeX-suppress-compilation-message t) (save-silently t)) (advice-add 'write-region :around #'preview-point@around@write-region) (unwind-protect ;; If we are working in a file buffer that is not a tex file, ;; then we want preview-region to operate in "non-file" mode, ;; where it passes "<none>" to TeX-region-create. (progn ;; (message "Running previewing at %S for %S" (point) ;; (buffer-substring-no-properties (preview-next-border t) ;; (preview-next-border nil))) (save-excursion (goto-char pt) ;; (preview-at-point) (preview-region (preview-next-border t) (preview-next-border nil)))) (advice-remove 'write-region #'preview-point@around@write-region))))))) --8<---------------cut here---------------end--------------->8--- >From preview-auto.el: --8<---------------cut here---------------start------------->8--- ;; Copyright (C) 2024 Free Software Foundation, Inc. ;; ... ;; This program is free software; you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; ... (defcustom preview-auto-interval 0.1 "Interval for preview timer. For this to have any effect, it must be set before `preview-auto-mode' is activated for the first time." :type 'number) (defun preview-auto--silent-write-region (orig-fun &rest args) "Like `write-region', but suppresses messages. Imperfection: still causes current message to disappear. ORIG-FUN is the original function, and ARGS are its arguments." (let ((noninteractive t) (inhibit-message t) message-log-max) (apply orig-fun args))) (defun preview-auto--preview-something () "Run `preview-region' on an appropriate region. Identify top level math environments near the window. Find a contiguous group of regions at which there are no active or inactive previews at point. Call `preview-region' on the smallest region that contains this group." (unless (or (get-buffer-process (TeX-process-buffer-name (TeX-region-file))) (get-buffer-process (TeX-process-buffer-name (TeX-master-file)))) (setq preview-auto--rules (preview-auto--generate-rules)) (setq preview-auto--begin-re (regexp-opt (mapcar #'car preview-auto--rules) t)) (pcase-let ((`(,pmin . ,pmax) (preview-auto--base-range))) (setq preview-auto--keepalive t) (cond ((and (< pmin (point) pmax) preview-protect-point (preview-auto--update-editing-region))) ((let ((region-above (preview-auto--last-valid-region pmin (min pmax (point)))) (region-below (preview-auto--first-valid-region (max pmin (point)) pmax))) (when (or region-above region-below) (let* ((should-preview-above (or (not region-below) (and region-above region-below (<= (- (point) (cdr region-above)) (- (car region-below) (point)))))) (region (if should-preview-above region-above region-below))) (preview-auto--debug-log (concat "Previewing " (if should-preview-above "above" "below") (when (and region-above region-below) "(closer)"))) (prog1 t (preview-auto--region-wrapper (car region) (cdr region))))))) (t (setq preview-auto--keepalive nil)))))) (defun preview-auto--after-change (beg end length) "Hook function for `preview-auto-mode'. BEG is the start of the modified region, END is the end of the region, and LENGTH is the length of the modification. If the modification occurs before some region where a preview is being generated, then cancel the preview, so that the preview is not misplaced." (save-match-data (preview-auto--debug-log "After change:") (preview-auto--debug-log " %d, %d, %d" beg end length) (when preview-current-region (preview-auto--debug-log " (%d, %d)" (car preview-current-region) (cdr preview-current-region)) (when-let ((proc (get-buffer-process (TeX-process-buffer-name (TeX-region-file))))) (preview-auto--debug-log " region: %s" proc)) (when-let ((proc (get-buffer-process (TeX-process-buffer-name (TeX-master-file))))) (preview-auto--debug-log " master: %s" proc))) (if (and preview-current-region (< beg (cdr preview-current-region))) (progn (preview-auto--debug-log "Cancelling preview") (ignore-errors (TeX-kill-job)) (setq preview-abort-flag t)) (preview-auto--debug-log "Not cancelling preview")))) (defun preview-auto--region-wrapper (beg end) "Preview region between BEG and END, possibly inhibiting messages." (preview-auto--debug-log "Previewing region %d, %d" beg end) (let ((TeX-suppress-compilation-message t) (save-silently t)) (advice-add 'write-region :around #'preview-auto--silent-write-region) (prog1 ;; If we are working in a file buffer that is not a tex file, ;; then we want preview-region to operate in "non-file" mode, ;; where it passes "<none>" to TeX-region-create. (if (eq TeX-master t) (preview-region beg end) (let ((buffer-file-name nil)) (preview-region beg end))) (advice-remove 'write-region #'preview-auto--silent-write-region)))) --8<---------------cut here---------------end--------------->8---