linux-doc.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] Documentation/CodingStyle: Use directory-local variables for emacs
@ 2019-01-04 21:08 Bart Van Assche
  2019-01-04 21:08 ` [PATCH] Documentation/CodingStyle: Move emacs settings into .dir-locals.el Bart Van Assche
  2019-01-04 22:10 ` [PATCH] Documentation/CodingStyle: Use directory-local variables for emacs Federico Vaga
  0 siblings, 2 replies; 17+ messages in thread
From: Bart Van Assche @ 2019-01-04 21:08 UTC (permalink / raw)
  To: Jonathan Corbet
  Cc: linux-doc, Bart Van Assche, Matthew Wilcox, Geyslan G . Bem,
	Tiago Natel de Moura, Alison Chaiken, Joe Perches, Federico Vaga,
	Li Yang

In emacs 23.1 support for directory-local variables was added (see also
https://lists.gnu.org/archive/html/info-gnu-emacs/2009-07/msg00000.html).
Simplify the settings in coding-style.rst by using that feature.
Additionally, do not inherit any settings from emacs' linux coding style
to minimize dependencies on the version of emacs that is being used.

I have verified with several large and nontrivial kernel source files
that the new settings format code according to what checkpatch expects.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Geyslan G. Bem <geyslan@gmail.com>
Cc: Tiago Natel de Moura <tiago4orion@gmail.com>
Cc: Alison Chaiken <alison@she-devel.com>
Cc: Joe Perches <joe@perches.com>
Cc: Federico Vaga <federico.vaga@vaga.pv.it>
Cc: Li Yang <leo@zh-kernel.org>
---
 Documentation/process/coding-style.rst | 57 +++++++++++++++++---------
 1 file changed, 37 insertions(+), 20 deletions(-)

Changes compared to v1:
- Removed top-level .dir-locals.el file again and updated coding-style.rst instead.
- Restored the humourous paragraph about emacs.
- Left out Italian and Chinese translations.
 
diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
index 277c113376a6..5013883aec8e 100644
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -592,26 +592,43 @@ values.  To do the latter, you can stick the following in your .emacs file:
       (* (max steps 1)
          c-basic-offset)))
 
-  (add-hook 'c-mode-common-hook
-            (lambda ()
-              ;; Add kernel style
-              (c-add-style
-               "linux-tabs-only"
-               '("linux" (c-offsets-alist
-                          (arglist-cont-nonempty
-                           c-lineup-gcc-asm-reg
-                           c-lineup-arglist-tabs-only))))))
-
-  (add-hook 'c-mode-hook
-            (lambda ()
-              (let ((filename (buffer-file-name)))
-                ;; Enable kernel mode for the appropriate files
-                (when (and filename
-                           (string-match (expand-file-name "~/src/linux-trees")
-                                         filename))
-                  (setq indent-tabs-mode t)
-                  (setq show-trailing-whitespace t)
-                  (c-set-style "linux-tabs-only")))))
+  (dir-locals-set-class-variables
+   'linux-kernel
+   '((c-mode . (
+          (c-basic-offset . 8)
+          (c-label-minimum-indentation . 0)
+          (c-offsets-alist . (
+                  (arglist-close         . c-lineup-arglist-tabs-only)
+                  (arglist-cont-nonempty .
+		      (c-lineup-gcc-asm-reg c-lineup-arglist-tabs-only))
+                  (arglist-intro         . +)
+                  (brace-list-intro      . +)
+                  (c                     . c-lineup-C-comments)
+                  (case-label            . 0)
+                  (comment-intro         . c-lineup-comment)
+                  (cpp-define-intro      . +)
+                  (cpp-macro             . -1000)
+                  (cpp-macro-cont        . +)
+                  (defun-block-intro     . +)
+                  (else-clause           . 0)
+                  (func-decl-cont        . +)
+                  (inclass               . +)
+                  (inher-cont            . c-lineup-multi-inher)
+                  (knr-argdecl-intro     . 0)
+                  (label                 . -1000)
+                  (statement             . 0)
+                  (statement-block-intro . +)
+                  (statement-case-intro  . +)
+                  (statement-cont        . +)
+                  (substatement          . +)
+                  ))
+          (indent-tabs-mode . t)
+          (show-trailing-whitespace . t)
+          ))))
+
+  (dir-locals-set-directory-class
+   (expand-file-name "~/src/linux-trees")
+   'linux-kernel)
 
 This will make emacs go better with the kernel coding style for C
 files below ``~/src/linux-trees``.
-- 
2.20.1.97.g81188d93c3-goog


^ permalink raw reply related	[flat|nested] 17+ messages in thread
* [PATCH] Documentation/CodingStyle: Move emacs settings into .dir-locals.el
@ 2019-01-04  0:39 Bart Van Assche
  2019-01-04  2:12 ` Matthew Wilcox
  2019-01-04  9:44 ` Federico Vaga
  0 siblings, 2 replies; 17+ messages in thread
From: Bart Van Assche @ 2019-01-04  0:39 UTC (permalink / raw)
  To: Jonathan Corbet
  Cc: linux-doc, Bart Van Assche, Geyslan G . Bem, Tiago Natel de Moura,
	Alison Chaiken, Joe Perches, Federico Vaga, Zhang Le, Li Yang

For new kernel developers who use emacs it is tedious to follow the
instructions in Documentation/process/coding-style.rst for configuring
emacs. Make it easier for emacs users by moving these settings into the
top-level .dir-locals.el file. Emacs supports directory-local variables
since version 23.1, released in 2009. See also
https://lists.gnu.org/archive/html/info-gnu-emacs/2009-07/msg00000.html

The settings in .dir-locals.el are not identical to those in
Documentation/process/coding-style.rst. The most important difference
is that "(arglist-cont-nonempty c-lineup-gcc-asm-reg
c-lineup-arglist-tabs-only)" (which is not a valid alist) has been
changed into "(arglist-cont-nonempty . c-lineup-arglist)". I have
verified with several large and nontrivial kernel source files that
the settings in .dir-locals.el format code according to what checkpatch
expects.

