├── .gitignore ├── env.mk.sample ├── run-ci.sh ├── .editorconfig ├── .github └── workflows │ └── test.yml ├── wgrep-subtest.el ├── wgrep-pt.el ├── Makefile ├── wgrep-test-helper.el ├── README.md ├── wgrep-ack.el ├── wgrep-helm.el ├── wgrep-deadgrep.el ├── wgrep-test.el ├── wgrep-ag.el ├── COPYING └── wgrep.el /.gitignore: -------------------------------------------------------------------------------- 1 | *.elc 2 | env.mk 3 | -------------------------------------------------------------------------------- /env.mk.sample: -------------------------------------------------------------------------------- 1 | ELPA-DIR = ~/path/to/developer/elpa/ 2 | -------------------------------------------------------------------------------- /run-ci.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh -e 2 | 3 | # This script is intended invoke by GithubAction `CI` job 4 | # Keep as *~ to unexpectedly be executed by developer. 5 | test -f env.mk && mv env.mk env.mk~ 6 | 7 | # Local elpa directory. 8 | echo "ELPA-DIR = ./elpa" >> env.mk 9 | 10 | make ci 11 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # Editor configuration, see https://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | charset = utf-8 6 | indent_style = space 7 | indent_size = 2 8 | insert_final_newline = true 9 | trim_trailing_whitespace = true 10 | 11 | [*.md] 12 | trim_trailing_whitespace = false 13 | -------------------------------------------------------------------------------- /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | schedule: 5 | - cron: '22 23 * * 5' 6 | pull_request: 7 | push: 8 | paths-ignore: 9 | - '**.md' 10 | 11 | jobs: 12 | build: 13 | runs-on: ${{ matrix.os }} 14 | strategy: 15 | matrix: 16 | os: 17 | - ubuntu-latest 18 | - macos-latest 19 | emacs_version: 20 | - 28.1 21 | - 28.2 22 | - 29.1 23 | - 29.2 24 | - 29.3 25 | - snapshot 26 | include: 27 | - os: ubuntu-latest 28 | emacs_version: 24.5 29 | - os: ubuntu-latest 30 | emacs_version: 25.3 31 | - os: ubuntu-latest 32 | emacs_version: 26.3 33 | - os: ubuntu-latest 34 | emacs_version: 27.2 35 | env: 36 | EMACS_LINT_IGNORE: ${{ matrix.lint_ignore }} 37 | steps: 38 | - uses: purcell/setup-emacs@master 39 | with: 40 | version: ${{ matrix.emacs_version }} 41 | 42 | - uses: actions/checkout@v3 43 | - name: Run tests 44 | run: './run-ci.sh' 45 | -------------------------------------------------------------------------------- /wgrep-subtest.el: -------------------------------------------------------------------------------- 1 | (require 'ert) 2 | (require 'wgrep-test-helper) 3 | (require 'ag) 4 | 5 | (ert-deftest wgrep-ag-normal () 6 | :tags '(wgrep-subtest) 7 | (wgrep-test-helper--default 8 | (wgrep-test-helper-fixture "HOGE\nFOO\nBAZ\n" 9 | (lambda (file) 10 | (wgrep-test-helper--ag "FOO|HOGE" file) 11 | (wgrep-change-to-wgrep-mode) 12 | (goto-char (point-min)) 13 | (wgrep-goto-first-found) 14 | ;; search hit line (hit by -C option) 15 | (should (re-search-forward "HOGE" nil t)) 16 | ;; delete 1st line 17 | (wgrep-mark-deletion) 18 | (should (re-search-forward "FOO" nil t)) 19 | ;; replace 2nd line 20 | (replace-match "FOO2") 21 | ;; apply to buffer 22 | (wgrep-finish-edit) 23 | ;; save to file 24 | (wgrep-save-all-buffers) 25 | ;; compare file contents is valid 26 | (should (equal "FOO2\nBAZ\n" (wgrep-test-helper--get-contents file))))))) 27 | 28 | (ert-deftest wgrep-deadgrep-normal () 29 | :tags '(wgrep-subtest) 30 | (wgrep-test-helper--default 31 | (wgrep-test-helper-fixture "HOGE\nFOO\nBAZ\n" 32 | (lambda (file) 33 | (wgrep-test-helper--deadgrep "FOO|HOGE") 34 | (wgrep-change-to-wgrep-mode) 35 | (goto-char (point-min)) 36 | (wgrep-goto-first-found) 37 | ;; search hit line (hit by -C option) 38 | (should (re-search-forward "HOGE" nil t)) 39 | ;; delete 1st line 40 | (wgrep-mark-deletion) 41 | (should (re-search-forward "FOO" nil t)) 42 | ;; replace 2nd line 43 | (replace-match "FOO2") 44 | ;; apply to buffer 45 | (wgrep-finish-edit) 46 | ;; save to file 47 | (wgrep-save-all-buffers) 48 | ;; compare file contents is valid 49 | (should (equal "FOO2\nBAZ\n" (wgrep-test-helper--get-contents file))))))) 50 | -------------------------------------------------------------------------------- /wgrep-pt.el: -------------------------------------------------------------------------------- 1 | ;;; wgrep-pt.el --- Writable pt buffer -*- lexical-binding: t -*- 2 | 3 | ;; Copyright (C) 2010-2020,2023 Masahiro Hayashi 4 | 5 | ;; Author: Masahiro Hayashi , Bailey Ling 6 | ;; Keywords: grep edit extensions 7 | ;; Package-Requires: ((emacs "25.1") (wgrep "3.0.0")) 8 | ;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep-pt.el 9 | ;; Emacs: GNU Emacs 25 or later 10 | ;; Version: 1.0.0 11 | 12 | ;; This program is free software; you can redistribute it and/or 13 | ;; modify it under the terms of the GNU General Public License as 14 | ;; published by the Free Software Foundation; either version 3, or (at 15 | ;; your option) any later version. 16 | 17 | ;; This program is distributed in the hope that it will be useful, but 18 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 | ;; General Public License for more details. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 25 | ;; Boston, MA 02110-1301, USA. 26 | 27 | ;;; Commentary: 28 | 29 | ;; wgrep-pt allows you to edit a pt buffer and apply those changes to 30 | ;; the file buffer. 31 | 32 | ;;; Install: 33 | 34 | ;; 1. Install pt.el 35 | ;; 36 | ;; https://github.com/bling/pt.el 37 | 38 | ;; 2. Install wgrep.el 39 | 40 | ;; 3. Put this file into load-path'ed directory, and byte compile it if 41 | ;; desired. And put the following expression into your ~/.emacs. 42 | ;; 43 | ;; (autoload 'wgrep-pt-setup "wgrep-pt") 44 | ;; (add-hook 'pt-search-mode-hook 'wgrep-pt-setup) 45 | 46 | ;;; Usage: 47 | 48 | ;; See wgrep.el 49 | 50 | ;;; Code: 51 | 52 | (require 'wgrep) 53 | 54 | ;;;###autoload 55 | (defun wgrep-pt-setup () 56 | (wgrep-setup-internal)) 57 | 58 | ;;;###autoload 59 | (add-hook 'pt-search-mode-hook 'wgrep-pt-setup) 60 | 61 | ;; For `unload-feature' 62 | (defun wgrep-pt-unload-function () 63 | (remove-hook 'pt-search-mode-hook 'wgrep-pt-setup)) 64 | 65 | (provide 'wgrep-pt) 66 | 67 | ;;; wgrep-pt.el ends here 68 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | ### 2 | ### Package 3 | ### 4 | 5 | -include env.mk 6 | 7 | NEEDED-PACKAGES ?= dash ag s 8 | 9 | EL := wgrep.el 10 | EL += wgrep-ack.el 11 | EL += wgrep-ag.el 12 | EL += wgrep-helm.el 13 | EL += wgrep-pt.el 14 | EL += wgrep-deadgrep.el 15 | 16 | TEST_EL := wgrep-test.el 17 | TEST_EL += wgrep-test-helper.el 18 | 19 | ## 20 | ## Emacs 21 | ## 22 | 23 | EMACS ?= emacs 24 | 25 | BATCH := $(EMACS) -Q -batch -L . 26 | 27 | ## 28 | ## package.el 29 | ## 30 | 31 | ifdef ELPA-DIR 32 | BATCH += -eval "(setq package-user-dir (expand-file-name \"$(ELPA-DIR)\"))" 33 | endif 34 | 35 | # This come from `package-lint/run-tests.sh` 36 | define package-installer 37 | "(progn \ 38 | (require 'package) \ 39 | (push '(\"melpa\" . \"https://melpa.org/packages/\") package-archives) \ 40 | (package-initialize) \ 41 | (package-refresh-contents) \ 42 | (dolist (pkg '($(1))) \ 43 | (unless (package-installed-p pkg) \ 44 | (package-install pkg))))" 45 | endef 46 | 47 | ### 48 | ### Command 49 | ### 50 | 51 | BUILD_BATCH := $(BATCH) -eval "(require 'package)" -f package-initialize 52 | ifndef EMACS_LINT_IGNORE 53 | BUILD_BATCH += -eval "(setq byte-compile-error-on-warn t)" 54 | endif 55 | 56 | ifdef EMACS_LINT_IGNORE 57 | LINT_BATCH := true 58 | else 59 | LINT_BATCH := $(BATCH) -eval $(call package-installer, package-lint) 60 | endif 61 | 62 | CI_BATCH := $(BATCH) -eval $(call package-installer, package-lint $(NEEDED-PACKAGES)) 63 | 64 | ### 65 | ### Files 66 | ### 67 | 68 | ELC := $(EL:%.el=%.elc) 69 | BUILD_GENERATED := *.elc 70 | MAINTAINER_GENERATED := elpa *~ 71 | 72 | LOAD_EL := $(EL:%=-l %) 73 | LOAD_ELC := $(ELC:%=-l %) 74 | 75 | LOAD_TEST_EL := $(TEST_EL:%=-l %) 76 | 77 | ### 78 | ### General rule 79 | ### 80 | 81 | .PHONY: all check compile clean 82 | 83 | all: check 84 | 85 | check: compile 86 | $(BUILD_BATCH) $(LOAD_EL) $(LOAD_TEST_EL) -f ert-run-tests-batch-and-exit 87 | $(BUILD_BATCH) $(LOAD_ELC) $(LOAD_TEST_EL) -f ert-run-tests-batch-and-exit 88 | 89 | compile: 90 | $(BUILD_BATCH) -f batch-byte-compile $(EL) 91 | 92 | clean: 93 | rm -rf $(BUILD_GENERATED) 94 | 95 | ### 96 | ### Maintainer rule 97 | ### 98 | 99 | .PHONY: lint package maintainer-clean 100 | 101 | lint: 102 | $(LINT_BATCH) -f package-lint-batch-and-exit $(EL) 103 | 104 | package: lint check 105 | 106 | 107 | maintainer-clean: clean 108 | rm -rf $(MAINTAINER_GENERATED) 109 | 110 | ### 111 | ### CI/CD rule 112 | ### 113 | 114 | .PHONY: ci prepare-cicd 115 | 116 | ci: prepare-cicd package 117 | 118 | prepare-cicd: 119 | $(CI_BATCH) 120 | -------------------------------------------------------------------------------- /wgrep-test-helper.el: -------------------------------------------------------------------------------- 1 | ;;; wgrep-test-helper.el --- A wgrep test helper -*- lexical-binding: t-*- 2 | 3 | ;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep-test-helper.el 4 | 5 | (require 'wgrep) 6 | 7 | (defun wgrep-test-helper--wait (buf) 8 | (let ((proc (get-buffer-process buf))) 9 | (while (eq (process-status proc) 'run) 10 | (sit-for 0.1)) 11 | (sleep-for 0.2) 12 | (switch-to-buffer buf))) 13 | 14 | (defun wgrep-test-helper--grep (command) 15 | (let ((buf (grep command))) 16 | (wgrep-test-helper--wait buf))) 17 | 18 | (defun wgrep-test-helper--get-contents (file &optional cs) 19 | (let ((coding-system-for-read cs)) 20 | (with-temp-buffer 21 | (insert-file-contents file) 22 | (buffer-string)))) 23 | 24 | (defun wgrep-test-helper--prepare-file (file contents &optional cs) 25 | ;; cleanup for convenience 26 | (let ((buf (get-file-buffer file))) 27 | (when (buffer-live-p buf) 28 | (kill-buffer buf))) 29 | (let ((coding-system-for-write cs)) 30 | (write-region contents nil file))) 31 | 32 | (defun wgrep-test-helper--cleanup-file (file) 33 | (when (file-exists-p file) 34 | (delete-file file)) 35 | (when (file-exists-p (concat file "~")) 36 | (delete-file (concat file "~")))) 37 | 38 | (defmacro wgrep-test-helper--default (&rest body) 39 | `(let ((wgrep-change-readonly-file nil) 40 | (wgrep-auto-save-buffer nil)) 41 | (progn ,@body))) 42 | 43 | (defun wgrep-test-helper--ag (string file) 44 | (let ((buf (ag/search string default-directory :file-regex (regexp-quote file) :regexp t))) 45 | (wgrep-test-helper--wait buf))) 46 | 47 | (defun wgrep-test-helper--deadgrep (string) 48 | (let ((deadgrep-project-root-function (lambda () default-directory)) 49 | (deadgrep--search-type 'regexp)) 50 | (deadgrep string)) 51 | (wgrep-test-helper--wait (current-buffer))) 52 | 53 | (defun wgrep-test-helper-fixture (data body-fn) 54 | (let ((test-directory (expand-file-name "test-work" default-directory))) 55 | (unless (file-directory-p test-directory) 56 | (make-directory test-directory t)) 57 | (let ((default-directory (file-name-as-directory test-directory))) 58 | (let ((file (concat (make-temp-name "test-data") ".txt"))) 59 | (pcase data 60 | ((pred stringp) 61 | (wgrep-test-helper--prepare-file file data)) 62 | (`(,(and (pred stringp) data) ,(and (pred coding-system-p) cs)) 63 | (wgrep-test-helper--prepare-file file data cs)) 64 | (_ 65 | (error "DATA should be STRING or (STRING CODING-SYSTEM)"))) 66 | (unwind-protect 67 | (funcall body-fn file) 68 | (wgrep-test-helper--cleanup-file file)))))) 69 | 70 | (put 'wgrep-test-helper-fixture 'lisp-indent-function 1) 71 | 72 | (provide 'wgrep-test-helper) 73 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | wgrep.el 2 | ======== 3 | 4 | [![CI](https://github.com/mhayashi1120/Emacs-wgrep/actions/workflows/test.yml/badge.svg)](https://github.com/mhayashi1120/Emacs-wgrep/actions/workflows/test.yml) 5 | 6 | ## Summary: 7 | 8 | wgrep allows you to edit a grep buffer and apply those changes to 9 | the file buffer like `sed` interactively. No need to learn sed 10 | script, just learn Emacs. 11 | 12 | ## Install: 13 | 14 | Put this file into load-path'ed directory, and byte compile it if 15 | desired. And put the following expression into your ~/.emacs. 16 | 17 | ``` 18 | (require 'wgrep) 19 | ``` 20 | 21 | ## Usage: 22 | 23 | You can edit the text in the *grep* buffer after typing `C-c C-p` . 24 | After that the changed text is highlighted. 25 | The following keybindings are defined: 26 | 27 | * `C-c C-e`: Apply the changes to file buffers. 28 | * `C-c C-u`: All changes are unmarked and ignored. 29 | * `C-c C-d`: Mark as delete to current line (including newline). 30 | * `C-c C-r`: Remove the changes in the region (these changes are not 31 | applied to the files. Of course, the remaining 32 | changes can still be applied to the files.) 33 | * `C-c C-p`: Toggle read-only area. 34 | * `C-c C-k`: Discard all changes and exit. 35 | * `C-x C-q`: Exit wgrep mode. 36 | 37 | * To save all buffers that wgrep has changed, run 38 | 39 | ``` 40 | M-x wgrep-save-all-buffers 41 | ``` 42 | 43 | * To save buffer automatically when `wgrep-finish-edit`. 44 | 45 | ``` 46 | (setq wgrep-auto-save-buffer t) 47 | ``` 48 | 49 | * You can change the default key binding to switch to wgrep. 50 | 51 | ``` 52 | (setq wgrep-enable-key "r") 53 | ``` 54 | 55 | * To apply all changes regardless of whether or not buffer is read-only. 56 | 57 | ``` 58 | (setq wgrep-change-readonly-file t) 59 | ``` 60 | 61 | ### Trivial features: 62 | 63 | - wdired.el like interface. 64 | - Support GNU grep `--context` (`-A` `-B` and `-C`) option. 65 | - Can handle too many files. 66 | - Can handle newline insertion in *grep* buffer. 67 | - Delete whole line include newline. 68 | 69 | ### Similar software: 70 | 71 | [GNU sed](https://www.gnu.org/software/sed/) 72 | [helm-ag](https://github.com/syohex/emacs-helm-ag) has a similar feature. 73 | 74 | ## History: 75 | 76 | This program was forked from Matsushita Akihisa's [grep-edit.el 77 | v2.11](http://www.bookshelf.jp/elc/grep-edit.el). As this website is 78 | now inaccessible, 79 | [emacswiki.org's](https://www.emacswiki.org/emacs/grep-edit.el) copy 80 | may be consulted. 81 | 82 | Imported version can be downloaded from this orphan commit: 83 | https://github.com/mhayashi1120/Emacs-wgrep/tree/import-original-author/ 84 | 85 | Following added implementations and differences. 86 | * Support GNU grep context option -A -B and -C 87 | * Some bugfix. (wrong coloring text etc..) 88 | * wdired.el like interface. 89 | * Remove all advice. 90 | * Bind to local variables. (grep-a-lot.el works well) 91 | * After save buffer, colored face will be removed. 92 | * Change face easy to see. 93 | * Reinforce checking error. 94 | * Support removing whole line include new-line. 95 | 96 | -------------------------------------------------------------------------------- /wgrep-ack.el: -------------------------------------------------------------------------------- 1 | ;;; wgrep-ack.el --- Writable ack-and-a-half buffer -*- lexical-binding: t -*- 2 | 3 | ;; Copyright (C) 2010-2020,2023 Masahiro Hayashi 4 | 5 | ;; Author: Masahiro Hayashi 6 | ;; Contributor: Ivan Andrus 7 | ;; Keywords: grep edit extensions 8 | ;; Package-Requires: ((emacs "25.1") (wgrep "3.0.0")) 9 | ;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep-ack.el 10 | ;; Emacs: GNU Emacs 25 or later 11 | ;; Version: 1.0.0 12 | 13 | ;; This program is free software; you can redistribute it and/or 14 | ;; modify it under the terms of the GNU General Public License as 15 | ;; published by the Free Software Foundation; either version 3, or (at 16 | ;; your option) any later version. 17 | 18 | ;; This program is distributed in the hope that it will be useful, but 19 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 | ;; General Public License for more details. 22 | 23 | ;; You should have received a copy of the GNU General Public License 24 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 25 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 26 | ;; Boston, MA 02110-1301, USA. 27 | 28 | ;;; Commentary: 29 | 30 | ;; wgrep-ack allows you to edit a ack-and-a-half buffer and apply those 31 | ;; changes to the file buffer. 32 | 33 | ;;; Install: 34 | 35 | ;; 1. Install ack-and-a-half.el 36 | ;; 37 | ;; https://github.com/jhelwig/ack-and-a-half 38 | 39 | ;; 2. Install wgrep.el 40 | 41 | ;; 3. Put this file into load-path'ed directory, and byte compile it if 42 | ;; desired. And put the following expression into your ~/.emacs. 43 | ;; 44 | ;; (require 'wgrep-ack) 45 | 46 | ;;; Usage: 47 | 48 | ;; See wgrep.el 49 | 50 | ;;; Code: 51 | 52 | (require 'wgrep) 53 | 54 | ;;;###autoload 55 | (defun wgrep-ack-and-a-half-setup () 56 | ;; ack-and-a-half-mode prints a column number too, so we catch that 57 | ;; if it exists. Here \2 is a colon + whitespace separator. This 58 | ;; might need to change if (caar grep-regexp-alist) does. 59 | (set (make-local-variable 'wgrep-line-file-regexp) 60 | (concat 61 | wgrep-default-line-header-regexp 62 | "\\(?:\\([1-9][0-9]*\\)\\2\\)?")) 63 | (wgrep-setup-internal)) 64 | 65 | ;;;###autoload 66 | (defun wgrep-ack-setup () 67 | (set (make-local-variable 'wgrep-results-parser) 68 | 'wgrep-ack-prepare-command-results) 69 | (wgrep-setup-internal)) 70 | 71 | (defun wgrep-ack-prepare-command-results () 72 | (let (fprop fn) 73 | (while (not (eobp)) 74 | (cond 75 | ((null fn) 76 | ;; index of filename 77 | (let ((bol (line-beginning-position)) 78 | (eol (line-end-position))) 79 | (when (/= bol eol) 80 | (setq fn (buffer-substring-no-properties bol eol)) 81 | (setq fprop (wgrep-construct-filename-property fn)) 82 | (put-text-property bol eol fprop fn) 83 | (put-text-property bol eol 'wgrep-ignore t)))) 84 | ((looking-at "^\\([0-9]+\\)[:-]") 85 | (let ((start (match-beginning 0)) 86 | (end (match-end 0)) 87 | (line (string-to-number (match-string 1)))) 88 | (put-text-property start end 'wgrep-line-filename fn) 89 | (put-text-property start end 'wgrep-line-number line))) 90 | ((looking-at "^$") 91 | (setq fn nil))) 92 | (forward-line 1)))) 93 | 94 | ;;;###autoload 95 | (add-hook 'ack-and-a-half-mode-hook 'wgrep-ack-and-a-half-setup) 96 | 97 | ;;;###autoload 98 | (add-hook 'ack-mode-hook 'wgrep-ack-setup) 99 | 100 | ;; For `unload-feature' 101 | (defun wgrep-ack-unload-function () 102 | (remove-hook 'ack-and-a-half-mode-hook 'wgrep-ack-and-a-half-setup) 103 | (remove-hook 'ack-mode-hook 'wgrep-ack-setup)) 104 | 105 | (provide 'wgrep-ack) 106 | 107 | ;;; wgrep-ack.el ends here 108 | -------------------------------------------------------------------------------- /wgrep-helm.el: -------------------------------------------------------------------------------- 1 | ;;; wgrep-helm.el --- Writable helm-grep-mode buffer -*- lexical-binding: t -*- 2 | 3 | ;; Copyright (C) 2010-2020,2023 Masahiro Hayashi 4 | 5 | ;; Author: Masahiro Hayashi 6 | ;; Keywords: grep edit extensions 7 | ;; Package-Requires: ((emacs "25.1") (wgrep "3.0.0")) 8 | ;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep-helm.el 9 | ;; Emacs: GNU Emacs 25 or later 10 | ;; Version: 3.0.0 11 | 12 | ;; This program is free software; you can redistribute it and/or 13 | ;; modify it under the terms of the GNU General Public License as 14 | ;; published by the Free Software Foundation; either version 3, or (at 15 | ;; your option) any later version. 16 | 17 | ;; This program is distributed in the hope that it will be useful, but 18 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 | ;; General Public License for more details. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 25 | ;; Boston, MA 02110-1301, USA. 26 | 27 | ;;; Commentary: 28 | 29 | ;; wgrep-helm allows you to edit a helm-grep-mode buffer and apply those 30 | ;; changes to the file buffer. 31 | 32 | ;;; Install: 33 | 34 | ;; Put this file into load-path'ed directory, and byte compile it if 35 | ;; desired. And put the following expression into your ~/.emacs. 36 | ;; 37 | ;; (require 'wgrep-helm) 38 | 39 | ;;; Usage: 40 | 41 | ;; See wgrep.el 42 | 43 | ;;; Code: 44 | 45 | (require 'wgrep) 46 | 47 | (declare-function helm-grep-split-line "helm-grep") 48 | 49 | ;;;###autoload 50 | (defun wgrep-helm-setup () 51 | (set (make-local-variable 'wgrep-header&footer-parser) 52 | 'wgrep-helm-prepare-header&footer) 53 | (set (make-local-variable 'wgrep-results-parser) 54 | 'wgrep-helm-parse-command-results) 55 | (wgrep-setup-internal)) 56 | 57 | (defun wgrep-helm-prepare-header&footer () 58 | (let (beg end) 59 | ;; Set read-only grep result header 60 | (setq beg (point-min)) 61 | (setq end (next-single-property-change 62 | (point-min) 'helm-realvalue)) 63 | (put-text-property beg end 'read-only t) 64 | (put-text-property beg end 'wgrep-header t) 65 | ;; helm-grep-mode have NO footer. 66 | )) 67 | 68 | (defun wgrep-helm-parse-command-results () 69 | (while (not (eobp)) 70 | (when (looking-at wgrep-line-file-regexp) 71 | (let* ((start (match-beginning 0)) 72 | (end (match-end 0)) 73 | (dispname (match-string 1)) 74 | (namelen (length dispname))) 75 | (let* ((value (get-text-property (point) 'helm-realvalue)) 76 | (data (when (eq major-mode 'helm-grep-mode) 77 | (helm-grep-split-line value))) 78 | (bufname (get-text-property (point) 'buffer-name)) 79 | (fn (or (and bufname (buffer-file-name (get-buffer bufname))) 80 | (get-text-property (point) 'helm-grep-fname))) 81 | (line (if data 82 | (string-to-number (nth 1 data)) 83 | ;; Real value of candidate is now the line 84 | ;; number in helm-occur. 85 | value)) 86 | (fprop (wgrep-construct-filename-property fn))) 87 | (put-text-property start end 'wgrep-line-filename fn) 88 | (put-text-property start end 'wgrep-line-number line) 89 | (put-text-property start (+ start namelen) fprop fn)))) 90 | (forward-line 1))) 91 | 92 | ;;;###autoload 93 | (add-hook 'helm-grep-mode-hook 'wgrep-helm-setup) 94 | 95 | ;;;###autoload 96 | (add-hook 'helm-occur-mode-hook 'wgrep-helm-setup) 97 | 98 | ;; For `unload-feature' 99 | (defun wgrep-helm-unload-function () 100 | (remove-hook 'helm-grep-mode-hook 'wgrep-helm-setup) 101 | (remove-hook 'helm-occur-mode-hook 'wgrep-helm-setup)) 102 | 103 | (provide 'wgrep-helm) 104 | 105 | ;;; wgrep-helm.el ends here 106 | -------------------------------------------------------------------------------- /wgrep-deadgrep.el: -------------------------------------------------------------------------------- 1 | ;;; wgrep-deadgrep.el --- Writable deadgrep buffer and apply the changes to files -*- lexical-binding: t -*- 2 | 3 | ;; Author: Masahiro Hayashi , Iku Iwasa 4 | ;; Keywords: grep edit extensions 5 | ;; Package-Requires: ((wgrep "2.3.0") (emacs "25.1")) 6 | ;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep-deadgrep.el 7 | ;; Emacs: GNU Emacs 25 or later 8 | ;; Version: 2.3.1 9 | 10 | ;; This program is free software; you can redistribute it and/or 11 | ;; modify it under the terms of the GNU General Public License as 12 | ;; published by the Free Software Foundation; either version 3, or (at 13 | ;; your option) any later version. 14 | 15 | ;; This program is distributed in the hope that it will be useful, but 16 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18 | ;; General Public License for more details. 19 | 20 | ;; You should have received a copy of the GNU General Public License 21 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 22 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 23 | ;; Boston, MA 02110-1301, USA. 24 | 25 | ;;; Commentary: 26 | 27 | ;; wgrep-deadgrep allows you to edit a deadgrep buffer and apply those changes to 28 | ;; the file buffer. 29 | 30 | ;;; Install: 31 | 32 | ;; 1. Install deadgrep.el 33 | ;; 34 | ;; https://github.com/Wilfred/deadgrep 35 | 36 | ;; 2. Install wgrep.el 37 | 38 | ;; 3. Put this file into load-path'ed directory, and byte compile it if 39 | ;; desired. And put the following expression into your ~/.emacs. 40 | ;; 41 | ;; (autoload 'wgrep-deadgrep-setup "wgrep-deadgrep") 42 | ;; (add-hook 'deadgrep-finished-hook 'wgrep-deadgrep-setup) 43 | 44 | ;;; Usage: 45 | 46 | ;; See wgrep.el 47 | 48 | ;;; Code: 49 | 50 | (require 'wgrep) 51 | 52 | (defun wgrep-deadgrep-prepare-header&footer () 53 | "Prepare header in `deadgrep' buffer for `wgrep'. 54 | Since `deadgrep' does not have footer, only header is handled." 55 | ;; `deadgrep' uses "deadgrep-filename" property for filename. 56 | (let ((pos (next-single-property-change (point-min) 'deadgrep-filename))) 57 | (if pos 58 | (add-text-properties (point-min) pos '(read-only t wgrep-header t)) 59 | (add-text-properties (point-min) (point-max) 60 | '(read-only t wgrep-header t))))) 61 | 62 | (defun wgrep-deadgrep-parse-command-results () 63 | "Parse `deadgrep' results for `wgrep'." 64 | ;; Note that this function is called with the buffer narrowed to 65 | ;; exclude the header and the footer. (We're going to assert that 66 | ;; fact here, because we use (bobp) result a bit further down to 67 | ;; decide that we're not reading grouped results; see below.) 68 | (unless (bobp) 69 | (error "Expected to be called with point at beginning of buffer")) 70 | (save-excursion 71 | (while (not (eobp)) 72 | (let* ((pos (point)) 73 | (filename (get-text-property pos 'deadgrep-filename)) 74 | (line (get-text-property pos 'deadgrep-line-number))) 75 | (when filename 76 | (if line 77 | (let* ((eol (line-end-position)) 78 | (end (next-single-property-change 79 | pos 'deadgrep-line-number nil eol))) 80 | (add-text-properties pos end 81 | (list 'wgrep-line-filename filename 82 | 'wgrep-line-number line))) 83 | ;; Ignore the line that introduces matches from a file, so that 84 | ;; wgrep doesn't let you edit it. 85 | (add-text-properties 86 | pos (line-end-position) 87 | (list 'wgrep-ignore t 88 | (wgrep-construct-filename-property filename) 89 | filename))))) 90 | (forward-line 1)))) 91 | 92 | ;;;###autoload 93 | (defun wgrep-deadgrep-setup () 94 | "Setup `wgrep-deadgrep' for `deadgrep'." 95 | (setq wgrep-prepared nil) 96 | (set (make-local-variable 'wgrep-header&footer-parser) 97 | 'wgrep-deadgrep-prepare-header&footer) 98 | (set (make-local-variable 'wgrep-results-parser) 99 | 'wgrep-deadgrep-parse-command-results) 100 | (wgrep-setup-internal)) 101 | 102 | ;;;###autoload 103 | (add-hook 'deadgrep-finished-hook 'wgrep-deadgrep-setup) 104 | 105 | ;; For `unload-feature' 106 | (defun wgrep-deadgrep-unload-function () 107 | "Unload `wgrep-deadgrep' setup." 108 | (remove-hook 'deadgrep-finished-hook 'wgrep-deadgrep-setup)) 109 | 110 | (provide 'wgrep-deadgrep) 111 | 112 | ;;; wgrep-deadgrep.el ends here 113 | -------------------------------------------------------------------------------- /wgrep-test.el: -------------------------------------------------------------------------------- 1 | (require 'ert) 2 | (require 'dash) 3 | (require 's) 4 | (require 'wgrep-test-helper) 5 | 6 | (ert-deftest wgrep-normal () 7 | :tags '(wgrep) 8 | (wgrep-test-helper--default 9 | (wgrep-test-helper-fixture "HOGE\nFOO\nBAZ\n" 10 | (lambda (file) 11 | (wgrep-test-helper--grep (concat "grep -nH -e FOO -C 1 " file)) 12 | (wgrep-change-to-wgrep-mode) 13 | (goto-char (point-min)) 14 | ;; header is readonly 15 | (should (re-search-forward "^grep" nil t)) 16 | (should-error (delete-char 1) :type 'text-read-only) 17 | ;; search hit line (hit by -C option) 18 | (should (re-search-forward "HOGE" nil t)) 19 | ;; delete 1st line 20 | (wgrep-mark-deletion) 21 | (should (re-search-forward "FOO" nil t)) 22 | ;; replace 2nd line 23 | (replace-match "FOO2") 24 | ;; footer is readonly 25 | (goto-char (point-max)) 26 | (should-error (delete-char -1) :type 'text-read-only) 27 | ;; apply to buffer 28 | (wgrep-finish-edit) 29 | ;; save to file 30 | (wgrep-save-all-buffers) 31 | ;; compare file contents is valid 32 | (should (equal "FOO2\nBAZ\n" (wgrep-test-helper--get-contents file))))))) 33 | 34 | (ert-deftest wgrep-normal-with-newline () 35 | :tags '(wgrep) 36 | (wgrep-test-helper--default 37 | (wgrep-test-helper-fixture "HOGE\n" 38 | (lambda (file) 39 | (wgrep-test-helper--grep (concat "grep -nH -e HOGE " file)) 40 | (wgrep-change-to-wgrep-mode) 41 | (goto-char (point-min)) 42 | ;; through the header 43 | (should (re-search-forward (concat "^" (regexp-quote file) ":") nil t)) 44 | ;; search hit line (hit by -C option) 45 | (should (re-search-forward "HOGE" nil t)) 46 | (replace-match "FOO\nBAZ") 47 | ;; apply to buffer 48 | (wgrep-finish-edit) 49 | ;; save to file 50 | (wgrep-save-all-buffers) 51 | ;; compare file contents is valid 52 | (should (equal "FOO\nBAZ\n" (wgrep-test-helper--get-contents file))))))) 53 | 54 | (ert-deftest wgrep-bom-with-multibyte () 55 | :tags '(wgrep) 56 | (wgrep-test-helper--default 57 | (wgrep-test-helper-fixture '("あ\nい\nう\n" utf-8-with-signature) 58 | (lambda (file) 59 | (wgrep-test-helper--grep (concat "grep -nH -e 'あ' -A 2 " file)) 60 | (wgrep-change-to-wgrep-mode) 61 | (goto-char (point-min)) 62 | ;; BOM check is valid. 63 | ;; skip BOM by `.*' 64 | (should (re-search-forward (concat (regexp-quote file) ":[0-9]+:.*\\(あ\\)$") nil t)) 65 | (replace-match "へのへのも" nil nil nil 1) 66 | ;; 2nd line 67 | (should (re-search-forward (concat (regexp-quote file) "-[0-9]+-\\(い\\)$") nil t)) 68 | (replace-match "へじ" nil nil nil 1) 69 | ;; apply to buffer 70 | (wgrep-finish-edit) 71 | ;; save to file 72 | (wgrep-save-all-buffers) 73 | ;; compare file contents is valid 74 | (should (equal "へのへのも\nへじ\nう\n" (wgrep-test-helper--get-contents file))) 75 | )))) 76 | 77 | (ert-deftest wgrep-bom-with-unibyte () 78 | :tags '(wgrep) 79 | (wgrep-test-helper--default 80 | (wgrep-test-helper-fixture '("a\nb\n" utf-8-with-signature) 81 | (lambda (file) 82 | (wgrep-test-helper--grep (concat "grep -nH -e 'a' -A 2 " file)) 83 | (wgrep-change-to-wgrep-mode) 84 | (goto-char (point-min)) 85 | ;; BOM check is valid. 86 | (should (re-search-forward (concat (regexp-quote file) ":[0-9]+:.*\\(a\\)$") nil t)) 87 | (replace-match "ABCD" nil nil nil 1) 88 | ;; apply to buffer 89 | (wgrep-finish-edit) 90 | ;; save to file 91 | (wgrep-save-all-buffers) 92 | ;; compare file contents is valid 93 | (should (equal "ABCD\nb\n" (wgrep-test-helper--get-contents file))))))) 94 | 95 | (ert-deftest wgrep-with-modify () 96 | :tags '(wgrep) 97 | (wgrep-test-helper--default 98 | (wgrep-test-helper-fixture "a\nb\nc\n" 99 | (lambda (file) 100 | (let (;; This test intended to check modified buffer is existing. 101 | ;; Keep that buffer is modifying while calling grep. 102 | (grep-save-buffers nil)) 103 | (with-current-buffer (find-file-noselect file) 104 | ;; modify file buffer 105 | (goto-char (point-min)) 106 | (and (re-search-forward "^a" nil t) 107 | (replace-match "hoge")) 108 | (and (re-search-forward "^b" nil t) 109 | (replace-match "foo"))) 110 | (wgrep-test-helper--grep (concat "grep -nH -e 'a' -A 2 " file)) 111 | (wgrep-change-to-wgrep-mode) 112 | (goto-char (point-min)) 113 | ;; delete "a" line (failed when saving) 114 | (should (re-search-forward (concat (regexp-quote file) ":[0-9]+:.*\\(a\\)$") nil t)) 115 | (wgrep-mark-deletion) 116 | ;; replace "b" line (failed when saving) 117 | (should (re-search-forward (concat (regexp-quote file) "-[0-9]+-.*\\(b\\)$") nil t)) 118 | (replace-match "B" nil nil nil 1) 119 | ;; replace "c" line 120 | (should (re-search-forward (concat (regexp-quote file) "-[0-9]+-.*\\(c\\)$") nil t)) 121 | (replace-match "C" nil nil nil 1) 122 | ;; apply to buffer 123 | (wgrep-finish-edit) 124 | ;; save to file 125 | (wgrep-save-all-buffers) 126 | ;; compare file contents is valid. (keep preceding file buffer's contents) 127 | (should (equal "hoge\nfoo\nC\n" (wgrep-test-helper--get-contents file)))))))) 128 | 129 | (ert-deftest wgrep-with-readonly-file () 130 | :tags '(wgrep) 131 | (wgrep-test-helper--default 132 | (wgrep-test-helper-fixture "a\nb\nc\n" 133 | (lambda (file) 134 | ;; make readonly 135 | (set-file-modes file ?\400) 136 | (wgrep-test-helper--grep (concat "grep -nH -e 'a' " file)) 137 | (wgrep-change-to-wgrep-mode) 138 | (goto-char (point-min)) 139 | (should (re-search-forward (concat (regexp-quote file) ":[0-9]+:.*\\(a\\)$") nil t)) 140 | (replace-match "A" nil nil nil 1) 141 | ;; only check with no error 142 | (wgrep-finish-edit) 143 | 144 | ;; TODO check result file is unchanged 145 | )))) 146 | 147 | ;; TODO (Not implemented testcase) 148 | ;; * wgrep-toggle-readonly-area 149 | ;; ** sort-lines 150 | ;; * wgrep-abort-changes 151 | ;; * wgrep-exit 152 | ;; * broken file contents (invalid coding system) 153 | ;; * new text contains newline 154 | ;; * wgrep-change-readonly-file 155 | ;; * test wgrep-*.el 156 | 157 | (provide 'wgrep-test) 158 | -------------------------------------------------------------------------------- /wgrep-ag.el: -------------------------------------------------------------------------------- 1 | ;;; wgrep-ag.el --- Writable ag buffer -*- lexical-binding: t -*- 2 | 3 | ;; Copyright (C) 2010-2020,2023 Masahiro Hayashi 4 | 5 | ;; Author: Masahiro Hayashi 6 | ;; Keywords: grep edit extensions 7 | ;; Package-Requires: ((emacs "25.1") (wgrep "3.0.0")) 8 | ;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep-ag.el 9 | ;; Emacs: GNU Emacs 25 or later 10 | ;; Version: 3.0.0 11 | 12 | ;; This program is free software; you can redistribute it and/or 13 | ;; modify it under the terms of the GNU General Public License as 14 | ;; published by the Free Software Foundation; either version 3, or (at 15 | ;; your option) any later version. 16 | 17 | ;; This program is distributed in the hope that it will be useful, but 18 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 | ;; General Public License for more details. 21 | 22 | ;; You should have received a copy of the GNU General Public License 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 25 | ;; Boston, MA 02110-1301, USA. 26 | 27 | ;;; Commentary: 28 | 29 | ;; wgrep-ag allows you to edit a ag buffer and apply those changes to 30 | ;; the file buffer. 31 | 32 | ;;; Install: 33 | 34 | ;; 1. Install ag.el 35 | ;; 36 | ;; https://github.com/Wilfred/ag.el 37 | 38 | ;; 2. Install wgrep.el 39 | 40 | ;; 3. Put this file into load-path'ed directory, and byte compile it if 41 | ;; desired. And put the following expression into your ~/.emacs. 42 | ;; 43 | ;; (autoload 'wgrep-ag-setup "wgrep-ag") 44 | ;; (add-hook 'ag-mode-hook 'wgrep-ag-setup) 45 | 46 | ;;; Usage: 47 | 48 | ;; See wgrep.el 49 | 50 | ;;; Code: 51 | 52 | (require 'wgrep) 53 | 54 | (defvar wgrep-ag-grouped-result-file-regexp "^File:[[:space:]]+\\(.*\\)$" 55 | "Regular expression for the start of results for a file in grouped results. 56 | \"Grouped results\" are what you get from ag.el when 57 | `ag-group-matches' is true or when you call ag with --group.") 58 | 59 | (defvar wgrep-ag-ungrouped-result-regexp 60 | "^\\(.+?\\):\\([[:digit:]]+\\)\\(?:-\\|:[[:digit:]]+:\\)" 61 | "Regular expression for an ungrouped result. 62 | You get \"ungrouped results\" when `ag-group-matches' is false or 63 | when you manage to call ag with --nogroup.") 64 | 65 | (defun wgrep-ag-prepare-header&footer () 66 | (save-excursion 67 | (goto-char (point-min)) 68 | ;; Look for the first useful result line. 69 | (if (re-search-forward (concat wgrep-ag-grouped-result-file-regexp 70 | "\\|" 71 | wgrep-ag-ungrouped-result-regexp)) 72 | (add-text-properties (point-min) (line-beginning-position) 73 | '(read-only t wgrep-header t)) 74 | ;; No results in this buffer, let's mark the whole thing as 75 | ;; header. 76 | (add-text-properties (point-min) (point-max) 77 | '(read-only t wgrep-header t))) 78 | 79 | ;; OK, header dealt with. Now let's try find the footer. 80 | (goto-char (point-max)) 81 | (re-search-backward "^\\(?:-[^:]+?:[[:digit:]]+:[[:digit:]]+:\\)" nil t) 82 | ;; Point is now at the beginning of the result nearest the end 83 | ;; of the buffer, AKA the last result. Move to the start of 84 | ;; the line after the last result, and mark everything from 85 | ;; that line forward as wgrep-footer. If we can't move to the 86 | ;; line after the last line then there apparently is no 87 | ;; footer. 88 | (when (zerop (forward-line 1)) 89 | (add-text-properties (point) (point-max) 90 | '(read-only t wgrep-footer t))))) 91 | 92 | (defun wgrep-ag-parse-command-results () 93 | ;; Note that this function is called with the buffer narrowed to 94 | ;; exclude the header and the footer. (We're going to assert that 95 | ;; fact here, because we use (bobp) result a bit further down to 96 | ;; decide that we're not reading grouped results; see below.) 97 | (unless (bobp) 98 | (error "Expected to be called with point at beginning of buffer")) 99 | (save-excursion 100 | ;; First look for grouped results (`ag-group-matches' is/was 101 | ;; probably true). 102 | (while (re-search-forward wgrep-ag-grouped-result-file-regexp nil t) 103 | ;; Ignore the line that introduces matches from a file, so that 104 | ;; wgrep doesn't let you edit it. 105 | (add-text-properties (match-beginning 0) (match-end 0) 106 | '(wgrep-ignore t)) 107 | (let ((file-name (match-string-no-properties 1))) 108 | ;; Note that I think wgrep uses this property to quickly find 109 | ;; the file it's interested in when searching during some 110 | ;; operation(s). We stick it on the file name in the results 111 | ;; group header. 112 | (add-text-properties (match-beginning 1) (match-end 1) 113 | (list (wgrep-construct-filename-property file-name) 114 | file-name)) 115 | ;; Matches are like: 999:55:line content here 116 | ;; Context lines are like: 999-line content here 117 | ;; 118 | ;; When context is enabled, matches from different parts of 119 | ;; the same file are separated by a line containing just "--". 120 | ;; The group of matches from a single file is terminated by a 121 | ;; blank line. 122 | (while (and (zerop (forward-line 1)) 123 | (looking-at 124 | (concat "^\\([[:digit:]]+\\)\\(?::[[:digit:]]+:\\|-\\)" 125 | "\\|\\(^--$\\)"))) 126 | (if (match-beginning 2) 127 | ;; Ignore "--" line. 128 | (add-text-properties (match-beginning 0) (match-end 0) 129 | '(wgrep-ignore t)) 130 | (add-text-properties (match-beginning 0) (match-end 0) 131 | (list 'wgrep-line-filename file-name 132 | 'wgrep-line-number 133 | (string-to-number (match-string 1)))))))) 134 | (when (bobp) 135 | ;; Search above never moved point, so match non-grouped results 136 | ;; (`ag-group-matches' is/was probably false). 137 | (let (last-file-name) 138 | ;; Matches are like: /foo/bar:999:55:line content here 139 | ;; Context lines are like: /foo/bar:999-line content here 140 | ;; 141 | ;; With context lines, matches from different parts of the 142 | ;; file are separated by a line containing just "--". 143 | (while (re-search-forward (concat wgrep-ag-ungrouped-result-regexp 144 | "\\|\\(^--$\\)") 145 | nil t) 146 | (if (match-beginning 3) 147 | ;; Ignore the "--" separator. 148 | (add-text-properties (match-beginning 0) (match-end 0) 149 | '(wgrep-ignore t)) 150 | (let ((file-name (match-string-no-properties 1)) 151 | (line-number (string-to-number (match-string 2)))) 152 | (unless (equal file-name last-file-name) 153 | ;; This line is a result from a different file than 154 | ;; the last match (or else this is the first match in 155 | ;; the results). Write the special file name property 156 | ;; for wgrep. 157 | (let ((file-name-prop 158 | (wgrep-construct-filename-property file-name))) 159 | (add-text-properties (match-beginning 1) (match-end 1) 160 | (list file-name-prop file-name))) 161 | (setq last-file-name file-name)) 162 | (add-text-properties (match-beginning 0) (match-end 0) 163 | (list 'wgrep-line-filename file-name 164 | 'wgrep-line-number line-number))))))))) 165 | 166 | ;;;###autoload 167 | (defun wgrep-ag-setup () 168 | (set (make-local-variable 'wgrep-header&footer-parser) 169 | 'wgrep-ag-prepare-header&footer) 170 | (set (make-local-variable 'wgrep-results-parser) 171 | 'wgrep-ag-parse-command-results) 172 | (wgrep-setup-internal)) 173 | 174 | ;;;###autoload 175 | (add-hook 'ag-mode-hook 'wgrep-ag-setup) 176 | 177 | ;; For `unload-feature' 178 | (defun wgrep-ag-unload-function () 179 | (remove-hook 'ag-mode-hook 'wgrep-ag-setup)) 180 | 181 | (provide 'wgrep-ag) 182 | 183 | ;;; wgrep-ag.el ends here 184 | -------------------------------------------------------------------------------- /COPYING: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /wgrep.el: -------------------------------------------------------------------------------- 1 | ;;; wgrep.el --- Writable grep buffer -*- lexical-binding: t -*- 2 | 3 | ;; Copyright (C) 2010-2020,2023 Masahiro Hayashi 4 | ;; Copyright (C) 2002-2009 Matsushita Akihisa 5 | 6 | ;; Author: Masahiro Hayashi 7 | ;; Keywords: grep edit extensions 8 | ;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep.el 9 | ;; Emacs: GNU Emacs 25 or later 10 | ;; Package-Requires: ((emacs "25.1")) 11 | ;; Version: 3.0.0 12 | 13 | ;; This program is free software; you can redistribute it and/or 14 | ;; modify it under the terms of the GNU General Public License as 15 | ;; published by the Free Software Foundation; either version 3, or (at 16 | ;; your option) any later version. 17 | 18 | ;; This program is distributed in the hope that it will be useful, but 19 | ;; WITHOUT ANY WARRANTY; without even the implied warranty of 20 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 21 | ;; General Public License for more details. 22 | 23 | ;; You should have received a copy of the GNU General Public License 24 | ;; along with GNU Emacs; see the file COPYING. If not, write to the 25 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 26 | ;; Boston, MA 02110-1301, USA. 27 | 28 | ;;; Commentary: 29 | 30 | ;; ## Summary: 31 | 32 | ;; wgrep allows you to edit a grep buffer and apply those changes to 33 | ;; the file buffer like `sed` interactively. No need to learn sed 34 | ;; script, just learn Emacs. 35 | 36 | ;; ## Install: 37 | 38 | ;; Put this file into load-path'ed directory, and byte compile it if 39 | ;; desired. And put the following expression into your ~/.emacs. 40 | ;; 41 | ;; (require 'wgrep) 42 | 43 | ;; ## Usage: 44 | 45 | ;; You can edit the text in the *grep* buffer after typing `C-c C-p` . 46 | ;; After that the changed text is highlighted. 47 | ;; The following keybindings are defined: 48 | 49 | ;; * `C-c C-e`: Apply the changes to file buffers. 50 | ;; * `C-c C-u`: All changes are unmarked and ignored. 51 | ;; * `C-c C-d`: Mark as delete to current line (including newline). 52 | ;; * `C-c C-r`: Remove the changes in the region (these changes are not 53 | ;; applied to the files. Of course, the remaining 54 | ;; changes can still be applied to the files.) 55 | ;; * `C-c C-p`: Toggle read-only area. 56 | ;; * `C-c C-k`: Discard all changes and exit. 57 | ;; * `C-x C-q`: Exit wgrep mode. 58 | 59 | ;; * To save all buffers that wgrep has changed, run 60 | ;; 61 | ;; M-x wgrep-save-all-buffers 62 | 63 | ;; * To save buffer automatically when `wgrep-finish-edit'. 64 | ;; 65 | ;; (setq wgrep-auto-save-buffer t) 66 | 67 | ;; * You can change the default key binding to switch to wgrep. 68 | ;; 69 | ;; (setq wgrep-enable-key "r") 70 | 71 | ;; * To apply all changes regardless of whether or not buffer is read-only. 72 | ;; 73 | ;; (setq wgrep-change-readonly-file t) 74 | 75 | ;; ### Trivial features: 76 | 77 | ;; - wdired.el like interface. 78 | ;; - Support GNU grep `--context` (`-A` `-B` and `-C`) option. 79 | ;; - Can handle too many files. 80 | ;; - Can handle newline insertion in *grep* buffer. 81 | ;; - Delete whole line include newline. 82 | 83 | ;; ### Similar software: 84 | 85 | ;; [GNU sed](https://www.gnu.org/software/sed/) 86 | ;; [helm-ag](https://github.com/syohex/emacs-helm-ag) has a similar feature. 87 | 88 | ;; ## History: 89 | 90 | ;; This program was forked from Matsushita Akihisa's [grep-edit.el 91 | ;; v2.11](http://www.bookshelf.jp/elc/grep-edit.el). As this website is 92 | ;; now inaccessible, 93 | ;; [emacswiki.org's](https://www.emacswiki.org/emacs/grep-edit.el) copy 94 | ;; may be consulted. 95 | 96 | ;; Imported version can be downloaded from this orphan commit: 97 | ;; https://github.com/mhayashi1120/Emacs-wgrep/tree/import-original-author/ 98 | 99 | ;; Following added implementations and differences. 100 | ;; * Support GNU grep context option -A -B and -C 101 | ;; * Some bugfix. (wrong coloring text etc..) 102 | ;; * wdired.el like interface. 103 | ;; * Remove all advice. 104 | ;; * Bind to local variables. (grep-a-lot.el works well) 105 | ;; * After save buffer, colored face will be removed. 106 | ;; * Change face easy to see. 107 | ;; * Reinforce checking error. 108 | ;; * Support removing whole line include new-line. 109 | 110 | ;;; Code: 111 | 112 | (require 'grep) 113 | 114 | (declare-function image-get-display-property "image-mode.el" ()) 115 | (declare-function image-mode-as-text "image-mode.el" ()) 116 | 117 | (defgroup wgrep nil 118 | "Customize wgrep" 119 | :prefix "wgrep-" 120 | :group 'grep) 121 | 122 | ;;; 123 | ;;; Variable / Constant 124 | ;;; 125 | 126 | ;;;; 127 | ;;;; Customize 128 | ;;;; 129 | 130 | (defcustom wgrep-change-readonly-file nil 131 | "Non-nil means to enable change read-only files." 132 | :group 'wgrep 133 | :type 'boolean) 134 | 135 | (defcustom wgrep-enable-key "\C-c\C-p" 136 | "This variable will be obsoleted in the future release. 137 | Key to enable `wgrep-mode'." 138 | :group 'wgrep 139 | :type 'string) 140 | 141 | (defcustom wgrep-auto-save-buffer nil 142 | "Non-nil means do `basic-save-buffer' automatically while `wgrep-finish-edit'." 143 | :group 'wgrep 144 | :type 'boolean) 145 | 146 | (defcustom wgrep-too-many-file-length 10 147 | "Number to detect as too many files." 148 | :group 'wgrep 149 | :type 'number) 150 | 151 | (defvar wgrep-setup-hook nil 152 | "Hooks to run when setting up wgrep.") 153 | 154 | (defvar wgrep-mode-map nil) 155 | 156 | ;;;; 157 | ;;;; Internal variable 158 | ;;;; 159 | 160 | (defvar wgrep-readonly-state nil) 161 | (make-variable-buffer-local 'wgrep-readonly-state) 162 | 163 | (defvar wgrep-prepared nil) 164 | (make-variable-buffer-local 'wgrep-prepared) 165 | 166 | (defvar wgrep-sibling-buffer nil) 167 | (make-variable-buffer-local 'wgrep-sibling-buffer) 168 | 169 | (defvar wgrep-original-mode-map nil) 170 | (make-variable-buffer-local 'wgrep-original-mode-map) 171 | 172 | (defvar wgrep-inhibit-modification-hook nil) 173 | 174 | (defvar wgrep-auto-apply-disk nil 175 | "Internal use `wgrep-auto-save-buffer' or too many file is editing.") 176 | 177 | (defvar wgrep-acceptable-modes nil) 178 | (make-obsolete 'wgrep-acceptable-modes nil "2.1.1") 179 | 180 | ;; Suppress elint warning 181 | ;; GNU Emacs have this variable at least version 21 or later 182 | (defvar auto-coding-regexp-alist) 183 | 184 | ;;;; 185 | ;;;; Constant 186 | ;;;; 187 | 188 | ;; These regexp come from `grep-regexp-alist' at grep.el 189 | (eval-and-compile 190 | (defconst wgrep-null-file-separator-header-regexp 191 | "\\(?1:[^\0\n]+\\)\\(?:\0\\)\\(?3:[0-9]+\\):") 192 | 193 | (defconst wgrep-colon-file-separator-header-regexp 194 | "\\(?1:[^\n:]+?[^\n/:]\\):[\t ]*\\(?3:[1-9][0-9]*\\)[\t ]*:")) 195 | 196 | ;; Generalized regexp, but wrong matching when colon `:' and null `\0' 197 | ;; is contained in grep result. 198 | (defconst wgrep-default-line-header-regexp 199 | (eval-when-compile 200 | (concat 201 | "^" 202 | "\\(?:" 203 | ;; `--null' argument is used. 204 | wgrep-null-file-separator-header-regexp 205 | "\\|" 206 | ;; Fallback regexp 207 | wgrep-colon-file-separator-header-regexp 208 | "\\)"))) 209 | 210 | ;;;; 211 | ;;;; Error 212 | ;;;; 213 | 214 | (define-error 'wgrep-error "wgrep error") 215 | 216 | ;;;; 217 | ;;;; Overridable functions / regexp 218 | ;;;; 219 | 220 | (defvar wgrep-line-file-regexp wgrep-default-line-header-regexp 221 | "Regexp that match to line header of grep result. 222 | 223 | That capture 1: filename 3: line-number 224 | End of this match equals start of file contents. 225 | ") 226 | 227 | (defvar wgrep-results-parser 'wgrep-parse-command-results 228 | "This function parse line oriented command output and set following properties. 229 | `wgrep-line-filename', `wgrep-line-number', `wgrep-ignore' and 230 | `wgrep-construct-filename-property' function construct the property name with 231 | `wgrep-line-filename' and the value is same. This property is used for searching 232 | correct point of filename. 233 | Not like `wgrep-header&footer-parser' should not set `read-only' property.") 234 | 235 | ;; Previously named `wgrep-header/footer-parser` this name violate `package-lint` 236 | ;; conventions. 237 | (defvar wgrep-header&footer-parser 'wgrep-prepare-header&footer 238 | "This function should set text properties `read-only' and `wgrep-header' to 239 | non editable region.") 240 | 241 | ;;; 242 | ;;; Basic utilities 243 | ;;; 244 | 245 | ;;;; 246 | ;;;; misc 247 | ;;;; 248 | 249 | ;;Hack function 250 | (defun wgrep-string-replace-bom (string cs) 251 | (let ((regexp (car (rassq (coding-system-base cs) auto-coding-regexp-alist))) 252 | ;;TODO check ack-grep 253 | ;; FIXME: `find-operation-coding-system' is not exactly correct. 254 | ;; However almost case is ok like this bom function. 255 | ;; e.g. (let ((default-process-coding-system 'some-coding)) 256 | ;; (call-interactively 'grep)) 257 | (grep-cs (or (find-operation-coding-system 'call-process grep-program) 258 | (terminal-coding-system))) 259 | str) 260 | (if (and regexp 261 | (setq str (encode-coding-string string grep-cs)) 262 | (string-match regexp str)) 263 | (decode-coding-string (substring str (match-end 0)) cs) 264 | string))) 265 | 266 | (defun wgrep-delete-whole-line () 267 | (delete-region (line-beginning-position) 268 | (line-beginning-position 2))) 269 | 270 | (defun wgrep-goto-line (line) 271 | (goto-char (point-min)) 272 | (forward-line (1- line))) 273 | 274 | (defun wgrep-process-exited-p () 275 | (let ((proc (get-buffer-process (current-buffer)))) 276 | (or (null proc) 277 | (eq (process-status proc) 'exit)))) 278 | 279 | ;;;; 280 | ;;;; error 281 | ;;;; 282 | 283 | (defun wgrep-check-file (file) 284 | (unless (file-exists-p file) 285 | (signal 'wgrep-error (list "File does not exist."))) 286 | (unless (file-writable-p file) 287 | (signal 'wgrep-error (list "File is not writable.")))) 288 | 289 | ;;;; 290 | ;;;; overlay 291 | ;;;; 292 | 293 | (defun wgrep-cleanup-overlays (beg end) 294 | (dolist (ov (overlays-in beg end)) 295 | (when (overlay-get ov 'wgrep) 296 | (delete-overlay ov)))) 297 | 298 | (defun wgrep-make-overlay (beg end) 299 | (let ((o (make-overlay beg end nil nil t))) 300 | (overlay-put o 'wgrep t) 301 | o)) 302 | 303 | (defun wgrep-file-overlays () 304 | (save-restriction 305 | (widen) 306 | (let (res) 307 | (dolist (ov (overlays-in (point-min) (point-max))) 308 | (when (overlay-get ov 'wgrep) 309 | (setq res (cons ov res)))) 310 | (nreverse res)))) 311 | 312 | (defun wgrep-edit-field-overlays () 313 | (let (res) 314 | (dolist (ov (overlays-in (point-min) (point-max))) 315 | (when (overlay-get ov 'wgrep-changed) 316 | (setq res (cons ov res)))) 317 | (sort res (lambda (x y) (< (overlay-start x) (overlay-start y)))))) 318 | 319 | ;;; 320 | ;;; grep result handler 321 | ;;; 322 | 323 | (defun wgrep-construct-filename-property (filename) 324 | (intern (format "wgrep-fn-%s" filename))) 325 | 326 | (defun wgrep-goto-grep-line (file number) 327 | (let ((first (point)) 328 | (fprop (wgrep-construct-filename-property file)) 329 | fn next) 330 | (catch 'found 331 | ;; FIXME 332 | ;; In a huge buffer, `next-single-property-change' loop make 333 | ;; slow down the program. 334 | ;; 1. sketchy move by filename (wgrep-fn-* property). 335 | ;; 2. search filename and line-number in text property. 336 | ;; 3. return to 1. while search is done or EOB. 337 | 338 | (goto-char (point-min)) 339 | 340 | (while (setq next (next-single-property-change (point) fprop)) 341 | (goto-char next) 342 | (while (and (not (eobp)) 343 | (or (null (setq fn (get-text-property 344 | (line-beginning-position) 345 | 'wgrep-line-filename))) 346 | (string= fn file))) 347 | (when fn 348 | (let ((num (get-text-property (point) 'wgrep-line-number)) 349 | (start (next-single-property-change (point) 'wgrep-line-number))) 350 | (when (eq number num) 351 | (goto-char start) 352 | (throw 'found t)))) 353 | (forward-line 1))) 354 | (goto-char first) 355 | nil))) 356 | 357 | (defun wgrep-get-old-text (file number) 358 | (when (and wgrep-sibling-buffer 359 | (buffer-live-p wgrep-sibling-buffer)) 360 | (with-current-buffer wgrep-sibling-buffer 361 | (when (wgrep-goto-grep-line file number) 362 | (buffer-substring-no-properties 363 | (point) (line-end-position)))))) 364 | 365 | ;;; 366 | ;;; Prepare and parse grep <-> wgrep 367 | ;;; 368 | 369 | (defun wgrep-to-original-mode () 370 | (kill-local-variable 'query-replace-skip-read-only) 371 | (remove-hook 'after-change-functions 'wgrep-after-change-function t) 372 | ;; do not remove `wgrep-maybe-echo-error-at-point' that display 373 | ;; errors at point 374 | (use-local-map wgrep-original-mode-map) 375 | (set-buffer-modified-p nil) 376 | (setq buffer-undo-list nil) 377 | (setq buffer-read-only t)) 378 | 379 | (defun wgrep-goto-first-found () 380 | (let ((header (previous-single-property-change (point-max) 'wgrep-header))) 381 | (cond 382 | (header 383 | (goto-char header) 384 | header) 385 | (t 386 | (goto-char (point-min)) 387 | (point))))) 388 | 389 | (defun wgrep-goto-end-of-found () 390 | (let ((footer (next-single-property-change (point-min) 'wgrep-footer))) 391 | (cond 392 | (footer 393 | (goto-char footer) 394 | footer) 395 | (t 396 | (goto-char (point-max)) 397 | (point-max))))) 398 | 399 | (defun wgrep-cleanup-temp-buffer () 400 | "Cleanup temp buffer in *grep* buffer." 401 | (let ((origin-buffer (current-buffer))) 402 | (dolist (buf (buffer-list)) 403 | (with-current-buffer buf 404 | (when (eq origin-buffer wgrep-sibling-buffer) 405 | (kill-buffer buf))))) 406 | (setq wgrep-sibling-buffer nil)) 407 | 408 | (defun wgrep-clone-to-temp-buffer () 409 | (wgrep-cleanup-temp-buffer) 410 | (let ((grepbuf (current-buffer)) 411 | (tmpbuf (generate-new-buffer " *wgrep temp* "))) 412 | (setq wgrep-sibling-buffer tmpbuf) 413 | (add-hook 'kill-buffer-hook 'wgrep-cleanup-temp-buffer nil t) 414 | (append-to-buffer tmpbuf (point-min) (point-max)) 415 | (with-current-buffer tmpbuf 416 | (setq wgrep-sibling-buffer grepbuf)) 417 | tmpbuf)) 418 | 419 | (defun wgrep-set-readonly-area (state) 420 | (let ((inhibit-read-only t) 421 | (wgrep-inhibit-modification-hook t) 422 | pos start end) 423 | (save-excursion 424 | ;; set readonly grep result filename 425 | (setq pos (point-min)) 426 | (while (setq start (next-single-property-change 427 | pos 'wgrep-line-filename)) 428 | (setq end (next-single-property-change 429 | start 'wgrep-line-filename)) 430 | (put-text-property start end 'read-only state) 431 | (put-text-property (1- end) end 'rear-nonsticky t) 432 | ;; set readonly all newline at end of grep line 433 | (when (eq (char-before start) ?\n) 434 | (put-text-property (1- start) start 'read-only state)) 435 | (setq pos end)) 436 | (setq pos (point-min)) 437 | (while (setq start (next-single-property-change 438 | pos 'wgrep-ignore)) 439 | (setq end (next-single-property-change 440 | start 'wgrep-ignore)) 441 | (put-text-property start end 'read-only state) 442 | ;; set readonly all newline at end of grep line 443 | (when (eq (char-before start) ?\n) 444 | (put-text-property (1- start) start 'read-only state)) 445 | (setq pos end)) 446 | ;; set readonly last of grep line 447 | (let ((footer (or (next-single-property-change (point-min) 'wgrep-footer) 448 | ;; to consider empty footer. 449 | (point-max)))) 450 | (when (eq (char-before footer) ?\n) 451 | (put-text-property (1- footer) footer 'read-only state)))) 452 | (setq wgrep-readonly-state state))) 453 | 454 | (defun wgrep-prepare-context () 455 | (save-restriction 456 | (let ((start (wgrep-goto-first-found)) 457 | (end (wgrep-goto-end-of-found))) 458 | (narrow-to-region start end) 459 | (goto-char (point-min)) 460 | (funcall wgrep-results-parser)))) 461 | 462 | ;; -A -B -C output may be misunderstood and set read-only. 463 | ;; Context match break font-lock if context have at least two `:'. 464 | ;; e.g. 465 | ;; filename-1-2010-01-01 23:59:99 466 | ;; filename:2:hoge 467 | ;; filename-3-20:10:25 468 | (defun wgrep-prepare-context-while (filename line direction fprop flen) 469 | (let* ((next (+ direction line)) 470 | (fregexp (regexp-quote filename))) 471 | (forward-line direction) 472 | (while (looking-at (format "^%s[-\0]%d-" fregexp next)) 473 | (let ((start (match-beginning 0)) 474 | (end (match-end 0)) 475 | (bol (line-beginning-position)) 476 | (eol (line-end-position))) 477 | (put-text-property start end 'wgrep-line-filename filename) 478 | (put-text-property start end 'wgrep-line-number next) 479 | (put-text-property start (+ start flen) fprop filename) 480 | (remove-text-properties bol eol '(wgrep-ignore)) 481 | (forward-line direction) 482 | (setq next (+ direction next)))))) 483 | 484 | (defun wgrep-parse-command-results () 485 | (let ((cache (make-hash-table))) 486 | (while (not (eobp)) 487 | (cond 488 | ((looking-at wgrep-line-file-regexp) 489 | (let* ((fn (match-string-no-properties 1)) 490 | (line (string-to-number (match-string 3))) 491 | (start (match-beginning 0)) 492 | (end (match-end 0)) 493 | (fprop (wgrep-construct-filename-property fn)) 494 | (flen (length fn))) 495 | ;; check relative path grep result 496 | ;; grep result may be --context result with number between 2 colon. 497 | ;; ./filename-1-:10: 498 | ;; that make misunderstand font-locking 499 | ;; check file existence decrease risk of the misunderstanding. 500 | (when (or (gethash fn cache nil) 501 | (and (file-exists-p fn) 502 | (puthash fn t cache))) 503 | (put-text-property start end 'wgrep-line-filename fn) 504 | (put-text-property start end 'wgrep-line-number line) 505 | (put-text-property start (+ start flen) fprop fn) 506 | ;; handle backward and forward following options. 507 | ;; -A (--after-context) -B (--before-context) -C (--context) 508 | (save-excursion 509 | (wgrep-prepare-context-while fn line -1 fprop flen)) 510 | (wgrep-prepare-context-while fn line 1 fprop flen) 511 | ;; end of context output `--'. 512 | (forward-line -1)))) 513 | (t 514 | ;; Add property but this may be removed by `wgrep-prepare-context-while' 515 | (put-text-property (line-beginning-position) 516 | (line-end-position) 517 | 'wgrep-ignore t))) 518 | (forward-line 1)))) 519 | 520 | (defun wgrep-current-file-and-linum () 521 | (save-excursion 522 | (forward-line 0) 523 | (let ((fn (get-text-property (point) 'wgrep-line-filename)) 524 | (linum (get-text-property (point) 'wgrep-line-number))) 525 | (when (and fn linum) 526 | (list fn linum))))) 527 | 528 | (defun wgrep-restore-from-temp-buffer () 529 | (cond 530 | ((and wgrep-sibling-buffer 531 | (buffer-live-p wgrep-sibling-buffer)) 532 | (let ((grepbuf (current-buffer)) 533 | (tmpbuf wgrep-sibling-buffer) 534 | (header (wgrep-current-file-and-linum)) 535 | (savedc (current-column)) 536 | (savedp (point)) 537 | (inhibit-read-only t) 538 | (wgrep-inhibit-modification-hook t) 539 | buffer-read-only) 540 | (erase-buffer) 541 | (with-current-buffer tmpbuf 542 | (append-to-buffer grepbuf (point-min) (point-max))) 543 | (goto-char (point-min)) 544 | ;; restore previous cursor 545 | (or (and header 546 | (apply 'wgrep-goto-grep-line header) 547 | (move-to-column savedc)) 548 | (goto-char (min (point-max) savedp))) 549 | (wgrep-cleanup-temp-buffer))) 550 | (t 551 | ;; non fatal error 552 | (message "Error! Saved buffer is unavailable.")))) 553 | 554 | (defun wgrep-prepare-to-edit () 555 | (unless wgrep-prepared 556 | (save-excursion 557 | (let ((inhibit-read-only t) 558 | (wgrep-inhibit-modification-hook t) 559 | buffer-read-only) 560 | (funcall (or wgrep-header&footer-parser 561 | ;; TODO FIXME: workaround compat for previous code. 562 | (and (boundp 'wgrep-header/footer-parser) 563 | wgrep-header/footer-parser))) 564 | (wgrep-prepare-context) 565 | (setq wgrep-prepared t))))) 566 | 567 | (defun wgrep-prepare-header&footer () 568 | (let (beg end) 569 | ;; Set read-only grep result header 570 | (goto-char (point-min)) 571 | (setq beg (point-min)) 572 | ;; See `compilation-start' 573 | (forward-line 4) 574 | (setq end (point)) 575 | (put-text-property beg end 'read-only t) 576 | (put-text-property beg end 'wgrep-header t) 577 | ;; Set read-only grep result footer 578 | (goto-char (point-max)) 579 | (forward-line -1) 580 | (when (re-search-backward "^$" end t) 581 | (setq beg (point)) 582 | (setq end (point-max)) 583 | (when beg 584 | (put-text-property beg end 'read-only t) 585 | (put-text-property beg end 'wgrep-footer t))))) 586 | 587 | (defun wgrep-set-header&footer-read-only (state) 588 | (let ((inhibit-read-only t) 589 | (wgrep-inhibit-modification-hook t)) 590 | ;; header 591 | (let ((header-end (next-single-property-change (point-min) 'wgrep-header))) 592 | (when header-end 593 | (put-text-property (point-min) header-end 'read-only state))) 594 | ;; footer 595 | (let ((footer-beg (next-single-property-change (point-min) 'wgrep-footer))) 596 | (when footer-beg 597 | (put-text-property footer-beg (point-max) 'read-only state))))) 598 | 599 | ;;; 600 | ;;; Editing handlers 601 | ;;; 602 | 603 | ;; get overlay BEG and END is passed by `after-change-functions' 604 | (defun wgrep-editing-overlay (&optional start end) 605 | (let ((beg (or start (line-beginning-position))) 606 | (fin (or end (line-end-position))) 607 | ov bol eol 608 | ;; beginning/end of grep 609 | bog eog) 610 | (goto-char beg) 611 | (setq bol (line-beginning-position)) 612 | (goto-char fin) 613 | (setq eol (line-end-position)) 614 | (catch 'done 615 | (dolist (o (overlays-in bol eol)) 616 | ;; find overlay that have changed by user. 617 | (when (overlay-get o 'wgrep-changed) 618 | (setq ov o) 619 | (throw 'done o)))) 620 | (if ov 621 | (setq bog (min beg (overlay-start ov)) 622 | eog (max (overlay-end ov) fin)) 623 | (setq bog bol 624 | eog eol)) 625 | (goto-char bog) 626 | (cond 627 | ;; When handling whole line, BOL equal beginning of edit. 628 | ((and (null ov) start (= bog start))) 629 | ((get-text-property (point) 'wgrep-line-filename) 630 | (let* ((header-end 631 | (next-single-property-change (point) 'wgrep-line-filename nil eol)) 632 | (filename (get-text-property (point) 'wgrep-line-filename)) 633 | (linum (get-text-property (point) 'wgrep-line-number)) 634 | (value (buffer-substring-no-properties header-end eog)) 635 | contents-begin) 636 | (goto-char header-end) 637 | (setq contents-begin (point-marker)) 638 | ;; create editing overlay 639 | (cond 640 | ((null ov) 641 | (let ((old (wgrep-get-old-text filename linum))) 642 | (setq ov (wgrep-make-overlay bog eog)) 643 | (overlay-put ov 'wgrep-contents-begin contents-begin) 644 | (overlay-put ov 'wgrep-filename filename) 645 | (overlay-put ov 'wgrep-linum linum) 646 | (overlay-put ov 'wgrep-changed t) 647 | (overlay-put ov 'priority 0) 648 | (overlay-put ov 'evaporate t) 649 | (overlay-put ov 'wgrep-old-text old))) 650 | (t 651 | (move-overlay ov bog eog))) 652 | (overlay-put ov 'wgrep-edit-text value)))) 653 | ov)) 654 | 655 | (defun wgrep-after-change-function (beg end _leng-before) 656 | (cond 657 | (wgrep-inhibit-modification-hook nil) 658 | ((= (point-min) (point-max)) 659 | ;; cleanup when first executing 660 | (wgrep-cleanup-overlays (point-min) (point-max))) 661 | (t 662 | (wgrep-put-change-face beg end)))) 663 | 664 | (defun wgrep-put-change-face (beg end) 665 | (save-excursion 666 | ;; `looking-at' may destroy match data while replace by regexp. 667 | (save-match-data 668 | (let ((ov (wgrep-editing-overlay beg end))) 669 | ;; delete overlay if text is same as old value. 670 | (cond 671 | ;; not a valid point 672 | ((null ov)) 673 | ((string= (overlay-get ov 'wgrep-old-text) 674 | (overlay-get ov 'wgrep-edit-text)) 675 | ;; back to unchanged 676 | (delete-overlay ov)) 677 | (t 678 | (overlay-put ov 'face 'wgrep-face))))))) 679 | 680 | ;;; 681 | ;;; Save grep buffer to file buffer/disk 682 | ;;; 683 | 684 | (defun wgrep-display-physical-data () 685 | (cond 686 | ;; `funcall' is a trick to suppress the elint warnings. 687 | ((derived-mode-p 'image-mode) 688 | ;; toggle to raw data if buffer has image. 689 | (when (image-get-display-property) 690 | (image-mode-as-text))) 691 | (t nil))) 692 | 693 | (defun wgrep-set-result (ov face &optional message) 694 | (overlay-put ov 'face face) 695 | (overlay-put ov 'priority 1) 696 | (overlay-put ov 'wgrep-reject-message message)) 697 | 698 | (defun wgrep-put-done-result (ov) 699 | (wgrep-set-result ov 'wgrep-done-face)) 700 | 701 | (defun wgrep-put-reject-result (ov error-data) 702 | (let ((message (mapconcat (lambda (x) (format "%s" x)) error-data " "))) 703 | (wgrep-set-result ov 'wgrep-reject-face message))) 704 | 705 | (defun wgrep-put-reject-result-all (editor error-data) 706 | (dolist (edit (cdr editor)) 707 | (let ((result (nth 3 edit))) 708 | (wgrep-put-reject-result result error-data)))) 709 | 710 | (defun wgrep-after-save-hook () 711 | (remove-hook 'after-save-hook 'wgrep-after-save-hook t) 712 | (dolist (ov (wgrep-file-overlays)) 713 | (delete-overlay ov))) 714 | 715 | (defun wgrep-put-overlay-to-file-buffer (beg end) 716 | "*Highlight the changes in the file" 717 | (let ((ov 718 | (catch 'done 719 | (dolist (o (overlays-in beg end)) 720 | (when (overlay-get o 'wgrep) 721 | (move-overlay o beg end) 722 | (throw 'done o))) 723 | (wgrep-make-overlay beg end)))) 724 | (overlay-put ov 'face 'wgrep-file-face) 725 | (overlay-put ov 'priority 0) 726 | (add-hook 'after-save-hook 'wgrep-after-save-hook nil t) 727 | ov)) 728 | 729 | (defun wgrep-let-destructive-overlay (ov) 730 | (dolist (prop '(modification-hooks insert-in-front-hooks insert-behind-hooks)) 731 | (overlay-put 732 | ov prop 733 | `(,(lambda (ov after-p &rest _ignore) 734 | (when after-p 735 | (delete-overlay ov))))))) 736 | 737 | (defun wgrep-replace-to-new-line (new-text) 738 | ;; delete grep extracted region (restricted to a line) 739 | (delete-region (line-beginning-position) (line-end-position)) 740 | (let ((beg (point))) 741 | (insert new-text) 742 | (let* ((end (point)) 743 | ;; highlight the changed line 744 | (ov (wgrep-put-overlay-to-file-buffer beg end))) 745 | ;; make overlay volatile. 746 | (wgrep-let-destructive-overlay ov)))) 747 | 748 | (defun wgrep-flush-whole-line () 749 | (wgrep-put-overlay-to-file-buffer 750 | (line-beginning-position) (line-end-position)) 751 | (wgrep-delete-whole-line)) 752 | 753 | ;; EDITOR ::= FILE (absolute-path) . EDITS 754 | ;; EDITS ::= EDIT [...] 755 | ;; EDIT ::= linum-or-marker old-text new-text result-overlay edit-field-overlay 756 | (defun wgrep-gather-editor () 757 | (let (res) 758 | (dolist (edit-field (wgrep-edit-field-overlays)) 759 | (goto-char (overlay-start edit-field)) 760 | (forward-line 0) 761 | (cond 762 | ;; ignore removed line or removed overlay 763 | ((eq (overlay-start edit-field) (overlay-end edit-field))) 764 | ((get-text-property (point) 'wgrep-line-filename) 765 | (let* ((name (get-text-property (point) 'wgrep-line-filename)) 766 | (linum (get-text-property (point) 'wgrep-line-number)) 767 | (start (next-single-property-change 768 | (point) 'wgrep-line-filename nil (line-end-position))) 769 | (file (expand-file-name name default-directory)) 770 | (old (overlay-get edit-field 'wgrep-old-text)) 771 | (new (overlay-get edit-field 'wgrep-edit-text)) 772 | result) 773 | ;; wgrep-result overlay show the committing of this editing 774 | (catch 'done 775 | (dolist (o (overlays-in (overlay-start edit-field) (overlay-end edit-field))) 776 | (when (overlay-get o 'wgrep-result) 777 | ;; get existing overlay 778 | (setq result o) 779 | (throw 'done t))) 780 | ;; create overlay to show result of committing 781 | (setq result (wgrep-make-overlay start (overlay-end edit-field))) 782 | (overlay-put result 'wgrep-result t)) 783 | (setq res 784 | (cons 785 | (list file (list linum old new result edit-field)) 786 | res)))))) 787 | (nreverse res))) 788 | 789 | (defun wgrep-compute-transaction () 790 | (let ((editors (wgrep-gather-editor)) 791 | editor-group tran) 792 | (dolist (editor editors) 793 | (let* ((file (car editor)) 794 | (edits (cdr editor)) 795 | (editor-cache (assoc file editor-group))) 796 | (unless editor-cache 797 | (setq editor-cache (cons file nil)) 798 | (setq editor-group (cons editor-cache editor-group))) 799 | ;; construct with current settings 800 | (setcdr editor-cache (append (cdr editor-cache) edits)))) 801 | (setq editor-group (nreverse editor-group)) 802 | 803 | ;; Check file accessibility 804 | (dolist (editor editor-group) 805 | (let ((file (car editor))) 806 | (condition-case err 807 | (progn 808 | (wgrep-check-file file) 809 | (setq tran (cons editor tran))) 810 | (wgrep-error 811 | (wgrep-put-reject-result-all editor (cdr err)))))) 812 | 813 | (nreverse tran))) 814 | 815 | (defun wgrep-compute-linum-to-marker (edits) 816 | ;; Convert linum to marker. 817 | ;; When new text contains newline destroy linum access. 818 | (dolist (edit edits) 819 | (let ((linum (car edit))) 820 | ;; get a marker 821 | (wgrep-goto-line linum) 822 | (setcar edit (point-marker))))) 823 | 824 | (defun wgrep-commit-edits (editor) 825 | (let ((file (car editor)) 826 | (edits (cdr editor))) 827 | (wgrep-compute-linum-to-marker edits) 828 | (let ((done 0) 829 | (first-result nil) 830 | (inhibit-read-only wgrep-change-readonly-file)) 831 | (dolist (edit edits) 832 | (let ((marker (nth 0 edit)) 833 | (old (nth 1 edit)) 834 | (new (nth 2 edit)) 835 | (result-ov (nth 3 edit)) 836 | (edit-ov (nth 4 edit))) 837 | (condition-case err 838 | (progn 839 | (unless first-result 840 | (setq first-result result-ov)) 841 | (wgrep-apply-change marker old new) 842 | (wgrep-put-done-result result-ov) 843 | (delete-overlay edit-ov) 844 | (setq done (1+ done))) 845 | (error 846 | (wgrep-put-reject-result result-ov (cdr err)))))) 847 | (cond 848 | ((or (not wgrep-auto-apply-disk) 849 | (= done 0))) 850 | (buffer-file-name 851 | (basic-save-buffer)) 852 | (t 853 | (let ((coding-system-for-write buffer-file-coding-system)) 854 | (write-region (point-min) (point-max) file nil 'no-msg)))) 855 | (list done first-result)))) 856 | 857 | (defun wgrep-commit-file (editor) 858 | ;; Apply EDITOR to file/buffer. See `wgrep-compute-transaction'. 859 | ;; Return succeeded count and first result overlay in *grep* buffer. 860 | (let* ((file (car editor)) 861 | (open-buffer (get-file-buffer file)) 862 | (buffer 863 | (cond 864 | (open-buffer open-buffer) 865 | (wgrep-auto-apply-disk 866 | (let ((buf (generate-new-buffer "*TMP *"))) 867 | (with-current-buffer buf 868 | ;; To detect coding-system and set `buffer-file-coding-system'. 869 | (insert-file-contents file)) 870 | buf)) 871 | (t 872 | (find-file-noselect file))))) 873 | (unwind-protect 874 | (with-current-buffer buffer 875 | (save-restriction 876 | (widen) 877 | (wgrep-display-physical-data) 878 | 879 | (cond 880 | ((and (not wgrep-change-readonly-file) 881 | buffer-read-only) 882 | (wgrep-put-reject-result-all 883 | editor 884 | (list (format "Buffer \"%s\" is read-only." (buffer-name)))) 885 | (list 0 nil)) 886 | (t 887 | (wgrep-commit-edits editor))))) 888 | (when wgrep-auto-apply-disk 889 | (when (null open-buffer) 890 | (kill-buffer buffer)))))) 891 | 892 | (defun wgrep-apply-change (marker old new) 893 | "The changes in the *grep* buffer are applied to the file. 894 | NEW may be nil this means deleting whole line." 895 | (let ((coding buffer-file-coding-system)) 896 | (goto-char marker) 897 | ;; check BOM 898 | (when (and (= (point-min-marker) marker) 899 | coding 900 | (coding-system-get coding :bom)) 901 | (setq old (wgrep-string-replace-bom old coding)) 902 | (when new 903 | (setq new (wgrep-string-replace-bom new coding)))) 904 | ;; Check buffer line was modified after execute grep. 905 | (unless (string= old 906 | (buffer-substring-no-properties 907 | (line-beginning-position) (line-end-position))) 908 | (signal 'wgrep-error (list "Buffer was changed after grep."))) 909 | (cond 910 | (new 911 | (wgrep-replace-to-new-line new)) 912 | (t 913 | ;; new nil means flush whole line. 914 | (wgrep-flush-whole-line))))) 915 | 916 | ;;; 917 | ;;; UI 918 | ;;; 919 | 920 | (defface wgrep-face 921 | '((((class color) 922 | (background dark)) 923 | (:background "SlateGray1" :foreground "Black")) 924 | (((class color) 925 | (background light)) 926 | (:background "ForestGreen" :foreground "white")) 927 | (t 928 | ())) 929 | "*Face used for the changed text in the grep buffer." 930 | :group 'wgrep) 931 | 932 | (defface wgrep-delete-face 933 | '((((class color) 934 | (background dark)) 935 | (:background "SlateGray1" :foreground "pink")) 936 | (((class color) 937 | (background light)) 938 | (:background "ForestGreen" :foreground "pink")) 939 | (t 940 | ())) 941 | "*Face used for the deleted whole line in the grep buffer." 942 | :group 'wgrep) 943 | 944 | (defface wgrep-file-face 945 | '((((class color) 946 | (background dark)) 947 | (:background "gray30" :foreground "white")) 948 | (((class color) 949 | (background light)) 950 | (:background "ForestGreen" :foreground "white")) 951 | (t 952 | ())) 953 | "*Face used for the changed text in the file buffer." 954 | :group 'wgrep) 955 | 956 | (defface wgrep-reject-face 957 | '((((class color) 958 | (background dark)) 959 | (:foreground "HotPink" :weight bold)) 960 | (((class color) 961 | (background light)) 962 | (:foreground "Red" :weight bold)) 963 | (t 964 | ())) 965 | "*Face used for the line in the grep buffer that can not be applied to 966 | a file." 967 | :group 'wgrep) 968 | 969 | (defface wgrep-done-face 970 | '((((class color) 971 | (background dark)) 972 | (:foreground "LightSkyBlue")) 973 | (((class color) 974 | (background light)) 975 | (:foreground "Blue")) 976 | (t 977 | ())) 978 | "*Face used for the line in the grep buffer that can be applied to a file." 979 | :group 'wgrep) 980 | 981 | (defun wgrep-maybe-echo-error-at-point () 982 | (when (null (current-message)) 983 | (let ((ov (catch 'found 984 | (dolist (o (overlays-in (line-beginning-position) 985 | (line-end-position))) 986 | (when (overlay-get o 'wgrep-reject-message) 987 | (throw 'found o)))))) 988 | (when ov 989 | (let (message-log-max) 990 | (message "%s" (overlay-get ov 'wgrep-reject-message))))))) 991 | 992 | ;;; 993 | ;;; Commands 994 | ;;; 995 | 996 | (defun wgrep-finish-edit () 997 | "Apply changes to file buffers. 998 | These changes are not immediately saved to disk unless 999 | `wgrep-auto-save-buffer' is non-nil." 1000 | (interactive) 1001 | (let* ((tran (wgrep-compute-transaction)) 1002 | (all-length (length tran)) 1003 | (wgrep-auto-apply-disk nil) 1004 | (done 0)) 1005 | (cond 1006 | (wgrep-auto-save-buffer 1007 | (setq wgrep-auto-apply-disk t)) 1008 | ((> all-length wgrep-too-many-file-length) 1009 | (when (y-or-n-p (eval-when-compile 1010 | (concat 1011 | "Edited files are too many." 1012 | " Apply the changes to disk with non-confirmation?"))) 1013 | (setq wgrep-auto-apply-disk t)))) 1014 | (while tran 1015 | (let* ((editor (car tran)) 1016 | (committed (wgrep-commit-file editor)) 1017 | (count (nth 0 committed)) 1018 | (result (nth 1 committed))) 1019 | (when result 1020 | (goto-char (overlay-start result)) 1021 | (forward-line 0)) 1022 | (setq done (+ done count)) 1023 | (setq tran (cdr tran)) 1024 | (let (message-log-max) 1025 | (message "Writing %d files, %d files are left..." 1026 | all-length (length tran))) 1027 | (redisplay t))) 1028 | (wgrep-cleanup-temp-buffer) 1029 | (wgrep-to-original-mode) 1030 | (let ((msg (format "(%d changed)" done)) 1031 | (ovs (wgrep-edit-field-overlays))) 1032 | (cond 1033 | ((null ovs) 1034 | (if (= done 0) 1035 | (message "(No changes to be performed)") 1036 | (message "Successfully finished. %s" msg))) 1037 | ((= (length ovs) 1) 1038 | (message "There is an unapplied change. %s" msg)) 1039 | (t 1040 | (message "There are %d unapplied changes. %s" 1041 | (length ovs) msg)))))) 1042 | 1043 | (defun wgrep-exit () 1044 | "Return to original mode." 1045 | (interactive) 1046 | (if (and (buffer-modified-p) 1047 | (y-or-n-p (format "Buffer %s modified; save changes? " 1048 | (current-buffer)))) 1049 | (wgrep-finish-edit) 1050 | (wgrep-abort-changes))) 1051 | 1052 | (defun wgrep-abort-changes () 1053 | "Discard all changes and return to original mode." 1054 | (interactive) 1055 | (wgrep-cleanup-overlays (point-min) (point-max)) 1056 | (wgrep-restore-from-temp-buffer) 1057 | (wgrep-to-original-mode) 1058 | (message "Changes discarded")) 1059 | 1060 | (defun wgrep-remove-change (beg end) 1061 | "Remove changes in the region between BEG and END." 1062 | (interactive "r") 1063 | (wgrep-cleanup-overlays beg end) 1064 | (setq mark-active nil)) 1065 | 1066 | (defun wgrep-remove-all-change () 1067 | "Remove changes in the whole buffer." 1068 | (interactive) 1069 | (wgrep-cleanup-overlays (point-min) (point-max))) 1070 | 1071 | (defun wgrep-toggle-readonly-area () 1072 | "Toggle read-only area to remove a whole line. 1073 | 1074 | See the following example: you obviously don't want to edit the first line. 1075 | If grep matches a lot of lines, it's hard to edit the grep buffer. 1076 | After toggling to editable, you can call 1077 | `delete-matching-lines', `delete-non-matching-lines'. 1078 | 1079 | Example: 1080 | ---------------------------------------------- 1081 | ./.svn/text-base/some.el.svn-base:87:(hoge) 1082 | ./some.el:87:(hoge) 1083 | ---------------------------------------------- 1084 | " 1085 | (interactive) 1086 | (let ((modified (buffer-modified-p)) 1087 | (read-only (not wgrep-readonly-state))) 1088 | (wgrep-set-readonly-area read-only) 1089 | (wgrep-set-header&footer-read-only read-only) 1090 | (set-buffer-modified-p modified) 1091 | (if wgrep-readonly-state 1092 | (message "Removing the whole line is now disabled.") 1093 | (message "Removing the whole line is now enabled.")))) 1094 | 1095 | (defun wgrep-change-to-wgrep-mode () 1096 | "Change to wgrep mode. 1097 | 1098 | When the *grep* buffer is huge, this might freeze your Emacs 1099 | for several minutes. 1100 | " 1101 | (interactive) 1102 | (unless (wgrep-process-exited-p) 1103 | (error "Active process working")) 1104 | (wgrep-prepare-to-edit) 1105 | (wgrep-set-readonly-area t) 1106 | (set (make-local-variable 'query-replace-skip-read-only) t) 1107 | (add-hook 'after-change-functions 'wgrep-after-change-function nil t) 1108 | (add-hook 'post-command-hook 'wgrep-maybe-echo-error-at-point nil t) 1109 | (use-local-map wgrep-mode-map) 1110 | (buffer-disable-undo) 1111 | (wgrep-clone-to-temp-buffer) 1112 | (setq buffer-read-only nil) 1113 | (buffer-enable-undo) 1114 | ;; restore modified status 1115 | (set-buffer-modified-p (wgrep-edit-field-overlays)) 1116 | (setq buffer-undo-list nil) 1117 | (message "%s" (substitute-command-keys 1118 | "Press \\[wgrep-finish-edit] when finished \ 1119 | or \\[wgrep-abort-changes] to abort changes."))) 1120 | 1121 | (defun wgrep-save-all-buffers () 1122 | "Save the buffers that wgrep changed." 1123 | (interactive) 1124 | (let ((count 0)) 1125 | (dolist (b (buffer-list)) 1126 | (with-current-buffer b 1127 | (let ((ovs (wgrep-file-overlays))) 1128 | (when (and ovs (buffer-modified-p)) 1129 | (basic-save-buffer) 1130 | (setq count (1+ count)))))) 1131 | (cond 1132 | ((= count 0) 1133 | (message "No buffer has been saved.")) 1134 | ((= count 1) 1135 | (message "Buffer has been saved.")) 1136 | (t 1137 | (message "%d buffers have been saved." count))))) 1138 | 1139 | (defun wgrep-mark-deletion () 1140 | "Mark as delete to current line. 1141 | This change will be applied when \\[wgrep-finish-edit]." 1142 | (interactive) 1143 | (save-excursion 1144 | (let ((ov (wgrep-editing-overlay))) 1145 | (unless ov 1146 | (error "Not a grep result")) 1147 | (condition-case nil 1148 | (progn 1149 | (overlay-put ov 'wgrep-edit-text nil) 1150 | (let ((wgrep-inhibit-modification-hook t) 1151 | (begin (overlay-get ov 'wgrep-contents-begin)) 1152 | (end (overlay-end ov))) 1153 | (delete-region begin end) 1154 | (overlay-put ov 'face 'wgrep-delete-face))) 1155 | (error 1156 | (delete-overlay ov)))))) 1157 | 1158 | (unless wgrep-mode-map 1159 | (let ((map (make-sparse-keymap))) 1160 | 1161 | (define-key map "\C-c\C-c" 'wgrep-finish-edit) 1162 | (define-key map "\C-c\C-d" 'wgrep-mark-deletion) 1163 | (define-key map "\C-c\C-e" 'wgrep-finish-edit) 1164 | (define-key map "\C-c\C-p" 'wgrep-toggle-readonly-area) 1165 | (define-key map "\C-c\C-r" 'wgrep-remove-change) 1166 | (define-key map "\C-x\C-s" 'wgrep-finish-edit) 1167 | (define-key map "\C-c\C-u" 'wgrep-remove-all-change) 1168 | (define-key map "\C-c\C-k" 'wgrep-abort-changes) 1169 | (define-key map "\C-x\C-q" 'wgrep-exit) 1170 | 1171 | (setq wgrep-mode-map map))) 1172 | 1173 | ;;; 1174 | ;;; Entry point 1175 | ;;; 1176 | 1177 | (defun wgrep-setup-internal () 1178 | (setq wgrep-original-mode-map (current-local-map)) 1179 | (define-key wgrep-original-mode-map 1180 | wgrep-enable-key 'wgrep-change-to-wgrep-mode) 1181 | ;; delete previous wgrep overlays 1182 | (wgrep-cleanup-overlays (point-min) (point-max)) 1183 | (remove-hook 'post-command-hook 'wgrep-maybe-echo-error-at-point t) 1184 | (run-hooks 'wgrep-setup-hook)) 1185 | 1186 | ;;;###autoload 1187 | (defun wgrep-setup () 1188 | "Setup wgrep preparation." 1189 | (cond 1190 | ((and (boundp 'grep-use-null-filename-separator) 1191 | grep-use-null-filename-separator 1192 | ;; FIXME: command may contain "--null" text in search text 1193 | ;; (e.g. grep -nH -e "searching --null argument") 1194 | ;; `grep-use-null-filename-separator' is non-nil 1195 | ;; enough to reduce that confusion. 1196 | (let ((command (car-safe compilation-arguments))) 1197 | (and (stringp command) 1198 | (string-match "[\s\t]--null[\s\t]" command)))) 1199 | (set (make-local-variable 'wgrep-line-file-regexp) 1200 | wgrep-null-file-separator-header-regexp)) 1201 | (t 1202 | (set (make-local-variable 'wgrep-line-file-regexp) 1203 | wgrep-colon-file-separator-header-regexp))) 1204 | (wgrep-setup-internal)) 1205 | 1206 | ;;; 1207 | ;;; activate/deactivate marmalade install or github install. 1208 | ;;; 1209 | 1210 | ;;;###autoload 1211 | (add-hook 'grep-setup-hook 'wgrep-setup) 1212 | 1213 | ;; For `unload-feature' 1214 | (defun wgrep-unload-function () 1215 | (remove-hook 'grep-setup-hook 'wgrep-setup)) 1216 | 1217 | (provide 'wgrep) 1218 | 1219 | ;;; wgrep.el ends here 1220 | --------------------------------------------------------------------------------