home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / modes / cc-guess.el < prev    next >
Encoding:
Text File  |  1995-08-29  |  3.6 KB  |  101 lines

  1. ;;; cc-guess.el --- guess indentation values by scanning existing code
  2.  
  3. ;; Copyright (C) 1994-1995 Free Software Foundation, Inc.
  4.  
  5. ;; Author:        1994-1995 Barry A. Warsaw
  6. ;; Maintainer:    cc-mode-help@merlin.cnri.reston.va.us
  7. ;; Created:       August 1994, split from cc-mode.el
  8. ;; Version:       1.7
  9. ;; Last Modified: 1995/08/28 20:39:43
  10. ;; Keywords: c languages oop
  11.  
  12. ;; This file is not part of GNU Emacs.
  13.  
  14. ;; This program is free software; you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation; either version 2 of the License, or
  17. ;; (at your option) any later version.
  18. ;; 
  19. ;; This program is distributed in the hope that it will be useful,
  20. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22. ;; GNU General Public License for more details.
  23. ;; 
  24. ;; You should have received a copy of the GNU General Public License
  25. ;; along with this program; if not, write to the Free Software
  26. ;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  27.  
  28. ;;; Commentary:
  29. ;;
  30. ;; This file contains routines that help guess the cc-mode style in a
  31. ;; particular region of C, C++, or Objective-C code.  It is provided
  32. ;; for example and experimentation only.  It is not supported in
  33. ;; anyway.  Some folks have asked for a style guesser and the best way
  34. ;; to show my thoughts on the subject is with this sample code.  Feel
  35. ;; free to improve upon it in anyway you'd like.  Please send me the
  36. ;; results.  Note that style guessing is lossy!
  37. ;;
  38. ;; The way this is intended to be run is for you to mark a region of
  39. ;; code to guess the style of, then run the command, cc-guess-region.
  40.  
  41. ;;; Code:
  42.  
  43. (defvar cc-guessed-style nil
  44.   "Currently guessed style.")
  45.  
  46. (defvar cc-guess-conversions
  47.   '((c . c-lineup-C-comments)
  48.     (inher-cont . c-lineup-multi-inher)
  49.     (string . -1000)
  50.     (comment-intro . c-lineup-comment)
  51.     (arglist-cont-nonempty . c-lineup-arglist)
  52.     (cpp-macro . -1000)))
  53.   
  54.  
  55. (defun cc-guess-region (start end &optional reset)
  56.   "Sets `c-offset-alist' indentation values based on region of code.
  57. Every line of code in the region is examined and the indentation
  58. values of the various syntactic symbols in `c-offset-alist' is
  59. guessed.  The first such positively identified indentation is used, so
  60. if an inconsistent style exists in the C code, the guessed indentation
  61. may be incorrect.
  62.  
  63. Note that the larger the region to guess in, the slower the
  64. guessing. Previous guesses can be concatenated together, unless the
  65. optional RESET is provided.
  66.  
  67. See `cc-guess-write-style' to find out how to save the guessed style,
  68. and `cc-guess-view-style' for viewing the guessed style."
  69.   (interactive "r\nP")
  70.   (if (consp reset)
  71.       (setq cc-guessed-style nil))
  72.   (save-excursion
  73.     (goto-char start)
  74.     (while (< (point) end)
  75.       (let* ((syntax (c-guess-basic-syntax))
  76.          (relpos (cdr (car syntax)))
  77.          (symbol (car (car syntax)))
  78.          point-indent relpos-indent)
  79.     ;; TBD: for now I can't guess indentation when more than 1
  80.     ;; symbol is on the list, nor for symbols without relpos's
  81.     (if (or (/= 1 (length syntax))
  82.         (not (numberp relpos))
  83.         ;; also, don't try to reguess an already guessed
  84.         ;; symbol
  85.         (assq symbol cc-guessed-style))
  86.         nil
  87.       (back-to-indentation)
  88.       (setq point-indent (current-column)
  89.         relpos-indent (save-excursion
  90.                 (goto-char relpos)
  91.                 (current-column)))
  92.       ;; guessed indentation is the difference between point's and
  93.       ;; relpos's current-column indentation
  94.       (setq cc-guessed-style
  95.         (cons (cons symbol (- point-indent relpos-indent))
  96.               cc-guessed-style))
  97.       ))
  98.       (forward-line 1))))
  99.  
  100. ;;; cc-guess.el ends here
  101.