Title: C Automatic indentation recognition Ref: sub30 Author: Jon Green Date: Thu Mar 25 21:45:20 2004 A restriction of the current ANSI C, C++ and Java modes is that the electric-c indentation is fixed to 4 characters by default. This is a problem when maintaining files that use a different indentation scheme. The following extension is added to myc.emf (also applicable to C++ and Java in exactly the same way) and determines the indention of the existing code and re-configures the indentation to the new scheme determined from the file contents. This is the very first implementation and some more work needs to be done to confirm the correct indentation. The macro should consider literal TABs with non-standard tab width. Extract the code body to a file called "myc.emf" (or append to the end of an existing file) and add this to your private "jasspa" directory which is:- ~.jasspa on UNIX. $APPDATA\jasspa on Windows. ==== myc.emf start ========================================================= ; -!- emf -!- ; This is part of the JASSPA MicroEmacs macro files ; Copyright (C) 1999-2004 JASSPA (www.jasspa.com) ; See the file me.emf for copying and conditions. ; ; Created: Thu Mar 25 21:49:50 2004 ; Synopsis: Private C extension to find correct indentation ; Authors: Jon Green ; ; Notes: This is a very crude search for the indentation. Need to ; do more work to process TABs and to validate indentation. ; ; The macro "c-indent" allows the indentation to be re-defined ; by the user. ; Set up the correct indentation define-macro my-fhook-c ; Save the old indentation set-variable :c-brace $c-brace set-variable :c-case $c-case set-variable :c-statement $c-statement set-position "\x91" ; Find the current indentation beginning-of-buffer !force search-forward "^{" set-variable #l0 $status !if ¬ #l0 ; K&R !force search-forward "^\\w.*{" set-variable #l0 $status !endif beginning-of-line !if #l0 !force forward-line !if $status !force search-forward "^[ \t]+\\w" !if $status set-variable #l1 &sub $window-acol 1 set-variable :c-brace &neg #l1 set-variable :c-case &neg #l1 set-variable :c-statement #l1 !endif !endif !endif goto-position "\x91" !emacro ; Swap to buffer context. define-macro my-bhook-c ; Set up the default constants set-variable :oc-brace $c-brace set-variable $c-brace :c-brace set-variable :oc-case $c-case set-variable $c-case :c-case set-variable :oc-statement $c-statement set-variable $c-statement :c-statement !emacro ; Swap away from buffer context define-macro my-ehook-c ; Swap out the default constants set-variable :c-brace $c-brace set-variable $c-brace :oc-brace set-variable :c-case $c-case set-variable $c-case :oc-case set-variable :c-statement $c-statement set-variable $c-statement :oc-statement !emacro ; Macro to allow manual over-ride of the indent. define-macro c-indent set-variable #l0 @ml2 "Indentation" $c-statement !if ¬ &gre #l0 0 ml-write &cat "Invalid value " #l0 !abort !endif set-variable $c-brace &neg #l0 set-variable $c-case &neg #l0 set-variable $c-statement #l0 !emacro ==== myc.emf end ===========================================================