-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdouble-saber.el
156 lines (137 loc) · 5.56 KB
/
double-saber.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
;;; double-saber.el --- Narrow and delete in search buffers. -*- lexical-binding: t; -*-
;; Author: Daniel Ting <deep.paren.12@gmail.com>
;; URL: https://github.com/dp12/double-saber.git
;; Version: 0.0.3
;; Package-Requires: ((emacs "24.4"))
;; Keywords: double-saber, narrow, delete, sort, tools, convenience, matching
;; This file is not part of GNU Emacs.
;; 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 of the License, 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; A minor mode for filtering search buffer output.
;;
;; With double-saber-mode enabled, use "d" to delete words and "x" to narrow.
;;
;; Installation:
;; To load this file, add (require 'double-saber) to your init file.
;;
;; You can configure double-saber for different modes using
;; double-saber-mode-setup. The following is an example for ripgrep.
;;
;;(add-hook 'ripgrep-search-mode-hook
;; (lambda ()
;; (double-saber-mode)
;; (setq-local double-saber-start-line 5)
;; (setq-local double-saber-end-text "Ripgrep finished")))
;;
;; Setting the start line and end text prevents useful text at those locations
;; from getting deleted.
;;
;; Please see README.md for more documentation, or read it online at
;; https://github.com/dp12/double-saber
;;; Code:
(require 'subr-x) ;; for string-trim
(defvar double-saber-start-line 5)
(defvar double-saber-end-text nil)
;;;
;;; Private functions
;;;
(defun double-saber--start-point ()
"Determine the start point of the region to act on."
(save-excursion
(if double-saber-start-line
(with-no-warnings
(goto-line double-saber-start-line))
(goto-char (point-min)))
(point)))
(defun double-saber--end-point ()
"Determine the end point of the region to act on."
(save-excursion
(goto-char (point-min))
(if double-saber-end-text
(if (search-forward double-saber-end-text nil t)
(forward-line -1)
(goto-char (point-max)))
(goto-char (point-max)))
(point)))
(defun double-saber--get-regexp (filter)
"Form a regular expression from FILTER.
If the input consists of multiple strings, use regex-opt to make a regular
expression that matches them. Otherwise, if the input is a single string with no
spaces, return it unchanged as the regular expression."
(let ((regexp (split-string (string-trim filter) " ")))
(if (> (length regexp) 1)
(regexp-opt regexp)
(car regexp))))
;;;
;;; Public functions
;;;
(defun double-saber-narrow (&optional filter)
"Narrow the search buffer output using FILTER.
The filter can either be a regular expression with no spaces or a list of
strings. All lines that do not match the regular expression or one of the
strings is deleted."
(interactive
(list (read-string "Narrow regex/strings: ")))
(let ((inhibit-read-only t))
(buffer-enable-undo)
(delete-non-matching-lines (double-saber--get-regexp filter)
(double-saber--start-point)
(double-saber--end-point))))
(defun double-saber-delete (&optional filter)
"Delete all lines in the buffer using FILTER.
The filter can either be a regular expression with no spaces or a list of
strings. All lines that match the regular expression or one of the strings is
deleted."
(interactive
(list (read-string "Delete regex/strings: ")))
(let ((inhibit-read-only t))
(buffer-enable-undo)
(delete-matching-lines (double-saber--get-regexp filter)
(double-saber--start-point)
(double-saber--end-point))))
(defun double-saber-sort-lines (&optional reverse)
"Sort lines in the search buffer output.
If the REVERSE flag is true, the lines are sorted in reverse order."
(interactive)
(let ((inhibit-read-only t))
(buffer-enable-undo)
(sort-lines reverse (double-saber--start-point) (double-saber--end-point))))
(defun double-saber-undo (&optional arg)
"Undo changes. An optional numeric ARG serves as a repeat count."
(interactive "P")
(let ((inhibit-read-only t))
(if (and (bound-and-true-p undo-tree-mode) (fboundp 'undo-tree-undo))
(undo-tree-undo arg)
(undo arg))))
(defun double-saber-redo (&optional arg)
"Redo changes. An optional numeric ARG serves as a repeat count."
(interactive "P")
(let ((inhibit-read-only t))
(if (and (bound-and-true-p undo-tree-mode) (fboundp 'undo-tree-redo))
(undo-tree-redo arg)
(undo arg))))
;; Mode-specific setup
;;;###autoload
(define-minor-mode double-saber-mode
"Delete/narrow text with regular expressions or multiple keywords."
:lighter " Dsaber"
:keymap (let ((keymap (make-sparse-keymap)))
(define-key keymap (kbd "d") 'double-saber-delete)
(define-key keymap (kbd "x") 'double-saber-narrow)
(define-key keymap (kbd "s") 'double-saber-sort-lines)
(define-key keymap (kbd "u") 'double-saber-undo)
(define-key keymap (kbd "C-r") 'double-saber-redo)
(define-key keymap (kbd "C-_") 'double-saber-redo)
keymap))
(provide 'double-saber)
;;; double-saber.el ends here