help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Indenting paragraphs manually


From: Teemu Likonen
Subject: Re: Indenting paragraphs manually
Date: Mon, 07 Mar 2011 10:18:26 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.2.94 (gnu/linux)

* 2011-03-06 23:44 (+0200), Teemu Likonen wrote:

> Do you mean the the next tab stop according to tab-stop-list variable?
> If the indentation of lines in the current region differs between
> lines, which line should be indented to the next tab stop position?
> Maybe the one with the smallest amount of indentation (excluding blank
> lines)? Here's a quick example:

I found that feature quite useful so I wrote a proper version. I bind it
to "C-x TAB" since this pretty much supersedes indent-rigidly. See the
command's documentation (below) for more info.


(global-set-key (kbd "C-c TAB") #'tl-indent-region)


(defun tl-region-indentation (beg end)
  "Return the smallest indentation in range from BEG to END.
Blank lines are ignored."
  (save-excursion
    (let ((beg (progn (goto-char beg) (line-beginning-position)))
          (end (progn (goto-char end) (line-end-position)))
          indent)
      (goto-char beg)
      (while (re-search-forward "^\\s-*[[:print:]]" end t)
        (setq indent (min (or indent (current-indentation))
                          (current-indentation))))
      indent)))


(defun tl-indent-region-next-tab-stop (beg end)
  (let* ((current (tl-region-indentation beg end))
         (next (catch 'answer
                 (dolist (i tab-stop-list (or (car (last tab-stop-list)) 0))
                   (when (> i current)
                     (throw 'answer i))))))
    (indent-rigidly beg end (- next current))))


(defun tl-indent-region-previous-tab-stop (beg end)
  (let* ((current (tl-region-indentation beg end))
         (prev (catch 'answer
                 (dolist (i (reverse (cons 0 tab-stop-list)) 0)
                   (when (< i current)
                     (throw 'answer i))))))
    (indent-rigidly beg end (- prev current))))


(defun tl-indent-region-engine (beg end arg)
  (interactive "r\nP")
  (let ((current (tl-region-indentation beg end)))
    (cond ((not arg)
           (tl-indent-region-next-tab-stop beg end))
          ((eq arg '-)
           (tl-indent-region-previous-tab-stop beg end))
          (t
           (indent-rigidly beg end (prefix-numeric-value arg))))))


(defun tl-indent-region (beg end arg)
  "Indent region to a tab stop column or to a specified column.

Indent the region from BEG to END according to the command's
prefix argument ARG. If ARG is nil (i.e., there is no prefix
argument) indent the region to the next tab stop column in
`tab-stop-list'. If ARG is negative indent the region to the
previous tab stop column. If ARG is a positive or negative
integer indent the region by ARG columns (just like
`indent-rigidly' command) .

If this command is invoked by a multi-character key sequence, it
can be repeated by repeating the final character of the
sequence."

  (interactive "r\nP")
  (let ((repeat-message-function #'ignore))
    (setq last-repeatable-command #'tl-indent-region-engine)
    (repeat nil)))



reply via email to

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