Why is my newline taking 4 spaces by default? - emacs

I have set c-basic-offset & c-basic-indent to 4 expecting it to indent newlines after brackets only in C/C++ files. But the issue I'm facing is every newline in ".txt" document is taking 4 spaces by default which I don't want.
Here is my init.el file
(global-display-line-numbers-mode 1)
(set-frame-font "JetBrains Mono-11" nil t)
(setq-default indent-tabs-mode nil)
(setq-default tab-width 4)
(setq indent-line-function 'insert-tab)
(setq c-basic-offset 4)
(setq c-basic-indent 4)
;(setq-default c-basic-offset 4)
;(c-set-offset 'substatement-open 0)
; stop creating backup files
(setq make-backup-files nil)
(recentf-mode 1)
(setq recentf-max-menu-items 25)
(setq recentf-max-saved-items 25)
(global-set-key "\C-x\ \C-r" 'recentf-open-files)
;; start the initial frame maximized
(add-to-list 'initial-frame-alist '(fullscreen . maximized))
;; start every frame maximized
(add-to-list 'default-frame-alist '(fullscreen . maximized))
Please help me get rid of 4 space characters in a normal text file.

You can make your settings specific for mode. In your case if you want indentation settings to apply only for C/C++ files, you need to apply these settings for that mode hook. There are various ways of doing it, one of them is using defining those settings in a defun and add that defun to appropriate hook.
Example below shows to setup c-basic-offset and c-basic-indent to 4.
(defun my-c-mode-common-hook ()
;; my customizations for all of c-mode and related modes
(setq c-basic-offset 4)
(setq c-basic-indent 4)
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

Related

emacs arglist-cont-nonempty to use 4 spaces, not a tab

So I am trying to follow the old Sun "cstyle" standard, then Solaris, now IllumOS. They use tabs pretty much everywhere, that is fine. But there is one case where it should have same indentation, plus 4 spaces.
If I set (c-set-offset 'arglist-cont-nonempty ' 4) I get the correct placement, but because 4 spaces is a tab, emacs puts a tab there. If I use 3 or 5, they are spaces. Is there someway I can quote a "space" to have literal 4 spaces without convertion, or, perhaps temporarily, disable tabs-everywhere?
rest of .emacs is here:
(setq default-tab-width 4)
(setq tab-width 4)
(setq-default c-indent-tabs-mode t
c-indent-level 4
c-argdecl-indent 0
c-tab-always-indent t
backward-delete-function nil)
(c-add-style "my-c-style" '((c-continued-statement-offset 4)))
(defun my-c-mode-hook ()
(c-set-style "my-c-style")
(c-set-offset 'substatement-open '0)
(c-set-offset 'inline-open '+)
(c-set-offset 'block-open '+)
(c-set-offset 'brace-list-open '+)
(c-set-offset 'arglist-cont-nonempty ' 4)
(c-set-offset 'case-label '+))
(add-hook 'c-mode-hook 'my-c-mode-hook)
(add-hook 'c++-mode-hook 'my-c-mode-hook)
If you want to use spaces instead of tabs when indenting, put the
following in your .emacs file:
(setq-default indent-tabs-mode nil)
You may also customize the indent-tabs-mode variable instead,
obviously. If you want to remove tabs in an existing file, mark the
whole buffer using C-x h and use M-x untabify
see here

Can't control width of sr-speedbar

I'm using emacs with sr-speedbar, but can't control its width. When I resize the emacs window, the sr-speedbar always expands with it. I have tried this both with xemacs and emacs.
My .emacs file below:
(require 'package)
(package-initialize)
(setq package-archives '(("gnu" . "http://elpa.gnu.org/packages/")
("marmalade" . "http://marmalade-repo.org/packages/")
("melpa" . "http://melpa.milkbox.net/packages/")))
(setq c-default-style "linux"
c-basic-offset 4)
(iswitchb-mode 1)
(setq inhibit-splash-screen t)
(load-theme 'zenburn t)
(require 'ecb)
(setq stack-trace-on-error t)
(desktop-save-mode 1)
;(defun toggle-fullscreen ()
; (interactive)
; (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
; '(2 "_NET_WM_STATE_MAXIMIZED_VERT" 0))
; (x-send-client-message nil 0 nil "_NET_WM_STATE" 32
; '(2 "_NET_WM_STATE_MAXIMIZED_HORZ" 0))
;)
;(toggle-fullscreen)
(require 'sr-speedbar)
(setq
sr-speedbar-right-side nil
sr-speedbar-width-x 10
sr-speedbar-width-console 10
sr-speedbar-max-width 10
sr-speedbar-delete-windows t)
(sr-speedbar-open)
(put 'dired-find-alternate-file 'disabled nil)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
;; Single char cursor movement. (assuming you are on qwerty)
(global-set-key (kbd "M-j") 'backward-char)
(global-set-key (kbd "M-l") 'forward-char)
(global-set-key (kbd "M-i") 'previous-line)
(global-set-key (kbd "M-k") 'next-line)
(global-set-key (kbd "M-SPC") 'set-mark-command)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(custom-safe-themes (quote ("d63e19a84fef5fa0341fa68814200749408ad4a321b6d9f30efc117aeaf68a2e" default)))
'(ecb-options-version "2.40"))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
)
It consumes variables at load time, not when open.
The sr-speedbar-width-* should be set before the script loaded.
This does not work, because the variables sr-speedbar-width-x and sr-speedbar-width-console do not exist any more. They have been removed from the code on 03 Aug 2014. See here: https://www.emacswiki.org/emacs/download/sr-speedbar.el
The information that you probably found on https://www.emacswiki.org/emacs/SrSpeedbar is outdated.
There is only one variable sr-speedbar-width
The variable is "consumed" when sr-speedbar-open is called. The user can define the variable before or after loading the library. This is due to the behaviour of the Emacs Lisp special form defvar (line 364), which does not overwrite the variable. For more information on this see here: https://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Variables.html

Indentation of groovy style method parameters in emacs

I am using emacs with groovy electric mode (installed as outlined here). The relevant parts (I think) of my .emacs file are given at the end of this question.
The current behaviour I am getting with indenting, as I type, is:
def someObject = new Something(
param1
Everything is how I would like it at this point, but as soon as I type the ':' character, it becomes:
def someObject = new Something(
param1:
I want the param/value pairs (and any that follow) to remain indented one level, as before, e.g.:
def someObject = new Something(
param1: val1,
param2: val2
)
.emacs excerpt:
(electric-indent-mode t)
(setq-default tab-width 4)
(setq-default indent-tabs-mode nil)
(setq indent-line-function 'insert-tab)
(setq tabify nil)
(setq-default c-basic-offset 4)
(delete-selection-mode 1)
(set-default-font "-apple-Bitstream_Vera_Sans_Mono-medium-normal-normal-*-*-*-*-*-m-0-iso10646-1")
(setq load-path (cons "~/.emacs.d/" load-path))
(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/")
(setq whitespace-action '(auto-cleanup)) ;; automatically clean up bad whitespace
(setq whitespace-style '(trailing space-before-tab indentation empty space-after-tab)) ;; only show bad whitespace
;;; delete trailing whitespace
(add-hook 'before-save-hook 'delete-trailing-whitespace)
;;; turn on syntax highlighting
(global-font-lock-mode 1)
;;; use groovy-mode when file ends in .groovy or has #!/bin/groovy at start
(autoload 'groovy-mode "groovy-mode" "Major mode for editing Groovy code." t)
(add-to-list 'auto-mode-alist '("\\.groovy\\'" . groovy-mode))
(add-to-list 'interpreter-mode-alist '("groovy" . groovy-mode))
;;; make Groovy mode electric by default.
(add-hook 'groovy-mode-hook
(lambda ()
(require 'groovy-electric)
(groovy-electric-mode)))
Edit: regarding #ataylor 's suggestion below, I have tried both:
(add-hook 'groovy-mode-hook
'(lambda ()
(require 'groovy-electric)
(groovy-electric-mode)))
(add-hook 'groovy-mode-hook '(c-set-offset 'label 4))
and
(add-hook 'groovy-mode-hook
'(lambda ()
(require 'groovy-electric)
(groovy-electric-mode)
(c-set-offset 'label 4)))
neither of which work as I want. Is this the correct spot?
Groovy mode is treating these types of parameters as labels, like case statement labels and the rarely used feature inherited from Java that allows break statements to specify a target.
You can override the indentation by adding this to your hook:
(add-hook 'groovy-mode-hook
(lambda ()
(c-set-offset 'label 4)))

major-mode hook configuration affects other buffers

Let me start by saying I'm very new to emacs.
I'm attempting to create customizations for major-modes. While my settings are functioning correctly, I'm observing that when I open a new buffer, that buffers major-mode customization is being applied to other buffers of a different type.
For instance, if I open a file named 'Makefile', makefile-mode is used and my customizations are applied. If I then open another file such as 'test.c', c-mode is used but customizations from makefile-mode are merged with customizations from c-mode.
The relevant portions of my .emacs file can be seen below:
(defun c-mode-settings ()
(c-set-style "bsd")
(set-buffer-file-coding-system 'utf-8-unix)
(show-paren-mode 1)
(setq c-basic-offset 4)
(setq tab-width 4)
(setq indent-tabs-mode nil)
(setq c-tab-always-indent t)
(setq require-final-newline t)
)
(defun makefile-mode-settings ()
(setq whitespace-style '(tabs spaces space-mark tab-mark face lines-tail))
(whitespace-mode t)
(show-paren-mode 1)
(setq tab-width 4)
(setq require-final-newline t)
)
(add-hook 'c-mode-hook 'c-mode-settings)
(add-hook 'makefile-mode-hook 'makefile-mode-settings)
How can I keep these mode hooks from affecting other buffers in different modes?
Thanks!
Andrew
You need to take into account, that some variables are becoming local to buffer when set, while some are global. Usually they have corresponding comment in their description (use C-h v var-name to get this description.
In some cases, you can force that any variable become local to buffer, using the
(set (make-local-variable 'var-name) var-value)
but you need to be careful with this.

Emacs Lisp function to toggle variable 'tab-width' between 4 & 8

The source tree that I work on has files indented with different tab values, (not to mention spaces) and the best I can do is to set emacs to use the style found in the region of code I am modifying. Instead of doing M-x set-variable tab-width to 4 or 8, a key binding to toggle the tab-width among these two value would help immensely.
Thanks.
;; Obviously substitute your preferred key for <f8>
(global-set-key (kbd "<f8>") 'tf-toggle-tab-width-setting) ; ' "fix" highlighting
(defun tf-toggle-tab-width-setting ()
"Toggle setting tab widths between 4 and 8"
(interactive)
(setq tab-width (if (= tab-width 8) 4 8))
(redraw-display))
Edited to add redraw-display as per comment suggested
Not quite answering the question (the answers given are good enough), but you might want to consider setting a per-file local variable. For example, assuming that "//" means comment in your language, you would put the following in the first line of the file:
// -*- tab-width: 4 -*-
And emacs will set the variable for you whenever you visit the file. See http://www.gnu.org/software/emacs/manual/html_node/emacs/Specifying-File-Variables.html for more information on file-based variables.
Of course this might not be a choice if the file is shared among a group - unless you can convince your colleges that this first line comment is completely harmless and extremely useful!
Thanks for all the answers. I added a redraw-display call so that the change is reflected immediately. (Tried posting this as a comment, but can't.)
(global-set-key (kbd "<f8>") 'tf-toggle-tab-width-setting)
(defun tf-toggle-tab-width-setting () "toggle setting tab widths between 4 and 8"
(interactive)
(setq tab-width (if (= tab-width 8) 4 8))
(message "set tab-width to %d." tab-width)
(redraw-display)
)
And along the same lines. :(
(global-set-key (kbd "<f7>") 'tf-toggle-indent-mode-setting)
(defun tf-toggle-indent-mode-setting ()
"toggle indenting modes"
(interactive)
(setq indent-tabs-mode (if (eq indent-tabs-mode t) nil t))
(message "Indenting using %s." (if (eq indent-tabs-mode t) "tabs" "spaces"))
)
Throw this in your .emacs or .emacs.d/init.el file:
(defun toggle-spaces ()
"Toggle tab-width between 4 and 8"
(interactive)
(if (eq tab-width 4)
(setq tab-width 8)
(setq tab-width 4)))
;; This will set Ctrl-g to toggle but you can set it to anything
;; you want.
(global-set-key "\C-g" 'toggle-spaces)