[Top][All Lists]

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

Re: creating a function that works for active region or whole buffer

From: Luca Ferrari
Subject: Re: creating a function that works for active region or whole buffer
Date: Wed, 23 Jan 2013 08:43:01 +0100

Ops...right answer to the wrong thread, sorry!
However below my function that is a simple indent behavior
(suggestions are welcome):

defun dataflex-indent-region-or-buffer ()
"This function indents the whole buffer or, in the case a region is
active, the active region."

  (let ( (current-block-end-line 0 ) (current-block-start-line 0)
(current-indentation-step 0) (next-line-indentation-step 0) )

      ;;  if using a region indent only such region, otherwise the whole buffer
      (if (use-region-p)
          ;;  using a region...
          (setq current-block-start-line (line-number-at-pos
(region-beginning) )
                current-block-end-line   (line-number-at-pos (region-end) ) )
        ;;  ...else use the whole buffer
        (setq current-block-start-line (line-number-at-pos (point-min) )
              current-block-end-line   (line-number-at-pos (point-max) ) ) )

      ;;  go to the starting line
      (goto-line current-block-start-line)

      ;;  go to the beginning of the line

    (while (<= current-block-start-line current-block-end-line )
      (if (looking-at "^[ \t]*\\(IF\\|WHILE\\|BEGIN\\|LOOP\\)")
          ;; the BEGIN line has to be indented at the current level,
and the next
          ;;  line at a deeper level
          (setq next-line-indentation-step (+ current-indentation-step
dataflex-mode-indent-step) )
        ;;   else if looking at an END line remove the indentation
        (if (looking-at "^[ \t]*\\(END\\|RETURN\\|ABORT\\)")
              (setq current-indentation-step (-
current-indentation-step dataflex-mode-indent-step) )
              (setq next-line-indentation-step current-indentation-step ) )
          ;;  else if this is a label line reset the indentation
          (if (looking-at  "^[ \t]*\\(.+\\):[ \t\n]*")
              (setq current-indentation-step 0
next-line-indentation-step dataflex-mode-indent-step-labels) ) ) )

      ;;  security check: do not indent at negative offset
      (if (< current-indentation-step 0 )
          (setq current-indentation-step 0) )
      (if (< next-line-indentation-step 0 )
          (setq next-line-indentation-step 0 ) )

      ;;  do the indent of the current line and go forward
      (indent-line-to current-indentation-step)
      (setq current-indentation-step next-line-indentation-step)
      (setq current-block-start-line (1+ current-block-start-line))
      (forward-line 1) ) ) ) )

reply via email to

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