diff options
author | David Robillard <d@drobilla.net> | 2017-12-10 14:34:27 +0100 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2017-12-10 14:39:53 +0100 |
commit | d2da4e5d2866083b8159f57eb04da01662b0dc17 (patch) | |
tree | 14fd5bf57fbaedccd00d1490639319c40b163638 /emacs/.emacs.d/site-lisp/ppindent.el | |
parent | ba6f1ea48ccc54f0afdd5eafee65ad6bf9b502c6 (diff) | |
download | dotfiles-d2da4e5d2866083b8159f57eb04da01662b0dc17.tar.gz dotfiles-d2da4e5d2866083b8159f57eb04da01662b0dc17.tar.bz2 dotfiles-d2da4e5d2866083b8159f57eb04da01662b0dc17.zip |
Make dotfiles stow compatible
Diffstat (limited to 'emacs/.emacs.d/site-lisp/ppindent.el')
-rw-r--r-- | emacs/.emacs.d/site-lisp/ppindent.el | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/emacs/.emacs.d/site-lisp/ppindent.el b/emacs/.emacs.d/site-lisp/ppindent.el new file mode 100644 index 0000000..84c9702 --- /dev/null +++ b/emacs/.emacs.d/site-lisp/ppindent.el @@ -0,0 +1,138 @@ +;;; ppindent.el --- Indents C preprocessor directives + +;; Copyright (C) 2007 Free Software Foundation, Inc. + +;; Author: Craig McDaniel <craigmcd@gmail.com> +;; Keywords: languages, c +;; Maintainer: Craig McDaniel <craigmcd@gmail.com> + +;; This file 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 file 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 GNU Emacs; see the file COPYING. If not, write to +;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, +;; Boston, MA 02111-1307, USA. + +;;; Commentary: + +;; PPINDENT indents C-preprocessor statements depending on the current +;; #if..., #el.., #endif level. It only modifies lines with "#" as the +;; first non-blank character and therefore does not conflict with the +;; normal C indentation. Unlike a true indent, it inserts the +;; requisite number of spaces after, rather than before the "#" +;; character. This type of preprocesor indent is commonly used to +;; provide the most compatibility with different C-compilers. +;; +;; Example: + +;; #ifdef WOO +;; .... +;; #if defined(X) && !defined(Y) +;; #ifdef Q8 +;; ... +;; #else +;; ... +;; .... +;; #elif defined (Z) +;; .... +;; #endif +;; #endif +;; #endif + +;; After "M-x ppindent-c" becomes: + +;; #ifdef WOO +;; .... +;; # if defined(X) && !defined(Y) +;; # ifdef Q8 +;; ... +;; # else +;; ... +;; .... +;; # elif defined (Z) +;; .... +;; # endif +;; # endif +;; #endif + +;; Two functions are provided: PPINDENT-C indents as described +;; above. PPINDENT-H does not indent the first level, assuming that +;; .h/.hpp files use an #ifdef guard around the entire file. + +;; You can customize PPINDENT-INCREMENT if you want to use something +;; other than 2 spaces for the indent. + +;;; History: + +;; 2007-01-19 WCM Initial version + +;;; Code: + +(provide 'ppindent) + +(defgroup pp-indent nil + "Indent C preproccessor directives." + :group 'c) + +(defcustom ppindent-increment 2 + "Number of spaces per indention level. + +Used in C pre-processor indent functions ppindent-c and ppindent-h" + :type 'number + :group 'pp-indent) + +(defun starts-withp (str prefix) + "str starts with prefix" + (eql (compare-strings prefix nil nil str nil (length prefix)) t)) + +(defun my-make-string (length init) + "just like make-string, but makes an empty string if length is negative" + (when (minusp length) + (setf length 0)) + (make-string length init)) + +(defun ppindent-aux (start) + (let ((cnt start)) + (goto-char (point-min)) + (while (re-search-forward "^[ \t]*#[ \t]*\\(.*\\)" nil t) + (cond ((starts-withp (match-string-no-properties 1) "if") + (replace-match (concat "#" (my-make-string cnt ?\s) "\\1")) + (incf cnt ppindent-increment)) + ((starts-withp (match-string-no-properties 1) "el") + (when (< (- cnt ppindent-increment) start) + (throw 'err `(,(line-number-at-pos) "Unmatched #else or #elif"))) + (replace-match (concat "#" (my-make-string + (- cnt ppindent-increment) + ?\s) "\\1"))) + ((starts-withp (match-string-no-properties 1) "endif") + (when (< (- cnt ppindent-increment) start) + (throw 'err `(,(line-number-at-pos) "Unmatched #endif"))) + (decf cnt ppindent-increment) + (replace-match (concat "#" (my-make-string cnt ?\s) "\\1"))) + (t + (replace-match (concat "#" (my-make-string cnt ?\s) "\\1"))))))) + +(defun ppindent-buffer (start) + (let ((result (catch 'err (save-excursion (ppindent-aux start))))) + (when result + (goto-line (car result)) + (error "Error: %s" (cadr result))))) + +(defun ppindent-c () + "Indent all C pre-processor statements" + (interactive) + (ppindent-buffer 0)) + +(defun ppindent-h () + "Indent C pre-processor statements, keeping first level #ifdef unindented" + (interactive) + (ppindent-buffer (- ppindent-increment))) + |