home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Fresh Fish 4
/
FreshFish_May-June1994.bin
/
bbs
/
gnu
/
emacs-18.59-bin.lha
/
lib
/
emacs
/
18.59
/
lisp
/
c-mode.el
< prev
next >
Wrap
Lisp/Scheme
|
1992-09-23
|
24KB
|
663 lines
;; C code editing commands for Emacs
;; Copyright (C) 1985, 1986, 1987 Free Software Foundation, Inc.
;; This file is part of GNU Emacs.
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 1, or (at your option)
;; any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
(defvar c-mode-abbrev-table nil
"Abbrev table in use in C-mode buffers.")
(define-abbrev-table 'c-mode-abbrev-table ())
(defvar c-mode-map ()
"Keymap used in C mode.")
(if c-mode-map
()
(setq c-mode-map (make-sparse-keymap))
(define-key c-mode-map "{" 'electric-c-brace)
(define-key c-mode-map "}" 'electric-c-brace)
(define-key c-mode-map ";" 'electric-c-semi)
(define-key c-mode-map ":" 'electric-c-terminator)
(define-key c-mode-map "\e\C-h" 'mark-c-function)
(define-key c-mode-map "\e\C-q" 'indent-c-exp)
(define-key c-mode-map "\177" 'backward-delete-char-untabify)
(define-key c-mode-map "\t" 'c-indent-command))
(autoload 'c-macro-expand "cmacexp"
"Display the result of expanding all C macros occurring in the region.
The expansion is entirely correct because it uses the C preprocessor."
t)
(defvar c-mode-syntax-table nil
"Syntax table in use in C-mode buffers.")
(if c-mode-syntax-table
()
(setq c-mode-syntax-table (make-syntax-table))
(modify-syntax-entry ?\\ "\\" c-mode-syntax-table)
(modify-syntax-entry ?/ ". 14" c-mode-syntax-table)
(modify-syntax-entry ?* ". 23" c-mode-syntax-table)
(modify-syntax-entry ?+ "." c-mode-syntax-table)
(modify-syntax-entry ?- "." c-mode-syntax-table)
(modify-syntax-entry ?= "." c-mode-syntax-table)
(modify-syntax-entry ?% "." c-mode-syntax-table)
(modify-syntax-entry ?< "." c-mode-syntax-table)
(modify-syntax-entry ?> "." c-mode-syntax-table)
(modify-syntax-entry ?& "." c-mode-syntax-table)
(modify-syntax-entry ?| "." c-mode-syntax-table)
(modify-syntax-entry ?\' "\"" c-mode-syntax-table))
(defconst c-indent-level 2
"*Indentation of C statements with respect to containing block.")
(defconst c-brace-imaginary-offset 0
"*Imagined indentation of a C open brace that actually follows a statement.")
(defconst c-brace-offset 0
"*Extra indentation for braces, compared with other text in same context.")
(defconst c-argdecl-indent 5
"*Indentation level of declarations of C function arguments.")
(defconst c-label-offset -2
"*Offset of C label lines and case statements relative to usual indentation.")
(defconst c-continued-statement-offset 2
"*Extra indent for lines not starting new statements.")
(defconst c-continued-brace-offset 0
"*Extra indent for substatements that start with open-braces.
This is in addition to c-continued-statement-offset.")
(defconst c-auto-newline nil
"*Non-nil means automatically newline before and after braces,
and after colons and semicolons, inserted in C code.")
(defconst c-tab-always-indent t
"*Non-nil means TAB in C mode should always reindent the current line,
regardless of where in the line point is when the TAB command is used.")
(defun c-mode ()
"Major mode for editing C code.
Expression and list commands understand all C brackets.
Tab indents for C code.
Comments are delimited with /* ... */.
Paragraphs are separated by blank lines only.
Delete converts tabs to spaces as it moves back.
\\{c-mode-map}
Variables controlling indentation style:
c-tab-always-indent
Non-nil means TAB in C mode should always reindent the current line,
regardless of where in the line point is when the TAB command is used.
c-auto-newline
Non-nil means automatically newline before and after braces,
and after colons and semicolons, inserted in C code.
c-indent-level
Indentation of C statements within surrounding block.
The surrounding block's indentation is the indentation
of the line on which the open-brace appears.
c-continued-statement-offset
Extra indentation given to a substatement, such as the
then-clause of an if or body of a while.
c-continued-brace-offset
Extra indentation given to a brace that starts a substatement.
This is in addition to c-continued-statement-offset.
c-brace-offset
Extra indentation for line if it starts with an open brace.
c-brace-imaginary-offset
An open brace following other text is treated as if it were
this far to the right of the start of its line.
c-argdecl-indent
Indentation level of declarations of C function arguments.
c-label-offset
Extra indentation for line that is a label, or case or default.
Settings for K&R and BSD indentation styles are
c-indent-level 5 8
c-continued-statement-offset 5 8
c-brace-offset -5 -8
c-argdecl-indent 0 8
c-label-offset -5 -8
Turning on C mode calls the value of the variable c-mode-hook with no args,
if that value is non-nil."
(interactive)
(kill-all-local-variables)
(use-local-map c-mode-map)
(setq major-mode 'c-mode)
(setq mode-name "C")
(setq local-abbrev-table c-mode-abbrev-table)
(set-syntax-table c-mode-syntax-table)
(make-local-variable 'paragraph-start)
(setq paragraph-start (concat "^$\\|" page-delimiter))
(make-local-variable 'paragraph-separate)
(setq paragraph-separate paragraph-start)
(make-local-variable 'paragraph-ignore-fill-prefix)
(setq paragraph-ignore-fill-prefix t)
(make-local-variable 'indent-line-function)
(setq indent-line-function 'c-indent-line)
(make-local-variable 'require-final-newline)
(setq require-final-newline t)
(make-local-variable 'comment-start)
(setq comment-start "/* ")
(make-local-variable 'comment-end)
(setq comment-end " */")
(make-local-variable 'comment-column)
(setq comment-column 32)
(make-local-variable 'comment-start-skip)
(setq comment-start-skip "/\\*+ *")
(make-local-variable 'comment-indent-hook)
(setq comment-indent-hook 'c-comment-indent)
(make-local-variable 'parse-sexp-ignore-comments)
(setq parse-sexp-ignore-comments t)
(run-hooks 'c-mode-hook))
;; This is used by indent-for-comment
;; to decide how much to indent a comment in C code
;; based on its context.
(defun c-comment-indent ()
(if (looking-at "^/\\*")
0 ;Existing comment at bol stays there.
(save-excursion
(skip-chars-backward " \t")
(max (1+ (current-column)) ;Else indent at comment column
comment-column)))) ; except leave at least one space.
(defun electric-c-brace (arg)
"Insert character and correct line's indentation."
(interactive "P")
(let (insertpos)
(if (and (not arg)
(eolp)
(or (save-excursion
(skip-chars-backward " \t")
(bolp))
(if c-auto-newline (progn (c-indent-line) (newline) t) nil)))
(progn
(insert last-command-char)
(c-indent-line)
(if c-auto-newline
(progn
(newline)
;; (newline) may have done auto-fill
(setq insertpos (- (point) 2))
(c-indent-line)))
(save-excursion
(if insertpos (goto-char (1+ insertpos)))
(delete-char -1))))
(if insertpos
(save-excursion
(goto-char insertpos)
(self-insert-command (prefix-numeric-value arg)))
(self-insert-command (prefix-numeric-value arg)))))
(defun electric-c-semi (arg)
"Insert character and correct line's indentation."
(interactive "P")
(if c-auto-newline
(electric-c-terminator arg)
(self-insert-command (prefix-numeric-value arg))))
(defun electric-c-terminator (arg)
"Insert character and correct line's indentation."
(interactive "P")
(let (insertpos (end (point)))
(if (and (not arg) (eolp)
(not (save-excursion
(beginning-of-line)
(skip-chars-forward " \t")
(or (= (following-char) ?#)