home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: gnu.emacs.help
- 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
- From: meissner@osf.ORG
- Subject: Re: How to capture process output?
- Message-ID: <9211051158.AA07611@curley.osf.org>
- Sender: daemon@cis.ohio-state.edu
- Organization: Gatewayed from the GNU Project mailing list help-gnu-emacs@prep.ai.mit.edu
- References: meissner@osf.ORG
- Date: Thu, 5 Nov 1992 01:58:48 GMT
- Lines: 55
-
- | In article <1992Nov3.171413.25598@viewlogic.com> greg@mobius.viewlogic.com (Gregory Larkin) writes:
- | >
- | >Hi there,
- | >
- | >I would like to set a variable to the string that is
- | >produced by the "pwd" shell command. So far, all I
- | >can think of is to put the output in a buffer and
- | >then use the (buffer-string) function to grab it.
- | >
- | >Unfortunately, that would also put some other
- | >garbage about "process finished" in the string. Is
- | >there an easy way to assign the output of a process
- | >directly to a variable?
-
- If you use the primitive call-process, you don't have to worry about
- the 'process finished' string you see in shell mode. Here is one way
- to write a function returning the output of pwd with the final \n
- stripped. I omitted checking for errors and such:
-
- (defun foo () "Demo to show call-process"
- (let ((curbuf (current-buffer)) ;; current buffer
- (buf (get-buffer "*foo*")) ;; temp buffer
- str) ;; return value
- (if buf ;; kill *foo*
- (kill-buffer buf))
- (setq buf (get-buffer-create "*foo*")) ;; create *foo*
- (call-process "pwd" nil buf) ;; pwd output to *foo*
- (set-buffer buf) ;; get dir, strip \n
- (setq str (buffer-substring (point-min) (- (point-max) 1)))
- (set-buffer curbuf) ;; restore current buffer
- (kill-buffer buf) ;; kill *foo*
- str)) ;; return directory
-
- | If you just need the current working directory, just use the lisp
- | function pwd ala: (setq current-dir (pwd)) this will give you a
- | string like: "Directory /u/xxx/xxxx". or you could do:
- | (setq current-dir (substring (pwd) (string-match " " (pwd)) nil)) to
- | get rid of the "Directory" junk at the beginning.
-
- This is needlessly complicated. The elisp variable default-directory
- always maintains the current directory, and is in fact what cd uses.
-
- | If you want to capture other stuff from a subprocess, use
- | set-process-filter and set-process-sentinel... Check out the
- | info-pages on subprocesses.
-
- It is much simpler to use call-process if you are content to wait
- until the subprocesses finishes completely. Obviously, for things
- like shell modes, you can't, but in this example it suffices.
-
- --
- Michael Meissner email: meissner@osf.org phone: 617-621-8861
- Open Software Foundation, 11 Cambridge Center, Cambridge, MA, 02142
-
- You are in a twisty little passage of standards, all conflicting.
-