From f4dea76b27b6daee7b17d7f830fcc2d048cbf851 Mon Sep 17 00:00:00 2001 From: Dirk Ullrich 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