summary refs log tree commit diff
path: root/www/text.causal.agency/013-hot-tips.7
blob: 63b6e3533e21b08a6ec0bc36b9fb595375c343af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
.Dd December  2, 2020
.Dt HOT-TIPS 7
.Os "Causal Agency"
.
.Sh NAME
.Nm hot tips
.Nd from my files
.
.Sh DESCRIPTION
This is a short list of tips
from my configuration files and code
that might be useful.
.
.Ss Shell
.Bl -tag -width Ds
.It CDPATH=:~
This is useful if you sometimes type,
for example,
.Ql cd src/bin
wanting to go to
.Pa ~/src/bin
but you aren't in
.Pa ~ .
If the path doesn't exist
in the current directory,
.Ic cd
will try it in
.Pa ~
as well.
.
.It alias ls='LC_COLLATE=C ls'
This makes it so that
.Xr ls 1
lists files in ASCIIbetical order,
which puts capitalized names like
.Pa README
and
.Pa Makefile
first.
.
.It git config --global commit.verbose true
Not shell but close enough.
This makes it so the entire diff is shown
below the usual summary
in the editor for a
.Xr git-commit(1)
message.
Useful for doing a quick review
of what you're committing.
.El
.
.Ss (neo)vim
.Bl -tag -width Ds
.It set inccommand=nosplit
This is the only
.Xr nvim 1
feature I really care about
aside from the improved defaults.
This provides a live preview of what a
.Ic :s
substitution command will do.
It makes it much easier to
write complex substitutions.
.
.It nmap <leader>s vip:sort<CR>
This mapping sorts the lines of a paragraph,
or block of text separated by blank lines.
I use this a lot to sort
#include directives.
.
.It nmap <leader>S $vi{:sort<CR>
Similar to the last mapping,
this one sorts lines inside braces.
I use this to sort
switch statement cases
or array initializers.
.
.It nmap <leader>a m':0/^#include <<CR>:nohlsearch<CR>O#include <
I use this mapping to add new
#include directives,
usually followed by
.Ic <leader>s
and
.Ic ''
to sort them
and return to where I was.
.
.It nmap <leader>d :0delete<CR>:0read !date +'.Dd \e%B \e%e, \e%Y'<CR>
I use this to replace the first line of
.Xr mdoc 7
files with the current date.
.El
.
.Ss C
.Bl -tag -width Ds
.It #define Q(...) #__VA_ARGS__
This is what I've started using
to quote things like SQL statements
or HTML fragments in C.
Anything that happens to be valid C tokens,
which is most code,
can be quoted this way.
Macros are not expanded
inside the quoted part.
You can embed (matched) quotes
without having to escape them.
Whitespace gets collapsed,
so you can write nicely formatted multi-line SQL
that doesn't mess up your debug logging,
for example.
.Bd -literal -offset indent
const char *sql = Q(
	INSERT OR IGNORE INTO names (nick, user, host)
	VALUES (:nick, :user, :host);
);
.Ed
.
.It #define BIT(x) x##Bit, x = 1 << x##Bit, x##Bit_ = x##Bit
I use this macro to declare bitflag enums.
It takes advantage of
auto-incrementing enum items
so you don't need to set the values manually.
You also get constants
for both the bit index
and the flag value
for each item.
.Bd -literal -offset indent
enum Attr {
	BIT(Bold),
	BIT(Reverse),
	BIT(Italic),
	BIT(Underline),
};
.Ed
.Pp
For example,
defines
.Sy ItalicBit = 2
and
.Sy Italic = 1 << 2 .
Ignore the extraneous constants.
.
.It typedef int FnType(const char *str, size_t len);
You can just typedef function types!
It annoys me more than it probably should
that everyone writes ugly
function pointer typedefs.
Just stick
.Sy typedef
on the front of a function declaration
and use
.Vt FnType * .
.El
.
.Sh AUTHORS
.Mt june@causal.agency
2013-03-20html: check return value of writeJason A. Donenfeld This squelches a gcc warning. It's also correct that we check to see if there are any partial or failed writes. For now, we just print a warning to stderr. In the future, perhaps it will prove wise to exit(1) on partial writes. 2013-03-20ui-shared: squelch compiler warning.Jason A. Donenfeld Since tail is initialized to 0, we will never get a warning on the last if statement, but recent gcc complains anyway. So, we initialize len as well. Future gcc versions should be able to optimize this out anyway. 2013-03-20cgit.mk: Use SHELL_PATH_SQ to run gen-version.shJohn Keeping On some platforms (notably Solaris) /bin/sh doesn't support enough of POSIX for gen-version.sh to run. Git's Makefile provides SHELL_PATH_SQ to address this issue so we just have to use it. Signed-off-by: John Keeping <john@keeping.me.uk> 2013-03-20cgit.mk: don't rebuild everything if CGIT_VERSION changesJohn Keeping If CGIT_VERSION is in CGIT_CFLAGS then a change in version (for example because you have committed your changes) causes all of the CGit objects to be rebuilt. Avoid this by using EXTRA_CPPFLAGS to add the version for only those files that are affected and make them depend on VERSION. Signed-off-by: John Keeping <john@keeping.me.uk> 2013-03-20ui-patch: use cgit_version not CGIT_VERSIONJohn Keeping We already have a global cgit_version which is set from the #define'd CGIT_VERSION in cgit.c. Change ui-patch.c to use this so that we only need to rebuild cgit.o when the version changes. Signed-off-by: John Keeping <john@keeping.me.uk> 2013-03-20Makefile: re-use Git's Makefile where possibleJohn Keeping