comparison lisp/progmodes/grep.el @ 81936:64c6efb87c40

* progmodes/grep.el (grep-probe): Use `process-file'. (grep-compute-defaults): Handle variables host specific.
author Michael Albinus <michael.albinus@gmx.de>
date Tue, 17 Jul 2007 21:08:49 +0000
parents 25b4502cd0b4
children 7447c9b7efeb
comparison
equal deleted inserted replaced
81935:66d338352087 81936:64c6efb87c40
369 (cons msg code)))) 369 (cons msg code))))
370 (run-hooks 'grep-setup-hook)) 370 (run-hooks 'grep-setup-hook))
371 371
372 (defun grep-probe (command args &optional func result) 372 (defun grep-probe (command args &optional func result)
373 (equal (condition-case nil 373 (equal (condition-case nil
374 (apply (or func 'call-process) command args) 374 (apply (or func 'process-file) command args)
375 (error nil)) 375 (error nil))
376 (or result 0))) 376 (or result 0)))
377 377
378 ;;;###autoload 378 ;;;###autoload
379 (defun grep-compute-defaults () 379 (defun grep-compute-defaults ()
380 (unless (or (not grep-use-null-device) (eq grep-use-null-device t)) 380 (let ((host-id
381 (setq grep-use-null-device 381 (intern (or (file-remote-p default-directory 'host) "localhost"))))
382 (with-temp-buffer 382 ;; There are different defaults on different hosts. They must be
383 (let ((hello-file (expand-file-name "HELLO" data-directory))) 383 ;; computed for every host once, then they are kept in the
384 (not 384 ;; variables' property host-id for reuse.
385 (and (if grep-command 385 (setq grep-command
386 ;; `grep-command' is already set, so 386 (or (get 'grep-command host-id)
387 ;; use that for testing. 387 (eval (car (get 'grep-command 'standard-value))))
388 (grep-probe grep-command 388
389 `(nil t nil "^English" ,hello-file) 389 grep-template
390 #'call-process-shell-command) 390 (or (get 'grep-template host-id)
391 ;; otherwise use `grep-program' 391 (eval (car (get 'grep-template 'standard-value))))
392 (grep-probe grep-program 392
393 `(nil t nil "-nH" "^English" ,hello-file))) 393 grep-use-null-device
394 (progn 394 (or (get 'grep-use-null-device host-id)
395 (goto-char (point-min)) 395 (eval (car (get 'grep-use-null-device 'standard-value))))
396 (looking-at 396
397 (concat (regexp-quote hello-file) 397 grep-find-command
398 ":[0-9]+:English"))))))))) 398 (or (get 'grep-find-command host-id)
399 (unless (and grep-command grep-find-command 399 (eval (car (get 'grep-find-command 'standard-value))))
400 grep-template grep-find-template) 400
401 (let ((grep-options 401 grep-find-template
402 (concat (if grep-use-null-device "-n" "-nH") 402 (or (get 'grep-find-template host-id)
403 (if (grep-probe grep-program 403 (eval (car (get 'grep-find-template 'standard-value))))
404 `(nil nil nil "-e" "foo" ,null-device) 404
405 nil 1) 405 grep-find-use-xargs
406 " -e")))) 406 (or (get 'grep-find-use-xargs host-id)
407 (unless grep-command 407 (eval (car (get 'grep-find-use-xargs 'standard-value))))
408 (setq grep-command 408
409 (format "%s %s " grep-program grep-options))) 409 grep-highlight-matches
410 (unless grep-template 410 (or (get 'grep-highlight-matches host-id)
411 (setq grep-template 411 (eval (car (get 'grep-highlight-matches 'standard-value)))))
412 (format "%s <C> %s <R> <F>" grep-program grep-options))) 412
413 (unless grep-find-use-xargs 413 (unless (or (not grep-use-null-device) (eq grep-use-null-device t))
414 (setq grep-find-use-xargs 414 (setq grep-use-null-device
415 (cond 415 (with-temp-buffer
416 ((and 416 (let ((hello-file (expand-file-name "HELLO" data-directory)))
417 (grep-probe find-program `(nil nil nil ,null-device "-print0")) 417 (not
418 (grep-probe "xargs" `(nil nil nil "-0" "-e" "echo"))) 418 (and (if grep-command
419 'gnu) 419 ;; `grep-command' is already set, so
420 (t 420 ;; use that for testing.
421 'exec)))) 421 (grep-probe grep-command
422 (unless grep-find-command 422 `(nil t nil "^English" ,hello-file)
423 (setq grep-find-command 423 #'call-process-shell-command)
424 (cond ((eq grep-find-use-xargs 'gnu) 424 ;; otherwise use `grep-program'
425 (format "%s . -type f -print0 | xargs -0 -e %s" 425 (grep-probe grep-program
426 find-program grep-command)) 426 `(nil t nil "-nH" "^English" ,hello-file)))
427 ((eq grep-find-use-xargs 'exec) 427 (progn
428 (let ((cmd0 (format "%s . -type f -exec %s" 428 (goto-char (point-min))
429 find-program grep-command))) 429 (looking-at
430 (cons 430 (concat (regexp-quote hello-file)
431 (format "%s {} %s %s" 431 ":[0-9]+:English")))))))))
432 cmd0 null-device 432 (unless (and grep-command grep-find-command
433 (shell-quote-argument ";")) 433 grep-template grep-find-template)
434 (1+ (length cmd0))))) 434 (let ((grep-options
435 (t 435 (concat (if grep-use-null-device "-n" "-nH")
436 (format "%s . -type f -print | xargs %s" 436 (if (grep-probe grep-program
437 find-program grep-command))))) 437 `(nil nil nil "-e" "foo" ,null-device)
438 (unless grep-find-template 438 nil 1)
439 (setq grep-find-template 439 " -e"))))
440 (let ((gcmd (format "%s <C> %s <R>" 440 (unless grep-command
441 grep-program grep-options))) 441 (setq grep-command
442 (format "%s %s " grep-program grep-options)))
443 (unless grep-template
444 (setq grep-template
445 (format "%s <C> %s <R> <F>" grep-program grep-options)))
446 (unless grep-find-use-xargs
447 (setq grep-find-use-xargs
448 (cond
449 ((and
450 (grep-probe find-program `(nil nil nil ,null-device "-print0"))
451 (grep-probe "xargs" `(nil nil nil "-0" "-e" "echo")))
452 'gnu)
453 (t
454 'exec))))
455 (unless grep-find-command
456 (setq grep-find-command
442 (cond ((eq grep-find-use-xargs 'gnu) 457 (cond ((eq grep-find-use-xargs 'gnu)
443 (format "%s . <X> -type f <F> -print0 | xargs -0 -e %s" 458 (format "%s . -type f -print0 | xargs -0 -e %s"
444 find-program gcmd)) 459 find-program grep-command))
445 ((eq grep-find-use-xargs 'exec) 460 ((eq grep-find-use-xargs 'exec)
446 (format "%s . <X> -type f <F> -exec %s {} %s %s" 461 (let ((cmd0 (format "%s . -type f -exec %s"
447 find-program gcmd null-device 462 find-program grep-command)))
448 (shell-quote-argument ";"))) 463 (cons
464 (format "%s {} %s %s"
465 cmd0 null-device
466 (shell-quote-argument ";"))
467 (1+ (length cmd0)))))
449 (t 468 (t
450 (format "%s . <X> -type f <F> -print | xargs %s" 469 (format "%s . -type f -print | xargs %s"
451 find-program gcmd)))))))) 470 find-program grep-command)))))
452 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t)) 471 (unless grep-find-template
453 (setq grep-highlight-matches 472 (setq grep-find-template
454 (with-temp-buffer 473 (let ((gcmd (format "%s <C> %s <R>"
455 (and (grep-probe grep-program '(nil t nil "--help")) 474 grep-program grep-options)))
456 (progn 475 (cond ((eq grep-find-use-xargs 'gnu)
457 (goto-char (point-min)) 476 (format "%s . <X> -type f <F> -print0 | xargs -0 -e %s"
458 (search-forward "--color" nil t)) 477 find-program gcmd))
459 t))))) 478 ((eq grep-find-use-xargs 'exec)
479 (format "%s . <X> -type f <F> -exec %s {} %s %s"
480 find-program gcmd null-device
481 (shell-quote-argument ";")))
482 (t
483 (format "%s . <X> -type f <F> -print | xargs %s"
484 find-program gcmd))))))))
485 (unless (or (not grep-highlight-matches) (eq grep-highlight-matches t))
486 (setq grep-highlight-matches
487 (with-temp-buffer
488 (and (grep-probe grep-program '(nil t nil "--help"))
489 (progn
490 (goto-char (point-min))
491 (search-forward "--color" nil t))
492 t))))
493
494 ;; Save defaults for this host.
495 (put 'grep-command host-id grep-command)
496 (put 'grep-template host-id grep-template)
497 (put 'grep-use-null-device host-id grep-use-null-device)
498 (put 'grep-find-command host-id grep-find-command)
499 (put 'grep-find-template host-id grep-find-template)
500 (put 'grep-find-use-xargs host-id grep-find-use-xargs)
501 (put 'grep-highlight-matches host-id grep-highlight-matches)))
460 502
461 (defun grep-tag-default () 503 (defun grep-tag-default ()
462 (or (and transient-mark-mode mark-active 504 (or (and transient-mark-mode mark-active
463 (/= (point) (mark)) 505 (/= (point) (mark))
464 (buffer-substring-no-properties (point) (mark))) 506 (buffer-substring-no-properties (point) (mark)))