@Zooker:
Below is a 'Print Source Code' plugin for the standard PureBasic IDE.
I wrote it in 2006 but recently updated it for PureBasic 4.31. Please feel free to experiment with it.
You will need to compile it and then plug the .EXE file into the PureBasic IDE via this menupath:
Tools -> Configure Tools -> New
Set the Commandline to the full pathname of the compiled .EXE file.
Set Arguments to the exact contents of the next line:
"%SELECTION" "%TEMPFILE" "%FILE"
Leave the Working Directory field empty
Set Name to something like: Print Source Code
Set Event to trigger the tool to: Menu or Shortcut
Leave everything else empty.
Before running the plugin (from the bottom section of the IDE Tools menu), highlight the source lines to be printed on the default printer.
If 0 or 1 lines are highlighted, the entire source code will be printed.
Code: Select all
; PrintFile AKJ 23-Jul-09 PB Add-in Tool
; Print a text file whose path is given as a command line parameter
; Lines too wide for the page are wrapped onto subsequent print lines
; Page titles, page numbers & line numbers are printed (as suggested by yrret)
; Any PureBUILD build number is included in the title (as suggested by yrret)
; If multiple text lines are selected, only those lines are printed,
; except: if only one line is selected, the entire file is printed
; The add-in tool configuration arguments must be "%SELECTION" "%TEMPFILE" "%FILE"
; www.purebasic.fr/english/viewtopic.php?t=19876
EnableExplicit
;- Constants
#program$="PrintFile"
#version$="2.4"
#file=1
; The following constants may be edited by the user:
#gap=5 ; Vertical gap (leading) in pixels between print lines
#Lmargin=200 ; Left hand margin in pixels
#Rmargin=100 ; Right margin in pixels
#Tmargin=180 ; Top margin in pixels
#Bmargin=0 ; Bottom margin in pixels
#continue$="" ; Continuation character shown at the start of non-empty overflow lines
;}
;- Declarations
Global NewList lines$() ; Buffer for storing the print lines prior to printing
Define selection$ ; Limits of selected text
Define linefrst, linelast ; First/last selected text lines
Define path$="" ; Pathname of file to be printed
Define pagew, pageh ; Page dimensions in pixels sans margins
Define linenumw ; Width in pixels of the line number column
Define printw ; Width available for printing file text (excludes the line number column)
Define lineh ; Vertical distance in pixels between lines
Define lpp ; Lines per page available for printing (including title lines)
Define pages ; Number of pages that will physically be printed
Define pagenumx ; Start position of the right-aligned page number
Define title$="" ; Title (excluding page number) at the top of each page
Define titlew ; Width in pixels available for the title
Define ln$ ; The text line currently being processed/printed
Define tmp$ ; Temporary string variable
Define whitespace$ ; A copy of any whitespace at the start of the current line
Define cut; Position at which a text line must be cut to fit on the page
Define p ; Position within text strings and command line parameter number
Define overflow$ ; Flag: "#" (line has a line number) or "+" (continuation line)
Define build$="" ; PureBUILD build number
Define pagenum=0 ; Current page number
Define linenum=0 ; Current printed line number within source file
Define line=0 ; Lines printed on the current page
Define y=#Tmargin ; Vertical print position on the current page
;}
;- Identify text selection from the command line
selection$=ProgramParameter(0)
linefrst=Val(StringField(selection$, 1, "x"))
linelast=Val(StringField(selection$, 3, "x"))
; If the selection ends at the beginning of a line, treat it as ending on the previous line
If StringField(selection$, 4, "x")="1": linelast-1: EndIf
; If the selection is confined to one line, print the entire file
If linefrst>=linelast: linefrst=1: linelast=99999: EndIf
;}
;- Get file pathnames from the command line
; path$ will point to the latest in-memory version of the file
; title$ will contain the program's pathname if it has ever been saved
For p=1 To 2
tmp$ = ProgramParameter(p)
If FindString(LCase(tmp$), "\purebasic_tempfile", 1) ; If a temporary filename
path$=tmp$
ElseIf Len(tmp$)
title$=tmp$
EndIf
Next p
If path$="": End: EndIf ; Quit without error message
If FileSize(path$)<=0: : End: EndIf ; Quit without error message
;}
;- Initialise printer and get printer metrics
If DefaultPrinter()=0
MessageRequester(#program$+" Error", "Cannot open default printing device")
End
EndIf
StartPrinting("Text File")
pagew=PrinterPageWidth()-#Lmargin-#Rmargin
pageh=PrinterPageHeight()-#Tmargin-#Bmargin
StartDrawing(PrinterOutput())
; !!! DrawingFont(LoadFont(0, "Arial", 10))
linenumw=TextWidth("1999 ") ; Width of line number column
printw=pagew-linenumw ; Width available for printing file text
lineh=TextHeight("bgj_~QX")+#gap
lpp=(pageh+#gap)/lineh ; Lines that will fit on a page
;}
;- Build print lines in a linked list
If ReadFile(#file, path$)=0: End: EndIf ; Quit without error message
While Not Eof(#file)
ln$=RTrim(ReadString(#file)) ; Preserve any whitespace on the left
; If using PureBUILD, try to get the build number
If FindString(LCase(ln$), "purebuild plugin", 1)
p=FindString(LCase(ln$), "purebuild build =", 1)
If p
build$=" Build "+Str(Val(Mid(ln$,p+17,9999)))
EndIf
EndIf
overflow$="#" ; Not a continuation line
linenum+1
; Does the line need to be split because it is too long for the page width?
Repeat
If TextWidth(ln$)<=printw ; If line will fit on page
If linenum>=linefrst And linenum<=linelast
AddElement(lines$()): lines$()=overflow$+ln$
EndIf
ln$=""
Else ; Line too wide for page
cut=Len(ln$) ; Point [just before] where the line will eventually be cut
Repeat ; Large steps
cut-12 ; New trial cut point
Until TextWidth(Left(ln$,cut))<=printw ; Until it will fit
cut+12
Repeat ; Small steps
cut-1 ; New trial cut point
Until TextWidth(Left(ln$,cut))<=printw ; Until it will fit
; Try not to split the last word, except at a hyphen
For p=cut To cut-12 Step -1
If FindString(" -_"+#TAB$, Mid(ln$,p,1), 1) ; If whitespace, hyphen or underscore
cut=p: Break ; Found a better cut point
EndIf
Next p
; Now cut the line, removing trailing whitespace
tmp$=RTrim(Left(ln$, cut))
If overflow$="+" ; If a continuation line
If Len(tmp$) ; Ignore empty continuation lines
If linenum>=linefrst And linenum<=linelast
AddElement(lines$()): lines$()=overflow$+tmp$
EndIf
EndIf
Else ; Not a continuation line
whitespace$=Left(tmp$, Len(tmp$)-Len(LTrim(tmp$))) ; Remember the leading whitespace
If linenum>=linefrst And linenum<=linelast
AddElement(lines$()): lines$()=overflow$+tmp$
EndIf
overflow$="+"
EndIf
ln$=LTrim(Mid(ln$,cut+1,9999)) ; Build the next continuation line
If Len(ln$): ln$=whitespace$+#continue$+ln$: EndIf ; Prepend the same leading whitespace
EndIf
Until Len(ln$)=0
Wend
CloseFile(#file)
;}
;- Print the lines
; Calculate the number of pages to be printed
pages=(ListSize(lines$())-1)/(lpp-2)+1 ; The -2 allows for the title lines on each page
; Calculate the start position of the [nearly] right-aligned page number
pagenumx=#Lmargin+pagew-TextWidth(Str(pages)+" of "+Str(pages))
; Fit the title [and build number] into the width available
title$+build$
titlew=pagenumx-#Lmargin-40
If TextWidth(title$)>titlew ; If too wide
titlew-TextWidth("...")
Repeat
title$=Mid(title$, FindString(title$,"\",2), 999) ; Truncate at left-most "\"
Until TextWidth(title$)<=titlew ; Until it will fit
title$="..."+title$ ; Prepend "..."
EndIf
; Now do the printing
linenum=linefrst-1
ForEach lines$()
If line=0 ; If starting a new page
pagenum+1
DrawText(#Lmargin, y, title$) ; Print title [and build number]
DrawText(pagenumx, y, Str(pagenum)+" of "+Str(pages)) ; Print page number
line=2: y+lineh*2 ; Leave a blank line
EndIf
If Left(lines$(),1)="#" ; If not a continuation line
linenum+1
DrawText(#Lmargin+TextWidth("1999")-TextWidth(Str(linenum)), y, Str(linenum)) ; "Print" linenumber
EndIf
DrawText(#Lmargin+linenumw, y, Mid(lines$(),2,9999)) ; "Print" text to the right of the line number column
line+1: y+lineh
If line>=lpp ; If the current page is full
line=0: y=#Tmargin
NewPrinterPage()
EndIf
Next lines$()
StopDrawing()
StopPrinting()
End
;}
If code folding is not working correctly, take the IDE menupath
File -> Preferences -> Editor -> Folding
Ensure ;- is on the list of Folding Start Keywords.
Ensure ;} is on the list of Folding End Keywords.
If the plugin fails to print anything, the probability is that a bug may exist in these lines of code:
Code: Select all
For p=1 To 2
tmp$ = ProgramParameter(p)
If FindString(LCase(tmp$), "\purebasic_tempfile", 1) ; If a temporary filename
path$=tmp$
ElseIf Len(tmp$)
title$=tmp$
EndIf
Next p
If path$="": End: EndIf ; Quit without error message
If FileSize(path$)<=0: : End: EndIf ; Quit without error message