Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to make o/O use 4 spaces or a tab, it does 2 spaces. #1937

Open
leimath opened this issue Oct 9, 2024 · 3 comments
Open

How to make o/O use 4 spaces or a tab, it does 2 spaces. #1937

leimath opened this issue Oct 9, 2024 · 3 comments

Comments

@leimath
Copy link

leimath commented Oct 9, 2024

Issue type

  • Question

Environment

Emacs version: GNU Emacs 29.4 (build 3, x86_64-pc-linux-gnu, X toolkit, cairo version 1.18.0, Xaw3d scroll \ bars) of 2024-10-07
Operating System: WSL2 + Ubuntu-24.04
Evil version: Evil version 1.15.0
Evil installation type: Elpaca
Graphical/Terminal: terminal-emulator (wsltty)
Tested in a make emacs session (see CONTRIBUTING.md):

Reproduction steps

  • Start Emacs

Open a C++ file.
Go to a line.
Press o.

Expected behavior

I want it so that it either inserts a tab (size: 4) in the next line or 4 spaces.

Actual behavior

Inserts 2 spaces.

Further notes

Noob tries to do stuff with tabs. Why is this so difficult.

; START TABS CONFIG
;; Create a variable for our preferred tab width
(setq custom-tab-width 4)

;; Two callable functions for enabling/disabling tabs in Emacs
(defun disable-tabs () (setq indent-tabs-mode nil))
(defun enable-tabs  ()
  (local-set-key (kbd "TAB") 'tab-to-tab-stop)
  (setq indent-tabs-mode t)
  (setq tab-width custom-tab-width))

;; Hooks to Enable Tabs
(add-hook 'prog-mode-hook 'enable-tabs)
;; Hooks to Disable Tabs
(add-hook 'lisp-mode-hook 'disable-tabs)
(add-hook 'emacs-lisp-mode-hook 'disable-tabs)
(add-hook 'c++-ts-mode-hook 'enable-tabs)
(add-hook 'c-ts-mode-hook 'enable-tabs)

;; Language-Specific Tweaks
(setq-default python-indent-offset custom-tab-width) ;; Python
(setq-default js-indent-level custom-tab-width)      ;; Javascript

;; Making electric-indent behave sanely
(setq-default electric-indent-inhibit t)

;; Make the backspace properly erase the tab instead of
;; removing 1 space at a time.
(setq backward-delete-char-untabify-method 'hungry)

;; (OPTIONAL) Shift width for evil-mode users
;; For the vim-like motions of ">>" and "<<".
(setq-default evil-shift-width custom-tab-width)

(setq-default c-basic-offset custom-tab-width)
(setq-default standard-indent custom-tab-width)
;; WARNING: This will change your life
;; (OPTIONAL) Visualize tabs as a pipe character - "|"
;; This will also show trailing characters as they are useful to spot.
(setq whitespace-style '(face tabs tab-mark trailing))
(custom-set-faces
 '(whitespace-tab ((t (:foreground "#636363")))))
(setq whitespace-display-mappings
  '((tab-mark 9 [124 9] [92 9]))) ; 124 is the ascii ID for '\|'
(global-whitespace-mode) ; Enable whitespace mode everywhere
; END TABS CONFIG

I am using evil-collection and evil. I have no clue what to do. Read manuals but cannot wrap my head around which option to set/unset.

@tomdl89
Copy link
Member

tomdl89 commented Oct 12, 2024

Setting c-basic-offset to 4 should be all you need afaik. That is, assuming you want o/O to follow the major-mode's indentation rules. If you want it to always indent with 4 spaces, you will need to bind those keys to your own function, as that's not what evil-open-below was ever meant to do. Also on that assumption, this isn't an evil issue, as I can replicate this behaviour without evil (just pressing return at the end of lines). lmk if I'm missing something, and it is in fact evil-specific.

@imiric
Copy link

imiric commented Oct 20, 2024

Hi, I'm having a similar issue with Go, where pressing o places the cursor at the beginning of the line, instead of using the default indentation for the current major mode or following the indentation of the current line.

Here's what I mean:

Peek.2024-10-20.15-55.mp4

I think this is indeed an issue with evil-mode, since when I turn it off and press Enter the cursor is placed at \t\t|, which is the behavior in Insert mode when evil-mode is enabled. The double tab is also unexpected, but at least it follows the indentation of the current line instead of placing the cursor at column 0.

Here's the behavior with evil-mode disabled:

Peek.2024-10-20.16-01.mp4

I've tried every combination of settings mentioned here, including:

(define-key global-map (kbd "RET") 'newline-and-indent)

...and other newline variants, but nothing seems to fix this.

Some guidance here would be deeply appreciated, as I'm not sure what combination of settings or packages is at fault here. I just want Go to have one tab worth of indentation, and for o/O to follow this setting in every scenario.

I'm using the same versions as @leimath. FWIW this started happening relatively recently. I can't pinpoint after which upgrade it broke, though. 😞

UPDATE: Managed to hack around this somewhat with:

(defun own/newline-and-indent ()
  (interactive)
  (evil-append-line 1)
  (newline-and-indent)
  (evil-backward-char 1))

(general-define-key
 :states '(normal)
 "o" 'own/newline-and-indent)

(general-define-key
 :states '(insert)
 [return] 'own/newline-and-indent)

It's far from perfect, but it removes most of the annoyance.

UPDATE2: Nope, this breaks other things, like the behavior when the cursor is between braces... 🤦

Help? 🙏

@leimath
Copy link
Author

leimath commented Oct 23, 2024

I managed to fix this for me at least partially (for C++ but).

I removed the entire START TABS CONFIG I posted above and also removed anything that evil does for indenting (aka, removed evil-indent thing)

The ONLY thing I have for indentation is:

(setq c-basic-offset 4)
(setq c-guess-guessed-basic-offset 4)
(setq c-ts-mode-indent-offset 4)
(setq c++-ts-mode-indent-offset 4)
(setq c++-ts-mode-basic-offet 4)
(setq c-ts-mode-indent-offset 4)

I frantically added all the variables here as I am not sure which is responsible for setting it.

The main issue is that in treesitter mode the indent has to be set with <lang>-ts-mode-indent-offset and <lang>-ts-mode-basic-offset and not the regular c/c++ mode ones.

@imiric

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants