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