all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Use tab key to indent both region AND line?
@ 2007-04-20 12:10 mopi
  2007-04-20 12:45 ` Denis Bueno
  0 siblings, 1 reply; 9+ messages in thread
From: mopi @ 2007-04-20 12:10 UTC (permalink / raw)
  To: help-gnu-emacs

Hi

I'm feeling a little lost when it comes to indentation and Emacs.

If there is a region marked I want the tab key to indent the region.
Shift-tab would preferably unindent the region. But if there is no
region marked I want tab to indent the current line. This is the
default behaviour in e.g Visual Studio and Ultraedit so I'm sure there
is someone out there who has some code snippets to share.

Thanks in advance.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Use tab key to indent both region AND line?
  2007-04-20 12:10 Use tab key to indent both region AND line? mopi
@ 2007-04-20 12:45 ` Denis Bueno
  2007-04-20 13:58   ` Livin Stephen Sharma
       [not found]   ` <mailman.2307.1177077814.7795.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 9+ messages in thread
From: Denis Bueno @ 2007-04-20 12:45 UTC (permalink / raw)
  To: mopi; +Cc: help-gnu-emacs

On 20 Apr 2007 05:10:40 -0700, mopi <52hands@gmail.com> wrote:
> If there is a region marked I want the tab key to indent the region.
> Shift-tab would preferably unindent the region. But if there is no

This is perhaps a good idea. However, C-M-\ is a workaround which will
indent when a region is selected

-Denis

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Use tab key to indent both region AND line?
  2007-04-20 12:45 ` Denis Bueno
@ 2007-04-20 13:58   ` Livin Stephen Sharma
       [not found]   ` <mailman.2307.1177077814.7795.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 9+ messages in thread
From: Livin Stephen Sharma @ 2007-04-20 13:58 UTC (permalink / raw)
  To: emacs-help gnu gnu



and
C-c C-q will "Indent the current top-level declaration or macro  
syntactically."
  (i.e. indent current function).

On 20-Apr-07, at 18:15 , Denis Bueno wrote:

> On 20 Apr 2007 05:10:40 -0700, mopi <52hands@gmail.com> wrote:
>> If there is a region marked I want the tab key to indent the region.
>> Shift-tab would preferably unindent the region. But if there is no
>
> This is perhaps a good idea. However, C-M-\ is a workaround which will
> indent when a region is selected
>
> -Denis
>
>
> _______________________________________________
> help-gnu-emacs mailing list
> help-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Use tab key to indent both region AND line?
       [not found]   ` <mailman.2307.1177077814.7795.help-gnu-emacs@gnu.org>
@ 2007-04-20 16:01     ` weber
  2007-04-20 17:16       ` Lennart Borgman (gmail)
       [not found]       ` <mailman.2326.1177089678.7795.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 9+ messages in thread
From: weber @ 2007-04-20 16:01 UTC (permalink / raw)
  To: help-gnu-emacs

On 20 abr, 10:58, Livin Stephen Sharma <livin.step...@gmail.com>
wrote:
> and
> C-c C-q will "Indent the current top-level declaration or macro
> syntactically."
>   (i.e. indent current function).
>
> On 20-Apr-07, at 18:15 , Denis Bueno wrote:
>
> > On 20 Apr 2007 05:10:40 -0700, mopi <52ha...@gmail.com> wrote:
> >> If there is a region marked I want the tab key to indent the region.
> >> Shift-tab would preferably unindent the region. But if there is no
>
> > This is perhaps a good idea. However, C-M-\ is a workaround which will
> > indent when a region is selected
>
> > -Denis
>
> > _______________________________________________
> > help-gnu-emacs mailing list
> > help-gnu-em...@gnu.org
> >http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

What do you mean to indent a region? Let emacs indent it or insert a
tab in the beginning of each selected line ?

In the first case, this should get you started:

(defun indent-line-or-region ()
  "indent line or region"
  (interactive)
  (if mark-active   ;; there is a region selected
      (indent-region)
      (indent-according-to-mode))) ;; indent line

Just bind it to tab.

Hope i could help you,
-weber

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Use tab key to indent both region AND line?
  2007-04-20 16:01     ` weber
