Zsh: how to see all buffers? - buffer

You can push things to buffer with ^Q and pop them with ESC-g. Alt+x vi-set-buffer changes buffer somehow. How can I see all the buffers? They are probably some files to look at.

^Q (and Esc-Q) and Esc-g work with an emacs buffer, so I don't think they interact with the vi buffers.
This works in emacs mode (set -o emacs):
function _showbuffers()
{
local nl=$'\n' kr
typeset -T kr KR $'\n'
KR=($killring)
typeset +g -a buffers
buffers+=" Pre: ${PREBUFFER:-$nl}"
buffers+=" Buffer: $BUFFER$nl"
buffers+=" Cut: $CUTBUFFER$nl"
buffers+=" L: $LBUFFER$nl"
buffers+=" R: $RBUFFER$nl"
buffers+="Killring:$nl$nl$kr"
zle -M "$buffers"
}
zle -N showbuffers _showbuffers
bindkey "^[o" showbuffers
To demonstrate it, try each of these steps, pressing Alt-o (letter "O") or Esc-o after each one:
To show the cut buffer, type echo abc then press Ctrl-u
To show the kill ring (the previous cut buffer moves there), type echo def then press Ctrl-u (or Home Ctrl-K or Ctrl-X Ctrl-K)
To show the pre buffer, type echo 'ghi, press enter, type jkl'
Press enter to execute the previous command and clear the current buffer (don't forget to press Alt-o to see the change)
To show the L and R buffers, type echo "mno pqr" and press the left arrow a couple of times

Related

Printing really long prints on Epson Thermal Printers

Working on a project where I'm printing really long prints on epson thermal printers (80mm x 18000mm) (length is variable per print).
My documents are set up as 1000mm long pages ie 14000mm print is 14 pages.
I'm able to print a correct length document with this command
lp -d 10_0_0_11 -o media=Custom.80x18000mm -o PageSize=Custom.80x1000mm -o TmtPaperSource=DocNoFeedCut -o landscape -o fit-to-page -o outputorder=reverse -o TmtSpeed=1 -o TmtPaperReduction=Both example/url/to/document
But this is really slow and don't want to use the command line for convenience.
When printing the document via Preview MacOS it only prints a portion of the document at the printer's max print size of 80x297mm. I have a custom page size of 80x1000mm setup but it seems to be ignoring that.
Any better workaround to gain the extra speed printing without the command line?

How to create a blank but valid EPS?

One of our printing applications runs an external program which does some magic and sometimes returns a barcode in EPS format to be printed on the document.
if [ ... some magic ]
then
gnu-barcode -b $1 -c -e code39 -u mm -t 1x3 > $TMP.ps
ps2epsi $TMP.ps $TMP.eps
cat $TMP.eps
rm -f $TMP.eps $TMP.ps
else
cat /dev/null
fi
This works OK. However, it generates an annoying warning on the printing application side about not receiving a valid EPS when the else ... runs and we do cat /dev/null. I need to return a blank but valid EPS instead of the cat /dev/null. How can I accomplish this?
The EPS format is defined in Adobe Technical note 5002, its available on the web but it moves around so much I won't attempt to post a URL. However, unless you are a PostScript programmer that probably won't help you.
The simplest possible valid EPS would be something like:
%!PS-Adobe-2.0 EPSF-3.0
%%BoundingBox:0 0 0 0
That's the only required content in an EPSF. Of course, a real printing application might not like a BoundingBox of 0 0 0 0.

Error when appending string from word or variable

I'm trying to append two strings in gforth, but I get some scary looking error messages.
While s" foo" s" bar" append type cr works fine, as soon as I start storing strings in variables or creating them from words, I get errors. For instance:
: make-string ( -- s )
s" foo" ;
: append-print ( s s -- )
append type cr ;
make-string s" bar" append-print
Running it produces the following error:
$ gforth prob1.fs -e bye
gforth(41572,0x7fff79cc2310) malloc: *** error for object 0x103a551a0: pointer being realloc'd was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6.
I'm well versed in C, so it seems pretty clear that I'm using Forth incorrectly!
I suppose I need to learn something very basic about memory management in Forth.
Can anyone please explain what goes wrong here, and what I should do?
I also run into problems when I try to append a string that is stored in a variable:
variable foo
s" foo" foo !
foo s" bar " append type cr
This ends in a loop that I have to break:
$ gforth prob2.fs
foo��^C
in file included from *OS command line*:-1
prob2.fs:4: User interrupt
foo s" bar " append >>>type<<< cr
Backtrace:
$10C7C2E90 write-file
For reference, I'm using gforth 0.7.2 on Mac OS X. I would be very grateful for some good explanations on what's going on.
Update
I can see the definition of append:
see append
: append
>l >l >l >l #local0 #local1 #local3 + dup >l resize throw >l #local4 #local0 #local3 + #local5
move #local0 #local1 lp+!# 48 ; ok
So, it would seem I need to manage memory myself in Forth? If so, how?
Solution
Andreas Bombe provides the clue below. The final program that works would be
: make-string ( -- s )
s" foo" ;
: append-print
s+ type cr ;
make-string s" bar" append-print
Output is
$ gforth b.fs -e bye
foobar
append uses resize on the first string make space to append the second string. This requires that the string be allocated on the heap.
When you compile a string with s" into a word, it gets allocated in the dictionary. If you try resize (directly or indirectly through append) on that pointer you will get the error you see.
Normally s" has undefined interpretation semantics. Gforth defines its interpretation semantics for convenience as allocating the string on the heap. That's why it works (in gforth) as long as you don't compile it.
Edit:
I've found the definition of append, it's part of libcc.fs (a foreign function interface builder as it seems) and not a standard word. This is the definition in the source, more readable than the see decompile:
: append { addr1 u1 addr2 u2 -- addr u }
addr1 u1 u2 + dup { u } resize throw { addr }
addr2 addr u1 + u2 move
addr u ;
Immediately before that is a definition of s+:
: s+ { addr1 u1 addr2 u2 -- addr u }
u1 u2 + allocate throw { addr }
addr1 addr u1 move
addr2 addr u1 + u2 move
addr u1 u2 +
;
As you can see this one allocates new memory space instead of resizing the first string and concatenates both strings into it. You could use this one instead. It is not a standard word however and just happens to be in your environment as an internal implementation detail of libcc.fs in gforth so you can't rely on it being available elsewhere.
The usage of strings in Forth doesn't warrant dynamic allocation mostly and at least not in your example. You can get by nicely with buffers that you allocate yourself using ALLOT
and some very simple words to manipulate them.
[ALLOT uses the data space (ANSI term) in an incremental fashion for adding words and buffers. It is not dynamic, you can't release an item without removing at the same time all items ALLOT-ted later. It is also simple. Do not confuse with ALLOCATE which is dynamic and is in a separate extension wordset]
You make a fundamental mistake in leaving out the specification of your append-buffer.
It doesn't work, and we don't know how it is supposed to work!
In ciforth's an example could be:
: astring S" foo" ;
CREATE buffer 100 ALLOT \ space for 100 chars
\ Put the first string in `buffer and append the second string.
\ Also print the second string
: append-print ( s s -- )
type cr 2swap
buffer $!
buffer $+! ;
astring s" bar" append-print
bar OK \ answer
buffer $# TYPE
foobar OK \ answer
Other Forths have other non-standard words to manipulate simple strings. An excursion through malloc land is really not necessary. In the gforth documentation you can look up 'place' and find an equivalent family of words.
Also nowadays (Forth 2012) you can have strings like so "foo".

extract a line from a file using csh

I am writing a csh script that will extract a line from a file xyz.
the xyz file contains a no. of lines of code and the line in which I am interested appears after 2-3 lines of the file.
I tried the following code
set product1 = `grep -e '<product_version_info.*/>' xyz`
I want it to be in a way so that as the script find out that line it should save that line in some variable as a string & terminate reading the file immediately ie. it should not read furthermore aftr extracting the line.
Please help !!
grep has an -m or --max-count flag that tells it to stop after a specified number of matches. Hopefully your version of grep supports it.
set product1 = `grep -m 1 -e '<product_version_info.*/>' xyz`
From the man page linked above:
-m NUM, --max-count=NUM
Stop reading a file after NUM matching lines. If the input is
standard input from a regular file, and NUM matching lines are
output, grep ensures that the standard input is positioned to
just after the last matching line before exiting, regardless of
the presence of trailing context lines. This enables a calling
process to resume a search. When grep stops after NUM matching
lines, it outputs any trailing context lines. When the -c or
--count option is also used, grep does not output a count
greater than NUM. When the -v or --invert-match option is also
used, grep stops after outputting NUM non-matching lines.
As an alternative, you can always the command below to just check the first few lines (since it always occurs in the first 2-3 lines):
set product1 = `head -3 xyz | grep -e '<product_version_info.*/>'`
I think you're asking to return the first matching line in the file. If so, one solution is to pipe the grep result to head
set product1 = `grep -e '<product_version_info.*/>' xyz | head -1`

How to print Java code on A3 page avoiding line-wrapping

I have to print Java code that some times reaches 300 columns (characters per line) in A3 paper and whatever editor I use (e.g. textmate) wraps the lines in order to fit in A4 paper.
Any suggestions?
cheers,
Asterios
Your editor undoubtably has either a Page Setup dialog or a Preferences dialogue as part of the Print Dialogue which will allow you to set the Paper Size to use for printing.
Even Notepad supports this
I finally made it to print using enscript. Here is the command I used to print Java code into PDF (and the used the pdf to print).
enscript -r -Ejava -M A3 -C -f "Courier8" input.java -o - | ps2pdf - output.pdf
where:
-r prints in landscape mode
-C prints the line numbers
-f changes the font and size
-M sets the output media to A3 (default is A4)
-Ejava adds syntax highlighting (you can also use --color if you need
colors in syntax highlighting but
they are not nicely printed in
greyscale)
It seems unlikely that every editor tries to format for A4. Which other editors have you tried? Does textmate not have a page size option? (Hmm... seems not)
Try a different editor that does let you set page size. Word, even.

Resources