annotate lisp/modes/cc-guess.el @ 128:13db4aa44a2e

Added tag xemacs-20-1p2 for changeset 7b236889ca3a
author cvs
date Mon, 13 Aug 2007 09:28:11 +0200
parents b82b59fe008d
children
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 ;;; Commentary:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ;; This file contains routines that help guess the cc-mode style in a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ;; particular region of C, C++, or Objective-C code. It is provided
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ;; for example and experimentation only. It is not supported in
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ;; anyway. Some folks have asked for a style guesser and the best way
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ;; to show my thoughts on the subject is with this sample code. Feel
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ;; 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
36 ;; results. Note that style guessing is lossy!
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ;;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ;; 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
39 ;; code to guess the style of, then run the command, cc-guess-region.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ;;; Code:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 (defvar cc-guessed-style nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 "Currently guessed style.")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 (defvar cc-guess-conversions
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47 '((c . c-lineup-C-comments)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 (inher-cont . c-lineup-multi-inher)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 (string . -1000)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50 (comment-intro . c-lineup-comment)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 (arglist-cont-nonempty . c-lineup-arglist)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 (cpp-macro . -1000)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 (defun cc-guess-region (start end &optional reset)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 "Sets `c-offset-alist' indentation values based on region of code.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57 Every line of code in the region is examined and the indentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 values of the various syntactic symbols in `c-offset-alist' is
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 guessed. The first such positively identified indentation is used, so
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 if an inconsistent style exists in the C code, the guessed indentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61 may be incorrect.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 Note that the larger the region to guess in, the slower the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
64 guessing. Previous guesses can be concatenated together, unless the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 optional RESET is provided.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 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
68 and `cc-guess-view-style' for viewing the guessed style."
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 (interactive "r\nP")
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 (if (consp reset)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 (setq cc-guessed-style nil))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73 (goto-char start)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 (while (< (point) end)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 (let* ((syntax (c-guess-basic-syntax))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 (relpos (cdr (car syntax)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 (symbol (car (car syntax)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 point-indent relpos-indent)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 ;; TBD: for now I can't guess indentation when more than 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
80 ;; symbol is on the list, nor for symbols without relpos's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 (if (or (/= 1 (length syntax))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 (not (numberp relpos))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 ;; also, don't try to reguess an already guessed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
84 ;; symbol
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 (assq symbol cc-guessed-style))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 nil
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 (back-to-indentation)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 (setq point-indent (current-column)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 relpos-indent (save-excursion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 (goto-char relpos)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91 (current-column)))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
92 ;; guessed indentation is the difference between point's and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
93 ;; relpos's current-column indentation
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94 (setq cc-guessed-style
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 (cons (cons symbol (- point-indent relpos-indent))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 cc-guessed-style))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 ))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 (forward-line 1))))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
99
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
100 ;;; cc-guess.el ends here