Skip to content

Instantly share code, notes, and snippets.

@deton
Last active July 17, 2017 00:56
Show Gist options
  • Save deton/5027300222758d2ed12b to your computer and use it in GitHub Desktop.
Save deton/5027300222758d2ed12b to your computer and use it in GitHub Desktop.
GNU screenで継続行をコピーする際に、`\`等を除く方法
bind g command -c buffilt
bind -c buffilt \\ eval writebuf screen 'stuff "scrbuf.sh rmwrap-emacs&&exit^M"'
bind -c buffilt > eval writebuf screen 'stuff "scrbuf.sh rmwrap-vim&&exit^M"'
bind -c buffilt + eval writebuf screen 'stuff "scrbuf.sh rmwrap-mutt&&exit^M"'
bind -c buffilt ^E eval writebuf screen 'stuff "scrbuf.sh rmwrap-width&&exit^M"'
bind -c buffilt $ eval writebuf screen 'stuff "scrbuf.sh picklastarg&&exit^M"'
bind -c buffilt ' ' eval writebuf screen 'stuff "scrbuf.sh join-with-blank&&exit^M"'
bind -c buffilt e eval writebuf screen 'stuff "scrbuf.sh edit&&exit^M"'
bind -c buffilt g eval writebuf screen 'stuff "scrbuf.sh grep2l&&exit^M"'
bind -c buffilt h eval writebuf screen 'stuff "scrbuf.sh rmhtmltag&&exit^M"'
bind -c buffilt i eval writebuf screen 'stuff "scrbuf.sh rmindent&&exit^M"'
bind -c buffilt j eval writebuf screen 'stuff "scrbuf.sh join&&exit^M"'
bind -c buffilt q eval writebuf screen 'stuff "scrbuf.sh quote&&exit^M"'
bind -c buffilt u eval writebuf screen 'stuff "scrbuf.sh pickurl&&exit^M"'
#!/usr/bin/awk -f
# remove emacs wrap markers of long line.
/\\$/ {
sub(/\\$/, "");
line = rm2ndmark($0, line);
line = line $0;
next;
}
line != "" {
line = rm2ndmark($0, line);
printf("%s%s\n", line, $0);
line = "";
next;
}
{
print;
}
END {
if (line != "") {
print line;
}
}
# remove second '\' for multi-byte
function rm2ndmark(curline, prevline) {
if (curline ~ /^[^ -~[:cntrl:]]/ && prevline ~ /\\$/) {
sub(/\\$/, "", prevline);
}
return prevline;
}
#!/usr/bin/awk -f
# remove mutt wrap markers of long line.
/^+/ {
sub(/^+/, "");
line = line $0;
next;
}
line != "" {
print line;
# FALLTHRU
}
{
line = $0;
}
END {
if (line != "") {
print line;
}
}
#!/usr/bin/awk -f
# remove vim wrap markers of long line.
/>$/ {
line = rmmark($0, line);
line = line $0;
next;
}
line != "" {
line = rmmark($0, line);
printf("%s%s\n", line, $0);
line = "";
next;
}
{
print;
}
END {
if (line != "") {
print line;
}
}
# remove wrap marker '>' for multi-byte
function rmmark(curline, prevline) {
if (curline ~ /^[^ -~[:cntrl:]]/ && prevline ~ />$/) {
sub(/>$/, "", prevline);
}
return prevline;
}
function! JoinByWidth(start, end, width)
execute a:end
let lnum = line('.')
let curline = getline(lnum)
while lnum > a:start
-
let lnum -= 1
let prevline = getline('.')
let prevwidth = strdisplaywidth(prevline, 1)
if curline =~ '^\S' && prevwidth == a:width
j!
elseif strwidth(matchstr(curline, '.')) > 1 && prevwidth == a:width - 1
" wide char
j!
"elseif curline =~ '^ \S' && prevwidth == a:width
" j!
"elseif curline =~ '^\S' && prevwidth == a:width - 1
" j!
endif
let curline = prevline
endwhile
endfunction
call JoinByWidth(1, '$', winwidth(0))
1,$p
#!/bin/sh
# apply filter and overwrite screen-xchg file
SRC=~/tmp/screen-xchg
DEST=~/tmp/screen-xchg2
case "$1" in
rmwrap-emacs)
rmwrap-emacs.awk $SRC >$DEST
;;
rmwrap-vim)
rmwrap-vim.awk $SRC >$DEST
;;
rmwrap-mutt)
rmwrap-mutt.awk $SRC >$DEST
;;
rmwrap-width)
vim -X --noplugin -e -s <~/lib/scrbuf/rmwrap-width.vim $SRC >$DEST
;;
edit)
cp $SRC $DEST && $EDITOR $DEST
;;
join)
tr -d '\n' <$SRC >$DEST
;;
join-with-blank)
tr '\n' ' ' <$SRC >$DEST
;;
rmindent)
# メール内でURLをインデントする際に使われる全角空白を除去
sed -e 's/^[  ]*//' $SRC >$DEST
;;
grep2l)
awk '/[[:graph:]]:/{print substr($0,1,index($0,":")-1)}' $SRC >$DEST
;;
pickurl)
sed -e 's/.*\(https\?:\/\/[-a-zA-Z0-9_\.@:\/~#!?\&=+%]\+\).*$/\1/' $SRC >$DEST
;;
picklastarg)
awk '{print $NF}' $SRC >$DEST
;;
rmhtmltag)
sed -e 's/<[^>]*>//g' -e 's/\&nbsp;/ /g' -e 's/\&quot;/"/g' -e 's/^ *//' $SRC >$DEST
;;
quote)
# copy from lynx to memo with quote(> )
# (remove numbered label from link like "[32]link")
sed -e 's/\[[0-9][0-9]*\]//g' $SRC | nkf -f74 -w -m0 | sed -e 's/^/> /' >$DEST
;;
*)
echo "$0: unknown filter: $1"
exit 2
;;
esac
EXITCODE=$?
if [ $EXITCODE -ne 0 ]; then
echo "$0: filter($1) exit code is not zero: $EXITCODE"
exit 1
fi
# シェルプロンプトに貼り付ける際に便利なように、最後の改行を削除。
printf %s "$(cat $DEST)" > $SRC
screen -X eval readbuf 'echo ""'
# 貼り付ける内容が長いと貼り付けできない
#mv $DEST $SRC
#screen -X register . "$(cat $SRC|sed -e 's/\$/\\$/g')"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment