EACS下如何自动调整C续行代码的自动缩进(indent-region不一致)

Vim、Emacs配置和使用
回复
reedarx
帖子: 4
注册时间: 2007-11-29 19:46

EACS下如何自动调整C续行代码的自动缩进(indent-region不一致)

#1

帖子 reedarx » 2007-11-29 20:04

我想让他自动调整续行代码,比如缩进2个空格。注意没有续行符号。

代码: 全选

if (1) {
	stmf_trace(iport->iport_alias, "it's from port init function, iport-%p",
	   iport);
}
复杂点的话,可能需要

代码: 全选

if (1) {
	stmf_trace(iport->iport_alias, "it's from port init function, iport-%p",
	  “current status is not online state-%02x, please check the cable",
	  iport, state);
}
请教高手如何定制emacs
谢谢!
reedarx
帖子: 4
注册时间: 2007-11-29 19:46

看来只能修改cc-cmds.el的c-indent-line了

#2

帖子 reedarx » 2007-11-30 12:14

好像没有别的办法啦。我想直接修改cc-cmds.el得了。
哪位高手有经验,请教一下如何修改?或者注释一下这段源码。

代码: 全选

(defun c-indent-line (&optional syntax quiet ignore-point-pos)
  "Indent the current line according to the syntactic context,
if `c-syntactic-indentation' is non-nil.  Optional SYNTAX is the
syntactic information for the current line.  Be silent about syntactic
errors if the optional argument QUIET is non-nil, even if
`c-report-syntactic-errors' is non-nil.  Normally the position of
point is used to decide where the old indentation is on a lines that
is otherwise empty \(ignoring any line continuation backslash), but
that's not done if IGNORE-POINT-POS is non-nil.  Returns the amount of
indentation change \(in columns)."

  (let ((line-cont-backslash (save-excursion
			       (end-of-line)
			       (eq (char-before) ?\\)))
	(c-fix-backslashes c-fix-backslashes)
	bs-col
	shift-amt)
    (when (and (not ignore-point-pos)
	       (save-excursion
		 (beginning-of-line)
		 (looking-at (if line-cont-backslash
				 ;; Don't use "\\s " - ^L doesn't count as WS
				 ;; here
				 "\\([ \t]*\\)\\\\$"
			       "\\([ \t]*\\)$")))
	       (<= (point) (match-end 1)))
      ;; Delete all whitespace after point if there's only whitespace
      ;; on the line, so that any code that does back-to-indentation
      ;; or similar gets the current column in this case.  If this
      ;; removes a line continuation backslash it'll be restored
      ;; at the end.
      (unless c-auto-align-backslashes
	;; Should try to keep the backslash alignment
	;; in this case.
	(save-excursion
	  (goto-char (match-end 0))
	  (setq bs-col (1- (current-column)))))
      (delete-region (point) (match-end 0))
      (setq c-fix-backslashes t))
    (if c-syntactic-indentation
	(setq c-parsing-error
	      (or (let ((c-parsing-error nil)
			(c-syntactic-context
			 (or syntax
			     (and (boundp 'c-syntactic-context)
				  c-syntactic-context))))
		    (c-save-buffer-state (indent)
		      (unless c-syntactic-context
			(setq c-syntactic-context (c-guess-basic-syntax)))
		      (setq indent (c-get-syntactic-indentation
				    c-syntactic-context))
		      (and (not (c-echo-parsing-error quiet))
			   c-echo-syntactic-information-p
			   (message "syntax: %s, indent: %d"
				    c-syntactic-context indent))
		      (setq shift-amt (- indent (current-indentation))))
		    (c-shift-line-indentation shift-amt)
		    (run-hooks 'c-special-indent-hook)
		    c-parsing-error)
		  c-parsing-error))
      (let ((indent 0))
	(save-excursion
	  (while (and (= (forward-line -1) 0)
		      (if (looking-at "\\s *\\\\?$")
			  t
			(setq indent (current-indentation))
			nil))))
	(setq shift-amt (- indent (current-indentation)))
	(c-shift-line-indentation shift-amt)))
    (when (and c-fix-backslashes line-cont-backslash)
      (if bs-col
	  (save-excursion
	    (indent-to bs-col)
	    (insert ?\\))
	(when c-auto-align-backslashes
	  ;; Realign the line continuation backslash.
	  (c-backslash-region (point) (point) nil t))))
    shift-amt))
deerlux
帖子: 14
注册时间: 2006-10-15 13:04

#3

帖子 deerlux » 2007-12-06 20:22

用不着那么复杂吧,多看一下c-mode的帮助,将下面这些内容加到你的.emacs里试试,是不是你要的效果

代码: 全选

(defun my-c-mode()
(define-key c-mode-map [return] 'newline-and-indent)
)
(add-hook 'c-mode-hook 'my-c-mode)
回复