Hello,

by accident I have sent this message to the "auctex" mailing list first.
Therefore I repost it here. Sorry for the noise.

Currently `LaTeX-environment' uses a simple heuristics to determine
the default ennvironment to insert: If there is only whitespace
between (bop) and point the default environment is "document".
otherwise the value of `‘LaTeX-default-environment' is used as default
environment.

I have tried to make this decision more robust such that "document" is
also choosen as default environment if there is a mix of
whitespace-only and comment-only lines before point.

The Git patch appended is against "master". I've tried my best to
adhere to the (seemingly) commit message style used in the AUCTeX
project.

Dirk
From f4dea76b27b6daee7b17d7f830fcc2d048cbf851 Mon Sep 17 00:00:00 2001
From: Dirk Ullrich <dirk.ullr...@gmail.com>
Date: Wed, 18 May 2016 14:39:57 +0200
Subject: [PATCH] More robust choosing of default LaTeX environment

* latex.el (LaTeX-environment): Suggest for a LaTeX master file
  "document" if the file contains only comments and whitespace
  between begin of buffer and point.

* tex.el (TeX-in-whitespace-line): new function to detect
  whitespace-only lines.
  (TeX-near-bobp): Add optional argument to not only skip
  whitespace but comment lines, too.
---
 latex.el |  2 +-
 tex.el   | 17 ++++++++++++++---
 2 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/latex.el b/latex.el
index 16d6760..974e9af 100644
--- a/latex.el
+++ b/latex.el
@@ -557,7 +557,7 @@ It may be customized with the following variables:
 
   (interactive "*P")
   (let* ((default (cond
-		   ((TeX-near-bobp) "document")
+		   ((TeX-near-bobp t) "document")
 		   ((and LaTeX-default-document-environment
 			 (string-equal (LaTeX-current-environment) "document"))
 		    LaTeX-default-document-environment)
diff --git a/tex.el b/tex.el
index c946851..7dce548 100644
--- a/tex.el
+++ b/tex.el
@@ -4678,10 +4678,21 @@ to look backward for."
    (buffer-file-name)
    (TeX-master-directory)))
 
-(defun TeX-near-bobp ()
-  "Return t iff there's nothing but whitespace between (bob) and (point)."
+(defun TeX-in-whitespace-line ()
+  "Return a non-nil if point is in a line consisting only of whitespace."
+  (forward-line 0)
+  (skip-chars-forward " \t")
+  (eolp))
+
+(defun TeX-near-bobp (&optional ignore-comments)
+  "Return t iff there's nothing but whitespace between (bob) and (point).
+For a non-nil IGNORE-COMMENTS comment lines will treated in the same way."
   (save-excursion
-    (skip-chars-backward " \t\n")
+    (while (and (or (TeX-in-whitespace-line)
+                    (and ignore-comments (TeX-in-commented-line)))
+                (not (bobp)))
+      (forward-line -1))
+    (forward-line 0)
     (bobp)))
 
 (defun TeX-deactivate-mark ()
-- 
2.8.0.GIT

_______________________________________________
auctex-devel mailing list
auctex-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/auctex-devel

Reply via email to