Can I just tabify begnning of lines in emacs - 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.

Related

How to show dots for whitespaces in Emacs 24.5?

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.

Wrong indentation of comments in Emacs

In many languages, the line comment starts with a single symbol, for example # in Python and R.
I find that in Emacs, when writing such line comments, I have to repeat the comment symbol twice to make the correct indentation.
See the following example:
(setq x-select-enable-clipboard t)
;using a single comment symbol indents wrongly
;; repeating the comment symbol indents fine
(setq-default c-basic-offset 4)
With a single ; at the beginning of the line cannot get the correct indentation. How to get the correct setting? Thanks!
EDIT:
I found the solution myself. In ESS's document:
Comments are also handled specially by ESS, using an idea borrowed
from the Emacs-Lisp indentation style. By default, comments beginning
with ‘###’ are aligned to the beginning of the line. Comments
beginning with ‘##’ are aligned to the current level of indentation
for the block containing the comment. Finally, comments beginning with
‘#’ are aligned to a column on the right (the 40th column by default,
but this value is controlled by the variable comment-column,) or just
after the expression on the line containing the comment if it extends
beyond the indentation column. You turn off the default behavior by
adding the line (setq ess-fancy-comments nil) to your .emacs file.
So I put this in my .emacs:
(setq ess-fancy-comments nil) ; this is for ESS
I think for Python mode, it has a similar variable.
Your example use Emacs Lisp, in this language the standard convention is that a single ; is indented to the right, whereas two ;; is indented like code would be indented at that point. I strongly recommend that you stick to this convention, otherwise your code would stand out as being different. And three ;;; is indented to the left. Four ;;;; is left indented, and used for major sections. (See https://www.gnu.org/software/emacs/manual/html_node/elisp/Comment-Tips.html)
For Ruby, comments always indent as code, as far as I know.
The major mode should take care of this properly. If not, consider filing an enhancement request or bug report to the maintainers. Of course, "properly" might be in the eye of the beholder. You can try to make your preferences known, however. And check whether the major-mode code might already have user options for this.
Beyond that, the function that is the value of variable comment-indent-function governs this. Normally, this is set by the major mode. You can set it to any function you want (e.g. on the mode hook, so that your definition overrides the one provided by the major-mode code).
It accepts no arguments, and it returns the column you want the comment to be indented to.
Here is code that indents a comment to column 0, for example:
(defun foo () (setq comment-indent-function (lambda () 0)))
(add-hook 'SOME-MODE-HOOK 'foo 'APPEND)
For Emacs-Lisp mode, for example, you would use (add-hook 'emacs-lisp-mode-hook 'foo 'APPEND).

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.)

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.

How do I run an Emacs hook when a buffer is modified?

Building on Getting Emacs to untabify when saving certain file types (and only those file types) , I'd like to run a hook to untabify my C++ files when I start modifying the buffer. I tried adding hooks to untabify the buffer on load, but then it untabifies all my writable files that are autoloaded when emacs starts.
(For those that wonder why I'm doing this, it's because where I work enforces the use of tabs in files, which I'm happy to comply with. The problem is that I mark up my files to tell me when lines are too long, but the regexp matches the number of characters in the line, not how much space the line takes up. 4 tabs in a line can push it far over my 132 character limit, but the line won't be marked appropriately. Thus, I need a way to tabify and untabify automatically.)
Take a look at the variable "before-change-functions".
Perhaps something along this line (warning: code not tested):
(add-hook 'before-change-functions
(lambda (&rest args)
(if (not (buffer-modified-p))
(untabify (point-min) (point-max)))))
Here is what I added to my emacs file to untabify on load:
(defun untabify-buffer ()
"Untabify current buffer"
(interactive)
(untabify (point-min) (point-max)))
(defun untabify-hook ()
(untabify-buffer))
; Add the untabify hook to any modes you want untabified on load
(add-hook 'nxml-mode-hook 'untabify-hook)
This answer is tangential, but may be of use.
The package wide-column.el link text changes the cursor color when the cursor is past a given column - and actually the cursor colors can vary depending on the settings. This sounds like a less intrusive a solution than your regular expression code, but it may not suit your needs.
And a different, tangential answer.
You mentioned that your regexp wasn't good enough to tell when the 132 character limit was met. Perhaps a better regexp...
This regexp will match a line when it has more than 132 characters, assuming a tabs width is 4. (I think I got the math right)
"^\\(?: \\|[^ \n]\\{4\\}\\)\\{33\\}\\(.+\\)$"
The last parenthesized expression is the set of characters that are over the limit. The first parenthesized expression is shy.