help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need help improving an elisp 'GNU grep --version' query


From: Kevin Rodgers
Subject: Re: Need help improving an elisp 'GNU grep --version' query
Date: Mon, 25 Aug 2003 12:12:02 -0600
User-agent: Mozilla/5.0 (X11; U; SunOS i86pc; en-US; rv:0.9.4.1) Gecko/20020406 Netscape6/6.2.2

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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]