;;;* Last edited: Jun 19 00:52 1992 (krab)
;;;;;;;;;;;;;;;;;;;;;;;;;;; -*- Mode: Emacs-Lisp -*- ;;;;;;;;;;;;;;;;;;;;;;;;;;
;; 
;; ltx-env.el - environments for auc-tex
;; 
;; Copyright (C) 1991 Kresten Krab Thorup (krab@iesd.auc.dk).
;; 
;; 
;; 
;; $Id: ltx-env.el,v 1.11 1992/09/09 20:55:38 amanda Exp $
;; Author          : Kresten Krab Thorup
;; Created On      : Thu May 30 23:50:55 1991
;; Last Modified By: Per Abrahamsen
;; Last Modified On: Mon Sep  7 01:03:10 1992
;; Update Count    : 116
;; 
;; HISTORY
;; 17-Mar-1992  (Last Mod: Tue Mar 17 17:05:53 1992 #97)  Per Abrahamsen
;;    Only opdate LaTeX-last-environment if the user explicitly
;;    entered it.  This avoids getting "document" as the default
;;    environment. 
;; 5-Mar-1992  (Last Mod: Thu Mar  5 13:27:10 1992 #94)  Per Abrahamsen
;;    Made LaTeX-insert-item use newline in order to cooperate with
;;    auto-fill-mode. Idea by jh@cwi.nl (Jan-Hein Buhrman).
;; 27-Jan-1992  (Last Mod: Mon Jan 27 11:57:34 1992 #92)  Per Abrahamsen
;;    Made some of gf's memory function work better with default arguments.  
;;    Use `y-or-n-p' when asking if you want to have a center in your figure.
;; 7-Dec-1991  (Last Mod: Sat Dec  7 14:39:46 1991 #43)  Kresten Krab Thorup
;;    Added LaTeX-close-environment, Idea by Leonard Roseman
;;    <roseman@hustat.harvard.edu>   
;; 5-Sep-1991  (Last Mod: Sun Sep  1 19:58:44 1991 #13) George Ferguson
;;    Modified by gf to use LaTeX-last-environment so the last environment
;;    environment used by LaTeX-environment() becomes the default.
;;    Also changed default value of LaTeX-float.
;;    Also added optional centering for figure and table environments,
;;    and LaTeX-center-floats to keep track of the default.
;;    Also added LaTeX-last-format to keep track of array and tabular
;;    formats, and changed LaTeX-array-hook() to set it.
;; 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; 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>
;; George Ferguson                   <ferguson@cs.rochester.edu>
;; Leonard Roseman                   <roseman@hustat.harvard.edu>   
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;  TO DO LIST  (to add items, mail auc-tex_mgr@iesd.auc.dk)
;;
;;  Make a database system for addresses in the letter environment
;;  Make the letter environment hook produce `documentstyle' too
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


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

(provide 'ltx-env)
(require 'tex-symb)

(defvar LaTeX-slide-color "" "Most recently used slide color.")
(make-variable-buffer-local 'LaTeX-slide-color)

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

(defvar LaTeX-style-alist '(("book" . 0)
			    ("article" . 2)
			    ("letter")
			    ("slides")
			    ("report"))
  "Alist of (documentstyle . level), where level is the largest
partitioning of the text, most often used with this documentstyle. If
a documentstyle isn't mentioned, or level is nil,
LaTeX-largest-section is assumed.")

; gf: added to keep track of centering preference
(defvar LaTeX-center-floats "yes"
  "Whether to insert a center environment inside a figure or table
environment.")

(defvar LaTeX-style-hook-alist '(("slides" . LaTeX-slides-hook))
  "An alist of documentstyle hooks for LaTeX mode. The cdr
(if not nil), is taken to be a function to be called when starting
the matching LaTeX-style. The function is called with the car as argument.

You may add your own favorite hooks after loading this variable:

(require 'ltx-env)
(setq LaTeX-style-hook-alist
      (cons (list \"style\" . myhook) LaTeX-style-hook-alist))")

(defvar LaTeX-environment-alist	'(("document" . LaTeX-document-hook)
				  ("enumerate" . LaTeX-item-hook)
				  ("itemize" . LaTeX-item-hook)
				  ("description" . LaTeX-item-hook)
				  ("list" . LaTeX-list-hook)
				  ("trivlist" . LaTeX-item-hook)
;;				  ("danish")
;;				  ("english")
;;				  ("center")
				  ("flushleft")
				  ("flushright")
				  ("verbatim")
				  ("verbatim*")
				  ("picture" . LaTeX-picture-hook)
;;				  ("tabbing")
				  ("figure" . LaTeX-figure-hook)
				  ("figure*" . LaTeX-figure-hook)
				  ("table" . LaTeX-figure-hook)
				  ("table*" . LaTeX-figure-hook)
				  ("tabular" . LaTeX-array-hook)
				  ("tabular*" . LaTeX-array-hook)
				  ("array" . LaTeX-array-hook)
				  ("eqnarray" . LaTeX-label-hook)
				  ("eqnarray*" . LaTeX-label-hook)
				  ("equation" . LaTeX-label-hook)
;;				  ("math")
;;				  ("displaymath")
;;				  ("abstract")
;;				  ("titlepage")
				  ("minipage" . LaTeX-minipage-hook)
				  ("thebibliography" . LaTeX-bib-hook)
;;				  ("quote")
;;				  ("quotation")
				  ("letter" . LaTeX-recipient-hook)
				  ("slide" . LaTeX-slide-hook)
				  ("overlay" . LaTeX-slide-hook)
;;				  ("note")
;;				  ("BIG")
;;				  ("Big")
;;				  ("big")
;;				  ("displaymath")
;;				  ("float")
;;				  ("flushleft")
;;				  ("flushright")
;;				  ("footnotesize")
;;				  ("list")
;;				  ("math")
;;				  ("sloppypar")
;;				  ("small")
;;				  ("theindex")
				  ("theorem")
;;				  ("trivlist")
;;				  ("verse")
				  )
  "An alist of all the possible environments in LaTeX mode. The cdr
(if not nil), is taken to be a function to be called when starting
LaTeX-environment. The function is called with the car as argument.

You may add your own favorite environments after loading this variable:

(require 'ltx-env)
(setq LaTeX-environment-alist
      (cons (list \"mylist\") LaTeX-environment-alist))")

(defvar LaTeX-item-alist '(("description" . LaTeX-item-argument-hook))
  "An alist of environments where items have a special syntax. The cdr
is the name of the function, used to insert this kind of items.")

; gf: Added so last environment becomes default
(defvar LaTeX-last-environment LaTeX-default-environment
  "Contains the name of the last environment added with LaTeX-environment
so that it will be the default next time.")

(defun LaTeX-environment ()
  "Make LaTeX environment (\\begin{...}-\\end{...} pair).
It may be cutstomized with the following variables:

LaTeX-default-environment	Your favorite environment.
LaTeX-default-style		Your favorite document style.
LaTeX-default-style-options	Your favorite document style options.
LaTeX-float                     Where you want figures and tables to float.
LaTeX-table-label               Your prefix to labels in tables.
LaTeX-figure-label              Your prefix to labels in figures.
LaTeX-environment-alist	        List of normally used environments.

Dublicate entries in the completion list may appear, due to the
mecanism for automatic recognition of environments defined in the
used LaTeX style files..."
  
  (interactive)
  (LaTeX-update-environments)
  (setq local-envs (append env-names LaTeX-environment-alist))
  (let ((environment
	 (completing-read
	  (concat "Environment type: (default "
		  (if (near-bobp)
		      "document"
		    LaTeX-last-environment)
		  ") ")
	  local-envs)))
    
    (cond ((and (zerop (length environment))
		(near-bobp))
	   (setq environment "document"))
	  ((zerop (length environment))
	   (setq environment LaTeX-last-environment))
	  (t ;; gf: set this so it's the default next time
	   (setq LaTeX-last-environment environment)))
    (if (and (assoc environment LaTeX-environment-alist)
	     (cdr (assoc environment LaTeX-environment-alist)))
	(funcall (cdr (assoc environment  LaTeX-environment-alist))
		 environment)
      (LaTeX-insert-environment environment))))

(defun LaTeX-insert-environment (environment &optional extra)
  "Insert environment of type ENV, with optional argument EXTRA."
  (LaTeX-indent-line)
  (insert TeX-esc "begin" TeX-grop environment TeX-grcl)
  (if extra (insert extra))
  (newline-and-indent)
  (newline)
  (insert TeX-esc "end" TeX-grop environment TeX-grcl)
  (LaTeX-indent-line)
  (end-of-line 0))

(defun LaTeX-document-hook (&optional ignore)
  "Create new LaTeX document."
  (let ((style (completing-read 
		(concat "Document style: (default " LaTeX-default-style ") ")
		LaTeX-style-alist))
	(options (read-input "Options: " LaTeX-default-style-options)))
    (if (zerop (length style))
	(setq style LaTeX-default-style))

    ;; save those to use with completion
    (setq LaTeX-style style)
    (setq LaTeX-options options)

    ;; insert the \documentstyle line
    (insert TeX-esc "documentstyle" )
    (if (not (zerop (length options)))
	(insert LaTeX-optop options LaTeX-optcl))
    (insert TeX-grop style TeX-grcl)

    (newline 3)
    (end-of-line 0)
    (LaTeX-insert-environment "document")
    (if (and (assoc style LaTeX-style-hook-alist)
	     (symbolp (cdr (assoc style LaTeX-style-hook-alist))))
	(funcall (cdr (assoc style LaTeX-style-hook-alist)) style))))

(defun LaTeX-item-hook (environment)
  "Insert ENVIRONMENT and the first item."
  (LaTeX-insert-environment environment)
  (end-of-line 0)
  (delete-char 1)
  (delete-horizontal-space)
  (LaTeX-insert-item))

; gf: added to keep track of centering preference
(defvar LaTeX-center-floats "yes"
  "Whether to insert a center environment inside a figure or table
environment.")

(defun LaTeX-figure-hook (environment)
  "Create ENVIRONMENT with \\label and \\caption commands."
  (let ((float (read-input "Float to: " LaTeX-float))
	(caption (read-input "Caption: "))
	(label (read-input "Label: " (cond ((string= "figure" environment)
					    LaTeX-figure-label)
					   ((string= "table" environment)
					    LaTeX-table-label)
					   (t nil))))
        ; gf: ask if this should be centered
        (center (y-or-n-p "Center: ")))

    (setq LaTeX-float (if (zerop (length float))
			  LaTeX-float
			float))
	  
    (LaTeX-insert-environment environment
			      (concat LaTeX-optop LaTeX-float LaTeX-optcl))
    
    ;; gf: add center if requested
    (if center
	(progn
	  (insert "\\leavevmode")
	  (newline-and-indent)
	  (LaTeX-insert-environment "center")))
    (setq LaTeX-center-floats center)
    
    (if (or (zerop (length label))
	    (and (string= "figure" environment)
		 (equal LaTeX-figure-label label))
	    (and (string= "table" environment)
		 (equal LaTeX-table-label label)))
	()
      (newline-and-indent)
      (insert TeX-esc "label" TeX-grop label TeX-grcl)
      (end-of-line 0)
      (LaTeX-indent-line))
    (if (zerop (length caption))
	()
      (newline-and-indent)
      (insert TeX-esc "caption" TeX-grop caption TeX-grcl)
      (end-of-line 0)
      (LaTeX-indent-line))
    
    ;; gf: Add tabular if we're in a floating table
    (if (string= environment "table") (LaTeX-array-hook "tabular"))))

; gf: added to keep track of formats
(defvar LaTeX-last-format ""
  "Specifies the default format string for array and tabular environments.")

;;; mk: added to keep track of positions
(defvar LaTeX-last-position ""
  "Specifies the default position string for array and tabular environments.")

(defun LaTeX-array-hook (environment)
  "Insert ENVIRONMENT with position and column specifications 
like array and tabular."
  (let ((pos (read-input "Position: "))
	(fmt (read-input "Format: " LaTeX-last-format)))
    (setq LaTeX-last-position pos)
      (setq LaTeX-last-format fmt)
    (LaTeX-insert-environment environment
			      (concat
			        (if (not (zerop (length pos)))
				    (format "[%s]" pos))
				(format "{%s}" fmt)))
    (end-of-line 0)
    (next-line 1)
    (delete-horizontal-space)))

(defun LaTeX-label-hook (environment)
  "Insert ENVIRONMENT and prompt for label."
  (let ((label (read-input "Label: ")))
    (LaTeX-insert-environment environment)
    (if (not (zerop (length label)))
	(progn
	  (newline-and-indent)
	  (insert TeX-esc "label" TeX-grop label TeX-grcl)
	  (end-of-line 0)))))

(defun LaTeX-recipient-hook (environment)
  "Insert ENVIRONMENT and prompt for recipient and address."
  (let ((sender (read-input "Sender: " (user-full-name)))
	(sender-address (read-input "Sender address: "))
	(recipient (read-input "Recipient: "))
	(address (read-input "Recipient address: "))
	(signature (read-input "Signature: "))
	(opening (read-input "Opening: "))
	(closing (read-input "Closing: "))
	(date (read-input "Date: " (LaTeX-today))))

    (insert TeX-esc "name" TeX-grop sender TeX-grcl)
    (newline-and-indent)
    (if (not (zerop (length sender-address)))
	(progn
	  (insert TeX-esc "address" TeX-grop sender-address TeX-grcl)
	  (newline-and-indent)))
    (if (not (zerop (length signature)))
	(progn
	  (insert TeX-esc "signature" TeX-grop signature TeX-grcl)
	  (newline-and-indent)))
    (if (not (zerop (length date)))
	(progn
	  (insert TeX-esc "def" TeX-esc "today" TeX-grop date TeX-grcl)
	  (newline-and-indent)))
    (newline-and-indent)

    (let ((indentation (current-column)))
      (LaTeX-insert-environment
       environment
       (concat TeX-grop recipient
	       (if (not (zerop (length address)))
		   (concat
		    (if (not (zerop (length recipient)))
			(concat " " TeX-esc TeX-esc " "))
		    address))
	       TeX-grcl))
      (save-excursion			; Fix indentation of address
	(if (search-backward TeX-grcl nil 'move)
	    (let ((addr-end (point-marker)))
	      (if (search-backward TeX-grop nil 'move)
		  (let ((addr-column (current-column)))
		    (while (search-forward
			    (concat TeX-esc TeX-esc)
			    (marker-position addr-end) 'move)
		      (progn
			(newline)
			(indent-to addr-column))))))))
      (insert "\n")
      (indent-to indentation))
    (insert TeX-esc "opening"
	    TeX-grop
	    (if (zerop (length opening))
		(concat TeX-esc " ")
	      opening)
	    TeX-grcl "\n")

    (indent-relative-maybe)
    (save-excursion
      (insert "\n" TeX-esc "closing"
	      TeX-grop
	      (if (zerop (length closing))
		  (concat TeX-esc " ")
		closing)
	      TeX-grcl "\n")
      (indent-relative-maybe))))

(defun LaTeX-today nil
  "Return a string representing todays date according to flavor."
  (interactive)
  (let ((ctime-string (current-time-string))
	(month-alist '(("Jan". "01")
		       ("Feb" . "02")
		       ("Mar" . "03")
		       ("Apr" . "04")
		       ("May" . "05")
		       ("Jun" . "06")
		       ("Jul" . "07")
		       ("Aug" . "08")
		       ("Sep" . "09")
		       ("Oct" . "10")
		       ("Nov" . "11")
		       ("Dec" . "12"))))
    (string-match
     "^\\S-+\\s-+\\(\\S-+\\)\\s-+\\(\\S-+\\)\\s-+\\S-+\\s-+\\(\\S-+\\)"
     ctime-string)
    (let ((year (substring ctime-string (match-beginning 3) (match-end 3)))
	  (month (substring ctime-string (match-beginning 1) (match-end 1)))
	  (day (substring ctime-string (match-beginning 2) (match-end 2))))
      (if (assoc month month-alist)
	  (progn
	    (setq month (cdr (assoc month month-alist)))
	    (if (> 2 (length day))
		(setq day (concat "0" day)))))
      (format "%s-%s-%s" year month day))))

(defun LaTeX-slides-hook (style)
  "Prompt for and insert STYLE options."
  (let ((slide-file (read-input "Slide file: "))
	(slide-colors (read-input "Slide colors (comma separetade list): "
				  "black")))
    (save-excursion
      (goto-char (point-min))		; insert before \end{document}
      (if (re-search-forward ".end.document." (point-max) t)
	  (beginning-of-line 1))
      (open-line 2)
      (indent-relative-maybe)
      (if (equal slide-colors "black")
	  (insert TeX-esc "blackandwhite"
		  TeX-grop slide-file TeX-grcl)
	(progn
	  (insert TeX-esc "colors"
		  TeX-grop slide-colors TeX-grcl)
	  (newline-and-indent)
	  (insert TeX-esc "colorslides"
		  TeX-grop slide-file TeX-grcl))))))

(defun LaTeX-slide-hook (environment)
  "Insert ENVIRONMENT and prompt for slide colors."
  (setq LaTeX-slide-color
	(read-input "Slide colors: " LaTeX-slide-color))
  (LaTeX-insert-environment environment
			    (concat TeX-grop LaTeX-slide-color TeX-grcl)))

(defun LaTeX-minipage-hook (environment)
  "Insert ENVIRONMENT and prompt for width and optional position colors."
  (let ((width (read-input "Minipage width: "))
	(place (read-input "Placement (none, t, or b): ")))
  (LaTeX-insert-environment
   environment
   (concat (if (not (zerop (length place)))
	       (concat LaTeX-optop place LaTeX-optcl))
	   TeX-grop width TeX-grcl))))

(defun LaTeX-current-environment (arg)
  "Return the name (a string) of the LaTeX environment ARG - 1 level's
from the level containing point."
  (save-excursion
    (if (re-search-backward (concat
			     (regexp-quote TeX-esc) "begin" (regexp-quote TeX-grop)
			     "\\|"
			     (regexp-quote TeX-esc) "end" (regexp-quote TeX-grop)) nil t 1)
	(if (looking-at (concat (regexp-quote TeX-esc) "end" (regexp-quote TeX-grop)))
	    (LaTeX-current-environment (1+ arg))
	  (if (/= arg 1)
	      (LaTeX-current-environment (1- arg))
	    (search-forward TeX-grop)
	    (let ((beg (point)))
	      (search-forward TeX-grcl)
	      (backward-char 1)
	      (buffer-substring beg (point)))))
      "document")))

(defun LaTeX-insert-item ()
  "Insert a new item in an environment. You may use LaTeX-item-alist
to change the routines used to insert the item."
  (interactive)
  (let ((environment (LaTeX-current-environment 1)))
    (if (assoc environment LaTeX-item-alist)
	(funcall (cdr (assoc environment LaTeX-item-alist)))
      (newline)
      (insert TeX-esc "item ")
      (LaTeX-indent-line)
      (end-of-line))))

(defun LaTeX-item-argument-hook ()
  "Insert a new item with an optional argument."
  (insert "\n" TeX-esc "item" LaTeX-optop LaTeX-optcl " ")
  (LaTeX-indent-line)
  (beginning-of-line)
  (search-forward LaTeX-optop))

(defun near-bobp ()
  "Return t iff there's nothing but whitespace between (bob) and (point)."
  (save-excursion
    (skip-chars-backward " \t\n")
    (bobp)))

(defun get-environments (files)
"Returns a list of symbols defined in FILES"
(require 'tex-misc)
(let ((visited '())
      (symbols '())
      (yet-to-visit files))
  (while (not (equal nil yet-to-visit))
    (recursive-add-environments (car yet-to-visit))
    (setq yet-to-visit (cdr yet-to-visit)))
  symbols))

(defun recursive-add-environments (file)
  (if (not (memq file visited))
      ;; if we have not already been here
      (progn
	;; add this file to the list of visited.
	(setq visited (cons file visited))
	
	;; and add the associated symbols to the symbols variable
	(setq symbols (append symbols
			      (car (cdr (cdr (assoc file TeX-symb))))))

	;; now loop over the subfiles of this file
	(let ((yet-to-visit (get-subfiles file)))
	  (while (not (equal nil yet-to-visit))
	    (recursive-add-environments (car yet-to-visit))
	    (setq yet-to-visit (cdr yet-to-visit)))))))

(defun LaTeX-update-environments ()
  "Update the completion list (env-names)"
  (interactive)
  (let ((fmt TeX-format-package)
	(sty (list (LaTeX-document-style)))
	(opt (split-string "," (if (LaTeX-style-options)
				   (LaTeX-style-options)
				 ""))))
    (if
	;; if none of the lists has changed - do nothing.
	;; otherwise do the actual update. and set the new values
	;; for the `last' variables.
	(not
	 (and (equal TeX-last-fmt (if fmt fmt '("")))
	      (equal LaTeX-last-sty (if sty sty '("")))
	      (equal LaTeX-last-opt (if opt opt '("")))))
	(let ((sty-pt (list (concat
			     (cond ((string= "article" (car sty))
				    "art")
				   ((string= "book" (car sty))
				    "bk")
				   ((string= "report" (car sty))
				    "rep")
				   (t
				    (car sty)))
			     (cond ((assoc "11pt" opt)
				    "11")
				   ((assoc "12pt" opt)
				    "12")
				   (t
				    "10"))))))
	  (setq env-names (sort (get-environments (append fmt sty opt sty-pt))
				'string-list-sorter))
	  (setq TeX-last-fmt (if fmt fmt '("")))
	  (setq LaTeX-last-sty (if sty sty '("")))
	  (setq LaTeX-last-opt (if opt opt '("")))
	  ()
	  ))))

(defun string-list-sorter (a b)
  (string< (car a) (car b)))


(defvar env-names (get-environments TeX-format-package)
  "List of currently available additional evironments.")


(defun LaTeX-close-environment ()
  "Creates an \\end{...} to match the current environment"
  (interactive "*")
  (end-of-line)
  (insert "\n\n")
  (insert "\\end{" (LaTeX-current-environment 1) "}\n")
  (forward-line -2)
  (LaTeX-indent-line))

 
(defun LaTeX-list-hook (environment)
  "Insert ENVIRONMENT and the first item."
  (let ((label (read-input "Default Label: ")))
    (LaTeX-insert-environment environment
			      (format "{%s}{}" label))
    (end-of-line 0)
    (delete-char 1)
    (delete-horizontal-space))
  (LaTeX-insert-item))

(defun LaTeX-minipage-hook (environment)
  "Create new LaTeX minipage."
  (let ((pos (read-input "Position: " LaTeX-last-position))
	(width (read-input "Width: ")))
    (setq LaTeX-last-position pos)
    (if (zerop (length width))
	(setq width "4cm"))
    (LaTeX-insert-environment environment
			      (concat (if (not (zerop (length pos)))
					  (format "[%s]" pos))
				      (format "{%s}" width)))
    (end-of-line 0)
    (next-line 1)
    (delete-horizontal-space)))

(defun LaTeX-tabular*-hook (environment)
  "Insert ENVIRONMENT with width, position and column specifications."
  (let ((width (read-input "Width: "))
	(pos (read-input "Position: " LaTeX-last-position))
	(fmt (read-input "Format: " LaTeX-last-format)))
    (setq LaTeX-last-position pos)
    (setq LaTeX-last-format fmt)
    (LaTeX-insert-environment environment
			      (concat
			        (if (not (zerop (length width)))
				    (format "{%s}" width))
			        (if (not (zerop (length pos)))
					  (format "[%s]" pos))
				(format "{%s}" fmt)))
    (end-of-line 0)
    (next-line 1)
    (delete-horizontal-space)))

(defun LaTeX-picture-hook (environment)
  "Insert ENVIRONMENT with width, height specifications."
  (let ((width (read-input "Width: "))
	(height (read-input "Height: "))
	(x-offset (read-input "X Offset: "))
	(y-offset (read-input "Y Offset: ")))
    (if (zerop (length x-offset))
	(setq x-offset "0"))
    (if (zerop (length y-offset))
	(setq y-offset "0"))
    (LaTeX-insert-environment environment
			      (concat (format "(%s,%s)" width height)
				      (if (not (and (string= x-offset "0")
						    (string= y-offset "0")))
					  (format "(%s,%s)" x-offset y-offset))))
				      
    (end-of-line 0)
    (next-line 1)
    (delete-horizontal-space)))

(defun LaTeX-bib-hook (environment)
  "Insert ENVIRONMENT with label for bibitem."
  (LaTeX-insert-environment environment
			    (concat TeX-grop
				    (read-input "Label for BibItem: " "99")
				    TeX-grcl)))

