emacs how to setup tab size to 4 chars? - emacs

I have a problem with tab size. It is always 2 chars but I want 4.
My code:
(defun my-c++-mode-hook ()
(set (make-local-variable 'compilation-parse-errors-filename-function)
'process-error-filename)
(local-set-key (kbd "C-c b") 'compile) ; KBD
(setq compile-command "scons")
(setq indent-tabs-mode nil)
(setq tab-width 4)
(setq c-basic-indent 4)
)
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(add-hook 'c-mode-common-hook 'my-c++-mode-hook)
So. When I'm typing:
void f() {
// Here I need 4 chars but I'm getting only 2 when I'm pressing TAB
}

basic offset means other indentations are based on it. So,
for () {
....if () { // 4 spaces
........ // 8 spaces
....}
}
to cite Gnue Emacs
This style variable holds the basic offset between indentation levels
So you won't get:
for () {
....if () { // 4 spaces
...... // 6 spaces
....}
}
But of course you could do that if you want.
And usually, it's recommended to use spaces instead of tabs:
(setq-default indent-tabs-mode nil)
Use M-x untabify to do that for a specific buffer.

Correct answer I've found at Post:
(setq c-basic-offset 4)
But I still do not understand what is (setq c-basic-indent 4) for and why so much suggestions to use it in the internet?

Related

how to configure the way of inserting double quote in auctex with smartparens

recently I updated emacs package and found double quotes were not inserted as regular double quotes (") any more.
Instead, it insert pair of LaTeX double quotes `` and '' on ". On the second typing of ", it reverts to a single LaTeX double quote ``.
I would like to setup auctex and smartparens such that
make a pair of regular double quotes (") inserted, and return to a pair of LaTeX double quote on the second typing of ".
or
make a pair of regular double quotes (") inserted, and return to a single regular double quote on the second typing of ".
my emacs setup for smartparens and auctex is as follows:
(use-package smartparens
:ensure t
:init
(progn
(use-package smartparens-config)
(smartparens-global-mode t)
(show-smartparens-global-mode t)
)
:config
(progn
(setq smartparens-strict-mode t)
(sp-local-pair 'emacs-lisp-mode "'" nil :when '(sp-in-string-p))
)
(use-package tex
:ensure auctex
:init
(setq-default TeX-master nil)
(setq TeX-auto-save t) ;; Enable parse on load
(setq TeX-parse-self t) ;; Enable parse on save
(setq TeX-engine 'xetex)
(setq TeX-command-extra-options "-shell-escape")
(setq TeX-source-correlate-method 'synctex)
(setq TeX-source-correlate-mode t)
(setq TeX-quote-after-quote 1)
(setq reftex-plug-into-AUCTeX t)
(setq latex-preview-pane-enable t)
:config
(add-hook 'LaTeX-mode-hook 'turn-on-reftex) ; with AUCTeX LaTeX mode
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode) ; with AUCTeX LaTeX math mode
(add-hook 'latex-mode-hook 'turn-on-reftex) ; with Emacs latex mode
(add-hook 'TeX-mode-hook '(lambda () (TeX-fold-mode 1)))
(add-hook 'TeX-mode-hook '(lambda () (outline-minor-mode 1)))
(add-hook 'TeX-mode-hook '(lambda () (TeX-PDF-mode 1)))
(add-hook 'TeX-mode-hook 'reftex-mode)
(add-hook 'LaTeX-mode-hook
(lambda ()
(define-key LaTeX-mode-map (kbd "<f10>") 'TeX-command-master)
(define-key LaTeX-mode-map (kbd "C-c `") 'TeX-error-overview)
))
)

Emacs Evil-mode key rebinding

How do I rebind
1 as 4 and 4 as 6 without 1 becoming 6?
Your question isn't clear at all. What do you want to achieve ?
If you want to insert "4" when you press 1:
(global-set-key (kbd "1") (lambda () (interactive) (insert "4")))
(global-set-key (kbd "4") (lambda () (interactive) (insert "6")))

Indent 2 spaces for switch statement in php-mode in Emacs

I setup 2 spaces for php-mode (using c-basic-offset) but switch statement remain 4 spaces,
function foo($items) {
foreach($item in $items) {
switch($item) {
case 1:
return 10;
case 2:
return 20;
}
}
}
how to change swich statement to be 2 spaces if c-basic-offset is 2?
You can customize the case-label offset:
(add-hook 'php-mode-hook
(lambda () (c-set-offset 'case-label 0)))
For more information, look at the definition of coding styles in php-mode.el (looking for c-add-style calls).
With help from #tungd I have code that work:
(c-set-offset 'case-label 2)
(c-set-offset 'statement-case-intro 2)

Moving the cursor inside brackets and intent when pressing RETURN

Let's see if I achieve to explain it. When I type this in emacs 24:
int foo() {|}
Note: | = Cursor
And press the Return key, I get the next output:
int foo() {
|}
So, my question is: how can I achieve the next behaviour?
int foo() {
|
}
Instead of global-set-key you should probably use something like (define-key 'c++-mode-map ..., but here's the basics.
(defun newline-and-push-brace ()
"`newline-and-indent', but bracket aware."
(interactive)
(insert "\n")
(when (looking-at "}")
(insert "\n")
(indent-according-to-mode)
(forward-line -1))
(indent-according-to-mode))
(global-set-key (kbd "RET") 'newline-and-push-brace)
You could define a function that checks if you're in that situation and does what you want if you are, and otherwise just calls whatever the newline command for your major mode is, e.g.:
(defun brackets-newline (point)
(interactive "d")
(setq next-char (char-before point))
(if (and next-char
(char-equal next-char 123))
;; if we are sitting in front of a close bracket, do what you want
(progn
(newline)
(newline)
(previous-line)
;;call whatever "TAB" is in this mode
(funcall (key-binding (kbd "TAB"))))
;; otherwise just insert a newline
(newline)))
Then bind this to (kbd "RET")
There may be a better way to do this using defadvice or some such, this seemed to work pretty well for me though.

Emacs auto compelete paren, indent and new line - how to?

In C - I want that when I type a { and then } emacs will insert a new line between them and then set the cursor in between them. For example:
int main() {
now I type } and the following happens:
int main()
{
//cursor is here
}
Edit: forgot to mention - I want emacs to know that when defining a function that it should do what was described above but when doing a for loop, or if statement for example I want it to do the following:
if (bla bla) {
type } and... :
if (bla bla) {
//cursor here
}
If you don't mind that the behaviour will be only almost, but not exactly the way you described it, there is a built-in way to do that. It's the auto-newline feature, that can be activated with the key combination C-c C-a or this line your .emacs:
(c-toggle-auto-newline 1)
The difference is that it will do the reformatting right after entering the opening brace {. When you finally enter the closing brace, it will indent it the right way, too.
You also need to set the right CC Mode style. The style "cc-mode" seems to define things the way you described it. You can activate it with the key combination C-c . and then choosing cc-mode, or the .emacs line
(c-set-style "cc-mode")
The c-mode functions are autoloaded and will therefore usually not be available while loading the .emacs file. Therefore you should wrap them in a hook for c-mode, like this
(add-hook 'c-mode-hook
(lambda ()
(c-toggle-auto-newline 1)
(c-set-style "cc-mode")))
As for the { stuff:
(define-minor-mode c-helpers-minor-mode
"This mode contains little helpers for C developement"
nil
""
'(((kbd "{") . insert-c-block-parentheses))
)
(defun insert-c-block-parentheses ()
(interactive)
(insert "{")
(newline)
(newline)
(insert "}")
(indent-for-tab-command)
(previous-line)
(indent-for-tab-command)
)
Paste the above into your .emacs. You can activate it with c-helpers-minor-mode.
Edit: The above inserts everything by just pressing {. The script below should do it if you type {}:
(defun insert-latex-brackets (opening closing) ; prototype function for all enclosing things
(interactive)
(insert opening)
(insert " ")
(insert closing)
(backward-char (+ 1 (length closing )))
)
(defun check-char-and-insert (char opening closing)
(interactive)
(if (equal char (char-to-string (char-before (point))))
(progn (delete-backward-char 1)
(insert-latex-brackets opening closing))
(insert char)
)
)
(local-set-key (kbd "}") 'check-char-and-insert)
One last note: You could try using yasnippet, which can be a real time saver used properly.