1

I found a neat code snippet for ~/.emacs configuration:

  (global-set-key (kbd "C->") 
                  (lambda (b e &optional n) 
                    (interactive "r")
                    (indent-code-rigidly b e (or n 4)))
                  )

  (global-set-key (kbd "C-<") 
                  (lambda (b e &optional n) 
                    (interactive "r")
                    (indent-code-rigidly b e (or n (- 0 4))))
                  )

This snippet allows me to use C->/C-< hotkeys to force indent/unindent selected code block. But after indent/uindent applies to code block, selection is lost. How to retain selection (or select again same code lines) programmatically?

1 Answer 1

2

I'm guessing that this is really what you want:

(global-set-key (kbd "C-<") 
        (lambda (b e n) 
          (interactive "r\nP")
          (indent-code-rigidly b e (if n (prefix-numeric-value n) -4))
          (setq deactivate-mark  nil)))

What you were missing was (setq deactivate-mark nil). After each command, the command loop normally deactivates the region, but if you set this variable to nil then it does not.

Beyond that, I fixed (according to what I think you wanted) the use of a prefix argument.

2
  • No bare lambdas, please! :) Commented Aug 3, 2016 at 16:03
  • @KaushalModi: I intentionally tried to keep the code close to the OP, so s?he could easily see the difference. It's not necessary to use a named function here, and using one might give the impression that it is. But I agree that it is generally better to use named functions in key bindings, hooks, menu items etc.
    – Drew
    Commented Aug 3, 2016 at 19:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.