Keywords: Emacs | indentation configuration | tab-stop-list
Abstract: This article delves into common configuration pitfalls when setting up 4-space indentation in Emacs text mode, focusing on the distinction between the tab-width and tab-stop-list variables. By analyzing the best answer, it explains why merely setting tab-width fails to alter TAB key behavior and provides multiple configuration methods, including using tab-stop-list, custom functions, and simplified solutions post-Emacs 24.4. The discussion also covers the essential differences between HTML tags like <br> and character \n, ensuring configuration accuracy and code example readability.
Problem Background and Common Misconceptions
Many Emacs users encounter confusion when configuring indentation in text mode: even after setting tab-width to 4 and disabling tabs (via indent-tabs-mode nil), pressing the TAB key often does not behave as expected. For instance, users might observe that if there is no text on the previous line, indentation is 8 spaces; if there is text, it indents to the start of the second word. This typically stems from misunderstanding the roles of tab-width and tab-stop-list.
Core Concepts: The Difference Between tab-width and tab-stop-list
According to the GNU Emacs manual, the tab-width variable controls the display width of literal TAB characters, while tab-stop-list determines what characters are inserted when the TAB key is pressed in certain modes. This means that setting tab-width alone does not change the insertion behavior of the TAB key; to modify indentation, tab-stop-list must be configured. For example, in text mode, the default indentation function may rely on tab-stop-list to define tab stops.
Configuration Methods and Practice
To achieve 4-space indentation when pressing TAB in text mode, it is recommended to use tab-stop-list. Here is a basic configuration method that can be added directly to the .emacs file:
(custom-set-variables
'(tab-stop-list (quote (4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120))))
This configuration defines a list of tab stops starting at 4, with stops every 4 spaces up to 120. Thus, when the TAB key is pressed, the cursor moves to the next multiple of 4 columns, achieving 4-space indentation. Alternatively, the M-x edit-tab-stops command can be used to edit tab stops interactively, as detailed in the GNU Emacs manual.
Advanced Configuration and Improvements in Emacs 24.4
For more flexible configurations, insights from other answers can be incorporated. For instance, Answer 1 suggests setting indent-line-function to 'insert-tab along with tab-width and indent-tabs-mode, but this method may not work in all modes and relies on tab conversion. Answer 2 provides a way to dynamically generate tab-stop-list using the number-sequence function, such as:
(setq tab-stop-list (number-sequence 4 200 4))
This generates a sequence from 4 to 200 with a step of 4, simplifying configuration. Starting from Emacs 24.4, the default value of tab-stop-list is nil, meaning a tab stop every tab-width columns, so explicit setting can be omitted by just setting tab-width to 4. However, for compatibility with older versions or specific needs, explicit configuration remains beneficial.
Code Examples and Considerations
When writing configurations, attention to HTML escaping is crucial to ensure code examples display correctly. For example, when describing text, if mentioning HTML tags like <br>, angle brackets should be escaped to prevent them from being parsed as HTML tags. Similarly, special characters such as quotes or backslashes in code must be handled properly. For instance, in the function print("<T>"), <T> should be escaped to maintain DOM structure integrity.
Summary and Best Practices
In summary, setting 4-space indentation in Emacs text mode hinges on defining tab stops via tab-stop-list, not merely relying on tab-width. It is recommended to use custom-set-variables or dynamic list generation methods, considering simplifications based on the Emacs version. Additionally, pay attention to escaping issues in code examples to enhance document readability and accuracy. By understanding these core concepts, users can more effectively customize Emacs indentation behavior, improving editing efficiency.