I am currently working on a C project with other people where they have decided that the indent style for wrapped lines is to indent 8 spaces, instead of where the opening parenthesis/bracket is, e.g.
if (long cond 1 &&
long cond 2 &&
long cond 3) {
//do stuff
}
instead of
if (long cond 1 &&
long cond 2 &&
long cond 3) {
//do stuff
}
However, emacs defaults to the latter and I don't know how to change it to behave like the former. Anyone know how to do this?
Put this in your .emacs:
(c-set-offset 'arglist-cont-nonempty 8)
Related
If I have code like so:
func main() {
a := `line 1
line 2
line 3
line 4`
fmt.Println(a)
}
doing forward-word or backword-word when the cursor is
in the multi-line string moves to the end or the start
of the string respectively. I'm using go-mode.el v1.3.1
from https://github.com/dominikh/go-mode.el
Similar if the cursor is inside the string and you do
I tried doing
(modify-syntax-entry ?` ".")
in the go-mode-hook but that didn't change the behavior.
I'd like to adjust indentation of my source code correctly at a time after I select some block of it.
Is there any function or key with which I can do it including parenthesis?
Here is original selected block of sample code I'd like to adjust indentation.
while(1)
{
func1();
if( )
{
func2();
}
}
if( x == 0 )
{
aa = 1;
}
This would be the correctly indented code how I just want to adjust.
while(1)
{
func1();
if( )
{
func2();
}
}
if( x == 0 )
{
aa = 1;
}
Select your code and press C-M-\, which should be bound to indent-region:
C-M-\
Indent all the lines in the region, as though you had typed TAB at the beginning of each line (indent-region).
If a numeric argument is supplied, indent every line in the region to that column number.
I'm using evil mode because I like vim editing keymap.
In my case, block auto indentation can be done by equal(=) key after selecting a code block.
It's very convenient to rearrange block of code in a c-default-style.
(1) install evil package
(2) Insert this code into you emacs init file.
; indentation style for c, c++, java
(setq c-default-style "linux"
c-basic-offset 4)
(3) select block using v and direction key
(4) press '='
I'm trying to write a skeleton-function to output expressions in a loop. Out of a loop I can do,
(define-skeleton test
""
> "a")
When I evaluate this function it outputs "a" into the working buffer as desired. However, I'm having issues when inserting this into a loop. I now have,
(define-skeleton test
"A test skeleton"
(let ((i 1))
(while (< i 5)
>"a"
(setq i (1+ i)))))
I would expect this to output "aaaaa". However, instead nothing is outputted into the working buffer in this case. What is happening when I insert the loop?
The > somestring skeleton dsl does not work inside lisp forms.
You can however concatenate the string inside a loop:
(define-skeleton barbaz
""
""
(let ((s ""))
(dotimes (i 5)
(setq s (concat s "a")))
s)
)
My understanding is that code such as
> "a"
only works at the first nesting level inside a skeleton.
[EDIT] Regarding your question
What is happening when I insert the loop?
The return value of the let form (that is, the return value of the while form)is inserted. I do not know why it does not raise an error when evaluating > "a", but the return value of a while form is nil, so nothing is inserted.
I do agree that there's not much point using define-skeleton if you're going to need an (insert function within the skeleton.
This is also a rather trivial example to be using define-skeleton.
That said, they are often easier to read than a defun and useful when you want to create a function that inserts text (and optionally, takes input).
For example you may wish to have a different character repeated a set no. of times... Below, str refers to the argument supplied with the function (usually a string) and v1, v2 are the default names for local variables in a skeleton. Thus:
(define-skeleton s2 ""
nil ; don't prompt for value of 'str'
'(set 'v1 (make-string 5 (string-to-char str)))
\n v1 \n \n)
Below, calling the function leads to a newline, the string, then leaves the cursor at the position indicated by the square brackets [].
(s2 "a")
aaaaa
[]
I tried to solve this problem, which is to count the amount of lines, blank spaces, and tabs.
My solution was incorrect because I don't know how to use { }.
main ()
{
int newline;
int tab;
int blank;
int c;
newline = 0;
tab = 0;
blank = 0;
while ((c = getchar()) != EOF)
if (c == '\n')
++newline;
if (c == '\t')
++tab;
if (c == 32)
++blank;
printf("lines: %d tabs: %d blanks: %d\n", newline, tab, blank);
}
In my code, only new lines were being counted. Tabs and spaces were never counted.
I know the answer is to add { } around the if statements section. But I only know this because I searched google for the solution.
Perhaps it is just me, but K&R do not really explain when I should use { }.
Can someone explain how I can know to add { } to my above code?
When I read the code, it seems fine without {}. It means I truly don't understand its usage. Why aren't tabs and spaces counted in the above code?
Is there another book on C that you can recommend?
I have no programming experience.
The syntax of a simple if is : if (<condition>) <statement>. The <statement> can be a single statement (as you have in your code) or it can be a block (zero or more statements enclosed in braces). When you have a single statement, it is strictly a question of style whether you surround it with braces—the behavior is the same.
One relatively straightforward "rule of thumb": Look for the semi-colon(s). Referring to your example, starting at the "while", read forward until you see a semi-colon. If you want anything beyond that semi-colon to be executed as part of your while block, you need to wrap it all in curly braces.
Another way to look at it: The semi-colon is a statement terminator. It terminated blank = 0 as your previous statement; it terminates not only the if but also the enclosing while statement. Thus, to execute the following if as part of the while block, you need to enclose the ifs in curly braces.
Oh, and by the way, C and similar languages do not attach syntactic meaning to whitespace. It is at most treated as a separator. Any indentation you choose to apply is for the benefit of the human reader only; it has no significance to the compiler.
Any if/while/for can be followed by a single statement without braces, or any number of statements encapsulated in braces. If you write an if/while/for followed by many statements and no braces, only the first statement falls under the if/while/for. Note that whatever whitespace you use does not matter, it is only for readability.
This is the equivalent of your code if it was written with braces:
while ((c = getchar()) != EOF)
{
if (c == '\n')
{
++newline;
}
}
if (c == '\t')
{
++tab;
}
if (c == 32)
{
++blank;
}
What you want:
while ((c = getchar()) != EOF)
{
if (c == '\n')
{
++newline;
}
if (c == '\t')
{
++tab;
}
if (c == 32)
{
++blank;
}
}
which is equivalent to
while ((c = getchar()) != EOF)
{
if (c == '\n')
++newline;
if (c == '\t')
++tab;
if (c == 32)
++blank;
}
Exclusion of braces serves absolutely no purpose but style. If you are ever in doubt, include the braces.
Braces ({ and }) are used to convert zero or more statements into a single compound statement. Anywhere you wish for a group of statments to be treated as a single statement you should use braces.
For example, a while loop executes a single statement whilst it evaluates to true:
while(some-condition)
statement
Obvious there will be times when you want to execute multiple statements, and this is where you surround them with { and } in order to turn them into a single compound statement.
In visual lisp, you can use (atoi "123") to convert "123" to 123. It seems there is no "atoi" like function in clisp ?
any suggestion is appreciated !
Now i want to convert '(1 2 3 20 30) to "1 2 3 20 30", then what's the best way to do it ?
parse-interger can convert string to integer, and how to convert integer to string ? Do i need to use format function ?
(map 'list #'(lambda (x) (format nil "~D" x)) '(1 2 3)) => ("1" "2" "3")
But i donot know how to cnovert it to "1 2 3" as haskell does:
concat $ intersperse " " ["1","2","3","4","5"] => "1 2 3 4 5"
Sincerely!
In Common Lisp, you can use the read-from-string function for this purpose:
> (read-from-string "123")
123 ;
3
As you can see, the primary return value is the object read, which in this case happens to be an integer. The second value—the position—is harder to explain, but here it indicates the next would-be character in the string that would need to be read next on a subsequent call to a reading function consuming the same input.
Note that read-from-string is obviously not tailored just for reading integers. For that, you can turn to the parse-integer function. Its interface is similar to read-from-string:
> (parse-integer "123")
123 ;
3
Given that you were asking for an analogue to atoi, the parse-integer function is the more appropriate choice.
Addressing the second part of your question, post-editing, you can interleave (or "intersperse") a string with the format function. This example hard-codes a single space character as the separating string, using the format iteration control directives ~{ (start), ~} (end), and ~^ (terminate if remaining input is empty):
> (format nil "Interleaved: ~{~S~^ ~}." '(1 2 3))
"Interleaved: 1 2 3."
Loosely translated, the format string says,
For each item in the input list (~{), print the item by its normal conversion (~S). If no items remain, stop the iteration (~^). Otherwise, print a space, and then repeat the process with the next item (~}).
If you want to avoid hard-coding the single space there, and accept the separator string as a separately-supplied value, there are a few ways to do that. It's not clear whether you require that much flexibility here.