The Italian and Chinese translations of the modified paragraphs have
been generated by Google Translate.

Signed-off-by: Bart Van Assche <bvanassche@acm.org>
Cc: Geyslan G. Bem <geyslan@gmail.com>
Cc: Tiago Natel de Moura <tiago4orion@gmail.com>
Cc: Alison Chaiken <alison_chaiken@mentor.com>
Cc: Joe Perches <joe@perches.com>
Cc: Federico Vaga <federico.vaga@vaga.pv.it>
Cc: Zhang Le <r0bertz@gentoo.org>
Cc: Li Yang <leo@zh-kernel.org>
---
 .dir-locals.el                                | 41 +++++++++++++++
 Documentation/process/coding-style.rst        | 49 ++----------------
 .../it_IT/process/coding-style.rst            | 50 ++-----------------
 .../translations/zh_CN/coding-style.rst       | 43 +---------------
 4 files changed, 53 insertions(+), 130 deletions(-)
 create mode 100644 .dir-locals.el

diff --git a/.dir-locals.el b/.dir-locals.el
new file mode 100644
index 000000000000..8cf857a00772
--- /dev/null
+++ b/.dir-locals.el
@@ -0,0 +1,41 @@
+;; Emacs settings for Linux kernel C code.
+
+(
+ (c-mode . (
+	(c-basic-offset . 8)
+	(c-cleanup-list . (brace-else-brace))
+	(c-hanging-braces-alist . (
+			(arglist-cont-nonempty)
+			(block-close . c-snug-do-while)
+			(brace-entry-open)
+			(brace-list-open)
+			(substatement-open after)
+			))
+	(c-label-minimum-indentation . 0)
+	(c-offsets-alist . (
+			(arglist-cont-nonempty . c-lineup-arglist)
+			(arglist-intro         . +)
+			(brace-list-intro      . +)
+			(c                     . c-lineup-C-comments)
+			(case-label            . 0)
+			(comment-intro         . c-lineup-comment)
+			(cpp-define-intro      . +)
+			(cpp-macro             . -1000)
+			(cpp-macro-cont        . +)
+			(defun-block-intro     . +)
+			(else-clause           . 0)
+			(func-decl-cont        . +)
+			(inclass               . +)
+			(inher-cont            . c-lineup-multi-inher)
+			(knr-argdecl-intro     . 0)
+			(label                 . 0)
+			(statement             . 0)
+			(statement-block-intro . +)
+			(statement-case-intro  . +)
+			(statement-cont        . +)
+			(substatement          . +)
+			))
+	(indent-tabs-mode . t)
+	)
+ )
+)
diff --git a/Documentation/process/coding-style.rst b/Documentation/process/coding-style.rst
index 277c113376a6..e12b845d6766 100644
--- a/Documentation/process/coding-style.rst
+++ b/Documentation/process/coding-style.rst
@@ -571,50 +571,11 @@ item, explaining its use.
 9) You've made a mess of it
 ---------------------------
 
