Introduction
Amacroisagroupofcommandstogethertocompleteaspecifictaskasasinglecommand.ThedefinitionofmacroinMicrosoftWordis:"Macroisaseriesofwordcommandsthatcanbeorganizedtogetherandusedasanindependentcommand,whichcanmakedailyworkeasier."WordusesthemacrolanguageVisualBasictowritemacrosasaseriesofinstructions.
Macrosincomputerscienceareabstract,replacingcertaintextpatternsaccordingtoaseriesofpredefinedrules.TheExcelofficesoftwareautomaticallyintegratesthe"VBA"high-levelprogramminglanguage,andtheprogramcompiledinthislanguageiscalled"macro".Theuseof"VBA"requiresacertainprogrammingfoundationandalsoconsumesalotoftime.Therefore,thevastmajorityofusersonlyusethegeneraltabulationfunctionofExcelandrarelyuse"VBA".
Theinterpreterorcompilerwillautomaticallyperformthispatternreplacementwhenitencountersamacro.Forcompiledlanguages,macroexpansionoccursatcompiletime,andthetoolformacroexpansionisoftencalledamacroexpander.Thetermmacroisalsooftenusedinmanysimilarenvironments.Theyarederivedfromtheconceptofmacroexpansion,whichincludeskeyboardmacrosandmacrolanguages.Inmostcases,theuseoftheword"macro"impliestheconversionofsmallcommandsoractionsintoaseriesofinstructions.
Thepurposeofmacrosistoautomatefrequentlyusedsequencesortoobtainamorepowerfulabstractionability.
ComputerlanguagessuchasCorassemblylanguagehavesimplemacrosystems,whichareimplementedbythepreprocessorofacompilerorassembler.ThejobofC'smacropreprocessorissimpletextsearchandreplacement.UsingadditionaltextprocessinglanguagessuchasM4,Cprogrammerscangetmoresophisticatedmacros.
Lisp-likelanguagessuchasCommonLispandSchemehavemoresophisticatedmacrosystems:Macrosbehavelikeafunction'stransformationoftheirownprogramtext,andalllanguagescanbeusedtoexpressthistransformation.ACmacrocandefinethesubstitutionofasectionofgrammar,butaLispmacrocancontrolthecalculationofasectionofcode.Gainedtheabilitytocontroltheexecutionorderofcode(seelazycalculationandunrestrictedfunctions),makingthenewlycreatedgrammaticalstructureindistinguishablefromthebuilt-ingrammaticalstructureofthelanguage.Forexample,ifaLispdialecthascondbutnoif,youcanusemacrostodefinethelatter.ThemainextensionsofLispsyntax,suchastheobject-orientedCLOSsystem,canbedefinedbymacros.
Typicalapplicationsofmacros
Macroscanspeedupdailyeditingandformatting.Combinemultiplecommandstomaketheoptionsinthedialogboxeasiertoaccess.Makeaseriesofcomplextasksautomaticallyexecuted
Applicationscanalsouseasystemwithamechanismsimilartomacrostoallowuserstocustomizeaseriesof(usuallythemostcommonlyusedoperations)asastep.Thatis,theuserperformsaseriesofoperationsandallowstheapplicationto"remember"theseoperationsandthesequence.Moreadvanceduserscandirectlyusethefunctionsofthoseapplicationsthroughthebuilt-inmacroprogramming.Whenusinganunfamiliarmacrolanguagetoprogram,amoreeffectivemethodistorecordaseriesofoperationsthattheuserhopestoget,andthenunderstandthestructureofthemacrocommandbyreadingthemacrofilerecordedbytheapplication.
Macroprogramming
Whenusinganunfamiliarmacrolanguageformacroprogramming,youcandothis.Firstrecordwhattheuserwantsthemacrotoaccomplish,thenopenthemacrofileandtryUnderstandhowthecommandstructureworks.Youcanalsomodifythecommandtoadjustthemacro.Somemacrolanguages,suchastheDexterityruntimeengineofGreatPlainsaccountingsoftware,cannotimportdatafromotherdatasources(suchastextfilesseparatedbycommas).Thislimitationcanbesolvedbyusingamorepowerfulprogramminglanguage,suchasVBA,tocreateacomputerprogramtogenerateaspecialmacrointhisweakprogramminglanguage.Forexample,youcanprogramMicrosoftExcelmacrostoreaddatafromanextendedstylesheetortextfileandcreateaGreatPlains.macfile.ThisfileisusedtoimportspecificdataintoGreatPlains.ItneedstobegeneratedforeachnewdatasetNew.macfile.
Keyboardmacro
Keyboardmacroandeditormacroareusedinteractivelyinthegraphicaluserinterfaceandeditorrespectively.Theycanbeusedtoreplacelengthycommandsequenceswithshortkeystrokesandprovideasimpleformofautomationforrepetitivetasks,butthishaschangedin2009.
Theprogrammer'stexteditorEmacs(shortfor"EditingMACroS")isaproductofthisidea.Infact,mosteditorsarecomposedofmacros.EmacswasoriginallydesignedasamacrosetoftheeditinglanguageTECO,andlaterportedtoEmacsLisp,adialectofLisp.Vimalsoallowsuserstorecordandplaysimplekeyboardmacros.
Macrolanguage
Macrolanguageisatypeofprogramminglanguage,allormostofitscalculationsarecompletedbyexpansionmacros.Macrolanguageisnotwidelyusedingeneralprogramming,butitiswidelyusedintextprocessingprograms.Forexample,CpreprocessorCpreprocessorInternetMacros(iOpus)M4(asmentionedabove,originatedfromAT&T,bundledwithUnix)
Macrodefinition
providedbythecprogramOneofthepreprocessingfunctions.Includingmacrodefinitionswithparametersandmacrodefinitionswithoutparameters.Specifically,itreferstoasimplestringreplacementorexplanatoryreplacementwithaspecifiedidentifier.Theformis:
#defineflag[(parametertable)]string
macroname
inthedefinitionaboveTheidentifieriscalleda"macroname".
Macroexpansion
Theprocessofreplacingthemacronamewithacharacterstringwhenthecprogramiscompilediscalled"macroexpansion".
Macrolanguageisakindofprogramminglanguage,allormostofitscalculationsarecompletedbyexpansionmacros.Macrolanguageisnotwidelyusedingeneralprogramming,butitiswidelyusedintextprocessingprograms.Forexample,
CpreprocessorCpreprocessor
InternetMacros(iOpus)
M4(asmentionedearlier,originatedfromAT&T,bundledwithUnix)
MicrosoftWordandmacroviruses
VisualBasicforApplications(VBA),isaprogramminglanguageinMicrosoftOffice.Butfromtheabovedefinition,itisnotamacrolanguageatall.However,itsfunctionhasevolvedfromitandeventuallyreplacedthemacroideaofuserapplications,soitiswidelyerroneouslycalledamacrolanguage.
VBAcanaccessmanyoperatingsystemfunctionsandsupportsautomaticmacroexecutionwhenthedocumentisopened.Thismakesitpossibletowritecomputervirusesinthislanguage.Inthemidtolate1990s,macrovirusesbecameoneofthemostpopulartypesofcomputerviruses.Otherprojectsthatincludemacrolanguages,suchasOpenOffice.org,deliberatelyexcludesomefunctions(suchasautomaticexecution)fromtheirmacrolanguagestoavoidprogramdamage.However,thisfeatureispopularinmanytransactions.