macOS Big Sur上でemcasをインストールする

はじめに_

homebrewをインストール済みとする。

テキストエディタ(エディタ)_

以上、いくつかの定義を紹介したがこれからわかる通り、エディタとワードプロセッサは違うものである。

エディタとはテキスト(普通の文字)を入力するためのソフトであり、乱暴な言い方をするとワードプロセッサから文書整形・修飾機能(太文字、斜体、拡大・縮小等)を取り去ったものともいえる。ワードプロセッサに比べて軽快に動き、かつ作成したファイルの互換性が高いため、ある程度長い文章でもエディタを愛用する人が多い。

たとえば、Windowsの代表的なエディタはメモ帳であり、ワードプロセッサはMicrosoft Wordである。

Emacs_

EmacsはUNIX/Linux上での代表的なエディタの一つである。

Emacs はUNIX上で有名かつ人気のあるエディタである。 Emacsは高度にカスタマイズ可能であり、Emacs Lispという言語で環境を記述すれば、メールやニュースのリーダーとして使用可能なのは勿論、ブラウザとしても使用可能である。タイピングソフトやプログラム作成時にデバッグする環境も構築できる。

caskをインストールする_

macOS 10.15からemacsを使う場合はインストールする必要がある。Homebrew経由でインストールできるemacsは3種類あり、そのうちcask経由でインストールする。

まず、caskをインストールする。

% brew install cask

emacsをインストールする_

% brew install emacs --cask

% which emacs
/usr/local/bin/emacs

% emacs --version
GNU Emacs 27.2
Copyright (C) 2021 Free Software Foundation, Inc.
GNU Emacs comes with ABSOLUTELY NO WARRANTY.
You may redistribute copies of GNU Emacs
under the terms of the GNU General Public License.
For more information about these matters, see the file named COPYING.

起動してみる。

% emacs &

2021年4月17日時点では、emacsの起動時に「Appleでアプリケーションが悪質なソフトウェアであることを確認できない」というメッセージが表示され、起動できない。

以下のページのとおり「システム環境設定」→「「セキュリティとプライバシー」をクリックし、「一般」をクリックする。ウィンドウの下の方にemacsの起動を許可するかどうかの選択が表示されるので許可する。

一度、許可すると二度目以降は自動的に許可される。

emacsの設定(2021年5月21日修正、2021年6月12日修正)_

emacsは設定ファイルによってさまざまにカスタマイズ可能なエディタである。emacsの設定ファイルはホームディレクトリ下の.emacs.d/init.el としておくこととなっている。このディレクトリはemacsを起動した際に自動的に作成される。確認してみる。

% cd 
% ls -ld .emacs.d
drwx------  9 gotoh  staff  288  5 22 00:45 .emacs.d

今回は後藤が用意した設定ファイルを使って設定してみる。 GitHub:yuichigoto yg-emacs-initsからダウンロードできる。設置方法は以下の通り。

% cd ~/.emacs.d
% git clone https://github.com/yuichigoto/yg-emacs-inits.git
% cp yg-emacs-inits/init-leaf-macOS.el ./init.el

emacsの設定パッケージleaf.el_

本設定ファイルは2020年代のEmacs入門に従って、leafというパッケージシステムを使っている。

今回の設定ファイルでは以下のツールを使えるようにしている。

ミニバッファ拡張ツールivy(2021年5月21日追記)_

ターミナル上で以下のコマンドを実行する。 (2021年5月21日追記:このコマンドはUbuntuのものでした。macOSでは使いません)

% sudo apt install -y elpa-counsel (注:このコマンドはmacOSでつかいません。)

2020年代のEmacs入門の記載を利用している。ただし、migemoとの併用ができなかったのでswiperの設定をコメントアウトしている。

