2016年8月3日水曜日

w32-shell-execute

 先日、Org-ModeのHyperLinkのことを調べていて「org-file-apps-defaults-windowsnt」という変数を見つけた。見るからにWindowsで何かアプリケーションを動かしてくれそうだ。
 その中に次のように定義されているところがあった。

(system w32-shell-execute "open" file)

 ——へっ?
 てなもんだ。先日、my-x-openをつくった(ググって見つけたものをパクったものだけれど)ばかりだというのに。あれ、Windowsにも「open」コマンドがあったっけ?
 だいたい「w32-shell-execute」って何よ。MacのEmacsには見当たらないんだけど。それでもしつこくelispをあさってみたら。

2 matches for "w32-shell-execute" in buffer: org.el.gz
   1951:                 'w32-shell-execute)
   1956:                 'w32-shell-execute)
2 matches for "w32-shell-execute" in buffer: browse-url.el.gz
    875:(declare-function w32-shell-execute "w32fns.c")    ;; Defined in C.
    885:        (t (w32-shell-execute "open" url))))

 というのがひっかかってきた。
 ——w32fns.c!
 Cか。
 するとこれはWindowsのときだけリンケージされるようなものなのかもしれない。ああ、もしかしたらcmdを呼ばなくてもすでにEmacsの中からWindowsでもファイル指定でエクセルを立ち上げるなんてことができるようになっていたってことではないだろうか……。

 Emacs for Windows1をあらためて調べてみたら「w32-shell-execute」という関数がやはり存在していてしかもこれなら——当たり前だが——わざわざ、file-pathをWindows用に変換しなくても動くのだった。
 というわけで

(defun my-x-open (file)
  "open file."
  (interactive "FOpen File: ")
  (message "Opening %s..." file)
  (w32-shell-execute "open" file)
  (message "Opening %s...done" file))

 これでいいのだった。しかもなんか、色々できそうな感じ……。
 というか、my-x-openなんで定義せずにw32-shell-executeを直接、呼び出せば、いいじゃん……orz

w32-shell-execute is a built-in function in `C source code'.

(w32-shell-execute OPERATION DOCUMENT &optional PARAMETERS SHOW-FLAG)

Get Windows to perform OPERATION on DOCUMENT.
This is a wrapper around the ShellExecute system function, which
invokes the application registered to handle OPERATION for DOCUMENT.

OPERATION is either nil or a string that names a supported operation.
What operations can be used depends on the particular DOCUMENT and its
handler application, but typically it is one of the following common
operations:

 "open"    - open DOCUMENT, which could be a file, a directory, or an
               executable program (application).  If it is an application,
               that application is launched in the current buffer's default
               directory.  Otherwise, the application associated with
               DOCUMENT is launched in the buffer's default directory.
 "opennew" - like "open", but instruct the application to open
               DOCUMENT in a new window.
 "openas"  - open the "Open With" dialog for DOCUMENT.
 "print"   - print DOCUMENT, which must be a file.
 "printto" - print DOCUMENT, which must be a file, to a specified printer.
               The printer should be provided in PARAMETERS, see below.
 "explore" - start the Windows Explorer on DOCUMENT.
 "edit"    - launch an editor and open DOCUMENT for editing; which
               editor is launched depends on the association for the
               specified DOCUMENT.
 "find"    - initiate search starting from DOCUMENT, which must specify
               a directory.
 "runas"   - run DOCUMENT, which must be an excutable file, with
               elevated privileges (a.k.a. "as Administrator").
 "properties"
           - open the the property sheet dialog for DOCUMENT; works
               for *.lnk desktop shortcuts, and little or nothing else.
 nil       - invoke the default OPERATION, or "open" if default is
               not defined or unavailable.

DOCUMENT is typically the name of a document file or a URL, but can
also be an executable program to run, or a directory to open in the
Windows Explorer.  If it is a file, it must be a local one; this
function does not support remote file names.

If DOCUMENT is an executable program, the optional third arg PARAMETERS
can be a string containing command line parameters that will be passed
to the program.  Some values of OPERATION also require parameters (e.g.,
"printto" requires the printer address).  Otherwise, PARAMETERS should
be nil or unspecified.

Optional fourth argument SHOW-FLAG can be used to control how the
application will be displayed when it is invoked.  If SHOW-FLAG is nil
or unspecified, the application is displayed as if SHOW-FLAG of 10 was
specified, otherwise it is an integer between 0 and 11 representing
a ShowWindow flag:

  0 - start hidden
  1 - start as normal-size window
  3 - start in a maximized window
  6 - start in a minimized window
 10 - start as the application itself specifies; this is the default.

Footnotes:

1

「GNU Emacs 24.5.1 (x8664-pc-mingw32) of 2015-04-14 on NTEMACS64」と「GNU Emacs 25.0.95.1 (i686-pc-cygwin) of 2016-07-09」