ack.el 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ;;; ack.el --- Use ack where you might usually use grep.
  2. ;; Copyright (C) 2008 Philip Jackson
  3. ;; Author: Philip Jackson <phil@shellarchive.co.uk>
  4. ;; Version: 0.1
  5. ;; This file is not currently part of GNU Emacs.
  6. ;; This program is free software; you can redistribute it and/or
  7. ;; modify it under the terms of the GNU General Public License as
  8. ;; published by the Free Software Foundation; either version 2, or (at
  9. ;; your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful, but
  11. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;; General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program ; see the file COPYING. If not, write to
  16. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. ;; Boston, MA 02111-1307, USA.
  18. ;;; Commentary:
  19. ;; ack.el provides a simple compilation mode for the perl grep-a-like
  20. ;; ack (http://petdance.com/ack/).
  21. ;; If `ack-guess-type' is non-nil and `ack-mode-type-map' has a
  22. ;; reasonable value then ack.el will try and guess what you would like
  23. ;; in the --type argument for ack.
  24. ;; To install/use put ack.el in your load-path and (require 'ack) in
  25. ;; your initialisation file. You can then M-x ack and you're off.
  26. (require 'compile)
  27. (defvar ack-guess-type t
  28. "Setting this value to `t' will have `ack' do its best to fill
  29. in the --type argument to the ack command")
  30. (defvar ack-mode-type-map
  31. '(((c++-mode) . "cpp")
  32. ((c-mode) . "cc")
  33. ((css-mode) . "css")
  34. ((emacs-lisp-mode) . "elisp")
  35. ((fortran-mode) . "fortran")
  36. ((html-mode) . "html")
  37. ((xml-mode nxml-mode) . "xml")
  38. ((java-mode) . "java")
  39. ((lisp-mode) . "lisp")
  40. ((perl-mode cperl-mode yaml-mode) . "perl"))
  41. "alist describing how to fill in the '--type=' argument to ack")
  42. (defvar ack-command "ack --nocolor --nogroup "
  43. "The command to be run by the ack function.")
  44. (defun ack-find-type-for-mode ()
  45. (catch 'found
  46. (dolist (mode-type ack-mode-type-map)
  47. (when (member major-mode (car mode-type))
  48. (throw 'found (cdr mode-type))))))
  49. (defun ack-build-command ()
  50. (let ((type (ack-find-type-for-mode)))
  51. (concat ack-command
  52. (when (and ack-guess-type type)
  53. (concat "--type=" type)) " ")))
  54. (define-compilation-mode ack-mode "Ack"
  55. "Ack compilation mode."
  56. nil)
  57. ;;;###autoload
  58. (defvar ack-history nil)
  59. (defvar ack-host-defaults-alist nil)
  60. (defun ack ()
  61. "Like grep, but using ack-command as the default"
  62. (interactive)
  63. ; Make sure grep has been initialized
  64. (if (>= emacs-major-version 22)
  65. (require 'grep)
  66. (require 'compile))
  67. ; Close STDIN to keep ack from going into filter mode
  68. (let ((null-device (format "< %s" null-device))
  69. (grep-command ack-command)
  70. (grep-history ack-history)
  71. (grep-host-defaults-alist ack-host-defaults-alist))
  72. (call-interactively 'grep)
  73. (setq ack-history grep-history
  74. ack-host-defaults-alist grep-host-defaults-alist)))
  75. (provide 'ack)