# macOS 27 でウィンドウリサイズと日本語インライン入力を両立する Emacs 環境構築

macOS 27 環境で Emacs を導入する際、パッケージの選択によって以下のトレードオフが発生します。

| インストール方法 | ウィンドウリサイズ | 日本語変換の文節判別（下線） | 備考 |
| :--- | :---: | :---: | :--- |
| `emacs-mac`（Emacs 29） | 不可 | 可能 | macOS 27 のライブウィンドウリサイズと競合し、マウスでサイズ変更できない |
| 公式 Cask 版 `emacs`（Emacs 31） | 可能 | 不可 | IMEインラインパッチが含まれず、変換中の文節の区切りが見えない |
| **`emacs-plus@31` + インラインパッチ** | **可能** | **可能** | **NS版ベースに日本語パッチを適用することで両立可能** |

本記事では、`d12frosted/emacs-plus` の **Emacs 31** に日本語インラインパッチ（`takaxp/ns-inline-patch`）を適用してビルドする手順をまとめます。

---

## 前提条件

* macOS 27
* Homebrew がインストール済みであること

---

## インストール手順

### 1. 競合する既存の Emacs を削除する（導入済みの場合）

他の方法でインストールした Emacs が残っているとコマンドのリンクが衝突するため、事前にアンインストールします。

```bash
# Cask版を入れている場合
brew uninstall --cask emacs

# emacs-mac を入れている場合
brew uninstall emacs-mac
```

### 2. タップの追加と信頼設定（`brew trust`）

`d12frosted/emacs-plus` を追加します。近年の Homebrew では外部タップの Formula をロードする前に明示的な信頼許可（`brew trust`）が必要になるため、あわせて実行します。

```bash
brew tap d12frosted/emacs-plus
brew trust d12frosted/emacs-plus
brew trust --formula d12frosted/emacs-plus/emacs-plus@31
```

### 3. 日本語インラインパッチ用の設定ファイル（`build.yml`）を作成

`emacs-plus` は `~/.config/emacs-plus/build.yml` にパッチの URL と SHA256 ハッシュ値を記述しておくことで、ビルド時に自動適用できます。

以下のコマンドを実行し、リポジトリから最新のパッチを取得して設定ファイルを生成します。

```bash
mkdir -p ~/.config/emacs-plus

PATCH_NAME=$(curl -fsSL https://api.github.com/repos/takaxp/ns-inline-patch/contents/ | grep -o '"name": "emacs-[0-9.]*-inline\.patch"' | cut -d'"' -f4 | sort | tail -n 1)
PATCH_URL="https://raw.githubusercontent.com/takaxp/ns-inline-patch/master/${PATCH_NAME}"
SHA=$(curl -fsSL "$PATCH_URL" | shasum -a 256 | awk '{print $1}')

cat <<EOF> ~/.config/emacs-plus/build.yml
patches:
  - ns-inline-patch:
      url: "$PATCH_URL"
      sha256: "$SHA"
EOF
```

生成された内容を確認します（`url` と64桁の `sha256` が書き込まれていれば正常です）。

```bash
cat ~/.config/emacs-plus/build.yml
```

### 4. `emacs-plus@31` のビルドとインストール

ソースコードからのコンパイルとパッチ適用が行われます（環境により数分かかります）。

```bash
brew install d12frosted/emacs-plus/emacs-plus@31
```

インストール完了後、以下のコマンドで直接起動して動作を確認します。

```bash
open /opt/homebrew/opt/emacs-plus@31/Emacs.app
```

### 5. 「アプリケーション」フォルダへの登録（Spotlight 対応）

Formula でビルドした `Emacs.app` は `/Applications` に自動配置されません。通常のシンボリックリンク（`ln -s`）では Spotlight 検索に表示されないため、Finder のエイリアスを作成します。

```bash
osascript -e 'tell application "Finder" to make alias file to POSIX file "/opt/homebrew/opt/emacs-plus@31/Emacs.app" at POSIX file "/Applications"'
```

---

## 動作確認チェックリスト

* [ ] ウィンドウの端をマウスカーソルでドラッグして自由にサイズ変更できる
* [ ] 日本語入力（IMEオン）時、変換中の文節（注目文節）と他の文節が下線や背景色の違いで判別できる
* [ ] Spotlight（`Command + Space`）で `Emacs` と検索して起動できる

