-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopenwith.el
183 lines (157 loc) · 7.14 KB
/
openwith.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
;;; openwith.el --- Open files with external programs
;; Copyright (C) 2007 Markus Triska
;; Author: Markus Triska <[email protected]>
;; Keywords: files, processes
;; URL: https://bitbucket.org/jpkotta/openwith
;; Version: 20120531
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; This lets you associate external applications with files so that
;; you can open them via C-x C-f, with RET in dired, etc.
;; Copy openwith.el to your load-path and add to your .emacs:
;; (require 'openwith)
;; (openwith-mode t)
;; To customize associations etc., use:
;; M-x customize-group RET openwith RET
;;; Code:
(defgroup openwith nil
"Associate external applications with file name patterns."
:group 'files
:group 'processes)
(defcustom openwith-associations
'(("\\.pdf\\'" "acroread" (file))
("\\.mp3\\'" "xmms" (file))
("\\.\\(?:mpe?g\\|avi\\|wmv\\)\\'" "mplayer" ("-idx" file))
("\\.\\(?:jp?g\\|png\\)\\'" "display" (file)))
"Associations of file patterns to external programs.
File pattern is a regular expression describing the files to
associate with a program. The program arguments are a list of
strings and symbols and are passed to the program on invocation,
where the symbol 'file' is replaced by the file to be opened."
:group 'openwith
:type '(repeat (list (regexp :tag "Files")
(string :tag "Program")
(sexp :tag "Parameters"))))
(defcustom openwith-confirm-invocation t
"Ask for confirmation before invoking external programs."
:group 'openwith
:type 'boolean)
(defun openwith-make-extension-regexp (strings)
"Make a regexp that matches a string that starts with a '.',
has any of the supplied STRINGS, and is at the end of the
string."
(concat "\\." (regexp-opt strings) "$"))
(defun openwith-open-unix (command arglist)
"Run external command COMMAND, in such a way that it is
disowned from the parent Emacs process. If Emacs dies, the
process spawned here lives on. ARGLIST is a list of strings,
each an argument to COMMAND."
(let* ((shell-file-name "/bin/sh")
(proc (start-process-shell-command
"openwith-process" nil
(concat
"exec nohup " command " "
(mapconcat 'shell-quote-argument arglist " ")
" >/dev/null"))))
(when (process-live-p proc)
(set-process-query-on-exit-flag proc nil))))
(defun openwith-open-windows (file)
"Run external command COMMAND, in such a way that it is
disowned from the parent Emacs process. If Emacs dies, the
process spawned here lives on. ARGLIST is a list of strings,
each an argument to COMMAND."
(w32-shell-execute "open" file))
(defcustom openwith-execluded-commands
'("org-.*-images")
"A list of regexp matching commands that should be excluded
from `openwith-file-handler'."
:group 'openwith
:type '(repeat (regexp :tag "Program")))
(defun openwith-excluded-command? (cmd-name)
"Test whether CMD matches one of `openwith-excluded-commands'"
;; a trick with `apply' and `append', as all empty list will appended into an
;; empty list. With `dash', `-none?' is all we need.
(car (apply #'append
(mapcar (lambda (exp)
(when (string-match-p exp cmd-name)
;; `append' requires all argument to be list
(list t)))
openwith-execluded-commands))))
(defvar openwith-last-activated-time (current-time)
"Work around the case where `insert-file-contents' is called
consecutively in code. Such a case is `dired-find-file' on an
image. Exact reason is unknown.
If `openwith' is activated within 2 secs twice, the second one
should fail silently. This is reasonable as `openwith' is for
user interaction only.")
(defun openwith-file-handler (operation &rest args)
"Open file with external program, if an association is configured."
(when (and openwith-mode
;; the two following is a hack into the Emacs: before inserting the
;; content a temp buffer is created which should be un-modified and
;; zero-sized.
(not (buffer-modified-p))
(zerop (buffer-size))
(when (time-less-p (seconds-to-time 2)
(time-subtract (current-time) openwith-last-activated-time))
(setq openwith-last-activated-time (current-time)))
(not (openwith-excluded-command? (symbol-name real-this-command))))
(let ((assocs openwith-associations)
(file (car args))
oa)
;; do not use `dolist' here, since some packages (like cl)
;; temporarily unbind it
(while assocs
(setq oa (car assocs)
assocs (cdr assocs))
(when (string-match-p (car oa) file)
(let ((params (mapcar (lambda (x) (if (eq x 'file) file x))
(nth 2 oa))))
(when (or (not openwith-confirm-invocation)
(y-or-n-p (format "%s %s? " (cadr oa)
(mapconcat #'identity params " "))))
(if (eq system-type 'windows-nt)
(openwith-open-windows file)
(openwith-open-unix (cadr oa) params))
(kill-buffer nil)
(when (featurep 'recentf)
(recentf-add-file file))
(message "Opened %s in external program"
(file-name-nondirectory file))
;; inhibit further actions
(keyboard-quit)))))))
;; when no association was found, relay the operation to other handlers
(let ((inhibit-file-name-handlers
(cons 'openwith-file-handler
(and (eq inhibit-file-name-operation operation)
inhibit-file-name-handlers)))
(inhibit-file-name-operation operation))
(apply operation args)))
;;;###autoload
(define-minor-mode openwith-mode
"Automatically open files with external programs."
:lighter ""
:global t
(if openwith-mode
(progn
;; register `openwith-file-handler' for all files
(put 'openwith-file-handler 'safe-magic t)
;; TODO the following marks `openwith-file-handler' only applies to
;; primitive `insert-file-contents', where this behavior is documented.
(put 'openwith-file-handler 'operations '(insert-file-contents))
(add-to-list 'file-name-handler-alist '("" . openwith-file-handler)))
(setq file-name-handler-alist
(delete '("" . openwith-file-handler) file-name-handler-alist))))
(provide 'openwith)
;;; openwith.el ends here