Ikumi Keita <ik...@ikumi.que.jp> writes: > Understood. Preview-latex (and presumably some other parts of AUCTeX) > calls TeX-command-expand directly, which bypasses setting > TeX-current-process-region-p. Thus TeX-active-master doesn't work as > expected.
Yeah, but preview-latex can bind that itself. I mean, it has separate commands for preview document and preview region and then passes either TeX-master-file or TeX-region-file. So if it binds TeX-current-process-region-p itself and TeX-active-master is used implicitly, it should just work. Here's the patch which was empty yesterday where I actually do that.
diff --git a/preview.el.in b/preview.el.in index c14e4900..b7ad0789 100644 --- a/preview.el.in +++ b/preview.el.in @@ -3671,8 +3671,7 @@ The fourth value is the transparent border thickness." (colors (preview-dvipng-color-string preview-colors res)) (command (with-current-buffer TeX-command-buffer (prog1 - (concat (TeX-command-expand preview-dvipng-command - (car file)) + (concat (TeX-command-expand preview-dvipng-command) " " colors resolution) (setq tempdir TeX-active-tempdir)))) (name "Preview-DviPNG")) @@ -3710,8 +3709,7 @@ If FAST is set, do a fast conversion." (prog1 (TeX-command-expand (if fast preview-fast-dvips-command - preview-dvips-command) - (car file)) + preview-dvips-command)) (setq tempdir TeX-active-tempdir)))) (name "Preview-DviPS")) (setq TeX-active-tempdir tempdir) @@ -3751,8 +3749,7 @@ If FAST is set, do a fast conversion." pdfsource (command (with-current-buffer TeX-command-buffer (prog1 - (TeX-command-expand preview-pdf2dsc-command - (car file)) + (TeX-command-expand preview-pdf2dsc-command) (setq tempdir TeX-active-tempdir pdfsource (funcall (car file) "pdf" t))))) (name "Preview-PDF2DSC")) @@ -3930,8 +3927,7 @@ If FORMAT-CONS is non-nil, a previous format may get reused." (master-file (expand-file-name (TeX-master-file t))) (command (preview-do-replacements (TeX-command-expand - (preview-string-expand preview-LaTeX-command) - 'TeX-master-file) + (preview-string-expand preview-LaTeX-command)) preview-dump-replacements)) (preview-auto-cache-preamble nil)) (unless (and (consp (cdr format-cons)) @@ -4003,12 +3999,12 @@ stored in `preview-dumped-alist'." (save-excursion (goto-char begin) (if (bolp) 0 -1)))))) - (preview-generate-preview t (TeX-region-file) - (preview-do-replacements - (TeX-command-expand - (preview-string-expand preview-LaTeX-command) - 'TeX-region-file) - preview-LaTeX-command-replacements))) + (let ((TeX-current-process-region-p t)) + (preview-generate-preview t (TeX-region-file) + (preview-do-replacements + (TeX-command-expand + (preview-string-expand preview-LaTeX-command)) + preview-LaTeX-command-replacements)))) (defun preview-buffer () "Run preview on current buffer." @@ -4046,8 +4042,7 @@ stored in `preview-dumped-alist'." nil (TeX-master-file) (preview-do-replacements (TeX-command-expand - (preview-string-expand preview-LaTeX-command) - 'TeX-master-file) + (preview-string-expand preview-LaTeX-command)) preview-LaTeX-command-replacements))) (defun preview-environment (count) @@ -4090,9 +4085,7 @@ It returns the started process." (let* ((geometry (preview-get-geometry)) (commandbuff (current-buffer)) (pr-file (cons - (if TeX-current-process-region-p - 'TeX-region-file - 'TeX-master-file) + 'TeX-active-master (file-name-nondirectory file))) (master (TeX-master-file)) (master-file (expand-file-name master)) diff --git a/tests/japanese/preview-latex.el b/tests/japanese/preview-latex.el index 41907dfd..5ca3d968 100644 --- a/tests/japanese/preview-latex.el +++ b/tests/japanese/preview-latex.el @@ -228,8 +228,7 @@ String encoded in `shift_jis' can have regexp meta characters in it." (setq process (TeX-inline-preview-internal (preview-do-replacements (TeX-command-expand - (preview-string-expand preview-LaTeX-command) - 'TeX-master-file) + (preview-string-expand preview-LaTeX-command)) preview-LaTeX-command-replacements) dummyfile '(nil . nil) (current-buffer) '(nil . (t . t)) dummyfile '(nil nil nil))) @@ -263,8 +262,7 @@ String encoded in `shift_jis' can have regexp meta characters in it." (setq process (TeX-inline-preview-internal (preview-do-replacements (TeX-command-expand - (preview-string-expand preview-LaTeX-command) - 'TeX-master-file) + (preview-string-expand preview-LaTeX-command)) preview-dump-replacements) dummyfile '(nil . nil) (current-buffer) nil dummyfile '(nil nil nil))) diff --git a/tests/tex/command-expansion.el b/tests/tex/command-expansion.el index 0de5831c..bb5c7d49 100644 --- a/tests/tex/command-expansion.el +++ b/tests/tex/command-expansion.el @@ -29,8 +29,7 @@ (should (string= (let ((TeX-command-list (list (cons "Test" '("%%%% %`%'" TeX-run-command t t))))) - (TeX-command-expand (nth 1 (assoc "Test" TeX-command-list)) - 'TeX-master-file)) + (TeX-command-expand (nth 1 (assoc "Test" TeX-command-list)))) "%% "))) (ert-deftest TeX-command-expansion-errors () @@ -39,7 +38,7 @@ ;; This error is actually thrown by `TeX-engine-in-engine-alist', but we want ;; to be sure that `TeX-command-expand' fails when the engine is not valid. (let ((TeX-engine 'non-existing-engine)) - (TeX-command-expand "%l" 'TeX-master-file)))) + (TeX-command-expand "%l")))) (ert-deftest TeX-view-command-raw-errors () "Tests to trigger errors in `TeX-view-command-raw'." @@ -94,7 +93,7 @@ (TeX-engine 'default) (TeX-master "/tmp/abc") (TeX-command-extra-options " \"\\foo\"")) - (TeX-command-expand "%`%(extraopts)%' %T" #'TeX-master-file)) + (TeX-command-expand "%`%(extraopts)%' %T")) " \"\\foo\" \"\\input\" \\\\detokenize\\{\\ abc.tex\\ \\}"))) (ert-deftest TeX-command-expand-skip-file-name () @@ -108,24 +107,24 @@ See <https://lists.gnu.org/r/bug-auctex/2014-08/msg00012.html>." (let ((TeX-master "abc-def") (TeX-expand-list '(("-" (lambda () ":"))))) (should (string= - (TeX-command-expand "%s" #'TeX-master-file) + (TeX-command-expand "%s") TeX-master)) (should (string= - (TeX-command-expand "%t" #'TeX-master-file) + (TeX-command-expand "%t") (TeX-master-file "tex" t))) (should (string= - (TeX-command-expand "%T" #'TeX-master-file) + (TeX-command-expand "%T") (TeX-master-file "tex" t))) (should (string= - (TeX-command-expand "%d" #'TeX-master-file) + (TeX-command-expand "%d") (TeX-master-file "dvi" t))) (should (string= - (TeX-command-expand "%f" #'TeX-master-file) + (TeX-command-expand "%f") (TeX-master-file "ps" t))) ;; The expander of "%o" does not yet cater for this possible endless ;; loop. ;; (should (string= - ;; (TeX-command-expand "%o" #'TeX-master-file) + ;; (TeX-command-expand "%o") ;; (TeX-master-file "pdf" t))) )) @@ -138,11 +137,11 @@ See <https://lists.gnu.org/r/bug-auctex/2014-08/msg00012.html>." TeX-current-process-region-p) (setq TeX-current-process-region-p nil) (should (string= - (TeX-command-expand "%s" #'TeX-active-master) + (TeX-command-expand "%s") TeX-master)) (setq TeX-current-process-region-p t) (should (string= - (TeX-command-expand "%s" #'TeX-active-master) + (TeX-command-expand "%s") TeX-region)))) ;;; command-expansion.el ends here diff --git a/tex-buf.el b/tex-buf.el index 975c6fc9..b9d90858 100644 --- a/tex-buf.el +++ b/tex-buf.el @@ -494,8 +494,7 @@ been set." ;; of point in the region file so that forward search works. (if (string= name "View") (TeX-region-update-point)) - (let ((command (TeX-command-expand (nth 1 (assoc name TeX-command-list)) - file-fn)) + (let ((command (TeX-command-expand (nth 1 (assoc name TeX-command-list)))) (hook (nth 2 (assoc name TeX-command-list))) (confirm (if override-confirm (> (prefix-numeric-value override-confirm) 0) @@ -535,8 +534,8 @@ remember to add /Library/TeX/texbin/ to your PATH" (TeX-process-set-variable file 'TeX-command-next TeX-command-Show) (funcall hook name command file)))) -(defun TeX-command-expand (command file-fn &optional list) - "Expand COMMAND for FILE as described in LIST. +(defun TeX-command-expand (command &optional list) + "Expand COMMAND for `TeX-active-master' as described in LIST. LIST default to `TeX-expand-list'. As a special exception, `%%' can be used to produce a single `%' sign in the output without further expansion." @@ -551,40 +550,32 @@ without further expansion." "%")) (or list (TeX-expand-list))) pat (regexp-opt (mapcar #'car list))) - ;; `TeX-command-expand' is called with `file-fn' argument being one - ;; of `TeX-master-file', `TeX-region-file' and - ;; `TeX-active-master'. The return value of these functions - ;; sometimes needs suitable "decorations" for an argument for - ;; underlying shell or latex executable, or both, when the - ;; relavant file-fn name involves some special characters such as - ;; space and multibyte characters. Hence embed that function in a - ;; template prepared for that purpose. - (setq file-fn #'TeX--master-or-region-file-with-extra-quotes) - (while (setq TeX-expand-pos (string-match pat TeX-expand-command TeX-expand-pos)) - (setq string (match-string 0 TeX-expand-command) - entry (assoc string list) - expansion (car (cdr entry)) ;Second element - arguments (cdr (cdr entry)) ;Remaining elements - string (save-match-data - (cond - ((memq expansion (list 'TeX-active-master - #'TeX-active-master)) - (let ((res (apply file-fn arguments))) - ;; Advance past the file name in order to - ;; prevent expanding any substring of it. - (setq TeX-expand-pos - (+ TeX-expand-pos (length res))) - res)) - ((functionp expansion) - (apply expansion arguments)) - ((boundp expansion) - (apply (symbol-value expansion) arguments)) - (t - (error "Nonexpansion %s" expansion))))) - (if (stringp string) - (setq TeX-expand-command - (replace-match string t t TeX-expand-command)))) - TeX-expand-command)) + (let ((file-fn #'TeX--master-or-region-file-with-extra-quotes)) + (while (setq TeX-expand-pos (string-match pat TeX-expand-command TeX-expand-pos)) + (setq string (match-string 0 TeX-expand-command) + entry (assoc string list) + expansion (car (cdr entry)) ;Second element + arguments (cdr (cdr entry)) ;Remaining elements + string (save-match-data + (cond + ((memq expansion (list 'TeX-active-master + #'TeX-active-master)) + (let ((res (apply file-fn arguments))) + ;; Advance past the file name in order to + ;; prevent expanding any substring of it. + (setq TeX-expand-pos + (+ TeX-expand-pos (length res))) + res)) + ((functionp expansion) + (apply expansion arguments)) + ((boundp expansion) + (apply (symbol-value expansion) arguments)) + (t + (error "Nonexpansion %s" expansion))))) + (if (stringp string) + (setq TeX-expand-command + (replace-match string t t TeX-expand-command)))) + TeX-expand-command))) (defun TeX--master-or-region-file-with-extra-quotes (&optional extension nondirectory ask extra) diff --git a/tex.el b/tex.el index 4719eb49..ce31831e 100644 --- a/tex.el +++ b/tex.el @@ -1567,11 +1567,11 @@ predicates are true, nil otherwise." (unless (or (null executable) (cond ((stringp executable) - (executable-find (TeX-command-expand executable nil))) + (executable-find (TeX-command-expand executable))) ((listp executable) (catch 'notfound (dolist (exec executable t) - (unless (executable-find (TeX-command-expand exec nil)) + (unless (executable-find (TeX-command-expand exec)) (throw 'notfound nil))))))) (error (format "Cannot find %S viewer. \ Select another one in `TeX-view-program-selection'" viewer)))
>>>> I think that the second argument of TeX-command-expand is no longer >>>> necessary, thanks to the fine utility TeX-active-master. > > So this thought of mine wasn't correct. I still think you were correct (as is usually the case). :-) Bye, Tassilo