emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Indentation in the METAPOST mode.


From: Michaël Cadilhac
Subject: Indentation in the METAPOST mode.
Date: Thu, 09 Aug 2007 03:00:39 +0200
User-agent: Gnus/5.110007 (No Gnus v0.7) Emacs/22.1.50 (gnu/linux)

Hi guys, long time no see !

Indentation in the METAPOST mode has, IMO, two major flaws :

1. A bug. If the buffer starts with beginfig, the contents of the figure
is not properly indented (it stays on the first column).

2. When wrapping an expression on several lines, the indentation is the
same for each line, this is bad style : I'd prefer

beginfig(1)
  draw (0,0)--(1,2)--(10,3)--
    (0,1)--(10,3);
  draw (2,2);
endfig

I propose the following patch which considers that keywords like
beginfig does not need ending semicolon (it's /quite/ the case).

Tell me if it's worth installing it.

Index: lisp/progmodes/meta-mode.el
===================================================================
RCS file: /sources/emacs/emacs/lisp/progmodes/meta-mode.el,v
retrieving revision 1.19
diff -c -r1.19 meta-mode.el
*** lisp/progmodes/meta-mode.el 26 Jul 2007 05:27:28 -0000      1.19
--- lisp/progmodes/meta-mode.el 9 Aug 2007 00:51:47 -0000
***************
*** 620,625 ****
--- 620,628 ----
       ((and meta-ignore-comment-regexp
             (looking-at meta-ignore-comment-regexp))
        (current-indentation))
+      ;; Beginning of buffer.
+      ((eq (point-at-bol) (point-min))
+       0)
       ;; Backindent at end of environments.
       ((looking-at
         (concat "\\<" meta-end-environment-regexp "\\>"))
***************
*** 631,661 ****
       (t (meta-indent-calculate-last)))))
  
  (defun meta-indent-calculate-last ()
!   "Return the indentation of previous line of Metafont or MetaPost source."
    (save-restriction
      (widen)
      (skip-chars-backward "\n\t ")
!     (move-to-column (current-indentation))
!     ;; Ignore comments.
!     (while (and (looking-at comment-start) (not (bobp)))
!       (skip-chars-backward "\n\t ")
!       (if (not (bobp))
!           (move-to-column (current-indentation))))
!     (cond
!      ((bobp) 0)
!      (t (+ (current-indentation)
!            (meta-indent-level-count)
!            (cond
!             ;; Compensate for backindent at end of environments.
!             ((looking-at
!               (concat "\\<"meta-end-environment-regexp "\\>"))
!              meta-indent-level)
!             ;; Compensate for backindent within environments.
!             ((looking-at
!               (concat "\\<" meta-within-environment-regexp "\\>"))
!              meta-indent-level)
!             (t 0)))))
!     ))
  
  (defun meta-indent-level-count ()
    "Count indentation change for begin-end commands in the current line."
--- 634,700 ----
       (t (meta-indent-calculate-last)))))
  
  (defun meta-indent-calculate-last ()
!   "Return the indentation of previous line of Metafont or MetaPost source.
! If the current line is a continuation, i.e. the previous line doesn't end
! up with a semicolon, add some more indentation."
    (save-restriction
      (widen)
+     (meta-indent-previous-line)
+     (+ (current-indentation)
+        (meta-indent-level-count)
+        (let ((unfinished (meta-indent-unfinished-line-p)))
+        (cond ((eq unfinished t) meta-indent-level)
+              ((and (not unfinished)
+                    (save-excursion
+                      (meta-indent-previous-line)
+                      (meta-indent-unfinished-line-p)))
+               ;; Compensate a previous continuation indentation.
+               (- meta-indent-level))
+              (t 0)))
+        (cond
+       ;; Compensate for backindent at end of environments.
+       ((looking-at
+         (concat "\\<" meta-end-environment-regexp "\\>"))
+        meta-indent-level)
+       ;; Compensate for backindent within environments.
+       ((looking-at
+         (concat "\\<" meta-within-environment-regexp "\\>"))
+        meta-indent-level)
+       (t 0)))))
+ 
+ (defun meta-indent-previous-line ()
+   "Go to the previous line of code, skipping comments."
+   (skip-chars-backward "\n\t ")
+   (move-to-column (current-indentation))
+   ;; Ignore comments.
+   (while (and (looking-at comment-start) (not (bobp)))
      (skip-chars-backward "\n\t ")
!     (if (not (bobp))
!       (move-to-column (current-indentation)))))
! 
! (defun meta-indent-unfinished-line-p ()
!   "Tell if the current line of code ends with an unfinished expression.
! Return t if the line is the first line of the unfinished expression.
! Return 'continued if the line is a continuation.
! Return nil if the line is a finished one."
!   (save-excursion
!     (end-of-line)
!     (if (search-backward ";" (point-at-bol) t)
!       (forward-char)
!       (beginning-of-line))
!     ;; See if the last statement of the line is environment-related,
!     ;; or exists at all.
!     (if (looking-at (concat "[ \t]*\\($\\|" (regexp-quote comment-start)
!                           "\\|\\<" meta-end-environment-regexp "\\>"
!                           "\\|\\<" meta-begin-environment-regexp "\\>"
!                           "\\|\\<" meta-within-environment-regexp "\\>\\)"))
!       nil
!       (if (or (bobp) (/= (point-at-bol) (point)))
!         t
!       ;; Otherwise, if there was no semicolon on the line, see if
!       ;; the previous line was a continuation.
!       (meta-indent-previous-line)
!       (if (meta-indent-unfinished-line-p) 'continued t)))))
  
  (defun meta-indent-level-count ()
    "Count indentation change for begin-end commands in the current line."
Index: lisp/ChangeLog
===================================================================
RCS file: /sources/emacs/emacs/lisp/ChangeLog,v
retrieving revision 1.11534
diff -C 0 -r1.11534 ChangeLog
*** lisp/ChangeLog      8 Aug 2007 16:38:31 -0000       1.11534
--- lisp/ChangeLog      9 Aug 2007 00:52:04 -0000
***************
*** 0 ****
--- 1,12 ----
+ 2007-08-09  Michaël Cadilhac  <address@hidden>
+ 
+       * progmodes/meta-mode.el (meta-indent-calculate): Force
+       indentation at bop to 0.
+       (meta-indent-previous-line): New.  Go to the previous line of code
+       by skipping comments backward.
+       (meta-indent-unfinished-line-p): New.  Tell if the current line is
+       unfinished, i.e. contains an expression that will be ended in a
+       future line.
+       (meta-indent-calculate-last): Use it to add some more indentation
+       when continuing a line.
+ 
-- 
 |   Michaël `Micha' Cadilhac       |    Le copillage-collage                |
 |   http://michael.cadilhac.name   |       tue le programmeur.              |
 |   JID/MSN:                       |           -- Dictons LRDE              |
 `----  address@hidden  |                                   -  --'

Attachment: pgpch7ApFOwSD.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]