unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: YUE Daian <sheepduke@gmail.com>
To: "R. Diez" <rdiezmail-emacs@yahoo.de>,
	"help-gnu-emacs\@gnu.org" <help-gnu-emacs@gnu.org>
Subject: Re: Indenting with the tab key like everyone else
Date: Tue, 04 Jun 2019 12:29:29 +0800	[thread overview]
Message-ID: <87blzegehi.fsf@work.i-did-not-set--mail-host-address--so-tickle-me> (raw)
In-Reply-To: <1019418503.3900238.1559510385829@mail.yahoo.com>

On 2019-06-02 21:19, "R. Diez via help-gnu-emacs" <help-gnu-emacs@gnu.org> wrote:
> Hi all:
>
> I want to change the indenting behaviour of the tab key when writing source code like C++, Perl or Bash. But I am a little confused, so I need some help.
>
> When in C mode, the Tab key is bound to c-indent-line-or-region . When in Perl mode, it is bound to indent-for-tab-command, which is a rather complicated function.
>
> I want the same behaviour as most other IDEs I know: If I just press tab, it should "intelligently" indent the current line of code as usual. But if I select several lines with the shift key, I want to rigidly indent the selected block of lines. At the moment, Emacs tries to reindent the selected lines, which often does nothing, because it thinks the lines are already properly indented.
>
> If I want to intelligently reindent a block of lines, I would rather manually call the right function, something like "reindent-code". I could create an alias or a new function just for that purpose.
>
> I never got used to Emacs "prefix" mechanism, so I do not want to use it. And I do not want to retrain my "muscle memory". I want the same behaviour as everywhere else.
>
> What would be the best way to achieve this? I know a little Lisp, but not enough for complex things.
>
> Many thanks in advance,
>   rdiez

Well, Emacs TAB key facility is a little bit complicated.

I had similar requirement to yours when I was using Emacs 23.
I wrote a function exactly for this.

It triggers company-mode completion when the "current" character
satisfies the regex.
Otherwise, it just indent the line.
Or when yasnippet is active, it moves to the next field.
AFAIK `company-indent-or-complete-common` does not support it.

--- BEGIN ---
(defcustom company-begin-regex "[0-9a-zA-Z_.>:-]"
	"Used by function `complete-or-indent' to decide whether or not to start
completion."
	:type 'string
	:group 'none
	:safe t)

  ;; Bind company complete to <TAB> in a smart way.
  ;; TODO Remove this snippet later...
  (defun complete-or-indent ()
	"Complete using company-mode or indent current line by checking "
	(interactive)
	(cond
	 ;; When in region, indent the region.
	 ((use-region-p)
	  (indent-region (region-beginning) (region-end)))
	 ;; When yasnippet is active, move to next field.
	 ((yas-active-snippets)
	  (yas-next-field))
	 ;; When it is possible to complete, do it.
	 ((and (string-match-p company-begin-regex (char-to-string (char-before)))
           (call-when-defined 'company-manual-begin))
	  (call-when-defined 'company-complete-common))
	 (t (indent-for-tab-command))))
--- END ---

Hope that helps.

Danny



  parent reply	other threads:[~2019-06-04  4:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <1019418503.3900238.1559510385829.ref@mail.yahoo.com>
2019-06-02 21:19 ` Indenting with the tab key like everyone else R. Diez via help-gnu-emacs
2019-06-04  1:19   ` Emanuel Berg via help-gnu-emacs
2019-06-04  4:29   ` YUE Daian [this message]
2019-06-25  8:09     ` R. Diez via help-gnu-emacs
2019-07-01  4:29       ` YUE Daian
2019-07-16  7:23         ` R. Diez

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87blzegehi.fsf@work.i-did-not-set--mail-host-address--so-tickle-me \
    --to=sheepduke@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=rdiezmail-emacs@yahoo.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).