help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Indenting with the tab key like everyone else


From: R. Diez
Subject: Re: Indenting with the tab key like everyone else
Date: Tue, 25 Jun 2019 10:09:29 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.0

Hallo Danny:

Thanks for your help. I got a little further, see the code I came up with below 
(I used some code I found on the Internet too).

There is a little issue I have not been able to fix yet. Say I place the cursor at the beginning of a line, and select a few lines by keeping the "shift" key pressed and hitting the down arrow key a few times. I call this selecting lines "downwards". Then indenting by hitting the tab key multiple times works fine.

However, if I select/mark the lines "upwards" (by using the up arrow key), then 'my-indent-rigidly' loses the selection after indenting the first time.

I am confused with the terms 'region', 'mark' and 'transient mark', and my Lisp skills are not very good either. Have you got any ideas on how to fix that?


The Lisp code is here:

; This routine keeps the region/mark/transient mark (?) when doing an 
indent-rigidly.
(defun my-indent-rigidly (amount) ""
  (save-excursion
    (let ((deactivate-mark nil)
          (beg (region-beginning)))
      (move-beginning-of-line nil)
      (indent-rigidly beg (region-end) amount)
      (push-mark beg t t))))


; Shift-Tab to decrease the indentation.
; As an alternative, block-select the columns of spaces you want to delete, and 
press the Del key.
(defun unindent-rigidly nil ""
  (interactive)
  (my-indent-rigidly -1)
)

(global-set-key [S-tab] 'unindent-rigidly)
; Sometimes "S-tab" does not work, use [backtab] instead:
(global-set-key [backtab] 'unindent-rigidly)


; The Tab key has 2 functions:
; - If some text is selected, ridigly increases indentation.
; - Otherwise, intelligently indent the current line.
(defun my-tab-indent nil ""
  (interactive)
  (if (use-region-p)
    (my-indent-rigidly 1)
    (indent-for-tab-command)))

(global-set-key (kbd "TAB") 'my-tab-indent)


Best regards,
  rdiez



reply via email to

[Prev in Thread] Current Thread [Next in Thread]