Re: Weird formatting after TeX-insert-environment

Ralf Angeli <[email protected]>
Newsgroups gmane.emacs.auc-tex
Message-ID <[email protected]>
* Frank Küster (2005-02-07) writes:

> I wanted to convert a tabular environment into a floating
> table. Therefore I marked the complete environment as the region,
> pressed C-c C-e, and selected table. I filled in a caption and
> answered y to "center?", but didn't give any answers for the tabular
> questions that followed. This is how it looked like before:
[...]
> Finally, this is what I expected:
>
> *************************************
> \begin{table}
>   \centering
>   \begin{tabular}{lrlcp{2.5cm}}
      [tabular contents]
>   \end{tabular}
>   \caption{Fitparameter für den Akzeptor-Decay in \proxx}
>   \label{tab:pro20-acc-fit}
> \end{table}

That's not easy to achieve.  I attached a patch which at least
recognizes an active region and moves point to the correct positions.
But there still is a problem.  The table environment is listed in
`LaTeX-indent-environment-list' and won't be filled or indented during
the insertion of an environment.  You can check this by marking the
tabular environment and typing `M-: (LaTeX-insert-environment "table")
RET'.  The result is an incorrect indentation.  Currently I don't have
time to investigate this further.  If anybody is interested, feel free
to have a look at it.

-- 
Ralf
(unnamed) (text/x-patch, 2.1 KB)
--- latex.el	07 Feb 2005 14:11:23 +0100	5.325
+++ latex.el	08 Feb 2005 11:34:29 +0100	
@@ -879,20 +879,26 @@
 					; do not prompt
 		    (read-string "(Optional) Float position: " LaTeX-float)))
 	(caption (read-string "Caption: "))
-	(center (y-or-n-p "Center? ")))
-
+	(center (y-or-n-p "Center? "))
+	(active-mark (and (TeX-active-mark)
+			  (not (eq (mark) (point)))))
+	start-marker end-marker)
+    (when active-mark
+      (if (< (mark) (point))
+	  (exchange-point-and-mark))
+      (setq start-marker (point-marker))
+      (set-marker-insertion-type start-marker t)
+      (setq end-marker (copy-marker (mark))))
     (setq LaTeX-float float)
-
     (LaTeX-insert-environment environment
 			      (unless (zerop (length float))
 				(concat LaTeX-optop float
 					LaTeX-optcl)))
-
+    (when active-mark (goto-char start-marker))
     (when center
       (insert TeX-esc "centering")
       (indent-according-to-mode)
       (LaTeX-newline))
-
     (if (member environment LaTeX-top-caption-list)
 	;; top caption -- do nothing if user skips caption
 	(unless (zerop (length caption))
@@ -906,6 +912,7 @@
 	    (indent-according-to-mode)))
       ;; bottom caption (default) -- do nothing if user skips caption
       (unless (zerop (length caption))
+	(when active-mark (goto-char end-marker))
 	(LaTeX-newline)
 	(indent-according-to-mode)
 	(insert TeX-esc "caption" TeX-grop caption TeX-grcl)
@@ -913,7 +920,11 @@
 	(indent-according-to-mode)
 	;; ask for a label -- if user skips label, remove the last new
 	;; line again
-	(unless (LaTeX-label environment)
+	(if (LaTeX-label environment)
+	    (progn
+	      (unless (looking-at "[ \t]*$")
+		(LaTeX-newline)
+		(end-of-line 0)))
 	  (delete-blank-lines)
 	  (end-of-line 0))
 	;; if there is a caption or a label, move point upwards again
@@ -929,8 +940,9 @@
 	  (end-of-line 0)
 	  (indent-according-to-mode))))
 
-    (if (member environment '("table" "table*"))
-	(LaTeX-env-array "tabular"))))
+    (when (and (member environment '("table" "table*"))
+	       (not active-mark))
+      (LaTeX-env-array "tabular"))))
 
 (defun LaTeX-env-array (environment)
   "Insert ENVIRONMENT with position and column specifications.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.