I'm trying to write a function/macro to wrap arbitrary interactive function with some pre- and post- processing. Those processing does not involve the arguments of the interactive function.
For example, give f = delete-region
, I'd like to write a function/macro that return a new function g = delete-region-wrapper
with the definition like
(defun delete-region-wrapper (begin end) # or maybe (&rest args)...
(interactive "r")
(read-only-mode -1) # 'pre-processing'
(delete-region begin end) # or maybe (apply #'delete-region ...) or maybe (funcall #'delete-region ... )
(read-only-mode 1)) # 'post-processing'
Or the Python equivalent looks like
def wrap(f):
def wrapper(*args, **kwargs):
read_only_mode(-1)
res = f(*args, **kwargs)
read_only_mode(1)
return res
return wrapper
delete_region_wrapper = wrap(delete_region)
How can I write this wrap
macro/function?
(defmacro/defun wrap(f)
???
)
(setq delete-region-wrapper (wrap #'delete-region))
let
-bindinhibit-read-only
instead of switchingread-only-mode
off and on. Aside from anything else, switching modes like that would require you to also do error-handling in order to ensure that you ended up in the correct state even if something went wrong in the code you were wrapping (notwithstanding that your example wasn't actually checking the pre-existing state at the beginning).