Advertisement
jpkotta

openwith with nohup

Feb 6th, 2012
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.86 KB | None | 0 0
  1. ;;; openwith.el --- Open files with external programs
  2.  
  3. ;; Copyright (C) 2007 Markus Triska
  4.  
  5. ;; Author: Markus Triska <markus.triska@gmx.at>
  6. ;; Keywords: files, processes
  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
  20. ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22.  
  23. ;;; Commentary:
  24.  
  25. ;; This lets you associate external applications with files so that
  26. ;; you can open them via C-x C-f, with RET in dired, etc.
  27.  
  28. ;; Copy openwith.el to your load-path and add to your .emacs:
  29.  
  30. ;; (require 'openwith)
  31. ;; (openwith-mode t)
  32.  
  33. ;; To customize associations etc., use:
  34.  
  35. ;; M-x customize-group RET openwith RET
  36.  
  37. ;;; Code:
  38.  
  39. (defconst openwith-version "0.8f")
  40.  
  41. (defgroup openwith nil
  42. "Associate external applications with file name patterns."
  43. :group 'files
  44. :group 'processes)
  45.  
  46. (defcustom openwith-associations
  47. '(("\\.pdf\\'" "acroread" (file))
  48. ("\\.mp3\\'" "xmms" (file))
  49. ("\\.\\(?:mpe?g\\|avi\\|wmv\\)\\'" "mplayer" ("-idx" file))
  50. ("\\.\\(?:jp?g\\|png\\)\\'" "display" (file)))
  51. "Associations of file patterns to external programs.
  52. File pattern is a regular expression describing the files to
  53. associate with a program. The program arguments are a list of
  54. strings and symbols and are passed to the program on invocation,
  55. where the symbol 'file' is replaced by the file to be opened."
  56. :group 'openwith
  57. :type '(repeat (list (regexp :tag "Files")
  58. (string :tag "Program")
  59. (sexp :tag "Parameters"))))
  60.  
  61. (defcustom openwith-confirm-invocation nil
  62. "Ask for confirmation before invoking external programs."
  63. :group 'openwith
  64. :type 'boolean)
  65.  
  66. (defun openwith-open (command arglist)
  67. "Run external command COMMAND, in such a way that it is
  68. disowned from the parent Emacs process. If Emacs dies, the
  69. process spawned here lives on. ARGLIST is a list of strings,
  70. each an argument to COMMAND."
  71. (let ((shell-file-name "/bin/sh"))
  72. (start-process-shell-command
  73. "openwith-process" nil
  74. (mapconcat 'identity
  75. (concatenate 'list
  76. '("nohup")
  77. (list command)
  78. (mapcar 'shell-quote-argument arglist)
  79. '(">/dev/null"))
  80. " "))))
  81.  
  82. (defun openwith-file-handler (operation &rest args)
  83. "Open file with external program, if an association is configured."
  84. (when (and openwith-mode (not (buffer-modified-p)) (zerop (buffer-size)))
  85. (let ((assocs openwith-associations)
  86. (file (car args))
  87. oa)
  88. ;; do not use `dolist' here, since some packages (like cl)
  89. ;; temporarily unbind it
  90. (while assocs
  91. (setq oa (car assocs)
  92. assocs (cdr assocs))
  93. (when (save-match-data (string-match (car oa) file))
  94. (let ((params (mapcar (lambda (x) (if (eq x 'file) file x))
  95. (nth 2 oa))))
  96. (when (or (not openwith-confirm-invocation)
  97. (y-or-n-p (format "%s %s? " (cadr oa)
  98. (mapconcat #'identity params " "))))
  99. (openwith-open (cadr oa) params)
  100. (kill-buffer nil)
  101. ;; inhibit further actions
  102. (error "Opened %s in external program"
  103. (file-name-nondirectory file))))))))
  104. ;; when no association was found, relay the operation to other handlers
  105. (let ((inhibit-file-name-handlers
  106. (cons 'openwith-file-handler
  107. (and (eq inhibit-file-name-operation operation)
  108. inhibit-file-name-handlers)))
  109. (inhibit-file-name-operation operation))
  110. (apply operation args)))
  111.  
  112. ;;;###autoload
  113. (define-minor-mode openwith-mode
  114. "Automatically open files with external programs."
  115. :lighter ""
  116. :global t
  117. (if openwith-mode
  118. (progn
  119. ;; register `openwith-file-handler' for all files
  120. (put 'openwith-file-handler 'safe-magic t)
  121. (put 'openwith-file-handler 'operations '(insert-file-contents))
  122. (add-to-list 'file-name-handler-alist '("" . openwith-file-handler)))
  123. (setq file-name-handler-alist
  124. (delete '("" . openwith-file-handler) file-name-handler-alist))))
  125.  
  126. (provide 'openwith)
  127.  
  128. ;;; openwith.el ends here
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement