diff --git a/tex-buf.el b/tex-buf.el --- a/tex-buf.el +++ b/tex-buf.el @@ -2092,8 +2092,10 @@ (if (not (re-search-forward TeX-header-end nil t)) "" (re-search-forward "[\r\n]" nil t) - (buffer-substring (point-min) (point))))))))) + (buffer-substring-no-properties + (point-min) (point))))))))) (header-offset 0) + first-line ;; We search for the trailer from the master file, if it is ;; not present in the region. (trailer-offset 0) @@ -2113,11 +2115,24 @@ ;;(beginning-of-line 1) (re-search-backward "[\r\n]" nil t) (setq trailer-offset (TeX-current-offset)) - (buffer-substring (point) (point-max)))))))))) + (buffer-substring-no-properties + (point) (point-max)))))))))) ;; file name should be relative to master (setq original (TeX-quote-filename (file-relative-name original (TeX-master-directory))) master-name (TeX-quote-filename master-name)) + + ;; Cater for %&FORMAT construct in the first line. + (setq first-line (if (and (> (length header) 1) + (string= (substring header 0 2) "%&")) + ;; This would work even if header has no newline. + (substring header 0 (string-match "\n" header)) + "")) + (unless (string= first-line "") + ;; Remove first-line from header. + (setq header (substring header (length first-line))) + (setq first-line (concat first-line "\n"))) + (with-current-buffer file-buffer (setq buffer-read-only t buffer-undo-list t) @@ -2127,7 +2142,8 @@ (when (boundp 'buffer-file-coding-system) (setq buffer-file-coding-system (with-current-buffer master-buffer buffer-file-coding-system))) - (insert "\\message{ !name(" master-name ")}" + (insert first-line + "\\message{ !name(" master-name ")}" header TeX-region-extra "\n\\message{ !name(" original ") !offset(")