Windows 10 に Emacs をインストールする

## <i class="fa fa-paw fa-fw"></i>はじめに
いつにまにか `Emacs 25.1` がリリースされていたので，取り敢えずビルドします．

`MSYS2` のインストールとアップデートは，済んでいるものとします．以前の環境からアップデートしたときに，`msys2-launcher` がインストールされなくて，起動するのに悩みました(>_<）今は，バッチファイルから起動しないのですね．

インストールするのに，[chuntaro/NTEmacs64](https://github.com/chuntaro/NTEmacs64)と[emacs-w64](https://sourceforge.net/p/emacsbinw64/wiki/)を参考にしました．これらのサイトの方が詳しいです．また，パッチは[rzl24ozi](https://gist.github.com/rzl24ozi)さんのを使いました．いつもありがとうございます．

## <i class="fa fa-leaf fa-fw"></i> インストール
### 下準備
今回は，`cmigemo` を組込みことにしたので，取り敢えずインストールします．
`mingw64.exe` を起動して作業する．

```bash:
$ curl -O http://iij.dl.osdn.jp/nkf/64158/nkf-2.1.4.tar.gz
$ tar zxvf nkf-2.1.4.tar.gz
$ cd nkf-2.1.4
$ make
$ make install

$ git clone https://github.com/koron/cmigemo.git
$ cd cmigemo
$ ./configure --prefix=/usr/local
$ make mingw-all
$ make mingw-install 
```

### ソースファイルの準備
`Emacs` のソースとパッチを当てる．ここでは，`~/Download/src` で作業しています．

```bash:
$ curl -O http://ftp.gnu.org/gnu/emacs/emacs-25.1.tar.xz
$ curl -O https://gist.githubusercontent.com/rzl24ozi/bceff92df3359267657b/raw/13fd97b85d752c8744499727dd170f0223e040b4/emacs-25.1-image-fit.diff
$ curl -O https://gist.githubusercontent.com/rzl24ozi/8c20b904c9f5e588ba99/raw/0d5694e22a70cb3ea639f27013e4134c9772f1ed/emacs-25.1-w32-ime.diff
$ curl -O https://gist.githubusercontent.com/rzl24ozi/6b1207d20970737f44c7/raw/313b2497dc7ddfc14735e56e0013fd7a75536af7/emacs-25.1-cmigemo.diff

$ tar Jxvf emacs-25.1.tar.xz
$ cd emacs-25.1
$ patch -p0 < ../emacs-25.1-w32-ime.diff
$ patch -p0 < ../emacs-25.1-image-fit.diff
$ patch -p0 < ../emacs-25.1-cmigemo.diff
```

### ビルド
インストール先は，`prefix` で変更します．ここでは，`D:\Programs\emacs-25.1` にしています．

```bash:
$ ./autogen.sh
$ CFLAGS='-Ofast -march=corei7 -mtune=corei7' CPPFLAGS=-I/usr/local/include ./configure --without-pop --without-imagemagick --without-dbus 
$ make
$ make install prefix=/d/Programs/emacs-25.1
```

### DLL のコピー
起動できるように必要な `DLL` をコピーします．(コピペしやすいように，プロンプトは除いていることもあります)

```:emacs
cp /mingw64/bin/{libwinpthread-*.dll,libXpm-noX*.dll,libdbus-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libgomp-*.dll,libgcc_s_seh-*.dll,libglib-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libintl-*.dll,libiconv-*.dll,libgobject-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libffi-*.dll,libgdk_pixbuf-*.dll,libgio-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libgmodule-*.dll,zlib*.dll,librsvg-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libcairo-*.dll,libcroco-*.dll,libpango-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libpangocairo-*.dll,libxml2-*.dll,libfontconfig-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libfreetype-*.dll,libpixman-*.dll,libpng*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libpangoft*.dll,libpangowin32-*.dll,liblzma-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libexpat-*.dll,libharfbuzz-*.dll,libgnutls-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libgnutlsxx-*.dll,libtiff-*.dll,libtiffxx-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libjpeg-*.dll,libgif-*.dll,libbz2-*.dll,libjbig-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libgmp-*.dll,libhogweed-*.dll,libnettle-*.dll} /d/Programs/emacs-25.1/bin
cp /mingw64/bin/{libidn-*.dll,libp11-kit-*.dll,libtasn1-*.dll} /d/Programs/emacs-25.1/bin
```

```bash:migemo
$ cp /usr/local/bin/migemo.dll /d/Programs/emacs-25.1/bin/
$ mkdir -p /d/Programs/emacs-25.1/share/emacs/25.1/etc/migemo/utf-8
$ cp /usr/local/share/migemo/utf-8/* /d/Programs/emacs-25.1/share/emacs/25.1/etc/migemo/utf-8/
```

### cmigemo.el

ソースファイルの `site-lisp` に `cmigemo.el` があるので，`load-path` の通った場所にコピーする．ここでは，`%HOME%\emacs.d\elisp` にしています．

(10/10 追記) 修正されたので，下記の作業は不要になりました．
` isearch-word` が obsolete されたので `isearch-regexp-function` に変更しますが，[emacs-jp/migemo](https://github.com/emacs-jp/migemo/commit/21022f45f5b9c6172dccc50f77761e24c433a389)で修正されていたのでそれを取り込みます．

下記の `cmigemo.el.diff` を `%HOME%\emacs.d\elisp` に作成します．

```bash:
$ cd ~/.emacs.d/elsip
$ patch < cmigemo.el.diff
```

後は，`init.el` などで，`(require 'cmigemo)` で使えるようになります．

```diff:cmigemo.el.diff
--- cmigemo.el.orig	2016-09-27 12:08:34.026393700 +0900
+++ cmigemo.el	2016-09-29 17:47:18.379048200 +0900
@@ -564,6 +564,10 @@
 		       migemo-dabbrev-pattern)))
     (add-hook 'pre-command-hook 'migemo-dabbrev-expand-done)))
 
+(defsubst migemo--isearch-regexp-function () 
+  (or (bound-and-true-p isearch-regexp-function) 
+      (bound-and-true-p isearch-word)))
+
 ;; Use migemo-{forward,backward} instead of search-{forward,backward}.
 (defadvice isearch-search (around migemo-search-ad activate)
   "adviced by migemo."
@@ -577,7 +581,7 @@
   "adviced by migemo."
   (let ((isearch-adjusted isearch-adjusted))
     (when (and migemo-isearch-enable-p
-	       (not isearch-forward) (not isearch-regexp) (not isearch-word))
+	       (not isearch-forward) (not isearch-regexp) (not (migemo--isearch-regexp-function)))
       ;; don't use 'looking-at'
       (setq isearch-adjusted t))
     ad-do-it))
@@ -612,7 +616,7 @@
   (defun isearch-search-fun-migemo ()
 	"Return default functions to use for the search with migemo."
 	(cond
-	 (isearch-word
+	 ((migemo--isearch-regexp-function)
 	  (lambda (string &optional bound noerror count)
 	;; Use lax versions to not fail at the end of the word while
 	;; the user adds and removes characters in the search string
@@ -625,8 +629,8 @@
 				(length (funcall state-string-func (car isearch-cmds))))))))
 	  (funcall
 	   (if isearch-forward #'re-search-forward #'re-search-backward)
-	   (if (functionp isearch-word)
-		   (funcall isearch-word string lax)
+	   (if (functionp (migemo--isearch-regexp-function))
+		   (funcall (migemo--isearch-regexp-function) string lax)
 		 (word-search-regexp string lax))
 	   bound noerror count))))
 	 ((and isearch-regexp isearch-regexp-lax-whitespace
@@ -718,7 +722,7 @@
     (when migemo-emacs21p
       (put-text-property 0 (length str) 'face migemo-message-prefix-face str))
     (when (and migemo-isearch-enable-p
-	       (not (or isearch-regexp isearch-word)))
+	       (not (or isearch-regexp (migemo--isearch-regexp-function))))
       (setq ad-return-value (concat str " " ret)))))
 
 ;;;; for isearch-lazy-highlight (Emacs 21)
@@ -732,7 +736,7 @@
                                                      activate)
     "adviced by migemo"
     (if (and migemo-isearch-enable-p
-             (not isearch-word)
+             (not (migemo--isearch-regexp-function))
              (not isearch-regexp))
         (let ((isearch-string (migemo-search-pattern-get isearch-string))
               (isearch-regexp t))
@@ -770,7 +774,7 @@
   (defun migemo-isearch-toggle-migemo ()
     "Toggle migemo mode in isearch."
     (interactive)
-    (unless (or isearch-regexp isearch-word)
+    (unless (or isearch-regexp (migemo--isearch-regexp-function))
       (discard-input)
       (setq migemo-isearch-enable-p (not migemo-isearch-enable-p)))
     (when (fboundp 'isearch-lazy-highlight-new-loop)
```