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

📄 c++-mode.el

📁 早期freebsd实现
💻 EL
📖 第 1 页 / 共 5 页
字号:
	  (self-insert-command (prefix-numeric-value arg)))      (self-insert-command (prefix-numeric-value arg)))))(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 t (the default), always just indent thecurrent line.  If nil, indent the current line only if point is at theleft margin or in the line's indentation; otherwise insert a tab.  Ifnot-nil-or-t, then tab is inserted only within literals (comments andstrings) and inside preprocessor directives, but line is always reindented.A numeric argument, regardless of its value, means indent rigidly allthe lines of the expression starting after point so that this linebecomes properly indented.  The relative indentation among the linesof the expression are preserved."  (interactive "P")  (let ((bod (c++-point 'bod)))    (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 bod))	      beg end)	  (save-excursion	    (if (eq c++-tab-always-indent t)		(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 "#")))      (cond       ((eq c++-tab-always-indent nil)	(if (and (save-excursion		   (skip-chars-backward " \t")		   (bolp))		 (or (looking-at "[ \t]*$")		     (/= (point) (c++-point 'boi))))	    (c++-indent-line bod)	  (insert-tab)))       ((eq c++-tab-always-indent t)	(c++-indent-line bod))       ((or (memq (c++-in-literal bod) '(c c++ string))	    (save-excursion	      (skip-chars-backward " \t")	      (= (preceding-char) ?#)))	(let ((here (point))	      (boi (save-excursion (back-to-indentation) (point)))	      (indent-p nil))	  (c++-indent-line bod)	  (save-excursion	    (back-to-indentation)	    (setq indent-p (and (> here boi) (= (point) boi))))	  (if indent-p (insert-tab))))       (t (c++-indent-line bod))))))(defun c++-indent-exp ()  "Indent each line of the C++ grouping following point."  (interactive)  (let ((indent-stack (list nil))	(contain-stack (list (point)))	(case-fold-search nil)	restart outer-loop-done inner-loop-done state ostate	this-indent last-sexp last-depth	at-else at-brace	(opoint (point))	(next-depth 0))    (save-excursion      (forward-sexp 1))    (save-excursion      (setq outer-loop-done nil)      (while (and (not (eobp)) (not outer-loop-done))	(setq last-depth next-depth)	;; Compute how depth changes over this line	;; plus enough other lines to get to one that	;; does not end inside a comment or string.	;; Meanwhile, do appropriate indentation on comment lines.	(setq inner-loop-done nil)	(while (and (not inner-loop-done)		    (not (and (eobp) (setq outer-loop-done t))))	  (setq ostate state)	  ;; fix by reed@adapt.net.com	  ;; must pass in the return past the end of line, so that	  ;; parse-partial-sexp finds it, and recognizes that a "//"	  ;; comment is over. otherwise, state is set that we're in a	  ;; comment, and never gets unset, causing outer-loop to only	  ;; terminate in (eobp). old:	  ;;(setq state (parse-partial-sexp (point)	  ;;(progn (end-of-line) (point))	  ;;nil nil state))	  (let ((start (point))		(line-end (progn (end-of-line) (point)))		(end (progn (forward-char) (point))))	    (setq state (parse-partial-sexp start end nil nil state))	    (goto-char line-end))	  (setq next-depth (car state))	  (if (and (car (cdr (cdr state)))		   (>= (car (cdr (cdr state))) 0))	      (setq last-sexp (car (cdr (cdr state)))))	  (if (or (nth 4 ostate))	      (c++-indent-line))	  (if (or (nth 3 state))	      (forward-line 1)	    (setq inner-loop-done t)))	(if (<= next-depth 0)	    (setq outer-loop-done t))	(if outer-loop-done	    nil	  ;; If this line had ..))) (((.. in it, pop out of the levels	  ;; that ended anywhere in this line, even if the final depth	  ;; doesn't indicate that they ended.	  (while (> last-depth (nth 6 state))	    (setq indent-stack (cdr indent-stack)		  contain-stack (cdr contain-stack)		  last-depth (1- last-depth)))	  (if (/= last-depth next-depth)	      (setq last-sexp nil))	  ;; Add levels for any parens that were started in this line.	  (while (< last-depth next-depth)	    (setq indent-stack (cons nil indent-stack)		  contain-stack (cons nil contain-stack)		  last-depth (1+ last-depth)))	  (if (null (car contain-stack))	      (setcar contain-stack (or (car (cdr state))					(save-excursion (forward-sexp -1)							(point)))))	  (forward-line 1)	  (skip-chars-forward " \t")	  (if (eolp)	      nil	    (if (and (car indent-stack)		     (>= (car indent-stack) 0))		;; Line is on an existing nesting level.		;; Lines inside parens are handled specially.		(if (or (/= (char-after (car contain-stack)) ?{)			(c++-at-top-level-p t))		    (setq this-indent (car indent-stack))		  ;; Line is at statement level.		  ;; Is it a new statement?  Is it an else?		  ;; Find last non-comment character before this line		  (save-excursion		    (setq at-else (looking-at "else\\W"))		    (setq at-brace (= (following-char) ?{))		    (c++-backward-over-syntactic-ws opoint)		    (if (not (memq (preceding-char) '(nil ?\, ?\; ?} ?: ?{)))			;; Preceding line did not end in comma or semi;			;; indent this line  c-continued-statement-offset			;; more than previous.			(progn			  (c-backward-to-start-of-continued-exp			   (car contain-stack))			  (setq this-indent				(+ c-continued-statement-offset				   (current-column)				   (if at-brace c-continued-brace-offset 0))))		      ;; Preceding line ended in comma or semi;		      ;; use the standard indent for this level.		      (if at-else			  (progn (c-backward-to-start-of-if opoint)				 (back-to-indentation)				 (skip-chars-forward "{ \t")				 (setq this-indent (current-column)))			(setq this-indent (car indent-stack))))))	      ;; Just started a new nesting level.	      ;; Compute the standard indent for this level.	      (let ((val (c++-calculate-indent			  (if (car indent-stack)			      (- (car indent-stack))))))		(setcar indent-stack			(setq this-indent val))))	    ;; Adjust line indentation according to its contents 	    (if (looking-at c++-access-key) 		(setq this-indent (+ this-indent c++-access-specifier-offset))	      (if (or (looking-at "case[ \t]")		      (and (looking-at "[A-Za-z]")			   (save-excursion			     (forward-sexp 1)			     (looking-at ":[^:]"))))		  (setq this-indent (max 0 (+ this-indent c-label-offset)))))	    ;; looking at a comment only line?	    (if (looking-at "//\\|/\\*")		(setq this-indent (+ this-indent				     c++-comment-only-line-offset)))	    (if (looking-at "friend[ \t]")		(setq this-indent (+ this-indent c++-friend-offset)))	    (if (= (following-char) ?})		(setq this-indent (- this-indent c-indent-level)))	    (if (= (following-char) ?{)		(setq this-indent (+ this-indent c-brace-offset)))	    ;; Put chosen indentation into effect.	    (or (= (current-column) this-indent)		(= (following-char) ?\#)		(progn		  (delete-region (point) (progn (beginning-of-line) (point)))		  (indent-to this-indent)))	    ;; Indent any comment following the text.	    (or (looking-at comment-start-skip)		(if (re-search-forward		     comment-start-skip		     (save-excursion (end-of-line) (point)) t)		    (progn (indent-for-comment) (beginning-of-line))))))))))(defun c++-fill-C-comment ()  "Fill a C style comment."  (interactive)  (save-excursion    (let ((fill-prefix fill-prefix))      (beginning-of-line 1)      (save-excursion	(re-search-forward comment-start-skip			   (save-excursion (end-of-line) (point))			   t)	(goto-char (match-end 0))	(set-fill-prefix))      (while (looking-at fill-prefix)	(forward-line -1))      (forward-line 1)      (insert-string "\n")      (fill-paragraph nil)      (delete-char -1))))(defun c++-insert-header ()  "Insert header denoting C++ code at top of buffer."  (interactive)  (save-excursion    (goto-char (point-min))    (insert "// "	    "This may look like C code, but it is really "	    "-*- C++ -*-"	    "\n\n")))(defun c++-tame-comments ()  "Backslashifies all untamed in comment regions found in the buffer.This is the best available workaround for an emacs syntax bug inscan-lists which exists at least as recently as v18.58.  Untamedcharacters to escape are defined in the variable c++-untame-characters."  (interactive)  ;; make the list into a valid charset, escaping where necessary  (let ((charset (concat "^" (mapconcat			      (function			       (lambda (char)				 (if (memq char '(?\\ ?^ ?-))				     (concat "\\" (char-to-string char))				   (char-to-string char))))			      c++-untame-characters ""))))    (save-excursion      (beginning-of-buffer)      (while (not (eobp))	(skip-chars-forward charset)	(if (and (not (zerop (following-char)))		 (memq (c++-in-literal) '(c c++))		 (/= (preceding-char) ?\\ ))	    (insert "\\"))	(if (not (eobp))	    (forward-char 1))))));; taken from match-paren.el. Author: unknown(defun c++-match-paren ()  "Jumps to the paren matching the one under point, if there is one."  (interactive)  (cond ((looking-at "[\(\[{]")	 (forward-sexp 1)	 (backward-char))	((looking-at "[])}]")	 (forward-char)	 (backward-sexp 1))	(t (message "Could not find matching paren."))));; ======================================================================;; defuns for parsing syntactic elements;; ======================================================================(defun c++-parse-state (&optional limit)  "Determinate the syntactic state of the code at point.Iteratively uses parse-partial-sexp from point to LIMIT and returnsthe result of parse-partial-sexp at point.  LIMIT is optional anddefaults to point-max."  (setq limit (or limit (point-max)))  (let (state (parse-sexp-ignore-comments t))    (while (< (point) limit)      (setq state (parse-partial-sexp (point) limit 0)))    state))(defun c++-at-top-level-p (wrt &optional bod)  "Return t if point is not inside a containing C++ expression, nilif it is embedded in an expression.  When WRT is non-nil, returns nilif not at the top level with respect to an enclosing class, or thedepth of class nesting at point.  With WRT nil, returns nil if not atthe \"real\" top level.  Optional BOD is the beginning of defun."  (save-excursion    (let ((indent-point (point))	  (case-fold-search nil)	  state containing-sexp paren-depth	  (bod (or bod (c++-point 'bod)))	  foundp)      (goto-char bod)      (setq state (c++-parse-state indent-point)	    containing-sexp (nth 1 state)	    paren-depth (nth 0 state))      (cond       ((eq major-mode 'c++-c-mode)	(and (null containing-sexp) 0))       ((not wrt)	(null containing-sexp))       ((c++-in-parens-p) nil)       ((null containing-sexp) 0)       (t	;; calculate depth wrt containing (possibly nested) classes	(goto-char containing-sexp)	(while (and (setq foundp (re-search-backward				  (concat "[;}]\\|" c++-class-key)				  (point-min) t))		    (let ((bod (c++-point 'bod)))		      (or (c++-in-literal bod)			  (c++-in-parens-p bod)			  ;; see if class key is inside a template spec			  (and (looking-at c++-class-key)			       (progn (skip-chars-backward " \t\n")				      (memq (preceding-char) '(?, ?<))))))))	(if (memq (following-char) '(?} ?\;))	    nil	  (setq state (c++-parse-state containing-sexp))	  (and foundp	       (not (nth 1 state))	       (nth 2 state)	       paren-depth))

⌨️ 快捷键说明

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