summary refs log tree commit diff stats
path: root/lisp/+hungry-delete.el
blob: 601aecfdeb2e8fcbe8b26936525893c1d6c0f494 (plain)
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
;;; +hungry-delete.el -*- lexical-binding: t; -*-

;; Copyright (C) 2022  Case Duckworth

;;; Code:

(require 'hungry-delete)

(defmacro +hungry-delete-define-alternative (func hd-func &optional direction)
  "Define a `hungry-delete' alternative for FUNC.
If the adjacent characters in DIRECTION are one of
`hungry-delete-chars-to-skip', call HD-FUNC.  Otherwise, call
FUNC.

If DIRECTION is a positive integer or `forward', look forward.
If it's negative or `backward', look backward.  If it's absent,
this macro will try to guess based on the names of FUNC and
HD-FUNC.

This macro creates a function taking a prefix argument that's
passed to both of FUNC and HD-FUNC."
  (let ((name (intern (format "%s|%s" func hd-func)))
	(lookfn (cond
		 ((or (and (string-match-p "forward" (symbol-name func))
			   (string-match-p "forward" (symbol-name hd-func))
			   (not direction))
		      (and (integerp direction)
			   (< 0 direction))
		      (eq direction 'forward))
		  'looking-at)
		 ((or (and (string-match-p "backward" (symbol-name func))
			   (string-match-p "backward" (symbol-name hd-func))
			   (not direction))
		   (and (integerp direction)
			   (> 0 direction))
		      (eq direction 'backward))
		  'looking-back)
		 (:else (error "Bad direction: %S" direction))))
	(arg (gensym)))
    `(defun ,name (,arg)
       ,(concat (format "Do `%s' or `%s', depending on whitespace.\n"
			func hd-func)
		(format "Pass prefix argument %s to the underlying functions.\n"
			(upcase (symbol-name arg)))
		"This function was defined by `define-hungry-delete-alternative'.")
       (interactive "*p")		; This is brittle
       (if (,lookfn (format "[%s]" hungry-delete-chars-to-skip))
	   (,hd-func (or ,arg 1))
	 (,func ,arg)))))

(provide '+hungry-delete)
;;; +hungry-delete.el ends here