all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Need help improving an elisp 'GNU grep --version' query
@ 2003-08-25 16:42 Andrew M. Scott
  2003-08-25 17:01 ` Thien-Thi Nguyen
  2003-08-25 18:12 ` Kevin Rodgers
  0 siblings, 2 replies; 3+ messages in thread
From: Andrew M. Scott @ 2003-08-25 16:42 UTC (permalink / raw)


Goal: 

Elisp code that can query GNU 'grep --version' feedback so I can
conditionally use more advanced GNU grep-command option flags, if a
more advanced version of grep is available. 

I'm looking for a *clean* solution that I can extend for use with the
--version results of other GNU utilities.


Current Ugly Code fragments:

(defun my-grep-version () 
 (interactive) 
 (insert (shell-command "ggrep --version | head -1 | awk '{print $4}'"  t nil))
 (backward-delete-char 1))

This M-x my-grep-version returns "2.5.1" or "2.4.2" at point in the current
buffer and "Invalid character: 01414, 780, 0x30c" in the minibuffer.


;; This next snippet would work if my-grep-version were a string variable with
;; value"2.5.1" or "2.4.2"

  (if (string-lessp "2.7" (my-grep-version))
    (progn
      ;; GNU grep 2.5+
      ;; (setq grep-command "ggrep -nH -i --color=always --exclude='\.newsrc.*' ")
      (setq grep-command "ggrep -nH -i --exclude='\.newsrc.*' ")
      )
      ;; GNU grep 2.4 or earlier
      (setq grep-command "ggrep -nH -i ")
      )

Notes & Questions:

1. How do I capture the output of the (shell-command ....) stuff
   into a variable?  I'd rather my-grep-version be a variable vs. a
   function, so I test it later in my .emacs.

2. GNU 'grep --version' returns its version information to stderr not
   stdout. I played with several variations of shell-command and
   call-process and couldn't figure out how to get the results of the
   (shell-command .. ) or equivalent stderr result into a variable
   which I could test.

Thanks in advance.
Andy Scott

-- 
The opinions expressed herein are mine, *not* Intel's.
Andrew M. Scott, Intel Corporation - WCCG-DA Strategic Capabilities
Intel Corporation, M/S CH6-210 Voice: 480-554-9615 RIM PIN: 16545823
ascott@sedona.intel.com (e-mail with "PAGE" in subj to forward to RIM)

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help improving an elisp 'GNU grep --version' query
  2003-08-25 16:42 Need help improving an elisp 'GNU grep --version' query Andrew M. Scott
@ 2003-08-25 17:01 ` Thien-Thi Nguyen
  2003-08-25 18:12 ` Kevin Rodgers
  1 sibling, 0 replies; 3+ messages in thread
From: Thien-Thi Nguyen @ 2003-08-25 17:01 UTC (permalink / raw)


ascott@sedona.intel.com (Andrew M. Scott) writes:

> result into a variable

try something like:

  (nth 3 (split-string (shell-command-to-string "grep --version")))

the 3, being a hardcoded constant (that is neither 0 nor 1 ;-), should
be a hint that there is a code enhancement opportunity here...

alternatively, avoid the pitfalls of inferring/assigning meaning to
version numbers and do some kind of test on the program in question,
directly.

thi

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Need help improving an elisp 'GNU grep --version' query
  2003-08-25 16:42 Need help improving an elisp 'GNU grep --version' query Andrew M. Scott
  2003-08-25 17:01 ` Thien-Thi Nguyen
@ 2003-08-25 18:12 ` Kevin Rodgers
  1 sibling, 0 replies; 3+ messages in thread
From: Kevin Rodgers @ 2003-08-25 18:12 UTC (permalink / raw)


Andrew M. Scott wrote:

> Elisp code that can query GNU 'grep --version' feedback so I can
> conditionally use more advanced GNU grep-command option flags, if a
> more advanced version of grep is available. 


The way I do that is to try to run the command with the option and check its
return status.  Here's an example from igrep.el:

(defvar igrep-regex-option
   (if (equal (call-process igrep-program nil nil nil
			   "-e" "foo" igrep-null-device)
	     1)
       "-e")
   "If non-nil, the option used to specify the REGEX argument to `\\[igrep]',
to protect an initial `-' from option processing.")

That runs "grep -e foo /dev/null" and checks to see that it failed with an
exit status 1 (meaning match not found, instead of any other code which would
indicate that the option was not recognized).

> I'm looking for a *clean* solution that I can extend for use with the
> --version results of other GNU utilities.
> 
> 
> Current Ugly Code fragments:
> 
> (defun my-grep-version () 
>  (interactive) 
>  (insert (shell-command "ggrep --version | head -1 | awk '{print $4}'"  t nil))
>  (backward-delete-char 1))


You can eliminate the head shell comand and the backward-delete-char Emacs
command with this:

grep --version | awk '{printf("%s", $NF); exit}'

> This M-x my-grep-version returns "2.5.1" or "2.4.2" at point in the current
> buffer and "Invalid character: 01414, 780, 0x30c" in the minibuffer.
> 
> 
> ;; This next snippet would work if my-grep-version were a string variable with
> ;; value"2.5.1" or "2.4.2"
> 
>   (if (string-lessp "2.7" (my-grep-version))
>     (progn
>       ;; GNU grep 2.5+
>       ;; (setq grep-command "ggrep -nH -i --color=always --exclude='\.newsrc.*' ")
>       (setq grep-command "ggrep -nH -i --exclude='\.newsrc.*' ")
>       )
>       ;; GNU grep 2.4 or earlier
>       (setq grep-command "ggrep -nH -i ")
>       )
> 
> Notes & Questions:
> 
> 1. How do I capture the output of the (shell-command ....) stuff
>    into a variable?  I'd rather my-grep-version be a variable vs. a
>    function, so I test it later in my .emacs.


(setq my-grep-version (shell-command-to-string "..."))


> 2. GNU 'grep --version' returns its version information to stderr not
>    stdout. I played with several variations of shell-command and
>    call-process and couldn't figure out how to get the results of the
>    (shell-command .. ) or equivalent stderr result into a variable
>    which I could test.

Since it's a shell command, you can redirect standard error to standard out

by appending "2>&1".

-- 
Kevin Rodgers

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2003-08-25 18:12 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-08-25 16:42 Need help improving an elisp 'GNU grep --version' query Andrew M. Scott
2003-08-25 17:01 ` Thien-Thi Nguyen
2003-08-25 18:12 ` Kevin Rodgers

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.