LoginSignup
8
9

More than 5 years have passed since last update.

EmacsでHaskellプログラミングするための環境構築手順(haskell-mode.elの導入)

Last updated at Posted at 2013-11-12

emacs も haskell もまったくの初心者なのですが
とある音系のソフトウェアのフロントエンドとして使うために環境構築してみました。

今回は、emacs 上で haskell のコードを実行するための環境の構築方法を、
簡単な(超ざっくりな)説明を加えながら記したいとおもいます。

概要


  1. MacPorts のインストール
  2. ghc の導入
  3. emacs と haskell-mode.el の導入
  4. emacs を起動してみる
  5. マクロの設定

詳細手順


1. MacPorts のインストール

MacPorts は Mac OS X 向けのパッケージ管理ツールです。
インストールされてない場合は、配布元からパッケージをダウンロードしてインストールしておきます。
http://www.macports.org

ちなみに MacPorts は、Xcode の "Command Line Tools" に多く依存しているので、
これも入ってなかったらあわせて導入しておきましょう。(以後の手順ではXcodeのバージョンは特に問いません)

2. ghc の導入

sudo port install ghc

ghc は "Glasgow Haskell Compiler" の略で、Haskell というプログラミング言語のコンパイラのひとつだそう。ここでは深入りせず、サクッと導入だけして先にすすみます。Haskell をためしてみたい人は、以下のコマンドでインタプリタも落ちてくるので、ghci と打ってみるとよいんじゃないかな。

$ port list ghc
ghc                            @7.6.3          lang/ghc
$ sudo port install ghc
# ・・長いので結果は省略・・

# インストール結果確認
$ which ghc
/opt/local/bin/ghc
$ which ghci
/opt/local/bin/ghci

3. emacs と haskell-mode.el の導入

sudo port install emacs haskell-mode.el

emacs はユーザがマクロを書いてガンガンに機能拡張できることで有名なエディタ/コード編集・実行環境。haskell-mode とはその emacs 上で Haskell プログラミングを快適化するマクロ群らしいです。

el ( = Emacs Lisp )という拡張子から、一種のテキストファイルとわかります。
こういうビルド不要のフラットなファイルも port で落としてこれる。

# 手元の環境で試した結果
$ port list emacs haskell-mode.el
emacs                          @24.3           editors/emacs
haskell-mode.el                @2.4            lang/haskell-mode.el
$ sudo port install emacs haskell-mode.el
Password:
--->  Computing dependencies for emacs
--->  Fetching archive for emacs
--->  Attempting to fetch emacs-24.3_0.darwin_12.x86_64.tbz2 from http://packages.macports.org/emacs
--->  Attempting to fetch emacs-24.3_0.darwin_12.x86_64.tbz2.rmd160 from http://packages.macports.org/emacs
--->  Installing emacs @24.3_0
--->  Activating emacs @24.3_0
--->  Cleaning emacs
--->  Computing dependencies for haskell-mode.el
--->  Fetching archive for haskell-mode.el
--->  Attempting to fetch haskell-mode.el-2.4_0.darwin_12.noarch.tbz2 from http://packages.macports.org/haskell-mode.el
--->  Attempting to fetch haskell-mode.el-2.4_0.darwin_12.noarch.tbz2 from http://jog.id.packages.macports.org/macports/packages/haskell-mode.el
--->  Attempting to fetch haskell-mode.el-2.4_0.darwin_12.noarch.tbz2 from http://mse.uk.packages.macports.org/sites/packages.macports.org/haskell-mode.el
--->  Fetching distfiles for haskell-mode.el
--->  Attempting to fetch haskell-mode-2.4.tar.gz from http://cjj.kr.distfiles.macports.org/haskell-mode.el
--->  Verifying checksums for haskell-mode.el
--->  Extracting haskell-mode.el
--->  Configuring haskell-mode.el
--->  Building haskell-mode.el
--->  Staging haskell-mode.el into destroot
--->  Installing haskell-mode.el @2.4_0
--->  Activating haskell-mode.el @2.4_0
To use this, put the following into your ~/.emacs:
(load "/opt/local/share/emacs/site-lisp/haskell-mode-2.4/haskell-site-file")
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
(add-hook 'haskell-mode-hook 'font-lock-mode)
(add-hook 'haskell-mode-hook 'imenu-add-menubar-index)

--->  Cleaning haskell-mode.el
--->  Updating database of binaries: 100.0%
--->  Scanning binaries for linking errors: 100.0%
--->  No broken files found.

# 結果確認
$ which emacs
/opt/local/bin/emacs
$ emacs --version
GNU Emacs 24.3.1
Copyright (C) 2013 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

# ファイルはローカルファイルシステムのこのあたりに落ちてきます
$ ls -l /opt/local/share/emacs/site-lisp/haskell-mode-2.4/haskell-site-file*
-rw-r--r--  1 root  admin  9311 Dec 12  2007 /opt/local/share/emacs/site-lisp/haskell-mode-2.4/haskell-site-file.el

4. emacs の起動

とにもかくにも、emacs を起動してみます

emacs

このタイミングで押さえておくべきなのは、

  • C-x C-c で emac を終了できる(Ctrl + x を押して、Ctrl + c)
  • M-x してキーワードを入力すると、マクロの処理が呼び出しできるということ
  • emacs の世界で Metaキー とよばれるキーが、Mac OS X では Esc に割りあてられていること

くらいです。

5. マクロの登録

haskell-mode をつねに有効化するために、emacs の設定ファイルに記載を加えます。
そもそも、emacs の設定ファイルがない、ということもあるのでその場合は作成してやることになります。
具体的に記述する内容は、haskell-mode.el のインストール時にコンソールログにて指示されたコマンド。

# Emacsのユーザ初期設定ファイルを作成
$ touch ~/.emacs.d/init.el

; init.el の記載内容
(load "/opt/local/share/emacs/site-lisp/haskell-mode-2.4/haskell-site-file")
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indent)
(add-hook 'haskell-mode-hook 'font-lock-mode)
(add-hook 'haskell-mode-hook 'imenu-add-menubar-index)

作成した初期設定ファイルにたいする確認は、
emacs を起動して以下のコマンド補完が通るようになっていたら成功です。

・emacs 起動
Meta-x haskell-m (ここでtabキーを2回押下)

成功するとこんな感じ。キャプチャの再下段にご注目。
install_haskell_emacs.png

まとめ


haskell-mode.el により, emacs 上で haskell を実行するための環境を作成する手順をまとめました。

8
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
9