annotate lisp/modes/cc-guess.el @ 0:376386a54a3c r19-14

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