Extend the compiler with resident Macros

Share your advanced PureBasic knowledge/code with the community.
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Extend the compiler with resident Macros

Post by Blue »

Sorry for the so-so topic title, but i'm not quite sure how to word what i want.

To make it simple, let me break it down in steps:
(1) i have a macro MsgBox() that i just can't live without... (yes, yes, i know; just take it as an example!)
(2) I use it in all my PB source code files and I would like to be able to call upon it without having to include it specifically in all source files.
(3) at present, I have the macro spelled out as as a template, which I can easily import in the source file when I remember to click on it.

Surely there is a way to call upon it as any other PB function or constant, without having to explicitly (and manually!) include it in all source files.

I'm under the impression that something like this is possible by using the \RESIDENTS folder in the PB setup, but i have no clue how to go about doing it practically. I remember coming acrosss a posting by NetMaestro alluding to such a possibility, but I haven't been able to find it again. Plus i didn't want to hijack the topic with my unrelated question.

Any suggestion, anyone ?

BTW: is there any documentation anywhere that explains in details the purpose of the many subfolders in the PB setup ? I've come across many tidbits here and there, but not any centralized explanation. I think that could be very useful to many PB users.
Last edited by Blue on Mon Jan 05, 2009 10:27 am, edited 3 times in total.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

Yes, create a PB source file with just the macro in it and save it. Then use the command line to invoke the PB compiler with the RESIDENT switch to create a resident file (alternatively Tailbite can create a resident file for you). Place this resident file in the \Residents subfolder of your PB installation and then restart the IDE / compiler. At this point you can use your macro without any additional work.
I may look like a mule, but I'm not a complete ass.
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post by Blue »

srod wrote:Yes [...] you can use your macro without any additional work.
WOW!!! So cool and so very easy to do. :shock:
Thanks, Srod.
I followed your suggestion and it works really well
:lol:

I'm extremely impresssed by how easy PB makes it for the user to customize and extend the development environment.
It's amazingly smart and simple.

Too bad the documentation isn't more explicit on this and some other good things.
i think that such rich features should be easier to discover, understand and apply.
Clearly, the present documentation's one-liner on /RESIDENT is a bit short on the purpose and fabulous usefulness of this parameter and its name-sake subfolder... :roll:
Last edited by Blue on Sat Jan 03, 2009 9:28 am, edited 1 time in total.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
Rings
Moderator
Moderator
Posts: 1435
Joined: Sat Apr 26, 2003 1:11 am

Post by Rings »

just a example,

Code: Select all

Macro MsgBox(a)
  MessageRequester("Info",a,0)
EndMacro
save the file in yourdirectory(named "Msgbox_Macro.pb" )
open a shell,
type in:
pbcompiler yourdirectory/msgbox_macro.pb /resident ../Residents/msgbox.res
the restart the compiler from the IDE.

if you want to overwrite the Resident file, delete it first from the Residents directory
SPAMINATOR NR.1
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post by Blue »

Thanks for the input, Rings.
That's pretty close to what I actually did following srod's instructions and, indeed, it works perfectly.

But since you seem in the know as to the raison d'être of the [Residents] sub-folder, maybe you know of other good but lesser known purposes for this folder or for the similarly named switch ?

--------------------------------------------------
BTW:
I just noticed the Moderator title following your name.
So, maybe you have the ability to make the information in this thread stand out as a sticky subject at the top of the section (Sorry: I just can't remember the word for this action... :oops:).
I'm convinced many users would greatly benefit from learning and making use of what is being offered here by you and Srod.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
SFSxOI
Addict
Addict
Posts: 2970
Joined: Sat Dec 31, 2005 5:24 pm
Location: Where ya would never look.....

Post by SFSxOI »

I never knew how to do this, or that I even wanted to do this, until now. I've just been including a utilities.pbi with my favorite things in it in my stuff. Now i'll do some residents.

Thanks all for about the best tip I got today. :)
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post by Blue »

SFSxOI wrote:I never knew [...] that I even wanted to do this [...]
:lol: :lol: :lol:
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Seldon
Enthusiast
Enthusiast
Posts: 405
Joined: Fri Aug 22, 2003 7:12 am
Location: Italia

Post by Seldon »

Me too. :D Maybe it's a silly question, but which is the difference between a RES and a LIB file ? Can a LIB be made only in C or assembly ? And can I put just constants and macros in a RES file or functions too ? Thanks.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

A residents file holds constants, structure definitions, prototypes,... only, i.e. no code.

Must admit that I no longer use these or user-libs; sticking firmly with source code includes only.
I may look like a mule, but I'm not a complete ass.
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post by Blue »

srod wrote:[...]Must admit that I no longer use these or user-libs; sticking firmly with source code includes only.
Yeah. Now that I've played with my new toy, I can see at least one reason to abstain from using it: if you want to share your code, you MUST remember to translate all your clever obscure hacks :!: back into regular universal PB.

Is there a smart(er) way to automate the inclusion of a piece of code into new source files?
I've been using a one-line template (which works OK), but i'm thinking there must be a better way.

I'm beginning to think i should have opened this topic in the "PB Editor" section.
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post by Trond »

Maybe it's possible to create a tool that creates a new file with some predefined contents (instead of the default empty one)?
User avatar
Blue
Addict
Addict
Posts: 967
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

Post by Blue »

Trond wrote:Maybe it's possible to create a tool that creates a new file with some predefined contents (instead of the default empty one)?
Right on, just as in MS Visual Basic (where you can even choose among a number of different template types!).
I have made, for myself, a tool that does what you suggest (creates a skeleton file, in a related folder, then opens the file in the IDE) ; it works, but its value is limited by the fact that it's not tied to the "New" command in the IDE.

Being able to do that would make the tool as practical (at least i think...) as the process used in VB. But i have not the slightest idea how to make the connection to that IDE command :cry:

Any idea on that ?
PB Forums : Proof positive that 2 heads (or more...) are better than one :idea:
User avatar
graves
Enthusiast
Enthusiast
Posts: 160
Joined: Wed Oct 03, 2007 2:38 pm
Location: To the deal with a pepper

Post by graves »

Hi,
I've found a trick to include procedures on a Resident file, and use it along all your programs.
The trick: include it on a macro.

1. Create a source file with this macro:

Code: Select all

macro TestProc
Procedure Test_Proc()
  pintn("Hello from proc")
EndProcedure
endmacro
2. Compile it as Resident file:

Code: Select all

pbcompiler path\file.pb /RESIDENT .\Residents\testproc.res
3. Create a new source with this little program, compile and execute:

Code: Select all

TestProc   ; to include the procedure in your program
OpenConsole()
Test_Proc()       ; to execute the procedure
input()
CloseConsole()
END
Hurga
Enthusiast
Enthusiast
Posts: 148
Joined: Thu Jul 17, 2003 2:53 pm
Contact:

Post by Hurga »

In jaPBe there is an possibility to include some file (more or less) automaically.

I suggested gnozal to make this option a bit more flexible so that I can just add a file to the specific folder and jaPBe registrate it so I can use it.
This would make it a bit easier and an lot more easy to change the "include"-files or add some code to them.
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Post by akj »

Following from what Blue says, for external tools, I would also very much welcome an additional trigger: 'New sourcecode created'.

After all, there is already a trigger for the opposite action of 'Sourcecode closed'.
Anthony Jordan
Post Reply