When GNU indent (at least version 2.2.6 and 2.2.10) is used on the following code...
void
main ()
{
int i = 0b01010101;
}
it will be reformatted into this ...
void
main ()
{
int i = 0 b01010101;
}
Is there any option in indent that avoids this behavior?
I got an answer to this question on the indent mailing list:
No intent doesn't support C derivatives: that construct isn't C.
The Binary constants using the `0b' prefix is just an extension of gcc.
#!/bin/sh
indent | sed -r 's/(0) (b[01]+)/\1\2/g'
Related
Take this simple code:
void CChristianLifeMinistryEditorDlg::OnOptionsAddTimeToConcludingComments()
{
BOOL bAddTime = CChristianLifeMinistryUtils::AddRemainingTimeToConcludingComments();
bAddTime = !bAddTime;
CChristianLifeMinistryUtils::SetAddRemainingTimeToConcludingComments(bAddTime);
UpdateMenuGUI();
SetModified(true);
}
The !bAddTime is being flagged:
It says:
Using logical '!' when bitwise '~' was probably intended.
I have used this technique before to toggle boolean values it appears to operate correctly. So why the warning? It is not related to Visual Assist.
Simply change
BOOL bAddTime = ...
to
bool bAddTime = ...
I guess the static code analysis gets confused by BOOL being a type alias for int.
In Haxe, what is the correct way to refer to a define with a dot?
For example, for the library thx.core, how to write conditional compilation against the library name?
#if thx.core
#end
Furthermore, are there general rules for special characters?
The #if flag syntax does not seem to handle dots.
However, Compiler.getDefine() handles most characters, including the dot:
hxml/build command: -D é'"(-è_.çà)=test
#if "é'\"(-è_çà)"
trace('will always be called, even without the -D');
#end
trace(haxe.macro.Compiler.getDefine("é'\"(-è_.çà)")); // test
There is a workaround for dots with initialization macros, even if it is not really pretty:
build.hxml
-x Main.hx
-D abc.def
--macro Macro.parseDefines()
Macro.hx
import haxe.macro.Compiler;
class Macro {
public static macro function parseDefines():Void {
if (Compiler.getDefine("abc.def") != null) {
Compiler.define("abc_def");
}
}
}
Main.hx
class Main {
public static function main() {
#if abc_def
trace("abc.def is defined!");
#end
}
}
Starting with Haxe 4.0.0-rc.2, defines with a dot are permitted in #if as long as they are surrounded by parens:
#if (thx.core)
#end
#if thx.core without parens will likely also work in the future.
In the case of thx.core, you can use thx_core (with an underscore):
#if thx_core
#end
As far as I know there is no general support for special characters other than hyphens (those get translated into underscores by the compiler).
The thx.core library defines -D thx_core itself, using haxelib's support for extraParams.hxml .
I am looking for a way to set where the carriage return, returns to or an equivalent way to do so.
For example I have a line like this:
^ denotes cursor location
myshell>cat file.txt
^
After carriage return it should look like this.
myshell>cat file.txt
^
You're probably after what's collectively called ANSI escape sequences. Its hard to search for if you really have no idea what you're after.
This tiny example saves/restores cursor position:
#include <stdio.h>
int main(int argc, char**argv)
{
char cmd_buf[100];
cmd_buf[0]=0;
while(strncmp(cmd_buf, "quit", 4))
{
printf("mypromt>\033[s <-Cursor should go there\033[u");
fflush(stdout);
fgets(cmd_buf, sizeof(cmd_buf), stdin);
printf("\nYou entered: %s\n", cmd_buf);
}
}
Note that in terminator, gnome-terminal and xterm on Ubuntu, this "magically" supports CTRL+U as-is, but not CTRL+A or CTRL+E.
There are many, many more sequences available. The wikipedia page is probably the simplest reference to get you started.
Update: Also, unless you're doing this as a learning exercise (which I get the impression Benjamin is), to build an interactive shell, you should probably use one of the two well established libraries for shell-style line editing, namely:
readline (GPLv3, but far more popular)
editline (BSD licensed, closest "second place")
They are the libraries that provide the emacs-style (typical default) and vi-style keybindings and history features we all know and love from bash, python, lua, perl, node, etc, etc.
For positioning on the screen, termios is of limited use (the ioctl's dealing with screensize are not in POSIX), and unless you want to assume a lot about the terminal characteristics, control characters and escape sequences have their limitations.
You can do what's asked in curses using the filter function to tell the library you want to use just the current line of the display. As written, the question is puzzling since it does not mention any output other than the current line. But for example (this is exactly what was asked):
#include <curses.h>
int
main(void)
{
int ch, y, x;
filter();
initscr();
cbreak();
addstr("myshell>");
getyx(stdscr, y, x);
while ((ch = getch()) != ERR) {
if (ch == '\n')
move(y, x);
}
endwin();
return 0;
}
However, a usable program would do more than that. There's an example of the filter() function in ncurses-examples, which you may find useful for reading. A screenshot:
For example --
when i do gg=G on
int main()
{
return 0;
}
it will change it to
int main()
{
return 0;
}
What I want is --
int main(){
return 0;
}
The '{' should be on the funciton prototype line
AFAIK:
= re-adjusts indent, it doesn't reformat your codes' style. e.g, the code block style (your question); or add/removing empty lines; add/remove spaces e.g. a=2 -> a = 2 ...
you could do this to change the { before/after you gg=G:
:%s/)\n\s*{\s*$/) {/g
you could also write them into one line, and make a mapping to do it in one short.
e.g, this line:
:%s/)\n\s*{\s*$/) {/g|norm! gg=G
will turn:
int main()
{
if(foo)
{
return 1;
}
if(a>0)
return a;
for(int i=1;i<20;i++)
{
int foo=0;
foo=i;
}
return 0;
}
into
int main() {
if(foo) {
return 1;
}
if(a>0)
return a;
for(int i=1;i<20;i++) {
int foo=0;
foo=i;
}
return 0;
}
EDIT
My original answer suggested :g/)$/j to "join" the two lines, but I found it is not safe, for example:
if (a>0)
return a;
will be turned into
if (a>0) return a;
which is not expected by OP.
To go along with Cubic's Answer
To use astyle without modifying file you can use the command gq and the option `formatprg'
formatprg specifies an external program that will be used to format the buffer. After the command has been run the buffer will be replaced by the output of the program.
For exmample: To set this to work with c files you can put the following in your vimdc
autocmd FileType *.c set formatprg=astyle\ --style=kr
Note: the \ allows you to pass the different command line options to style.
Now to use this in your file you can type gggqG to apply the formatting to the whole file.
You could use astyle, with something like
nnoremap <A-S-f> :w<CR>:!astyle % --style=java<CR>:edit<CR>
Which binds it to Alt-Shift-f (note that this saves/reloads the file which may not always be what you want, there are ways around that but I didn't want to go too much into this right now).
Of course, you'll have to figure out what options to pass to astyle for your preferred formatting yourself.
If you really need to, you can specify __attribute__((weak)) in C (see scriptedmain). This allows a program to double as API and executable, allowing code that imports the API to overwrite the main function.
Does D have a way to do this? Python has if __name__=="__main__": main(), but the weak syntax in C seems much closer.
Yes, using version directives, which require special options to rdmd and dmd.
scriptedmain.d:
#!/usr/bin/env rdmd -version=scriptedmain
module scriptedmain;
import std.stdio;
int meaningOfLife() {
return 42;
}
version (scriptedmain) {
void main(string[] args) {
writeln("Main: The meaning of life is ", meaningOfLife());
}
}
test.d:
#!/usr/bin/env rdmd -version=test
import scriptedmain;
import std.stdio;
version (test) {
void main(string[] args) {
writeln("Test: The meaning of life is ", meaningOfLife());
}
}
Example:
$ ./scriptedmain.d
Main: The meaning of life is 42
$ ./test.d
Test: The meaning of life is 42
$ dmd scriptedmain.d -version=scriptedmain
$ ./scriptedmain
Main: The meaning of life is 42
$ dmd test.d scriptedmain.d -version=test
$ ./test
Test: The meaning of life is 42
Also posted on RosettaCode.
I believe __attribute__((weak)) is a GNU extension which emits special linker instructions for weak linking, so it's very toolchain-specific. There is nothing in DMD for this AFAIK, but other D compilers (GDC or LDC) may support their backends' extensions.
IIRC there is a way to get code to compile to a library rather than an object file. Because of the way the linker searches for things, you can use that to get the same effect; just put the target with the main you want to use first in the link order.