Making a region-indent-function keep the region ma

2019-05-04 19:39发布

问题:

I'm having trouble with haml-mode's region-indent-function, which I'm trying to reuse in another major mode. We're supposed to be able to cycle the region indentation by keeping the region marked after the haml-indent-region is being evaled, but it doesn't work as intended. After some hacking around, I've found out that throwing an error at the end of the function makes Emacs keep the region marked, as in this example:

(defun haml-indent-region (start end)
  (save-excursion
    ...)
  (error "")) ;; Terrible hack

But I really don't like it. Is there a clean way of getting this behavior without such an horrible hack?

回答1:

The region is reset after the command completes, so calling activate-mark does not have any effect. Throwing an error (a non-local exit) apparently prevents this step, but that might be a bug.

The trick is: deactivate-mark

If an editing command sets this to t, deactivate the mark afterward. The command loop sets this to nil before each command, and tests the value when the command returns. Buffer modification stores t in this variable.

So just do this at the end of your command:

  (setq deactivate-mark nil)