使用 当前的region(如果有) 否则 使用当前的buffer

现在我有2个函数可用

  1. defun mongo-send-region (start end)
  2. defun mongo-send-buffer ()

我想达到效果是

(defun smart-mongo-send () (if (has-region) (mongo-send-region start, end) (mongo-send-buffer)) )

当相关的几个api 却没有找到, 有人熟悉相关的api 吗?

(defmacro make-active-region-alternate (new old1 old2)
  `(defun ,new (&optional beg end)
     (interactive "r")
     (if (region-active-p)
         (,old1 beg end)
       (,old2))))

(make-active-region-alternate smart-mongo-send
                              mongo-send-region
                              mongo-send-buffer)

;;; following is a test, don't copy to u config

(pp (macroexpand-1 '(make-active-region-alternate smart-mongo-send
                                            mongo-send-region
                                            mongo-send-buffer)))
(defun smart-mongo-send
    (&optional beg end)
  (interactive "r")
  (if
      (region-active-p)
      (mongo-send-region beg end)
    (mongo-send-buffer)))
1 个赞

相比 region-active-puse-region-p 会排除掉 Region 为空的情况。

1 个赞

另一个选择是使用 region-bindings-mode.