shell on Emacs [Linux]
Emacs 上で shell を複数起動する場合、これまでは下記の手順を実行していた。
・1個目の shell の起動
~/.emacs への (shell) の設定による自動起動
または
M-x shell の実行
・2個目の shell の起動
ミニバッファでの (shell "*shell-2*") の実行
今回、elisp の学習のため、専用の関数を作成した。
設定および使用方法は、下記の通りである。
1. shell-util.el の作成
shell-util.el を作成し、~/lib/emacs/lisp に保存する。
(ソース・コードは、備考を参照。)
2. ~/.emacs の編集
(1) 変数 load-path の変更
load-path に ~/lib/emacs/lisp を追加する。
(2) shell-util.el のロード
(require 'shell-util)
3. 使用方法
(1) Emacs の実行
(2) shell-nth の実行
M-x shell-nth
4. 備考
(1) shell-util.el
・1個目の shell の起動
~/.emacs への (shell) の設定による自動起動
または
M-x shell の実行
・2個目の shell の起動
ミニバッファでの (shell "*shell-2*") の実行
今回、elisp の学習のため、専用の関数を作成した。
設定および使用方法は、下記の通りである。
1. shell-util.el の作成
shell-util.el を作成し、~/lib/emacs/lisp に保存する。
(ソース・コードは、備考を参照。)
2. ~/.emacs の編集
(1) 変数 load-path の変更
load-path に ~/lib/emacs/lisp を追加する。
(2) shell-util.el のロード
(require 'shell-util)
3. 使用方法
(1) Emacs の実行
(2) shell-nth の実行
M-x shell-nth
4. 備考
(1) shell-util.el
;;; -*- tab-width: 4 -*- ;;; shell-util.el --- utilities for shell command (defun shell-command-exists-p (command &optional display-path) "Check whether COMMAND exist. Return t, if COMMAND exists in some directory of command search path. Return nil, if COMMAND does not exist. If DISPLAY-PATH is non-nil, display full path of COMMAND." (let ((path (getenv "PATH")) (plist nil) (match nil) (cnt 0) (dir nil)) (when path (setq plist (split-string path ":"))) (while (and (null match) (setq dir (nth cnt plist))) (when (file-executable-p (concat dir "/" command)) (setq match t)) (setq cnt (+ cnt 1))) (if display-path (message "%s/%s" dir command) match))) (defun shell-which (command &optional display-path) "Check whether COMMAND exist. Return t, if COMMAND exists in some directory of command search path. Return nil, if COMMAND does not exist. If DISPLAY-PATH is non-nil, display full path of COMMAND. This function execute 'which' command." (let ((stat nil) path) (setq path (shell-command-to-string (format "which %s" command))) (cond ((string-match "^/.*" path) (setq path (car (split-string path))) (setq stat t)) (t (setq path ""))) (if display-path (message "%s" path) stat))) (defun shell-nth () "Create Shell mode buffer and run an inferior shell. If `*shell*' does not exist, buffer name is `*shell*'. Otherwise, buffer name is `*shell-N*' (2 <= N)." (interactive) (let ((bl (buffer-list)) bl-shell match buffer-name) ;; get name list of Shell mode buffers (while bl (let ((buffer (car bl))) (setq bl (cdr bl)) (save-excursion (set-buffer buffer) (when (equal mode-name "Shell") (setq buffer-name (buffer-name)) (push buffer-name bl-shell) (when (equal buffer-name "*shell*") (setq match t)))))) ;; set buffer name (cond (match ;; '*shell*' buffer exists (setq bl (sort bl-shell 'string<)) (let ((cnt (length bl)) (c1 2) c2 b-name end) (while (and (<= c1 (1+ cnt)) (null end)) (setq buffer-name (format "*shell-%d*" c1)) (setq c2 0) (setq match nil) (while (and (< c2 cnt) (null match)) (setq b-name (nth c2 bl)) (when (equal b-name buffer-name) (setq match t)) (setq c2 (1+ c2))) (when (null match) (setq end t)) (setq c1 (1+ c1))))) (t (setq buffer-name "*shell*"))) ;; run an inferior shell (shell buffer-name) (message "\"%s\"" buffer-name))) (provide 'shell-util) ;;; shell-util.el ends here
コメント 0