1

I'm having a lot of trouble getting sml-mode in Emacs to sanely indent my code. For example, here is a block where it is behaving particularly strangely:

    datatype type_node
      = Param of TyParam.t
      | LongId of LongId.t
      | Offset of field * field list option
      | List of type_t list
      | Fun of type_t list * t list
      | Any
      | VProc
      | Cont of TyArg.t list option
      | Addr of type_t
         and dataconsdef_node
             = ConsDef of BomId.t * type_t option
         and field_node
             = Immutable of int * type_t
             | Mutable of int * type_t
         and fundef_node
             = Def of Attrs.t option * BomId.t * TyParam.t list option
                      * Param.t list option * Param.t list option * type_t * exp_t
         and varpat_node
             = Wild
             | Var of BomId.t * type_t option
         and caserule_node
             = LongRule of LongId.t * varpat_t list * exp_t
           | LiteralRule Literal.t * exp_t
           | DefaultRule of varpat_t * exp_t
         and tycaserule_node
             = TyRule of type_t * exp_t
             | Default of exp_t
         and simpleexp_node
             = PrimOp of 'var Prim.prim * simpleexp_t list
             | AllocId of LongId.t * simpleexp_t list
             | AllocType of Type.t * simpleexp_t list
             | AtIndex of int * simpleexp_t * simpleexp_t option
             | TypeCast of Type.t * simpleexp_t
             | HostVproc
             | VpLoad of int * simpleexp_t
             | VpAddr of int * simpleexp_t
             | VpStore of int * simpleexp_t * t
             | Id of LongId.t
             | Lit of Literal.t
             | MLString of string
         and exp_node
             = Let of VarPat.t list * rhs * t
             | Do of SimpleExp.t * t
             | Fun of FunDef.t list * t
             | Cont of BomId.t * Param.t list option * t * t
             | If of SimpleExp.t * t * t
             | Case of SimpleExp.t * CaseRule.t list
                     | Typecase of TyParam.t * TyCaseRule.t list
                                 | Apply of LongId.t * SimpleExp.t list option * SimpleExp.t list option
                                          | Throw of LongId.t * SimpleExp.t list option
                                                   | Return of SimpleExp.t list option
  and foo_node
          = bar

Apologies for the huge code dump, but the content doesn't matter -- just the extremely inconsistent indentation. Attempting to define signatures inside of a structure Foo : sig ... end = struct ... end block is even worse:

structure Attrs : sig
              type t
              datatype node
                = Attributes of string list
                                       include WRAPPED
                                       sharing type node' = node
                                                                sharing type obj = t
          end

or:

functor DoWrap(type node) : sig
            type t = node Wrap.t
                          include WRAPPED
                          sharing type node' = node
                                                   sharing type obj = t
        end = struct
                            open Wrap
                            type t = node Wrap.t
                            type node' = node
                            type obj = t
                            end

I'm a diehard Emacs fan and I would hate to have to avoid it on this project, since it's one that I'll be working on the the next few months. However, I can't turn code like this in to my supervisor. The above is the behavior that I get when I select a block of text and indent-region.

I've looked at this answer, but the solution doesn't apply, since this is the behavior M-C-\ gives me, and it would be a huge pain to work on code without being able to auto-indent the whole file.

Is there an easy fix for this? Are there alternative sml-mode implementations?

6
  • 1
    Looks like a bug. Please report it (to me).
    – Stefan
    Jun 3, 2014 at 3:20
  • @Stefan what information do you want in a bug report, and where should I send it? Jun 3, 2014 at 3:21
  • Just kidding. The code you quoted is all I need. [ Parsing SML's functor sigs is a pain in the rear. ]
    – Stefan
    Jun 3, 2014 at 3:37
  • 1
    But if you send me an email I can send you patches to test.
    – Stefan
    Jun 3, 2014 at 3:53
  • The code in git.savannah.gnu.org/cgit/emacs/elpa.git/tree/packages/sml-mode/… should behave better w.r.t functors and signatures. Haven't looked at the datatype issue yet.
    – Stefan
    Jun 4, 2014 at 14:08

1 Answer 1

2

Presuming that you are using the latest version of SML Mode from GNU ELPA, it looks like you have hit a corner-case in which the built-in SMIE grammar of SML Mode breaks down. I had similar issues with Tuareg Mode for OCaml.

You likely need to downgrade your version of SML Mode to version 4.0 or lower, which can obtain from http://www.iro.umontreal.ca/~monnier/elisp/. These versions do not yet use SMIE for indentation, but some ad-hoc indentation code. At least for OCaml and Tuareg, I found this old-fashioned approach to yield much better and much more reliable indentation.

You may want to report this issue to the maintainer of SML Mode, though, whose mail address you can find in the header of SML Mode, or in the footer of the aforementioned website.

2
  • Thanks for the praise of SMIE ;-) I don't know of separate "Tuareg SMIE" and "Ocaml SMIE" support, so your bad experience is just with one case (presumably Tuareg's SMIE code). And this one case is compared to Tuareg's "manual" indentation which has been tuned for many more years. So it's not very fair. Please do report the problems you seen with Tuareg's SMIE code so we can bring it up to par.
    – Stefan
    Jun 3, 2014 at 3:27
  • I mean no offence, but I do not care whether I'm "fair". While I like SMIE, it's a fact, that the old indentation code of Tuareg works, while SMIE breaks in many cases. I did report some of these issues to Tuaregs Github tracker, but there was zero reaction, so I lost interest, and disabled SMIE in Tuareg instead.
    – user355252
    Jun 3, 2014 at 6:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.