⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 c-mode.el

📁 早期freebsd实现
💻 EL
📖 第 1 页 / 共 2 页
字号:
;; 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   -8Turning 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) ?#)			;; Colon is special only after a label, or case ....			;; So quickly rule out most other uses of colon			;; and do no indentation for them.			(and (eq last-command-char ?:)			     (not (looking-at "case[ \t]"))			     (save-excursion			       (forward-word 1)			       (skip-chars-forward " \t")			       (< (point) end)))			(progn			  (beginning-of-defun)			  (let ((pps (parse-partial-sexp (point) end)))			    (or (nth 3 pps) (nth 4 pps) (nth 5 pps))))))))	(progn	  (insert last-command-char)	  (c-indent-line)	  (and c-auto-newline	       (not (c-inside-parens-p))	       (progn		 (newline)		 (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 c-inside-parens-p ()  (condition-case ()      (save-excursion	(save-restriction	  (narrow-to-region (point)			    (progn (beginning-of-defun) (point)))	  (goto-char (point-max))	  (= (char-after (or (scan-lists (point) -1 1) (point-min))) ?\()))    (error nil)))(defun c-indent-command (&optional whole-exp)  "Indent current line as C code, or in some cases insert a tab character.If c-tab-always-indent is non-nil (the default), always indent current line.Otherwise, indent the current line only if point is at the left marginor in the line's indentation; otherwise insert a tab.A numeric argument, regardless of its value,means indent rigidly all the lines of the expression starting after pointso that this line becomes properly indented.The relative indentation among the lines of the expression are preserved."  (interactive "P")  (if whole-exp      ;; If arg, always indent this line as C      ;; and shift remaining lines of expression the same amount.      (let ((shift-amt (c-indent-line))	    beg end)	(save-excursion	  (if c-tab-always-indent	      (beginning-of-line))	  (setq beg (point))	  (forward-sexp 1)	  (setq end (point))	  (goto-char beg)	  (forward-line 1)	  (setq beg (point)))	(if (> end beg)	    (indent-code-rigidly beg end shift-amt "#")))    (if (and (not c-tab-always-indent)	     (save-excursion	       (skip-chars-backward " \t")	       (not (bolp))))	(insert-tab)      (c-indent-line))))(defun c-indent-line ()  "Indent current line as C code.Return the amount the indentation changed by."  (let ((indent (calculate-c-indent nil))	beg shift-amt	(case-fold-search nil)	(pos (- (point-max) (point))))    (beginning-of-line)    (setq beg (point))    (cond ((eq indent nil)	   (setq indent (current-indentation)))	  ((eq indent t)	   (setq indent (calculate-c-indent-within-comment)))	  ((looking-at "[ \t]*#")	   (setq indent 0))	  (t	   (skip-chars-forward " \t")	   (if (listp indent) (setq indent (car indent)))	   (cond ((or (looking-at "case[ \t]")		      (and (looking-at "[A-Za-z]")			   (save-excursion			     (forward-sexp 1)			     (looking-at ":"))))		  (setq indent (max 1 (+ indent c-label-offset))))		 ((and (looking-at "else\\b")		       (not (looking-at "else\\s_")))		  (setq indent (save-excursion				 (c-backward-to-start-of-if)				 (current-indentation))))		 ((= (following-char) ?})		  (setq indent (- indent c-indent-level)))		 ((= (following-char) ?{)		  (setq indent (+ indent c-brace-offset))))))    (skip-chars-forward " \t")    (setq shift-amt (- indent (current-column)))    (if (zerop shift-amt)	(if (> (- (point-max) pos) (point))	    (goto-char (- (point-max) pos)))      (delete-region beg (point))      (indent-to indent)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -