all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Sebastian Urban <mrsebastianurban@gmail.com>
To: monnier@iro.umontreal.ca
Cc: 36358@debbugs.gnu.org
Subject: bug#36358: Indentation of not matched braces in latex-mode
Date: Thu, 9 Jul 2020 19:18:35 +0200	[thread overview]
Message-ID: <c9e64fc2-08de-0d4a-a4b6-65461d89c859@gmail.com> (raw)
In-Reply-To: <jwvmui0m7h5.fsf-monnier+emacs@gnu.org>

So, I tried to write something that could work better:
1st method: noindent, unless indent; basically current approach
             reversed: indent, unless noindent;
2nd method: no text before AND after - indent, otherwise don't.

Although, I'm not sure, what to do with things like:
    Some paragraph...

    {\small Quisque ullamcorper placerat ipsum.  Cras nibh.  Morbi
      vel justo vitae lacus tincidunt ultrices.  Lorem ipsum dolor sit
      amet, consectetuer adipiscing elit.}

    Other paragraph...
if it's not inside of a paragraph, should it be indented?

BTW I did not test them thoroughly...

=======================================
1. Check if command should be indented.
=======================================

By default it won't indent commands, unless they are in
latex-indent-commands.  Commands like section or TAlign, will have to
be added to that var.  This could be done for the most common
commands, like mentioned "section", by default.  This, basically,
reverses current approach, and I think that it'll be easier (less
commands typed into var) to tell what should be indented, than what
shouldn't.  But I didn't use LaTeX extensively, so I may be wrong.

--8<---------------cut here---------------start------------->8---
--- old/tex-mode.el	2020-06-26 18:34:05.000000000 +0200
+++ new/tex-mode.el	2020-07-09 11:22:15.246268900 +0200
@@ -2802,8 +2802,8 @@
    :group 'tex-file
    :version "27.1")

