File: wgrep-deadgrep.el

package info (click to toggle)
emacs-wgrep 3.0.0%2B20.g208b9d0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 252 kB
  • sloc: lisp: 1,319; makefile: 64; sh: 3
file content (112 lines) | stat: -rw-r--r-- 4,225 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
;;; wgrep-deadgrep.el --- Writable deadgrep buffer and apply the changes to files -*- lexical-binding: t -*-

;; Author: Masahiro Hayashi <mhayashi1120@gmail.com>, Iku Iwasa <iku.iwasa@gmail.com>
;; Keywords: grep edit extensions
;; Package-Requires: ((wgrep "2.3.0") (emacs "25.1"))
;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep-deadgrep.el
;; Emacs: GNU Emacs 25 or later
;; Version: 2.3.1

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3, or (at
;; your option) any later version.

;; This program is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; wgrep-deadgrep allows you to edit a deadgrep buffer and apply those changes to
;; the file buffer.

;;; Install:

;; 1. Install deadgrep.el
;;
;;   https://github.com/Wilfred/deadgrep

;; 2. Install wgrep.el

;; 3. Put this file into load-path'ed directory, and byte compile it if
;; desired. And put the following expression into your ~/.emacs.
;;
;;     (autoload 'wgrep-deadgrep-setup "wgrep-deadgrep")
;;     (add-hook 'deadgrep-finished-hook 'wgrep-deadgrep-setup)

;;; Usage:

;; See wgrep.el

;;; Code:

(require 'wgrep)

(defun wgrep-deadgrep-prepare-header&footer ()
  "Prepare header in `deadgrep' buffer for `wgrep'.
Since `deadgrep' does not have footer, only header is handled."
  ;; `deadgrep' uses "deadgrep-filename" property for filename.
  (let ((pos (next-single-property-change (point-min) 'deadgrep-filename)))
    (if pos
        (add-text-properties (point-min) pos '(read-only t wgrep-header t))
      (add-text-properties (point-min) (point-max)
                           '(read-only t wgrep-header t)))))

(defun wgrep-deadgrep-parse-command-results ()
  "Parse `deadgrep' results for `wgrep'."
  ;; Note that this function is called with the buffer narrowed to
  ;; exclude the header and the footer.  (We're going to assert that
  ;; fact here, because we use (bobp) result a bit further down to
  ;; decide that we're not reading grouped results; see below.)
  (unless (bobp)
    (error "Expected to be called with point at beginning of buffer"))
  (save-excursion
    (while (not (eobp))
      (let* ((pos (point))
             (filename (get-text-property pos 'deadgrep-filename))
             (line (get-text-property pos 'deadgrep-line-number)))
        (when filename
          (if line
              (let* ((eol (line-end-position))
                     (end (next-single-property-change
                           pos 'deadgrep-line-number nil eol)))
                (add-text-properties pos end
                                     (list 'wgrep-line-filename filename
                                           'wgrep-line-number line)))
            ;; Ignore the line that introduces matches from a file, so that
            ;; wgrep doesn't let you edit it.
            (add-text-properties
             pos (line-end-position)
             (list 'wgrep-ignore t
                   (wgrep-construct-filename-property filename)
                   filename)))))
      (forward-line 1))))

;;;###autoload
(defun wgrep-deadgrep-setup ()
  "Setup `wgrep-deadgrep' for `deadgrep'."
  (setq wgrep-prepared nil)
  (set (make-local-variable 'wgrep-header&footer-parser)
       'wgrep-deadgrep-prepare-header&footer)
  (set (make-local-variable 'wgrep-results-parser)
       'wgrep-deadgrep-parse-command-results)
  (wgrep-setup-internal))

;;;###autoload
(add-hook 'deadgrep-finished-hook 'wgrep-deadgrep-setup)

;; For `unload-feature'
(defun wgrep-deadgrep-unload-function ()
  "Unload `wgrep-deadgrep' setup."
  (remove-hook 'deadgrep-finished-hook 'wgrep-deadgrep-setup))

(provide 'wgrep-deadgrep)

;;; wgrep-deadgrep.el ends here