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