そろそろwindowsにも慣れておいたほうがいいかもしれない。

コマンドプロンプトが使いにくくて嫌になったwindowsですが、そろそろ周りと同じような環境に慣れた方が良いような気がしました。そんな訳で、しばらくはwindowsを使おうと思います。
ただ、コマンドプロンプトだけでは発狂しそうになるので代替のものを探しました。PowerShellとか良さそうです。

幸いmeadowpowershell-modeもありました

http://www.viveksharma.com/techlog/attache/powershell-mode.el.txt
結構サイズが小さいので、major-modeの作り方を学ぶのに良いかもしれません(まだ、全部理解してないです。)

ただ、これだけだとうまくコマンドが実行できませんでした

調べてみたところ、以下のことが分かったので

  • PowerShell -command "<何か>" 」という形で実行できる。
  • "」でくくらないとだめなので、shell-command-on-regionが使えない
  • コマンドオプションにNointeractiveも加えたほうが良さそう

そのことを念頭において以下のような追加を加えました(作ったばかりでろくにテストもしていないので変な挙動を示すかもしれません><))

;;Nointeractiveの追加
(setq  powershell-program "PowerShell -noprofile -nologo -Noninteractive -Command ")

(defun powershell-make-context (start end)
  (let* ((context (buffer-substring-no-properties start end))
	 (context (replace-regexp-in-string "\"" "\\\\\"" context)))
    ;(concat powershell-program (concat "\"" context "\""))))
    (concat powershell-program (concat "\"" context "\""))))


(defun run-powershell-script-currentline () (interactive)
 "現在カーソルがある行の部分だけ実行"
  (save-excursion
    (beginning-of-line 1)
    (let ((start (point)))
      (end-of-line 1)  
      (let ((end (point)))
	(shell-command (powershell-make-context start end))))))

(defun powershell-delete-comment (str)
  (replace-regexp-in-string "\\\(\\\#.*\\\)\n*" "" str))

(defun run-powershell-script () (interactive)
 "カレントバッファの内容を実行"
  (shell-command 
   (powershell-delete-comment (powershell-make-context (point-min) (point-max)))))

あと、Cmdletの名前を補完したかったのでanything-powershell-complete.elというファイルを作りました

alistを作るところでrubyを使っています。(出力を文字列にしてrubyのString#gsubみたいなもので整形する方法が分かりません><)

;;==anything-powershell.el==

(defun anything-powershell-complete () (interactive)
  (let ((anything-sources (list anything-c-source-powershell))
	(anything-initial-input (anything--current-function)))
    (anything)))

(defvar anything-c-source-powershell 
      '((name . "PWS (Command)")
	(candidates . (lambda () (anything-powershell-source)))
	(action . (("insert" . (lambda (c)
				 (let ((n (length anything-initial-input)))
				   (delete-backward-char n)
				   (insert c))))))))

(defvar anything-powershell-source nil)

(defun anything-powershell-source ()
  (or anything-powershell-source
      (let* ((cmds "Get-Command | select name")
	     (context (shell-command-to-string (concat powershell-program  
						       (concat "\"" cmds "\""))))
	     (alist (shell-command-to-string
		     (concat "ruby -e '"
			     "print(%Q{(list \"#{ARGV.first.gsub(/[\n\s]+/,%Q{\" \"})}\")})" 
			     "' \"" context "\""))))
	(setq anything-powershell-source (eval (read alist)))
	anything-powershell-source)))

(provide 'anything-powershell.el)

powershellとか詳しい人達ってどこにいるのでしょう?わかりません><