Skip to content

Save and load file positions

David-Apps edited this page Nov 13, 2022 · 20 revisions

You can use these functions to save and load your position in files. This can make it easier to return to where you were in a file after taking a break.

The s function writes the file name and the current line number on a pair of lines in a new buffer. You can save this pair of lines to use later with the l or lall functions. You can use the ^ command to return to the file in the previous buffer.

The l function reads a pair of lines, opening the file whose file name appears on the first line in the pair and moving to the line number that is on the second line in the pair.

The sall function writes pairs of lines for the current file in each session to a single buffer. You can save these pairs to use later with the l or lall functions.

The lall function reads each pair of lines in the buffer, opening each file and moving to its line number.

The sfiles and lfiles functions show how you can use these functions. The sfiles function writes a pair of lines for each session to a file. The lfiles function opens this file and loads the files it contains.

# Write the current file name and line number.
# usage <s
# This function uses session 999.
function+s {
db0
H-
ub
if(*) {
A
up
b
} else {
A
up
}
,.w999
down
# Remove surrounding HTML code.
g/^<br><a href='.*'>$/f +,+2d
,s/^<br><a href='\(.*\)'>$/$1/f
# Replace character entity references.
,s/&apos;/'/gf
if(?) {
}
,s/&amp;/\&/gf
if(?) {
}
# Get the line number.
e999
s/.*/%line/f
bw
eret
$r999@.
q999
bw
}

# Open a file at a line number.
# usage <l
# This function uses session 999.
function+l {
db0
H-
sw+
.g/^\d+$/fX
if(?) {
/^\d+$/fX
}
.w999
-X
bw
g
1X
e999
r !seq $(((EB_DOT - 1) / 1000000))
.g/^0$/fd
if(?) {
2X
while(*) {
eret
+1000000X
eret
$d
2X
}
}
r !seq $((((EB_DOT - 1) % 1000000) / 1000))
.g/^0$/fd
if(?) {
2X
while(*) {
eret
+1000X
eret
$d
2X
}
}
0r !seq $(((EB_DOT - 1) % 1000))
.g/^0$/fd
if(?) {
$-1,$d
while(*) {
eret
+X
eret
d
}
}
bw
eret
q999
}

# Write the file name and current line number of each session.
# usage: <sall
# This function uses session 998.
function+sall {
db0
while(*) {
e-
}
while(*) {
A
^
e+
}
while(*) {
<s
w+998
if(?) {
w998
}
^
e-
}
enew
r998
e998
bw
eret
q998
bw
}

# Open each file at its line number.
# usage: <lall
function+lall {
db0
$X
<l
while(*) {
M0
;d
<l
}
q
}

# Save file positions.
# usage: <sfiles
function+sfiles {
db0
<sall
w ~/file_positions.txt
while(*) {
q
}
}

# Open file positions.
# usage: <lfiles
function+lfiles {
db0
e ~/file_positions.txt
<lall
}