;;; Copyright (C) 2002 Thomas Mailund ;;; This file is not part of GNU Emacs, but it is distributed under the ;;; same conditions. ;;; ==================================================================== ;;; This program is free software; you can redistribute it and/or ;;; modify it under the terms of the GNU General Public License as ;;; published by the Free Software Foundation; either version 2, or (at ;;; your option) any later version. ;;; This program is distributed in the hope that it will be useful, but ;;; WITHOUT ANY WARRANTY; without even the implied warranty of ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;;; General Public License for more details. ;;; You should have received a copy of the GNU General Public License ;;; along with this software; see the file COPYING. If not, write to the ;;; Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. ;;; ==================================================================== (require 'dprog-constants) (provide 'dprog-indent) (defun dprog-indent-to (column) "Indent this line to `column'." (let ((cur-column (current-column)) (dummy (back-to-indentation)) (cur-indent (current-column))) ;; handle indentation (delete-horizontal-space) (indent-to-column column) ;; position point afterwards (if (<= cur-column cur-indent) (move-to-column column) (move-to-column (+ cur-column (- column cur-indent))) ))) (defun dprog-open-close-search (nesting) (progn (if (re-search-backward (concat "\\(" dprog-open-block "\\|" dprog-close-block "\\)") 0 t) (let ((new-nesting (if (looking-at dprog-open-block) (- nesting 1) (+ nesting 1)))) (if (= new-nesting 0) (+ (current-column) 1) (dprog-open-close-search new-nesting))) 0))) (defun dprog-find-block-indent-level nil "Find the level of indentation inside a block for the line containing point." (save-excursion (back-to-indentation) (dprog-open-close-search 1))) (defun dprog-find-kw-indent-level nil "Find the level of indentation inside a keyword block." (save-excursion (back-to-indentation) (if (looking-at dprog-block-kw-regex) 0 tab-width))) (defun dprog-find-indent-level nil "Find the level of indentation for the line containing point." (let ((indent-level (dprog-find-block-indent-level))) (if (= indent-level 0) ;; if indent-level is 0 here, we're out of all {} blocks--try ;; keyword blocks (dprog-find-kw-indent-level) indent-level))) (defun dprog-indent-line nil "Indent current line." (let ((indent-level (dprog-find-indent-level))) (dprog-indent-to indent-level)))