comparison lisp/cc-mode/cc-guess.el @ 165:5a88923fcbfe r20-3b9

Import from CVS: tag r20-3b9
author cvs
date Mon, 13 Aug 2007 09:44:42 +0200
parents
children
comparison
equal deleted inserted replaced
164:4e0740e5aab2 165:5a88923fcbfe
1 ;;; cc-guess.el --- guess indentation values by scanning existing code
2
3 ;; Copyright (C) 1985,87,92,93,94,95,96,97 Free Software Foundation, Inc.
4
5 ;; Author: 1994-1995 Barry A. Warsaw
6 ;; Maintainer: Unmaintained
7 ;; Created: August 1994, split from cc-mode.el
8 ;; Version: 5.11
9 ;; Keywords: c languages oop
10
11 ;; This file is not part of GNU Emacs.
12
13 ;; This program is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2 of the License, or
16 ;; (at your option) any later version.
17 ;;
18 ;; This program is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22 ;;
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, 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