From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: YUE Daian Newsgroups: gmane.emacs.help Subject: Re: Indenting with the tab key like everyone else Date: Tue, 04 Jun 2019 12:29:29 +0800 Message-ID: <87blzegehi.fsf@work.i-did-not-set--mail-host-address--so-tickle-me> References: <1019418503.3900238.1559510385829.ref@mail.yahoo.com> <1019418503.3900238.1559510385829@mail.yahoo.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="197358"; mail-complaints-to="usenet@blaine.gmane.org" To: "R. Diez" , "help-gnu-emacs\@gnu.org" Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Jun 04 06:30:08 2019 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hY15L-000pE2-7K for geh-help-gnu-emacs@m.gmane.org; Tue, 04 Jun 2019 06:30:07 +0200 Original-Received: from localhost ([127.0.0.1]:45411 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hY15K-0000Ny-0o for geh-help-gnu-emacs@m.gmane.org; Tue, 04 Jun 2019 00:30:06 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:54125) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hY151-0000Nh-Pf for help-gnu-emacs@gnu.org; Tue, 04 Jun 2019 00:29:48 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hY150-000763-Of for help-gnu-emacs@gnu.org; Tue, 04 Jun 2019 00:29:47 -0400 Original-Received: from mail-pg1-x544.google.com ([2607:f8b0:4864:20::544]:46602) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hY150-00075H-IT for help-gnu-emacs@gnu.org; Tue, 04 Jun 2019 00:29:46 -0400 Original-Received: by mail-pg1-x544.google.com with SMTP id v9so9536264pgr.13 for ; Mon, 03 Jun 2019 21:29:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:in-reply-to:references:date:message-id:mime-version; bh=NCAYgZEyHsZWVO0c9X/nW9e0HdkSZ8WuLxj8QevKQ0w=; b=QAD4y8y9PxNZTWmOvYfWg2fQrr9nKqAtjZ75nF/Lv5P/AH+KpA9VUNBKNTPeA2sHa7 WMjSZCCBL1xm1a23gDo1N0ZmyUlrecUAk7PZk9J0E/oJgG4JCpOyFDUJAJZOiXDn3IMz 0l4T6KilJMQRTwKV6WxoFLGXNIuPS9wNe+/vjJKUa5CsX6zIrG0VFA5KljrqN0HsILwB NNrNodGFqOe9y8j1cI2VVPxn8zjOwJtPzBZlmq5E79uzdveGUIerIIXv6w5wGnopel7l iw3wPK1NmNlyGLJ0hm0VPqerd16V8+ko6KgCES/ZMwJCo/t5mWsQmq/V5NenenTGAyXa JfnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:in-reply-to:references:date :message-id:mime-version; bh=NCAYgZEyHsZWVO0c9X/nW9e0HdkSZ8WuLxj8QevKQ0w=; b=kcmahBw6muuvMvr4tcY0z8BpPn4NszoPg4fgDZ9S6DeieVYw5Y9WEAr1UDGgImOboN kVJX3mmFNTxIkeiU/iHOGyK9P18LjZxobFC4ixLbC3+ake1nO4hygUUBvD2bUu5IxAKj n5snxQaTwOuaMCC6qPUMnsQfNpkIsnKibHlQVL7FFVXT80Hsi6tIVvKmRFk7cbIPOwib aMZJd0Ns29klpQTDAqhid39HsvIQnqNtRuce3ktfBHaJQolzNC+4SzlGDaG/eCIuaBQs nNYigEx6IdFZp8Q2AHw+1ZieWI5JcqdACJ71Q391riGqQFX6Hai/SldR2198d2Odx/GL 2e4Q== X-Gm-Message-State: APjAAAWpY0vWQNDVvoGVMODTpedgmJZQihOd1pYwMs1H0I6iBtFi817M UshyrhlUmXiJ1l4fbZzqU58= X-Google-Smtp-Source: APXvYqxKqa6ABtdnqq+TScbNtU52/Z3mX05pF1qa5kvj4OcQPesAPmGTtPPZcKqVL4UecjwE8Npueg== X-Received: by 2002:a17:90a:a790:: with SMTP id f16mr35021402pjq.27.1559622584414; Mon, 03 Jun 2019 21:29:44 -0700 (PDT) Original-Received: from localhost ([118.201.58.126]) by smtp.gmail.com with ESMTPSA id 188sm468047pfe.30.2019.06.03.21.29.42 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Mon, 03 Jun 2019 21:29:43 -0700 (PDT) In-Reply-To: <1019418503.3900238.1559510385829@mail.yahoo.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::544 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:120736 Archived-At: On 2019-06-02 21:19, "R. Diez via help-gnu-emacs" 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 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