;;; +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