home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #26 / NN_1992_26.iso / spool / gnu / emacs / help / 4696 < prev    next >
Encoding:
Text File  |  1992-11-05  |  2.8 KB  |  67 lines

  1. Newsgroups: gnu.emacs.help
  2. Path: sparky!uunet!cs.utexas.edu!usc!sdd.hp.com!spool.mu.edu!uwm.edu!linac!pacific.mps.ohio-state.edu!cis.ohio-state.edu!osf.ORG!meissner
  3. From: meissner@osf.ORG
  4. Subject: Re: How to capture process output?
  5. Message-ID: <9211051158.AA07611@curley.osf.org>
  6. Sender: daemon@cis.ohio-state.edu
  7. Organization: Gatewayed from the GNU Project mailing list help-gnu-emacs@prep.ai.mit.edu
  8. References: meissner@osf.ORG
  9. Date: Thu, 5 Nov 1992 01:58:48 GMT
  10. Lines: 55
  11.  
  12. | In article <1992Nov3.171413.25598@viewlogic.com> greg@mobius.viewlogic.com (Gregory Larkin) writes:
  13. | >
  14. | >Hi there,
  15. | >
  16. | >I would like to set a variable to the string that is
  17. | >produced by the "pwd" shell command.  So far, all I
  18. | >can think of is to put the output in a buffer and
  19. | >then use the (buffer-string) function to grab it.
  20. | >
  21. | >Unfortunately, that would also put some other
  22. | >garbage about "process finished" in the string.  Is
  23. | >there an easy way to assign the output of a process
  24. | >directly to a variable?
  25.  
  26. If you use the primitive call-process, you don't have to worry about
  27. the 'process finished' string you see in shell mode.  Here is one way
  28. to write a function returning the output of pwd with the final \n
  29. stripped.  I omitted checking for errors and such:
  30.  
  31. (defun foo () "Demo to show call-process"
  32.   (let ((curbuf (current-buffer))        ;; current buffer
  33.     (buf (get-buffer "*foo*"))        ;; temp buffer
  34.     str)                    ;; return value
  35.     (if buf                    ;; kill *foo*
  36.     (kill-buffer buf))
  37.     (setq buf (get-buffer-create "*foo*"))    ;; create *foo*
  38.     (call-process "pwd" nil buf)        ;; pwd output to *foo*
  39.     (set-buffer buf)                ;; get dir, strip \n
  40.     (setq str (buffer-substring (point-min) (- (point-max) 1)))
  41.     (set-buffer curbuf)                ;; restore current buffer
  42.     (kill-buffer buf)                ;; kill *foo*
  43.     str))                    ;; return directory
  44.  
  45. |    If you just need the current working directory, just use the lisp
  46. | function pwd ala: (setq current-dir (pwd))  this will give you a
  47. | string like: "Directory /u/xxx/xxxx".  or you could do:
  48. | (setq current-dir (substring (pwd) (string-match " " (pwd)) nil)) to
  49. | get rid of the "Directory" junk at the beginning.
  50.  
  51. This is needlessly complicated.  The elisp variable default-directory
  52. always maintains the current directory, and is in fact what cd uses.
  53.  
  54. |    If you want to capture other stuff from a subprocess, use
  55. | set-process-filter and set-process-sentinel... Check out the
  56. | info-pages on subprocesses.
  57.  
  58. It is much simpler to use call-process if you are content to wait
  59. until the subprocesses finishes completely.  Obviously, for things
  60. like shell modes, you can't, but in this example it suffices.
  61.  
  62. --
  63. Michael Meissner    email: meissner@osf.org        phone: 617-621-8861
  64. Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142
  65.  
  66. You are in a twisty little passage of standards, all conflicting.
  67.