auctex-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[AUCTeX-diffs] GNU AUCTeX branch, tex-build-only, created. 683b9f9e04aae


From: Al Haji-Ali
Subject: [AUCTeX-diffs] GNU AUCTeX branch, tex-build-only, created. 683b9f9e04aae55218cd7fdcff7a16a0e6479f99
Date: Thu, 11 Mar 2021 11:19:03 -0500 (EST)

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU AUCTeX".

The branch, tex-build-only has been created
        at  683b9f9e04aae55218cd7fdcff7a16a0e6479f99 (commit)

- Log -----------------------------------------------------------------
commit 683b9f9e04aae55218cd7fdcff7a16a0e6479f99
Author: Al Haji-Ali <a.hajiali@hw.ac.uk>
Date:   Thu Mar 11 15:17:04 2021 +0000

    Reduced complications by not reading TeX-output-dir from the master file.

diff --git a/tex-buf.el b/tex-buf.el
index 8464b59..f317cbc 100644
--- a/tex-buf.el
+++ b/tex-buf.el
@@ -807,12 +807,7 @@ omitted) and `TeX-region-file'."
   "Guess the next command to be run on NAME."
   (let ((command-next nil)
         (name (TeX--concat-ext name-or-file-fn)))
-    (cond (;; name might be absolute or relative, so expand it for
-           ;; comparison.
-           ;; NOTE: Instead of comparing the filename to TeX-region-file
-           ;; we can use name-or-file-fn to check if it is 'TeX-region-file
-           (if (string-equal (expand-file-name name)
-                             (expand-file-name (TeX-region-file)))
+    (cond ((if (equal name-or-file-fn 'TeX-region-file)
                (TeX-check-files (TeX-region-file (TeX-output-extension))
                                 ;; Each original will be checked for all dirs
                                 ;; in `TeX-check-path' so this needs to be just
@@ -2244,14 +2239,15 @@ The compatibility argument IGNORE is ignored."
   ;; third argument `ask'.  For example, it's used in `TeX-command-sequence',
   ;; where we don't know which function has to be called.  Keep this in mind
   ;; should you want to use another argument here.
