;;;;;;;;;;;;;;;;;;;;;;;;;;; -*- Mode: Emacs-Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 
;; tex-misc.el - miscellaneous utlility functions for auc-tex.
;; 
;; Copyright (C) 1991 Kresten Krab Thorup (krab@iesd.auc.dk).
;; 
;; $Id: tex-misc.el,v 5.18 1992/03/18 17:34:39 krab Exp $
;; 
;; Author          : Kresten Krab Thorup
;; Created On      : Fri May 31 05:46:10 1991
;; Last Modified By: Kresten Krab Thorup
;; Last Modified On: Wed Mar 18 17:31:21 1992
;; Update Count    : 55
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; AUC TeX is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY.  No author or distributor
;; accepts responsibility to anyone for the consequences of using it
;; or for whether it serves any particular purpose or works at all,
;; unless he says so in writing. 
;;
;; Everyone is granted permission to copy, modify and redistribute
;; this file, but only under the conditions described in the
;; document "GNU Emacs copying permission notice".   An exact copy
;; of the document is supposed to have been given to you along with
;; this file so that you can know how you may redistribute it all.
;; It should be in a file named COPYING.  Among other things, the
;; copyright notice and this notice must be preserved on all copies.
;;
;; This software was written as part of the author's official duty as
;; an employee of the University of Aalborg (denmark) and is in the
;; public domain.  You are free to use this software as you wish, but
;; WITHOUT ANY WARRANTY WHATSOEVER.  It would be nice, though if when
;; you use this code, you give due credit to the author.
;;

;; HISTORY 
;; 11-Sep-1991  (Last Mod: Wed Sep 11 10:20:26 1991 #44)  Kresten Krab Thorup
;;    Updated comment functions as provided by Per Abrahamsen. These
;;    are his own comments: 
;;    - undo now works on complete region, instead of line by line.
;;    - they now also works on the boundery of the buffer.
;;    - (interactive "r") is used as God and Stallman intended.
;; 2-Sep-1991  (Last Mod: Mon Sep  2 00:22:58 1991 #40)  Kresten Krab Thorup
;;    moved TeX-complete-symbol to it's own module.


;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; comment management
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(provide 'tex-misc)
(require 'tex-symb)

(defun TeX-comment-out-region (start end)
  "Make all lines in the region into TeX comments."
  (interactive "*r")
  (save-excursion
    (goto-char start)
    (beginning-of-line)
    (setq start (point))
    (goto-char end)
    (end-of-line)
    (setq end (point))
    (save-restriction 
      (narrow-to-region end start)
      (goto-char (point-min))
      (while (not (eobp))
	(if (looking-at (regexp-quote comment-start))
	    ()
	  (insert comment-start)
	  (beginning-of-line))
	(forward-line 1)))))

(defun TeX-un-comment-region (start end)
 "Remove any comment characters in front of the lines in the region."
  (interactive "*r") 
  (save-excursion
    (save-restriction 
      (narrow-to-region start end)
      (goto-char (point-min))
      (while (not (eobp))
	(while (and (not (eobp))
		    (looking-at (regexp-quote comment-start)))
	  (delete-char 1))
	(forward-line 1)))))

(defun TeX-un-comment ()
  "Delete comment characters from the beginning of each line in
the current comment."
  (interactive "*")
  (save-excursion
    ; Find first comment line
    (re-search-backward (concat "^[^" comment-start "]") nil 'limit)
    (let ((beg (point)))
      (forward-line 1)
      ; Find last comment line
      (re-search-forward (concat "^[^" comment-start "]") nil 'limit)
      ; Uncomment region
      (TeX-un-comment-region beg (point)))))

(defun TeX-comment-out-paragraph ()
  "Inserts % at the beginning of every line in the current paragraph."
  (interactive)
  (save-excursion
    (end-of-line)
    (start-of-paragraph-text)
    (beginning-of-line)
    (setq beg (point))
    (end-of-paragraph-text)
    ; (forward-line -1)
    (beginning-of-line)
    (TeX-comment-out-region beg (point))))

;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Validate TeX text
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun TeX-validate-buffer ()
  "Check current buffer for paragraphs containing mismatched $'s.
As each such paragraph is found, a mark is pushed at its beginning,
and the location is displayed for a few seconds."
  (interactive)
  (let ((opoint (point)))
    (goto-char (point-max))
    ;; Does not use save-excursion
    ;; because we do not want to save the mark.
    (unwind-protect
	(while (and (not (input-pending-p)) (not (bobp)))
	  (let ((end (point)))
	    (search-backward "\n\n" nil 'move)
	    (or (TeX-validate-paragraph (point) end)
		(progn
		  (push-mark (point))
		  (message "Mismatch found in pararaph starting here")
		  (sit-for 4)))))
      (goto-char opoint))))

(defun TeX-validate-paragraph (start end)
  (condition-case ()
      (save-excursion
	(save-restriction
	  (narrow-to-region start end)
	  (goto-char start)
	  (forward-sexp (- end start))
	  t))
    (error nil)))

(defun TeX-terminate-paragraph (inhibit-validation)
  "Insert two newlines, breaking a paragraph for TeX.
Check for mismatched braces/$'s in paragraph being terminated.
A prefix arg inhibits the checking."
  (interactive "P")
  (or inhibit-validation
      (TeX-validate-paragraph
       (save-excursion
	 (search-backward "\n\n" nil 'move)
	 (point))
       (point))
      (message "Paragraph being closed appears to contain a mismatch"))
  (reindent-then-newline-and-indent)
  (newline-and-indent))


(defun get-subfiles (file)
"Returns a list of environments defined in file FILE"
(car (cdr (cdr (cdr (assoc file TeX-symb))))))
