haskell repl in emacs

前端 未结 1 1627
南方客
南方客 2020-12-25 09:14

Hi i am starting with haskell and trying to set up my emacs for its development.

I have haskell-mod and ghc-mod latest in emacs 24.3.

相关标签:
1条回答
  • 2020-12-25 10:10

    UPDATE 2017

    now that we have Intero I would suggest you try Intero/Stack - it works rather well.

    With it my config is slimmed even more down:

    ;; ----------------------------------------------------------------------
    ;; HASKELL
    (require 'haskell-mode)
    (require 'intero)
    (add-hook 'haskell-mode-hook 'intero-mode)
    (require 'flycheck)
    (setq flycheck-check-syntax-automatically '(save new-line))
    (flycheck-add-next-checker 'intero '(warning . haskell-hlint))
    

    Concerning the REPL you basically just load the file and then C-c C-l into the repl (you can always switch between the two windows with C-c C-z and you can clear out the repl-buffer with C-c C-k (when inside).

    The only drawback is that Intero is usually installed locally in your project, so the first startup into a new project will take a while for Intero to download/compile/boot-up - but it's no big deal and you gain much.


    UPDATE

    Now that stack is out and running great I would recommend setting the haskell-process-type to auto and maybe installing ghc-mod using stack.

    here is the current setup I am using:

    (require 'haskell-mode)
    (add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
    (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
    (add-hook 'haskell-mode-hook 'linum-mode)
    (add-to-list 'exec-path "~/.local/bin")
    (eval-after-load 'haskell-mode '(progn
      (define-key haskell-mode-map (kbd "C-c C-l") 'haskell-process-load-file)
      (define-key haskell-mode-map (kbd "C-c C-n C-t") 'haskell-process-do-type)
      (define-key haskell-mode-map (kbd "C-c C-n C-i") 'haskell-process-do-info)
      (define-key haskell-mode-map "\C-ch" 'haskell-hoogle)))
    (eval-after-load 'haskell-cabal '(progn
      (define-key haskell-cabal-mode-map (kbd "C-c C-k") 'haskell-interactive-ode-clear)
      (define-key haskell-cabal-mode-map (kbd "C-c C-c") 'haskell-process-cabal-build)
      (define-key haskell-cabal-mode-map (kbd "C-c c") 'haskell-process-cabal)))
    
    (require 'company-ghci)
    (push 'company-ghci company-backends)
    (add-hook 'haskell-mode-hook 'company-mode)
    (add-hook 'haskell-interactive-mode-hook 'company-mode)
    

    As you can see I basically got rid of all ghc-mod related stuff (stack works as is with the current haskell-mode) and replaced the company backend (although this one is really slow and I hope to find something better)

    variables

    Theses you usually set within Emacs/Configuration of Haskell-Mode

    (custom-set-variables
     ;; there is of course more in here - these are the Haskell related:
     '(haskell-font-lock-symbols (quote unicode))
     '(haskell-hoogle-command nil)
     '(haskell-mode-hook
       (quote
        (linum-mode turn-on-haskell-indentation turn-on-haskell-doc-mode)) t)
     '(haskell-process-auto-import-loaded-modules t)
     '(haskell-process-load-or-reload-prompt t)
     '(haskell-process-log t)
     '(haskell-process-suggest-language-pragmas nil)
     '(haskell-process-suggest-no-warn-orphans t)
     '(haskell-process-type (quote auto))
     '(haskell-process-use-presentation-mode t)
     '(haskell-tags-on-save t)
     '(inferior-haskell-wait-and-jump t)
     '(safe-local-variable-values
       (quote
        ((haskell-process-use-ghci . t)
         (haskell-indent-spaces . 4))))
     (uniquify)))
    

    These are the packages I added:

    • haskell-mode
    • company (not Haskell-related as is but a dependency)
    • company-cabal
    • company-ghci
    • ghci-completion
    • shakespeare-mode (for Yesod)
    • company-quickhelp (for nice display of :i)

    First

    if it asks for your cabal directory - it wants to know where your myProject.cabal file is - if you don't have one just take the folder where your file is (the default - I think the default/find never failed me till now).

    in case you need some sample-.emacs-setup

    Here are parts from my .emacs file that works for me:

    (autoload 'ghc-init "ghc" nil t)
    (autoload 'ghc-debug "ghc" nil t)
    (add-hook 'haskell-mode-hook (lambda () (ghc-init)))
    (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
    
    (eval-after-load 'haskell-mode '(progn
      (define-key haskell-mode-map (kbd "C-c C-l") 'haskell-process-load-or-reload)
      (define-key haskell-mode-map (kbd "C-`") 'haskell-interactive-bring)
      (define-key haskell-mode-map (kbd "C-c C-n C-t") 'haskell-process-do-type)
      (define-key haskell-mode-map (kbd "C-c C-n C-i") 'haskell-process-do-info)
      (define-key haskell-mode-map (kbd "C-c C-n C-c") 'haskell-process-cabal-build)
      (define-key haskell-mode-map (kbd "C-c C-n c") 'haskell-process-cabal)))
    (eval-after-load 'haskell-cabal '(progn
      (define-key haskell-cabal-mode-map (kbd "C-`") 'haskell-interactive-bring)
      (define-key haskell-cabal-mode-map (kbd "C-c C-k") 'haskell-interactive-ode-clear)
      (define-key haskell-cabal-mode-map (kbd "C-c C-c") 'haskell-process-cabal-build)
      (define-key haskell-cabal-mode-map (kbd "C-c c") 'haskell-process-cabal)))
    
    (custom-set-variables
     '(haskell-interactive-mode-hide-multi-line-errors nil)
     '(haskell-process-log t)
     '(haskell-process-type (quote cabal-repl)))
    

    Please note the part with (quote cabal-repl) - this should work with cabal sandboxes. Also this will add more logging information to a buffer named haskell-process-log where you might find more about your problem.

    I use this with ghc-mod version 5.1.0.2 compiled by GHC 7.8.3 and GHC 7.8.3 obviously.

    Of course you have to make sure that your .cabal folder and the place where ghc-mod is in your path (I think you can configure this somewhere in the emacs settings too - but it's much easier this way).

    remarks

    • you have to install the package ghc in emacs not ghc-mod.
    • I had problems with different version of ghc-mod and the emacs package before - so make sure that you get the latest with cabal-install and then reinstall the emacs-package (was only problems with parsing of the ghc-output on my part though)

    further information

    If this does not help you please feel free to

    • add a comment
    • have a look at the happy haskell programming page
    • have a look at the ghc wiki on emacs

    sample run on my machine

    I edited a sample file like this:

    Helo Emacs

    Then I hit C-c C-l and press y:

    Step 2

    And accept the folder (this is where the file is located) and any other questions with Ret - now you should see the *haskell* buffer with a friendly message/lambda:

    Step 3

    Finally I can check that the file got loaded:

    enter image description here

    0 讨论(0)
提交回复
热议问题