1

NB: This problem seems to be limited to mma-mode (for Mathematica).

For example, suppose I define a snippet like this:

# key: test
# name: test
# --
$1
$1

Then, I test it (with C-c C-t). After I type the first character (x), my terminal looks like this (the indicates the position of the cursor).

x▮
  x

Note that a couple of spurious spaces were added in front of the mirrored x.

This behavior continues with every additional keystroke, as long as mirroring is taking place:

xy▮
    xy

It happens even if the keystroke is for a backspace:

x▮
      x

As I said at the beginning, I see this problem only with mma-mode.

Now, mma-mode is not maintained, AFAICT. Therefore, the most I can hope for is some workaround.


Any ideas on how I could prevent the addition of spurious spaces before mirrors?

  • 1
    Does setting yas-indent-line to fixed help? If you have those 2 lines in a .m file, does M-x indent-according-to-mode on the second line also cause indentation? – npostavs Apr 3 '17 at 17:51
  • @npostavs: indent-according-to-mode does give a clue: if I run it on an empty buffer under mma-mode, two spaces get inserted. Unfortunately, however, if I add the line # expand-env: ((yas-indent-line 'fixed)) to the snippet's header, I still get the behavior I showed before. – kjo Apr 3 '17 at 18:08
  • 1
    What yasnippet version are you using? I think older versions wouldn't apply expand-env at the right time (see github.com/joaotavora/yasnippet/pull/743). Setting yas-indent-line in mma-mode-hook should be okay. – npostavs Apr 3 '17 at 18:31
  • 1
    Actually these comments near the top of mma-mode.el give a hint, "If point is not on the leading whitespace of the current line, indents by steps of `mma-indentation'" - yasnippet calls the indent function with point sitting just after the leading whitespace. Perhaps replacing (back-to-indentation) with (beginning-of-line) in yas--indent-region would help. – npostavs Apr 3 '17 at 21:16
  • @npostavs: Thanks for your suggestions. I'm using version 0.11.0 of yasnippet. In any case, setting yas-indent-line in mma-mode-hook did not help. I don't entirely understand you last suggestion. Do you mean that I should redefine yas--indent-region? If so, is there a way to do this "buffer-locally", for mma-mode buffers only? – kjo Apr 4 '17 at 10:31
1

mma-mode seems to have a strange (non-idempotent) indentation function. Therefore you should stop yasnippet from trying to use it when in mma-mode:

(defun my-mma-mode-hook ()
  (setq-local yas-indent-line 'fixed))
(add-hook 'mma-mode-hook #'my-mma-mode-hook)

Unfortunately, this doesn't work in yasnippet versions 0.11 and earlier because it doesn't respect yas-indent-line for mirror updates. This will be fixed in the upcoming 0.12, but you can use this advice to fix it in the meantime:

;; NOTE: this also disables `$>' indent markers in snippets,
;; but hardly anyone uses those anyway.
(defun yas-indent-line-is-auto-p (&rest _)
  (eq yas-indent-line 'auto))
(advice-add 'yas--indent-region :before-while #'yas-indent-line-is-auto-p)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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