-That's OK, we all do.  You've probably been told by your long-time Unix
-user helper that ``GNU emacs`` automatically formats the C sources for
-you, and you've noticed that yes, it does do that, but the defaults it
-uses are less than desirable (in fact, they are worse than random
-typing - an infinite number of monkeys typing into GNU emacs would never
-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:
-
-.. code-block:: none
-
-  (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-common-hook
-            (lambda ()
-              ;; Add kernel style
-              (c-add-style
-               "linux-tabs-only"
-               '("linux" (c-offsets-alist
-                          (arglist-cont-nonempty
-                           c-lineup-gcc-asm-reg
-                           c-lineup-arglist-tabs-only))))))
-
-  (add-hook 'c-mode-hook
-            (lambda ()
-              (let ((filename (buffer-file-name)))
-                ;; Enable kernel mode for the appropriate files
-                (when (and filename
-                           (string-match (expand-file-name "~/src/linux-trees")
-                                         filename))
-                  (setq indent-tabs-mode t)
-                  (setq show-trailing-whitespace t)
-                  (c-set-style "linux-tabs-only")))))
-
-This will make emacs go better with the kernel coding style for C
-files below ``~/src/linux-trees``.
+``GNU emacs`` automatically formats the C sources for you. However,
+the defaults it uses are less than desirable. Use a version of emacs
+that support directory local variables such that it automatically
+picks up the settings from .dir-locals.el in the kernel top level
+directory.
 
 But even if you fail in getting emacs to do sane formatting, not
 everything is lost: use ``indent``.
diff --git a/Documentation/translations/it_IT/process/coding-style.rst b/Documentation/translations/it_IT/process/coding-style.rst
index b707bdbe178c..05a86c69bd5d 100644
--- a/Documentation/translations/it_IT/process/coding-style.rst
+++ b/Documentation/translations/it_IT/process/coding-style.rst
@@ -578,51 +578,11 @@ commento per spiegarne l'uso.
 9) Avete fatto un pasticcio
 ---------------------------
 
-Va bene, li facciamo tutti.  Probabilmente vi è stato detto dal vostro
-aiutante Unix di fiducia che ``GNU emacs`` formatta automaticamente il
-codice C per conto vostro, e avete notato che sì, in effetti lo fa, ma che
-i modi predefiniti non sono proprio allettanti (infatti, sono peggio che
-premere tasti a caso - un numero infinito di scimmie che scrivono in
-GNU emacs non faranno mai un buon programma).
-
-Quindi, potete sbarazzarvi di GNU emacs, o riconfigurarlo con valori più
-sensati.  Per fare quest'ultima cosa, potete appiccicare il codice che
-segue nel vostro file .emacs:
-
-.. code-block:: none
-
-  (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-common-hook
-            (lambda ()
-              ;; Add kernel style
-              (c-add-style
-               "linux-tabs-only"
-               '("linux" (c-offsets-alist
-                          (arglist-cont-nonempty
-                           c-lineup-gcc-asm-reg
-                           c-lineup-arglist-tabs-only))))))
-
-  (add-hook 'c-mode-hook
-            (lambda ()
-              (let ((filename (buffer-file-name)))
-                ;; Enable kernel mode for the appropriate files
-                (when (and filename
-                           (string-match (expand-file-name "~/src/linux-trees")
-                                         filename))
-                  (setq indent-tabs-mode t)
-                  (setq show-trailing-whitespace t)
-                  (c-set-style "linux-tabs-only")))))
-
-Questo farà funzionare meglio emacs con lo stile del kernel per i file che
-si trovano nella cartella ``~/src/linux-trees``.
+`` GNU emacs`` formatta automaticamente le sorgenti C per te. Tuttavia,
+le impostazioni predefinite che utilizza sono meno desiderabili.
+Utilizzare una versione di emacs che supporti le variabili locali della
+directory in modo tale che raccolga automaticamente le impostazioni da
+.dir-locals.el nella directory di livello superiore del kernel.
 
 Ma anche se doveste fallire nell'ottenere una formattazione sensata in emacs
 non tutto è perduto: usate ``indent``.
