-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalign.ex
More file actions
101 lines (100 loc) · 2.72 KB
/
align.ex
File metadata and controls
101 lines (100 loc) · 2.72 KB
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
" Defines an :align alias for aligning = signs, or some other delimiter. This
" can be handy when you're trying to make a Makefile look pretty.
" Contributed by Ian Utley (iu@apertus.uk.com)
alias align {
"Align any = signs (or other given text) in selected line
local f=0 i=0 k report=0 nosaveregexp magic magicchar=^$.[* noignorecase
"
" The following if tests to see if we have visually highlighted lines.
"
if ( !> !!= "" )
then {
!< mark a
!> mark b
let f=1
}
if ( f == 1)
then {
"
" Initialise i which will store the alignment column.
" Mark the current line to return the cursor at the end.
"
set i=0
mark z
"
" Remove any whitespace before the alignment character.
"
'a,'b s/[ ]*!(=)\$/!(=)\$/
"
" We could be aligning != <= or >= so we want to keep this letter
" near. Of course we may not be aligning an equals but we commonly do.
"
if ( "!(=)\$" == "=" )
then {
'a,'b s/[ ]*\([!!<>]*\)!(=)\$[ ]*/ \1!(=)\$ /
}
"
"
let f=0
'a,'bglobal /!(=)\$/ {
"
" Special case for the top line as -1 will not work.
"
if ( current("line") == 1 )
then {
1 insert ""
let f=1
}
-1
/!(=)\$
"
"
" Remember the largest column number for alignment.
"
if (current("column")>i)
then let i=current("column")
"
" Special case removal
"
if ( current("line") > 1 && f == 1)
then {
1 delete
let f=0
}
}
"
" Do the alignment.
"
let f=0
'a,'bglobal /!(=)\$/ {
"
" Special case for the top line as -1 will not work.
"
if ( current("line") == 1 )
then {
1i ""
let f=1
}
-1
/!(=)\$
"
" Not sure why I need to add +1
"
let k=i-current("column")+1
s/\([!!<>]*\)!(=)\$/ \1!(=)\$/
eval s/ *\\\( \{(k)\}[!!<>]*!(=)\$\\\)/\1
"
" Special case removal
"
if ( current("line") > 1 && f == 1 )
then {
1 delete
let f=0
}
}
"
" Return the cursor to the line it was previously on.
"
'z
}
}