;;;;;;;;;;;;;;;;;;;;;;;;;;; -*- Mode: Emacs-Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 
;; ltx-sec.el - sectim management for auc-tex
;; 
;; Copyright (C) 1991 Kresten Krab Thorup (krab@iesd.auc.dk).
;; 
;; 
;; 
;; $Id: ltx-sec.el,v 1.8 1992/09/16 12:14:20 amanda Exp $
;; Author          : Kresten Krab Thorup
;; Created On      : Thu May 30 23:46:50 1991
;; Last Modified By: Kresten Krab Thorup
;; Last Modified On: Wed Mar 18 17:31:24 1992
;; Update Count    : 19
;; 
;; HISTORY
;; 12-Jun-1991  (Last Mod: Wed Jun 12 07:32:44 1991 #9)  Kresten Krab Thorup
;;    Added some patch form Sven Mattisson
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; THE GOOD GUYS
;; 
;; Per Abrahamsen                    <abraham@iesd.auc.dk> 
;; Sven Mattisson                    <sven@tde.lth.se>
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;  TO DO LIST  (to add items, mail auc-tex_mgr@iesd.auc.dk)
;;
;;  Make variables so that one can donot need to set labels on
;;  sections below a certain level.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Customization
;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(require 'ltx-env)
(require 'tex-site)

(defvar LaTeX-default-section "section"
  "*The default kind of section used by LaTeX-section, when it can't
make up something else.")

(defvar LaTeX-expected-style "report"
  "*The documentstyle normally used in multi-file documents.
Probably `report' or `book'..")

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defvar LaTeX-section-alist '(("part") ("chapter") ("section")
			      ("subsection") ("subsubsection")
			      ("paragraph") ("subparagraph"))
  "alist which elements is the names of the sections used by LaTeX.")

(defvar LaTeX-section-vector (vector "part" "chapter" "section"
				     "subsection" "subsubsection"
				     "paragraph" "subparagraph")
  "vector which elements is the names of the sections used by LaTeX.")

(defvar LaTeX-largest-level 1
  "Largest usual partition of documentstyles not mentioned in
LaTeX-style-alist.")

(defun LaTeX-section (arg)
  "Template for a LaTeX section. Determinate the type of section to be
inserted, by the argument ARG.

If ARG is nil or missing, use the current level.
If ARG is a list (selected by C-u), go downward one level.
If ARG is negative, go up that many levels.
If ARG is positive or zero, use absolute level:

  0 : part
  1 : chapter
  2 : section
  3 : subsection
  4 : subsubsection
  5 : paragraph
  6 : subparagraph

The following variables can be set to customize:

LaTeX-section-label		Prefix to all section references.
LaTeX-default-section		The default for new sections.
LaTeX-expected-style		The documentstyle assumed, when no 
				\documentstyle{} appear."
  
  (interactive "P")
  (let ((val (prefix-numeric-value arg)))
    (LaTeX-insert-section
     (cond ((null arg)
	    (LaTeX-current-section))
	   ((listp arg)
	    (LaTeX-down-section))
	   ((< val 0)
	    (LaTeX-up-section (- val)))
	   (t val)))))

(defun LaTeX-insert-section (level)
  "Insert section LEVEL at point. Try to read from minibuffer if
LEVEL is out of range."
  (interactive "p")
  (let ((name (completing-read
	       (concat "Select level: (default " 
		       (LaTeX-section-name level) ") ")
	       LaTeX-section-alist
	       nil nil nil))
	(title (read-input "What title: "))
	(label (read-input "What label: " LaTeX-section-label))
	(toc-entry (read-input "Toc Entry: ")))
    (if (zerop (length name))
	(setq name (LaTeX-section-name level)))
    (if (save-excursion (re-search-backward "\\S-+" (point-min) t))
	  (newline-and-indent))
    (insert TeX-esc name)
    (if (zerop (length toc-entry))
	(insert TeX-grop title TeX-grcl)
      (insert LaTeX-optop toc-entry LaTeX-optcl TeX-grop title TeX-grcl) )
    (if (or (zerop (length label))
	    (equal label LaTeX-section-label))
	()
      (newline-and-indent)
      (insert TeX-esc "label" TeX-grop label TeX-grcl))
    (newline)
    (newline-and-indent)))

(defun LaTeX-section-name (level)
  "Return the name of the section, corresponding to LEVEL."
  (interactive "p")
  (if (or (< level 0)
	  (> level 6))
      LaTeX-default-section
    (aref LaTeX-section-vector level)))

(defun LaTeX-current-section ()
  "Return the level of the section that contain point. If point is
not contained in any section, it will return negative.

See also LaTeX-section for description of levels."
  (save-excursion
    (let ((level (if (re-search-backward
		      (concat "^" LaTeX-outline-regexp) nil t)
		     (- (LaTeX-outline-level) 3)
		   -1)))
      (if (or (< level 0)
	      (> level 6))
	  (LaTeX-largest-level)
	level))))

(defun LaTeX-largest-level ()
  "Return the largest partitioning level in the current documentstyle."
  (let ((style (LaTeX-document-style)))
    (if (and style
	     (assoc style LaTeX-style-alist)
	     (cdr (assoc style LaTeX-style-alist)))
	(cdr (assoc style LaTeX-style-alist))
      LaTeX-largest-level)))

(defun LaTeX-down-section ()
  "Return the value of a section one level under the current. Tries to
find what kind of section that have been used earlier in the text, If
this fail, it will just return one less than the current section."
  (save-excursion 
    (let ((current (LaTeX-current-section))
	  (next nil)
	  (regexp (concat "^" LaTeX-outline-regexp)))
      (if (not (re-search-backward regexp nil t))
	  (1+ current)
	(while (not next)
	  (cond
	   ((eq (LaTeX-current-section) current)
	    (if (re-search-forward regexp nil t)
		(if (<= (setq next (LaTeX-current-section)) current) ;Wow!
		    (setq next (1+ current)))
	      (setq next (1+ current))))
	   ((not (re-search-backward regexp nil t))
	    (setq next (1+ current)))))
	next))))

(defun LaTeX-up-section (arg)
  "Return the value of the section ARG levels above this one."
  (save-excursion
    (if (zerop arg)
	(LaTeX-current-section)
      (let ((current (LaTeX-current-section)))
	(while (and (>= (LaTeX-current-section) current)
		    (re-search-backward (concat "^" LaTeX-outline-regexp)
					nil t)))
	(LaTeX-up-section (1- arg))))))


