Function: TeX-region-create
TeX-region-create is a byte-compiled function defined in tex.el.
Signature
(TeX-region-create FILE REGION ORIGINAL OFFSET)
Documentation
Create a new file named FILE with the string REGION.
The region is taken from ORIGINAL starting at line OFFSET.
The current buffer and master file is searched, in order to ensure that the TeX header and trailer information is also included.
The OFFSET is used to provide the debugger with information about the original file.
Source Code
;; Defined in ~/.emacs.d/elpa/auctex-14.1.2/tex.el
(defun TeX-region-create (file region original offset)
"Create a new file named FILE with the string REGION.
The region is taken from ORIGINAL starting at line OFFSET.
The current buffer and master file is searched, in order to ensure
that the TeX header and trailer information is also included.
The OFFSET is used to provide the debugger with information about the
original file."
(if (fboundp 'preview--skip-preamble-region)
(let ((temp (preview--skip-preamble-region region offset)))
(if temp
;; Skip preamble for the sake of predumped formats.
(setq region (car temp)
offset (cdr temp)))))
(let* (;; We shift buffer a lot, so we must keep track of the buffer
;; local variables.
(header-end TeX-header-end)
(trailer-start TeX-trailer-start)
;; We search for header and trailer in the master file.
(orig-buffer (current-buffer))
(master-name (TeX-master-file TeX-default-extension))
(master-buffer (find-file-noselect master-name))
;; Attempt to disable font lock.
(font-lock-mode-hook nil)
;; And insert them into the FILE buffer.
(file-buffer (let (;; Don't query for master file
(TeX-transient-master t)
;; Don't choose a special mode (and call its hooks)
(auto-mode-alist nil)
(magic-mode-alist nil)
(enable-local-variables nil)
;; Don't run any f-f hooks
(find-file-hook nil))
(find-file-noselect file t)))
;; But remember original content.
original-content
;; We search for the header from the master file, if it is
;; not present in the region.
(header (if (string-match header-end region)
""
(save-excursion
(save-restriction
(set-buffer master-buffer)
(save-excursion
(save-restriction
(widen)
(goto-char (point-min))
;; NOTE: We use the local value of
;; TeX-header-end from the master file.
(if (not (re-search-forward TeX-header-end nil t))
""
(re-search-forward "[\r\n]" nil t)
(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)
(trailer (if (string-match trailer-start region)
""
(save-excursion
(save-restriction
(set-buffer master-buffer)
(save-excursion
(save-restriction
(widen)
(goto-char (point-max))
;; NOTE: We use the local value of
;; TeX-trailer-start from the master file.
(if (not (re-search-backward TeX-trailer-start nil t))
""
;;(beginning-of-line 1)
(re-search-backward "[\r\n]" nil t)
(setq trailer-offset (TeX-current-offset))
(buffer-substring-no-properties
(point) (point-max))))))))))
;; file name should be relative to master
(unless (string= original "<none>") ; cf. `preview-region'
(setq original (TeX-quote-filename (file-relative-name
original (TeX-master-directory)))))
(setq master-name (TeX-quote-filename master-name))
;; If the first line begins with "%&", put that line separately on
;; the very first line of the region file so that the first line
;; parsing will work.
(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)
(setq original-content (buffer-string))
(let ((inhibit-read-only t))
(erase-buffer)
(setq buffer-file-coding-system
(with-current-buffer master-buffer buffer-file-coding-system))
(insert first-line
"\\message{ !name(" master-name ")}"
header
TeX-region-extra
"\n\\message{ !name(" original ") !offset(")
(setq header-offset (- offset
(1+ (TeX-current-offset))))
(insert (int-to-string header-offset)
") }\n"
region
"\n\\message{ !name(" master-name ") !offset(")
(insert (int-to-string (- trailer-offset
(1+ (TeX-current-offset))))
") }\n"
trailer)
(setq TeX-region-orig-buffer orig-buffer)
(setq TeX-region-master-buffer master-buffer)
(run-hooks 'TeX-region-hook)
(if (string-equal (buffer-string) original-content)
(set-buffer-modified-p nil)
(save-buffer 0))))))