@ 2007-04-20 17:16       ` Lennart Borgman (gmail)
  2007-04-20 22:41         ` Lennart Borgman (gmail)
       [not found]       ` <mailman.2326.1177089678.7795.help-gnu-emacs@gnu.org>
  1 sibling, 1 reply; 9+ messages in thread
From: Lennart Borgman (gmail) @ 2007-04-20 17:16 UTC (permalink / raw)
  To: weber; +Cc: help-gnu-emacs

weber wrote:

> (defun indent-line-or-region ()
>   "indent line or region"
>   (interactive)
>   (if mark-active   ;; there is a region selected
>       (indent-region)
>       (indent-according-to-mode))) ;; indent line
> 
> Just bind it to tab.
> 
> Hope i could help you,
> -weber


And here is something that can be used:

;; From an idea by weber <hugows@gmail.com>
(defun indent-line-or-region ()
   "indent line or region"
   (interactive)
   (if mark-active ;; there is a region selected
       (indent-region (region-beginning) (region-end))
     (indent-according-to-mode))) ;; indent line

(define-minor-mode indent-line-mode
   "Use TAB to indent line and region."
   :global t
   :keymap '(([tab] . indent-line-or-region)))
(when indent-line-mode (indent-line-mode 1))

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Use tab key to indent both region AND line?
       [not found]       ` <mailman.2326.1177089678.7795.help-gnu-emacs@gnu.org>
@ 2007-04-20 17:54         ` mopi
  2007-04-20 19:07           ` mopi
  0 siblings, 1 reply; 9+ messages in thread
From: mopi @ 2007-04-20 17:54 UTC (permalink / raw)
  To: help-gnu-emacs

