home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / gnu / emacs / help / 5096 < prev    next >
Encoding:
Text File  |  1992-12-15  |  6.9 KB  |  178 lines

  1. Xref: sparky gnu.emacs.help:5096 gnu.emacs.sources:864
  2. Newsgroups: gnu.emacs.help,gnu.emacs.sources
  3. Path: sparky!uunet!zaphod.mps.ohio-state.edu!darwin.sura.net!jabba.ess.harris.com!jabba!klaprade
  4. From: klaprade@jabba.ess.harris.com (Ken Laprade)
  5. Subject: Re: DIRED/find-file .gif mode??
  6. Message-ID: <BzBCJ1.IHy@jabba.ess.harris.com>
  7. Sender: usenet@jabba.ess.harris.com (Usenet News Feed Account)
  8. Nntp-Posting-Host: jabba
  9. Reply-To: laprade@trantor.harris-atd.com
  10. Organization: Advanced Technology Dept., Harris Corp., Melbourne, FL
  11. References: <SHAWN.92Dec4115828@litsun17.epfl.ch> <IDF.92Dec9125059@fat-controller.cs.bham.ac.uk>
  12. Date: Tue, 15 Dec 1992 18:25:49 GMT
  13. Lines: 163
  14.  
  15.  
  16. On 4 Dec 92, Shawn Koppenhoefer wrote
  17.  
  18. >> MY QUESTION: is there a "gif-mode.el" somewhere that will
  19. >> automagically launch my "xv" program if I do a find-file on a gif
  20. >> program?? how 'bout a "postscript-mode.el" for launching ghostview if
  21. >> the file is a postscript file.... and lastly, a dvi-mode.el for
  22. >> launching xtex if the file is a .dvi file.
  23.  
  24. I wrote this extension to find-file-noselect to launch such a program
  25. BEFORE reading it into emacs.  I use it with FrameMaker all the time.
  26.  
  27. ----------------------------------------
  28. ;;; -*-Emacs-Lisp-*-
  29. ;;; launcher.el - 15 Dec 92 - KCL
  30. ;;;
  31. ;;; Extension to find-file-noselect to check filetype before reading in.  A
  32. ;;; specific shell command or elisp function can be run on the file rather
  33. ;;; than reading it into a buffer.
  34. ;;;
  35. ;;; $Id: launcher.el,v 1.2 1992/12/15 14:47:36 laprade Exp $
  36. ;;;
  37. ;;; Copyright (C) 1992  Kenneth C. Laprade.
  38. ;;;
  39. ;;; Author: Kenneth C. Laprade (laprade@trantor.harris-atd.com)
  40. ;;;
  41. ;;; This program is free software; you can redistribute it and/or modify
  42. ;;; it under the terms of the GNU General Public License as published by
  43. ;;; the Free Software Foundation; either version 1, or (at your option)
  44. ;;; any later version.
  45. ;;;
  46. ;;; This program is distributed in the hope that it will be useful,
  47. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  48. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  49. ;;; GNU General Public License for more details.
  50. ;;;
  51. ;;; A copy of the GNU General Public License can be obtained from this
  52. ;;; program's author (send electronic mail to laprade@trantor.harris-atd.com)
  53. ;;; or from the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  54. ;;; 02139, USA.
  55. ;;;
  56. ;;;    LCD Archive Entry:
  57. ;;;    launcher|Ken Laprade|laprade@trantor.harris-atd.com
  58. ;;;    |Run a program on a file by checking filetype before reading in
  59. ;;;    |$Date: 1992/12/15 14:47:36 $|$Revision: 1.2 $|
  60.  
  61. ;;; From epoch's wrapper.el:
  62. (or (fboundp 'functionp)
  63.     (defun functionp (thing)
  64.       "Returns t if the argument is a function, nil otherwise"
  65.       (cond
  66.        ((symbolp thing) (fboundp thing))
  67.        ((consp thing)
  68.     (and (eq (car thing) 'lambda) (listp (cadr thing)))    )
  69.        (t nil))))
  70.  
  71. (defvar launcher-list
  72.   '(("\\.doc$" "Frame Maker document" "domaker %s&")
  73.     ("\\.doc$" "Frame Maker book" "domaker %s&")
  74.     (nil "rasterfile\\|P[BGP]M\\|GIF\\|TIFF" "xv %s&")
  75.     (nil "audio data:" "play %s&"))
  76.   "Selection list for program to execute.  May be selected based on either
  77. filename or output of `file' command.  Each element of list is a list.  First
  78. element of each list is a regexp that matches filenames of the file type.
  79. Second element is a regexp that matches the output of the `file' command when
  80. run on a file of the file type.  Either of the selection criteria elements may
  81. be nil, and only the other criteria will be used.  The third element is the
  82. action specifier used if either of the selection criteria matches.  If it is a
  83. string, it is a format specifier for the shell command to run.  The fully
  84. expanded filename will be supplied as an argument to format, so a `%s' should
  85. be placed in the command where the filename should go.  If the element is a
  86. function, it will be called with the fully expanded filename as its argument.")
  87.  
  88. (defvar launcher-magic-file (or (getenv "MAGIC") "/etc/magic")
  89.   "*File to use instead of `/etc/magic' with the `file' command.")
  90.  
  91. (defvar launcher-enabled t
  92.   "*If nil, launcher-find-file-noselect will never try to launch a file.")
  93.  
  94. (defun launcher-file-type (file)
  95.   "Return the type of FILE as reported by the `file' command."
  96.   (let ((buffer (generate-new-buffer " *file-output*"))
  97.     stuff)
  98.     (call-process "file" nil buffer nil "-L" "-m" launcher-magic-file file)
  99.     (save-excursion
  100.       (set-buffer buffer)
  101.       (goto-char (point-min))
  102.       (setq stuff (if (re-search-forward "^[^:]+:[ \t]*\\(.*\\)$" nil t)
  103.               (buffer-substring (match-beginning 1) (match-end 1))
  104.             ""))
  105.       (kill-buffer buffer)
  106.       stuff)))
  107.  
  108. (defun launcher-parse-launcher-list (file)
  109.   "Determine if FILE satisfies any of the criteria in launcher-list.  All
  110. filename regexp's are checked before any `file' command regexp's.  If a match
  111. is found, the command element is returned.  If no criteria is satisfied, nil is
  112. returned."
  113.   (let ((l launcher-list)
  114.     result)
  115.     (while l
  116.       (and (car (car l))
  117.        (string-match (car (car l)) file)
  118.        (setq result (nth 2 (car l))
  119.          l nil))
  120.       (setq l (cdr l)))
  121.     (if result
  122.     ()
  123.       (setq l launcher-list
  124.         file (launcher-file-type file))
  125.       (while l
  126.       (and (nth 1 (car l))
  127.        (string-match (nth 1 (car l)) file)
  128.        (setq result (nth 2 (car l))
  129.          l nil))
  130.       (setq l (cdr l))))
  131.     result))
  132.  
  133. (defun launch-file (file &optional noerror)
  134.   "Try to run a command on FILE as determined by launcher-list.
  135. With NOERROR non-nil, will not call error if FILE cannot be launched."
  136.   (interactive "fLaunch file: ")
  137.   (setq file (expand-file-name file))
  138.   (let ((command (launcher-parse-launcher-list file)))
  139.     (cond
  140.      ((stringp command)
  141.       (call-process "/bin/csh" nil 0 nil "-c" (format command file))
  142.       t)
  143.      ((functionp command)
  144.       (funcall command file)
  145.       t)
  146.      (t
  147.       (or noerror
  148.       (error "Could not launch %s" file))
  149.       nil))))
  150.  
  151. (defun launcher-find-file-noselect (filename &optional nowarn)
  152.   "Try to start up an application for FILENAME based on launcher-list.  If
  153. fails or launcher-enabled is nil, do launcher-original-find-file-noselect.
  154.  
  155. See documentation of launcher-original-find-file-noselect for the original
  156. definition of find-file-noselect."
  157.   (or (and launcher-enabled
  158.        (let (launcher-enabled)    ; In case we launch a find-file.
  159.          (launch-file filename t))
  160.        ;; find-file-noselect is expected to return a buffer to select, so
  161.        ;; return the current buffer.
  162.        (current-buffer))
  163.       (launcher-original-find-file-noselect filename nowarn)))
  164.  
  165. ;;; Interpose launcher-find-file-noselect ahead of find-file-noselect.
  166. (if (fboundp 'launcher-original-find-file-noselect)
  167.     ()
  168.   (fset 'launcher-original-find-file-noselect (symbol-function 'find-file-noselect))
  169.   (fset 'find-file-noselect 'launcher-find-file-noselect))
  170.  
  171. (provide 'launcher)
  172. --
  173. ---
  174. Ken Laprade            INTERNET: laprade@trantor.harris-atd.com
  175. Harris Corporation         Usenet:  ...!uunet!x102a!trantor!laprade
  176. PO Box 37, MS 3A/1912        Voice: (407)727-4433
  177. Melbourne, FL 32902        FAX: (407)729-3363
  178.