Skip to content

Commit

Permalink
Change double-saber to be a minor mode and update ecukes tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dp12 committed Feb 14, 2019
1 parent 13476a9 commit 9a29e8c
Show file tree
Hide file tree
Showing 4 changed files with 198 additions and 198 deletions.
45 changes: 34 additions & 11 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,30 @@ You can load double-saber with use-package:
:load-path "~/double-saber"
:config
(with-eval-after-load "ripgrep"
(double-saber-mode-setup ripgrep-search-mode-map 'ripgrep-search-mode-hook 5 "Ripgrep finished"))
(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"))))
#+end_src

Or the traditional way with require:
#+begin_src emacs-lisp
(add-to-list 'load-path "/path/to/double-saber-dir/")
(require 'double-saber)
(with-eval-after-load "ripgrep"
(double-saber-mode-setup ripgrep-search-mode-map 'ripgrep-search-mode-hook 5 "Ripgrep finished"))
(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"))))
#+end_src

The =double-saber-mode-setup= function can be used with an arbitrary mode to set up double-saber with some default keybindings. It needs to be called with the following items:
- The mode-map (most likely the major mode's name followed by "-mode-map")
- The mode-hook (most likely the major mode's name followed by "-mode-hook")
- The start line where double-saber should start deleting/narrowing from. In most cases, it should be the line after the search command. Can be =nil=.
- The end text, which denotes the line beyond which double-saber should not delete (inclusive). This prevents useful text like the number of search hits from getting deleted. Can be =nil=.
To get double-saber working for a given mode, you can enable =double-saber-mode= in that function's mode hook, as shown above. Additionally, you probably want to set the following variables locally for that mode:
- =double-saber-start-line=: The start line where double-saber should start deleting/narrowing from. In most cases, it should be the line after the search command. Can be =nil=.
- =double-saber-end-text=: The end text, which denotes the line beyond which double-saber should not delete (inclusive). This prevents useful text like the number of search hits from getting deleted. Can be =nil=.

=double-saber-mode-setup= sets up the following keybinds for the major mode specified:
=double-saber-mode= provides the following keybinds when enabled:
| Key | Command |
|--------------+-------------------------|
| =d= | double-saber-delete |
Expand All @@ -55,9 +61,26 @@ The =double-saber-mode-setup= function can be used with an arbitrary mode to set

The following are examples of setting up double-saber for different major modes:
#+begin_src emacs-lisp
(double-saber-mode-setup ggtags-global-mode-map 'ggtags-global-mode-hook 5 "Global found")
(double-saber-mode-setup grep-mode-map 'grep-mode-hook 5 "Grep finished")
(double-saber-mode-setup ivy-occur-grep-mode-map 'ivy-occur-grep-mode-hook 5 nil)

(with-eval-after-load "grep"
(add-hook 'grep-mode-hook
(lambda ()
(double-saber-mode)
(setq-local double-saber-start-line 5)
(setq-local double-saber-end-text "Grep finished")))

(with-eval-after-load "ggtags"
(add-hook 'ggtags-global-mode-hook
(lambda ()
(double-saber-mode)
(setq-local double-saber-start-line 5)
(setq-local double-saber-end-text "Global found"))))

(with-eval-after-load "ivy"
(add-hook 'ivy-occur-grep-mode-hook
(lambda ()
(double-saber-mode)
(setq-local double-saber-start-line 5)))
#+end_src
* Hydras and Transient States
If you have abo-abo's excellent [[https://github.com/abo-abo/hydra][hydra]] package, you can define a keymap to narrow or delete specific strings without having to type them.
Expand Down
52 changes: 20 additions & 32 deletions double-saber.el
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@
;; 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 are a few examples.
;; double-saber-mode-setup. The following is an example for ripgrep.
;;
;; (double-saber-mode-setup grep-mode-map 'grep-mode-hook 5 "Grep finished")
;; (double-saber-mode-setup ggtags-global-mode-map 'ggtags-global-mode-hook 5 "Global found")
;; (double-saber-mode-setup ripgrep-search-mode-map 'ripgrep-search-mode-hook 5 "Ripgrep finished")
;; (double-saber-mode-setup ivy-occur-grep-mode-map 'ivy-occur-grep-mode-hook 5 nil)
;;(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")))
;;
;; (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.

;;; Code:
(require 'subr-x) ;; for string-trim
Expand Down Expand Up @@ -131,28 +129,18 @@ If the REVERSE flag is true, the lines are sorted in reverse order."
(undo arg))))

;; Mode-specific setup
(defun double-saber-mode-setup (keymap hook &optional start-line end-text)
"Set up double-saber default keybindings and start/end bounds for a mode.
KEYMAP is the keymap of the major mode to configure.
HOOK is the mode hook of the major mode to configure. The HOOK argument must be
quoted.
START-LINE should be set to the line after the search command, to avoid deleting
the search command. If nil, it defaults to the start of the buffer.
END-TEXT should be set to the end text in the buffer that should not be deleted,
e.g. a string such as 'Search finished.' If nil, it defaults to the end of the
buffer."
(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)
(add-hook hook
(lambda ()
(setq-local double-saber-start-line start-line)
(setq-local double-saber-end-text end-text))))
;;;###autoload
(define-minor-mode double-saber-mode
"Delete/narrow text with regular expressions or multiple keywords."
:lighter ""
: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)

Expand Down
Loading

0 comments on commit 9a29e8c

Please sign in to comment.