;;; ミニバッファの拡張機能 ivy
;;; ref. https://emacs-jp.github.io/tips/emacs-in-2020
;;; ref. https://github.com/abo-abo/swiper
(leaf ivy
  :doc "Incremental Vertical completYon"
  :req "emacs-24.5"
  :tag "matching" "emacs>=24.5"
  :url "https://github.com/abo-abo/swiper"
  :emacs>= 24.5
  :ensure t
  :blackout t
  :leaf-defer nil
  :custom ((ivy-initial-inputs-alist . nil)
           (ivy-use-selectable-prompt . t))
  :global-minor-mode t
  :config

 
 (leaf swiper
    :doc "Isearch with an overview. Oh, man!"
    :req "emacs-24.5" "ivy-0.13.0"
    :tag "matching" "emacs>=24.5"
    :url "https://github.com/abo-abo/swiper"
    :emacs>= 24.5
    :ensure t
    :bind (("C-s" . swiper)))

  (leaf counsel
    :doc "Various completion functions using Ivy"
    :req "emacs-24.5" "swiper-0.13.0"
    :tag "tools" "matching" "convenience" "emacs>=24.5"
    :url "https://github.com/abo-abo/swiper"
    :emacs>= 24.5
    :ensure t
    :blackout t
    :bind (("C-S-s" . counsel-imenu)
           ("C-x C-r" . counsel-recentf))
    :custom `((counsel-yank-pop-separator . "\n----------\n")
              (counsel-find-file-ignore-regexp . ,(rx-to-string '(or "./" "../") 'no-group)))
    :global-minor-mode t))

(leaf prescient
  :doc "Better sorting and filtering"
  :req "emacs-25.1"
  :tag "extensions" "emacs>=25.1"
  :url "https://github.com/raxod502/prescient.el"
  :emacs>= 25.1
  :ensure t
  :custom ((prescient-aggressive-file-save . t))
  :global-minor-mode prescient-persist-mode)
  
(leaf ivy-prescient
  :doc "prescient.el + Ivy"
  :req "emacs-25.1" "prescient-4.0" "ivy-0.11.0"
  :tag "extensions" "emacs>=25.1"
  :url "https://github.com/raxod502/prescient.el"
  :emacs>= 25.1
  :ensure t
  :after prescient ivy
  :custom ((ivy-prescient-retain-classic-highlighting . t))
  :global-minor-mode t)

migemoの設定(2021年6月12日修正)_

まず、cmigemoをインストールする。

% brew install cmigemo

これを使う設定は以下のとおり。

;; ローマ字検索を可能にするパッケージMIGEMO
;; 以下の辞書のパスは macOS BigSurでの設定
(leaf migemo
  :ensure t
  :require t
  :custom
    (migemo-command . "cmigemo")
    (migemo-options . '("-q" "--emacs"))
    (migemo-dictionary . "/usr/local/Cellar/cmigemo/20110227/share/migemo/utf-8/migemo-dict")
    (migemo-user-dictionary . nil)
    (migemo-regex-dictionary . nil)
    (migemo-coding-system . 'utf-8-unix)
    :config
    (migemo-init))

leaf.plのバイトコンパイル(2021年6月12日修正)_

leaf.plはinit.elを修正するたびに以下のコマンドでバイトコンパイルをする必要がある。init.elcというファイルがバイトコンパイルで生成されるファイル。

% cd ~/.emacs.d
% emacs --batch -f batch-byte-compile init.el
% ls init.elc
init.elc

2021年6月12日現在、バイトコンパイル後にemacsを起動すると「Symbols's function definition is void : leaf-this-file」とエラーがでて、init.elが読み込まれない場合がある。これは2021年5月21日にアップロードしたinit-leaf-macOS.elに原因があった蓋然性が高い。2021年6月12日に差し替えたので、以下のように修正する。

(2021年6月12日に差し替えた版ではmigemoの設定を加えているのでcmigemoをインストールする)
% brew install cmigemo

% cd ~/.emacs.d

(古いダウンロードパッケージとinit.elcを削除する)
% mv elpa elpa.org
% mv var var.org
% rm init.elc

(設定ファイルを最新版に差し替える)
% cd yg-emacs-inits/
% git pull 
% cp init-leaf-macOS.el ../init.el 
% emacs --batch -f batch-byte-compile init.el

動作確認_

背景、文字の色、カーソルの色の変更_

init.elの以下の部分で設定している。現状は背景が黒、文字が白、カーソルが白となっている。

;; 背景、文字の色、カーソルの色の指定
(progn
  (set-background-color "black")
  (set-foreground-color "white")
  (set-cursor-color "white"))

ivyによるミニバッファの拡張_

emacs上で ~/tmp.txt を開く。キーバードから開く(C-x f)で開くと、ivyによるミニバッファの拡張を体験できる。

タブ、全角スペースの可視化_

タブや全角スペースを表示する設定をしている。プログラムを書くようになると、プログラム中に全角スペースが含まれることでエラーになることがある。該当部分は以下のとおり。

;;参考:
;; https://www.emacswiki.org/emacs/WhiteSpace
;; https://qiita.com/itiut@github/items/4d74da2412a29ef59c3a
;; https://ja.wikipedia.org/wiki/Unicode%E4%B8%80%E8%A6%A7_0000-0FFF
(progn
  (require 'whitespace)
  (global-whitespace-mode 1)
  (setq whitespace-style '(
			   face            ; 可視化
			   trailing        ; 行末
			   tabs            ; タブ
			   spaces        ; スペース
			   empty         ; 先頭、末尾のスペース
			   space-mark ; 全角スペースの別記号表記
			   tab-mark    ; タブの別記号表記
			   ))
  (setq whitespace-display-mappings
        '(
          (space-mark ?\u3000 [?\u20DE]) ; 全角スペースを別記号で表記
          (tab-mark ?\t [?\u00BB ?\t] [?\\ ?\t]) ; タブを別記号で表記
          ))
  (setq whitespace-trailing-regexp  "\\([ \u00A0]+\\)$")
  (setq whitespace-space-regexp "\\(\u3000+\\)")
  (set-face-attribute 'whitespace-trailing nil
                      :foreground "RoyalBlue4"
                      :background "RoyalBlue4"
                      :underline nil)
  (set-face-attribute 'whitespace-tab nil
                      :foreground "orange red"
                      :background "yellow4"
                      :underline nil)
  (set-face-attribute 'whitespace-space nil
                      :foreground "gray40"
                      :background "light gray"
                      :underline nil))

どのようになるのか試してみる。

  • サンプルファイルをダウンロードしてみる。
    % cd
    % wget http://www.aise.ics.saitama-u.ac.jp/~gotoh/Download/Samples/Example.whitespace.txt
    
    % ls Example.whitespace.txt (ダウンロードできていることを確かめる)
    
  • emacsでExample.whitespace.txtを開く(emacs上で C-x C-f ~/Example.whitespace.txtを開く)
  • 下の画像のようにタブや全角スペースが色付き表示される。

日本語インクリメンタル検索(2021年6月12日追記)_

日本語で検索できるかどうか試してみる。

  • サンプルファイルをダウンロードしてみる。
    % cd
    % wget http://www.aise.ics.saitama-u.ac.jp/~gotoh/Download/Samples/Example.utf8.txt
    
    % ls Example.utf8.txt (ダウンロードできていることを確かめる)
    
  • emacsでExample.utf8.txtを開く(emacs上で C-x C-f ~/Example.utf8.txtを開く)
  • 検索してみる。C-s pasokon
  • 下の画像のように「パソコン」が検索候補としてヒットする。C-sを押すたびに次候補へ進む
  • 検索終了する場合はEnterキーを押す

戻る_