On Apr 20, 7:16 pm, "Lennart Borgman (gmail)"
<lennart.borg...@gmail.com> wrote:
> And here is something that can be used:
>
> ;; From an idea by weber <hug...@gmail.com>
> (defun indent-line-or-region ()
>    "indent line or region"
>    (interactive)
>    (if mark-active ;; there is a region selected
>        (indent-region (region-beginning) (region-end))
>      (indent-according-to-mode))) ;; indent line
>
> (define-minor-mode indent-line-mode
>    "Use TAB to indent line and region."
>    :global t
>    :keymap '(([tab] . indent-line-or-region)))
> (when indent-line-mode (indent-line-mode 1))


Thank you very much!
I changed it slightly to use indent-rigidly:

(defun indent-line-or-region ()
   "indent line or region"
   (interactive)
   (if mark-active ;; there is a region selected
       (indent-rigidly (region-beginning) (region-end) 4)
     (indent-according-to-mode))) ;; indent line

Is there a way to make it keep the region? Now after I press tab the
region is lost so I can't repeat the indentation without reselecting
the region.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Use tab key to indent both region AND line?
  2007-04-20 17:54         ` mopi
@ 2007-04-20 19:07           ` mopi
  2007-04-20 22:12             ` weber
  0 siblings, 1 reply; 9+ messages in thread
From: mopi @ 2007-04-20 19:07 UTC (permalink / raw)
  To: help-gnu-emacs

Wohoo!

I found this little thing in easymacs.el that does exactly what I
want.


(defun easymacs-rigidly-indent-line-or-region ()
  (interactive)
  (if mark-active
(increase-left-margin (region-beginning) (region-end) nil)
    (increase-left-margin (point-at-bol) (point-at-eol) nil))
  (setq deactivate-mark nil))

(defun easymacs-rigidly-unindent-line-or-region ()
  (interactive)
  (if mark-active
(decrease-left-margin (region-beginning) (region-end) nil)
    (decrease-left-margin (point-at-bol) (point-at-eol) nil))
  (setq deactivate-mark nil))

(global-set-key [tab]       'easymacs-rigidly-indent-line-or-region)
(global-set-key [S-tab]       'easymacs-rigidly-unindent-line-or-
region)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Use tab key to indent both region AND line?
  2007-04-20 19:07           ` mopi
@ 2007-04-20 22:12             ` weber
  0 siblings, 0 replies; 9+ messages in thread
From: weber @ 2007-04-20 22:12 UTC (permalink / raw)
  To: help-gnu-emacs

On Apr 20, 4:07 pm, mopi <52ha...@gmail.com> wrote:
> Wohoo!
>
> I found this little thing in easymacs.el that does exactly what I
> want.
>
> (defun easymacs-rigidly-indent-line-or-region ()
>   (interactive)
>   (if mark-active
> (increase-left-margin (region-beginning) (region-end) nil)
>     (increase-left-margin (point-at-bol) (point-at-eol) nil))
>   (setq deactivate-mark nil))
>
> (defun easymacs-rigidly-unindent-line-or-region ()
>   (interactive)
>   (if mark-active
> (decrease-left-margin (region-beginning) (region-end) nil)
>     (decrease-left-margin (point-at-bol) (point-at-eol) nil))
>   (setq deactivate-mark nil))
>
> (global-set-key [tab]       'easymacs-rigidly-indent-line-or-region)
> (global-set-key [S-tab]       'easymacs-rigidly-unindent-line-or-
> region)

Hummm I want that also :)

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Use tab key to indent both region AND line?
  2007-04-20 17:16       ` Lennart Borgman (gmail)
@ 2007-04-20 22:41         ` Lennart Borgman (gmail)
  0 siblings, 0 replies; 9+ messages in thread
From: Lennart Borgman (gmail) @ 2007-04-20 22:41 UTC (permalink / raw)
  Cc: help-gnu-emacs, weber

Lennart Borgman (gmail) wrote:
> weber wrote:
> 
>> (defun indent-line-or-region ()
>>   "indent line or region"
>>   (interactive)
>>   (if mark-active   ;; there is a region selected
>>       (indent-region)
>>       (indent-according-to-mode))) ;; indent line
>>
>> Just bind it to tab.
>>
>> Hope i could help you,
>> -weber
> 
> 
> And here is something that can be used:
> 
> ;; From an idea by weber <hugows@gmail.com>
> (defun indent-line-or-region ()
>   "indent line or region"
>   (interactive)
>   (if mark-active ;; there is a region selected
>       (indent-region (region-beginning) (region-end))
>     (indent-according-to-mode))) ;; indent line
> 
> (define-minor-mode indent-line-mode
>   "Use TAB to indent line and region."
>   :global t
>   :keymap '(([tab] . indent-line-or-region)))
> (when indent-line-mode (indent-line-mode 1))


And here is something that can actually be used ;-)

After using the above for some minutes I realized that you do not want 
to indent in all buffers. Here is a heuristic solution to that problem:

;; From an idea by weber <hugows@gmail.com>
(defun indent-line-or-region ()
   "indent line or region"
   (interactive)
   ;; Do a wild guess if we should indent or not ...
   (let* ((indent-line-mode)
          (t-bound (key-binding [?\t]))
          (do-indent)
         )
     (if (not
          (save-match-data
            (string-match "indent" (symbol-name t-bound))))
         (call-interactively t-bound t)
       (if mark-active ;; there is a region selected
           (indent-region (region-beginning) (region-end))
         (indent-according-to-mode))))) ;; indent line

(define-minor-mode indent-line-mode
   "Use TAB to indent line and region."
   :global t
   :keymap '(([?\t] . indent-line-or-region)))
(when indent-line-mode (indent-line-mode 1))

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2007-04-20 22:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-20 12:10 Use tab key to indent both region AND line? mopi
2007-04-20 12:45 ` Denis Bueno
2007-04-20 13:58   ` Livin Stephen Sharma
     [not found]   ` <mailman.2307.1177077814.7795.help-gnu-emacs@gnu.org>
2007-04-20 16:01     ` weber
2007-04-20 17:16       ` Lennart Borgman (gmail)
2007-04-20 22:41         ` Lennart Borgman (gmail)
     [not found]       ` <mailman.2326.1177089678.7795.help-gnu-emacs@gnu.org>
2007-04-20 17:54         ` mopi
2007-04-20 19:07           ` mopi
2007-04-20 22:12             ` weber

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.