home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / languages / elisp / misc / igrep.el < prev    next >
Encoding:
Text File  |  1993-06-21  |  4.3 KB  |  132 lines

  1. ;;;; igrep.el
  2.  
  3. ;;; Description:
  4. ;;; Define the \[igrep] command, which is like \[grep] except that it
  5. ;;; accepts two arguments (EXPRESSION and FILES) instead of one and
  6. ;;; provides defaults when called interactively; plus the \[egrep] and
  7. ;;; \[fgrep] commands.
  8.  
  9. ;;; Copyright:
  10. ;;; Copyright (C) 1993 Kevin Rodgers
  11. ;;;
  12. ;;; This program is free software; you can redistribute it and/or modify
  13. ;;; it under the terms of the GNU General Public License as published by
  14. ;;; the Free Software Foundation; either version 1, or (at your option)
  15. ;;; any later version.
  16. ;;;
  17. ;;; This program is distributed in the hope that it will be useful,
  18. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  20. ;;; GNU General Public License for more details.
  21. ;;;
  22. ;;; You should have received a copy of the GNU General Public License
  23. ;;; along with this program; if not, write to the Free Software
  24. ;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. ;;;
  26. ;;; Martin Marietta has not disclaimed any copyright interest in
  27. ;;; igrep.el.
  28. ;;;
  29. ;;; Kevin Rodgers            kevin@traffic.den.mmc.com
  30. ;;; Martin Marietta MS DC5695        (303) 971-2396
  31. ;;; P.O. Box 179
  32. ;;; Denver CO 80201 USA            GO BUFFS!
  33.  
  34. ;;; Installation:
  35. ;;; 1. Put this file in a directory that is a member of load-path, and
  36. ;;;    byte-compile it for better performance.
  37. ;;; 2. Put these forms in default.el or ~/.emacs:
  38. ;;;    (autoload (function igrep) "igrep"
  39. ;;;       "*Match EXPRESSION in FILES..." t)
  40. ;;;    (autoload (function egrep) "igrep"
  41. ;;;       "*Run egrep, by calling \\[igrep]..." t)
  42. ;;;    (autoload (function fgrep) "igrep"
  43. ;;;       "*Run fgrep, by calling \\[igrep]..." t)
  44.  
  45. ;;; Usage:
  46. ;;; M-x igrep
  47. ;;; M-x egrep
  48. ;;; M-x fgrep
  49. ;;; C-x `
  50.  
  51. ;;; Acknowledgments:
  52. ;;; Wolfgang Rupprecht <wolfgang@mgm.mit.edu>, author of ~/as-is/unix.el.
  53.  
  54. ;;; LCD Archive Entry:
  55. ;;; igrep|Kevin Rodgers|kevin@traffic.den.mmc.com|
  56. ;;; A more convenient interface to grep; plus egrep and fgrep.|
  57. ;;; 22-Jun-1993|1.0|~/misc/igrep.el.Z|
  58.  
  59.  
  60. ;; Package interface:
  61.  
  62. (provide 'igrep)
  63.  
  64. (require 'compile)            ; compile1
  65.  
  66.  
  67. ;; User options:
  68.  
  69. (defvar igrep-command "grep"
  70.   "*The shell command run by \\[igrep].
  71. It must accept a -n flag, an expression argument, and one or more file
  72. arguments.")
  73.  
  74.  
  75. ;; Commands:
  76.  
  77. (defun igrep (expression files)
  78.   "*Match EXPRESSION in FILES, by running igrep-command \(asynchronously\).
  79. The output is displayed in a *compilation* buffer, which the
  80. \\[next-error] command parses to find each line of matched text."
  81.   (interactive (list (read-string (format "%s Expression: " igrep-command)
  82.                   (symbol-around-point))
  83.              (read-string "Files: "
  84.                   (buffer-file-name-pattern))))
  85.   (compile1 (format "%s -n '%s' %s /dev/null" igrep-command expression files)
  86.         (format "No more %s matches" igrep-command) "igrep"))
  87.  
  88. (defun egrep ()
  89.   "*Run egrep, by calling \\[igrep] interactively with igrep-command bound to
  90. \"egrep\"."
  91.   (interactive)
  92.   (let ((igrep-command "egrep"))
  93.     (call-interactively (function igrep))))
  94.  
  95. (defun fgrep ()
  96.   "*Run fgrep, by calling \\[igrep] interactively with igrep-command bound to
  97. \"fgrep\"."
  98.   (interactive)
  99.   (let ((igrep-command "fgrep"))
  100.     (call-interactively (function igrep))))
  101.  
  102.  
  103. ;; Utilities:
  104.  
  105. (defun buffer-file-name-pattern (&optional buffer)
  106.   ;; Return a shell filename pattern that matches files with the same
  107.   ;; extension as the file being visited in BUFFER (which defaults to
  108.   ;; the current buffer if nil or not supplied).
  109.   ;; (Based on other-possibly-interesting-files in ~/as-is/unix.el, by
  110.   ;; Wolfgang Rupprecht <wolfgang@mgm.mit.edu>.)
  111.   (let ((buffer-file-name (buffer-file-name buffer)))
  112.     (concat "*"
  113.         (if buffer-file-name
  114.         (let ((match-data (match-data)))
  115.           (unwind-protect
  116.               (if (string-match "\.[^.]+$" buffer-file-name)
  117.               (substring buffer-file-name
  118.                      (match-beginning 0) (match-end 0)))
  119.             (store-match-data match-data)))))))
  120.  
  121. (defun symbol-around-point ()
  122.   ;; Return the symbol around the point.
  123.   ;; (From ~/as-is/unix.el, by Wolfgang Rupprecht <wolfgang@mgm.mit.edu>.)
  124.   (save-excursion
  125.     (let ((match-data (match-data)))
  126.       (unwind-protect
  127.       (if (not (looking-at "\\sw\\|\\s_"))
  128.           (re-search-backward "\\sw\\|\\s_" nil t))
  129.     (store-match-data match-data)))
  130.     (buffer-substring (progn (backward-sexp 1) (point))
  131.               (progn (forward-sexp 1) (point)))))
  132.