4

When using haskell-mode (the Haskell layer) with evil-mode keybindings in spacemacs, the indentation created by pressing the o key is often incorrect.

For instance, imagine I want to write the following Haskell file:

module Foo where

foo :: Int
foo = 3

However, I've only written the following. I haven't yet created the function body for the foo function:

module Foo where

foo :: Int

If I am in insert mode and have my cursor after the "t" in Int and press Enter, the cursor goes to the next line in the first column, directly under the "f" in "foo". Using an underscore (_) for the cursor position, it looks like this:

module Foo where

foo :: Int
_

This is correct.

However, if I am in normal mode and have my cursor anywhere in the line with "foo" and press o, the cursor goes to then next line in the third column. It looks like this:

module Foo where

foo :: Int
  _

This is incorrect. I have to press Backspace twice to put the cursor in the first column before I can start writing the definition of foo.

It seems like indentation rules are getting messed up. Why is the indentation different depending on whether Enter is pressed in insert mode, or o is pressed in normal mode?

How can I get o to indent the same as Enter in insert mode?


Here is my current .spacemacs config file. To my knowledge, I don't have any settings set that effect indentation.

edit: Just so my intentions are clear, I do want indentation to be happening when I press either Enter or o. But I want it to be the appropriate indentation. Currently, pressing o results in incorrect indentation.

illabout
  • 285
  • 2
  • 14
  • Evil's `o` key inserts a newline and reindents, so the reason your `Enter` key doesn't behave the same way is either because you've disabled auto-indent or Spacemacs does weird things. I'm personally leaning towards the latter. Repro this without Spacemacs, then we'll talk. – wasamasa Sep 11 '17 at 07:05
  • 1
    @wasamasa What is it that you want to talk about? I'm a beginner with Spacemacs (and Emacs in general), and I'm not sure how to go about trying to reproduce this without Spacemacs. However, if someone sees this question and is already using haskell-mode with evil-mode, I'd be very interested in hearing whether the problem persists. I imagine you are probably right that it is a problem with Spacemacs. In this question, I'm asking about how I can fix this problem. – illabout Sep 11 '17 at 08:00
  • Why not running describe-key on your `Enter` and `o` keys, and seeing what commands are being run? In Spacemacs that would be `SPC h k Enter` and `SPC h k o`. You can then modify `o` so that it runs the same function as `Enter`. I don't know why you wouldn't want your code indented, though. – Daniel Sep 11 '17 at 10:29
  • 1
    @dangom, sorry for the confusion about indentation. I *do* want my code indented when it is appropriate to do so. However, in my example, indenting two spaces after the type of `foo` is not appropriate. I've updated the question to make this more clear. – illabout Sep 11 '17 at 10:59
  • @dangom, I'm not sure about how evil-mode works, but I'm guessing that if I modified `o` to run the same command as `Enter`, then I would no longer be able to hit `o` to add a new line below and go into insert mode. Instead it would work like `Enter` where it just moves the cursor down one line and stays in normal mode. – illabout Sep 11 '17 at 11:01
  • Yp, that makes sense. How about assigning `o` to something like a `(lambda () (progn (original-function-assigned-to-o) (beginning-of-line)))` – Daniel Sep 11 '17 at 11:35
  • @dangom Thanks for the suggestion. However, I don't want the cursor to go to the beginning of the line necessarily, but instead to go the correct indentation position. The correct indentation position may not be at the very beginning of the line. If I look at the difference between `evil-ret` and `evil-open-below`, I may be able to figure out what command I need to call to make `evil-open-below` work like `evil-ret`. Or, at the very least, what configuration from spacemacs is making it not work correctly. – illabout Sep 11 '17 at 12:08
  • 1
    Try opening an issue at the Spacemacs GitHub issue tracker. – Daniel Sep 11 '17 at 12:24
  • @dangom Good idea. I've posted about this on the Spacemacs issue tracker here: https://github.com/syl20bnr/spacemacs/issues/9577 – illabout Sep 12 '17 at 16:45

1 Answers1

3

This appears to be a problem with the interaction between evil and haskell-mode.

Here are some relavent issues on the spacemacs and haskell-mode bug trackers:

Per a comment on the issue in the haskell-mode repository, here is a temporary fix for the "o" key. Put the following code into your ~/.spacemacs file:

(defun dotspacemacs/user-config ()
 (with-eval-after-load "haskell-mode"
    ;; This changes the evil "O" and "o" keys for haskell-mode to make sure that
    ;; indentation is done correctly. See
    ;; https://github.com/haskell/haskell-mode/issues/1265#issuecomment-252492026.
    (defun haskell-evil-open-above ()
      (interactive)
      (evil-digit-argument-or-evil-beginning-of-line)
      (haskell-indentation-newline-and-indent)
      (evil-previous-line)
      (haskell-indentation-indent-line)
      (evil-append-line nil))

    (defun haskell-evil-open-below ()
      (interactive)
      (evil-append-line nil)
      (haskell-indentation-newline-and-indent))

    (evil-define-key 'normal haskell-mode-map
      "o" 'haskell-evil-open-below
      "O" 'haskell-evil-open-above)
  )
)
illabout
  • 285
  • 2
  • 14