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: YUE Daian
Subject: Re: Indenting with the tab key like everyone else
Date: Tue, 04 Jun 2019 12:29:29 +0800

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



reply via email to

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