comparison lisp/progmodes/grep.el @ 72173:691a901b4418

(grep-default-command): Catch errors from wildcard-to-regexp.
author Richard M. Stallman <rms@gnu.org>
date Sat, 29 Jul 2006 02:03:21 +0000
parents 301cc56a3482
children 35e43fe3d8ef 858cb33ae39d
comparison
equal deleted inserted replaced
72172:7ceb8ae279c9 72173:691a901b4418
463 (get major-mode 'find-tag-default-function) 463 (get major-mode 'find-tag-default-function)
464 'find-tag-default)) 464 'find-tag-default))
465 "")) 465 ""))
466 466
467 (defun grep-default-command () 467 (defun grep-default-command ()
468 "Compute the default grep command for C-u M-x grep to offer."
468 (let ((tag-default (shell-quote-argument (grep-tag-default))) 469 (let ((tag-default (shell-quote-argument (grep-tag-default)))
470 ;; This a regexp to match single shell arguments.
471 ;; Could someone please add comments explaining it?
469 (sh-arg-re "\\(\\(?:\"\\(?:[^\"]\\|\\\\\"\\)+\"\\|'[^']+'\\|[^\"' \t\n]\\)+\\)") 472 (sh-arg-re "\\(\\(?:\"\\(?:[^\"]\\|\\\\\"\\)+\"\\|'[^']+'\\|[^\"' \t\n]\\)+\\)")
470 (grep-default (or (car grep-history) grep-command))) 473 (grep-default (or (car grep-history) grep-command)))
471 ;; Replace the thing matching for with that around cursor. 474 ;; In the default command, find the arg that specifies the pattern.
472 (when (or (string-match 475 (when (or (string-match
473 (concat "[^ ]+\\s +\\(?:-[^ ]+\\s +\\)*" 476 (concat "[^ ]+\\s +\\(?:-[^ ]+\\s +\\)*"
474 sh-arg-re "\\(\\s +\\(\\S +\\)\\)?") 477 sh-arg-re "\\(\\s +\\(\\S +\\)\\)?")
475 grep-default) 478 grep-default)
476 ;; If the string is not yet complete. 479 ;; If the string is not yet complete.
477 (string-match "\\(\\)\\'" grep-default)) 480 (string-match "\\(\\)\\'" grep-default))
478 (unless (or (not (stringp buffer-file-name)) 481 ;; Maybe we will replace the pattern with the default tag.
479 (when (match-beginning 2) 482 ;; But first, maybe replace the file name pattern.
480 (save-match-data 483 (condition-case nil
481 (string-match 484 (unless (or (not (stringp buffer-file-name))
482 (wildcard-to-regexp 485 (when (match-beginning 2)
483 (file-name-nondirectory 486 (save-match-data
484 (match-string 3 grep-default))) 487 (string-match
485 (file-name-nondirectory buffer-file-name))))) 488 (wildcard-to-regexp
486 (setq grep-default (concat (substring grep-default 489 (file-name-nondirectory
487 0 (match-beginning 2)) 490 (match-string 3 grep-default)))
488 " *." 491 (file-name-nondirectory buffer-file-name)))))
489 (file-name-extension buffer-file-name)))) 492 (setq grep-default (concat (substring grep-default
493 0 (match-beginning 2))
494 " *."
495 (file-name-extension buffer-file-name))))
496 ;; In case wildcard-to-regexp gets an error
497 ;; from invalid data.
498 (error nil))
499 ;; Now replace the pattern with the default tag.
490 (replace-match tag-default t t grep-default 1)))) 500 (replace-match tag-default t t grep-default 1))))
491 501
492 502
493 ;;;###autoload 503 ;;;###autoload
494 (define-compilation-mode grep-mode "Grep" 504 (define-compilation-mode grep-mode "Grep"