-  (concat (or (TeX-master-output-dir nondirectory)
-              (if nondirectory "" (TeX-master-directory)))
-          (cond ((eq extension t)
-                 (concat TeX-region "." TeX-default-extension))
-                (extension
-                 (concat TeX-region "." extension))
-                (t
-                 TeX-region))))
+  (let ((master-dir (TeX-master-directory)))
+    (concat (or (TeX-master-output-dir master-dir nondirectory)
+                (if nondirectory "" master-dir))
+            (cond ((eq extension t)
+                   (concat TeX-region "." TeX-default-extension))
+                  (extension
+                   (concat TeX-region "." extension))
+                  (t
+                   TeX-region)))))
 
 (defcustom TeX-region "_region_"
   "Base name of temporary file for `TeX-command-region' and 
`TeX-command-buffer'."
diff --git a/tex.el b/tex.el
index c622891..fb1e7f2 100644
--- a/tex.el
+++ b/tex.el
@@ -2423,15 +2423,15 @@ name of master file if it cannot be determined 
otherwise."
           ;; Otherwise drop it.
           (setq name (TeX-strip-extension name))))
 
-    (let* ((output-dir (and (or (stringp TeX-master)
-                                (buffer-file-name)) ;; otherwise `name' is 
"<none>"
-                            (TeX-master-output-dir nondirectory
-                                                   (concat name "." 
TeX-default-extension))))
-           (reg (and output-dir (TeX--clean-extensions-regexp t)))
-           (is-output-ext
-            (and reg (or (string-match-p reg (concat "." extension))
-                         (string= "prv" extension)))))
-      (if is-output-ext
+    (let* ((reg (TeX--clean-extensions-regexp t))
+           (is-output-ext (and reg
+                               (or (string-match-p reg (concat "." extension))
+                                   (string= "prv" extension))))
+           (output-dir (and is-output-ext
+                            (TeX-master-output-dir
+                             (file-name-directory name)
+                             nondirectory))))
+      (if output-dir
           ;; If output extesnion, use output-dir (directory is already removed)
           ;; in TeX-master-output-dir
           (setq name (concat output-dir (file-name-nondirectory name)))
@@ -2534,23 +2534,21 @@ directory is the same as the directory of TeX-master."
   :safe 'string-or-null-p
   :type 'string)
 
-(defun TeX-master-output-dir (&optional relative-to-master master-file)
-  (with-current-buffer
-      (find-file-noselect (or master-file (TeX-master-file t)) t)
-    (let ((out-dir (and TeX-output-dir
-                        (file-name-as-directory
-                         (abbreviate-file-name
-                          (substitute-in-file-name
-                           (expand-file-name
-                            TeX-output-dir
-                            (file-name-directory buffer-file-name))))))))
+(defun TeX-master-output-dir (master-dir relative-to-master)
+  (when TeX-output-dir
+    (let* ((master-dir (expand-file-name (or master-dir "")))
+           (out-dir (file-name-as-directory
+                     (abbreviate-file-name
+                      (substitute-in-file-name
+                       (expand-file-name
+                        TeX-output-dir
+                        master-dir))))))
       ;; Make sure the directory exists
-      (when out-dir
-        (unless (file-exists-p out-dir)
-          (make-directory (file-name-as-directory out-dir) t))
-        (if relative-to-master
-            (file-relative-name out-dir)
-          out-dir)))))
+      (unless (file-exists-p out-dir)
+        (make-directory (file-name-as-directory out-dir) t))
+      (if relative-to-master
+          (file-relative-name out-dir master-dir)
+        out-dir))))
 
 (defcustom TeX-style-local "style"
   "Directory containing hand generated TeX information.
@@ -2561,7 +2559,7 @@ be relative to that."
   :type 'string)
 
 (defun TeX--output-dir-arg (argname)
-  (let ((out-dir (TeX-master-output-dir t)))
+  (let ((out-dir (TeX-master-output-dir (TeX-master-directory) t)))
     (if out-dir
         (concat argname "\"" out-dir "\"")
       "")))

commit c955473368fdffa7def3fbf03016fd0615a53528
Author: Al Haji-Ali <a.hajiali@hw.ac.uk>
Date:   Mon Mar 1 13:46:15 2021 +0000

    Region files in output-dir and removed TeX-command-file-function
    
    TeX-save-document, TeX-command-query and TeX-command-default now take
    `name-or-file-fn` instead of just `name`.

diff --git a/etc/auctex-output-dir.patch b/etc/auctex-output-dir.patch
deleted file mode 100644
index 1725cc4..0000000
--- a/etc/auctex-output-dir.patch
+++ /dev/null
@@ -1,517 +0,0 @@
-diff --git a/preview.el.in b/preview.el.in
-index e22d2419..a1844a3b 100644
---- a/preview.el.in
-+++ b/preview.el.in
-@@ -552,7 +552,7 @@ if you customize this."
-   :type 'string)
- 
- (defcustom preview-pdf2dsc-command
--  "pdf2dsc %s.pdf %m/preview.dsc"
-+  "pdf2dsc %(O?pdf) %m/preview.dsc"
-   "Command used for generating dsc from a PDF file."
-   :group 'preview-latex
-   :type 'string)
-@@ -3155,7 +3155,9 @@ later while in use."
-                 topdir
-                 0))
-     (shell-quote-argument
--     (concat (file-name-as-directory (file-name-nondirectory topdir))
-+     (concat (file-name-as-directory
-+              ;; Don't use topdir, because %m expects the path to be relative 
to master
-+              (TeX-active-master "prv" t))
-              (file-name-nondirectory (nth 0 TeX-active-tempdir))))))
- 
- (defun preview-parse-counters (string)
-@@ -3911,8 +3913,8 @@ This is passed through `preview-do-replacements'."
-   ;; If -kanji option exists, pick it up as the second match.
-   ;; Discard all other options.
-     ("\\`\\([^ ]+\\)\
--\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|-\\(?:[^ 
\\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*\\(.*\\)\\'"
--     . ("\\1 -ini \\2 -interaction=nonstopmode \"&\\1\" " preview-format-name 
".ini \\3")))
-+\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|\\(--?output-directory[= ][^ 
]+\\)\\|-\\(?:[^ \\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*\\(.*\\)\\'"
-+     . ("\\1 -ini \\2 \\3 -interaction=nonstopmode \"&\\1\" " 
preview-format-name ".ini \\4")))
-   "Generate a dump command from the usual preview command."
-   :group 'preview-latex
-   :type '(repeat
-@@ -3923,10 +3925,10 @@ This is passed through `preview-do-replacements'."
-   ;; If -kanji option exists, pick it up as the second match.
-   ;; Discard all other options.
-   '(("\\`\\([^ ]+\\)\
--\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|-\\(?:[^ 
\\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*.*\
-+\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|\\(--?output-directory[= ][^ 
]+\\)\\|-\\(?:[^ \\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*.*\
-  \"\\\\input\" \"\\\\detokenize{\" \\(.*\\) \"}\"\\'"
--     . ("\\1 \\2 -interaction=nonstopmode -file-line-error "
--        preview-format-name " \"/AUCTEXINPUT{\" \\3 \"}\"")))
-+     . ("\\1 \\2 \\3 -interaction=nonstopmode -file-line-error "
-+        preview-format-name " \"/AUCTEXINPUT{\" \\4 \"}\"")))
-   ;; See the ini file code below in `preview-cache-preamble' for the
-   ;; weird /AUCTEXINPUT construct.  In short, it is crafted so that
-   ;; dumped format file can read file of non-ascii name.
-@@ -4160,7 +4162,10 @@ internal parameters, STR may be a log to insert into 
the current log."
-       ((preview-format-name (shell-quote-argument
-                              (concat "&"
-                                      (preview-dump-file-name
--                                      (file-name-nondirectory master)))))
-+                                        ;; Get the filename from 
TeX-master-file with prv
-+                                        ;; to get the correct path but then 
strip
-+                                        ;; the extension
-+                                        (file-name-sans-extension 
(TeX-master-file "prv" t))))))
-        (process-environment (copy-sequence process-environment))
-        (process
-         (progn
-diff --git a/tex-buf.el b/tex-buf.el
-index b334b7fa..93e909d6 100644
---- a/tex-buf.el
-+++ b/tex-buf.el
-@@ -61,6 +61,8 @@
-   :group 'TeX-command
-   :type 'string)
- 
-+(defvar TeX-command-file-function)
-+
- ;;; Interactive Commands
- ;;
- ;; The general idea is, that there is one process and process buffer
-@@ -78,22 +80,30 @@
- (defun TeX-save-document (name)
-   "Save all files belonging to the current document.
- Return non-nil if document needs to be re-TeX'ed."
--  (interactive (list (TeX-master-file)))
-+  (interactive (list 'TeX-master-file))
-   (if (string-equal name "")
--      (setq name (TeX-master-file)))
-+      (setq name 'TeX-master-file))
- 
--  (TeX-check-files (concat name "." (TeX-output-extension))
--                   (cons name (TeX-style-list))
-+  (TeX-check-files (TeX-get-command-filename name (TeX-output-extension))
-+                   (cons (TeX-get-command-filename name) (TeX-style-list))
-                    TeX-file-extensions))
- 
-+(defun TeX-get-command-filename (name &optional extension)
-+  (if (or TeX-command-file-function (functionp name))
-+      (funcall (or TeX-command-file-function name) extension)
-+    (if extension
-+        (concat name "." extension)
-+      name)))
-+
- (defun TeX-command-master (&optional override-confirm)
-   "Run command on the current document.
- 
- If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
- depend on it being positive instead of the entry in `TeX-command-list'."
-   (interactive "P")
--  (TeX-command (TeX-command-query (TeX-master-file nil nil t))
--               'TeX-master-file override-confirm))
-+  (let ((TeX-command-file-function 'TeX-master-file))
-+   (TeX-command (TeX-command-query (TeX-master-file nil nil t))
-+               'TeX-master-file override-confirm)))
- 
- (defvar TeX-command-region-begin nil)
- (defvar TeX-command-region-end nil)
-@@ -185,8 +195,10 @@ all text after `TeX-trailer-start'."
-   ;; `nondirectory' argument, otherwise `TeX-comand-default' called
-   ;; within `TeX-command-query' won't work in included files not
-   ;; placed in `TeX-master-directory'.
--  (TeX-command (TeX-command-query (TeX-region-file)) 'TeX-region-file
--               override-confirm))
-+  (let ((TeX-command-file-function 'TeX-region-file))
-+    (TeX-command (TeX-command-query (TeX-region-file))
-+                 TeX-command-file-function
-+                 override-confirm)))
- 
- (defun TeX-command-buffer (&optional override-confirm)
-   "Run TeX on the current buffer.
-@@ -530,9 +542,11 @@ remember to add /Library/TeX/texbin/ to your PATH"
-                           ""))))
- 
-     ;; Now start the process
--    (let ((file (funcall file-fn)))
-+    (let ((TeX-command-file-function file-fn)
-+          (file (funcall file-fn)))
-       (TeX-process-set-variable file 'TeX-command-next TeX-command-Show)
--      (funcall hook name command file))))
-+      (funcall hook name command file)))
-+  )
- 
- (defun TeX-command-expand (command &optional list)
-   "Expand COMMAND for `TeX-active-master' as described in LIST.
-@@ -800,18 +814,18 @@ omitted) and `TeX-region-file'."
-            ;; comparison.
-            (if (string-equal (expand-file-name name)
-                              (expand-file-name (TeX-region-file)))
--               (TeX-check-files (concat name "." (TeX-output-extension))
-+               (TeX-check-files (TeX-get-command-filename name 
(TeX-output-extension))
-                                 ;; Each original will be checked for all dirs
-                                 ;; in `TeX-check-path' so this needs to be 
just
-                                 ;; a filename without directory.
--                                (list (file-name-nondirectory name))
-+                                (list (file-relative-name 
(TeX-get-command-filename name)))
-                                 TeX-file-extensions)
--             (TeX-save-document (TeX-master-file)))
-+             (TeX-save-document 'TeX-master-file))
-            TeX-command-default)
-           ((and (memq major-mode '(doctex-mode latex-mode))
-                 ;; Want to know if bib file is newer than .bbl
-                 ;; We don't care whether the bib files are open in emacs
--                (TeX-check-files (concat name ".bbl")
-+                (TeX-check-files (TeX-get-command-filename name "bbl")
-                                  (mapcar #'car
-                                          (LaTeX-bibliography-list))
-                                  (append BibTeX-file-extensions
-@@ -828,12 +842,12 @@ omitted) and `TeX-region-file'."
-             (member
-              (setq command-next
-                    (TeX-process-get-variable
--                    name
-+                    (TeX-get-command-filename name)
-                     'TeX-command-next
-                     (or (and TeX-PDF-mode (TeX-PDF-from-DVI))
-                         TeX-command-Show)))
-              (list "Dvips" "Dvipdfmx" TeX-command-Show))
--            (cdr (assoc (expand-file-name (concat name ".idx"))
-+            (cdr (assoc (expand-file-name (TeX-get-command-filename "idx"))
-                         LaTeX-idx-changed-alist)))
-            "Index")
-           (command-next)
-@@ -1138,6 +1152,7 @@ run of `TeX-run-TeX', use
-   ;; Presence of error is reported inside `TeX-TeX-sentinel-check'
-   (let ((current-master (TeX-master-file))
-         (idx-file nil) (element nil))
-+
-     ;; the current master file is saved because error routines are
-     ;; parsed in other buffers;
-     (setq TeX-error-report-switches
-@@ -1151,7 +1166,7 @@ run of `TeX-run-TeX', use
-     ;; Store md5 hash of the index file before running LaTeX.
-     (and (memq major-mode '(doctex-mode latex-mode))
-          (prog1 (file-exists-p
--                 (setq idx-file (expand-file-name (concat file ".idx"))))
-+                 (setq idx-file (expand-file-name (TeX-get-command-filename 
file "idx"))))
-            ;; In order to avoid confusion and pollution of
-            ;; `LaTeX-idx-md5-alist', remove from this alist all md5 hashes of
-            ;; the current index file.  Note `assq-delete-all' doesn't work 
with
-@@ -1233,7 +1248,7 @@ run of `TeX-run-TeX', use
-                  ;; `default-directory', then we have to expand `file' 
file-name
-                  ;; in the same directory of `TeX-command-buffer'.
-                  (assoc (with-current-buffer TeX-command-buffer
--                            (expand-file-name (concat file ".idx")))
-+                            (expand-file-name (TeX-get-command-filename 
"idx")))
-                         LaTeX-idx-changed-alist))
-       (setq LaTeX-idx-changed-alist (delq element LaTeX-idx-changed-alist)))
-     (if TeX-process-asynchronous
-@@ -1684,14 +1699,14 @@ Rerun to get mark in right position\\." nil t)
-   (let ((idx-file nil) (master nil))
-     (and (file-exists-p
-           (setq idx-file
--                (concat
--                 (setq master
--                       (with-current-buffer TeX-command-buffer
--                         (expand-file-name (TeX-active-master)))) ".idx")))
-+                (setq master
-+                      (with-current-buffer TeX-command-buffer
-+                        (expand-file-name (TeX-active-master "idx"))))))
-          ;; imakeidx package automatically runs makeindex, thus, we need to be
-          ;; sure .ind file isn't newer than .idx.
--         (TeX-check-files (concat master ".ind")
--                          (list (file-name-nondirectory master)) '("idx"))
-+         (TeX-check-files (with-current-buffer TeX-command-buffer
-+                            (expand-file-name (TeX-active-master "ind")))
-+                          (list (file-relative-name idx-file)) '("idx"))
-          (with-temp-buffer
-            (insert-file-contents idx-file)
-            (not (equal
-@@ -1995,6 +2010,9 @@ command."
- (defvar TeX-current-process-region-p nil
-   "This variable is set to t iff the last TeX command is on a region.")
- 
-+(defvar TeX-command-file-function nil
-+  "The current file function in TeX-command.")
-+
- (defun TeX-active-process ()
-   "Return the active process for the current buffer."
-   (TeX-process (TeX-active-master)))
-diff --git a/tex.el b/tex.el
-index b59aee05..df9fbf1c 100644
---- a/tex.el
-+++ b/tex.el
-@@ -89,6 +89,7 @@
- (defvar LaTeX-optop)
- (defvar TeX-Biber-global-files)
- (defvar TeX-global-input-files)
-+(defvar TeX-output-dir)
- ;; tex-buf.el
- (defvar TeX-current-process-region-p)
- (defvar TeX-region)
-@@ -182,18 +183,18 @@ If nil, none is specified."
- ;; `TeX-expand-list-builtin' for a description of the % escapes
- 
- (defcustom TeX-command-list
--  '(("TeX" "%(PDF)%(tex) %(file-line-error) %`%(extraopts) 
%S%(PDFout)%(mode)%' %t"
-+  '(("TeX" "%(PDF)%(tex) %(file-line-error) %`%(extraopts) %(output-dir) 
%S%(PDFout)%(mode)%' %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")
-     ;; Not part of standard TeX.
--    ("Makeinfo" "makeinfo %(extraopts) %t" TeX-run-compile nil
-+    ("Makeinfo" "makeinfo %(extraopts) %(o-dir) %t" TeX-run-compile nil
-      (texinfo-mode) :help "Run Makeinfo with Info output")
--    ("Makeinfo HTML" "makeinfo %(extraopts) --html %t" TeX-run-compile nil
-+    ("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)%' %t"
-+    ("AmSTeX" "amstex %(PDFout) %`%(extraopts) %(output-dir) %S%(mode)%' %t"
-      TeX-run-TeX nil (ams-tex-mode) :help "Run AMSTeX")
-     ;; support for ConTeXt  --pg
-     ;; first version of ConTeXt to support nonstopmode: 2003.2.10
-@@ -202,11 +203,11 @@ If nil, none is specified."
-     ("ConTeXt Full" "%(cntxcom) %(extraopts) %(execopts)%t"
-      TeX-run-TeX nil
-      (context-mode) :help "Run ConTeXt until completion")
--    ("BibTeX" "bibtex %s" TeX-run-BibTeX nil
-+    ("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 %s" TeX-run-Biber nil
-+    ("Biber" "biber %s %(output-dir)" TeX-run-Biber nil
-      (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode)
-      :help "Run Biber")
-     ("View" "%V" TeX-run-discard-or-function t t :help "Run Viewer")
-@@ -219,20 +220,20 @@ If nil, none is specified."
-     ("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 %d" TeX-run-dvipdfmx nil
-+    ("Dvipdfmx" "dvipdfmx %d -o %(O?pdf)" 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" TeX-run-ps2pdf nil
-+    ("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 %s" TeX-run-command nil
-+    ("Glossaries" "makeglossaries %(O?aux)" 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 %s" TeX-run-index nil
-+    ("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 %s" TeX-run-index t
-+    ("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
-@@ -393,7 +394,7 @@ The executable `latex' is LaTeX version 2e."
- 
- (defcustom LaTeX-command-style
-   ;; They have all been combined in LaTeX 2e.
--  '(("" "%(PDF)%(latex) %(file-line-error) %(extraopts) %S%(PDFout)"))
-+  '(("" "%(PDF)%(latex) %(file-line-error) %(extraopts) %(output-dir) 
%S%(PDFout)"))
- "List of style options and LaTeX commands.
- 
- If the first element (a regular expression) matches the name of one of
-@@ -420,7 +421,7 @@ string."
- ;; TeX-print-command.
- 
- (defcustom TeX-print-command
--  "{ test -e %s.dvi && %(o?)dvips -P%p %r %s; } || lpr -P%p %o"
-+  "{ test -e %d && %(o?)dvips -P%p %r %s; } || lpr -P%p %o"
-   "Command used to print a file.
- 
- First `%p' is expanded to the printer name, then ordinary expansion is
-@@ -448,7 +449,7 @@ the printer has no corresponding command."
-      ;; Print to the (unnamed) default printer.  If there is a DVI
-      ;; file print via Dvips.  If not, pass the output file (which
-      ;; should then be a Postscript or PDF file) directly to lpr.
--     "{ test -e %s.dvi && %(o?)dvips -f %r %s | lpr; } || lpr %o"
-+     "{ test -e %d && %(o?)dvips -f %r %s | lpr; } || lpr %o"
-      ;; Show the queue for the (unnamed) default printer.
-      "lpq"))
-   "List of available printers.
-@@ -563,6 +564,8 @@ string."
-     ("%(cntxcom)" ConTeXt-expand-command)
-     ("%(execopts)" ConTeXt-expand-options)
-     ("%(extraopts)" (lambda () TeX-command-extra-options))
-+    ("%(output-dir)" (lambda () (TeX--output-dir-arg "--output-directory=")))
-+    ("%(o-dir)" (lambda () (TeX--output-dir-arg "-o ")))
-     ("%S" TeX-source-correlate-expand-options)
-     ("%dS" TeX-source-specials-view-expand-options)
-     ("%cS" TeX-source-specials-view-expand-client)
-@@ -632,6 +635,9 @@ string."
-     ("%n" TeX-current-line)
-     ("%d" TeX-active-master-with-quotes "dvi" t)
-     ("%f" TeX-active-master-with-quotes "ps" t)
-+    ("%(O?aux)" TeX-active-master-with-quotes "aux" t)
-+    ("%(O?idx)" TeX-active-master-with-quotes "idx" t)
-+    ("%(O?pdf)" TeX-active-master-with-quotes "pdf" t)
-     ("%o" (lambda nil (TeX-active-master-with-quotes (TeX-output-extension) 
t)))
-     ;; for source specials the file name generated for the xdvi
-     ;; command needs to be relative to the master file, just in
-@@ -2210,26 +2216,20 @@ Used as a default in TeX, LaTeX and docTeX mode.")
- If prefix ARG is non-nil, not only remove intermediate but also
- output files."
-   (interactive "P")
--  (let* ((mode-prefix (TeX-mode-prefix))
--         (suffixes (append (symbol-value
--                            (intern (concat mode-prefix
--                                            "-clean-intermediate-suffixes")))
--                           (when arg
--                             (symbol-value
--                              (intern (concat mode-prefix
--                                              "-clean-output-suffixes"))))))
--         (master (TeX-active-master))
--         (master-dir (file-name-directory master))
--         (regexp (concat "\\("
--                         (regexp-quote (file-name-nondirectory master)) "\\|"
--                         (regexp-quote (TeX-region-file nil t))
--                         "\\)"
--                         "\\("
--                         (mapconcat 'identity suffixes "\\|")
--                         "\\)\\'"
--                         "\\|" (regexp-quote (TeX-region-file t t))))
--         (files (when regexp
--                  (directory-files (or master-dir ".") nil regexp))))
-+  (let* (;; Add output extension then remove it, to make sure we get the 
correct
-+       ;; directory in cases TeX-output-dir is non-nil
-+       (master (file-name-sans-extension (TeX-active-master 
(TeX-output-extension))))
-+       (master-dir (file-name-directory master))
-+       (regexp (concat "\\("
-+                       (regexp-quote (file-name-nondirectory master)) "\\|"
-+                       (regexp-quote (TeX-region-file nil t))
-+                       "\\)"
-+                       "\\("
-+                       (TeX--clean-extensions-regexp arg)
-+                       "\\)\\'"
-+                       "\\|" (regexp-quote (TeX-region-file t t))))
-+       (files (when (and regexp (or (not master-dir) (file-exists-p 
master-dir)))
-+                (directory-files (or master-dir ".") nil regexp))))
-     (if files
-         (when (or (not TeX-clean-confirm)
-                   (dired-mark-pop-up " *Deletions*" 'delete
-@@ -2241,6 +2241,21 @@ output files."
-             (delete-file (concat master-dir file))))
-       (message "No files to be deleted"))))
- 
-+(defun TeX--clean-extensions-regexp (&optional arg)
-+  "Returns a regexp to match extensions that should be cleaned by TeX-clean.
-+If the optional argument ARG is non-nil then output files are included"
-+  (let* ((mode-prefix (TeX-mode-prefix))
-+         (suffixes (and mode-prefix
-+                        (append (symbol-value
-+                                 (intern (concat mode-prefix
-+                                                 
"-clean-intermediate-suffixes")))
-+                                (when arg
-+                                  (symbol-value
-+                                   (intern (concat mode-prefix
-+                                                   
"-clean-output-suffixes"))))))))
-+    (when suffixes
-+      (mapconcat 'identity suffixes "\\|"))))
-+
- ;;; Master File
- 
- (defcustom TeX-master t
-@@ -2398,21 +2413,33 @@ name of master file if it cannot be determined 
otherwise."
-                     TeX-master
-                   my-name)))
- 
--      (if (TeX-match-extension name)
--          ;; If it already has an extension...
--          (if (equal extension TeX-default-extension)
--              ;; Use instead of the default extension
--              (setq extension nil)
--            ;; Otherwise drop it.
--            (setq name (TeX-strip-extension name))))
--
--      ;; Remove directory if needed.
--      (if nondirectory
--          (setq name (file-name-nondirectory name)))
--
--      (if extension
--          (concat name "." extension)
--        name))))
-+    (if (TeX-match-extension name)
-+        ;; If it already has an extension...
-+        (if (equal extension TeX-default-extension)
-+            ;; Use instead of the default extension
-+            (setq extension nil)
-+          ;; Otherwise drop it.
-+          (setq name (TeX-strip-extension name))))
-+
-+    (let* ((output-dir (and (or (stringp TeX-master)
-+                                (buffer-file-name)) ;; otherwise `name' is 
"<none>"
-+                            (TeX-master-output-dir nondirectory
-+                                                   (concat name "." 
TeX-default-extension))))
-+           (reg (and output-dir (TeX--clean-extensions-regexp t)))
-+           (is-output-ext
-+            (and reg (or (string-match-p reg (concat "." extension))
-+                         (string= "prv" extension)))))
-+      (if is-output-ext
-+          ;; If output extesnion, use output-dir (directory is already 
removed)
-+          ;; in TeX-master-output-dir
-+          (setq name (concat output-dir (file-name-nondirectory name)))
-+        ;; Remove directory if needed.
-+        (if nondirectory
-+            (setq name (file-name-nondirectory name))))
-+
-+        (if extension
-+          (concat name "." extension)
-+        name)))))
- 
- (defun TeX-master-directory ()
-   "Directory of master file."
-@@ -2494,6 +2521,35 @@ be relative to that."
-   :group 'TeX-file
-   :type 'string)
- 
-+(defcustom TeX-output-dir nil
-+  "The directory where the output files will be generated. The
-+  directory cannot start with a `.'.
-+
-+If this variable is nil, AUCTeX will assume that the output
-+directory is the same as the directory of TeX-master."
-+  :group 'TeX-file
-+  :local t
-+  :safe 'string-or-null-p
-+  :type 'string)
-+
-+(defun TeX-master-output-dir (&optional relative-to-master master-file)
-+  (with-current-buffer
-+      (find-file-noselect (or master-file (TeX-master-file t)) t)
-+    (let ((out-dir (and TeX-output-dir
-+                        (file-name-as-directory
-+                         (abbreviate-file-name
-+                          (substitute-in-file-name
-+                           (expand-file-name
-+                            TeX-output-dir
-+                            (file-name-directory buffer-file-name))))))))
-+      ;; Make sure the directory exists
-+      (when out-dir
-+        (unless (file-exists-p out-dir)
-+          (make-directory (file-name-as-directory out-dir) t))
-+        (if relative-to-master
-+            (file-relative-name out-dir)
-+          out-dir)))))
-+
- (defcustom TeX-style-local "style"
-   "Directory containing hand generated TeX information.
- 
-@@ -2502,6 +2558,14 @@ be relative to that."
-   :group 'TeX-file
-   :type 'string)
- 
-+(defun TeX--output-dir-arg (argname)
-+  (let ((out-dir (if TeX-current-process-region-p
-+                     (file-name-directory TeX-region)
-+                   (TeX-master-output-dir t))))
-+    (if out-dir
-+        (concat argname "\"" out-dir "\"")
-+      "")))
-+
- (defun TeX-split-string (regexp string)
-   "Return a list of strings.
- Given REGEXP the STRING is split into sections which in string was
diff --git a/preview.el.in b/preview.el.in
index 1d9cc93..82b35f8 100644
--- a/preview.el.in
+++ b/preview.el.in
@@ -2771,7 +2771,7 @@ list of LaTeX commands is inserted just before 
\\begin{document}."
 (defcustom preview-LaTeX-command '("%`%l \"\\nonstopmode\\nofiles\
 \\PassOptionsToPackage{" ("," . preview-required-option-list) "}{preview}\
 \\AtBeginDocument{\\ifx\\ifPreview\\undefined"
-preview-default-preamble "\\fi}\"%' \"\\detokenize{\" %t \"}\"")
+preview-default-preamble "\\fi}\"%' \"\\detokenize{\" %(t-filename-only) 
\"}\"")
   ;; Since TeXLive 2018, the default encoding for LaTeX files has been
   ;; changed to UTF-8 if used with classic TeX or pdfTeX.  I.e.,
   ;; \usepackage[utf8]{inputenc} is enabled by default in (pdf)latex.
@@ -3980,7 +3980,7 @@ If FORMAT-CONS is non-nil, a previous format may get 
reused."
 \\catcode`/ 0 %
 \\def\\AUCTEXINPUT##1{\\catcode`/ 12\\relax\\catcode`\\ 
9\\relax\\input\\detokenize{##1}\\relax}%
 \\let\\dump\\PREVIEWdump\\dump}\\input mylatex.ltx \\relax%\n" nil dump-file)
-      (TeX-save-document master)
+      (TeX-save-document 'TeX-master-file)
       (prog1 (preview-generate-preview master command)
         (add-hook 'kill-emacs-hook #'preview-cleanout-tempfiles t)
         (setq TeX-sentinel-function
@@ -4067,7 +4067,7 @@ stored in `preview-dumped-alist'."
 (defun preview-document ()
   "Run preview on master document."
   (interactive)
-  (TeX-save-document (TeX-master-file))
+  (TeX-save-document 'TeX-master-file)
   (setq TeX-current-process-region-p nil)
   (preview-generate-preview
    (TeX-master-file)
diff --git a/tex-bar.el b/tex-bar.el
index 9866cc5..fa2ccdd 100644
--- a/tex-bar.el
+++ b/tex-bar.el
@@ -136,13 +136,13 @@ the argument BUTTON-ALIST in function 
`toolbarx-install-toolbar'."
 (defcustom TeX-bar-TeX-button-alist
   '((tex :image (lambda nil (if TeX-PDF-mode "pdftex" "tex"))
          :command (progn
-                    (TeX-save-document (TeX-master-file))
+                    (TeX-save-document 'TeX-master-file)
                     (TeX-command "TeX" 'TeX-master-file -1))
          :help (lambda (&rest ignored)
                  (TeX-bar-help-from-command-list "TeX")))
     (pdftex :image "pdftex"
             :command (progn
-                       (TeX-save-document (TeX-master-file))
+                       (TeX-save-document 'TeX-master-file)
                        (TeX-command "PDFTeX" 'TeX-master-file -1))
             :help (lambda (&rest ignored)
                     (TeX-bar-help-from-command-list "PDFTeX")))
@@ -277,13 +277,13 @@ the argument BUTTON-ALIST in function 
`toolbarx-install-toolbar'."
 (defcustom TeX-bar-LaTeX-button-alist
   '((latex :image (lambda nil (if TeX-PDF-mode "pdftex" "tex"))
            :command (progn
-                      (TeX-save-document (TeX-master-file))
+                      (TeX-save-document 'TeX-master-file)
                       (TeX-command "LaTeX" 'TeX-master-file -1))
            :help (lambda (&rest ignored)
                    (TeX-bar-help-from-command-list "LaTeX")))
     (pdflatex :image "pdftex"
               :command (progn
-                         (TeX-save-document (TeX-master-file))
+                         (TeX-save-document 'TeX-master-file)
                          (TeX-command "PDFLaTeX" 'TeX-master-file -1))
               :help (lambda (&rest ignored)
                       (TeX-bar-help-from-command-list "PDFLaTeX")))
diff --git a/tex-buf.el b/tex-buf.el
index 57db910..8464b59 100644
--- a/tex-buf.el
+++ b/tex-buf.el
@@ -61,8 +61,6 @@
   :group 'TeX-command
   :type 'string)
 
-(defvar TeX-command-file-function)
-
 ;;; Interactive Commands
 ;;
 ;; The general idea is, that there is one process and process buffer
@@ -77,23 +75,23 @@
 ;; THEN ``the'' process is the region process
 ;; ELSE ``the'' process is the master file (of the current buffer) process
 
-(defun TeX-save-document (name)
+(defun TeX-save-document (name-or-file-fn)
   "Save all files belonging to the current document.
 Return non-nil if document needs to be re-TeX'ed."
   (interactive (list 'TeX-master-file))
-  (if (string-equal name "")
-      (setq name 'TeX-master-file))
-
-  (TeX-check-files (TeX-get-command-filename name (TeX-output-extension))
-                   (cons (TeX-get-command-filename name) (TeX-style-list))
+  (TeX-check-files (TeX--concat-ext name-or-file-fn (TeX-output-extension))
+                   (cons (TeX--concat-ext name-or-file-fn) (TeX-style-list))
                    TeX-file-extensions))
 
-(defun TeX-get-command-filename (name &optional extension)
-  (if (or TeX-command-file-function (functionp name))
-      (funcall (or TeX-command-file-function name) extension)
-    (if extension
-        (concat name "." extension)
-      name)))
+(defun TeX--concat-ext (name-or-file-fn &optional extension)
+  "Concatenate extension to NAME-OR-FILE-FN if it is a
+string. Otherwise assumes NAME-OR-FILE-FN is a callable function
+and calls it with extension as argument"
+  (if (stringp name-or-file-fn)
+      (if extension
+          (concat name-or-file-fn "." extension)
+        name-or-file-fn)
+    (funcall name-or-file-fn extension)))
 
 (defun TeX-command-master (&optional override-confirm)
   "Run command on the current document.
@@ -101,9 +99,9 @@ Return non-nil if document needs to be re-TeX'ed."
 If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
 depend on it being positive instead of the entry in `TeX-command-list'."
   (interactive "P")
-  (let ((TeX-command-file-function 'TeX-master-file))
-   (TeX-command (TeX-command-query (TeX-master-file nil nil t))
-               'TeX-master-file override-confirm)))
+  (TeX-master-file nil nil t)  ;; call to ask if necessary
+  (TeX-command (TeX-command-query 'TeX-master-file)
+               'TeX-master-file override-confirm))
 
 (defvar TeX-command-region-begin nil)
 (defvar TeX-command-region-end nil)
@@ -195,10 +193,8 @@ all text after `TeX-trailer-start'."
   ;; `nondirectory' argument, otherwise `TeX-command-default' called
   ;; within `TeX-command-query' won't work in included files not
   ;; placed in `TeX-master-directory'.
-  (let ((TeX-command-file-function 'TeX-region-file))
-    (TeX-command (TeX-command-query (TeX-region-file))
-                 TeX-command-file-function
-                 override-confirm)))
+  (TeX-command (TeX-command-query 'TeX-region-file) 'TeX-region-file
+               override-confirm))
 
 (defun TeX-command-buffer (&optional override-confirm)
   "Run TeX on the current buffer.
@@ -480,7 +476,7 @@ Do you want to select one of these engines? "
 
 FILE-FN is the symbol of a function returning a file name.  The
 function has one optional argument, the extension to use on the
-file.
+file. Valid choices are `TeX-master-file' and `TeX-region-file'
 
 Use the information in `TeX-command-list' to determine how to run
 the command.
@@ -542,11 +538,9 @@ remember to add /Library/TeX/texbin/ to your PATH"
                           ""))))
 
     ;; Now start the process
-    (let ((TeX-command-file-function file-fn)
-          (file (funcall file-fn)))
+    (let ((file (funcall file-fn)))
       (TeX-process-set-variable file 'TeX-command-next TeX-command-Show)
-      (funcall hook name command file)))
-  )
+      (funcall hook name command file))))
 
 (defun TeX-command-expand (command &optional list)
   "Expand COMMAND for `TeX-active-master' as described in LIST.
@@ -583,7 +577,7 @@ without further expansion."
     TeX-expand-command))
 
 (defun TeX-active-master-with-quotes
-    (&optional extension nondirectory ask extra)
+    (&optional extension nondirectory ask extra preprocess-fn)
   "Return the current master or region file name with quote for shell.
 Pass arguments EXTENSION NONDIRECTORY ASK to `TeX-active-master'.
 If the returned file name contains space, enclose it within
@@ -595,7 +589,9 @@ the following three conditions are met:
   2. \" \\input\" is supplemented
   3. EXTRA is non-nil (default when expanding \"%T\")
 Adjust dynamically bound variable `TeX-expand-pos' to avoid possible
-infinite loop in `TeX-command-expand'.
+infinite loop in `TeX-command-expand'. If PREPROCESS-FN is non-nil then
+it is called with the filename as an argument and the results is
+enclosed instead of the filename.
 
 Helper function of `TeX-command-expand'. Use only within entries in
 `TeX-expand-list-builtin' and `TeX-expand-list'."
@@ -622,7 +618,11 @@ Helper function of `TeX-command-expand'. Use only within 
entries in
                 ;; recognize non-ascii characters in the file name
                 ;; when \input precedes.
                 "\\detokenize{ %s }" "%s")
-            (concat quote-for-space raw quote-for-space)))))
+            (concat quote-for-space
+                    (if preprocess-fn
+                        (funcall preprocess-fn raw)
+                      raw)
+                    quote-for-space)))))
     ;; Advance past the file name in order to
     ;; prevent expanding any substring of it.
     (setq TeX-expand-pos
@@ -757,15 +757,11 @@ omitted) and `TeX-region-file'."
         (setq cmd (funcall command)
               TeX-command-sequence-command command))
        (t
-        (setq cmd (TeX-command-default
-                   ;; File function should be called with nil `nondirectory'
-                   ;; argument, otherwise `TeX-command-sequence' won't work in
-                   ;; included files not placed in `TeX-master-directory'.  In
-                   ;; addition, `TeX-master-file' is called with the third
-                   ;; argument (`ask') set to t, so that the master file is
-                   ;; properly set.  This is also what `TeX-command-master'
-                   ;; does.
-                   (funcall TeX-command-sequence-file-function nil nil t))
+        ;; We first call `TeX-master-file' with the third argument
+        ;; (`ask') set to t, so that the master file is properly set.
+        ;; This is also what `TeX-command-master' does.
+        (funcall TeX-command-sequence-file-function nil nil t)
+        (setq cmd (TeX-command-default TeX-command-sequence-file-function)
               TeX-command-sequence-command t)))
       (TeX-command cmd TeX-command-sequence-file-function 0)
       (when reset
@@ -807,25 +803,28 @@ omitted) and `TeX-region-file'."
 
 (defvar TeX-command-history nil)
 
-(defun TeX-command-default (name)
+(defun TeX-command-default (name-or-file-fn)
   "Guess the next command to be run on NAME."
-  (let ((command-next nil))
+  (let ((command-next nil)
+        (name (TeX--concat-ext name-or-file-fn)))
     (cond (;; name might be absolute or relative, so expand it for
            ;; comparison.
+           ;; NOTE: Instead of comparing the filename to TeX-region-file
+           ;; we can use name-or-file-fn to check if it is 'TeX-region-file
            (if (string-equal (expand-file-name name)
                              (expand-file-name (TeX-region-file)))
-               (TeX-check-files (TeX-get-command-filename name 
(TeX-output-extension))
+               (TeX-check-files (TeX-region-file (TeX-output-extension))
                                 ;; Each original will be checked for all dirs
                                 ;; in `TeX-check-path' so this needs to be just
                                 ;; a filename without directory.
-                                (list (file-relative-name 
(TeX-get-command-filename name)))
+                                (list (file-relative-name name))
                                 TeX-file-extensions)
-             (TeX-save-document 'TeX-master-file))
+             (TeX-save-document name-or-file-fn))
            TeX-command-default)
           ((and (memq major-mode '(doctex-mode latex-mode))
                 ;; Want to know if bib file is newer than .bbl
                 ;; We don't care whether the bib files are open in emacs
-                (TeX-check-files (TeX-get-command-filename name "bbl")
+                (TeX-check-files (TeX--concat-ext name-or-file-fn "bbl")
                                  (mapcar #'car
                                          (LaTeX-bibliography-list))
                                  (append BibTeX-file-extensions
@@ -842,20 +841,20 @@ omitted) and `TeX-region-file'."
             (member
              (setq command-next
                    (TeX-process-get-variable
-                    (TeX-get-command-filename name)
+                    name
                     'TeX-command-next
                     (or (and TeX-PDF-mode (TeX-PDF-from-DVI))
                         TeX-command-Show)))
              (list "Dvips" "Dvipdfmx" TeX-command-Show))
-            (cdr (assoc (expand-file-name (TeX-get-command-filename "idx"))
+            (cdr (assoc (expand-file-name (TeX--concat-ext name-or-file-fn 
"idx"))
                         LaTeX-idx-changed-alist)))
            "Index")
           (command-next)
           (TeX-command-Show))))
 
-(defun TeX-command-query (name)
+(defun TeX-command-query (name-or-file-fn)
   "Query the user for what TeX command to use."
-  (let* ((default (TeX-command-default name))
+  (let* ((default (TeX-command-default name-or-file-fn))
          (completion-ignore-case t)
          (answer (or TeX-command-force
                      (completing-read
@@ -1152,7 +1151,6 @@ run of `TeX-run-TeX', use
   ;; Presence of error is reported inside `TeX-TeX-sentinel-check'
   (let ((current-master (TeX-master-file))
         (idx-file nil) (element nil))
-
     ;; the current master file is saved because error routines are
     ;; parsed in other buffers;
     (setq TeX-error-report-switches
@@ -1166,7 +1164,7 @@ run of `TeX-run-TeX', use
     ;; Store md5 hash of the index file before running LaTeX.
     (and (memq major-mode '(doctex-mode latex-mode))
          (prog1 (file-exists-p
-                 (setq idx-file (expand-file-name (TeX-get-command-filename 
file "idx"))))
+                 (setq idx-file (expand-file-name (TeX-active-master "idx"))))
            ;; In order to avoid confusion and pollution of
            ;; `LaTeX-idx-md5-alist', remove from this alist all md5 hashes of
            ;; the current index file.  Note `assq-delete-all' doesn't work with
@@ -1248,7 +1246,7 @@ run of `TeX-run-TeX', use
                  ;; `default-directory', then we have to expand `file' 
file-name
                  ;; in the same directory of `TeX-command-buffer'.
                  (assoc (with-current-buffer TeX-command-buffer
-                            (expand-file-name (TeX-get-command-filename 
"idx")))
+                            (expand-file-name (TeX-active-master "idx")))
                         LaTeX-idx-changed-alist))
       (setq LaTeX-idx-changed-alist (delq element LaTeX-idx-changed-alist)))
     (if TeX-process-asynchronous
@@ -1696,17 +1694,18 @@ Rerun to get mark in right position\\." nil t)
          (setq TeX-command-next TeX-command-default)))
 
   ;; Check whether the idx file changed.
-  (let ((idx-file nil) (master nil))
+  (let (idx-file)
     (and (file-exists-p
           (setq idx-file
-                (setq master
-                      (with-current-buffer TeX-command-buffer
-                        (expand-file-name (TeX-active-master "idx"))))))
+                (with-current-buffer TeX-command-buffer
+                  (expand-file-name (TeX-active-master "idx")))))
          ;; imakeidx package automatically runs makeindex, thus, we need to be
          ;; sure .ind file isn't newer than .idx.
          (TeX-check-files (with-current-buffer TeX-command-buffer
                             (expand-file-name (TeX-active-master "ind")))
-                          (list (file-relative-name idx-file)) '("idx"))
+                          (with-current-buffer TeX-command-buffer
+                            (list (file-relative-name (TeX-active-master))))
+                          '("idx"))
          (with-temp-buffer
            (insert-file-contents idx-file)
            (not (equal
@@ -2010,9 +2009,6 @@ command."
 (defvar TeX-current-process-region-p nil
   "This variable is set to t iff the last TeX command is on a region.")
 
-(defvar TeX-command-file-function nil
-  "The current file function in TeX-command.")
-
 (defun TeX-active-process ()
   "Return the active process for the current buffer."
   (TeX-process (TeX-active-master)))
@@ -2248,7 +2244,8 @@ The compatibility argument IGNORE is ignored."
   ;; third argument `ask'.  For example, it's used in `TeX-command-sequence',
   ;; where we don't know which function has to be called.  Keep this in mind
   ;; should you want to use another argument here.
-  (concat (if nondirectory "" (TeX-master-directory))
+  (concat (or (TeX-master-output-dir nondirectory)
+              (if nondirectory "" (TeX-master-directory)))
           (cond ((eq extension t)
                  (concat TeX-region "." TeX-default-extension))
                 (extension
diff --git a/tex.el b/tex.el
index df9fbf1..c622891 100644
--- a/tex.el
+++ b/tex.el
@@ -586,6 +586,7 @@ string."
     ;; adds suitable quotes for use in shell command line.
     ("%s" TeX-active-master-with-quotes nil t)
     ("%t" TeX-active-master-with-quotes t t)
+    ("%(t-filename-only)" TeX-active-master-with-quotes t t nil nil 
file-name-nondirectory)
     ;; If any TeX codes appear in the interval between %` and %', move
     ;; all of them after the interval and supplement " \input".  The
     ;; appearance is marked by leaving the bind to `TeX-command-text'
@@ -2216,18 +2217,19 @@ Used as a default in TeX, LaTeX and docTeX mode.")
 If prefix ARG is non-nil, not only remove intermediate but also
 output files."
   (interactive "P")
-  (let* (;; Add output extension then remove it, to make sure we get the 
correct
-        ;; directory in cases TeX-output-dir is non-nil
+  (let* (;; Call with output extension then remove it, to make sure we
+        ;; get the correct directory in cases TeX-output-dir is
+        ;; non-nil
         (master (file-name-sans-extension (TeX-active-master 
(TeX-output-extension))))
         (master-dir (file-name-directory master))
         (regexp (concat "\\("
                         (regexp-quote (file-name-nondirectory master)) "\\|"
-                        (regexp-quote (TeX-region-file nil t))
+                        (regexp-quote (file-name-nondirectory (TeX-region-file 
nil t)))
                         "\\)"
                         "\\("
                         (TeX--clean-extensions-regexp arg)
                         "\\)\\'"
-                        "\\|" (regexp-quote (TeX-region-file t t))))
+                        "\\|" (regexp-quote (file-name-nondirectory 
(TeX-region-file t t)))))
         (files (when (and regexp (or (not master-dir) (file-exists-p 
master-dir)))
                  (directory-files (or master-dir ".") nil regexp))))
     (if files
@@ -2559,9 +2561,7 @@ be relative to that."
   :type 'string)
 
 (defun TeX--output-dir-arg (argname)
-  (let ((out-dir (if TeX-current-process-region-p
-                     (file-name-directory TeX-region)
-                   (TeX-master-output-dir t))))
+  (let ((out-dir (TeX-master-output-dir t)))
     (if out-dir
         (concat argname "\"" out-dir "\"")
       "")))

commit b650ab2847b1a775f36117f0d6077aecfff0419a
Author: Al Haji-Ali <a.hajiali@hw.ac.uk>
Date:   Fri Feb 26 14:22:26 2021 +0000

    Simplified implementation and got rid of Emacs 25 features

diff --git a/etc/auctex-output-dir.patch b/etc/auctex-output-dir.patch
index 014d08e..1725cc4 100644
--- a/etc/auctex-output-dir.patch
+++ b/etc/auctex-output-dir.patch
@@ -232,7 +232,7 @@ index b334b7fa..93e909d6 100644
    "Return the active process for the current buffer."
    (TeX-process (TeX-active-master)))
 diff --git a/tex.el b/tex.el
-index b59aee05..a69c57a5 100644
+index b59aee05..df9fbf1c 100644
 --- a/tex.el
 +++ b/tex.el
 @@ -89,6 +89,7 @@
@@ -393,78 +393,78 @@ index b59aee05..a69c57a5 100644
      (if files
          (when (or (not TeX-clean-confirm)
                    (dired-mark-pop-up " *Deletions*" 'delete
-@@ -2241,6 +2241,19 @@ output files."
+@@ -2241,6 +2241,21 @@ output files."
              (delete-file (concat master-dir file))))
        (message "No files to be deleted"))))
  
 +(defun TeX--clean-extensions-regexp (&optional arg)
 +  "Returns a regexp to match extensions that should be cleaned by TeX-clean.
 +If the optional argument ARG is non-nil then output files are included"
-+  (when-let ((mode-prefix (TeX-mode-prefix))
-+             (suffixes (append (symbol-value
-+                                (intern (concat mode-prefix
-+                                                
"-clean-intermediate-suffixes")))
-+                               (when arg
-+                                 (symbol-value
-+                                  (intern (concat mode-prefix
-+                                                  
"-clean-output-suffixes")))))))
-+    (mapconcat 'identity suffixes "\\|")))
++  (let* ((mode-prefix (TeX-mode-prefix))
++         (suffixes (and mode-prefix
++                        (append (symbol-value
++                                 (intern (concat mode-prefix
++                                                 
"-clean-intermediate-suffixes")))
++                                (when arg
++                                  (symbol-value
++                                   (intern (concat mode-prefix
++                                                   
"-clean-output-suffixes"))))))))
++    (when suffixes
++      (mapconcat 'identity suffixes "\\|"))))
 +
  ;;; Master File
  
  (defcustom TeX-master t
-@@ -2334,6 +2347,21 @@ this variable to \"<none>\"."
-                                                    'path))
-              (TeX-add-local-master))))))
+@@ -2398,21 +2413,33 @@ name of master file if it cannot be determined 
otherwise."
+                     TeX-master
+                   my-name)))
  
-+(defun TeX-master-output-file (&optional extension relative-to-master)
-+  "Returns an output file based on `TeX-output-dir' in the
-+master-file, opening it if necessary. if the optional argument
-+EXTENSION is non-nil it is appended as an extension to the output
-+file. If EXTENSION is t then (TeX-output-extension) is used."
-+  (interactive)
-+  (if (eq extension t)
-+      (setq extension (TeX-output-extension)))
-+  (if-let ((output-file
-+            (when-let ((master-out-dir (TeX-master-output-dir 
relative-to-master)))
-+              (concat master-out-dir (file-name-nondirectory 
(TeX-master-file))))))
-+      (if extension (concat output-file "." extension)
-+        output-file)
-+    (TeX-master-file extension relative-to-master)))
-+
- (defun TeX-master-file (&optional extension nondirectory ask)
-   "Set and return the name of the master file for the current document.
- 
-@@ -2394,9 +2422,14 @@ name of master file if it cannot be determined 
otherwise."
-          ;; Ask the user (but add it as a local variable).
-          (ask (TeX-master-file-ask)))))
- 
--    (let ((name (if (stringp TeX-master)
--                    TeX-master
--                  my-name)))
-+    (if (and (when-let (reg (TeX--clean-extensions-regexp t))
-+               (or (string-match-p reg (concat "." extension))
-+                   (string= "prv" extension)))
-+             (TeX-master-output-dir))
-+        (TeX-master-output-file extension nondirectory)
-+      (let ((name (if (stringp TeX-master)
-+                  TeX-master
-+                my-name)))
- 
-       (if (TeX-match-extension name)
-           ;; If it already has an extension...
-@@ -2411,8 +2444,8 @@ name of master file if it cannot be determined 
otherwise."
-           (setq name (file-name-nondirectory name)))
- 
-       (if extension
+-      (if (TeX-match-extension name)
+-          ;; If it already has an extension...
+-          (if (equal extension TeX-default-extension)
+-              ;; Use instead of the default extension
+-              (setq extension nil)
+-            ;; Otherwise drop it.
+-            (setq name (TeX-strip-extension name))))
+-
+-      ;; Remove directory if needed.
+-      (if nondirectory
+-          (setq name (file-name-nondirectory name)))
+-
+-      (if extension
 -          (concat name "." extension)
 -        name))))
-+        (concat name "." extension)
-+      name)))))
++    (if (TeX-match-extension name)
++        ;; If it already has an extension...
++        (if (equal extension TeX-default-extension)
++            ;; Use instead of the default extension
++            (setq extension nil)
++          ;; Otherwise drop it.
++          (setq name (TeX-strip-extension name))))
++
++    (let* ((output-dir (and (or (stringp TeX-master)
++                                (buffer-file-name)) ;; otherwise `name' is 
"<none>"
++                            (TeX-master-output-dir nondirectory
++                                                   (concat name "." 
TeX-default-extension))))
++           (reg (and output-dir (TeX--clean-extensions-regexp t)))
++           (is-output-ext
++            (and reg (or (string-match-p reg (concat "." extension))
++                         (string= "prv" extension)))))
++      (if is-output-ext
++          ;; If output extesnion, use output-dir (directory is already 
removed)
++          ;; in TeX-master-output-dir
++          (setq name (concat output-dir (file-name-nondirectory name)))
++        ;; Remove directory if needed.
++        (if nondirectory
++            (setq name (file-name-nondirectory name))))
++
++        (if extension
++          (concat name "." extension)
++        name)))))
  
  (defun TeX-master-directory ()
    "Directory of master file."
-@@ -2494,6 +2527,38 @@ be relative to that."
+@@ -2494,6 +2521,35 @@ be relative to that."
    :group 'TeX-file
    :type 'string)
  
@@ -479,40 +479,38 @@ index b59aee05..a69c57a5 100644
 +  :safe 'string-or-null-p
 +  :type 'string)
 +
-+(defun TeX-master-buffer ()
-+  (let* ((master-file (TeX-master-file t)))
-+    (or (find-buffer-visiting master-file)
-+        (find-file-noselect master-file))))
-+
-+(defun TeX-master-output-dir (&optional relative-to-master)
-+  (with-current-buffer (TeX-master-buffer)
-+    (when-let ((out-dir (and TeX-output-dir
-+                             (file-name-as-directory
-+                              (abbreviate-file-name
-+                               (substitute-in-file-name
-+                                (expand-file-name
-+                                 TeX-output-dir
-+                                 (file-name-directory buffer-file-name))))))))
++(defun TeX-master-output-dir (&optional relative-to-master master-file)
++  (with-current-buffer
++      (find-file-noselect (or master-file (TeX-master-file t)) t)
++    (let ((out-dir (and TeX-output-dir
++                        (file-name-as-directory
++                         (abbreviate-file-name
++                          (substitute-in-file-name
++                           (expand-file-name
++                            TeX-output-dir
++                            (file-name-directory buffer-file-name))))))))
 +      ;; Make sure the directory exists
-+      (unless (file-exists-p out-dir)
-+        (make-directory (file-name-as-directory out-dir) t))
-+      (if relative-to-master
-+          (file-relative-name out-dir)
-+        out-dir))))
++      (when out-dir
++        (unless (file-exists-p out-dir)
++          (make-directory (file-name-as-directory out-dir) t))
++        (if relative-to-master
++            (file-relative-name out-dir)
++          out-dir)))))
 +
  (defcustom TeX-style-local "style"
    "Directory containing hand generated TeX information.
  
-@@ -2502,6 +2567,13 @@ be relative to that."
+@@ -2502,6 +2558,14 @@ be relative to that."
    :group 'TeX-file
    :type 'string)
  
 +(defun TeX--output-dir-arg (argname)
-+  (if-let ((out-dir (if TeX-current-process-region-p
-+                        (file-name-directory TeX-region)
-+                      (TeX-master-output-dir t))))
-+      (concat argname "\"" out-dir "\"")
-+    ""))
++  (let ((out-dir (if TeX-current-process-region-p
++                     (file-name-directory TeX-region)
++                   (TeX-master-output-dir t))))
++    (if out-dir
++        (concat argname "\"" out-dir "\"")
++      "")))
 +
  (defun TeX-split-string (regexp string)
    "Return a list of strings.
diff --git a/tex.el b/tex.el
index a69c57a..df9fbf1 100644
--- a/tex.el
+++ b/tex.el
@@ -2244,15 +2244,17 @@ output files."
 (defun TeX--clean-extensions-regexp (&optional arg)
   "Returns a regexp to match extensions that should be cleaned by TeX-clean.
 If the optional argument ARG is non-nil then output files are included"
-  (when-let ((mode-prefix (TeX-mode-prefix))
-             (suffixes (append (symbol-value
-                                (intern (concat mode-prefix
-                                                
"-clean-intermediate-suffixes")))
-                               (when arg
-                                 (symbol-value
-                                  (intern (concat mode-prefix
-                                                  
"-clean-output-suffixes")))))))
-    (mapconcat 'identity suffixes "\\|")))
+  (let* ((mode-prefix (TeX-mode-prefix))
+         (suffixes (and mode-prefix
+                        (append (symbol-value
+                                 (intern (concat mode-prefix
+                                                 
"-clean-intermediate-suffixes")))
+                                (when arg
+                                  (symbol-value
+                                   (intern (concat mode-prefix
+                                                   
"-clean-output-suffixes"))))))))
+    (when suffixes
+      (mapconcat 'identity suffixes "\\|"))))
 
 ;;; Master File
 
@@ -2347,21 +2349,6 @@ this variable to \"<none>\"."
                                                    'path))
              (TeX-add-local-master))))))
 
-(defun TeX-master-output-file (&optional extension relative-to-master)
-  "Returns an output file based on `TeX-output-dir' in the
-master-file, opening it if necessary. if the optional argument
-EXTENSION is non-nil it is appended as an extension to the output
-file. If EXTENSION is t then (TeX-output-extension) is used."
-  (interactive)
-  (if (eq extension t)
-      (setq extension (TeX-output-extension)))
-  (if-let ((output-file
-            (when-let ((master-out-dir (TeX-master-output-dir 
relative-to-master)))
-              (concat master-out-dir (file-name-nondirectory 
(TeX-master-file))))))
-      (if extension (concat output-file "." extension)
-        output-file)
-    (TeX-master-file extension relative-to-master)))
-
 (defun TeX-master-file (&optional extension nondirectory ask)
   "Set and return the name of the master file for the current document.
 
@@ -2422,30 +2409,37 @@ name of master file if it cannot be determined 
otherwise."
          ;; Ask the user (but add it as a local variable).
          (ask (TeX-master-file-ask)))))
 
-    (if (and (when-let (reg (TeX--clean-extensions-regexp t))
-               (or (string-match-p reg (concat "." extension))
-                   (string= "prv" extension)))
-             (TeX-master-output-dir))
-        (TeX-master-output-file extension nondirectory)
-      (let ((name (if (stringp TeX-master)
-                   TeX-master
-                 my-name)))
-
-      (if (TeX-match-extension name)
-          ;; If it already has an extension...
-          (if (equal extension TeX-default-extension)
-              ;; Use instead of the default extension
-              (setq extension nil)
-            ;; Otherwise drop it.
-            (setq name (TeX-strip-extension name))))
-
-      ;; Remove directory if needed.
-      (if nondirectory
-          (setq name (file-name-nondirectory name)))
-
-      (if extension
-         (concat name "." extension)
-       name)))))
+    (let ((name (if (stringp TeX-master)
+                    TeX-master
+                  my-name)))
+
+    (if (TeX-match-extension name)
+        ;; If it already has an extension...
+        (if (equal extension TeX-default-extension)
+            ;; Use instead of the default extension
+            (setq extension nil)
+          ;; Otherwise drop it.
+          (setq name (TeX-strip-extension name))))
+
+    (let* ((output-dir (and (or (stringp TeX-master)
+                                (buffer-file-name)) ;; otherwise `name' is 
"<none>"
+                            (TeX-master-output-dir nondirectory
+                                                   (concat name "." 
TeX-default-extension))))
+           (reg (and output-dir (TeX--clean-extensions-regexp t)))
+           (is-output-ext
+            (and reg (or (string-match-p reg (concat "." extension))
+                         (string= "prv" extension)))))
+      (if is-output-ext
+          ;; If output extesnion, use output-dir (directory is already removed)
+          ;; in TeX-master-output-dir
+          (setq name (concat output-dir (file-name-nondirectory name)))
+        ;; Remove directory if needed.
+        (if nondirectory
+            (setq name (file-name-nondirectory name))))
+
+        (if extension
+           (concat name "." extension)
+         name)))))
 
 (defun TeX-master-directory ()
   "Directory of master file."
@@ -2538,26 +2532,23 @@ directory is the same as the directory of TeX-master."
   :safe 'string-or-null-p
   :type 'string)
 
-(defun TeX-master-buffer ()
-  (let* ((master-file (TeX-master-file t)))
-    (or (find-buffer-visiting master-file)
-        (find-file-noselect master-file))))
-
-(defun TeX-master-output-dir (&optional relative-to-master)
-  (with-current-buffer (TeX-master-buffer)
-    (when-let ((out-dir (and TeX-output-dir
-                             (file-name-as-directory
-                              (abbreviate-file-name
-                               (substitute-in-file-name
-                                (expand-file-name
-                                 TeX-output-dir
-                                 (file-name-directory buffer-file-name))))))))
+(defun TeX-master-output-dir (&optional relative-to-master master-file)
+  (with-current-buffer
+      (find-file-noselect (or master-file (TeX-master-file t)) t)
+    (let ((out-dir (and TeX-output-dir
+                        (file-name-as-directory
+                         (abbreviate-file-name
+                          (substitute-in-file-name
+                           (expand-file-name
+                            TeX-output-dir
+                            (file-name-directory buffer-file-name))))))))
       ;; Make sure the directory exists
-      (unless (file-exists-p out-dir)
-        (make-directory (file-name-as-directory out-dir) t))
-      (if relative-to-master
-          (file-relative-name out-dir)
-        out-dir))))
+      (when out-dir
+        (unless (file-exists-p out-dir)
+          (make-directory (file-name-as-directory out-dir) t))
+        (if relative-to-master
+            (file-relative-name out-dir)
+          out-dir)))))
 
 (defcustom TeX-style-local "style"
   "Directory containing hand generated TeX information.
@@ -2568,11 +2559,12 @@ be relative to that."
   :type 'string)
 
 (defun TeX--output-dir-arg (argname)
-  (if-let ((out-dir (if TeX-current-process-region-p
-                        (file-name-directory TeX-region)
-                      (TeX-master-output-dir t))))
-      (concat argname "\"" out-dir "\"")
-    ""))
+  (let ((out-dir (if TeX-current-process-region-p
+                     (file-name-directory TeX-region)
+                   (TeX-master-output-dir t))))
+    (if out-dir
+        (concat argname "\"" out-dir "\"")
+      "")))
 
 (defun TeX-split-string (regexp string)
   "Return a list of strings.

commit ce1057d689df8b4e80824ccba356fba446108b00
Author: Al Haji-Ali <a.hajiali@hw.ac.uk>
Date:   Wed Feb 24 09:31:32 2021 +0000

    Complete implementation of Tex-build

diff --git a/etc/auctex-output-dir.patch b/etc/auctex-output-dir.patch
new file mode 100644
index 0000000..014d08e
--- /dev/null
+++ b/etc/auctex-output-dir.patch
@@ -0,0 +1,519 @@
+diff --git a/preview.el.in b/preview.el.in
+index e22d2419..a1844a3b 100644
+--- a/preview.el.in
++++ b/preview.el.in
+@@ -552,7 +552,7 @@ if you customize this."
+   :type 'string)
+ 
+ (defcustom preview-pdf2dsc-command
+-  "pdf2dsc %s.pdf %m/preview.dsc"
++  "pdf2dsc %(O?pdf) %m/preview.dsc"
+   "Command used for generating dsc from a PDF file."
+   :group 'preview-latex
+   :type 'string)
+@@ -3155,7 +3155,9 @@ later while in use."
+                 topdir
+                 0))
+     (shell-quote-argument
+-     (concat (file-name-as-directory (file-name-nondirectory topdir))
++     (concat (file-name-as-directory
++              ;; Don't use topdir, because %m expects the path to be relative 
to master
++              (TeX-active-master "prv" t))
+              (file-name-nondirectory (nth 0 TeX-active-tempdir))))))
+ 
+ (defun preview-parse-counters (string)
+@@ -3911,8 +3913,8 @@ This is passed through `preview-do-replacements'."
+   ;; If -kanji option exists, pick it up as the second match.
+   ;; Discard all other options.
+     ("\\`\\([^ ]+\\)\
+-\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|-\\(?:[^ 
\\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*\\(.*\\)\\'"
+-     . ("\\1 -ini \\2 -interaction=nonstopmode \"&\\1\" " preview-format-name 
".ini \\3")))
++\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|\\(--?output-directory[= ][^ 
]+\\)\\|-\\(?:[^ \\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*\\(.*\\)\\'"
++     . ("\\1 -ini \\2 \\3 -interaction=nonstopmode \"&\\1\" " 
preview-format-name ".ini \\4")))
+   "Generate a dump command from the usual preview command."
+   :group 'preview-latex
+   :type '(repeat
+@@ -3923,10 +3925,10 @@ This is passed through `preview-do-replacements'."
+   ;; If -kanji option exists, pick it up as the second match.
+   ;; Discard all other options.
+   '(("\\`\\([^ ]+\\)\
+-\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|-\\(?:[^ 
\\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*.*\
++\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|\\(--?output-directory[= ][^ 
]+\\)\\|-\\(?:[^ \\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*.*\
+  \"\\\\input\" \"\\\\detokenize{\" \\(.*\\) \"}\"\\'"
+-     . ("\\1 \\2 -interaction=nonstopmode -file-line-error "
+-        preview-format-name " \"/AUCTEXINPUT{\" \\3 \"}\"")))
++     . ("\\1 \\2 \\3 -interaction=nonstopmode -file-line-error "
++        preview-format-name " \"/AUCTEXINPUT{\" \\4 \"}\"")))
+   ;; See the ini file code below in `preview-cache-preamble' for the
+   ;; weird /AUCTEXINPUT construct.  In short, it is crafted so that
+   ;; dumped format file can read file of non-ascii name.
+@@ -4160,7 +4162,10 @@ internal parameters, STR may be a log to insert into 
the current log."
+       ((preview-format-name (shell-quote-argument
+                              (concat "&"
+                                      (preview-dump-file-name
+-                                      (file-name-nondirectory master)))))
++                                        ;; Get the filename from 
TeX-master-file with prv
++                                        ;; to get the correct path but then 
strip
++                                        ;; the extension
++                                        (file-name-sans-extension 
(TeX-master-file "prv" t))))))
+        (process-environment (copy-sequence process-environment))
+        (process
+         (progn
+diff --git a/tex-buf.el b/tex-buf.el
+index b334b7fa..93e909d6 100644
+--- a/tex-buf.el
++++ b/tex-buf.el
+@@ -61,6 +61,8 @@
+   :group 'TeX-command
+   :type 'string)
+ 
++(defvar TeX-command-file-function)
++
+ ;;; Interactive Commands
+ ;;
+ ;; The general idea is, that there is one process and process buffer
+@@ -78,22 +80,30 @@
+ (defun TeX-save-document (name)
+   "Save all files belonging to the current document.
+ Return non-nil if document needs to be re-TeX'ed."
+-  (interactive (list (TeX-master-file)))
++  (interactive (list 'TeX-master-file))
+   (if (string-equal name "")
+-      (setq name (TeX-master-file)))
++      (setq name 'TeX-master-file))
+ 
+-  (TeX-check-files (concat name "." (TeX-output-extension))
+-                   (cons name (TeX-style-list))
++  (TeX-check-files (TeX-get-command-filename name (TeX-output-extension))
++                   (cons (TeX-get-command-filename name) (TeX-style-list))
+                    TeX-file-extensions))
+ 
++(defun TeX-get-command-filename (name &optional extension)
++  (if (or TeX-command-file-function (functionp name))
++      (funcall (or TeX-command-file-function name) extension)
++    (if extension
++        (concat name "." extension)
++      name)))
++
+ (defun TeX-command-master (&optional override-confirm)
+   "Run command on the current document.
+ 
+ If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
+ depend on it being positive instead of the entry in `TeX-command-list'."
+   (interactive "P")
+-  (TeX-command (TeX-command-query (TeX-master-file nil nil t))
+-               'TeX-master-file override-confirm))
++  (let ((TeX-command-file-function 'TeX-master-file))
++   (TeX-command (TeX-command-query (TeX-master-file nil nil t))
++               'TeX-master-file override-confirm)))
+ 
+ (defvar TeX-command-region-begin nil)
+ (defvar TeX-command-region-end nil)
+@@ -185,8 +195,10 @@ all text after `TeX-trailer-start'."
+   ;; `nondirectory' argument, otherwise `TeX-comand-default' called
+   ;; within `TeX-command-query' won't work in included files not
+   ;; placed in `TeX-master-directory'.
+-  (TeX-command (TeX-command-query (TeX-region-file)) 'TeX-region-file
+-               override-confirm))
++  (let ((TeX-command-file-function 'TeX-region-file))
++    (TeX-command (TeX-command-query (TeX-region-file))
++                 TeX-command-file-function
++                 override-confirm)))
+ 
+ (defun TeX-command-buffer (&optional override-confirm)
+   "Run TeX on the current buffer.
+@@ -530,9 +542,11 @@ remember to add /Library/TeX/texbin/ to your PATH"
+                           ""))))
+ 
+     ;; Now start the process
+-    (let ((file (funcall file-fn)))
++    (let ((TeX-command-file-function file-fn)
++          (file (funcall file-fn)))
+       (TeX-process-set-variable file 'TeX-command-next TeX-command-Show)
+-      (funcall hook name command file))))
++      (funcall hook name command file)))
++  )
+ 
+ (defun TeX-command-expand (command &optional list)
+   "Expand COMMAND for `TeX-active-master' as described in LIST.
+@@ -800,18 +814,18 @@ omitted) and `TeX-region-file'."
+            ;; comparison.
+            (if (string-equal (expand-file-name name)
+                              (expand-file-name (TeX-region-file)))
+-               (TeX-check-files (concat name "." (TeX-output-extension))
++               (TeX-check-files (TeX-get-command-filename name 
(TeX-output-extension))
+                                 ;; Each original will be checked for all dirs
+                                 ;; in `TeX-check-path' so this needs to be 
just
+                                 ;; a filename without directory.
+-                                (list (file-name-nondirectory name))
++                                (list (file-relative-name 
(TeX-get-command-filename name)))
+                                 TeX-file-extensions)
+-             (TeX-save-document (TeX-master-file)))
++             (TeX-save-document 'TeX-master-file))
+            TeX-command-default)
+           ((and (memq major-mode '(doctex-mode latex-mode))
+                 ;; Want to know if bib file is newer than .bbl
+                 ;; We don't care whether the bib files are open in emacs
+-                (TeX-check-files (concat name ".bbl")
++                (TeX-check-files (TeX-get-command-filename name "bbl")
+                                  (mapcar #'car
+                                          (LaTeX-bibliography-list))
+                                  (append BibTeX-file-extensions
+@@ -828,12 +842,12 @@ omitted) and `TeX-region-file'."
+             (member
+              (setq command-next
+                    (TeX-process-get-variable
+-                    name
++                    (TeX-get-command-filename name)
+                     'TeX-command-next
+                     (or (and TeX-PDF-mode (TeX-PDF-from-DVI))
+                         TeX-command-Show)))
+              (list "Dvips" "Dvipdfmx" TeX-command-Show))
+-            (cdr (assoc (expand-file-name (concat name ".idx"))
++            (cdr (assoc (expand-file-name (TeX-get-command-filename "idx"))
+                         LaTeX-idx-changed-alist)))
+            "Index")
+           (command-next)
+@@ -1138,6 +1152,7 @@ run of `TeX-run-TeX', use
+   ;; Presence of error is reported inside `TeX-TeX-sentinel-check'
+   (let ((current-master (TeX-master-file))
+         (idx-file nil) (element nil))
++
+     ;; the current master file is saved because error routines are
+     ;; parsed in other buffers;
+     (setq TeX-error-report-switches
+@@ -1151,7 +1166,7 @@ run of `TeX-run-TeX', use
+     ;; Store md5 hash of the index file before running LaTeX.
+     (and (memq major-mode '(doctex-mode latex-mode))
+          (prog1 (file-exists-p
+-                 (setq idx-file (expand-file-name (concat file ".idx"))))
++                 (setq idx-file (expand-file-name (TeX-get-command-filename 
file "idx"))))
+            ;; In order to avoid confusion and pollution of
+            ;; `LaTeX-idx-md5-alist', remove from this alist all md5 hashes of
+            ;; the current index file.  Note `assq-delete-all' doesn't work 
with
+@@ -1233,7 +1248,7 @@ run of `TeX-run-TeX', use
+                  ;; `default-directory', then we have to expand `file' 
file-name
+                  ;; in the same directory of `TeX-command-buffer'.
+                  (assoc (with-current-buffer TeX-command-buffer
+-                            (expand-file-name (concat file ".idx")))
++                            (expand-file-name (TeX-get-command-filename 
"idx")))
+                         LaTeX-idx-changed-alist))
+       (setq LaTeX-idx-changed-alist (delq element LaTeX-idx-changed-alist)))
+     (if TeX-process-asynchronous
+@@ -1684,14 +1699,14 @@ Rerun to get mark in right position\\." nil t)
+   (let ((idx-file nil) (master nil))
+     (and (file-exists-p
+           (setq idx-file
+-                (concat
+-                 (setq master
+-                       (with-current-buffer TeX-command-buffer
+-                         (expand-file-name (TeX-active-master)))) ".idx")))
++                (setq master
++                      (with-current-buffer TeX-command-buffer
++                        (expand-file-name (TeX-active-master "idx"))))))
+          ;; imakeidx package automatically runs makeindex, thus, we need to be
+          ;; sure .ind file isn't newer than .idx.
+-         (TeX-check-files (concat master ".ind")
+-                          (list (file-name-nondirectory master)) '("idx"))
++         (TeX-check-files (with-current-buffer TeX-command-buffer
++                            (expand-file-name (TeX-active-master "ind")))
++                          (list (file-relative-name idx-file)) '("idx"))
+          (with-temp-buffer
+            (insert-file-contents idx-file)
+            (not (equal
+@@ -1995,6 +2010,9 @@ command."
+ (defvar TeX-current-process-region-p nil
+   "This variable is set to t iff the last TeX command is on a region.")
+ 
++(defvar TeX-command-file-function nil
++  "The current file function in TeX-command.")
++
+ (defun TeX-active-process ()
+   "Return the active process for the current buffer."
+   (TeX-process (TeX-active-master)))
+diff --git a/tex.el b/tex.el
+index b59aee05..a69c57a5 100644
+--- a/tex.el
++++ b/tex.el
+@@ -89,6 +89,7 @@
+ (defvar LaTeX-optop)
+ (defvar TeX-Biber-global-files)
+ (defvar TeX-global-input-files)
++(defvar TeX-output-dir)
+ ;; tex-buf.el
+ (defvar TeX-current-process-region-p)
+ (defvar TeX-region)
+@@ -182,18 +183,18 @@ If nil, none is specified."
+ ;; `TeX-expand-list-builtin' for a description of the % escapes
+ 
+ (defcustom TeX-command-list
+-  '(("TeX" "%(PDF)%(tex) %(file-line-error) %`%(extraopts) 
%S%(PDFout)%(mode)%' %t"
++  '(("TeX" "%(PDF)%(tex) %(file-line-error) %`%(extraopts) %(output-dir) 
%S%(PDFout)%(mode)%' %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")
+     ;; Not part of standard TeX.
+-    ("Makeinfo" "makeinfo %(extraopts) %t" TeX-run-compile nil
++    ("Makeinfo" "makeinfo %(extraopts) %(o-dir) %t" TeX-run-compile nil
+      (texinfo-mode) :help "Run Makeinfo with Info output")
+-    ("Makeinfo HTML" "makeinfo %(extraopts) --html %t" TeX-run-compile nil
++    ("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)%' %t"
++    ("AmSTeX" "amstex %(PDFout) %`%(extraopts) %(output-dir) %S%(mode)%' %t"
+      TeX-run-TeX nil (ams-tex-mode) :help "Run AMSTeX")
+     ;; support for ConTeXt  --pg
+     ;; first version of ConTeXt to support nonstopmode: 2003.2.10
+@@ -202,11 +203,11 @@ If nil, none is specified."
+     ("ConTeXt Full" "%(cntxcom) %(extraopts) %(execopts)%t"
+      TeX-run-TeX nil
+      (context-mode) :help "Run ConTeXt until completion")
+-    ("BibTeX" "bibtex %s" TeX-run-BibTeX nil
++    ("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 %s" TeX-run-Biber nil
++    ("Biber" "biber %s %(output-dir)" TeX-run-Biber nil
+      (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode)
+      :help "Run Biber")
+     ("View" "%V" TeX-run-discard-or-function t t :help "Run Viewer")
+@@ -219,20 +220,20 @@ If nil, none is specified."
+     ("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 %d" TeX-run-dvipdfmx nil
++    ("Dvipdfmx" "dvipdfmx %d -o %(O?pdf)" 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" TeX-run-ps2pdf nil
++    ("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 %s" TeX-run-command nil
++    ("Glossaries" "makeglossaries %(O?aux)" 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 %s" TeX-run-index nil
++    ("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 %s" TeX-run-index t
++    ("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
+@@ -393,7 +394,7 @@ The executable `latex' is LaTeX version 2e."
+ 
+ (defcustom LaTeX-command-style
+   ;; They have all been combined in LaTeX 2e.
+-  '(("" "%(PDF)%(latex) %(file-line-error) %(extraopts) %S%(PDFout)"))
++  '(("" "%(PDF)%(latex) %(file-line-error) %(extraopts) %(output-dir) 
%S%(PDFout)"))
+ "List of style options and LaTeX commands.
+ 
+ If the first element (a regular expression) matches the name of one of
+@@ -420,7 +421,7 @@ string."
+ ;; TeX-print-command.
+ 
+ (defcustom TeX-print-command
+-  "{ test -e %s.dvi && %(o?)dvips -P%p %r %s; } || lpr -P%p %o"
++  "{ test -e %d && %(o?)dvips -P%p %r %s; } || lpr -P%p %o"
+   "Command used to print a file.
+ 
+ First `%p' is expanded to the printer name, then ordinary expansion is
+@@ -448,7 +449,7 @@ the printer has no corresponding command."
+      ;; Print to the (unnamed) default printer.  If there is a DVI
+      ;; file print via Dvips.  If not, pass the output file (which
+      ;; should then be a Postscript or PDF file) directly to lpr.
+-     "{ test -e %s.dvi && %(o?)dvips -f %r %s | lpr; } || lpr %o"
++     "{ test -e %d && %(o?)dvips -f %r %s | lpr; } || lpr %o"
+      ;; Show the queue for the (unnamed) default printer.
+      "lpq"))
+   "List of available printers.
+@@ -563,6 +564,8 @@ string."
+     ("%(cntxcom)" ConTeXt-expand-command)
+     ("%(execopts)" ConTeXt-expand-options)
+     ("%(extraopts)" (lambda () TeX-command-extra-options))
++    ("%(output-dir)" (lambda () (TeX--output-dir-arg "--output-directory=")))
++    ("%(o-dir)" (lambda () (TeX--output-dir-arg "-o ")))
+     ("%S" TeX-source-correlate-expand-options)
+     ("%dS" TeX-source-specials-view-expand-options)
+     ("%cS" TeX-source-specials-view-expand-client)
+@@ -632,6 +635,9 @@ string."
+     ("%n" TeX-current-line)
+     ("%d" TeX-active-master-with-quotes "dvi" t)
+     ("%f" TeX-active-master-with-quotes "ps" t)
++    ("%(O?aux)" TeX-active-master-with-quotes "aux" t)
++    ("%(O?idx)" TeX-active-master-with-quotes "idx" t)
++    ("%(O?pdf)" TeX-active-master-with-quotes "pdf" t)
+     ("%o" (lambda nil (TeX-active-master-with-quotes (TeX-output-extension) 
t)))
+     ;; for source specials the file name generated for the xdvi
+     ;; command needs to be relative to the master file, just in
+@@ -2210,26 +2216,20 @@ Used as a default in TeX, LaTeX and docTeX mode.")
+ If prefix ARG is non-nil, not only remove intermediate but also
+ output files."
+   (interactive "P")
+-  (let* ((mode-prefix (TeX-mode-prefix))
+-         (suffixes (append (symbol-value
+-                            (intern (concat mode-prefix
+-                                            "-clean-intermediate-suffixes")))
+-                           (when arg
+-                             (symbol-value
+-                              (intern (concat mode-prefix
+-                                              "-clean-output-suffixes"))))))
+-         (master (TeX-active-master))
+-         (master-dir (file-name-directory master))
+-         (regexp (concat "\\("
+-                         (regexp-quote (file-name-nondirectory master)) "\\|"
+-                         (regexp-quote (TeX-region-file nil t))
+-                         "\\)"
+-                         "\\("
+-                         (mapconcat 'identity suffixes "\\|")
+-                         "\\)\\'"
+-                         "\\|" (regexp-quote (TeX-region-file t t))))
+-         (files (when regexp
+-                  (directory-files (or master-dir ".") nil regexp))))
++  (let* (;; Add output extension then remove it, to make sure we get the 
correct
++       ;; directory in cases TeX-output-dir is non-nil
++       (master (file-name-sans-extension (TeX-active-master 
(TeX-output-extension))))
++       (master-dir (file-name-directory master))
++       (regexp (concat "\\("
++                       (regexp-quote (file-name-nondirectory master)) "\\|"
++                       (regexp-quote (TeX-region-file nil t))
++                       "\\)"
++                       "\\("
++                       (TeX--clean-extensions-regexp arg)
++                       "\\)\\'"
++                       "\\|" (regexp-quote (TeX-region-file t t))))
++       (files (when (and regexp (or (not master-dir) (file-exists-p 
master-dir)))
++                (directory-files (or master-dir ".") nil regexp))))
+     (if files
+         (when (or (not TeX-clean-confirm)
+                   (dired-mark-pop-up " *Deletions*" 'delete
+@@ -2241,6 +2241,19 @@ output files."
+             (delete-file (concat master-dir file))))
+       (message "No files to be deleted"))))
+ 
++(defun TeX--clean-extensions-regexp (&optional arg)
++  "Returns a regexp to match extensions that should be cleaned by TeX-clean.
++If the optional argument ARG is non-nil then output files are included"
++  (when-let ((mode-prefix (TeX-mode-prefix))
++             (suffixes (append (symbol-value
++                                (intern (concat mode-prefix
++                                                
"-clean-intermediate-suffixes")))
++                               (when arg
++                                 (symbol-value
++                                  (intern (concat mode-prefix
++                                                  
"-clean-output-suffixes")))))))
++    (mapconcat 'identity suffixes "\\|")))
++
+ ;;; Master File
+ 
+ (defcustom TeX-master t
+@@ -2334,6 +2347,21 @@ this variable to \"<none>\"."
+                                                    'path))
+              (TeX-add-local-master))))))
+ 
++(defun TeX-master-output-file (&optional extension relative-to-master)
++  "Returns an output file based on `TeX-output-dir' in the
++master-file, opening it if necessary. if the optional argument
++EXTENSION is non-nil it is appended as an extension to the output
++file. If EXTENSION is t then (TeX-output-extension) is used."
++  (interactive)
++  (if (eq extension t)
++      (setq extension (TeX-output-extension)))
++  (if-let ((output-file
++            (when-let ((master-out-dir (TeX-master-output-dir 
relative-to-master)))
++              (concat master-out-dir (file-name-nondirectory 
(TeX-master-file))))))
++      (if extension (concat output-file "." extension)
++        output-file)
++    (TeX-master-file extension relative-to-master)))
++
+ (defun TeX-master-file (&optional extension nondirectory ask)
+   "Set and return the name of the master file for the current document.
+ 
+@@ -2394,9 +2422,14 @@ name of master file if it cannot be determined 
otherwise."
+          ;; Ask the user (but add it as a local variable).
+          (ask (TeX-master-file-ask)))))
+ 
+-    (let ((name (if (stringp TeX-master)
+-                    TeX-master
+-                  my-name)))
++    (if (and (when-let (reg (TeX--clean-extensions-regexp t))
++               (or (string-match-p reg (concat "." extension))
++                   (string= "prv" extension)))
++             (TeX-master-output-dir))
++        (TeX-master-output-file extension nondirectory)
++      (let ((name (if (stringp TeX-master)
++                  TeX-master
++                my-name)))
+ 
+       (if (TeX-match-extension name)
+           ;; If it already has an extension...
+@@ -2411,8 +2444,8 @@ name of master file if it cannot be determined 
otherwise."
+           (setq name (file-name-nondirectory name)))
+ 
+       (if extension
+-          (concat name "." extension)
+-        name))))
++        (concat name "." extension)
++      name)))))
+ 
+ (defun TeX-master-directory ()
+   "Directory of master file."
+@@ -2494,6 +2527,38 @@ be relative to that."
+   :group 'TeX-file
+   :type 'string)
+ 
++(defcustom TeX-output-dir nil
++  "The directory where the output files will be generated. The
++  directory cannot start with a `.'.
++
++If this variable is nil, AUCTeX will assume that the output
++directory is the same as the directory of TeX-master."
++  :group 'TeX-file
++  :local t
++  :safe 'string-or-null-p
++  :type 'string)
++
++(defun TeX-master-buffer ()
++  (let* ((master-file (TeX-master-file t)))
++    (or (find-buffer-visiting master-file)
++        (find-file-noselect master-file))))
++
++(defun TeX-master-output-dir (&optional relative-to-master)
++  (with-current-buffer (TeX-master-buffer)
++    (when-let ((out-dir (and TeX-output-dir
++                             (file-name-as-directory
++                              (abbreviate-file-name
++                               (substitute-in-file-name
++                                (expand-file-name
++                                 TeX-output-dir
++                                 (file-name-directory buffer-file-name))))))))
++      ;; Make sure the directory exists
++      (unless (file-exists-p out-dir)
++        (make-directory (file-name-as-directory out-dir) t))
++      (if relative-to-master
++          (file-relative-name out-dir)
++        out-dir))))
++
+ (defcustom TeX-style-local "style"
+   "Directory containing hand generated TeX information.
+ 
+@@ -2502,6 +2567,13 @@ be relative to that."
+   :group 'TeX-file
+   :type 'string)
+ 
++(defun TeX--output-dir-arg (argname)
++  (if-let ((out-dir (if TeX-current-process-region-p
++                        (file-name-directory TeX-region)
++                      (TeX-master-output-dir t))))
++      (concat argname "\"" out-dir "\"")
++    ""))
++
+ (defun TeX-split-string (regexp string)
+   "Return a list of strings.
+ Given REGEXP the STRING is split into sections which in string was
diff --git a/preview.el.in b/preview.el.in
index ba10ee8..1d9cc93 100644
--- a/preview.el.in
+++ b/preview.el.in
@@ -552,7 +552,7 @@ if you customize this."
   :type 'string)
 
 (defcustom preview-pdf2dsc-command
-  "pdf2dsc %s.pdf %m/preview.dsc"
+  "pdf2dsc %(O?pdf) %m/preview.dsc"
   "Command used for generating dsc from a PDF file."
   :group 'preview-latex
   :type 'string)
@@ -3155,7 +3155,9 @@ later while in use."
                 topdir
                 0))
     (shell-quote-argument
-     (concat (file-name-as-directory (file-name-nondirectory topdir))
+     (concat (file-name-as-directory
+              ;; Don't use topdir, because %m expects the path to be relative 
to master
+              (TeX-active-master "prv" t))
              (file-name-nondirectory (nth 0 TeX-active-tempdir))))))
 
 (defun preview-parse-counters (string)
@@ -3911,8 +3913,8 @@ This is passed through `preview-do-replacements'."
   ;; If -kanji option exists, pick it up as the second match.
   ;; Discard all other options.
     ("\\`\\([^ ]+\\)\
-\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|-\\(?:[^ 
\\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*\\(.*\\)\\'"
-     . ("\\1 -ini \\2 -interaction=nonstopmode \"&\\1\" " preview-format-name 
".ini \\3")))
+\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|\\(--?output-directory[= ][^ 
]+\\)\\|-\\(?:[^ \\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*\\(.*\\)\\'"
+     . ("\\1 -ini \\2 \\3 -interaction=nonstopmode \"&\\1\" " 
preview-format-name ".ini \\4")))
   "Generate a dump command from the usual preview command."
   :group 'preview-latex
   :type '(repeat
@@ -3923,10 +3925,10 @@ This is passed through `preview-do-replacements'."
   ;; If -kanji option exists, pick it up as the second match.
   ;; Discard all other options.
   '(("\\`\\([^ ]+\\)\
-\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|-\\(?:[^ 
\\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*.*\
+\\(?: +\\(?:\\(--?kanji[= ][^ ]+\\)\\|\\(--?output-directory[= ][^ 
]+\\)\\|-\\(?:[^ \\\"]\\|\\\\.\\|\"[^\"]*\"\\)*\\)\\)*.*\
  \"\\\\input\" \"\\\\detokenize{\" \\(.*\\) \"}\"\\'"
-     . ("\\1 \\2 -interaction=nonstopmode -file-line-error "
-        preview-format-name " \"/AUCTEXINPUT{\" \\3 \"}\"")))
+     . ("\\1 \\2 \\3 -interaction=nonstopmode -file-line-error "
+        preview-format-name " \"/AUCTEXINPUT{\" \\4 \"}\"")))
   ;; See the ini file code below in `preview-cache-preamble' for the
   ;; weird /AUCTEXINPUT construct.  In short, it is crafted so that
   ;; dumped format file can read file of non-ascii name.
@@ -4160,7 +4162,10 @@ internal parameters, STR may be a log to insert into the 
current log."
       ((preview-format-name (shell-quote-argument
                              (concat "&"
                                      (preview-dump-file-name
-                                      (file-name-nondirectory master)))))
+                                        ;; Get the filename from 
TeX-master-file with prv
+                                        ;; to get the correct path but then 
strip
+                                        ;; the extension
+                                        (file-name-sans-extension 
(TeX-master-file "prv" t))))))
        (process-environment (copy-sequence process-environment))
        (process
         (progn
diff --git a/tex-buf.el b/tex-buf.el
index e13a211..57db910 100644
--- a/tex-buf.el
+++ b/tex-buf.el
@@ -61,6 +61,8 @@
   :group 'TeX-command
   :type 'string)
 
+(defvar TeX-command-file-function)
+
 ;;; Interactive Commands
 ;;
 ;; The general idea is, that there is one process and process buffer
@@ -78,22 +80,30 @@
 (defun TeX-save-document (name)
   "Save all files belonging to the current document.
 Return non-nil if document needs to be re-TeX'ed."
-  (interactive (list (TeX-master-file)))
+  (interactive (list 'TeX-master-file))
   (if (string-equal name "")
-      (setq name (TeX-master-file)))
+      (setq name 'TeX-master-file))
 
-  (TeX-check-files (concat name "." (TeX-output-extension))
-                   (cons name (TeX-style-list))
+  (TeX-check-files (TeX-get-command-filename name (TeX-output-extension))
+                   (cons (TeX-get-command-filename name) (TeX-style-list))
                    TeX-file-extensions))
 
+(defun TeX-get-command-filename (name &optional extension)
+  (if (or TeX-command-file-function (functionp name))
+      (funcall (or TeX-command-file-function name) extension)
+    (if extension
+        (concat name "." extension)
+      name)))
+
 (defun TeX-command-master (&optional override-confirm)
   "Run command on the current document.
 
 If a prefix argument OVERRIDE-CONFIRM is given, confirmation will
 depend on it being positive instead of the entry in `TeX-command-list'."
   (interactive "P")
-  (TeX-command (TeX-command-query (TeX-master-file nil nil t))
-               'TeX-master-file override-confirm))
+  (let ((TeX-command-file-function 'TeX-master-file))
+   (TeX-command (TeX-command-query (TeX-master-file nil nil t))
+               'TeX-master-file override-confirm)))
 
 (defvar TeX-command-region-begin nil)
 (defvar TeX-command-region-end nil)
@@ -185,8 +195,10 @@ all text after `TeX-trailer-start'."
   ;; `nondirectory' argument, otherwise `TeX-command-default' called
   ;; within `TeX-command-query' won't work in included files not
   ;; placed in `TeX-master-directory'.
-  (TeX-command (TeX-command-query (TeX-region-file)) 'TeX-region-file
-               override-confirm))
+  (let ((TeX-command-file-function 'TeX-region-file))
+    (TeX-command (TeX-command-query (TeX-region-file))
+                 TeX-command-file-function
+                 override-confirm)))
 
 (defun TeX-command-buffer (&optional override-confirm)
   "Run TeX on the current buffer.
@@ -530,9 +542,11 @@ remember to add /Library/TeX/texbin/ to your PATH"
                           ""))))
 
     ;; Now start the process
-    (let ((file (funcall file-fn)))
+    (let ((TeX-command-file-function file-fn)
+          (file (funcall file-fn)))
       (TeX-process-set-variable file 'TeX-command-next TeX-command-Show)
-      (funcall hook name command file))))
+      (funcall hook name command file)))
+  )
 
 (defun TeX-command-expand (command &optional list)
   "Expand COMMAND for `TeX-active-master' as described in LIST.
@@ -800,18 +814,18 @@ omitted) and `TeX-region-file'."
            ;; comparison.
            (if (string-equal (expand-file-name name)
                              (expand-file-name (TeX-region-file)))
-               (TeX-check-files (concat name "." (TeX-output-extension))
+               (TeX-check-files (TeX-get-command-filename name 
(TeX-output-extension))
                                 ;; Each original will be checked for all dirs
                                 ;; in `TeX-check-path' so this needs to be just
                                 ;; a filename without directory.
-                                (list (file-name-nondirectory name))
+                                (list (file-relative-name 
(TeX-get-command-filename name)))
                                 TeX-file-extensions)
-             (TeX-save-document (TeX-master-file)))
+             (TeX-save-document 'TeX-master-file))
            TeX-command-default)
           ((and (memq major-mode '(doctex-mode latex-mode))
                 ;; Want to know if bib file is newer than .bbl
                 ;; We don't care whether the bib files are open in emacs
-                (TeX-check-files (concat name ".bbl")
+                (TeX-check-files (TeX-get-command-filename name "bbl")
                                  (mapcar #'car
                                          (LaTeX-bibliography-list))
                                  (append BibTeX-file-extensions
@@ -828,12 +842,12 @@ omitted) and `TeX-region-file'."
             (member
              (setq command-next
                    (TeX-process-get-variable
-                    name
+                    (TeX-get-command-filename name)
                     'TeX-command-next
                     (or (and TeX-PDF-mode (TeX-PDF-from-DVI))
                         TeX-command-Show)))
              (list "Dvips" "Dvipdfmx" TeX-command-Show))
-            (cdr (assoc (expand-file-name (concat name ".idx"))
+            (cdr (assoc (expand-file-name (TeX-get-command-filename "idx"))
                         LaTeX-idx-changed-alist)))
            "Index")
           (command-next)
@@ -1138,6 +1152,7 @@ run of `TeX-run-TeX', use
   ;; Presence of error is reported inside `TeX-TeX-sentinel-check'
   (let ((current-master (TeX-master-file))
         (idx-file nil) (element nil))
+
     ;; the current master file is saved because error routines are
     ;; parsed in other buffers;
     (setq TeX-error-report-switches
@@ -1151,7 +1166,7 @@ run of `TeX-run-TeX', use
     ;; Store md5 hash of the index file before running LaTeX.
     (and (memq major-mode '(doctex-mode latex-mode))
          (prog1 (file-exists-p
-                 (setq idx-file (expand-file-name (concat file ".idx"))))
+                 (setq idx-file (expand-file-name (TeX-get-command-filename 
file "idx"))))
            ;; In order to avoid confusion and pollution of
            ;; `LaTeX-idx-md5-alist', remove from this alist all md5 hashes of
            ;; the current index file.  Note `assq-delete-all' doesn't work with
@@ -1233,7 +1248,7 @@ run of `TeX-run-TeX', use
                  ;; `default-directory', then we have to expand `file' 
file-name
                  ;; in the same directory of `TeX-command-buffer'.
                  (assoc (with-current-buffer TeX-command-buffer
-                            (expand-file-name (concat file ".idx")))
+                            (expand-file-name (TeX-get-command-filename 
"idx")))
                         LaTeX-idx-changed-alist))
       (setq LaTeX-idx-changed-alist (delq element LaTeX-idx-changed-alist)))
     (if TeX-process-asynchronous
@@ -1684,14 +1699,14 @@ Rerun to get mark in right position\\." nil t)
   (let ((idx-file nil) (master nil))
     (and (file-exists-p
           (setq idx-file
-                (concat
-                 (setq master
-                       (with-current-buffer TeX-command-buffer
-                         (expand-file-name (TeX-active-master)))) ".idx")))
+                (setq master
+                      (with-current-buffer TeX-command-buffer
+                        (expand-file-name (TeX-active-master "idx"))))))
          ;; imakeidx package automatically runs makeindex, thus, we need to be
          ;; sure .ind file isn't newer than .idx.
-         (TeX-check-files (concat master ".ind")
-                          (list (file-name-nondirectory master)) '("idx"))
+         (TeX-check-files (with-current-buffer TeX-command-buffer
+                            (expand-file-name (TeX-active-master "ind")))
+                          (list (file-relative-name idx-file)) '("idx"))
          (with-temp-buffer
            (insert-file-contents idx-file)
            (not (equal
@@ -1995,6 +2010,9 @@ command."
 (defvar TeX-current-process-region-p nil
   "This variable is set to t iff the last TeX command is on a region.")
 
+(defvar TeX-command-file-function nil
+  "The current file function in TeX-command.")
+
 (defun TeX-active-process ()
   "Return the active process for the current buffer."
   (TeX-process (TeX-active-master)))
diff --git a/tex.el b/tex.el
index b59aee0..a69c57a 100644
--- a/tex.el
+++ b/tex.el
@@ -89,6 +89,7 @@
 (defvar LaTeX-optop)
 (defvar TeX-Biber-global-files)
 (defvar TeX-global-input-files)
+(defvar TeX-output-dir)
 ;; tex-buf.el
 (defvar TeX-current-process-region-p)
 (defvar TeX-region)
@@ -182,18 +183,18 @@ If nil, none is specified."
 ;; `TeX-expand-list-builtin' for a description of the % escapes
 
 (defcustom TeX-command-list
-  '(("TeX" "%(PDF)%(tex) %(file-line-error) %`%(extraopts) 
%S%(PDFout)%(mode)%' %t"
+  '(("TeX" "%(PDF)%(tex) %(file-line-error) %`%(extraopts) %(output-dir) 
%S%(PDFout)%(mode)%' %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")
     ;; Not part of standard TeX.
-    ("Makeinfo" "makeinfo %(extraopts) %t" TeX-run-compile nil
+    ("Makeinfo" "makeinfo %(extraopts) %(o-dir) %t" TeX-run-compile nil
      (texinfo-mode) :help "Run Makeinfo with Info output")
-    ("Makeinfo HTML" "makeinfo %(extraopts) --html %t" TeX-run-compile nil
+    ("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)%' %t"
+    ("AmSTeX" "amstex %(PDFout) %`%(extraopts) %(output-dir) %S%(mode)%' %t"
      TeX-run-TeX nil (ams-tex-mode) :help "Run AMSTeX")
     ;; support for ConTeXt  --pg
     ;; first version of ConTeXt to support nonstopmode: 2003.2.10
@@ -202,11 +203,11 @@ If nil, none is specified."
     ("ConTeXt Full" "%(cntxcom) %(extraopts) %(execopts)%t"
      TeX-run-TeX nil
      (context-mode) :help "Run ConTeXt until completion")
-    ("BibTeX" "bibtex %s" TeX-run-BibTeX nil
+    ("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 %s" TeX-run-Biber nil
+    ("Biber" "biber %s %(output-dir)" TeX-run-Biber nil
      (plain-tex-mode latex-mode doctex-mode ams-tex-mode texinfo-mode)
      :help "Run Biber")
     ("View" "%V" TeX-run-discard-or-function t t :help "Run Viewer")
@@ -219,20 +220,20 @@ If nil, none is specified."
     ("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 %d" TeX-run-dvipdfmx nil
+    ("Dvipdfmx" "dvipdfmx %d -o %(O?pdf)" 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" TeX-run-ps2pdf nil
+    ("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 %s" TeX-run-command nil
+    ("Glossaries" "makeglossaries %(O?aux)" 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 %s" TeX-run-index nil
+    ("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 %s" TeX-run-index t
+    ("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
@@ -393,7 +394,7 @@ The executable `latex' is LaTeX version 2e."
 
 (defcustom LaTeX-command-style
   ;; They have all been combined in LaTeX 2e.
-  '(("" "%(PDF)%(latex) %(file-line-error) %(extraopts) %S%(PDFout)"))
+  '(("" "%(PDF)%(latex) %(file-line-error) %(extraopts) %(output-dir) 
%S%(PDFout)"))
 "List of style options and LaTeX commands.
 
 If the first element (a regular expression) matches the name of one of
@@ -420,7 +421,7 @@ string."
 ;; TeX-print-command.
 
 (defcustom TeX-print-command
-  "{ test -e %s.dvi && %(o?)dvips -P%p %r %s; } || lpr -P%p %o"
+  "{ test -e %d && %(o?)dvips -P%p %r %s; } || lpr -P%p %o"
   "Command used to print a file.
 
 First `%p' is expanded to the printer name, then ordinary expansion is
@@ -448,7 +449,7 @@ the printer has no corresponding command."
      ;; Print to the (unnamed) default printer.  If there is a DVI
      ;; file print via Dvips.  If not, pass the output file (which
      ;; should then be a Postscript or PDF file) directly to lpr.
-     "{ test -e %s.dvi && %(o?)dvips -f %r %s | lpr; } || lpr %o"
+     "{ test -e %d && %(o?)dvips -f %r %s | lpr; } || lpr %o"
      ;; Show the queue for the (unnamed) default printer.
      "lpq"))
   "List of available printers.
@@ -563,6 +564,8 @@ string."
     ("%(cntxcom)" ConTeXt-expand-command)
     ("%(execopts)" ConTeXt-expand-options)
     ("%(extraopts)" (lambda () TeX-command-extra-options))
+    ("%(output-dir)" (lambda () (TeX--output-dir-arg "--output-directory=")))
+    ("%(o-dir)" (lambda () (TeX--output-dir-arg "-o ")))
     ("%S" TeX-source-correlate-expand-options)
     ("%dS" TeX-source-specials-view-expand-options)
     ("%cS" TeX-source-specials-view-expand-client)
@@ -632,6 +635,9 @@ string."
     ("%n" TeX-current-line)
     ("%d" TeX-active-master-with-quotes "dvi" t)
     ("%f" TeX-active-master-with-quotes "ps" t)
+    ("%(O?aux)" TeX-active-master-with-quotes "aux" t)
+    ("%(O?idx)" TeX-active-master-with-quotes "idx" t)
+    ("%(O?pdf)" TeX-active-master-with-quotes "pdf" t)
     ("%o" (lambda nil (TeX-active-master-with-quotes (TeX-output-extension) 
t)))
     ;; for source specials the file name generated for the xdvi
     ;; command needs to be relative to the master file, just in
@@ -2210,26 +2216,20 @@ Used as a default in TeX, LaTeX and docTeX mode.")
 If prefix ARG is non-nil, not only remove intermediate but also
 output files."
   (interactive "P")
-  (let* ((mode-prefix (TeX-mode-prefix))
-         (suffixes (append (symbol-value
-                            (intern (concat mode-prefix
-                                            "-clean-intermediate-suffixes")))
-                           (when arg
-                             (symbol-value
-                              (intern (concat mode-prefix
-                                              "-clean-output-suffixes"))))))
-         (master (TeX-active-master))
-         (master-dir (file-name-directory master))
-         (regexp (concat "\\("
-                         (regexp-quote (file-name-nondirectory master)) "\\|"
-                         (regexp-quote (TeX-region-file nil t))
-                         "\\)"
-                         "\\("
-                         (mapconcat 'identity suffixes "\\|")
-                         "\\)\\'"
-                         "\\|" (regexp-quote (TeX-region-file t t))))
-         (files (when regexp
-                  (directory-files (or master-dir ".") nil regexp))))
+  (let* (;; Add output extension then remove it, to make sure we get the 
correct
+        ;; directory in cases TeX-output-dir is non-nil
+        (master (file-name-sans-extension (TeX-active-master 
(TeX-output-extension))))
+        (master-dir (file-name-directory master))
+        (regexp (concat "\\("
+                        (regexp-quote (file-name-nondirectory master)) "\\|"
+                        (regexp-quote (TeX-region-file nil t))
+                        "\\)"
+                        "\\("
+                        (TeX--clean-extensions-regexp arg)
+                        "\\)\\'"
+                        "\\|" (regexp-quote (TeX-region-file t t))))
+        (files (when (and regexp (or (not master-dir) (file-exists-p 
master-dir)))
+                 (directory-files (or master-dir ".") nil regexp))))
     (if files
         (when (or (not TeX-clean-confirm)
                   (dired-mark-pop-up " *Deletions*" 'delete
@@ -2241,6 +2241,19 @@ output files."
             (delete-file (concat master-dir file))))
       (message "No files to be deleted"))))
 
+(defun TeX--clean-extensions-regexp (&optional arg)
+  "Returns a regexp to match extensions that should be cleaned by TeX-clean.
+If the optional argument ARG is non-nil then output files are included"
+  (when-let ((mode-prefix (TeX-mode-prefix))
+             (suffixes (append (symbol-value
+                                (intern (concat mode-prefix
+                                                
"-clean-intermediate-suffixes")))
+                               (when arg
+                                 (symbol-value
+                                  (intern (concat mode-prefix
+                                                  
"-clean-output-suffixes")))))))
+    (mapconcat 'identity suffixes "\\|")))
+
 ;;; Master File
 
 (defcustom TeX-master t
@@ -2334,6 +2347,21 @@ this variable to \"<none>\"."
                                                    'path))
              (TeX-add-local-master))))))
 
+(defun TeX-master-output-file (&optional extension relative-to-master)
+  "Returns an output file based on `TeX-output-dir' in the
+master-file, opening it if necessary. if the optional argument
+EXTENSION is non-nil it is appended as an extension to the output
+file. If EXTENSION is t then (TeX-output-extension) is used."
+  (interactive)
+  (if (eq extension t)
+      (setq extension (TeX-output-extension)))
+  (if-let ((output-file
+            (when-let ((master-out-dir (TeX-master-output-dir 
relative-to-master)))
+              (concat master-out-dir (file-name-nondirectory 
(TeX-master-file))))))
+      (if extension (concat output-file "." extension)
+        output-file)
+    (TeX-master-file extension relative-to-master)))
+
 (defun TeX-master-file (&optional extension nondirectory ask)
   "Set and return the name of the master file for the current document.
 
@@ -2394,9 +2422,14 @@ name of master file if it cannot be determined 
otherwise."
          ;; Ask the user (but add it as a local variable).
          (ask (TeX-master-file-ask)))))
 
-    (let ((name (if (stringp TeX-master)
-                    TeX-master
-                  my-name)))
+    (if (and (when-let (reg (TeX--clean-extensions-regexp t))
+               (or (string-match-p reg (concat "." extension))
+                   (string= "prv" extension)))
+             (TeX-master-output-dir))
+        (TeX-master-output-file extension nondirectory)
+      (let ((name (if (stringp TeX-master)
+                   TeX-master
+                 my-name)))
 
       (if (TeX-match-extension name)
           ;; If it already has an extension...
@@ -2411,8 +2444,8 @@ name of master file if it cannot be determined otherwise."
           (setq name (file-name-nondirectory name)))
 
       (if extension
-          (concat name "." extension)
-        name))))
+         (concat name "." extension)
+       name)))))
 
 (defun TeX-master-directory ()
   "Directory of master file."
@@ -2494,6 +2527,38 @@ be relative to that."
   :group 'TeX-file
   :type 'string)
 
+(defcustom TeX-output-dir nil
+  "The directory where the output files will be generated. The
+  directory cannot start with a `.'.
+
+If this variable is nil, AUCTeX will assume that the output
+directory is the same as the directory of TeX-master."
+  :group 'TeX-file
+  :local t
+  :safe 'string-or-null-p
+  :type 'string)
+
+(defun TeX-master-buffer ()
+  (let* ((master-file (TeX-master-file t)))
+    (or (find-buffer-visiting master-file)
+        (find-file-noselect master-file))))
+
+(defun TeX-master-output-dir (&optional relative-to-master)
+  (with-current-buffer (TeX-master-buffer)
+    (when-let ((out-dir (and TeX-output-dir
+                             (file-name-as-directory
+                              (abbreviate-file-name
+                               (substitute-in-file-name
+                                (expand-file-name
+                                 TeX-output-dir
+                                 (file-name-directory buffer-file-name))))))))
+      ;; Make sure the directory exists
+      (unless (file-exists-p out-dir)
+        (make-directory (file-name-as-directory out-dir) t))
+      (if relative-to-master
+          (file-relative-name out-dir)
+        out-dir))))
+
 (defcustom TeX-style-local "style"
   "Directory containing hand generated TeX information.
 
@@ -2502,6 +2567,13 @@ be relative to that."
   :group 'TeX-file
   :type 'string)
 
+(defun TeX--output-dir-arg (argname)
+  (if-let ((out-dir (if TeX-current-process-region-p
+                        (file-name-directory TeX-region)
+                      (TeX-master-output-dir t))))
+      (concat argname "\"" out-dir "\"")
+    ""))
+
 (defun TeX-split-string (regexp string)
   "Return a list of strings.
 Given REGEXP the STRING is split into sections which in string was

-----------------------------------------------------------------------


hooks/post-receive
-- 
GNU AUCTeX



reply via email to

[Prev in Thread] Current Thread [Next in Thread]