[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Indenting with the tab key like everyone else
From: |
YUE Daian |
Subject: |
Re: Indenting with the tab key like everyone else |
Date: |
Mon, 01 Jul 2019 12:29:35 +0800 |
On 2019-06-25 10:09, "R. Diez" <rdiezmail-emacs@yahoo.de> wrote:
> 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
Sorry for the late reply.
I am not sure what you really want to do exactly. There are some tips I
thought of based on the code:
- If you look at the document of the function `push-mark`, you may
notice that "Novice Emacs Lisp programmers often try to use the mark
for the wrong purposes". I am not sure if this is the case but I
presume that the weird behavior relates to it.
- Key binding Tab can be obtained by calling `(kbd "<tab>")` and
back-tab using `(kbd "S-<tab>")`. I tried your function and both
selecting downwards and upwards work. Please check the real key
binding by typing `C-h k <tab>` and see if your function got
called. The global key binding can be overrode by mode-map.
- If you want to edit multiple lines, you may consider using the
multi-cursors package.
- It is very unusual to use `nil` instead of `()` for argument
list. Also document string usually remains below the header line of a
function.
Please describe what you want to achieve exactly so that more specific
help could be given.
Cheers.
- Re: Indenting with the tab key like everyone else,
YUE Daian <=