How to show dots for whitespaces in Emacs 24.5? - emacs

I wonder, isn't there a simple way to show dots for whitespaces in Emacs 24.5? I've tried to find information and and have always found some complex solutions which worked for some people and didn't for others. Isn't there a de-facto and easy way?

Use whitespace-mode, or even global-whitespace-mode to see them in all buffers. If you only want to see spaces, configure it with the following:
(setq whitespace-style '(space-mark))
(setq whitespace-display-mappings '((space-mark 32 [183] [46])))
Or, if you want a different colour for the dots, include also
(setq whitespace-space 'your-favourite-whitespace-face) ; <- insert the face
(setq whitespace-style '(face spaces space-mark))
You can customize the settings rather then set the values manually.

Related

How do I make diff-mode display whitespace similar to other programming modes?

When I enable whitespace-mode with added whitespace options:
(add-hook 'diff-mode-hook (lambda ()
(setq-local whitespace-style
'(face
tabs
spaces
wspaces
trailing
indentation::space
indentation::tab
newline))
(whitespace-mode 1)))
But I do not get the appearance similar to in programming modes, but instead just colors to represent different types of whitespace and it looks unpleasant. Is there a way to make it look like in programming modes?
space-mark defaults to a middle-dot (Unicode \u00B7 ·) -- "SPACEs and HARD SPACEs are visualized via display table." wspaces is not a valid setting -- thus, substitute wspaces with space-mark.
The original poster may also be interested in visualizing tabs with tab-mark (Unicode \u00BB »); and hard returns with newline-mark (i.e., the dollar sign $). They are both visualized via the display table.
(add-hook 'diff-mode-hook (lambda ()
(setq-local whitespace-style
'(face
tabs
tab-mark
spaces
space-mark
trailing
indentation::space
indentation::tab
newline
newline-mark))
(whitespace-mode 1)))
No idea what you mean by make it look like in programming modes. You might want to explain that or give an example.
Library highlight-chars.el gives you more flexibility wrt how you indicate whitespace (and other chars). See Highlight Chars. You can pretty much do whatever you like. (But see #1: better describe what you actually want.)

Can I just tabify begnning of lines in emacs

I like format all my code using tab instead of space, but I just want to convert spaces to tabs at the beginning of each lines.
Can tabify just convert space to tabs at the beging of lines?
The documentation for tabify mentions a suitable value for operating on line-leading whitespace only. I used it to write this function which I find handy, but you could just set it in your init file and forego a separate function:
(defun tabify-leading (start end)
"Call `tabify' with `tabify-regexp' set so that only leading
spaces are treated."
(interactive "r")
(setq tabify-regexp-old tabify-regexp)
(unwind-protect
(progn
(setq tabify-regexp "^\t* [ \t]+")
(tabify start end))
(setq tabify-regexp tabify-regexp-old)))
Take a look at SmartTabs
It'll add onto several modes (for several languages) and make it so code indentation are tabs only, while ensuring the display of code is correct regardless of the viewer's tab width.
Excerpt:
Tabs are only used at the beginning of lines. Everything else, like ASCII art and tables, should be formatted with spaces.
Tabs are only used for expressing the indentation level. One tab per “block” – any remaining whitespace is spaces only.
Together with this, you can "tabify" existing code using the tabify command.

fix an auto-complete-mode and linum-mode annoyance

I'm using auto-complete-mode which I think is totally fantastic. I'm also a big fan of linum-mode but I've got a very irritating issue when the two are used together, especially when I'm working in a new buffer (or a buffer with very few lines).
Basically the buffer is 'x' lines long but when auto-complete kicks in it "adds" lines to the buffer, so linum-mode keeps switching, for example, between displaying line numbers on one column or two columns, depending on whether auto-complete is suggesting a completion or not.
So you type a sentence and you see your buffer's content frantically shifting from left to right at every keypress. It is really annoying.
I take it the solution involves configuring the linum-format variable but I don't know how.
Ideally it would be great if my linum-format was:
dynamic
right-aligned
considering there are 'y' more lines to the buffer than what the buffer actually has
My rationale being that auto-complete shall not suggest more than 'y' suggestion and that, hence, the two shall start playing nicely together.
For example, if 'y' is set to 20 and my buffer has 75 lines, then linum should use two columns: because no matter where I am auto-complete shall not make the buffer 'bigger' than 99 lines.
On the contrary, if 'y' is still set to 20 and my buffer has 95 lines, then linum should use three columns because otherwise if I'm near the end of the buffer and auto-complete kicks in my buffer shall start "wobbling" left and right when I type.
I'd rather not hardcode "3 columns wide" for linum.
I guess using "dynamic but always at least two columns" would somehow fix most annoyances but still something as I described would be great.
P.S: I realize that my 'fix' would imply that linum would always display on at least two columns, and I'm fine with that... As long as it stays right-aligned and use 2, 3 or 4 columns depending on the need.
Simply put the following line in .emacs which resolves this issue. It is in auto-complete.el.
(ac-linum-workaround)
I've written a couple of previous answers on modifying the linum-mode output, which you could probably adapt to your purposes.
Relative Line Numbers In Emacs
Colorize current line number
Edit: Here's the most basic version of that code (also on EmacsWiki, albeit somewhat buried), which doesn't modify the default output at all, but uses the techniques from those other answers to be more efficient than the default code. That's probably a more useful starting point for you.
(defvar my-linum-format-string "%4d")
(add-hook 'linum-before-numbering-hook 'my-linum-get-format-string)
(defun my-linum-get-format-string ()
(let* ((width (length (number-to-string
(count-lines (point-min) (point-max)))))
(format (concat "%" (number-to-string width) "d")))
(setq my-linum-format-string format)))
(setq linum-format 'my-linum-format)
(defun my-linum-format (line-number)
(propertize (format my-linum-format-string line-number) 'face 'linum))
Just have the same problem, after seeing 'patching the source' I believe it could be done with advice. Here is what I come up with
(defadvice linum-update
(around tung/suppress-linum-update-when-popup activate)
(unless (ac-menu-live-p)
ad-do-it))
I would like to use popup-live-p as mentioned but unfortunately it requires the variable for the popup, which we couldn't know in advance.
Update:
I ended up patching the source for linum.el. I added an extra hook that runs before updates.
Here's the patched file: linum.el (github)
Here's the code I have in my init.el:
;; Load custom linum.
(load-file "~/.emacs.d/linum.el")
;; Suppress line number updates while auto-complete window
;; is displayed.
(add-hook 'linum-before-update-hook
'(lambda ()
(when auto-complete-mode
(if (ac-menu-live-p)
(setq linum-suppress-updates t)
(setq linum-suppress-updates nil)))))
Hope it helps!

Using tabs alone for indentation in Emacs

I love Emacs but don't like how it does indentation: either an uncontrollable mix of tabs-and-spaces or just spaces ((setq indent-tabs-mode nil)).
I want Emacs to do do indentation:
With tabs alone.
Do the indentation to a fixed number of places (not 6 sometimes, 8 other times and 4 in some other places).
Be able to set one level of indentation as being equal to 4 (or 2) spaces.
If I change the value of the tab stop, all newly-opened or reloaded files should use the new value (or can this change be affected only by re-starting Emacs?)
Is all of the above possible? Some settings in .emacs or a package?
Most IDEs (e.g. Eclipse) offer the above.
smart tabs would insert tabs and spaces contextually.
Personally I only use spaces for both indentation and alignment (at least for my own projects). Here is another article on emacswiki I found very useful about the topic
For C/C++/Java, you could try adding to your mode hook an identical tab-width, indent-level and c-basic-offet:
(defun my-c-mode-common-hook ()
(setq c-indent-level 3
c-brace-offset -3)
(setq c-basic-offset 3)
(setq-default tab-width 3)
(setq tab-width 3))
(add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
This makes it so when you're in cc-mode, tabs and indenting are equal so emacs will always choose tabs over spaces. See also Indenting C
The tab-width can be set elsewhere and you can apply it to other modes in conjunction with indent length variables like python's python-indent.
Edit:
Actually, it looks like kindahero's link pretty much does this: http://www.emacswiki.org/SmartTabs
I use tabs for indentation. But when someone else using a different editor, they could see that the indentation is gone. So, you can select the piece of code that you indented using tabs and run "M-x untabify". This replaces the tabs with white space, so the first said issue won't be there for the users using a different editor.

completely lucid tabs and spaces in emacs?

The title of my question is a reference to sane tabs in emacs.
Basically what I want is to globally set tabs and indention to work in
some uniform way. I feel like emacs is so much better than TextMate
or BBEdit but really the way they handle indention is simple and great
for my purposes. In emacs if you use some tab/space scheme that's
different than the scheme enforced by a minor mode you use you're in
trouble.
When I press enter I'd like to be moved to the next line indented to
the right place using tabs. If I can have my cake and eat it too I'd
like to be indented using spaces if the rest of the file is composed
that way.
I've tried these also:
doing tabs in emacs
force emacs to use tabs
Thanks to anyone who can help me achieve this.
-Mike
Perhaps (global-set-key (kbd "RET") 'newline-and-indent) is what you want?
(Or reindent-then-newline-and-indent if that's available, or you could just hit C-j instead of the Enter key.)
For this part of your question:
If I can have my cake and eat it too I'd like to be indented using spaces if the rest of
the file is composed that way.
does this do what you want?
(defun dtrt-indent ()
(setq indent-tabs-mode
(save-excursion
(goto-char (point-min))
(search-forward "\t" nil t))))
(add-hook 'text-mode-hook #'dtrt-indent)
(add-hook 'c-mode-hook #'dtrt-indent)
; etc for all modes you care about
So if there's a tab anywhere in the buffer, indent using tabs; if there is no tab, indent using spaces.
if you do your setup as described:
(setq indent-tabs-mode t)
(setq-default indent-tabs-mode t)
(setq tab-width 4) ;; 8 is way too many
(setq default-tab-width 4) ;; 8 is way too many
(global-set-key (kbd "RET") 'newline-and-indent)
The indent-tabs-mode thing will tell emacs to create your indentation by using TABS and SPACES to make up the desired indentation (defined by the individual mode). This means, if you want to have a TAB inserted instead of TABS/SPACES you need to configure your mode to use tab-width as indentation.
For example if you use c-mode and select cc-mode as indentation style (select with C-c .) which uses 4 as indentation value, newline-and-indent will insert spaces.
To conclude:
Check that your mode uses tab-width as indentation
Check that your mode doesn't overwride indent-tabs-mode (python-mode seems to do this)
Although I personally don't like TABS good luck on your journey :)
The best strategy is to convince your programming mode of choice to
indent things the way you like. This is generally very easy; I am
picky about indentation and my emacs always does the right thing
automatically. (That means that "indent-region" also always does
what I want, which is very convenient.)