How do I make Emacs show blank spaces? - emacs

How can I make Emacs show blank spaces (like a space, a tab, a line-jump, etc). Many other editors like Kate and Eclipse have this feature and I find it very useful to see when code is indent-broken because of mix of spaces and tabs (particularly Python).

WhiteSpace mode is an Emacs minor mode for visualizing all white space characters in the current buffer. It can be activated with M-x whitespace-mode.
Here is a screenshot of WhiteSpace in action taken directly from the Emacs wiki,
Note: WhiteSpaceMode has now replaced BlankMode

All the possible settings to do that seem to be summarized here (blank-mode) and here and here (ShowWhiteSpace)
also:
(if (>= emacs-major-version 22)
(progn
;; Mode to use with Emacs 22
;; http://emacswiki.org/cgi-bin/wiki/BlankMode
(require 'blank-mode)
;; Mode not active by default: let's activate it
(global-blank-mode t)
;; ... activate it when text mode where color syntax is not active by default
(add-hook 'text-mode-hook 'blank-mode-on)
;; All invisible chars are shown, except newline char.
(setq blank-chars '(tabs spaces trailing lines space-before-tab))
;; Show only for one color, no mark inserted
(setq blank-style '(color))
;; Use for normal space (not shown)
(set-face-background 'blank-space-face nil)
(set-face-foreground 'blank-space-face "black")
;; used for non breakable space
(set-face-background 'blank-hspace-face "PaleGreen")
(set-face-foreground 'blank-hspace-face "black")
;; Used for spaces left of a tab
(set-face-background 'blank-space-before-tab-face "orange")
(set-face-foreground 'blank-space-before-tab-face "black")
;; Used for tab
(set-face-background 'blank-tab-face "lemonchiffon")
(set-face-foreground 'blank-tab-face "black")
;; used for extra space at the end of a line
(set-face-background 'blank-trailing-face "gold")
(set-face-foreground 'blank-trailing-face "black")
;; Used for line too long
(set-face-background 'blank-line-face "snow2")
(set-face-foreground 'blank-line-face "black")
)
(progn
;; For older Emacs prior to version 22.
;; http://www.emacswiki.org/cgi-bin/wiki/show-wspace.el
(require 'show-wspace)
(add-hook 'font-lock-mode-hook 'show-ws-highlight-tabs)
(add-hook 'font-lock-mode-hook 'show-ws-highlight-hard-spaces)
(add-hook 'font-lock-mode-hook 'show-ws-highlight-trailing-whitespace)
)
)

indent-broken? - never use tabs in your code - disk space is cheap these days.
Put (setq-default indent-tabs-mode nil) in your .emacs file. Get used to typing C-x h M-x untabify to untabify the entire buffer. To search for tabs type C-s C-i. If you have obscure control characters in your buffers you can see them with M-x hexl-mode.
Also C-x h M-x indent-region will indent the entire buffer. Some modes like vhdl-mode have a beautify region command.

Related

emacs indentation inconsistent in nesed loops

I am working on C code.
I have a problem same as that in this question.
The solution there is to use spaces instead of tabs. But I'm trying/I'd prefer to use Smart Tabs Mode.
I've tried enabling and disabling both "c-tab-always-indent" and "indent-tabs-mode" (in cc-mode-hook).
The problem I see is that in some parts of the code a nested loop generates two Tabs, as expected(?). But in some cases, it generates only one tab and four spaces, while in other cases it generates only one tab, something like below:
function_name
open brace here
...code indented by 4 spaces (though I want a tab)>
open_brace
_tab_ code under condition
_4spc_ close_brace
some more 4 space aligned code
_tab_ open_brace
_tab+4spc_ code under some block
_tab_ close_brace
some more 4 space aligned code
_tab_ open_brace
_2 tabs_ code aligned as I prefer with two tabs
_tab_ close_brace
Can someone help me with getting the last style in the whole code?
My .emacs file is here (I use a few packages, like cscope and hide-show, but I don't think that should cause problems):
(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.
'(ansi-color-faces-vector [default default default italic underline success warning error])
'(ansi-color-names-vector ["black" "red3" "ForestGreen" "yellow3" "blue" "magenta3" "DeepSkyBlue" "gray50"])
'(c-basic-offset 4)
'(c-cleanup-list (quote (scope-operator space-before-funcall compact-empty-funcall)))
'(c-default-style (quote ((c-mode . "linux") (c++-mode . "linux") (java-mode . "java") (awk-mode . "awk") (other . "gnu"))))
'(c-hanging-braces-alist (quote set-from-style))
; '(c-tab-always-indent t)
'(custom-enabled-themes (quote (tango-dark)))
'(save-place t nil (saveplace))
'(show-paren-mode t))
(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.
)
(setq savehist-additional-variables ;; also save...
'(search-ring regexp-search-ring) ;; ... my search entries
savehist-file "~/.emacs.d/savehist") ;; keep my home clean
(defalias 'yes-or-no-p 'y-or-n-p) ; to answer y or n instead of yes or no :-P ...I'm to lazy
(setq search-highlight t ;; highlight when searching...
query-replace-highlight 1) ;; ...and replacing
(add-to-list 'load-path "~/.emacs.d/packages/")
(add-to-list 'load-path "~/share/emacs/site-lisp")
;;;add packages
;;(require 'doxymacs)
;;; turn ons
(ido-mode)
(savehist-mode 1)
(setq show-paren-mode 1)
(global-linum-mode 1)
(setq column-number-mode 1)
;;cc-mode changes
(require 'smart-tabs-mode)
(require 'xcscope)
(cscope-setup)
(autoload 'smart-tabs-mode "smart-tabs-mode"
"Intelligently indent with tabs, align with spaces!")
(autoload 'smart-tabs-mode-enable "smart-tabs-mode")
(autoload 'smart-tabs-advice "smart-tabs-mode")
(autoload 'smart-tabs-insinuate "smart-tabs-mode")
(defun my-c-mode-common-hook ()
(setq require-trailing-newline 1) ;; Always add a final newline
(which-function-mode 1)
(subword-mode 1)
(hs-minor-mode 1)
(setq show-paren-style 'parenthesis)
; (setq indent-tabs-mode t)
(smart-tabs-mode 1)
)
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)

How do I disable electric indent on RET but still keep other electric characters (e.g., ‘{’)?

In Emacs 24.4, the default indentation behavior has been changed—new lines are now automatically indented. From the release notes:
*** `electric-indent-mode' is now enabled by default.
Typing RET reindents the current line and indents the new line.
`C-j' inserts a newline but does not indent. In some programming modes,
additional characters are electric (eg `{').
I prefer the old behavior, so I added
(electric-indent-mode 0)
to my .emacs file. However, this disables all electric characters, which is not what I intended.
Is there any way to disable the new behavior while still having characters like ‘{’ or ‘:’ trigger an indentation?
You want to remove ?\n from electric-indent-chars. You can do this globally with:
(setq electric-indent-chars (remq ?\n electric-indent-chars))
or only in a particular mode (e.g. C):
(add-hook 'c-mode-hook
(lambda ()
(setq-local electric-indent-chars (remq ?\n electric-indent-chars))))
By checking the documentation for c-electric-brace, I found that the behavior of electric characters is controlled by the buffer-local variable c-electric-flag. It worked after I added the following lines to my .emacs file:
(add-hook 'c-mode-hook
(lambda ()
(set 'c-electric-flag t)))
(add-hook 'c++-mode-hook
(lambda ()
(set 'c-electric-flag t)))

How to change face of whitespace-tab in whitespace-mode to red background in Emacs

I have this code in my .emacs file:
(setq-default show-trailing-whitespace t)
(setq whitespace-style '(face tabs))
(whitespace-mode)
How can I change the var whitespace-tab so my tabs look the same as trailing whitespace (red background)?
An alternative: do not bother with whitespace-mode for this.
Use library highlight-chars.el (see your related question), and just customize face hc-tab (M-x customize-face hc-tab).
Found it, need to use this code:
(setq whitespace-style '(face tabs))
(setq tab-face (make-face 'tab-face))
(set-face-background 'tab-face "red")
(setq whitespace-tab 'tab-face)
(whitespace-mode)

Emacs: highlight 2 or more empty lines

I have entered the following code in my .emacs file to highlight unwanted white spaces.
(require 'whitespace)
(setq whitespace-style '(face empty tabs lines-tail trailing))
(global-whitespace-mode t)
This shows (1) empty lines at the beginning & end of buffer
(2) tabs
(3) lines which go over the 80 character limit
(4) trailing white spaces
I would like emacs to automatically highlight '2 or more empty lines'. Any ideas on how to implement this? I did find a blog post explaining a way to do this with the help of regexp, but I am not sure how to implement this in .emacs file.
Edit 1: Found a way to delete extra blank lines but this still doesn't help me with highlighting multiple blank lines automatically. delete extra blank lines in emacs
Edit 2: Adding the following to .emacs seems to work, but only after I save and reopen file in a buffer.
(add-hook 'change-major-mode-hook '(lambda () (highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)))
Edit 3: After adding (global-hi-lock-mode 1) to .emacs file just before the line in Edit 2, it seems to highlight 1 or more empty lines within the buffer. I am not sure how to modify the regexp so that it will only accept 2 or more empty lines.
Just use library Highlight (highlight.el). That's what it's for.
Use command hlt-highlight-regexp-region (C-x X h x) or hlt-highlight-regexp-to-end (C-x X h e). (To unhighlight a regexp, use C-x X u x or C-x X u e.)
Interactively, you input the regexp to use as usual in Emacs (with C-q C-j to match a newline character, and no need for double backslashes), so you type \(^\s-*$\) C-q C-j.
Your highlight-regexp-solution can be made into a minor-mode with the following elisp (e.g., in your .emacs file).
You can activate the minor mode by right-clicking onto one of the mode-names in the mode-line and then selecting nl2-mode. You can deactivate the minor mode by clicking on nl2 in the mode line and selecting Turn off minor mode.
To understand the code see the help for define-minor-mode and define-key (e.g., C-h f define-minor-mode RET). Note, that in emacs also mouse clicks in menus count as key strokes.
(define-minor-mode nl2-mode
"Highlight two successive newlines."
:global t
:lighter " nl2"
(if nl2-mode
(highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)
(unhighlight-regexp "\\(^\\s-*$\\)\n")))
(define-key mode-line-mode-menu [nl2-mode]
`(menu-item ,(purecopy "nl2-mode") nl2-mode
:help "Highlight two succesive newlines."
:button (:toggle . (bound-and-true-p nl2-mode))))
There are several facts that make highlighting two consecutive empty lines more complicated (font-lock tends to only highlight non-empty regions, linebreaks are limits for the region to re-fontify, re-fontification after buffer changes are required).
The following code shows one way. Maybe, there are easier ways.
(require 'font-lock)
(global-font-lock-mode)
(defface jit-lock-nl2-face '((default :background "yellow"))
"Face to indicate two or more successive newlines."
:group 'jit-lock)
(defun jit-nl2-extend (start end &optional old)
"Extend region to be re-fontified"
(save-excursion
(save-match-data
;; trailing:
(goto-char end)
(skip-chars-forward "[[:blank:]]\n")
(setq jit-lock-end (point))
;; leading:
(goto-char start)
(beginning-of-line)
(skip-chars-backward "[[:blank:]]\n")
(unless (bolp) (forward-line))
(setq jit-lock-start (point)))))
(defun jit-nl2 (jit-lock-start jit-lock-end)
"Highlight two or more successive newlines."
(save-excursion
(save-match-data
(jit-nl2-extend jit-lock-start jit-lock-end)
;; cleanup
(remove-text-properties jit-lock-start jit-lock-end '(font-lock-face jit-lock-nl2-face))
;; highlight
(while (< (point) jit-lock-end)
(if (looking-at "[[:blank:]]*\n\\([[:blank:]]*\n\\)+")
(progn (put-text-property (match-beginning 0) (match-end 0) 'font-lock-face 'jit-lock-nl2-face)
(goto-char (match-end 0)))
(forward-line))))))
(add-hook 'after-change-major-mode-hook (lambda ()
(add-hook 'jit-lock-after-change-extend-region-functions 'jit-nl2-extend)
(jit-lock-register 'jit-nl2)
(jit-lock-mode 1)
))

How to change indentation in text-mode for emacs

I'm in text mode and want my tab key to indent a line to two spaces.
The file looks like this:
Line one
Line two
The cursor is situated before the 'L' : "Line two", and I hit TAB and it gets indented 6 spaces as opposed to the desired 2 spaces.
Actions I've tried:
I've tried updating the variable: tab-stop-list
(setq tab-stop-list '(2 4 6 8 10 12 14 16))
I've tried adding a text-mode-hook
(add-hook 'text-mode-hook
'(lambda ()
(setq tab-width 2)))
Add this to your .emacs :
(add-hook 'text-mode-hook
'(lambda ()
(setq indent-tabs-mode nil)
(setq tab-width 2)
(setq indent-line-function (quote insert-tab))))
See Emacs Indentation Tutorial.
The default for in text-mode will indent to the first non-whitespace character in the line above it.
From the key binding documentation in text mode
TAB (translated from ) runs the command indent-for-tab-command,
which is an interactive compiled Lisp function in `indent.el'.
It is bound to TAB.
(indent-for-tab-command &optional ARG)
Indent line or region in proper way for current major mode or insert a tab.
Depending on `tab-always-indent', either insert a tab or indent.
In most major modes, if point was in the current line's indentation,
it is moved to the first non-whitespace character after indenting;
otherwise it stays at the same position in the text....
Luckily, this can be changed. Adding the following to your text-mode-hook should do what you need:
(setq tab-width 2)
(setq indent-line-function (quote insert-tab))
Try setting
(setq standard-indent 2)
In your .emacs