* Ralf Angeli (2008-05-02) writes:

> We could add a hook to be run at the end of `LaTeX-insert-environment'.
> Then beamer.el could add a function to the hook which determines the
> current environment with `LaTeX-current-environment', check for the
> optional argument if it is a frame environment and add it if not
> present.

Attached is a patch doing that if somebody wants to try.

The argument manipulation (checking for an existing argument list and
inserting the new argument) could probably be made into a generic
feature.

-- 
Ralf
Index: latex.el
===================================================================
RCS file: /cvsroot/auctex/auctex/latex.el,v
retrieving revision 5.441
diff -u -r5.441 latex.el
--- latex.el	3 Feb 2008 14:53:33 -0000	5.441
+++ latex.el	2 May 2008 21:00:10 -0000
@@ -612,12 +612,16 @@
 			 marker))
 	(move-marker marker nil)))))
 
+(defvar LaTeX-after-insert-env-hooks nil
+  "List of functions to be run at the end of `LaTeX-insert-environment'.
+Each function is called with three arguments: the name of the
+environment just inserted, the buffer position just before
+\\begin and the position just before \\end.")
+
 (defun LaTeX-insert-environment (environment &optional extra)
   "Insert LaTeX ENVIRONMENT with optional argument EXTRA."
   (let ((active-mark (and (TeX-active-mark) (not (eq (mark) (point)))))
-	(macrocode-p (and (eq major-mode 'doctex-mode)
-			  (string-match "\\`macrocode\\*?\\'" environment)))
-	prefix content-start)
+	prefix content-start env-start env-end)
     (when (and active-mark (< (mark) (point))) (exchange-point-and-mark))
     ;; Compute the prefix.
     (when (and LaTeX-insert-into-comments (TeX-in-commented-line))
@@ -663,7 +667,8 @@
 	       (newline)
 	       (when prefix (insert prefix))))))
     ;; Now insert the environment.
-    (if macrocode-p (insert "%") (when prefix (insert prefix)))
+    (when prefix (insert prefix))
+    (setq env-start (point))
     (insert TeX-esc "begin" TeX-grop environment TeX-grcl)
     (indent-according-to-mode)
     (when extra (insert extra))
@@ -673,7 +678,8 @@
       (when prefix (insert prefix))
       (newline))
     (when active-mark (goto-char (mark)))
-    (if macrocode-p (insert "%") (when prefix (insert prefix)))
+    (when prefix (insert prefix))
+    (setq env-end (point))
     (insert TeX-esc "end" TeX-grop environment TeX-grcl)
     (end-of-line 0)
     (if active-mark
@@ -682,8 +688,10 @@
 	      (LaTeX-fill-region content-start (line-beginning-position 2)))
 	  (set-mark content-start))
       (indent-according-to-mode))
-    (save-excursion (beginning-of-line 2) (indent-according-to-mode)))
-  (TeX-math-input-method-off))
+    (save-excursion (beginning-of-line 2) (indent-according-to-mode))
+    (TeX-math-input-method-off)
+    (run-hook-with-args 'LaTeX-after-insert-env-hooks
+			environment env-start env-end)))
 
 (defun LaTeX-modify-environment (environment)
   "Modify current ENVIRONMENT."
Index: style/beamer.el
===================================================================
RCS file: /cvsroot/auctex/auctex/style/beamer.el,v
retrieving revision 1.14
diff -u -r1.14 beamer.el
--- style/beamer.el	3 Feb 2008 15:19:05 -0000	1.14
+++ style/beamer.el	2 May 2008 21:00:25 -0000
@@ -29,9 +29,31 @@
 
 ;;; Code:
 
+(defun LaTeX-beamer-after-insert-env (env start end)
+  "Do beamer-specific stuff after the insertion of an environment."
+  (when (and (TeX-member env LaTeX-verbatim-environments 'string-equal)
+	     (save-excursion
+	       (goto-char start)
+	       (string-equal (LaTeX-current-environment) "frame")))
+    (save-excursion
+      (when (re-search-backward "\\\\begin[ \t]*{frame}" nil t)
+	(let ((end-of-begin (match-end 0)))
+	  (goto-char end-of-begin)
+	  (while (forward-comment 1))
+	  (if (eq (char-after) (string-to-char LaTeX-optop))
+	      (progn
+		(forward-char)
+		(insert "fragile")
+		(unless (looking-at (concat "[ \t]*" LaTeX-optcl))
+		  (insert ",")))
+	    (goto-char end-of-begin)
+	    (insert "[fragile]")))))))
+
 (TeX-add-style-hook
  "beamer"
  (lambda ()
+   (add-hook 'LaTeX-after-insert-env-hooks 'LaTeX-beamer-after-insert-env nil t)
+
    (unless LaTeX-beamer-section-labels-flag
      (make-local-variable 'LaTeX-section-hook)
      (setq LaTeX-section-hook
Index: style/doc.el
===================================================================
RCS file: /cvsroot/auctex/auctex/style/doc.el,v
retrieving revision 1.8
diff -u -r1.8 doc.el
--- style/doc.el	3 Feb 2008 14:53:30 -0000	1.8
+++ style/doc.el	2 May 2008 21:00:25 -0000
@@ -40,6 +40,21 @@
       (delete-region (line-beginning-position) (line-end-position))
       (indent-according-to-mode))))
 
+(defun LaTeX-doc-after-insert-macrocode (env start end)
+  "Make sure the macrocode environment is properly formatted after insertion."
+  (when (TeX-member env '("macrocode" "macrocode*") 'string-equal)
+    (save-excursion
+      (goto-char end)
+      (skip-chars-backward " \t")
+      (when (bolp)
+	(insert "%")
+	(indent-according-to-mode))
+      (goto-char start)
+      (skip-chars-backward " \t")
+      (when (bolp)
+	(insert "%")
+	(indent-according-to-mode)))))
+
 (TeX-add-style-hook
  "doc"
  (function
@@ -48,6 +63,8 @@
 		 '("macrocode" current-indentation))
     (add-to-list 'LaTeX-indent-environment-list
 		 '("macrocode*" current-indentation))
+    (add-hook 'LaTeX-after-insert-env-hooks 'LaTeX-doc-after-insert-macrocode
+	      nil t)
     (LaTeX-add-environments
      "theglossary"
      '("macrocode" LaTeX-env-no-comment)
_______________________________________________
auctex-devel mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/auctex-devel

Reply via email to