-(defcustom latex-noindent-commands '("emph" "footnote")
-  "Commands for which `tex-indent-basic' should not be used."
+(defcustom latex-indent-commands '("section")
+  "Commands for which `tex-indent-basic' should be used."
    :type '(repeat string)
    :safe (lambda (x) (not (memq nil (mapcar #'stringp x))))
    :group 'tex-file
@@ -2915,17 +2915,20 @@
  	     ;; We're the first element after a hanging brace.
  	     (goto-char up-list-pos)
  	     (+ (if (if (eq (char-after) ?\{)
-                        (save-excursion
-                          (skip-chars-backward " \t")
-                          (let ((end (point)))
-                            (skip-chars-backward "a-zA-Z")
-                            (and (eq (char-before) ?\\)
-                                 (member (buffer-substring (point) end)
-                                         latex-noindent-commands))))
+                        (unless (save-excursion
+				  (skip-chars-backward " \t")
+				  (if (eq (char-before) ?\])
+				      (backward-list))
+				  (let ((end (point)))
+                                    (skip-chars-backward "a-zA-Z")
+                                    (and (eq (char-before) ?\\)
+					 (member (buffer-substring (point) end)
+						 latex-indent-commands))))
+                          t)
                        (and (looking-at "\\\\begin *{\\([^\n}]+\\)")
-			 (member (match-string 1)
-				 latex-noindent-environments)))
-		    0 tex-indent-basic)
+                           (member (match-string 1)
+                                   latex-noindent-environments)))
+                    0 tex-indent-basic)
  		indent (latex-find-indent 'virtual))))
  	  ;; We're now at the "beginning" of a line.
  	  ((not (and (not virtual) (eq (char-after) ?\\)))
--8<---------------cut here---------------end--------------->8---


=======================================================
2. Check if command/declaration(?) is within paragraph.
=======================================================

This checks if there is nothing but "beginning of the line" (ignoring
whitespaces) before command or "{" AND nothing but "end of the line"
(ignoring whitespaces) after "}" - if "yes" then indent, otherwise
don't.

The good thing about this, is that we don't need
latex-noindent-commands.  The bad thing is, that constructs like:
    prefix: \TAlign{
      foo\\
      bar
    }
won't work, because of the "prefix:" at BOL - it'll think it's
a paragraph.

If, "not-in-paragraph" declarations, like:
    {\delaration Integer tempus convallis augue.  Etiam facilisis.
    Nunc elementum fermentum wisi.  Integer tempus convallis augue.}
should NOT be indented (like in example above), then:
- uncomment: (skip-chars-backward " \t");
- delete: (eq (char-after) ?\{) AND "or" from first condition of "and".

--8<---------------cut here---------------start------------->8---
--- old/tex-mode.el	2020-06-26 18:34:05.000000000 +0200
+++ new/tex-mode.el	2020-07-09 12:42:14.067428300 +0200
@@ -2802,13 +2802,6 @@
    :group 'tex-file
    :version "27.1")

-(defcustom latex-noindent-commands '("emph" "footnote")
-  "Commands for which `tex-indent-basic' should not be used."
-  :type '(repeat string)
-  :safe (lambda (x) (not (memq nil (mapcar #'stringp x))))
-  :group 'tex-file
-  :version "27.1")
-
  (defvar tex-latex-indent-syntax-table
    (let ((st (make-syntax-table tex-mode-syntax-table)))
      (modify-syntax-entry ?$ "." st)
@@ -2915,17 +2908,22 @@
  	     ;; We're the first element after a hanging brace.
  	     (goto-char up-list-pos)
  	     (+ (if (if (eq (char-after) ?\{)
-                        (save-excursion
-                          (skip-chars-backward " \t")
-                          (let ((end (point)))
-                            (skip-chars-backward "a-zA-Z")
-                            (and (eq (char-before) ?\\)
-                                 (member (buffer-substring (point) end)
-                                         latex-noindent-commands))))
+			(not (and (save-excursion
+				    ;; (skip-chars-backward " \t")
+				    (cond ((eq (char-before) ?\]) (backward-list)))
+				    (skip-chars-backward "a-zA-Z")
+				    (cond ((or (eq (char-before) ?\\)
+					       (eq (char-after) ?\{))
+					   (skip-chars-backward " \t\\\\")
+					   (bolp))))
+				  (save-excursion
+				    (forward-list)
+				    (skip-chars-forward " \t")
+				    (eolp))))
                        (and (looking-at "\\\\begin *{\\([^\n}]+\\)")
-			 (member (match-string 1)
-				 latex-noindent-environments)))
-		    0 tex-indent-basic)
+                           (member (match-string 1)
+                                   latex-noindent-environments)))
+                    0 tex-indent-basic)
  		indent (latex-find-indent 'virtual))))
  	  ;; We're now at the "beginning" of a line.
  	  ((not (and (not virtual) (eq (char-after) ?\\)))
--8<---------------cut here---------------end--------------->8---


=============================
? Indentation in itemize env.
=============================

I'm not sure how it suppose to look, but when in example below
    \begin{itemize}#
      \item foo*
      \item bar
    \end{itemize}
the cursor is where "#" is, and I press <RET> - cursor moves to the
next line and is auto-indented by the value of tex-indent-basic.  But
when the cursor is at the position of "*", and I press <TAB>, the
indentation is gone (zero).  The question is, should \item(s) be
indented or not?  If they should, maybe code below will help.

--8<---------------cut here---------------start------------->8---
--- old/tex-mode.el	2020-06-26 18:34:05.000000000 +0200
+++ new/tex-mode.el	2020-07-09 11:04:11.583564000 +0200
@@ -2880,7 +2880,13 @@
         ;; Default (maybe an argument)
         (let ((pos (point))
  	     ;; Outdent \item if necessary.
-	     (indent (if (looking-at tex-indent-item-re) (- tex-indent-item) 0))
+	     (indent (if (looking-at tex-indent-item-re)
+			 (if (save-excursion
+			       (forward-line -1)
+			       (beginning-of-line)
+			       (looking-at "\\\\begin *{\\([^\n}]+\\)"))
+			     0 (- tex-indent-item))
+		       0))
  	     up-list-pos)
  	 ;; Find the previous point which determines our current indentation.
  	 (condition-case err
--8<---------------cut here---------------end--------------->8---


Anyway, I hope it'll help... somehow.

S. U.





  reply	other threads:[~2020-07-09 17:18 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-24 16:08 bug#36358: Indentation of not matched braces in latex-mode Sebastian Urban
2019-06-24 23:22 ` Stefan Monnier
2019-06-25  7:30   ` Sebastian Urban
2019-06-25 14:53     ` Stefan Monnier
2019-06-26  7:46       ` Sebastian Urban
2019-06-26 13:49         ` Stefan Monnier
2019-06-27  8:53           ` Sebastian Urban
2019-06-29 20:08             ` Sebastian Urban
2019-06-29 21:02               ` Stefan Monnier
2020-07-09 17:18                 ` Sebastian Urban [this message]
2019-07-05 15:20               ` Basil L. Contovounesios

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=c9e64fc2-08de-0d4a-a4b6-65461d89c859@gmail.com \
    --to=mrsebastianurban@gmail.com \
    --cc=36358@debbugs.gnu.org \
    --cc=monnier@iro.umontreal.ca \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.