all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#70409: 30.0.50; `latexenc-find-file-coding-system` uses `TeX-master` before we know it's safe
@ 2024-04-15 22:42 Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
  2024-04-16 10:19 ` Arash Esbati
  2024-04-16 11:49 ` Eli Zaretskii
  0 siblings, 2 replies; 8+ messages in thread
From: Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-04-15 22:42 UTC (permalink / raw)
  To: 70409; +Cc: monnier

[-- Attachment #1: Type: text/plain, Size: 2192 bytes --]

Package: Emacs
Version: 30.0.50


If I open a file `foo.tex` with a local variable setting of
`TeX-master: "paper.tex"` and that `paper.tex` file has a local
variable setting of `TeX-master: t`, I get the funny behavior that
Emacs first asks me whether to obey the `TeX-master: t` setting of
`paper.tex` before asking me whether to obey the `TeX-master:
"paper.tex"` setting of `foo.tex`, even though it obviously had to use
the `TeX-master: "paper.tex"` setting in order to decide to open the
`paper.tex` file (and ask me about its `TeX-master: t`).

The corresponding backtrace looks as below:

    Debugger entered--Lisp error: (minibuffer-quit)
      #<subr F616e6f6e796d6f75732d6c616d626461_anonymous_lambda_57>()
      read-char-from-minibuffer("Please type y, n, ! or i, or C-v/M-v to scroll: " (33 105 121 110 32))
      read-char-choice("Please type y, n, ! or i, or C-v/M-v to scroll: " (33 105 121 110 32))
      hack-local-variables-confirm(((TeX-master . t)) ((TeX-master . t)) nil nil)
      hack-local-variables-filter(((TeX-master . t)) nil)
      hack-local-variables(no-mode)
      run-mode-hooks(latex-mode-hook)
      latex-mode()
      set-auto-mode-0(latex-mode nil)
      set-auto-mode()
      normal-mode(t)
      after-find-file(nil nil)
      find-file-noselect-1(#<buffer paper.tex> ".../paper.tex" t nil ".../paper.tex" (3064599 65026))
      find-file-noselect("paper.tex" t)
      latexenc-find-file-coding-system((insert-file-contents ".../foo.tex" t nil nil nil))
      insert-file-contents(".../foo.tex" t)
      find-file-noselect-1(#<buffer foo.tex> ".../foo.tex" nil nil ".../foo.tex" (3064581 65026))
      find-file-noselect(".../foo.tex")
      command-line-1((".../foo.tex"))
      command-line()
      normal-top-level()

showing that the problem is that `latexenc-find-file-coding-system`
is the function that opens `paper.tex` before the users had a chance to
confirm that they think this is safe.

I suggest the patch below which makes `latexenc-find-file-coding-system`
use `safe-local-variable-p` before using a file-local setting, and also
adds corresponding `safe-local-variable` settings for `TeX-master` and
`tex-main-file`.


        Stefan

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: tex-master.diff --]
[-- Type: text/x-diff, Size: 2519 bytes --]

diff --git a/lisp/international/latexenc.el b/lisp/international/latexenc.el
index 6e2306449bc..66e3faa37b9 100644
--- a/lisp/international/latexenc.el
+++ b/lisp/international/latexenc.el
@@ -155,14 +155,16 @@ latexenc-find-file-coding-system
               (when (re-search-forward
                      "^%+ *\\(TeX-master\\|tex-main-file\\): *\"\\(.+\\)\""
                      nil t)
-                (let ((file (match-string 2)))
-                  (dolist (ext `("" ,(if (boundp 'TeX-default-extension)
-                                         (concat "." TeX-default-extension)
-                                       "")
-                                 ".tex" ".ltx" ".dtx" ".drv"))
-                    (if (and (null latexenc-main-file) ;Stop at first.
-                             (file-exists-p (concat file ext)))
-                        (setq latexenc-main-file (concat file ext)))))))
+                (let ((var (match-string 1))
+                      (file (match-string 2)))
+                  (when (safe-local-variable-p (intern var) file)
+                    (dolist (ext `("" ,(if (boundp 'TeX-default-extension)
+                                           (concat "." TeX-default-extension)
+                                         "")
+                                   ".tex" ".ltx" ".dtx" ".drv"))
+                      (if (and (null latexenc-main-file) ;Stop at first.
+                               (file-exists-p (concat file ext)))
+                          (setq latexenc-main-file (concat file ext))))))))
             ;; try tex-modes tex-guess-main-file
             (when (and (not latexenc-dont-use-tex-guess-main-file-flag)
                        (not latexenc-main-file))
diff --git a/lisp/textmodes/tex-mode.el b/lisp/textmodes/tex-mode.el
index 02ee1242c72..e7b1522751f 100644
--- a/lisp/textmodes/tex-mode.el
+++ b/lisp/textmodes/tex-mode.el
@@ -89,6 +89,7 @@ tex-main-file
 if the variable is non-nil."
   :type '(choice (const :tag "None" nil)
                  file)
+  :safe #'stringp
   :group 'tex-file)
 
 ;;;###autoload
@@ -2213,6 +2214,10 @@ tex-guess-main-file
 			  header-re (+ (point) 10000) t))))
 	    (throw 'found (expand-file-name buffer-file-name))))))))
 
+(unless (get 'TeX-master 'safe-local-variable) ;Don't override AUCTeX's setting.
+  (put 'TeX-master 'safe-local-variable
+       (lambda (x) (or (booleanp x) (stringp x)))))
+
 (defun tex-main-file ()
   "Return the relative name of the main file."
   (let* ((file (or tex-main-file

^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-04-17 17:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-04-15 22:42 bug#70409: 30.0.50; `latexenc-find-file-coding-system` uses `TeX-master` before we know it's safe Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-16 10:19 ` Arash Esbati
2024-04-16 12:54   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-16 20:46     ` Arash Esbati
2024-04-16 11:49 ` Eli Zaretskii
2024-04-16 12:53   ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors
2024-04-16 13:35     ` Eli Zaretskii
2024-04-17 17:52       ` Stefan Monnier via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.