bug-gnu-emacs
[Top][All Lists]
Advanced

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

[patch] enhanced functionality of tex-region in case when buffer has no


From: Joe Corneli
Subject: [patch] enhanced functionality of tex-region in case when buffer has no header
Date: Tue, 30 Dec 2003 00:03:33 -0600

Purpose.

This patch addresses an interest in being able to TeX regions in
latex files that don't have headers (or footer) by prefixing them
with some standard header.  Without this patch, tex-region typically
generates either nothing or garbage. With this patch and a proper
(one to two line) configuration, tex-region behaves as you would want
it to behave.  All is not perfect, however, because in making the
patch I broke some other functionality.


Details.

In .emacs one should have something like this:

(setq tex-default-header-file "~/apm/Xi.tex")

Then you can tex a region in a LaTeX file that doesn't have a header,
using the header found in the tex-default-header-file.  (If the file
has a header then that file's own header is used.)

If tex-default-header-file is not bound, behavior is approximately
the same as it was before I made the changes, i.e. when you attempt
to tex a region in a file that doesn't have a header, things don't
work very well.

I say "approximately" because in making the changes, I broke a number
of features, hopefully commented on sufficiently well in the patch.
The most egregious problem is that now only latex files work, so in
order to use this patch, one should

(setq auto-mode-alist (cons '("\\.tex$" . latex-mode)  auto-mode-alist))

otherwise there will be an error.

I have decided not to pursue this code any further (attempting to
fixing broken things, extending to tex-file) until I learn whether
the behavior I am after can be gotten in some standard way (I thought
AUCTeX would do this, but I couldn't figure out how).

If this behavior is standard, may I humbly suggest making it part of
tex-mode. If it is already part of tex mode, please let me know so
that I can kick myself.


Diff.

cd /Users/joe/
diff -c /sw/share/emacs/21.3/lisp/textmodes/tex-mode.el 
/Users/joe/site-lisp/advanced-tex-mode.el
*** /sw/share/emacs/21.3/lisp/textmodes/tex-mode.el     Sun Mar  3 19:38:27 2002
--- /Users/joe/site-lisp/advanced-tex-mode.el   Sat Dec 27 18:48:33 2003
***************
*** 89,94 ****
--- 89,116 ----
                   file)
    :group 'tex-file)
  
+ ;; The functionality here should probably be combined with the functionality 
+ ;; provided in relation to the variable tex-main-file.  But I found it
+ ;; more convenient to define something seperate for now.  Note that in
+ ;; carrying out the operation, I have broken the functionality of tex-region
+ ;; on plain tex files (everything is regarded as LaTeX at present).
+ ;; Maybe the functionality is even present in AUC TeX -- it would seem
+ ;; to have to be, if the master file was working properly, you would
+ ;; pretty much have to be able to tex regions using the header found in
+ ;; that file.  However, as far as I understand it the AUC TeX functions
+ ;; work in a more complicated way than I need. Maybe I'm wrong, maybe
+ ;; that would be the best way to go, but the simple changes to this file
+ ;; seem to work for my simple needs.
+ 
+ ;; -- jac Sat Dec 27 18:43:23 2003
+ 
+ ;;;###autoload
+ (defcustom tex-default-header-file nil
+   "*Grab the header from this file when TeXing a region (or file) with no 
header."
+   :type '(choice (const :tag "None" nil)
+                  file)
+   :group 'tex-file)
+ 
  ;;;###autoload
  (defcustom tex-offer-save t
    "*If non-nil, ask about saving modified buffers before \\[tex-file] is run."
***************
*** 780,787 ****
    (tex-common-initialization)
    (setq tex-command latex-run-command)
    (setq tex-start-of-header "\\\\document\\(style\\|class\\)")
!   (setq tex-end-of-header "\\\\begin\\s-*{document}")
!   (setq tex-trailer "\\end\\s-*{document}\n")
    ;; A line containing just $$ is treated as a paragraph separator.
    ;; A line starting with $$ starts a paragraph,
    ;; but does not separate paragraphs if it has more stuff on it.
--- 802,809 ----
    (tex-common-initialization)
    (setq tex-command latex-run-command)
    (setq tex-start-of-header "\\\\document\\(style\\|class\\)")
!   (setq tex-end-of-header "\\\\begin{document}")
!   (setq tex-trailer "\\end{document}\n")
    ;; A line containing just $$ is treated as a paragraph separator.
    ;; A line starting with $$ starts a paragraph,
    ;; but does not separate paragraphs if it has more stuff on it.
***************
*** 1564,1569 ****
--- 1586,1628 ----
  
  ;;; The commands:
  
+ (defun grab-header (buffer tex-out-file)
+   "Output the header if it exists."
+   (save-excursion 
+     (set-buffer buffer)
+     (widen)
+     (goto-char (point-min))
+     (forward-line 100)
+     (let ((search-end (point))
+         (already-output 0))
+       (goto-char (point-min))
+ 
+       ;; Maybe copy first line, such as `\input texinfo', to temp file.
+       (and tex-first-line-header-regexp
+          (looking-at tex-first-line-header-regexp)
+          (write-region (point)
+                        (progn (forward-line 1)
+                               (setq already-output (point)))
+                        tex-out-file nil nil))
+ 
+       (re-search-forward tex-start-of-header search-end t)
+       (let ((hbeg nil)
+           (hend nil))
+       (beginning-of-line)
+       (setq hbeg (point))             ;mark beginning of header
+       (if (re-search-forward tex-end-of-header nil t)
+           (progn (forward-line 1)
+                  (setq hend (point))  ;mark end of header
+                  (write-region 
+                   ;; if something has been output already,
+                   ;; we don't need to ouput it again, so skip
+                   ;; that part
+                   (max hbeg already-output)
+                   hend
+                   tex-out-file
+                   (not (zerop already-output)) nil)))
+       hend))))
+ 
  (defun tex-region (beg end)
    "Run TeX on the current region, via a temporary file.
  The file's name comes from the variable `tex-zap-file' and the
***************
*** 1598,1653 ****
      ;; Write the new temp file.
      (save-excursion
        (save-restriction
!       (widen)
!       (goto-char (point-min))
!       (forward-line 100)
!       (let ((search-end (point))
!             (default-directory zap-directory)
!             (already-output 0))
!         (goto-char (point-min))
! 
!           ;; Maybe copy first line, such as `\input texinfo', to temp file.
!         (and tex-first-line-header-regexp
!              (looking-at tex-first-line-header-regexp)
!              (write-region (point)
!                            (progn (forward-line 1)
!                                   (setq already-output (point)))
!                            tex-out-file nil nil))
! 
!         ;; Write out the header, if there is one,
!         ;; and any of the specified region which extends before it.
!         ;; But don't repeat anything already written.
!         (if (re-search-forward tex-start-of-header search-end t)
!             (let (hbeg)
!               (beginning-of-line)
!               (setq hbeg (point))     ;mark beginning of header
!               (if (re-search-forward tex-end-of-header nil t)
!                   (let (hend)
!                     (forward-line 1)
!                     (setq hend (point)) ;mark end of header
!                     (write-region (max (min hbeg beg) already-output)
!                                   hend
!                                   tex-out-file
!                                   (not (zerop already-output)) nil)
!                     (setq already-output hend)))))
  
          ;; Write out the specified region
!         ;; (but don't repeat anything already written).
!         (write-region (max beg already-output) end
                        tex-out-file
!                       (not (zerop already-output)) nil))
        ;; Write the trailer, if any.
        ;; Precede it with a newline to make sure it
        ;; is not hidden in a comment.
        (if tex-trailer
            (write-region (concat "\n" tex-trailer) nil
!                         tex-out-file t nil))))
      ;; Record the file name to be deleted afterward.
      (setq tex-last-temp-file tex-out-file)
      ;; Use a relative file name here because (1) the proper dir
      ;; is already current, and (2) the abs file name is sometimes
      ;; too long and can make tex crash.
!     (tex-start-tex tex-command (concat tex-zap-file ".tex") zap-directory)
      (setq tex-print-file tex-out-file)))
  
  (defun tex-buffer ()
--- 1657,1697 ----
      ;; Write the new temp file.
      (save-excursion
        (save-restriction
!         ;; Write out the header, if there is one
!         ; it would be good to be able to specify a default header! jac -- Mon 
Dec  8 15:02:53 2003
! 
!       (let  ((already-output
!               (or (grab-header (current-buffer) tex-out-file)
!                   ;; we now have a second chance to find the header (by 
looking in the default header file)
!                   (if tex-default-header-file
!                       (grab-header (save-window-excursion 
!                                      (save-excursion 
!                                        (find-file tex-default-header-file)
!                                        (current-buffer)))
!                                    tex-out-file)))))
  
          ;; Write out the specified region
!         ;; (but don't repeat anything already written). 
!         ;; (I broke the parenthetical part.) -- jac Sat Dec 27 18:25:20 2003 
!         (write-region beg end 
                        tex-out-file
!                       (not (zerop already-output)) nil)))
! 
        ;; Write the trailer, if any.
        ;; Precede it with a newline to make sure it
        ;; is not hidden in a comment.
        (if tex-trailer
            (write-region (concat "\n" tex-trailer) nil
!                         tex-out-file t nil)))
      ;; Record the file name to be deleted afterward.
      (setq tex-last-temp-file tex-out-file)
      ;; Use a relative file name here because (1) the proper dir
      ;; is already current, and (2) the abs file name is sometimes
      ;; too long and can make tex crash.
!     (tex-start-tex 
!      latex-run-command  ;;note this horrid loss of generality! 
!                         ;;(should be tex-command) jac -- Sat Dec 27 18:06:48 
2003
!      (concat tex-zap-file ".tex") zap-directory)
      (setq tex-print-file tex-out-file)))
  
  (defun tex-buffer ()

Diff finished at Mon Dec 29 23:34:00




reply via email to

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