-
Notifications
You must be signed in to change notification settings - Fork 45
/
wgrep-helm.el
105 lines (83 loc) · 3.61 KB
/
wgrep-helm.el
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
;;; wgrep-helm.el --- Writable helm-grep-mode buffer -*- lexical-binding: t -*-
;; Copyright (C) 2010-2020,2023 Masahiro Hayashi
;; Author: Masahiro Hayashi <mhayashi1120@gmail.com>
;; Keywords: grep edit extensions
;; Package-Requires: ((emacs "25.1") (wgrep "3.0.0"))
;; URL: http://github.com/mhayashi1120/Emacs-wgrep/raw/master/wgrep-helm.el
;; Emacs: GNU Emacs 25 or later
;; Version: 3.0.0
;; 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-helm allows you to edit a helm-grep-mode buffer and apply those
;; changes to the file buffer.
;;; Install:
;; Put this file into load-path'ed directory, and byte compile it if
;; desired. And put the following expression into your ~/.emacs.
;;
;; (require 'wgrep-helm)
;;; Usage:
;; See wgrep.el
;;; Code:
(require 'wgrep)
(declare-function helm-grep-split-line "helm-grep")
;;;###autoload
(defun wgrep-helm-setup ()
(set (make-local-variable 'wgrep-header&footer-parser)
'wgrep-helm-prepare-header&footer)
(set (make-local-variable 'wgrep-results-parser)
'wgrep-helm-parse-command-results)
(wgrep-setup-internal))
(defun wgrep-helm-prepare-header&footer ()
(let (beg end)
;; Set read-only grep result header
(setq beg (point-min))
(setq end (next-single-property-change
(point-min) 'helm-realvalue))
(put-text-property beg end 'read-only t)
(put-text-property beg end 'wgrep-header t)
;; helm-grep-mode have NO footer.
))
(defun wgrep-helm-parse-command-results ()
(while (not (eobp))
(when (looking-at wgrep-line-file-regexp)
(let* ((start (match-beginning 0))
(end (match-end 0))
(dispname (match-string 1))
(namelen (length dispname)))
(let* ((value (get-text-property (point) 'helm-realvalue))
(data (when (eq major-mode 'helm-grep-mode)
(helm-grep-split-line value)))
(bufname (get-text-property (point) 'buffer-name))
(fn (or (and bufname (buffer-file-name (get-buffer bufname)))
(get-text-property (point) 'helm-grep-fname)))
(line (if data
(string-to-number (nth 1 data))
;; Real value of candidate is now the line
;; number in helm-occur.
value))
(fprop (wgrep-construct-filename-property fn)))
(put-text-property start end 'wgrep-line-filename fn)
(put-text-property start end 'wgrep-line-number line)
(put-text-property start (+ start namelen) fprop fn))))
(forward-line 1)))
;;;###autoload
(add-hook 'helm-grep-mode-hook 'wgrep-helm-setup)
;;;###autoload
(add-hook 'helm-occur-mode-hook 'wgrep-helm-setup)
;; For `unload-feature'
(defun wgrep-helm-unload-function ()
(remove-hook 'helm-grep-mode-hook 'wgrep-helm-setup)
(remove-hook 'helm-occur-mode-hook 'wgrep-helm-setup))
(provide 'wgrep-helm)
;;; wgrep-helm.el ends here