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)
Related
I have found many ways to automatically insert "}" after typing "{" but never found the following implementation:
After typing "{" it moved on the next line (with indented if necessary), next is an empty string with the cursor, and "}" on the next line:
if (i == 0)*here we typing "{"*
and get the following:
if (i == 0)
{
|
}
and for nested brackets:
if (i == 0)
{
if (j == 0)
{
|
}
}
How to do this?
Note: I already use yasnipped but it does not work for functions.
If you are using electric-pair-mode, you can supply your own function:
(defun my-electric-pair-open-newline-between-pairs()
"Indent paired char and empty line"
(when (and (eq last-command-event ?\n)
(< (1+ (point-min)) (point) (point-max))
(eq (save-excursion
(skip-chars-backward "\t\s")
(char-before (1- (point))))
(matching-paren (char-after))))
(save-excursion
(insert "\n")
(indent-according-to-mode))
(indent-according-to-mode))
nil)
(setq-default electric-pair-open-newline-between-pairs 'my-electric-pair-open-newline-between-pairs)
(electric-pair-mode 1)
It will do what you describe only if you hit return between empty brackets.
I did something similar to this to try to emulate Eclipse's newline functionality.
(defun my-newline ()
(interactive)
(let ((s (buffer-substring (point-at-bol) (point))))
(cond
((string-match ".*{$" s) ; matching a line ending with {
(move-end-of-line nil)
(insert "}") ; insert }. Can be removed if already being inserted
(backward-char 2)
(newline-and-indent)
(forward-char)
(newline-and-indent)
(move-beginning-of-line nil)
(backward-char 1)
(newline-and-indent))
(t (newline-and-indent)))))
You can then attach it to whatever keybinding you like. I overrode C-j as that was what I was used to.
(defun my-newline-hook ()
(local-set-key (kbd "C-j") 'my-newline))
(add-hook 'java-mode-hook 'my-java-hook)
The regex and steps taken can be adjusted to fit your needs.
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?
I am working with emacs24 with cc-mode, I want to know how to make my emacs more "clever". After I type a }, it will auto insert a new line and indent as excepted. I want to know how to switch the point to previous line.
For example, when i define a function, Now my emacs behavior is:
void f()
{
}
//point
"//point" is the position of cursor after } was input.
But i want is this:
void f()
{
//point
}
I hope the position of cursor can switch to previous line and indent automatically.
I know emacs can do this, but I don't know how to do it, who can help me?
I think you are after these.. C-M-u, C-M-d, C-M-f and C-M-b
Practice a bit... They are kind of global and they do behave contextually in almost all modes..
UPDATE:
ohh.. It seems you want to place the cursor automatically.. actually in more general Emacs will help you not to type } at all. I mean emacs can insert closing paran automatically.
There is inbuilt one
electric pair mode
third party
autopair.el
I don't trust anything electric, so I wrote this function.
(defconst insert-logical-brackets-logical-bracket-begin "{")
(defconst insert-logical-brackets-logical-bracket-end "}")
(defconst insert-logical-brackets-default-style 0)
(make-variable-buffer-local 'logical-bracket-begin)
(make-variable-buffer-local 'logical-bracket-end)
(make-variable-buffer-local 'insert-logical-brackets-default-style)
(defun insert-logical-brackets(&optional style)
"If STYLE = 0(default, according to `insert-logical-brackets-default-style' value), make a newline before opening bracket, if line is not empty. Make a newline after closing bracket, if there is something after this bracket. Make two newlines in the middle.
If STYLE = 1, don't make newlines before opening a bracket(one of c styles).
If STYLE = 2, don't make newlines before opening and after closing bracket.
If STYLE = 3, allways make all newlines.
If STYLE is not nil, don't make newlines between brackets(still makes before/after lines)."
(interactive "P")
(when (eq style nil)
(setq style insert-logical-brackets-default-style))
(funcall indent-line-function)
(unless (or (eq 1 style) (eq 2 style))
(when (or (/= (point) (save-excursion (back-to-indentation) (point))) (eq 3 style))
(newline)
(funcall indent-line-function)))
(unless (and (integerp style) (= 2 style))
(when (or (not (looking-at "\n")) (eq 3 style))
(newline)
(funcall indent-line-function)
(forward-line -1)
(goto-char (point-at-eol))))
(insert logical-bracket-begin)
(funcall indent-line-function)
(let ((return-point (point)))
(when (or (not style) (or (eq 0 style) (eq 1 style) (eq 2 style) (eq 3 style)))
(newline)
(funcall indent-line-function)
(setq return-point (point))
(newline))
(insert logical-bracket-end)
(funcall indent-line-function)
(goto-char return-point)))
Take a look at template systems like yasnippet: http://www.emacswiki.org/emacs/CategoryTemplates
auto-indent-mode maybe what you want!
ESS/Stata mode in emacs incorrectly indents lines that follow lines ending in operators. It seems to incorrectly interpret these lines as multiline commands.
For example:
gen foo = 1
/* generate another variable */
gen bar = 1
The line "gen bar = 1" should not be indented. It looks like EMACS interprets the trailing slash in the comment as an operator, and thinks this line of code spans two lines.
In fact, multiline commands in stata have 3 trailing slashes, and newlines without 3 trailing slashes indicate the end of a statement. e.g. the following indentation would be correct:
gen bar = 1
gen ///
foo = 1
Is there something I can put in my .emacs to correct this behavior? I don't want to give up automatic tabbing completely - it works very well for everything except comments that /* look like this */.
Thanks,
Pnj
You're right, ESS interprets the trailing / as an indication of line continuation. This is hard-coded into the function ess-continued-statement-p, so to modify the behaviour you have to rewrite the code. The following code (in your .emacs) works for your examples.
(eval-after-load 'ess-mode
'(defun ess-continued-statement-p ()
"this is modified code"
(let ((eol (point)))
(save-excursion
(cond ((memq (preceding-char) '(nil ?\, ?\; ?\} ?\{ ?\]))
nil)
;; ((bolp))
((= (preceding-char) ?\))
(forward-sexp -2)
(looking-at "if\\b[ \t]*(\\|function\\b[ \t]*(\\|for\\b[ \t]*(\\|while\\b[ \t]*("))
((progn (forward-sexp -1)
(and (looking-at "else\\b\\|repeat\\b")
(not (looking-at "else\\s_\\|repeat\\s_"))))
(skip-chars-backward " \t")
(or (bolp)
(= (preceding-char) ?\;)))
(t
(progn (goto-char eol)
(skip-chars-backward " \t")
(or (and (> (current-column) 1)
(save-excursion (backward-char 1)
;;;; Modified code starts here: ;;;;
(or (looking-at "[-:+*><=]")
(and (looking-at "/")
(save-excursion (backward-char 1)
(not (looking-at "*")))))))
;;;; End of modified code ;;;;
(and (> (current-column) 3)
(progn (backward-char 3)
(looking-at "%[^ \t]%")))))))))))
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.