コメントの修正、プログラムの推敲
[exopen-mode.git] / exopen-mode.el
1 ;;; exopen-mode.el
2
3 ;; Copyright (C) 2013  j8takagi
4
5 ;; Author: j8takagi <j8takagi@nifty.com>
6 ;; Keywords: Emacs external program 外部プログラム
7
8 ;; This file is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
12 ;;
13 ;; This file is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
21 ;; Boston, MA 02110-1301 USA
22
23 ;;; Commentary:
24
25 ;; Emacsからの外部プログラム呼び出しを支援するマイナーモード。
26
27 ;; ■動作環境
28 ;; Linux、Mac OS X、WindowsのいずれかのWindowシステム上
29
30 ;; ■インストール方法
31 ;; 1. LOAD-PATHで指定されているディレクトリにこのファイルをコピーする
32 ;;
33 ;; 2. Emacsの設定ファイル(~/.emacs.d/init.el など)に以下の行を追加する
34 ;;
35 ;; (require 'exopen-mode)
36
37 ;; Windowシステム上でEmacsが動作している場合、exopen-mode は自動的に有効になります。
38 ;;
39 ;; ■使い方
40 ;; exopen-modeが有効な場合、次の関数を使うことができます。
41 ;;
42 ;;   exopen-file (file) 外部ファイルを呼び出す
43 ;;   標準で、C-x C-M-fにバインド
44 ;; ■hook
45
46 ;; マイナーモードの定義
47 (define-minor-mode exopen-mode
48 "Toggle exopen-mode.
49 With a prefix argument ARG, enable Auto Composition mode if ARG
50 is positive, and disable it otherwise.  If called from Lisp,
51 enable the mode if ARG is omitted or nil.
52
53 open file in external program."
54 (display-graphic-p)                     ; Windowシステムかどうかの判定
55 nil                                     ; モード行に何も表示しない
56 '(("\C-x\C-\M-f" . exopen-find-file)))  ; キーバインド
57
58 ;; exopen-std-cmd: OSやWindowで設定された関連付けをもとに
59 ;; ファイルを開くプログラムコマンド
60 (defvar exopen-std-cmd nil)
61
62 ;; Window別にexopen-std-cmdを設定する
63 (setq exopen-std-cmd
64       (cond
65        ((eq window-system 'x) "xdg-open")
66        ((eq window-system 'ns) "open")
67        ((eq window-system 'w32) "cmd.exe /c start")))
68
69 ;; exopen-modeでの拡張子とプログラムの関連付けリスト
70 (defvar exopen-suffix-cmd nil)
71
72 ;;; ファイルを外部プログラムでオープン
73 ;;; exopen-std-cmdで指定されたプログラムを使用
74 (defun exopen-file (file)
75   "open file in external program"
76   (let ((process-connection-type nil) (cmd))
77     (if exopen-suffix-cmd
78         (setq cmd (cdr(assoc (file-name-extension file 1) exopen-suffix-cmd))))
79     (unless cmd
80         (setq cmd exopen-std-cmd))
81     (start-process "exopen" nil cmd file)
82     (message
83      (concat "exopen: " cmd " " file " at "
84              (format-time-string "%Y/%m/%d %H:%M:%S" (current-time))))))
85
86 ;;; バッファで開いているファイルを外部プログラムでオープン
87 (defun exopen-buffer-file ()
88   "open buffer file in external program"
89   (interactive)
90   (if buffer-file-name
91       (exopen-file buffer-file-name)
92     (error "This buffer is not visiting a file")))
93
94 ;;; 指定したファイルと同名で拡張子が異なるファイルを外部プログラムでオープン
95 (defun exopen-buffer-file-suffix (suffix)
96   "open buffer file of other extension in external program"
97   (let (afile)
98     (unless buffer-file-name
99         (error "This buffer is not visiting a file"))
100     (setq afile (concat (file-name-sans-extension (buffer-file-name)) suffix))
101     (if (file-exists-p afile)
102         (exopen-file afile)
103       (error (concat afile ": file not found")))))
104
105 ;;; 指定したファイルと同名のPDFファイルを外部プログラムでオープン
106 (defun exopen-buffer-pdffile ()
107   "open pdf file in external program"
108   (interactive)
109   (exopen-buffer-file-suffix ".pdf"))
110
111 ;;; バッファファイルと同名のDVIファイルを外部プログラムでオープン
112 (defun exopen-buffer-dvifile ()
113   "open dvi file in external program"
114   (interactive)
115   (exopen-buffer-file-suffix ".dvi"))
116
117 ;;; バッファファイルと同名のHTMLファイルを外部プログラムでオープン
118 (defun exopen-buffer-htmlfile ()
119   "open html file in external program"
120   (interactive)
121   (exopen-buffer-file-suffix ".html"))
122
123 ;;; find-or-bufferがnilの場合はプロンプトで指定したファイル、
124 ;;; nil以外の場合はバッファのファイルを外部プログラムでオープン
125 (defun exopen-find-file(&optional find-or-buffer)
126   "open buffer file or find-file in external program"
127   (interactive "P")
128   (let ((afile))
129     (unless find-or-buffer
130         (progn
131           (setq afile (expand-file-name
132                        (read-file-name "Find external open file: " buffer-file-name)))
133           (if afile
134               (exopen-file afile)
135             (error "file not found")))
136       (exopen-buffer-file)
137       )))
138
139 ;;; dired-modeからファイルやディレクトリーを開く
140 (require 'dired)
141 (add-hook 'dired-mode-hook
142           (function
143            (lambda ()
144              ;; カーソル下のファイルやディレクトリーを外部プログラムで開く
145              (defun dired-exopen-file ()
146                "Open file mentioned on this line in external program"
147                (interactive)
148                (exopen-file (dired-get-filename)))
149
150              ;; 現在のディレクトリーを外部プログラムで開く
151              (defun dired-exopen-curdir ()
152                "Open current directory in external program"
153                (interactive)
154                (exopen-file (expand-file-name dired-directory)))
155
156              ;; キーバインド
157              (define-key dired-mode-map "r" 'dired-exopen-file)
158              (define-key dired-mode-map "\C-cr" 'dired-exopen-file)
159              (define-key dired-mode-map "\C-c." 'dired-exopen-curdir))))
160
161 (provide 'exopen-mode)
162 ;;; exopen-mode.el ends here