2008-07-04 17:53:58

by Johannes Weiner

[permalink] [raw]
Subject: [PATCH -mm] 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 <[email protected]>
---

Sorry, forgot linux-kernel on first send.

diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle
index 6caa146..4148e9f 100644
--- a/Documentation/CodingStyle
+++ b/Documentation/CodingStyle
@@ -474,25 +474,29 @@ make a good program).
So, you can either get rid of GNU emacs, or change it to use saner
values. To do the latter, you can stick the following in your .emacs file:

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

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


2008-07-04 19:42:16

by Jonathan Corbet

[permalink] [raw]
Subject: Re: [PATCH -mm] documentation: update CodingStyle tips for Emacs users

On Fri, 04 Jul 2008 19:52:16 +0200
Johannes Weiner <[email protected]> wrote:

> +(add-hook 'c-mode-hook
> + (lambda ()
> + (let ((filename (buffer-file-name)))
> + ;; Enable kernel mode for the appropriate files
> + (when (and file
> + (string-match "/usr/src/linux" filename))

Shouldn't that be "(and filename ..." rather than "file"?

A test for /usr/src/linux seems misplaced, though; I don't think a
whole lot of people keep their kernel trees there anymore. There must
be a better heuristic one can use to figure out whether a kernel source
file is being edited.

jon

2008-07-05 11:38:26

by Johannes Weiner

[permalink] [raw]
Subject: Re: [PATCH -mm] documentation: update CodingStyle tips for Emacs users

Hi,

Jonathan Corbet <[email protected]> writes:

> On Fri, 04 Jul 2008 19:52:16 +0200
> Johannes Weiner <[email protected]> wrote:
>
>> +(add-hook 'c-mode-hook
>> + (lambda ()
>> + (let ((filename (buffer-file-name)))
>> + ;; Enable kernel mode for the appropriate files
>> + (when (and file
>> + (string-match "/usr/src/linux" filename))
>
> Shouldn't that be "(and filename ..." rather than "file"?

Whoops, you are right.

> A test for /usr/src/linux seems misplaced, though; I don't think a
> whole lot of people keep their kernel trees there anymore. There must
> be a better heuristic one can use to figure out whether a kernel source
> file is being edited.

Is this really needed? I think most people have their kernel trees in
one directory and I used /usr/src/linux explicitely because it is so
well known and whoever reads this snippet knows what to substitute it
for.

But perhaps it should be "~/src/linux-trees/" to emphasize that it it
matches everything descendant from this directory.

A heuristics seems overkill as this snippet is not distributed code that
needs to be portable but it is just a tip for ones .emacs. I don't want
heuristics in my .emacs if I know exactly where my kernel trees are.

But then, we should remove the check for linux-files completely and make
people use this coding style for every code they write ;)

Hannes

2008-07-05 14:32:46

by Jonathan Corbet

[permalink] [raw]
Subject: Re: [PATCH -mm] documentation: update CodingStyle tips for Emacs users

On Sat, 05 Jul 2008 11:36:44 +0000
Johannes Weiner <[email protected]> wrote:

> A heuristics seems overkill as this snippet is not distributed code
> that needs to be portable but it is just a tip for ones .emacs.

You're right, it's fine the way it is.

jon

2008-07-05 18:35:40

by Johannes Weiner

[permalink] [raw]
Subject: [PATCH -mm] documentation: update CodingStyle tips for Emacs users v2

Hi,

Jonathan Corbet <[email protected]> writes:

> On Sat, 05 Jul 2008 11:36:44 +0000
> Johannes Weiner <[email protected]> wrote:
>
>> A heuristics seems overkill as this snippet is not distributed code
>> that needs to be portable but it is just a tip for ones .emacs.
>
> You're right, it's fine the way it is.

Okay, I fixed the file->filename mistake. How about the following?

Hannes

---
From: Johannes Weiner <[email protected]>
Subject: 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 <[email protected]>
CC: Jonathan Corbet <[email protected]>
---

diff --git a/Documentation/CodingStyle b/Documentation/CodingStyle
index 6caa146..1875e50 100644
--- a/Documentation/CodingStyle
+++ b/Documentation/CodingStyle
@@ -474,25 +474,29 @@ make a good program).
So, you can either get rid of GNU emacs, or change it to use saner
values. To do the latter, you can stick the following in your .emacs file:

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

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