Skip to content

Commit

Permalink
code formatting
Browse files Browse the repository at this point in the history
 * added formatting styles for clang-format and perltidy
 * formatted main.c and shellex.in accordingly
 * TODO: integrate formatting into Makefile / hooks / CI
  • Loading branch information
pseyfert committed Jan 21, 2017
1 parent 09b811e commit b25d3c3
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 23 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ preload/shellex_preload.so
conf/shellexrc
shellex
urxvt/shellex
urxvt/shellex.in.bak
doc/man/shellex.1
doc/man/shellex.man
90 changes: 90 additions & 0 deletions preload/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
Language: Cpp
# BasedOnStyle: WebKit
AccessModifierOffset: -4
AlignAfterOpenBracket: DontAlign
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands: false
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: All
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: false
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: true
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
BeforeCatch: false
BeforeElse: false
IndentBraces: false
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
ColumnLimit: 0
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: false
DisableFormat: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros: [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
- Regex: '^(<|"(gtest|isl|json)/)'
Priority: 3
- Regex: '.*'
Priority: 1
IndentCaseLabels: false
IndentWidth: 4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: Inner
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Right
ReflowComments: true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard: Cpp03
TabWidth: 8
UseTab: Never
...

13 changes: 6 additions & 7 deletions preload/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
*/
#define _GNU_SOURCE
#include <dlfcn.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* We can not take this from <sys/ioctl.h>, because it would define the
Expand All @@ -19,7 +19,7 @@ struct winsize {
unsigned short ws_ypixel;
};

int ioctl (int d, int request, char *argp) {
int ioctl(int d, int request, char *argp) {
static int (*orig_ioctl)(int, int, char *);
if (orig_ioctl == NULL) {
orig_ioctl = dlsym(RTLD_NEXT, "ioctl");
Expand All @@ -36,27 +36,26 @@ int ioctl (int d, int request, char *argp) {
* the right size! Leave max_rows negative to indicat that it still needs to
* be read from the SHELLEX_SIZE_FILE */

if (max_rows < 0 ) {
if (max_rows < 0) {

char *fname = getenv("SHELLEX_SIZE_FILE");
if (fname != NULL && fname[0] != '\0') {
FILE *stream = fopen(fname,"r");
FILE *stream = fopen(fname, "r");
char str[5] = "-500";
if (stream != NULL) {
char *ret = fgets(str,5,stream);
char *ret = fgets(str, 5, stream);
fclose(stream);
if (ret != NULL) {
/* this may be -500 */
max_rows = atoi(str);
if (max_rows > 0 ) {
if (max_rows > 0) {
unlink(fname);
}
}
}
}
}


int retval = orig_ioctl(d, request, (char *)argp);
struct winsize *ws = (struct winsize *)argp;

Expand Down
4 changes: 4 additions & 0 deletions urxvt/.perltidyrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
-pt=2
-ce
-l=91
-b
42 changes: 26 additions & 16 deletions urxvt/shellex.in
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ sub geometry_from_ptr {
my $ptr = { $self->{X}->QueryPointer($self->DefaultRootWindow) };

for my $output (@outputs) {
if ($output->{x} <= $ptr->{root_x} && $ptr->{root_x} < $output->{x} + $output->{w}) {
if ($output->{x} <= $ptr->{root_x} && $ptr->{root_x} < $output->{x} + $output->{w})
{
$self->{x} = $output->{x};
if ($self->{bottom}) {

# The real y-coordinate will change during execution, when the window grows
$self->{y} = $output->{y} + $output->{h};
} else {
Expand All @@ -54,6 +56,7 @@ sub max {
}
return $max;
}

sub min {
my $min = shift;
while (my $n = shift) {
Expand Down Expand Up @@ -85,7 +88,8 @@ sub geometry_from_focus {
# The (x,y) coordinates we get are relative to the parent not the
# root-window. So we just translate the coordinates of the upper-left
# corner into the coordinate-system of the root-window
my (undef, undef, $fx, $fy) = $self->{X}->TranslateCoordinates($focus, $self->DefaultRootWindow, 0, 0);
my (undef, undef, $fx, $fy) =
$self->{X}->TranslateCoordinates($focus, $self->DefaultRootWindow, 0, 0);

# Returns the area (in pixel²) of the intersection of two rectangles.
# To understand how it works, best draw a picture.
Expand Down Expand Up @@ -117,6 +121,7 @@ sub geometry_from_focus {
$max_area = $area;
$self->{x} = $output->{x};
if ($self->{bottom}) {

# The real y-coordinate will change during execution, when the window grows
$self->{y} = $output->{y} + $output->{h};
} else {
Expand Down Expand Up @@ -152,14 +157,14 @@ sub on_start {

# TODO: Remove compatibility code in future version
if (defined $self->x_resource("%.edge") || defined $self->x_resource("%.pos")) {
print "WARNING: URxvt.shellex.* resources are deprecated and will be removed in the future. Use shellex.*\n"
print
"WARNING: URxvt.shellex.* resources are deprecated and will be removed in the future. Use shellex.*\n";
}


if ($self->x_resource("edge") eq 'bottom' || $self->x_resource("%.edge") eq 'bottom') {
print "position should be at the bottom\n";
$self->{bottom} = 1;
$self->{y} = $self->{h};
$self->{y} = $self->{h};
} else {
print "position should be at the top\n";
}
Expand All @@ -172,11 +177,11 @@ sub on_start {
$self->geometry_from_focus();
}

# This environment variable is used by the LD_PRELOAD ioctl-override to
# determine the values to send to the shell
# TODO revisit communication protocol (from file to pipe?)
# TODO check if user defined their own SHELLEX_MAX_ROWS, which should be used like
#$ENV{SHELLEX_MAX_ROWS} = $sane_max_rows < $ENV{SHELLEX_MAX_ROWS} ? $sane_max_rows : $ENV{SHELLEX_MAX_ROWS} ;
# This environment variable is used by the LD_PRELOAD ioctl-override to
# determine the values to send to the shell
# TODO revisit communication protocol (from file to pipe?)
# TODO check if user defined their own SHELLEX_MAX_ROWS, which should be used like
#$ENV{SHELLEX_MAX_ROWS} = $sane_max_rows < $ENV{SHELLEX_MAX_ROWS} ? $sane_max_rows : $ENV{SHELLEX_MAX_ROWS} ;
my $sane_max_rows = int($self->{h} / $self->fheight) - 10;

$ENV{SHELLEX_MAX_ROWS} = $sane_max_rows;
Expand All @@ -196,7 +201,8 @@ sub on_start {
$self->{row_height} = $self->fheight + $self->x_resource('lineSpace');

my $height = $self->{row_height} + 2 * $self->{border};
my $y = $self->{y};
my $y = $self->{y};

# Our initial position is different, if we have to be at the bottom
$y -= $height if $self->{bottom};

Expand All @@ -219,7 +225,7 @@ sub on_line_update {
# TODO: Does this work as intended, if there is an empty line in the
# middle?
my $nrow = 0;
for my $i ($self->top_row .. $self->nrow-1) {
for my $i ($self->top_row .. $self->nrow - 1) {
if ($self->ROW_l($i) > 0) {
$nrow++;
}
Expand All @@ -232,7 +238,7 @@ sub on_line_update {
# window up a little bit
my $y = $self->{y};
if ($self->{bottom}) {
$y -= 2+$nrow*$self->fheight;
$y -= 2 + $nrow * $self->fheight;
}
$self->cmd_parse("\e[8;$nrow;t\e[3;$self->{x};${y}t");
();
Expand All @@ -256,28 +262,31 @@ sub predict_term_size {
$i += $line->end - $line->beg + 1;
unless ($line->beg <= $row && $row <= $line->end) {
$n += $line->end - $line->beg + 1;
next
next;
}

my $len = ($row - $line->beg) * $self->ncol + $col;

# Because there might be control-sequences in $string, affecting the
# number of lines, we need to manually walk it
for (my $j = 0; $j < length($string); $j++) {
for (my $j = 0 ; $j < length($string) ; $j++) {

# Linebreaks mean the creating of a new line, finishing the old one
if (substr($string, $j, 1) eq "\n") {
$len = ($len == 0 ? 1 : $len);
$n += ceil(($len * 1.0) / $self->ncol);
$len = 0;
next;
}

# Carriage-returns mean starting from the beginning. Though the new
# len does not really have to be 0 (because the text is not
# actually erased) it is a good enough estimate for now
if (substr($string, $j, 1) eq "\r") {
$len = 0;
next;
}

# We just add one per other char. This actually might not work
# correctly with wide-chars, but it is a good enough estimate for
# now
Expand Down Expand Up @@ -306,7 +315,7 @@ sub on_add_lines {
# window up a little bit
my $y = $self->{y};
if ($self->{bottom}) {
$y -= 2+$nrow*$self->fheight;
$y -= 2 + $nrow * $self->fheight;
}
$self->cmd_parse("\e[8;$nrow;t\e[3;$self->{x};${y}t");
();
Expand Down Expand Up @@ -348,6 +357,7 @@ sub on_map_notify {
my ($self, $ev) = @_;

$self->{X}->SetInputFocus($self->parent, 2, $self->{data}{event}{time});

# We use GetInputFocus as a syncing-mechanism
$self->{X}->GetInputFocus();

Expand Down

0 comments on commit b25d3c3

Please sign in to comment.