Skip to content

Commit a7f371e

Browse files
Johannes Weinertorvalds
authored andcommitted
documentation: update CodingStyle tips for Emacs users
Describe a setup that integrates better with Emacs' cc-mode and also fixes up the alignment of continuation lines to really only use tabs. Signed-off-by: Johannes Weiner <hannes@saeurebad.de> Cc: Jonathan Corbet <corbet@lwn.net> Cc: Randy Dunlap <randy.dunlap@oracle.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 197dcff commit a7f371e

1 file changed

Lines changed: 23 additions & 19 deletions

File tree

‎Documentation/CodingStyle‎

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -474,25 +474,29 @@ make a good program).
474474
So, you can either get rid of GNU emacs, or change it to use saner
475475
values. To do the latter, you can stick the following in your .emacs file:
476476

477-
(defun linux-c-mode ()
478-
"C mode with adjusted defaults for use with the Linux kernel."
479-
(interactive)
480-
(c-mode)
481-
(c-set-style "K&R")
482-
(setq tab-width 8)
483-
(setq indent-tabs-mode t)
484-
(setq c-basic-offset 8))
485-
486-
This will define the M-x linux-c-mode command. When hacking on a
487-
module, if you put the string -*- linux-c -*- somewhere on the first
488-
two lines, this mode will be automatically invoked. Also, you may want
489-
to add
490-
491-
(setq auto-mode-alist (cons '("/usr/src/linux.*/.*\\.[ch]$" . linux-c-mode)
492-
auto-mode-alist))
493-
494-
to your .emacs file if you want to have linux-c-mode switched on
495-
automagically when you edit source files under /usr/src/linux.
477+
(defun c-lineup-arglist-tabs-only (ignored)
478+
"Line up argument lists by tabs, not spaces"
479+
(let* ((anchor (c-langelem-pos c-syntactic-element))
480+
(column (c-langelem-2nd-pos c-syntactic-element))
481+
(offset (- (1+ column) anchor))
482+
(steps (floor offset c-basic-offset)))
483+
(* (max steps 1)
484+
c-basic-offset)))
485+
486+
(add-hook 'c-mode-hook
487+
(lambda ()
488+
(let ((filename (buffer-file-name)))
489+
;; Enable kernel mode for the appropriate files
490+
(when (and filename
491+
(string-match "~/src/linux-trees" filename))
492+
(setq indent-tabs-mode t)
493+
(c-set-style "linux")
494+
(c-set-offset 'arglist-cont-nonempty
495+
'(c-lineup-gcc-asm-reg
496+
c-lineup-arglist-tabs-only))))))
497+
498+
This will make emacs go better with the kernel coding style for C
499+
files below ~/src/linux-trees.
496500

497501
But even if you fail in getting emacs to do sane formatting, not
498502
everything is lost: use "indent".

0 commit comments

Comments
 (0)