diff --git a/Documentation/translations/zh_CN/coding-style.rst b/Documentation/translations/zh_CN/coding-style.rst
index 1466aa64b8b4..32a72a9c110a 100644
--- a/Documentation/translations/zh_CN/coding-style.rst
+++ b/Documentation/translations/zh_CN/coding-style.rst
@@ -516,47 +516,8 @@ Documentation/doc-guide/ 和 scripts/kernel-doc 以获得详细信息。
 9) 你已经把事情弄糟了
 ------------------------------
 
-这没什么,我们都是这样。可能你的使用了很长时间 Unix 的朋友已经告诉你
-``GNU emacs`` 能自动帮你格式化 C 源代码,而且你也注意到了,确实是这样,不过它
-所使用的默认值和我们想要的相去甚远 (实际上,甚至比随机打的还要差——无数个猴子
-在 GNU emacs 里打字永远不会创造出一个好程序) (译注:Infinite Monkey Theorem)
-
-所以你要么放弃 GNU emacs,要么改变它让它使用更合理的设定。要采用后一个方案,
-你可以把下面这段粘贴到你的 .emacs 文件里。
-
-.. code-block:: none
-
-  (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-common-hook
-            (lambda ()
-              ;; Add kernel style
-              (c-add-style
-               "linux-tabs-only"
-               '("linux" (c-offsets-alist
-                          (arglist-cont-nonempty
-                           c-lineup-gcc-asm-reg
-                           c-lineup-arglist-tabs-only))))))
-
-  (add-hook 'c-mode-hook
-            (lambda ()
-              (let ((filename (buffer-file-name)))
-                ;; Enable kernel mode for the appropriate files
-                (when (and filename
-                           (string-match (expand-file-name "~/src/linux-trees")
-                                         filename))
-                  (setq indent-tabs-mode t)
-                  (setq show-trailing-whitespace t)
-                  (c-set-style "linux-tabs-only")))))
-
-这会让 emacs 在 ``~/src/linux-trees`` 下的 C 源文件获得更好的内核代码风格。
+``GNU emacs``會自動為您設置C源代碼。但是,它使用的默認值不太理想。
+使用支持目錄本地變量的emacs版本,以便它自動從內核頂級目錄中的.dir-locals.el中獲取設置。
 
 不过就算你尝试让 emacs 正确的格式化代码失败了,也并不意味着你失去了一切:还可
 以用 ``indent`` 。
-- 
2.20.1.415.g653613c723-goog


^ permalink raw reply related	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2019-01-07 10:27 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-01-04 21:08 [PATCH] Documentation/CodingStyle: Use directory-local variables for emacs Bart Van Assche
2019-01-04 21:08 ` [PATCH] Documentation/CodingStyle: Move emacs settings into .dir-locals.el Bart Van Assche
2019-01-04 21:11   ` Bart Van Assche
2019-01-04 22:10 ` [PATCH] Documentation/CodingStyle: Use directory-local variables for emacs Federico Vaga
2019-01-04 22:39   ` Bart Van Assche
2019-01-05 19:58     ` Alison Chaiken
2019-01-05 21:04       ` Bart Van Assche
  -- strict thread matches above, loose matches on Subject: below --
2019-01-04  0:39 [PATCH] Documentation/CodingStyle: Move emacs settings into .dir-locals.el Bart Van Assche
2019-01-04  2:12 ` Matthew Wilcox
2019-01-04 16:06   ` Bart Van Assche
2019-01-04  9:44 ` Federico Vaga
2019-01-04 11:18   ` Jani Nikula
2019-01-04 16:40     ` Bart Van Assche
2019-01-07 10:29       ` Jani Nikula
2019-01-04 16:32   ` Bart Van Assche
2019-01-04 16:41     ` Jonathan Corbet
2019-01-04 18:26       ` Joe Perches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).