artist.el 190 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609
  1. ;;; artist.el --- draw ascii graphics with your mouse
  2. ;; Copyright (C) 2000-2012 Free Software Foundation, Inc.
  3. ;; Author: Tomas Abrahamsson <tab@lysator.liu.se>
  4. ;; Maintainer: Tomas Abrahamsson <tab@lysator.liu.se>
  5. ;; Keywords: mouse
  6. ;; Version: 1.2.6
  7. ;; Release-date: 6-Aug-2004
  8. ;; Location: http://www.lysator.liu.se/~tab/artist/
  9. ;; Yoni Rabkin <yoni@rabkins.net> contacted the maintainer of this
  10. ;; file on 19/3/2008, and the maintainer agreed that when a bug is filed in
  11. ;; the Emacs bug reporting system against this file, a copy of the bug
  12. ;; report be sent to the maintainer's email address.
  13. ;; This file is part of GNU Emacs.
  14. ;; GNU Emacs is free software: you can redistribute it and/or modify
  15. ;; it under the terms of the GNU General Public License as published by
  16. ;; the Free Software Foundation, either version 3 of the License, or
  17. ;; (at your option) any later version.
  18. ;; GNU Emacs is distributed in the hope that it will be useful,
  19. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. ;; GNU General Public License for more details.
  22. ;; You should have received a copy of the GNU General Public License
  23. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  24. ;;; Commentary:
  25. ;; What is artist?
  26. ;; ---------------
  27. ;;
  28. ;; Artist is an Emacs lisp package that allows you to draw lines,
  29. ;; rectangles and ellipses by using your mouse and/or keyboard. The
  30. ;; shapes are made up with the ascii characters |, -, / and \.
  31. ;;
  32. ;; Features are:
  33. ;;
  34. ;; * Intersecting: When a `|' intersects with a `-', a `+' is
  35. ;; drawn, like this: | \ /
  36. ;; --+-- X
  37. ;; | / \
  38. ;;
  39. ;; * Rubber-banding: When drawing lines you can interactively see the
  40. ;; result while holding the mouse button down and moving the mouse. If
  41. ;; your machine is not fast enough (a 386 is a bit to slow, but a
  42. ;; pentium is well enough), you can turn this feature off. You will
  43. ;; then see 1's and 2's which mark the 1st and 2nd endpoint of the line
  44. ;; you are drawing.
  45. ;;
  46. ;; * Drawing operations: The following drawing operations are implemented:
  47. ;;
  48. ;; lines straight-lines
  49. ;; rectangles squares
  50. ;; poly-lines straight poly-lines
  51. ;; ellipses circles
  52. ;; text (see-thru) text (overwrite)
  53. ;; spray-can setting size for spraying
  54. ;; vaporize line vaporize lines
  55. ;; erase characters erase rectangles
  56. ;;
  57. ;; Straight lines are lines that go horizontally, vertically or
  58. ;; diagonally. Plain lines go in any direction. The operations in
  59. ;; the right column are accessed by holding down the shift key while
  60. ;; drawing.
  61. ;;
  62. ;; It is possible to vaporize (erase) entire lines and connected lines
  63. ;; (rectangles for example) as long as the lines being vaporized are
  64. ;; straight and connected at their endpoints. Vaporizing is inspired
  65. ;; by the drawrect package by Jari Aalto <jari.aalto@poboxes.com>.
  66. ;;
  67. ;; * Flood-filling: You can fill any area with a certain character by
  68. ;; flood-filling.
  69. ;;
  70. ;; * Cut copy and paste: You can cut, copy and paste rectangular
  71. ;; regions. Artist also interfaces with the rect package (this can be
  72. ;; turned off if it causes you any trouble) so anything you cut in
  73. ;; artist can be yanked with C-x r y and vice versa.
  74. ;;
  75. ;; * Drawing with keys: Everything you can do with the mouse, you can
  76. ;; also do without the mouse.
  77. ;;
  78. ;; * Arrows: After having drawn a (straight) line or a (straight)
  79. ;; poly-line, you can set arrows on the line-ends by typing < or >.
  80. ;;
  81. ;; * Aspect-ratio: You can set the variable artist-aspect-ratio to
  82. ;; reflect the height-width ratio for the font you are using. Squares
  83. ;; and circles are then drawn square/round. Note, that once your
  84. ;; ascii-file is shown with font with a different height-width ratio,
  85. ;; the squares won't be square and the circles won't be round.
  86. ;;
  87. ;; * Picture mode compatibility: Artist is picture mode compatible (this
  88. ;; can be turned off).
  89. ;;
  90. ;; See the documentation for the function artist-mode for a detailed
  91. ;; description on how to use artist.
  92. ;;
  93. ;;
  94. ;; What about adding my own drawing modes?
  95. ;; ---------------------------------------
  96. ;;
  97. ;; See the short guide at the end of this file.
  98. ;; If you add a new drawing mode, send it to me, and I would gladly
  99. ;; include in the next release!
  100. ;;; Installation:
  101. ;; To use artist, put this in your .emacs:
  102. ;;
  103. ;; (autoload 'artist-mode "artist" "Enter artist-mode" t)
  104. ;;; Requirements:
  105. ;; Artist requires Emacs 19.28 or higher.
  106. ;;
  107. ;; Artist requires the `rect' package (which comes with Emacs) to be
  108. ;; loadable, unless the variable `artist-interface-with-rect' is set
  109. ;; to nil.
  110. ;;
  111. ;; Artist also requires the Picture mode (which also comes with Emacs)
  112. ;; to be loadable, unless the variable `artist-picture-compatibility'
  113. ;; is set to nil.
  114. ;;; Known bugs:
  115. ;; The shifted operations are not available when drawing with the mouse
  116. ;; in Emacs 19.29 and 19.30.
  117. ;;
  118. ;; It is not possible to change between shifted and unshifted operation
  119. ;; while drawing with the mouse. (See the comment in the function
  120. ;; artist-shift-has-changed for further details.)
  121. ;;; ChangeLog:
  122. ;; 1.2.6 6-Aug-2004
  123. ;; New: Coerced with the artist.el that's in Emacs-21.3.
  124. ;; (minor editorial changes)
  125. ;;
  126. ;; 1.2.5 4-Aug-2004
  127. ;; New: Added tool selection via the mouse-wheel
  128. ;; Function provided by Andreas Leue <al@sphenon.de>
  129. ;;
  130. ;; 1.2.4 25-Oct-2001
  131. ;; Bugfix: Some operations (the edit menu) got hidden
  132. ;; Bugfix: The first arrow for poly-lines was always pointing
  133. ;; to the right
  134. ;; Changed: Updated with changes made for Emacs 21.1
  135. ;;
  136. ;; 1.2.3 20-Nov-2000
  137. ;; Bugfix: Autoload cookie corrected
  138. ;;
  139. ;; 1.2.2 19-Nov-2000
  140. ;; Changed: More documentation fixes.
  141. ;; Bugfix: The arrow characters (`artist-arrows'), which
  142. ;; got wrong in 1.1, are now corrected.
  143. ;;
  144. ;; 1.2.1 15-Nov-2000
  145. ;; New: Documentation fixes.
  146. ;; Bugfix: Sets next-line-add-newlines to t while in artist-mode.
  147. ;; Drawing with keys was confusing without this fix, if
  148. ;; next-line-add-newlines was set to nil.
  149. ;; Thanks to Tatsuo Furukawa <tatsuo@kobe.hp.com> for this.
  150. ;;
  151. ;; 1.2 22-Oct-2000
  152. ;; New: Updated to work with Emacs 21
  153. ;;
  154. ;; 1.1 15-Aug-2000
  155. ;; Bugfix: Cursor follows mouse pointer more closely.
  156. ;; New: Works with Emacs 20.x
  157. ;; New: Variables are customizable
  158. ;;
  159. ;; 1.1-beta1 21-Apr-1998
  160. ;; New: Spray-can (Utterly useless, I believe, but it was fun
  161. ;; to implement :-) after an idea by Karl-Johan Karlsson
  162. ;; <kj@lysator.liu.se>.
  163. ;; New: Freehand drawing (with pen).
  164. ;; New: Vaporizing lines.
  165. ;; New: Text-rendering using figlet.
  166. ;; New: Picture mode compatibility.
  167. ;; Changed: All Artist keys now uses the prefix C-c C-a not to conflict
  168. ;; with Picture mode.
  169. ;; Bugfix: No longer leaves traces of lines when rubberbanding
  170. ;; if the buffer auto-scrolls.
  171. ;; Bugfix: Infinite loop sometimes when rubberbanding was turned
  172. ;; off.
  173. ;;
  174. ;; 1.0 01-Mar-1998
  175. ;; First official release.
  176. ;;; Code:
  177. ;; Variables
  178. (defconst artist-version "1.2.6")
  179. (defconst artist-maintainer-address "tab@lysator.liu.se")
  180. (defvar x-pointer-crosshair)
  181. ;; User options
  182. ;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
  183. (defgroup artist nil
  184. "Customization of the Artist mode."
  185. :group 'mouse)
  186. (defgroup artist-text nil
  187. "Customization of the text rendering."
  188. :group 'artist)
  189. (defcustom artist-rubber-banding t
  190. "Interactively do rubber-banding when non-nil."
  191. :group 'artist
  192. :type 'boolean)
  193. (defcustom artist-first-char ?1
  194. "Character to set at first point when not rubber-banding."
  195. :group 'artist
  196. :type 'character)
  197. (defcustom artist-second-char ?2
  198. "Character to set at second point when not rubber-banding."
  199. :group 'artist
  200. :type 'character)
  201. (defcustom artist-interface-with-rect t
  202. "Whether to interface with the rect package or not.
  203. Interfacing to the rect package means that the Copy and Paste operations
  204. will use the rectangle buffer when accessing the copied area. This means
  205. that you can insert a rectangle which is copied using the artist package
  206. and vice versa.
  207. If this causes any problem for you (for example, if the implementation of
  208. the rectangle package changes), you can set this variable to nil, and the
  209. artist package will use its own copy buffer."
  210. :group 'artist
  211. :type 'boolean)
  212. (defvar artist-arrows [ ?> nil ?v ?L ?< nil ?^ nil ]
  213. ;; This is a defvar, not a defcustom, since the custom
  214. ;; package shows vectors of characters as a vector of integers,
  215. ;; which is confusing
  216. "A vector of characters to use as arrows.
  217. The vector is 8 elements long and contains a character for each
  218. direction, or nil if there is no suitable character to use for arrow
  219. in that direction.
  220. The directions are as follows:
  221. 5 6 7
  222. \\ | /
  223. 4 - * - 0
  224. / | \\
  225. 3 2 1")
  226. (defcustom artist-aspect-ratio 1
  227. "Defines the character height-to-width aspect ratio.
  228. This is used when drawing squares and circles."
  229. :group 'artist
  230. :type 'number)
  231. (defcustom artist-trim-line-endings t
  232. "Whether or not to remove white-space at end of lines.
  233. If non-nil, line-endings are trimmed (that is, extraneous white-space
  234. at the end of the line is removed) when the shape is drawn."
  235. :group 'artist
  236. :type 'boolean)
  237. (defcustom artist-flood-fill-right-border 'window-width
  238. "Right edge definition, used when flood-filling.
  239. When flood-filling, if the area is not closed off to the right, then
  240. flood-filling will fill no more to the right than specified by this
  241. variable. This limit is called the fill-border."
  242. :group 'artist
  243. :type '(choice (const :tag "limited to window" window-width)
  244. (const :tag "limited to value of `fill-column'" fill-column)))
  245. (defcustom artist-flood-fill-show-incrementally t
  246. "Whether or not to incrementally update display when flood-filling.
  247. If non-nil, incrementally update display when flood-filling.
  248. If set to non-nil, this currently implies discarding any input events
  249. during the flood-fill."
  250. :group 'artist
  251. :type 'boolean)
  252. (defcustom artist-ellipse-right-char ?\)
  253. "Character to use at the rightmost position when drawing narrow ellipses.
  254. In this figure, it is the right parenthesis (the ``)'' character):
  255. -----
  256. ( )
  257. -----"
  258. :group 'artist
  259. :type 'character)
  260. (defcustom artist-ellipse-left-char ?\(
  261. "Character to use at the leftmost position when drawing narrow ellipses.
  262. In this figure, it is the left parenthesis (the ``('' character):
  263. -----
  264. ( )
  265. -----"
  266. :group 'artist
  267. :type 'character)
  268. (defcustom artist-picture-compatibility t
  269. "Whether or not picture mode compatibility is on."
  270. :group 'artist
  271. :type 'boolean)
  272. (defcustom artist-vaporize-fuzziness 1
  273. "How to vaporize lines that are cut off.
  274. Accept this many characters cutting off a line and still treat
  275. it as one line.
  276. Example:
  277. If `artist-vaporize-fuzziness' is 2, then those will be recognized as
  278. lines from A to B (provided you start vaporizing them at the ``*''):
  279. /
  280. A----*------/-----------B
  281. \\/
  282. A----*----/\\------------B
  283. / \\
  284. but this one won't, since it is cut off by more than 2 characters:
  285. \\/ /
  286. A----*----/\\/----------B
  287. / /\\
  288. (in fact, only the left part [between the A and the leftmost ``/''
  289. crossing the line] will be vaporized)."
  290. :group 'artist
  291. :type 'integer)
  292. (defvar artist-pointer-shape (if (eq window-system 'x) x-pointer-crosshair nil)
  293. "*If in X Windows, use this pointer shape while drawing with the mouse.")
  294. (defcustom artist-text-renderer-function 'artist-figlet
  295. "Function for doing text rendering."
  296. :group 'artist-text
  297. :type 'symbol)
  298. (defvaralias 'artist-text-renderer 'artist-text-renderer-function)
  299. (defcustom artist-figlet-program "figlet"
  300. "Program to run for `figlet'."
  301. :group 'artist-text
  302. :type 'string)
  303. (defcustom artist-figlet-default-font "standard"
  304. "Default font for `figlet'."
  305. :group 'artist-text
  306. :type 'string)
  307. (defcustom artist-figlet-list-fonts-command
  308. ;; list files ending with *.flf in any directory printed by the
  309. ;; ``figlet -I2'' command. I think this will not produce more than
  310. ;; one directory, but it never hurts to be on the safe side...
  311. "for dir in `figlet -I2`; do cd $dir; ls *.flf; done"
  312. "Command to run to get list of available fonts."
  313. :group 'artist-text
  314. :type 'string)
  315. (defcustom artist-spray-interval 0.2
  316. "Number of seconds between repeated spraying."
  317. :group 'artist
  318. :type 'number)
  319. (defcustom artist-spray-radius 4
  320. "Size of the area for spraying."
  321. :group 'artist
  322. :type 'integer)
  323. (defvar artist-spray-chars '(?\s ?. ?- ?+ ?m ?% ?* ?#)
  324. ;; This is a defvar, not a defcustom, since the custom
  325. ;; package shows lists of characters as a lists of integers,
  326. ;; which is confusing
  327. "*Characters (``color'') to use when spraying.
  328. They should be ordered from the ``lightest'' to the ``heaviest''
  329. since spraying replaces a light character with the next heavier one.")
  330. (defvar artist-spray-new-char ?.
  331. "*Initial character to use when spraying.
  332. This character is used if spraying upon a character that is not in
  333. `artist-spray-chars'. The character defined by this variable should
  334. be in `artist-spray-chars', or spraying will behave strangely.")
  335. ;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  336. ;; End of user options
  337. ;; Internal variables
  338. ;;
  339. (defvar artist-mode nil
  340. "Non-nil to enable `artist-mode' and nil to disable.")
  341. (make-variable-buffer-local 'artist-mode)
  342. (defvar artist-mode-name " Artist"
  343. "Name of Artist mode beginning with a space (appears in the mode-line).")
  344. (defvar artist-curr-go 'pen-line
  345. "Current selected graphics operation.")
  346. (make-variable-buffer-local 'artist-curr-go)
  347. (defvar artist-line-char-set nil
  348. "Boolean to tell whether user has set some char to use when drawing lines.")
  349. (make-variable-buffer-local 'artist-line-char-set)
  350. (defvar artist-line-char nil
  351. "Char to use when drawing lines.")
  352. (make-variable-buffer-local 'artist-line-char)
  353. (defvar artist-fill-char-set nil
  354. "Boolean to tell whether user has set some char to use when filling.")
  355. (make-variable-buffer-local 'artist-fill-char-set)
  356. (defvar artist-fill-char nil
  357. "Char to use when filling.")
  358. (make-variable-buffer-local 'artist-fill-char)
  359. (defvar artist-erase-char ?\s
  360. "Char to use when erasing.")
  361. (make-variable-buffer-local 'artist-erase-char)
  362. (defvar artist-default-fill-char ?.
  363. "Char to use when a fill-char is required but none is set.")
  364. (make-variable-buffer-local 'artist-default-fill-char)
  365. ; This variable is not buffer local
  366. (defvar artist-copy-buffer nil
  367. "Copy buffer.")
  368. (defvar artist-draw-region-min-y 0
  369. "Line-number for top-most visited line for draw operation.")
  370. (make-variable-buffer-local 'artist-draw-region-min-y)
  371. (defvar artist-draw-region-max-y 0
  372. "Line-number for bottom-most visited line for draw operation.")
  373. (make-variable-buffer-local 'artist-draw-region-max-y)
  374. (defvar artist-borderless-shapes nil
  375. "When non-nil, draw shapes without border.
  376. The fill char is used instead, if it is set.")
  377. (make-variable-buffer-local 'artist-borderless-shapes)
  378. (defvar artist-prev-next-op-alist nil
  379. "Assoc list for looking up next and/or previous draw operation.
  380. The structure is as follows: (OP . (PREV-OP . NEXT-OP))
  381. where the elements are as follows:
  382. * OP is an atom: the KEY-SYMBOL in the `artist-mt' structure
  383. * PREV-OP and NEXT-OP are strings: the KEYWORD in the `artist-mt' structure
  384. This variable is initialized by the `artist-make-prev-next-op-alist' function.")
  385. (eval-when-compile
  386. ;; Make rect available at compile-time
  387. (require 'rect) ; for interfacing with rect
  388. (require 'reporter) ; the bug-reporting tool
  389. (require 'picture)) ; picture mode compatibility
  390. (if artist-interface-with-rect
  391. (require 'rect))
  392. (require 'reporter)
  393. (if artist-picture-compatibility
  394. (require 'picture))
  395. ;; Variables that are made local in artist-mode-init
  396. (defvar artist-key-is-drawing nil)
  397. (defvar artist-key-endpoint1 nil)
  398. (defvar artist-key-poly-point-list nil)
  399. (defvar artist-key-shape nil)
  400. (defvar artist-key-draw-how nil)
  401. (defvar artist-popup-menu-table nil)
  402. (defvar artist-key-compl-table nil)
  403. (defvar artist-rb-save-data nil)
  404. (defvar artist-arrow-point-1 nil)
  405. (defvar artist-arrow-point-2 nil)
  406. (defvar artist-menu-map
  407. (let ((map (make-sparse-keymap)))
  408. (define-key map [spray-chars]
  409. '(menu-item "Characters for Spray" artist-select-spray-chars
  410. :help "Choose characters for sprayed by the spray-can"))
  411. (define-key map [borders]
  412. '(menu-item "Draw Shape Borders" artist-toggle-borderless-shapes
  413. :help "Toggle whether shapes are drawn with borders"
  414. :button (:toggle . (not artist-borderless-shapes))))
  415. (define-key map [trimming]
  416. '(menu-item "Trim Line Endings" artist-toggle-trim-line-endings
  417. :help "Toggle trimming of line-endings"
  418. :button (:toggle . artist-trim-line-endings)))
  419. (define-key map [rubber-band]
  420. '(menu-item "Rubber-banding" artist-toggle-rubber-banding
  421. :help "Toggle rubber-banding"
  422. :button (:toggle . artist-rubber-banding)))
  423. (define-key map [set-erase]
  424. '(menu-item "Character to Erase..." artist-select-erase-char
  425. :help "Choose a specific character to erase"))
  426. (define-key map [set-line]
  427. '(menu-item "Character for Line..." artist-select-line-char
  428. :help "Choose the character to insert when drawing lines"))
  429. (define-key map [set-fill]
  430. '(menu-item "Character for Fill..." artist-select-fill-char
  431. :help "Choose the character to insert when filling in shapes"))
  432. (define-key map [artist-separator] '(menu-item "--"))
  433. (dolist (op '(("Vaporize" artist-select-op-vaporize-lines vaporize-lines)
  434. ("Erase" artist-select-op-erase-rectangle erase-rect)
  435. ("Spray-can" artist-select-op-spray-set-size spray-get-size)
  436. ("Text" artist-select-op-text-overwrite text-ovwrt)
  437. ("Ellipse" artist-select-op-circle circle)
  438. ("Poly-line" artist-select-op-straight-poly-line spolyline)
  439. ("Square" artist-select-op-square square)
  440. ("Rectangle" artist-select-op-rectangle rectangle)
  441. ("Line" artist-select-op-straight-line s-line)
  442. ("Pen" artist-select-op-pen-line pen-line)))
  443. (define-key map (vector (nth 2 op))
  444. `(menu-item ,(nth 0 op)
  445. ,(nth 1 op)
  446. :help ,(format "Draw using the %s style" (nth 0 op))
  447. :button (:radio . (eq artist-curr-go ',(nth 2 op))))))
  448. map))
  449. (defvar artist-mode-map
  450. (let ((map (make-sparse-keymap)))
  451. (setq artist-mode-map (make-sparse-keymap))
  452. (define-key map [down-mouse-1] 'artist-down-mouse-1)
  453. (define-key map [S-down-mouse-1] 'artist-down-mouse-1)
  454. (define-key map [down-mouse-2] 'artist-mouse-choose-operation)
  455. (define-key map [S-down-mouse-2] 'artist-mouse-choose-operation)
  456. (define-key map [down-mouse-3] 'artist-down-mouse-3)
  457. (define-key map [S-down-mouse-3] 'artist-down-mouse-3)
  458. (define-key map [C-mouse-4] 'artist-select-prev-op-in-list)
  459. (define-key map [C-mouse-5] 'artist-select-next-op-in-list)
  460. (define-key map "\r" 'artist-key-set-point) ; return
  461. (define-key map [up] 'artist-previous-line)
  462. (define-key map "\C-p" 'artist-previous-line)
  463. (define-key map [down] 'artist-next-line)
  464. (define-key map "\C-n" 'artist-next-line)
  465. (define-key map [left] 'artist-backward-char)
  466. (define-key map "\C-b" 'artist-backward-char)
  467. (define-key map [right] 'artist-forward-char)
  468. (define-key map "\C-f" 'artist-forward-char)
  469. (define-key map "<" 'artist-toggle-first-arrow)
  470. (define-key map ">" 'artist-toggle-second-arrow)
  471. (define-key map "\C-c\C-a\C-e" 'artist-select-erase-char)
  472. (define-key map "\C-c\C-a\C-f" 'artist-select-fill-char)
  473. (define-key map "\C-c\C-a\C-l" 'artist-select-line-char)
  474. (define-key map "\C-c\C-a\C-o" 'artist-select-operation)
  475. (define-key map "\C-c\C-a\C-r" 'artist-toggle-rubber-banding)
  476. (define-key map "\C-c\C-a\C-t" 'artist-toggle-trim-line-endings)
  477. (define-key map "\C-c\C-a\C-s" 'artist-toggle-borderless-shapes)
  478. (define-key map "\C-c\C-c" 'artist-mode-off)
  479. (define-key map "\C-c\C-al" 'artist-select-op-line)
  480. (define-key map "\C-c\C-aL" 'artist-select-op-straight-line)
  481. (define-key map "\C-c\C-ar" 'artist-select-op-rectangle)
  482. (define-key map "\C-c\C-aR" 'artist-select-op-square)
  483. (define-key map "\C-c\C-as" 'artist-select-op-square)
  484. (define-key map "\C-c\C-ap" 'artist-select-op-poly-line)
  485. (define-key map "\C-c\C-aP" 'artist-select-op-straight-poly-line)
  486. (define-key map "\C-c\C-ae" 'artist-select-op-ellipse)
  487. (define-key map "\C-c\C-ac" 'artist-select-op-circle)
  488. (define-key map "\C-c\C-at" 'artist-select-op-text-see-thru)
  489. (define-key map "\C-c\C-aT" 'artist-select-op-text-overwrite)
  490. (define-key map "\C-c\C-aS" 'artist-select-op-spray-can)
  491. (define-key map "\C-c\C-az" 'artist-select-op-spray-set-size)
  492. (define-key map "\C-c\C-a\C-d" 'artist-select-op-erase-char)
  493. (define-key map "\C-c\C-aE" 'artist-select-op-erase-rectangle)
  494. (define-key map "\C-c\C-av" 'artist-select-op-vaporize-line)
  495. (define-key map "\C-c\C-aV" 'artist-select-op-vaporize-lines)
  496. (define-key map "\C-c\C-a\C-k" 'artist-select-op-cut-rectangle)
  497. (define-key map "\C-c\C-a\M-w" 'artist-select-op-copy-rectangle)
  498. (define-key map "\C-c\C-a\C-y" 'artist-select-op-paste)
  499. (define-key map "\C-c\C-af" 'artist-select-op-flood-fill)
  500. (define-key map "\C-c\C-a\C-b" 'artist-submit-bug-report)
  501. (define-key map [menu-bar artist] (cons "Artist" artist-menu-map))
  502. map)
  503. "Keymap for `artist-minor-mode'.")
  504. (defvar artist-replacement-table (make-vector 256 0)
  505. "Replacement table for `artist-replace-char'.")
  506. ;;;
  507. ;;; The table of graphic operations
  508. ;;;
  509. (defvar artist-mt
  510. ;; Implementation note: Maybe this should be done using a structure
  511. ;; in the cl package?
  512. ;;
  513. '(
  514. (menu
  515. ("Drawing"
  516. ((function-call
  517. ( "Undo" do-undo undo))
  518. (separator )
  519. (graphics-operation
  520. ("Pen" (("Pen" pen-char "pen-c"
  521. artist-no-arrows nil
  522. nil nil nil
  523. artist-do-continously
  524. artist-pen
  525. (nil))
  526. ("Pen Line" pen-line "pen-l"
  527. artist-arrows artist-pen-set-arrow-points
  528. artist-pen-reset-last-xy nil nil
  529. artist-do-continously
  530. artist-pen-line
  531. (nil)))))
  532. (graphics-operation
  533. ("Line" (("line" line "line"
  534. artist-arrows artist-set-arrow-points-for-2points
  535. nil nil nil
  536. 2
  537. artist-draw-line
  538. (artist-undraw-line
  539. artist-nil nil))
  540. ("straight line" s-line "sline"
  541. artist-arrows artist-set-arrow-points-for-2points
  542. nil nil nil
  543. 2
  544. artist-draw-sline
  545. (artist-undraw-sline
  546. artist-nil nil)))))
  547. (graphics-operation
  548. ("Rectangle" (("rectangle" rect "rect"
  549. artist-no-arrows nil
  550. nil nil nil
  551. 2
  552. artist-draw-rect
  553. (artist-undraw-rect
  554. artist-t-if-fill-char-set artist-fill-rect))
  555. ("square" square "square"
  556. artist-no-arrows nil
  557. nil nil nil
  558. 2
  559. artist-draw-square
  560. (artist-undraw-square
  561. artist-t-if-fill-char-set artist-fill-square)))))
  562. (graphics-operation
  563. ("Poly-line" (("poly-line" polyline "poly"
  564. artist-arrows artist-set-arrow-points-for-poly
  565. nil nil nil
  566. artist-do-poly
  567. artist-draw-line
  568. (artist-undraw-line
  569. artist-nil nil))
  570. ("straight poly-line" spolyline "s-poly"
  571. artist-arrows artist-set-arrow-points-for-poly
  572. nil nil nil
  573. artist-do-poly
  574. artist-draw-sline
  575. (artist-undraw-sline
  576. artist-nil nil)))))
  577. (graphics-operation
  578. ("Ellipse" (("ellipse" ellipse "ellipse"
  579. artist-no-arrows nil
  580. nil nil nil
  581. 2
  582. artist-draw-ellipse
  583. (artist-undraw-ellipse
  584. artist-t-if-fill-char-set artist-fill-ellipse))
  585. ("circle" circle "circle"
  586. artist-no-arrows nil
  587. nil nil nil
  588. 2
  589. artist-draw-circle
  590. (artist-undraw-circle
  591. artist-t-if-fill-char-set artist-fill-circle)))))
  592. (graphics-operation
  593. ("Text" (("text see-thru" text-thru "text-thru"
  594. artist-no-arrows nil
  595. nil nil nil
  596. 1
  597. artist-text-see-thru
  598. nil)
  599. ("text overwrite" text-ovwrt "text-ovwrt"
  600. artist-no-arrows nil
  601. nil nil nil
  602. 1
  603. artist-text-overwrite
  604. nil))))
  605. (graphics-operation
  606. ("Spray-can" (("spray-can" spray-can "spray-can"
  607. artist-no-arrows nil
  608. nil nil nil
  609. artist-do-continously
  610. artist-spray
  611. (artist-spray-get-interval))
  612. ("spray set size" spray-get-size "spray-size"
  613. artist-no-arrows nil
  614. nil artist-spray-clear-circle artist-spray-set-radius
  615. 2
  616. artist-draw-circle
  617. (artist-undraw-circle
  618. artist-nil nil)))))
  619. (graphics-operation
  620. ("Erase" (("erase char" erase-char "erase-c"
  621. artist-no-arrows nil
  622. nil nil nil
  623. artist-do-continously
  624. artist-erase-char
  625. (nil))
  626. ("erase rectangle" erase-rect "erase-r"
  627. artist-no-arrows nil
  628. nil nil nil
  629. 2
  630. artist-draw-rect
  631. (artist-undraw-rect
  632. artist-t artist-erase-rect)))))
  633. (graphics-operation
  634. ("Vaporize" (("vaporize line" vaporize-line "vaporize-1"
  635. artist-no-arrows nil
  636. nil nil nil
  637. 1
  638. artist-vaporize-line
  639. nil)
  640. ("vaporize lines" vaporize-lines "vaporize-n"
  641. artist-no-arrows nil
  642. nil nil nil
  643. 1
  644. artist-vaporize-lines
  645. nil)))))))
  646. (menu
  647. ("Edit"
  648. ((graphics-operation
  649. ("Cut" (("cut rectangle" cut-r "cut-r"
  650. artist-no-arrows nil
  651. nil nil nil
  652. 2
  653. artist-draw-rect
  654. (artist-undraw-rect
  655. artist-t artist-cut-rect))
  656. ("cut square" cut-s "cut-s"
  657. artist-no-arrows nil
  658. nil nil nil
  659. 2
  660. artist-draw-square
  661. (artist-undraw-square
  662. artist-t artist-cut-square)))))
  663. (graphics-operation
  664. ("Copy" (("copy rectangle" copy-r "copy-r"
  665. artist-no-arrows nil
  666. nil nil nil
  667. 2
  668. artist-draw-rect
  669. (artist-undraw-rect
  670. artist-t artist-copy-rect))
  671. ("copy square" copy-s "copy-s"
  672. artist-no-arrows nil
  673. nil nil nil
  674. 2
  675. artist-draw-square
  676. (artist-undraw-square
  677. artist-t artist-copy-square)))))
  678. (graphics-operation
  679. ("Paste" (("paste" paste "paste"
  680. artist-no-arrows nil
  681. nil nil nil
  682. 1
  683. artist-paste
  684. nil)
  685. ("paste" paste "paste"
  686. artist-no-arrows nil
  687. nil nil nil
  688. 1
  689. artist-paste
  690. nil))))
  691. (graphics-operation
  692. ("Flood-fill" (("flood-fill" flood-fill "flood"
  693. artist-no-arrows nil
  694. nil nil nil
  695. 1
  696. artist-flood-fill
  697. nil)
  698. ("flood-fill" flood-fill "flood"
  699. artist-no-arrows nil
  700. nil nil nil
  701. 1
  702. artist-flood-fill
  703. nil)))))))
  704. (menu
  705. ("Settings"
  706. ((function-call
  707. ("Set Fill" set-fill artist-select-fill-char))
  708. (function-call
  709. ("Set Line" set-line artist-select-line-char))
  710. (function-call
  711. ("Set Erase" set-erase artist-select-erase-char))
  712. (function-call
  713. ("Rubber-banding" rubber-band artist-toggle-rubber-banding))
  714. (function-call
  715. ("Trimming" trimming artist-toggle-trim-line-endings))
  716. (function-call
  717. ("Borders" borders artist-toggle-borderless-shapes))
  718. (function-call
  719. ("Spray-chars" spray-chars artist-select-spray-chars)))))
  720. ) ;; end of list
  721. "Master Table for `artist-mode'.
  722. This table is primarily a table over the different graphics operations
  723. available in Artist mode, but it also holds layout information for the
  724. popup menu.
  725. The master table is a list of table elements. The elements of this table
  726. have the layout
  727. (TAG INFO-PART)
  728. There are three kinds of TAG:
  729. `menu' -- a sub-menu
  730. `separator' -- produce a separator in the popup menu
  731. `function-call' -- call a function
  732. `graphics-operation' -- a graphics operation
  733. The layout of the INFO-PART for `menu' is
  734. (TITLE ((TAG-1 INFO-PART-1) (TAG-2 INFO-PART-2) ...))
  735. TITLE is the title of the submenu; this is followed by a list of
  736. menu items, each on the general form (TAG INFO-PART).
  737. The layout of the INFO-PART for `separator' is empty and not used.
  738. This is the layout of the INFO-PART for `function-call':
  739. (KEYWORD SYMBOL FN)
  740. KEYWORD is a string naming the operation, and appears in the popup menu.
  741. SYMBOL is the symbol for the operations.
  742. FN is the function performing the operation. This function
  743. is called with no arguments. Its return value is ignored.
  744. The layout of the INFO-PART for `graphics-operation' is
  745. (TITLE (UNSHIFTED SHIFTED))
  746. TITLE is the title that appears in the popup menu. UNSHIFTED
  747. and SHIFTED specify for unshifted and shifted operation. Both
  748. have the form
  749. (KEYWORD KEY-SYMBOL MODE-LINE ARROW-PRED ARROW-SET-FN
  750. INIT-FN PREP-FILL-FN EXIT-FN DRAW-HOW DRAW-FN EXTRA-DRAW-INFO)
  751. KEYWORD is a string specifying the name of the shape to draw.
  752. This is used when selecting drawing operation.
  753. KEY-SYMBOL is the key which is used when looking up members
  754. through the functions `artist-go-get-MEMBER-from-symbol'
  755. and `artist-fc-get-MEMBER-from-symbol'.
  756. MODE-LINE is a string that appears in the mode-line when drawing
  757. the shape.
  758. ARROW-PRED is a function that is called to find out if the shape
  759. can have arrows. The function is called with no arguments and
  760. must return nil or t.
  761. ARROW-SET-FN is a function that is called to set arrow end-points.
  762. Arguments and return values for this function are described below.
  763. INIT-FN is, if non-nil, a function that is called when the first
  764. point of the shape is set. Arguments and return values for
  765. this function are described below.
  766. PREP-FILL-FN is, if non-nil, a function that is called after
  767. the last point is set, but before the filling is done.
  768. Arguments and return values for this function are described below.
  769. EXIT-FN is, if non-nil, a function that is called after filling
  770. is done. Arguments and return values for this function are
  771. described below.
  772. DRAW-HOW defines the kind of shape. The kinds of shapes are:
  773. `artist-do-continously' -- Do drawing operation continuously,
  774. as long as the mouse button is held down.
  775. `artist-do-poly' -- Do drawing operation many times.
  776. 1 -- Do drawing operation only once.
  777. 2 -- The drawing operation requires two points.
  778. DRAW-FN is the function to call for drawing. Arguments and
  779. return values for this function are described below.
  780. EXTRA-DRAW-INFO the layout of this depends on the value of DRAW-HOW:
  781. If DRAW-HOW is `artist-do-continously':
  782. (INTERVAL-FN)
  783. INTERVAL-FN is, if non-nil, a function to call for getting
  784. an interval between repeated calls to the DRAW-FN.
  785. This function is called with no arguments and must
  786. return a number, the interval in seconds.
  787. If nil, calls to DRAW-FN are done only when the mouse
  788. or cursor is moved.
  789. If DRAW-HOW is either `artist-do-poly' or 2:
  790. (UNDRAW-FN FILL-PRED FILL-FN)
  791. UNDRAW-FN is a function to call for undrawing the shape.
  792. Arguments and return values for this function are
  793. described below.
  794. FILL-PRED is a function that is called to find out if the shape
  795. can have arrows. The function must take no arguments and
  796. return nil or t.
  797. FILL-FN is a function to call for filling the shape.
  798. Arguments and return values for this function are
  799. described below.
  800. If DRAW-HOW is 1:
  801. ()
  802. Note! All symbols and keywords (both in the `function-call' INFO-PART
  803. as well as in the `graphics-operation' INFO-PART) must be unique.
  804. The following table describe function arguments and return value
  805. for different functions and DRAW-HOWs.
  806. If DRAW-HOW is either `artist-do-continously' or 1:
  807. INIT-FN X Y ==> ignored
  808. PREP-FILL-FN X Y ==> ignored
  809. EXIT-FN X Y ==> ignored
  810. ARROW-SET-FN X Y ==> ignored
  811. DRAW-FN X Y ==> ignored
  812. If DRAW-HOW is 2:
  813. INIT-FN X1 Y1 ==> ignored
  814. PREP-FILL-FN X1 Y1 X2 Y2 ==> ignored
  815. EXIT-FN X1 Y1 X2 Y2 ==> ignored
  816. ARROW-SET-FN X1 Y1 X2 Y2 ==> ignored
  817. DRAW-FN X1 Y1 X2 Y2 ==> (ENDPOINT-1 ENDPOINT-2 SHAPE)
  818. UNDRAW-FN (ENDPOINT-1 ENDPOINT-2 SHAPE) ==> ignored
  819. FILL-FN (ENDPOINT-1 ENDPOINT-2 SHAPE) X1 Y1 X2 Y2 ==> ignored
  820. ENDPOINT-1 and ENDPOINT-2 are endpoints which are created with
  821. `artist-make-endpoint'
  822. SHAPE is an opaque structure, created by the DRAW-FN and intended
  823. to be used only by the UNDRAW-FN.
  824. If DRAW-HOW is `artist-do-poly':
  825. INIT-FN X1 Y1
  826. PREP-FILL-FN POINT-LIST
  827. ARROW-SET-FN POINT-LIST
  828. EXIT-FN POINT-LIST
  829. DRAW-FN X-LAST Y-LAST X-NEW Y-NEW ==> (ENDPOINT-1 ENDPOINT-2 SHAPE)
  830. UNDRAW-FN (ENDPOINT-1 ENDPOINT-2 SHAPE)
  831. FILL-FN POINT-LIST
  832. ENDPOINT-1 and ENDPOINT-2 are endpoints which are created with
  833. `artist-make-endpoint'.
  834. SHAPE is an opaque structure, created by the DRAW-FN and intended
  835. to be used only by the UNDRAW-FN.
  836. POINT-LIST is a list of vectors [X Y].")
  837. ;;
  838. ;; Accessors for the master table
  839. ;;
  840. (defun artist-mt-get-tag (element)
  841. "Retrieve the tag component from the master table ELEMENT."
  842. (elt element 0))
  843. (defun artist-mt-get-info-part (element)
  844. "Retrieve the info part component from the master table ELEMENT."
  845. (elt element 1))
  846. ;; For the 'graphics-operation info-parts
  847. ;;
  848. (defsubst artist-go-get-desc (info-part)
  849. "Retrieve the description component from a graphics operation INFO-PART."
  850. (elt info-part 0))
  851. (defsubst artist-go-get-unshifted (info-part)
  852. "Retrieve the unshifted info from a graphics operation INFO-PART."
  853. (elt (elt info-part 1) 0))
  854. (defsubst artist-go-get-shifted (info-part)
  855. "Retrieve the shifted info from a graphics operation INFO-PART."
  856. (elt (elt info-part 1) 1))
  857. (defsubst artist-go-get-keyword (info-variant-part)
  858. "Retrieve the keyword component from an INFO-VARIANT-PART.
  859. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  860. (elt info-variant-part 0))
  861. (defsubst artist-go-get-symbol (info-variant-part)
  862. "Retrieve the symbol component from an INFO-VARIANT-PART.
  863. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  864. (elt info-variant-part 1))
  865. (defsubst artist-go-get-mode-line (info-variant-part)
  866. "Retrieve the mode line component from an INFO-VARIANT-PART.
  867. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  868. (elt info-variant-part 2))
  869. (defsubst artist-go-get-arrow-pred (info-variant-part)
  870. "Retrieve the arrow predicate component from an INFO-VARIANT-PART.
  871. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  872. (elt info-variant-part 3))
  873. (defsubst artist-go-get-arrow-set-fn (info-variant-part)
  874. "Retrieve the arrow set component from an INFO-VARIANT-PART.
  875. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  876. (elt info-variant-part 4))
  877. (defsubst artist-go-get-init-fn (info-variant-part)
  878. "Retrieve the init function component from an INFO-VARIANT-PART.
  879. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  880. (elt info-variant-part 5))
  881. (defsubst artist-go-get-prep-fill-fn (info-variant-part)
  882. "Retrieve the fill preparation function component from an INFO-VARIANT-PART.
  883. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  884. (elt info-variant-part 6))
  885. (defsubst artist-go-get-exit-fn (info-variant-part)
  886. "Retrieve the exit component from an INFO-VARIANT-PART.
  887. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  888. (elt info-variant-part 7))
  889. (defsubst artist-go-get-draw-how (info-variant-part)
  890. "Retrieve the draw how component from an INFO-VARIANT-PART.
  891. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  892. (elt info-variant-part 8))
  893. (defsubst artist-go-get-draw-fn (info-variant-part)
  894. "Retrieve the draw function component from an INFO-VARIANT-PART.
  895. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part."
  896. (elt info-variant-part 9))
  897. (defsubst artist-go-get-undraw-fn (info-variant-part)
  898. "Retrieve the undraw function component from an INFO-VARIANT-PART.
  899. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
  900. This interval function component is available only if the `draw-how'
  901. component is other than `artist-do-continously' or 1."
  902. (elt (elt info-variant-part 10) 0))
  903. (defsubst artist-go-get-interval-fn (info-variant-part)
  904. "Retrieve the interval function component from an INFO-VARIANT-PART.
  905. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
  906. This interval function component is available only if the `draw-how'
  907. component is `artist-do-continously'."
  908. (elt (elt info-variant-part 10) 0))
  909. (defsubst artist-go-get-fill-pred (info-variant-part)
  910. "Retrieve the fill predicate component from an INFO-VARIANT-PART.
  911. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
  912. This interval function component is available only if the `draw-how'
  913. component is other than `artist-do-continously' or 1."
  914. (elt (elt info-variant-part 10) 1))
  915. (defsubst artist-go-get-fill-fn (info-variant-part)
  916. "Retrieve the fill function component from an INFO-VARIANT-PART.
  917. An INFO-VARIANT-PART is the shifted or unshifted info from a info-part.
  918. This interval function component is available only if the `draw-how'
  919. component is other than `artist-do-continously' or 1."
  920. (elt (elt info-variant-part 10) 2))
  921. ;; For the 'function-call info-parts
  922. ;;
  923. (defsubst artist-fc-get-keyword (info-part)
  924. "Retrieve the keyword component from a graphics operation INFO-PART."
  925. (elt info-part 0))
  926. (defsubst artist-fc-get-symbol (info-part)
  927. "Retrieve the symbol component from a graphics operation INFO-PART."
  928. (elt info-part 1))
  929. (defsubst artist-fc-get-fn (info-part)
  930. "Retrieve the function component from a graphics operation INFO-PART."
  931. (elt info-part 2))
  932. ;; For the 'menu info-parts
  933. ;;
  934. (defsubst artist-mn-get-title (info-part)
  935. "Retrieve the title component from a graphics operation INFO-PART."
  936. (elt info-part 0))
  937. (defsubst artist-mn-get-items (info-part)
  938. "Retrieve the items component from a graphics operation INFO-PART."
  939. (elt info-part 1))
  940. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  941. ;; mouse wheel cyclic operation selection
  942. (defun artist-get-last-non-nil-op (op-list &optional last-non-nil)
  943. "Find the last non-nil draw operation in OP-LIST.
  944. Optional LAST-NON-NIL will be returned if OP-LIST is nil."
  945. (if op-list
  946. (artist-get-last-non-nil-op (cdr op-list)
  947. (or (car (car op-list)) last-non-nil))
  948. last-non-nil))
  949. (defun artist-get-first-non-nil-op (op-list)
  950. "Find the first non-nil draw operation in OP-LIST."
  951. (or (car (car op-list)) (artist-get-first-non-nil-op (cdr op-list))))
  952. (defun artist-is-in-op-list-p (op op-list)
  953. "Check whether OP is in OP-LIST."
  954. (and op-list
  955. (or (and (car (car op-list)) (string= op (car (car op-list))))
  956. (artist-is-in-op-list-p op (cdr op-list)))))
  957. (defun artist-make-prev-next-op-alist (op-list
  958. &optional
  959. last-non-nil-arg first-non-nil-arg
  960. prev-entry prev-op-arg)
  961. "Build an assoc-list of OP-LIST.
  962. The arguments LAST-NON-NIL-ARG, FIRST-NON-NIL-ARG, PREV-ENTRY and
  963. PREV-OP-ARG are used when invoked recursively during the build-up."
  964. (let* ((last-non-nil (or last-non-nil-arg
  965. (artist-get-last-non-nil-op
  966. artist-key-compl-table)))
  967. (first-non-nil (or first-non-nil-arg
  968. (artist-get-first-non-nil-op
  969. artist-key-compl-table)))
  970. (prev-op (or prev-op-arg last-non-nil))
  971. (op (car (car op-list)))
  972. (opsym (artist-mt-get-symbol-from-keyword op))
  973. (entry (cons opsym (cons prev-op nil))))
  974. (if (or (and op-list (not op))
  975. (artist-is-in-op-list-p op (cdr op-list)))
  976. (artist-make-prev-next-op-alist (cdr op-list)
  977. last-non-nil first-non-nil
  978. prev-entry prev-op)
  979. (if prev-entry (setcdr (cdr prev-entry) op))
  980. (if op-list
  981. (cons entry (artist-make-prev-next-op-alist
  982. (cdr op-list)
  983. last-non-nil first-non-nil
  984. entry op))
  985. (progn (setcdr (cdr prev-entry) first-non-nil) nil)))))
  986. (defun artist-select-next-op-in-list ()
  987. "Cyclically select next drawing mode operation."
  988. (interactive)
  989. (let ((next-op (cdr (cdr (assoc artist-curr-go artist-prev-next-op-alist)))))
  990. (artist-select-operation next-op)
  991. (message "%s" next-op)))
  992. (defun artist-select-prev-op-in-list ()
  993. "Cyclically select previous drawing mode operation."
  994. (interactive)
  995. (let ((prev-op (car (cdr (assoc artist-curr-go artist-prev-next-op-alist)))))
  996. (artist-select-operation prev-op)
  997. (message "%s" prev-op)))
  998. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  999. ;;; ---------------------------------
  1000. ;;; The artist-mode
  1001. ;;; ---------------------------------
  1002. ;;;###autoload
  1003. (defun artist-mode (&optional state)
  1004. "Toggle Artist mode.
  1005. With argument STATE, turn Artist mode on if STATE is positive.
  1006. Artist lets you draw lines, squares, rectangles and poly-lines,
  1007. ellipses and circles with your mouse and/or keyboard.
  1008. How to quit Artist mode
  1009. Type \\[artist-mode-off] to quit artist-mode.
  1010. How to submit a bug report
  1011. Type \\[artist-submit-bug-report] to submit a bug report.
  1012. Drawing with the mouse:
  1013. mouse-2
  1014. shift mouse-2 Pops up a menu where you can select what to draw with
  1015. mouse-1, and where you can do some settings (described
  1016. below).
  1017. mouse-1
  1018. shift mouse-1 Draws lines, rectangles or poly-lines, erases, cuts, copies
  1019. or pastes:
  1020. Operation Not shifted Shifted
  1021. --------------------------------------------------------------
  1022. Pen fill-char at point line from last point
  1023. to new point
  1024. --------------------------------------------------------------
  1025. Line Line in any direction Straight line
  1026. --------------------------------------------------------------
  1027. Rectangle Rectangle Square
  1028. --------------------------------------------------------------
  1029. Poly-line Poly-line in any dir Straight poly-lines
  1030. --------------------------------------------------------------
  1031. Ellipses Ellipses Circles
  1032. --------------------------------------------------------------
  1033. Text Text (see thru) Text (overwrite)
  1034. --------------------------------------------------------------
  1035. Spray-can Spray-can Set size for spray
  1036. --------------------------------------------------------------
  1037. Erase Erase character Erase rectangle
  1038. --------------------------------------------------------------
  1039. Vaporize Erase single line Erase connected
  1040. lines
  1041. --------------------------------------------------------------
  1042. Cut Cut rectangle Cut square
  1043. --------------------------------------------------------------
  1044. Copy Copy rectangle Copy square
  1045. --------------------------------------------------------------
  1046. Paste Paste Paste
  1047. --------------------------------------------------------------
  1048. Flood-fill Flood-fill Flood-fill
  1049. --------------------------------------------------------------
  1050. * Straight lines can only go horizontally, vertically
  1051. or diagonally.
  1052. * Poly-lines are drawn while holding mouse-1 down. When you
  1053. release the button, the point is set. If you want a segment
  1054. to be straight, hold down shift before pressing the
  1055. mouse-1 button. Click mouse-2 or mouse-3 to stop drawing
  1056. poly-lines.
  1057. * See thru for text means that text already in the buffer
  1058. will be visible through blanks in the text rendered, while
  1059. overwrite means the opposite.
  1060. * Vaporizing connected lines only vaporizes lines whose
  1061. _endpoints_ are connected. See also the variable
  1062. `artist-vaporize-fuzziness'.
  1063. * Cut copies, then clears the rectangle/square.
  1064. * When drawing lines or poly-lines, you can set arrows.
  1065. See below under ``Arrows'' for more info.
  1066. * The mode line shows the currently selected drawing operation.
  1067. In addition, if it has an asterisk (*) at the end, you
  1068. are currently drawing something.
  1069. * Be patient when flood-filling -- large areas take quite
  1070. some time to fill.
  1071. mouse-3 Erases character under pointer
  1072. shift mouse-3 Erases rectangle
  1073. Settings
  1074. Set fill Sets the character used when filling rectangles/squares
  1075. Set line Sets the character used when drawing lines
  1076. Erase char Sets the character used when erasing
  1077. Rubber-banding Toggles rubber-banding
  1078. Trimming Toggles trimming of line-endings (that is: when the shape
  1079. is drawn, extraneous white-space at end of lines is removed)
  1080. Borders Toggles the drawing of line borders around filled shapes
  1081. Drawing with keys
  1082. \\[artist-key-set-point] Does one of the following:
  1083. For lines/rectangles/squares: sets the first/second endpoint
  1084. For poly-lines: sets a point (use C-u \\[artist-key-set-point] to set last point)
  1085. When erase characters: toggles erasing
  1086. When cutting/copying: Sets first/last endpoint of rect/square
  1087. When pasting: Pastes
  1088. \\[artist-select-operation] Selects what to draw
  1089. Move around with \\[artist-next-line], \\[artist-previous-line], \\[artist-forward-char] and \\[artist-backward-char].
  1090. \\[artist-select-fill-char] Sets the character to use when filling
  1091. \\[artist-select-line-char] Sets the character to use when drawing
  1092. \\[artist-select-erase-char] Sets the character to use when erasing
  1093. \\[artist-toggle-rubber-banding] Toggles rubber-banding
  1094. \\[artist-toggle-trim-line-endings] Toggles trimming of line-endings
  1095. \\[artist-toggle-borderless-shapes] Toggles borders on drawn shapes
  1096. Arrows
  1097. \\[artist-toggle-first-arrow] Sets/unsets an arrow at the beginning
  1098. of the line/poly-line
  1099. \\[artist-toggle-second-arrow] Sets/unsets an arrow at the end
  1100. of the line/poly-line
  1101. Selecting operation
  1102. There are some keys for quickly selecting drawing operations:
  1103. \\[artist-select-op-line] Selects drawing lines
  1104. \\[artist-select-op-straight-line] Selects drawing straight lines
  1105. \\[artist-select-op-rectangle] Selects drawing rectangles
  1106. \\[artist-select-op-square] Selects drawing squares
  1107. \\[artist-select-op-poly-line] Selects drawing poly-lines
  1108. \\[artist-select-op-straight-poly-line] Selects drawing straight poly-lines
  1109. \\[artist-select-op-ellipse] Selects drawing ellipses
  1110. \\[artist-select-op-circle] Selects drawing circles
  1111. \\[artist-select-op-text-see-thru] Selects rendering text (see thru)
  1112. \\[artist-select-op-text-overwrite] Selects rendering text (overwrite)
  1113. \\[artist-select-op-spray-can] Spray with spray-can
  1114. \\[artist-select-op-spray-set-size] Set size for the spray-can
  1115. \\[artist-select-op-erase-char] Selects erasing characters
  1116. \\[artist-select-op-erase-rectangle] Selects erasing rectangles
  1117. \\[artist-select-op-vaporize-line] Selects vaporizing single lines
  1118. \\[artist-select-op-vaporize-lines] Selects vaporizing connected lines
  1119. \\[artist-select-op-cut-rectangle] Selects cutting rectangles
  1120. \\[artist-select-op-copy-rectangle] Selects copying rectangles
  1121. \\[artist-select-op-paste] Selects pasting
  1122. \\[artist-select-op-flood-fill] Selects flood-filling
  1123. Variables
  1124. This is a brief overview of the different variables. For more info,
  1125. see the documentation for the variables (type \\[describe-variable] <variable> RET).
  1126. artist-rubber-banding Interactively do rubber-banding or not
  1127. artist-first-char What to set at first/second point...
  1128. artist-second-char ...when not rubber-banding
  1129. artist-interface-with-rect If cut/copy/paste should interface with rect
  1130. artist-arrows The arrows to use when drawing arrows
  1131. artist-aspect-ratio Character height-to-width for squares
  1132. artist-trim-line-endings Trimming of line endings
  1133. artist-flood-fill-right-border Right border when flood-filling
  1134. artist-flood-fill-show-incrementally Update display while filling
  1135. artist-pointer-shape Pointer shape to use while drawing
  1136. artist-ellipse-left-char Character to use for narrow ellipses
  1137. artist-ellipse-right-char Character to use for narrow ellipses
  1138. artist-borderless-shapes If shapes should have borders
  1139. artist-picture-compatibility Whether or not to be picture mode compatible
  1140. artist-vaporize-fuzziness Tolerance when recognizing lines
  1141. artist-spray-interval Seconds between repeated sprayings
  1142. artist-spray-radius Size of the spray-area
  1143. artist-spray-chars The spray-``color''
  1144. artist-spray-new-chars Initial spray-``color''
  1145. Hooks
  1146. When entering artist-mode, the hook `artist-mode-init-hook' is called.
  1147. When quitting artist-mode, the hook `artist-mode-exit-hook' is called.
  1148. Keymap summary
  1149. \\{artist-mode-map}"
  1150. (interactive)
  1151. (if (setq artist-mode
  1152. (if (null state) (not artist-mode)
  1153. (> (prefix-numeric-value state) 0)))
  1154. (artist-mode-init)
  1155. (artist-mode-exit)))
  1156. ;; insert our minor mode string
  1157. (or (assq 'artist-mode minor-mode-alist)
  1158. (setq minor-mode-alist
  1159. (cons '(artist-mode artist-mode-name)
  1160. minor-mode-alist)))
  1161. ;; insert our minor mode keymap
  1162. (or (assq 'artist-mode minor-mode-map-alist)
  1163. (setq minor-mode-map-alist
  1164. (cons (cons 'artist-mode artist-mode-map)
  1165. minor-mode-map-alist)))
  1166. ;; Init and exit
  1167. (defun artist-mode-init ()
  1168. "Init Artist mode. This will call the hook `artist-mode-init-hook'."
  1169. ;; Set up a conversion table for mapping tabs and new-lines to spaces.
  1170. ;; the last case, 0, is for the last position in buffer/region, where
  1171. ;; the `following-char' function returns 0.
  1172. (let ((i 0))
  1173. (while (< i 256)
  1174. (aset artist-replacement-table i i)
  1175. (setq i (1+ i))))
  1176. (aset artist-replacement-table ?\n ?\s)
  1177. (aset artist-replacement-table ?\t ?\s)
  1178. (aset artist-replacement-table 0 ?\s)
  1179. ;; More setup
  1180. (make-local-variable 'artist-key-is-drawing)
  1181. (make-local-variable 'artist-key-endpoint1)
  1182. (make-local-variable 'artist-key-poly-point-list)
  1183. (make-local-variable 'artist-key-shape)
  1184. (make-local-variable 'artist-key-draw-how)
  1185. (make-local-variable 'artist-popup-menu-table)
  1186. (make-local-variable 'artist-key-compl-table)
  1187. (make-local-variable 'artist-prev-next-op-alist)
  1188. (make-local-variable 'artist-rb-save-data)
  1189. (make-local-variable 'artist-arrow-point-1)
  1190. (make-local-variable 'artist-arrow-point-2)
  1191. (setq artist-key-is-drawing nil)
  1192. (setq artist-key-endpoint1 nil)
  1193. (setq artist-key-poly-point-list nil)
  1194. (setq artist-key-shape nil)
  1195. (setq artist-popup-menu-table (artist-compute-popup-menu-table artist-mt))
  1196. (setq artist-key-compl-table (artist-compute-key-compl-table artist-mt))
  1197. (setq artist-prev-next-op-alist
  1198. (artist-make-prev-next-op-alist artist-key-compl-table))
  1199. (setq artist-rb-save-data (make-vector 7 0))
  1200. (setq artist-arrow-point-1 nil)
  1201. (setq artist-arrow-point-2 nil)
  1202. (make-local-variable 'next-line-add-newlines)
  1203. (setq next-line-add-newlines t)
  1204. (setq artist-key-draw-how
  1205. (artist-go-get-draw-how-from-symbol artist-curr-go))
  1206. (if (and artist-picture-compatibility (not (eq major-mode 'picture-mode)))
  1207. (progn
  1208. (picture-mode)
  1209. (message "")))
  1210. (run-hooks 'artist-mode-init-hook)
  1211. (artist-mode-line-show-curr-operation artist-key-is-drawing))
  1212. (defun artist-mode-exit ()
  1213. "Exit Artist mode. This will call the hook `artist-mode-exit-hook'."
  1214. (if (and artist-picture-compatibility (eq major-mode 'picture-mode))
  1215. (picture-mode-exit))
  1216. (kill-local-variable 'next-line-add-newlines)
  1217. (run-hooks 'artist-mode-exit-hook))
  1218. (defun artist-mode-off ()
  1219. "Turn Artist mode off."
  1220. (interactive)
  1221. (artist-mode -1)
  1222. (force-mode-line-update))
  1223. ;;
  1224. ;; General routines
  1225. ;;
  1226. (defun artist-update-display ()
  1227. "Repaint the display."
  1228. (sit-for 0))
  1229. (defun artist-mode-line-show-curr-operation (is-drawing)
  1230. "Show current operation in mode-line. If IS-DRAWING, show that."
  1231. (let ((mtext (concat artist-mode-name "/"
  1232. (artist-go-get-mode-line-from-symbol artist-curr-go)
  1233. (if is-drawing "/*" ""))))
  1234. (setcdr (assq 'artist-mode minor-mode-alist) (list mtext)))
  1235. (force-mode-line-update))
  1236. (defun artist-t-if-fill-char-set ()
  1237. "Return the value of the variable `artist-fill-char-set'."
  1238. artist-fill-char-set)
  1239. (defun artist-t ()
  1240. "Always return t."
  1241. t)
  1242. (defun artist-nil ()
  1243. "Always return nil."
  1244. nil)
  1245. (defun artist-arrows ()
  1246. "Say yes to arrows!"
  1247. t)
  1248. (defun artist-no-arrows ()
  1249. "Say no to arrows!"
  1250. nil)
  1251. ;;
  1252. ;; Auxiliary init-routines
  1253. ;;
  1254. ;
  1255. ; Computing the table for the x-popup-menu from the master table
  1256. ;
  1257. (defun artist-compute-popup-menu-table (menu-table)
  1258. "Create a menu from MENU-TABLE data.
  1259. The returned value is suitable for the `x-popup-menu' function."
  1260. (cons "Artist menu"
  1261. (artist-compute-popup-menu-table-sub menu-table)))
  1262. (defun artist-compute-popup-menu-table-sub (menu-table)
  1263. "Compute operation table suitable for `x-popup-menu' from MENU-TABLE."
  1264. (mapcar
  1265. (lambda (element)
  1266. (let ((element-tag (artist-mt-get-tag element)))
  1267. (cond ((eq element-tag 'graphics-operation)
  1268. (let* ((info-part (artist-mt-get-info-part element))
  1269. (descr (artist-go-get-desc info-part))
  1270. (unshifted (artist-go-get-unshifted info-part))
  1271. (symbol (artist-go-get-symbol unshifted)))
  1272. (list descr symbol)))
  1273. ((eq element-tag 'function-call)
  1274. (let* ((info-part (artist-mt-get-info-part element))
  1275. (keyword (artist-fc-get-keyword info-part))
  1276. (symbol (artist-fc-get-symbol info-part)))
  1277. (list keyword symbol)))
  1278. ((eq element-tag 'separator)
  1279. '("" ""))
  1280. ((eq element-tag 'menu)
  1281. (let* ((info-part (artist-mt-get-info-part element))
  1282. (title (artist-mn-get-title info-part))
  1283. (items (artist-mn-get-items info-part)))
  1284. (cons title (artist-compute-popup-menu-table-sub items))))
  1285. (t
  1286. (error "Internal error: unknown element-tag: \"%s\""
  1287. element-tag)))))
  1288. menu-table))
  1289. ;
  1290. ; Computing the completion table from the master table
  1291. ;
  1292. (defun artist-compute-key-compl-table (menu-table)
  1293. "Compute completion table from MENU-TABLE, suitable for `completing-read'."
  1294. (apply
  1295. 'nconc
  1296. (remq nil
  1297. (mapcar
  1298. (lambda (element)
  1299. (let ((element-tag (artist-mt-get-tag element)))
  1300. (cond ((eq element-tag 'graphics-operation)
  1301. (let* ((info-part (artist-mt-get-info-part element))
  1302. (unshifted (artist-go-get-unshifted info-part))
  1303. (shifted (artist-go-get-shifted info-part))
  1304. (unshifted-kwd (artist-go-get-keyword unshifted))
  1305. (shifted-kwd (artist-go-get-keyword shifted)))
  1306. (list (list unshifted-kwd) (list shifted-kwd))))
  1307. ((eq element-tag 'menu)
  1308. (let* ((info-part (artist-mt-get-info-part element))
  1309. (items (artist-mn-get-items info-part)))
  1310. (artist-compute-key-compl-table items)))
  1311. (t
  1312. nil))))
  1313. menu-table))))
  1314. ;
  1315. ; Retrieving a symbol (graphics operation or function-call) from a keyword
  1316. ;
  1317. (defun artist-mt-get-symbol-from-keyword (kwd)
  1318. "Search master table for keyword KWD and return its symbol."
  1319. (artist-mt-get-symbol-from-keyword-sub artist-mt kwd))
  1320. (defun artist-mt-get-symbol-from-keyword-sub (table kwd)
  1321. "Search TABLE for keyword KWD and return its symbol."
  1322. (catch 'found
  1323. (mapc
  1324. (lambda (element)
  1325. (let ((element-tag (artist-mt-get-tag element)))
  1326. (cond ((eq element-tag 'graphics-operation)
  1327. (let* ((info-part (artist-mt-get-info-part element))
  1328. (unshifted (artist-go-get-unshifted info-part))
  1329. (shifted (artist-go-get-shifted info-part))
  1330. (unshifted-kwd (artist-go-get-keyword unshifted))
  1331. (shifted-kwd (artist-go-get-keyword shifted))
  1332. (unshifted-sym (artist-go-get-symbol unshifted))
  1333. (shifted-sym (artist-go-get-symbol shifted)))
  1334. (if (string-equal kwd unshifted-kwd)
  1335. (throw 'found unshifted-sym))
  1336. (if (string-equal kwd shifted-kwd)
  1337. (throw 'found shifted-sym))))
  1338. ((eq element-tag 'function-call)
  1339. (let* ((info-part (artist-mt-get-info-part element))
  1340. (keyword (artist-fc-get-keyword info-part))
  1341. (symbol (artist-fc-get-symbol info-part)))
  1342. (if (string-equal kwd keyword)
  1343. (throw 'found symbol))))
  1344. ((eq element-tag 'menu)
  1345. (let* ((info-part (artist-mt-get-info-part element))
  1346. (items (artist-mn-get-items info-part))
  1347. (answer (artist-mt-get-symbol-from-keyword-sub
  1348. items kwd)))
  1349. (if answer (throw 'found answer))))
  1350. (t
  1351. nil))))
  1352. table)
  1353. nil))
  1354. ;
  1355. ; Retrieving info from a graphics operation symbol
  1356. ;
  1357. (defun artist-go-retrieve-from-symbol (symbol retrieve-fn)
  1358. "Search the master table for a graphics operation SYMBOL.
  1359. Calls RETRIEVE-FN to retrieve information from that symbol's
  1360. info-variant-part."
  1361. (artist-go-retrieve-from-symbol-sub artist-mt symbol retrieve-fn))
  1362. (defun artist-go-retrieve-from-symbol-sub (table symbol retrieve-fn)
  1363. "Search the TABLE for a graphics operation SYMBOL.
  1364. Calls RETRIEVE-FN to retrieve information from that symbol's
  1365. info-variant-part."
  1366. (catch 'found
  1367. (mapc
  1368. (lambda (element)
  1369. (let ((element-tag (artist-mt-get-tag element)))
  1370. (cond ((eq element-tag 'graphics-operation)
  1371. (let* ((info-part (artist-mt-get-info-part element))
  1372. (unshifted (artist-go-get-unshifted info-part))
  1373. (shifted (artist-go-get-shifted info-part))
  1374. (unshifted-sym (artist-go-get-symbol unshifted))
  1375. (shifted-sym (artist-go-get-symbol shifted))
  1376. (variant-part (cond
  1377. ((eq unshifted-sym symbol) unshifted)
  1378. ((eq shifted-sym symbol) shifted)
  1379. (t nil))))
  1380. (if variant-part ; if found do:
  1381. (throw 'found (funcall retrieve-fn variant-part)))))
  1382. ((eq element-tag 'menu)
  1383. (let* ((info-part (artist-mt-get-info-part element))
  1384. (items (artist-mn-get-items info-part))
  1385. (answer (artist-go-retrieve-from-symbol-sub
  1386. items symbol retrieve-fn)))
  1387. (if answer (throw 'found answer)))))))
  1388. table)
  1389. nil))
  1390. (defun artist-go-get-keyword-from-symbol (symbol)
  1391. "Search the master table, get keyword from a graphics operation SYMBOL."
  1392. (artist-go-retrieve-from-symbol symbol 'artist-go-get-keyword))
  1393. (defun artist-go-get-mode-line-from-symbol (symbol)
  1394. "Search the master table, get mode-line from a graphics operation SYMBOL."
  1395. (artist-go-retrieve-from-symbol symbol 'artist-go-get-mode-line))
  1396. (defun artist-go-get-arrow-pred-from-symbol (symbol)
  1397. "Search the master table, get arrow-pred from a graphics operation SYMBOL."
  1398. (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-pred))
  1399. (defun artist-go-get-arrow-set-fn-from-symbol (symbol)
  1400. "Search the master table, get arrow-set-fn from a graphics operation SYMBOL."
  1401. (artist-go-retrieve-from-symbol symbol 'artist-go-get-arrow-set-fn))
  1402. (defun artist-go-get-init-fn-from-symbol (symbol)
  1403. "Search the master table, get init-fn from a graphics operation SYMBOL."
  1404. (artist-go-retrieve-from-symbol symbol 'artist-go-get-init-fn))
  1405. (defun artist-go-get-prep-fill-fn-from-symbol (symbol)
  1406. "Search the master table, get prep-fill-fn from a graphics operation SYMBOL."
  1407. (artist-go-retrieve-from-symbol symbol 'artist-go-get-prep-fill-fn))
  1408. (defun artist-go-get-exit-fn-from-symbol (symbol)
  1409. "Search the master table, get exit-fn from a graphics operation SYMBOL."
  1410. (artist-go-retrieve-from-symbol symbol 'artist-go-get-exit-fn))
  1411. (defun artist-go-get-draw-fn-from-symbol (symbol)
  1412. "Search the master table, get draw-fn from a graphics operation SYMBOL."
  1413. (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-fn))
  1414. (defun artist-go-get-draw-how-from-symbol (symbol)
  1415. "Search the master table, get draw-how from a graphics operation SYMBOL."
  1416. (artist-go-retrieve-from-symbol symbol 'artist-go-get-draw-how))
  1417. (defun artist-go-get-undraw-fn-from-symbol (symbol)
  1418. "Search the master table, get undraw-fn from a graphics operation SYMBOL."
  1419. (artist-go-retrieve-from-symbol symbol 'artist-go-get-undraw-fn))
  1420. (defun artist-go-get-interval-fn-from-symbol (symbol)
  1421. "Search the master table, get interval-fn from a graphics operation SYMBOL."
  1422. (artist-go-retrieve-from-symbol symbol 'artist-go-get-interval-fn))
  1423. (defun artist-go-get-fill-pred-from-symbol (symbol)
  1424. "Search the master table, get fill-pred from a graphics operation SYMBOL."
  1425. (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-pred))
  1426. (defun artist-go-get-fill-fn-from-symbol (symbol)
  1427. "Search the master table, get fill-fn from a graphics operation SYMBOL."
  1428. (artist-go-retrieve-from-symbol symbol 'artist-go-get-fill-fn))
  1429. (defun artist-go-get-symbol-shift (symbol is-shifted)
  1430. "Search for (shifted or unshifted) graphics operation SYMBOL.
  1431. If IS-SHIFTED is non-nil, return the shifted symbol,
  1432. otherwise the unshifted symbol."
  1433. (artist-go-get-symbol-shift-sub artist-mt symbol is-shifted))
  1434. (defun artist-go-get-symbol-shift-sub (table symbol is-shifted)
  1435. "Search TABLE for (shifted or unshifted) graphics SYMBOL.
  1436. If IS-SHIFTED is non-nil, return the shifted symbol,
  1437. otherwise the unshifted symbol."
  1438. (catch 'found
  1439. (mapc
  1440. (lambda (element)
  1441. (let ((element-tag (artist-mt-get-tag element)))
  1442. (cond ((eq element-tag 'graphics-operation)
  1443. (let* ((info-part (artist-mt-get-info-part element))
  1444. (unshift-variant (artist-go-get-unshifted info-part))
  1445. (shift-variant (artist-go-get-shifted info-part))
  1446. (unshift-sym (artist-go-get-symbol unshift-variant))
  1447. (shift-sym (artist-go-get-symbol shift-variant)))
  1448. (if (or (eq symbol unshift-sym) (eq symbol shift-sym))
  1449. (throw 'found (if is-shifted shift-sym unshift-sym)))))
  1450. ((eq element-tag 'menu)
  1451. (let* ((info-part (artist-mt-get-info-part element))
  1452. (items (artist-mn-get-items info-part))
  1453. (answer (artist-go-get-symbol-shift-sub
  1454. items symbol is-shifted)))
  1455. (if answer (throw 'found answer)))))))
  1456. table)
  1457. nil))
  1458. ;
  1459. ; Retrieving info from a function-call symbol
  1460. ;
  1461. (defun artist-fc-retrieve-from-symbol (symbol retrieve-fn)
  1462. "Search the master table for a function call SYMBOL.
  1463. Calls RETRIEVE-FN to retrieve information from that symbol's
  1464. info-variant-part."
  1465. (artist-fc-retrieve-from-symbol-sub artist-mt symbol retrieve-fn))
  1466. (defun artist-fc-retrieve-from-symbol-sub (table symbol retrieve-fn)
  1467. "Search TABLE for a function-call SYMBOL.
  1468. Calls RETRIEVE-FN to retrieve information from that symbol's
  1469. info-variant-part."
  1470. (catch 'found
  1471. (mapc
  1472. (lambda (element)
  1473. (let ((element-tag (artist-mt-get-tag element)))
  1474. (cond ((eq element-tag 'function-call)
  1475. (let* ((info-part (artist-mt-get-info-part element))
  1476. (fc-symbol (artist-fc-get-symbol info-part)))
  1477. (if (eq fc-symbol symbol)
  1478. (throw 'found (funcall retrieve-fn info-part)))))
  1479. ((eq element-tag 'menu)
  1480. (let* ((info-part (artist-mt-get-info-part element))
  1481. (items (artist-mn-get-items info-part))
  1482. (answer (artist-fc-retrieve-from-symbol-sub
  1483. items symbol retrieve-fn)))
  1484. (if answer (throw 'found answer)))))))
  1485. table)
  1486. nil))
  1487. (defun artist-fc-get-fn-from-symbol (symbol)
  1488. "Search the master table to get function from a function call SYMBOL."
  1489. (artist-fc-retrieve-from-symbol symbol 'artist-fc-get-fn))
  1490. ;;
  1491. ;; Utilities
  1492. ;;
  1493. ;; Macro that won't funcall the function if it is nil.
  1494. ;;
  1495. (defmacro artist-funcall (fn &rest args)
  1496. "Call function FN with ARGS, if FN is not nil."
  1497. (list 'if fn (cons 'funcall (cons fn args))))
  1498. (defun artist-uniq (l)
  1499. "Remove consecutive duplicates in list L. Comparison is done with `equal'."
  1500. (cond ((null l) nil)
  1501. ((null (cdr l)) l) ; only one element in list
  1502. ((equal (car l) (car (cdr l))) (artist-uniq (cdr l))) ; first 2 equal
  1503. (t (cons (car l) (artist-uniq (cdr l)))))) ; first 2 are different
  1504. (defun artist-string-split (str r)
  1505. "Split string STR at occurrences of regexp R, returning a list of strings."
  1506. (let ((res nil)
  1507. (start 0)
  1508. (match-pos 0))
  1509. (while (setq match-pos (string-match r str start))
  1510. (setq res (cons (copy-sequence (substring str start match-pos)) res))
  1511. (setq start (match-end 0)))
  1512. (if (null res)
  1513. (list str)
  1514. (if (< (match-end 0) (- (length str) 1))
  1515. (setq res (cons (substring str (match-end 0) (length str)) res)))
  1516. (reverse res))))
  1517. (defun artist-string-to-file (str file-name)
  1518. "Write string STR to file FILE-NAME."
  1519. (write-region str 'end-is-ignored file-name nil 'no-message))
  1520. (defun artist-file-to-string (file-name)
  1521. "Read from file FILE-NAME into a string."
  1522. (save-excursion
  1523. (let ((tmp-buffer (get-buffer-create (concat "*artist-" file-name "*"))))
  1524. (set-buffer tmp-buffer)
  1525. (goto-char (point-min))
  1526. (insert-file-contents file-name nil nil nil t)
  1527. (let ((str (copy-sequence (buffer-substring (point-min)
  1528. (point-max)))))
  1529. (kill-buffer tmp-buffer)
  1530. str))))
  1531. (defun artist-clear-buffer (buf)
  1532. "Clear contents of buffer BUF."
  1533. (with-current-buffer buf
  1534. (goto-char (point-min))
  1535. (delete-char (- (point-max) (point-min)) nil)))
  1536. (defun artist-system (program stdin &optional program-args)
  1537. "Run PROGRAM synchronously with the contents of string STDIN to stdin.
  1538. Optional args PROGRAM-ARGS are arguments to PROGRAM.
  1539. Return a list (RETURN-CODE STDOUT STDERR)."
  1540. (save-excursion
  1541. (let* ((tmp-stdin-file-name (if stdin
  1542. (make-temp-file "artist-stdin.")
  1543. nil))
  1544. (tmp-stdout-buffer (get-buffer-create
  1545. (concat "*artist-" program "*")))
  1546. (tmp-stderr-file-name (make-temp-file "artist-stdout."))
  1547. (binary-process-input nil) ; for msdos
  1548. (binary-process-output nil))
  1549. ;; Prepare stdin
  1550. (if stdin (artist-string-to-file stdin tmp-stdin-file-name))
  1551. ;; Clear the buffer
  1552. (artist-clear-buffer tmp-stdout-buffer)
  1553. ;; Start the program
  1554. (unwind-protect
  1555. (let ((res (if program-args
  1556. (apply 'call-process
  1557. program
  1558. tmp-stdin-file-name
  1559. (list tmp-stdout-buffer
  1560. tmp-stderr-file-name)
  1561. nil
  1562. (if (stringp program-args)
  1563. (list program-args)
  1564. program-args))
  1565. (apply 'call-process
  1566. program
  1567. tmp-stdin-file-name
  1568. (list tmp-stdout-buffer
  1569. tmp-stderr-file-name)
  1570. nil))))
  1571. ;; the return value
  1572. (list res
  1573. (with-current-buffer tmp-stdout-buffer
  1574. (buffer-substring (point-min) (point-max)))
  1575. (artist-file-to-string tmp-stderr-file-name)))
  1576. ;; Unwind: remove temporary files and buffers
  1577. (if (and stdin (file-exists-p tmp-stdin-file-name))
  1578. (delete-file tmp-stdin-file-name))
  1579. (if (file-exists-p tmp-stderr-file-name)
  1580. (delete-file tmp-stderr-file-name))
  1581. (if (memq tmp-stdout-buffer (buffer-list))
  1582. (kill-buffer tmp-stdout-buffer))))))
  1583. ;; Routines that deal with the buffer
  1584. ;;
  1585. ;; artist-current-line get line number (top of buffer is 0)
  1586. ;;
  1587. ;; artist-move-to-xy move to (x,y) (0,0) is beg-of-buffer
  1588. ;;
  1589. ;; artist-get-char-at-xy get char in at (x,y)
  1590. ;;
  1591. ;; artist-replace-char overwrite (replace) char at point
  1592. ;; artist-replace-chars overwrite (replace) chars at point
  1593. ;;
  1594. (defsubst artist-current-column ()
  1595. "Return point's current column."
  1596. (current-column))
  1597. (defsubst artist-current-line ()
  1598. "Return point's current line, buffer-relative. Top of buffer is 0."
  1599. (+ (count-lines 1 (point))
  1600. (if (= (current-column) 0) 1 0)
  1601. -1))
  1602. (defsubst artist-move-to-xy (x y)
  1603. "Move to column X, at row Y from the top of buffer. Top line is 0."
  1604. ;;
  1605. ;; Q: Why do we do forward-line twice?
  1606. ;; A: The documentation for forward-line says
  1607. ;;
  1608. ;; "... Returns the count of lines left to move. ... With
  1609. ;; positive N, a non-empty line at the end counts as one
  1610. ;; line successfully moved (for the return value)."
  1611. ;;
  1612. ;; This means that if we are trying to move forward past the end
  1613. ;; of the buffer, and that last line happened to be longer than
  1614. ;; the current column, then we end up at the end of that last
  1615. ;; line, and forward-line returns one less than we actually
  1616. ;; wanted to move.
  1617. ;;
  1618. ;; Example: In the figure below, the `X' is the very last
  1619. ;; character in the buffer ("a non-empty line at the
  1620. ;; end"). Suppose point is at P. Then (forward-line 1)
  1621. ;; returns 0 and puts point after the `X'.
  1622. ;;
  1623. ;; --------top of buffer--------
  1624. ;;
  1625. ;; P X
  1626. ;; -------bottom of buffer------
  1627. ;;
  1628. ;; But, if we are at the end of buffer when trying to move
  1629. ;; forward, then forward-line will return the (for us) correct
  1630. ;; value, which is good, because we will come to the end of the
  1631. ;; buffer by the first forward-line. The second forward-line
  1632. ;; will then get us where we really wanted to go.
  1633. ;;
  1634. ;; If we are not moving past the end of the buffer, then the
  1635. ;; second forward-line will return 0.
  1636. ;;
  1637. ;; Q: What happens if we are moving upwards?
  1638. ;; A: That will work good. insert-char won't insert a negative
  1639. ;; number of chars, and forward-line will fail silently if we are
  1640. ;; moving past the beginning of the buffer.
  1641. ;;
  1642. (forward-line (- y (artist-current-line)))
  1643. (insert-char ?\n (forward-line (- y (artist-current-line))))
  1644. (move-to-column (max x 0) t)
  1645. (let ((curr-y (artist-current-line)))
  1646. (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y))
  1647. (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y))))
  1648. (defsubst artist-get-char-at-xy (x y)
  1649. "Return the character found at column X, row Y.
  1650. Also updates the variables `artist-draw-min-y' and `artist-draw-max-y'."
  1651. (artist-move-to-xy x y)
  1652. (let ((curr-y (artist-current-line)))
  1653. (setq artist-draw-region-min-y (min curr-y artist-draw-region-min-y))
  1654. (setq artist-draw-region-max-y (max curr-y artist-draw-region-max-y)))
  1655. (following-char))
  1656. (defsubst artist-get-replacement-char (c)
  1657. "Retrieve a replacement for character C from `artist-replacement-table'.
  1658. The replacement is used to convert tabs and new-lines to spaces."
  1659. ;; Characters may be outside the range of the `artist-replacement-table',
  1660. ;; for example if they are Unicode code points >= 256.
  1661. ;; Check so we don't attempt to access the array out of its bounds,
  1662. ;; assuming no such character needs to be replaced.
  1663. (if (< c (length artist-replacement-table))
  1664. (aref artist-replacement-table c)
  1665. c))
  1666. (defun artist-get-char-at-xy-conv (x y)
  1667. "Retrieve the character at X, Y, converting tabs and new-lines to spaces."
  1668. (save-excursion
  1669. (artist-get-replacement-char (artist-get-char-at-xy x y))))
  1670. (defun artist-replace-char (new-char)
  1671. "Replace the character at point with NEW-CHAR."
  1672. (let ((overwrite-mode 'overwrite-mode-textual)
  1673. (fill-column 32765) ; Large :-)
  1674. (blink-matching-paren nil))
  1675. (setq last-command-event (artist-get-replacement-char new-char))
  1676. (self-insert-command 1)))
  1677. (defun artist-replace-chars (new-char count)
  1678. "Replace characters at point with NEW-CHAR. COUNT chars are replaced."
  1679. ;; Check that the variable exists first. The doc says it was added in 19.23.
  1680. (if (and (and (boundp 'emacs-major-version) (= emacs-major-version 20))
  1681. (and (boundp 'emacs-minor-version) (<= emacs-minor-version 3)))
  1682. ;; This is a bug workaround for Emacs 20, versions up to 20.3:
  1683. ;; The self-insert-command doesn't care about the overwrite-mode,
  1684. ;; so the insertion is done in the same way as in picture mode.
  1685. ;; This seems to be a little bit slower.
  1686. (let* ((replaced-c (artist-get-replacement-char new-char))
  1687. (replaced-s (make-string count replaced-c)))
  1688. (artist-move-to-xy (+ (artist-current-column) count)
  1689. (artist-current-line))
  1690. (delete-char (- count))
  1691. (insert replaced-s))
  1692. ;; In emacs-19, the self-insert-command works better
  1693. (let ((overwrite-mode 'overwrite-mode-textual)
  1694. (fill-column 32765) ; Large :-)
  1695. (blink-matching-paren nil))
  1696. (setq last-command-event (artist-get-replacement-char new-char))
  1697. (self-insert-command count))))
  1698. (defsubst artist-replace-string (string &optional see-thru)
  1699. "Replace contents at point with STRING.
  1700. With optional argument SEE-THRU set to non-nil, text in the buffer
  1701. ``shines thru'' blanks in the STRING."
  1702. (let ((char-list (append string nil)) ; convert the string to a list
  1703. (overwrite-mode 'overwrite-mode-textual)
  1704. (fill-column 32765) ; Large :-)
  1705. (blink-matching-paren nil))
  1706. (while char-list
  1707. (let ((c (car char-list)))
  1708. (if (and see-thru (= (artist-get-replacement-char c) ?\s))
  1709. (artist-move-to-xy (1+ (artist-current-column))
  1710. (artist-current-line))
  1711. (artist-replace-char c)))
  1712. (setq char-list (cdr char-list)))))
  1713. ;;
  1714. ;; Routines for setting and unsetting points
  1715. ;; Used when not rubber-banding
  1716. ;;
  1717. (defun artist-no-rb-unset-point1 ()
  1718. "Unsets point 1 when not rubber-banding."
  1719. (let ((x-now (artist-current-column))
  1720. (y-now (artist-current-line))
  1721. (x (aref artist-rb-save-data 0))
  1722. (y (aref artist-rb-save-data 1)))
  1723. (artist-move-to-xy x y)
  1724. (artist-replace-char (aref artist-rb-save-data 2))
  1725. (artist-move-to-xy x-now y-now)))
  1726. (defun artist-no-rb-set-point1 (x y)
  1727. "Set point 1 at X, Y when not rubber-banding."
  1728. (let ((x-now (artist-current-column))
  1729. (y-now (artist-current-line)))
  1730. (aset artist-rb-save-data 0 x)
  1731. (aset artist-rb-save-data 1 y)
  1732. (aset artist-rb-save-data 2 (artist-get-char-at-xy x y))
  1733. (artist-move-to-xy x y)
  1734. (artist-replace-char artist-first-char)
  1735. (artist-move-to-xy x-now y-now)
  1736. (aset artist-rb-save-data 6 0)))
  1737. (defun artist-no-rb-unset-point2 ()
  1738. "This function unsets point 2 when not rubber-banding."
  1739. (if (= (aref artist-rb-save-data 6) 1)
  1740. (let ((x-now (artist-current-column))
  1741. (y-now (artist-current-line))
  1742. (x (aref artist-rb-save-data 3))
  1743. (y (aref artist-rb-save-data 4)))
  1744. (artist-move-to-xy x y)
  1745. (artist-replace-char (aref artist-rb-save-data 5))
  1746. (artist-move-to-xy x-now y-now))))
  1747. (defun artist-no-rb-set-point2 (x y)
  1748. "Set point 2 at X, Y when not rubber-banding."
  1749. (let ((x-now (artist-current-column))
  1750. (y-now (artist-current-line)))
  1751. (aset artist-rb-save-data 3 x)
  1752. (aset artist-rb-save-data 4 y)
  1753. (aset artist-rb-save-data 5 (artist-get-char-at-xy x y))
  1754. (artist-move-to-xy x y)
  1755. (artist-replace-char artist-second-char)
  1756. (artist-move-to-xy x-now y-now)
  1757. (aset artist-rb-save-data 6 1)))
  1758. (defun artist-no-rb-unset-points ()
  1759. "This function unsets point 1 and 2 when not rubber-banding."
  1760. (artist-no-rb-unset-point1)
  1761. (artist-no-rb-unset-point2))
  1762. ;; artist-intersection-char
  1763. ;;
  1764. ;; Note: If changing this, see the notes for artist-unintersection-char
  1765. ;; and artist-vaporize-lines
  1766. ;;
  1767. (defun artist-intersection-char (new-c old-c)
  1768. "Calculates intersection character when drawing a NEW-C on top of an OLD-C.
  1769. Return character according to this scheme:
  1770. OLD-C NEW-C return
  1771. - | +
  1772. | - +
  1773. + | +
  1774. + - +
  1775. \\ / X
  1776. / \\ X
  1777. X / X
  1778. X \\ X
  1779. other combinations NEW-C"
  1780. (cond ((and (= old-c ?- ) (= new-c ?| )) ?+ )
  1781. ((and (= old-c ?| ) (= new-c ?- )) ?+ )
  1782. ((and (= old-c ?+ ) (= new-c ?- )) ?+ )
  1783. ((and (= old-c ?+ ) (= new-c ?| )) ?+ )
  1784. ((and (= old-c ?\\ ) (= new-c ?/ )) ?X )
  1785. ((and (= old-c ?/ ) (= new-c ?\\ )) ?X )
  1786. ((and (= old-c ?X ) (= new-c ?/ )) ?X )
  1787. ((and (= old-c ?X ) (= new-c ?\\ )) ?X )
  1788. (t new-c)))
  1789. ;; artist-unintersection-char
  1790. ;;
  1791. ;; Note: If changing this, see the note for artist-vaporize-lines
  1792. ;;
  1793. (defun artist-unintersection-char (line-c buffer-c)
  1794. "Restore character to before intersection when removing LINE-C from BUFFER-C.
  1795. Return character according to this scheme:
  1796. LINE-C BUFFER-C return
  1797. - + |
  1798. | + -
  1799. \\ X /
  1800. / X \\
  1801. other combinations `artist-erase-char'."
  1802. (cond ((and (= line-c ?- ) (= buffer-c ?+ )) ?| )
  1803. ((and (= line-c ?| ) (= buffer-c ?+ )) ?- )
  1804. ((and (= line-c ?\\ ) (= buffer-c ?X )) ?/ )
  1805. ((and (= line-c ?/ ) (= buffer-c ?X )) ?\\ )
  1806. ((= line-c buffer-c) artist-erase-char)
  1807. (t buffer-c)))
  1808. ;; Computing the line-char to use
  1809. ;; for use with borderless shapes
  1810. ;;
  1811. (defsubst artist-compute-line-char ()
  1812. "Compute which character to use for lines, if any.
  1813. Return value is either nil for the default characters that make up lines, or
  1814. a character chosen depending on the variables `artist-borderless-shapes',
  1815. `artist-fill-char-set', `artist-fill-char' and
  1816. `artist-line-char-set' and `artist-line-char'."
  1817. (if (and artist-borderless-shapes artist-fill-char-set)
  1818. artist-fill-char
  1819. (if artist-line-char-set
  1820. artist-line-char
  1821. nil)))
  1822. ;; Things for drawing horizontal, vertical and diagonal (straight) lines.
  1823. ;;
  1824. ;; A line here is a vector:
  1825. ;; [ start-x start-y length direction saved-char-1 saved-char-2 ... ]
  1826. ;; directions start with 0 at the x-axis and counts anti clockwise.
  1827. ;;
  1828. (defvar artist-direction-info
  1829. ;; x y char
  1830. [ [ 1 0 ?- ] ; direction 0
  1831. [ 1 1 ?\\ ] ; direction 1
  1832. [ 0 1 ?| ] ; direction 2
  1833. [ -1 1 ?/ ] ; direction 3
  1834. [ -1 0 ?- ] ; direction 4
  1835. [ -1 -1 ?\\ ] ; direction 5
  1836. [ 0 -1 ?| ] ; direction 6
  1837. [ 1 -1 ?/ ] ] ; direction 7
  1838. "Table used for stepping x and y coordinates in a specific direction.
  1839. This table is also used for determining which char to use for that direction.")
  1840. (defsubst artist-direction-step-x (direction)
  1841. "Return the x-step for DIRECTION from the `artist-direction-info' table."
  1842. (aref (aref artist-direction-info direction) 0))
  1843. (defsubst artist-direction-step-y (direction)
  1844. "Return the y-step for DIRECTION from the `artist-direction-info' table."
  1845. (aref (aref artist-direction-info direction) 1))
  1846. (defun artist-direction-char (direction)
  1847. "Return the character for DIRECTION from the `artist-direction-info' table."
  1848. (aref (aref artist-direction-info direction) 2))
  1849. ;; artist-find-direction
  1850. ;;
  1851. ;;
  1852. ;;
  1853. (defun artist-find-direction (x1 y1 x2 y2)
  1854. "Find the direction from point X1,Y1 to X2,Y2.
  1855. Returns a DIRECTION, a number 0--7, coded as follows:
  1856. 5 6 7
  1857. \\ | /
  1858. 4 - * - 0
  1859. / | \\
  1860. 3 2 1"
  1861. (let ((delta-x (- x2 x1))
  1862. (delta-y (- y2 y1)))
  1863. (cond ((>= delta-x (* 2 (abs delta-y))) 0)
  1864. ((>= delta-y (* 2 (abs delta-x))) 2)
  1865. ((>= (- delta-x) (* 2 (abs delta-y))) 4)
  1866. ((>= (- delta-y) (* 2 (abs delta-x))) 6)
  1867. ((and (>= delta-x 0) (>= delta-y 0)) 1)
  1868. ((and (<= delta-x 0) (>= delta-y 0)) 3)
  1869. ((and (<= delta-x 0) (<= delta-y 0)) 5)
  1870. ((and (>= delta-x 0) (<= delta-y 0)) 7))))
  1871. (defun artist-straight-calculate-length (direction x1 y1 x2 y2)
  1872. "Calculate length for a straight line in DIRECTION from X1,Y1 to X2,Y2."
  1873. (cond ((or (= direction 7)
  1874. (= direction 0)
  1875. (= direction 1)) (1+ (- x2 x1)))
  1876. ((or (= direction 3)
  1877. (= direction 4)
  1878. (= direction 5)) (1+ (- x1 x2)))
  1879. (t (1+ (abs (- y2 y1))))))
  1880. (defun artist-sline (x1 y1 x2 y2)
  1881. "Create a straight line from X1,Y1 to X2,Y2."
  1882. (let* ((direction (artist-find-direction x1 y1 x2 y2))
  1883. (length (artist-straight-calculate-length direction x1 y1 x2 y2))
  1884. (line (make-vector (+ length 4) x1)))
  1885. ;; not needed:
  1886. ;; (aset line 0 x1)
  1887. ;; because we set all elements to x1
  1888. (aset line 1 y1)
  1889. (aset line 2 length)
  1890. (aset line 3 direction)
  1891. line))
  1892. (defun artist-save-chars-under-sline (line)
  1893. "Save characters under a LINE."
  1894. (let ((x (aref line 0))
  1895. (y (aref line 1))
  1896. (length (+ (aref line 2) 4))
  1897. (direction (aref line 3))
  1898. (i 4))
  1899. (while (< i length)
  1900. (aset line i (artist-get-char-at-xy x y))
  1901. (setq x (+ x (artist-direction-step-x direction)))
  1902. (setq y (+ y (artist-direction-step-y direction)))
  1903. (setq i (1+ i))))
  1904. line)
  1905. ;; Things for drawing lines in all directions.
  1906. ;; The line drawing engine is the eight-point algorithm.
  1907. ;;
  1908. ;; A line is here a list of (x y saved-char new-char)s.
  1909. ;;
  1910. (defvar artist-octant-info
  1911. ;; Initial Step in Step in
  1912. ;; coeffs x and y x and y
  1913. ;; for if q >= 0 if g < 0
  1914. ;; dfdx,dfdy
  1915. [ [ 2 1 1 0 1 1 ] ; 1st octant
  1916. [ 1 2 1 1 0 1 ] ; 2nd octant
  1917. [ -1 2 0 1 -1 1 ] ; 3rd octant
  1918. [ -2 1 -1 1 -1 0 ] ; 4th octant
  1919. [ -2 -1 -1 0 -1 -1 ] ; 5th octant
  1920. [ -1 -2 -1 -1 0 -1 ] ; 6th octant
  1921. [ 1 -2 0 -1 1 -1 ] ; 7th octant
  1922. [ 2 -1 1 -1 1 0 ] ] ; 8th octant
  1923. "Table used by line drawing algorithm (eight point).")
  1924. ;; Primitives for the artist-octant-info.
  1925. ;; Decrease octant by 1 since elt counts from 0 and octant counts from 1.
  1926. ;;
  1927. (defsubst artist-get-dfdx-init-coeff (octant)
  1928. "Retrieve dfdx component for OCTANT."
  1929. (aref (aref artist-octant-info (- octant 1)) 0))
  1930. (defsubst artist-get-dfdy-init-coeff (octant)
  1931. "Retrieve dfdy component for OCTANT."
  1932. (aref (aref artist-octant-info (- octant 1)) 1))
  1933. (defsubst artist-get-x-step-q>=0 (octant)
  1934. "Retrieve x-step component for OCTANT when q >= 0."
  1935. (aref (aref artist-octant-info (- octant 1)) 2))
  1936. (defsubst artist-get-y-step-q>=0 (octant)
  1937. "Retrieve y-step component for OCTANT when q >= 0."
  1938. (aref (aref artist-octant-info (- octant 1)) 3))
  1939. (defsubst artist-get-x-step-q<0 (octant)
  1940. "Retrieve x-step component for OCTANT for q < 0."
  1941. (aref (aref artist-octant-info (- octant 1)) 4))
  1942. (defsubst artist-get-y-step-q<0 (octant)
  1943. "Retrieve y-step component for OCTANT for q < 0."
  1944. (aref (aref artist-octant-info (- octant 1)) 5))
  1945. ;; Find octant from x1 y1 x2 y2 coordinates.
  1946. ;;
  1947. (defun artist-find-octant (x1 y1 x2 y2)
  1948. "Find octant for a line from X1,Y1 to X2,Y2.
  1949. Octant are numbered 1--8, anti-clockwise as:
  1950. \\3|2/
  1951. 4\\|/1
  1952. ---+---
  1953. 5/|\\8
  1954. /6|7\\"
  1955. (if (<= x1 x2) ; quadrant 1 or 4
  1956. (if (<= y1 y2) ; quadrant 1, octant 1 or 2
  1957. (if (>= (- x2 x1) (- y2 y1))
  1958. 1
  1959. 2)
  1960. (if (>= (- x2 x1) (- (- y2 y1))) ; quadrant 4, octant 7 or 8
  1961. 8
  1962. 7))
  1963. (if (<= y1 y2) ; quadrant 2 or 3
  1964. (if (>= (- (- x2 x1)) (- y2 y1)) ; quadrant 2, octant 3 or 4
  1965. 4
  1966. 3)
  1967. (if (>= (- (- x2 x1)) (- (- y2 y1))) ; quadrant 3, octant 5 or 6
  1968. 5
  1969. 6))))
  1970. ;; Some inline functions for creating, setting and reading
  1971. ;; members of a coordinate
  1972. ;;
  1973. (defsubst artist-new-coord (x y &optional new-char)
  1974. "Create a new coordinate at X,Y for use in a line.
  1975. Optional argument NEW-CHAR can be used for setting the new-char component
  1976. in the coord."
  1977. (let ((coord (make-vector 4 x)))
  1978. (aset coord 1 y)
  1979. (aset coord 3 new-char)
  1980. coord))
  1981. (defsubst artist-coord-get-x (coord)
  1982. "Retrieve the x component of a COORD."
  1983. (aref coord 0))
  1984. (defsubst artist-coord-get-y (coord)
  1985. "Retrieve the y component of a COORD."
  1986. (aref coord 1))
  1987. (defsubst artist-coord-set-x (coord new-x)
  1988. "Set the x component of a COORD to NEW-X."
  1989. (aset coord 0 new-x)
  1990. coord)
  1991. (defsubst artist-coord-set-y (coord new-y)
  1992. "Set the y component of a COORD to NEW-Y."
  1993. (aset coord 1 new-y)
  1994. coord)
  1995. (defsubst artist-coord-get-saved-char (coord)
  1996. "Retrieve the saved char component of a COORD."
  1997. (aref coord 2))
  1998. (defsubst artist-coord-get-new-char (coord)
  1999. "Retrieve the new char component of a COORD."
  2000. (aref coord 3))
  2001. (defsubst artist-coord-add-saved-char (coord saved-char)
  2002. "Set the saved char component of a COORD to SAVED-CHAR."
  2003. (aset coord 2 saved-char)
  2004. coord)
  2005. (defsubst artist-coord-add-new-char (coord new-char)
  2006. "Set the new char component of a COORD to NEW-CHAR."
  2007. (aset coord 3 new-char)
  2008. coord)
  2009. (defsubst artist-coord-set-new-char (coord new-char)
  2010. "Set the new char component of a COORD to NEW-CHAR."
  2011. (aset coord 3 new-char)
  2012. coord)
  2013. ;; Pretend we are plotting a pixel. Instead we just list it
  2014. ;;
  2015. (defmacro artist-put-pixel (point-list x y)
  2016. "In POINT-LIST, store a ``pixel'' at coord X,Y."
  2017. (list 'setq point-list
  2018. (list 'append point-list (list 'list (list 'artist-new-coord x y)))))
  2019. ;; Calculate list of points using eight point algorithm
  2020. ;; return a list of coords
  2021. ;;
  2022. (defun artist-eight-point (x1 y1 x2 y2)
  2023. "Run the eight-point algorithm to get a list of coords from X1,Y1 to X2,Y2."
  2024. (let* ((point-list nil)
  2025. (octant (artist-find-octant x1 y1 x2 y2))
  2026. (dfdx-coeff (artist-get-dfdx-init-coeff octant))
  2027. (dfdy-coeff (artist-get-dfdy-init-coeff octant))
  2028. (x-step-q>=0 (artist-get-x-step-q>=0 octant))
  2029. (y-step-q>=0 (artist-get-y-step-q>=0 octant))
  2030. (x-step-q<0 (artist-get-x-step-q<0 octant))
  2031. (y-step-q<0 (artist-get-y-step-q<0 octant))
  2032. (dfdx (- (- y2 y1)))
  2033. (dfdy (- x2 x1))
  2034. (x x1)
  2035. (y y1)
  2036. (f 0)
  2037. (q (+ (* 2 f)
  2038. (* dfdx-coeff dfdx)
  2039. (* dfdy-coeff dfdy))))
  2040. (artist-put-pixel point-list x y)
  2041. (while (or (not (eq x x2)) (not (eq y y2)))
  2042. (if (>= q 0)
  2043. (progn
  2044. (setq x (+ x x-step-q>=0))
  2045. (setq y (+ y y-step-q>=0))
  2046. (setq f (+ f (* x-step-q>=0 dfdx) (* y-step-q>=0 dfdy))))
  2047. (progn
  2048. (setq x (+ x x-step-q<0))
  2049. (setq y (+ y y-step-q<0))
  2050. (setq f (+ f (* x-step-q<0 dfdx) (* y-step-q<0 dfdy)))))
  2051. (setq q (+ (* 2 f) (* dfdx-coeff dfdx) (* dfdy-coeff dfdy)))
  2052. (artist-put-pixel point-list x y))
  2053. point-list))
  2054. ;; artist-save-chars-under-point-list
  2055. ;; Remembers the chars that were there before we did draw the line.
  2056. ;; Returns point-list.
  2057. ;;
  2058. (defun artist-save-chars-under-point-list (point-list)
  2059. "Save characters originally under POINT-LIST."
  2060. (mapcar
  2061. (lambda (coord)
  2062. (artist-coord-add-saved-char
  2063. coord
  2064. (artist-get-char-at-xy (artist-coord-get-x coord)
  2065. (artist-coord-get-y coord))))
  2066. point-list))
  2067. ;; artist-calculate-new-char, artist-calculate-new-chars
  2068. ;; Calculates which char to insert depending on direction of point-list.
  2069. ;;
  2070. ;; Depending on new-coord's position relative to last-coord one of the
  2071. ;; following chars are returned: \ | / - o, as indicated by this:
  2072. ;;
  2073. ;; \ | /
  2074. ;; - o -
  2075. ;; / | \
  2076. ;;
  2077. ;; artist-calculate-new-char works on one coordinate, returns char.
  2078. ;; artist-calculate-new-chars works on a point-list, returns point-list.
  2079. ;;
  2080. (defun artist-calculate-new-char (last-coord new-coord)
  2081. "Return a line-char to use when moving from LAST-COORD to NEW-COORD."
  2082. (let ((last-x (artist-coord-get-x last-coord))
  2083. (last-y (artist-coord-get-y last-coord))
  2084. (new-x (artist-coord-get-x new-coord))
  2085. (new-y (artist-coord-get-y new-coord)))
  2086. (cond ((> new-x last-x) (cond ((< new-y last-y) ?/ )
  2087. ((> new-y last-y) ?\\ )
  2088. (t ?- )))
  2089. ((< new-x last-x) (cond ((< new-y last-y) ?\\ )
  2090. ((> new-y last-y) ?/ )
  2091. (t ?- )))
  2092. ((eq new-y last-y) ?o)
  2093. (t ?| ))))
  2094. (defun artist-calculate-new-chars (point-list)
  2095. "Return a list of coords with line-chars calculated. Input: POINT-LIST."
  2096. (if (null (cdr point-list))
  2097. (list (artist-coord-add-new-char (car point-list) ?o ))
  2098. (let ((last-coord (car point-list)))
  2099. (cons (artist-coord-add-new-char
  2100. (car point-list)
  2101. (artist-calculate-new-char (car (cdr point-list))
  2102. (car point-list)))
  2103. (mapcar
  2104. (lambda (this-coord)
  2105. (prog1
  2106. (artist-coord-add-new-char
  2107. this-coord
  2108. (artist-calculate-new-char last-coord this-coord))
  2109. (setq last-coord this-coord)))
  2110. (cdr point-list))))))
  2111. ;; artist-modify-new-chars
  2112. ;; Replaces some characters with some other characters.
  2113. ;;
  2114. ;; artist-modify-new-chars works on a point-list, returns point-list.
  2115. ;;
  2116. (defun artist-modify-new-chars (point-list)
  2117. "Replace intersecting characters in POINT-LIST.
  2118. This function returns a point-list."
  2119. (mapcar
  2120. (lambda (coord)
  2121. (let* ((new-c (artist-coord-get-new-char coord))
  2122. (saved-c (artist-coord-get-saved-char coord))
  2123. (modified-c (artist-intersection-char new-c saved-c)))
  2124. (artist-coord-set-new-char coord modified-c)))
  2125. point-list))
  2126. ;;
  2127. ;; functions for accessing endpoints and elements in object requiring
  2128. ;; 2 endpoints
  2129. ;;
  2130. (defun artist-make-endpoint (x y)
  2131. "Create an endpoint at X, Y."
  2132. (let ((new-endpoint (make-vector 2 x)))
  2133. (aset new-endpoint 1 y)
  2134. new-endpoint))
  2135. (defun artist-endpoint-get-x (endpoint)
  2136. "Retrieve the x component of an ENDPOINT."
  2137. (aref endpoint 0))
  2138. (defun artist-endpoint-get-y (endpoint)
  2139. "Retrieve the y component of an ENDPOINT."
  2140. (aref endpoint 1))
  2141. (defun artist-make-2point-object (endpoint1 endpoint2 shapeinfo)
  2142. "Create a 2-point object of ENDPOINT1, ENDPOINT2 and SHAPEINFO."
  2143. (list endpoint1 endpoint2 shapeinfo))
  2144. (defun artist-2point-get-endpoint1 (obj)
  2145. "Retrieve the first endpoint of a 2-point object OBJ."
  2146. (elt obj 0))
  2147. (defun artist-2point-get-endpoint2 (obj)
  2148. "Retrieve the second endpoint of a 2-point object OBJ."
  2149. (elt obj 1))
  2150. (defun artist-2point-get-shapeinfo (obj)
  2151. "Retrieve the shapeinfo component of a 2-point object OBJ."
  2152. (elt obj 2))
  2153. ;;
  2154. ;; Drawing and undrawing lines (any direction)
  2155. ;;
  2156. (defun artist-draw-line (x1 y1 x2 y2)
  2157. "Draw a line from X1, Y1 to X2, Y2.
  2158. Output is a line, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
  2159. END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
  2160. SHAPE-INFO is a list of vectors [X Y SAVED-CHAR NEW-CHAR]."
  2161. (let ((endpoint1 (artist-make-endpoint x1 y1))
  2162. (endpoint2 (artist-make-endpoint x2 y2)))
  2163. (artist-make-2point-object
  2164. endpoint1
  2165. endpoint2
  2166. (mapcar
  2167. (lambda (coord)
  2168. (artist-move-to-xy (artist-coord-get-x coord)
  2169. (artist-coord-get-y coord))
  2170. (if artist-line-char-set
  2171. (artist-replace-char artist-line-char)
  2172. (artist-replace-char (artist-coord-get-new-char coord)))
  2173. coord)
  2174. (artist-modify-new-chars
  2175. (artist-calculate-new-chars
  2176. (artist-save-chars-under-point-list
  2177. (artist-eight-point x1 y1 x2 y2))))))))
  2178. (defun artist-undraw-line (line)
  2179. "Undraw LINE."
  2180. (mapcar
  2181. (lambda (coord)
  2182. (artist-move-to-xy (artist-coord-get-x coord)
  2183. (artist-coord-get-y coord))
  2184. (artist-replace-char (artist-coord-get-saved-char coord))
  2185. coord)
  2186. (artist-2point-get-shapeinfo line)))
  2187. ;;
  2188. ;; Drawing and undrawing straight lines
  2189. ;;
  2190. (defun artist-draw-sline (x1 y1 x2 y2)
  2191. "Draw a straight line from X1, Y1 to X2, Y2.
  2192. Straight lines are vertical, horizontal or diagonal lines.
  2193. They are faster to draw and most often they are what you need
  2194. when drawing a simple image.
  2195. Output is a straight line, which is a list on the form
  2196. \(END-POINT-1 END-POINT-2 SHAPE-INFO).
  2197. END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
  2198. SHAPE-INFO is a vector [START-X START-Y LENGTH-OF-LINE DIRECTION
  2199. ORIGINAL-CHAR-1 ORIGINAL-CHAR-2 ... ]."
  2200. (let* ((line (artist-save-chars-under-sline (artist-sline x1 y1 x2 y2)))
  2201. (x (aref line 0))
  2202. (y (aref line 1))
  2203. (length (+ (aref line 2) 4))
  2204. (direction (aref line 3))
  2205. (line-char (artist-direction-char direction))
  2206. (i 4)
  2207. (endpoint1 (artist-make-endpoint x y))
  2208. (endpoint2 nil))
  2209. (while (< i length)
  2210. (artist-move-to-xy x y)
  2211. (if artist-line-char-set
  2212. (artist-replace-char artist-line-char)
  2213. (artist-replace-char (artist-intersection-char
  2214. line-char
  2215. (aref line i))))
  2216. (if (not (< (1+ i) length))
  2217. ;; This is the last element. Set the second endpoint
  2218. (setq endpoint2 (artist-make-endpoint x y)))
  2219. (setq x (+ x (artist-direction-step-x direction)))
  2220. (setq y (+ y (artist-direction-step-y direction)))
  2221. (setq i (1+ i)))
  2222. (artist-make-2point-object endpoint1 endpoint2 line)))
  2223. (defun artist-undraw-sline (line)
  2224. "Undraw a straight line LINE."
  2225. (if line
  2226. (let* ((shape-info (artist-2point-get-shapeinfo line))
  2227. (x (aref shape-info 0))
  2228. (y (aref shape-info 1))
  2229. (length (+ (aref shape-info 2) 4))
  2230. (direction (aref shape-info 3))
  2231. (i 4))
  2232. (while (< i length)
  2233. (artist-move-to-xy x y)
  2234. (artist-replace-char (aref shape-info i))
  2235. (setq x (+ x (artist-direction-step-x direction)))
  2236. (setq y (+ y (artist-direction-step-y direction)))
  2237. (setq i (1+ i))))))
  2238. ;;
  2239. ;; Drawing and undrawing rectangles and squares
  2240. ;;
  2241. (defun artist-draw-rect (x1 y1 x2 y2)
  2242. "Draw a rectangle with corners at X1, Y1 and X2, Y2.
  2243. Output is a rectangle, which is a list on the form
  2244. \(END-POINT-1 END-POINT-2 SHAPE-INFO).
  2245. END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
  2246. SHAPE-INFO is a list of four straight lines."
  2247. (let* ((artist-line-char (artist-compute-line-char))
  2248. (artist-line-char-set artist-line-char)
  2249. (line1 (artist-draw-sline x1 y1 x2 y1))
  2250. (line2 (artist-draw-sline x2 y1 x2 y2))
  2251. (line3 (artist-draw-sline x2 y2 x1 y2))
  2252. (line4 (artist-draw-sline x1 y2 x1 y1))
  2253. (endpoint1 (artist-make-endpoint x1 y1))
  2254. (endpoint2 (artist-make-endpoint x2 y2)))
  2255. (artist-make-2point-object endpoint1
  2256. endpoint2
  2257. (list line1 line2 line3 line4))))
  2258. (defun artist-undraw-rect (rectangle)
  2259. "Undraw RECTANGLE."
  2260. (if rectangle
  2261. (let ((shape-info (artist-2point-get-shapeinfo rectangle)))
  2262. (artist-undraw-sline (elt shape-info 3))
  2263. (artist-undraw-sline (elt shape-info 2))
  2264. (artist-undraw-sline (elt shape-info 1))
  2265. (artist-undraw-sline (elt shape-info 0)))))
  2266. (defun artist-rect-corners-squarify (x1 y1 x2 y2)
  2267. "Compute square corners from rectangle corners at X1, Y1 and X2, Y2.
  2268. The square's first corner will be X1, Y1. The position of the second
  2269. corner depends on which of X2 and Y2 is most far away from X1, Y1."
  2270. (let* ((delta-x (- x2 x1))
  2271. (delta-y (- y2 y1))
  2272. (delta-x-sign (if (< delta-x 0) -1 1))
  2273. (delta-y-sign (if (< delta-y 0) -1 1))
  2274. (new-x2) ; set below
  2275. (new-y2)) ; set below
  2276. ;; Check which of x2 and y2 is most distant
  2277. ;; take care to the aspect ratio
  2278. (if (> (abs delta-x) (abs delta-y))
  2279. ;; *** x2 more distant than y2 (with care taken to aspect ratio)
  2280. (progn
  2281. (setq new-x2 x2)
  2282. (setq new-y2 (+ y1 (round (/ (* (abs delta-x) delta-y-sign)
  2283. artist-aspect-ratio)))))
  2284. ;; *** y2 more distant than x2 (with care taken to aspect ratio)
  2285. (progn
  2286. (setq new-x2 (round (+ x1 (* (* (abs delta-y) delta-x-sign)
  2287. artist-aspect-ratio))))
  2288. (setq new-y2 y2)))
  2289. ;; Return this
  2290. (list x1 y1 new-x2 new-y2)))
  2291. (defun artist-draw-square (x1 y1 x2 y2)
  2292. "Draw a square with corners at X1, Y1 and X2, Y2.
  2293. Output is a square, which is a list on the form
  2294. \(END-POINT-1 END-POINT-2 SHAPE-INFO).
  2295. END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
  2296. SHAPE-INFO is a list of four straight lines."
  2297. (let* ((artist-line-char (artist-compute-line-char))
  2298. (artist-line-char-set artist-line-char)
  2299. (square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
  2300. (new-x1 (elt square-corners 0))
  2301. (new-y1 (elt square-corners 1))
  2302. (new-x2 (elt square-corners 2))
  2303. (new-y2 (elt square-corners 3))
  2304. (endpoint1 (artist-make-endpoint new-x1 new-y1))
  2305. (endpoint2 (artist-make-endpoint new-x2 new-y2))
  2306. (line1 (artist-draw-sline new-x1 new-y1 new-x2 new-y1))
  2307. (line2 (artist-draw-sline new-x2 new-y1 new-x2 new-y2))
  2308. (line3 (artist-draw-sline new-x2 new-y2 new-x1 new-y2))
  2309. (line4 (artist-draw-sline new-x1 new-y2 new-x1 new-y1)))
  2310. (artist-make-2point-object endpoint1
  2311. endpoint2
  2312. (list line1 line2 line3 line4))))
  2313. (defun artist-undraw-square (square)
  2314. "Undraw SQUARE."
  2315. (if square
  2316. (let ((shape-info (artist-2point-get-shapeinfo square)))
  2317. (artist-undraw-sline (elt shape-info 3))
  2318. (artist-undraw-sline (elt shape-info 2))
  2319. (artist-undraw-sline (elt shape-info 1))
  2320. (artist-undraw-sline (elt shape-info 0)))))
  2321. ;;
  2322. ;; Filling rectangles and squares
  2323. ;;
  2324. (defun artist-fill-rect (rect x1 y1 x2 y2)
  2325. "Fill rectangle RECT from X1,Y1 to X2,Y2."
  2326. (let ((x (1+ (min x1 x2)))
  2327. (y (1+ (min y1 y2)))
  2328. (x-max (max x1 x2))
  2329. (y-max (max y1 y2)))
  2330. (let ((w (- x-max x)))
  2331. (while (< y y-max)
  2332. (artist-move-to-xy x y)
  2333. (artist-replace-chars artist-fill-char w)
  2334. (setq y (1+ y))))))
  2335. (defun artist-fill-square (square x1 y1 x2 y2)
  2336. "Fill a SQUARE from X1,Y1 to X2,Y2."
  2337. (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
  2338. (new-x1 (elt square-corners 0))
  2339. (new-y1 (elt square-corners 1))
  2340. (new-x2 (elt square-corners 2))
  2341. (new-y2 (elt square-corners 3))
  2342. (x (1+ (min new-x1 new-x2)))
  2343. (y (1+ (min new-y1 new-y2)))
  2344. (x-max (max new-x1 new-x2))
  2345. (y-max (max new-y1 new-y2))
  2346. (w (- x-max x)))
  2347. (while (< y y-max)
  2348. (artist-move-to-xy x y)
  2349. (artist-replace-chars artist-fill-char w)
  2350. (setq y (1+ y)))))
  2351. ;;
  2352. ;; Pen drawing
  2353. ;;
  2354. (defun artist-pen (x1 y1)
  2355. "Draw a character at X1, Y1.
  2356. The character is replaced with the character in `artist-fill-char'."
  2357. (artist-move-to-xy x1 y1)
  2358. (artist-replace-char (if artist-line-char-set
  2359. artist-line-char
  2360. (if artist-fill-char-set
  2361. artist-fill-char
  2362. artist-default-fill-char))))
  2363. (defun artist-pen-line (x1 y1)
  2364. "Draw a line from last pen position to X1, Y1.
  2365. The character is replaced with the character in `artist-fill-char'.
  2366. This will store all points in `artist-key-poly-point-list' in reversed
  2367. order (I assume it is faster to cons to the beginning of the list than
  2368. to append to the end of the list, when doing free-hand drawing)."
  2369. (let ((artist-line-char (if artist-line-char-set
  2370. artist-line-char
  2371. (if artist-fill-char-set
  2372. artist-fill-char
  2373. artist-default-fill-char))))
  2374. ;; Draw line from last point to this
  2375. (let ((x-last (car (car artist-key-poly-point-list)))
  2376. (y-last (cdr (car artist-key-poly-point-list))))
  2377. (artist-move-to-xy x-last y-last)
  2378. (artist-replace-char artist-line-char)
  2379. (artist-draw-line x-last y-last x1 y1))
  2380. ;; Update the point-list
  2381. (setq artist-key-poly-point-list
  2382. (cons (cons x1 y1) artist-key-poly-point-list))))
  2383. (defun artist-pen-reset-last-xy (x1 y1)
  2384. "Reset the last x and y points to X1, Y1 when doing pen-drawing."
  2385. (artist-clear-arrow-points)
  2386. (setq artist-key-poly-point-list (list (cons x1 y1))))
  2387. (defun artist-pen-set-arrow-points (x1 y1)
  2388. "Set arrow points for pen drawing using X1, Y1.
  2389. Also, the `artist-key-poly-point-list' is reversed."
  2390. (setq artist-key-poly-point-list
  2391. (artist-uniq artist-key-poly-point-list))
  2392. (if (>= (length artist-key-poly-point-list) 2)
  2393. ;; Only set arrow-points if the point-list has two or more entries
  2394. (let ((xn (car (car artist-key-poly-point-list)))
  2395. (yn (cdr (car artist-key-poly-point-list)))
  2396. (xn-1 (car (car (cdr artist-key-poly-point-list))))
  2397. (yn-1 (cdr (car (cdr artist-key-poly-point-list))))
  2398. (dirn)) ; direction for point n
  2399. (setq artist-key-poly-point-list (reverse artist-key-poly-point-list))
  2400. (let ((x0 (car (car artist-key-poly-point-list)))
  2401. (y0 (cdr (car artist-key-poly-point-list)))
  2402. (x1 (car (car (cdr artist-key-poly-point-list))))
  2403. (y1 (cdr (car (cdr artist-key-poly-point-list))))
  2404. (dir0)) ; direction for point 0
  2405. (setq dir0 (artist-find-direction x1 y1 x0 y0))
  2406. (setq dirn (artist-find-direction xn-1 yn-1 xn yn))
  2407. (setq artist-arrow-point-1 (artist-make-arrow-point x0 y0 dir0))
  2408. (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn))))))
  2409. ;;
  2410. ;; Text rendering
  2411. ;;
  2412. (defun artist-figlet-run (text font extra-args)
  2413. "Run figlet rendering TEXT using FONT.
  2414. EXTRA-ARGS for figlet, for the command line, may be specified."
  2415. (let* ((figlet-args (cond ((and font extra-args)
  2416. (cons (concat "-f" font)
  2417. (artist-string-split extra-args "[ \t]+")))
  2418. (font (concat "-f" font))
  2419. (extra-args
  2420. (artist-string-split extra-args "[ \t]+"))
  2421. (t nil)))
  2422. (figlet-output (artist-system artist-figlet-program text figlet-args))
  2423. (exit-code (elt figlet-output 0))
  2424. (stdout (elt figlet-output 1))
  2425. (stderr (elt figlet-output 2)))
  2426. (if (not (= exit-code 0))
  2427. (error "Failed to render font: %s (%d)" stderr exit-code))
  2428. stdout))
  2429. (defun artist-figlet-get-font-list ()
  2430. "Read fonts in with the shell command.
  2431. Returns a list of strings."
  2432. (let* ((cmd-interpreter "/bin/sh")
  2433. (ls-cmd artist-figlet-list-fonts-command)
  2434. (result (artist-system cmd-interpreter ls-cmd nil))
  2435. (exit-code (elt result 0))
  2436. (stdout (elt result 1))
  2437. (stderr (elt result 2)))
  2438. (if (not (= exit-code 0))
  2439. (error "Failed to read available fonts: %s (%d)" stderr exit-code))
  2440. (artist-string-split stdout ".flf\n")))
  2441. (defun artist-figlet-choose-font ()
  2442. "Read any extra arguments for figlet."
  2443. (interactive)
  2444. (let* ((avail-fonts (artist-figlet-get-font-list))
  2445. (font (completing-read (concat "Select font (default "
  2446. artist-figlet-default-font
  2447. "): ")
  2448. (mapcar
  2449. (lambda (font) (cons font font))
  2450. avail-fonts))))
  2451. (if (string= font "") artist-figlet-default-font font)))
  2452. (defun artist-figlet-get-extra-args ()
  2453. "Read any extra arguments for figlet."
  2454. (let ((extra-args (read-string "Extra args to figlet: ")))
  2455. (if (string= extra-args "")
  2456. nil
  2457. extra-args)))
  2458. (defun artist-figlet (text)
  2459. "Render TEXT using figlet."
  2460. (let* ((figlet-font (artist-figlet-choose-font))
  2461. (figlet-extra-args (artist-figlet-get-extra-args)))
  2462. (artist-figlet-run text figlet-font figlet-extra-args)))
  2463. (defun artist-text-insert-common (x y text see-thru)
  2464. "At position X, Y, insert text TEXT.
  2465. If SEE-THRU is non-nil, then blanks in TEXT do not replace text
  2466. in the buffer."
  2467. (let* ((string-list (artist-string-split text "\n"))
  2468. (i 0)
  2469. (len (length string-list)))
  2470. (while (< i len)
  2471. (artist-move-to-xy x (+ y i))
  2472. (artist-replace-string (car string-list) see-thru)
  2473. (setq string-list (cdr string-list))
  2474. (setq i (1+ i)))))
  2475. (defun artist-text-insert-see-thru (x y text)
  2476. "At position X, Y, insert text TEXT.
  2477. Let text already in buffer shine thru the TEXT inserted."
  2478. (artist-text-insert-common x y text t))
  2479. (defun artist-text-insert-overwrite (x y text)
  2480. "At position X, Y, insert text TEXT.
  2481. Let blanks in TEXT overwrite any text already in the buffer."
  2482. (artist-text-insert-common x y text nil))
  2483. (defun artist-text-see-thru (x y)
  2484. "Prompt for text to render, render it at X,Y.
  2485. This is done by calling the function specified by
  2486. `artist-text-renderer-function', which must return a list of strings,
  2487. to be inserted in the buffer.
  2488. Text already in the buffer ``shines thru'' blanks in the rendered text."
  2489. (let* ((input-text (read-string "Type text to render: "))
  2490. (rendered-text (artist-funcall artist-text-renderer-function input-text)))
  2491. (artist-text-insert-see-thru x y rendered-text)))
  2492. (defun artist-text-overwrite (x y)
  2493. "Prompt for text to render, render it at X,Y.
  2494. This is done by calling the function specified by
  2495. `artist-text-renderer-function', which must return a list of strings,
  2496. to be inserted in the buffer.
  2497. Blanks in the rendered text overwrite any text in the buffer."
  2498. (let* ((input-text (read-string "Type text to render: "))
  2499. (rendered-text (artist-funcall artist-text-renderer-function input-text)))
  2500. (artist-text-insert-overwrite x y rendered-text)))
  2501. ;;
  2502. ;; Spraying
  2503. ;;
  2504. (defun artist-spray-get-interval ()
  2505. "Retrieve the interval for repeated spray."
  2506. artist-spray-interval)
  2507. (defun artist-spray-random-points (n radius)
  2508. "Generate N random points within a radius of RADIUS.
  2509. Returns a list of points. Each point is on the form (X1 . Y1)."
  2510. (let ((points))
  2511. (while (> n 0)
  2512. (let* ((angle (* (random 359) (/ float-pi 180)))
  2513. (dist (random radius))
  2514. (point (cons (round (* dist (cos angle)))
  2515. (round (* dist (sin angle))))))
  2516. (setq points (cons point points)))
  2517. (setq n (- n 1)))
  2518. points))
  2519. (defun artist-spray (x1 y1)
  2520. "Spray at X1, Y1."
  2521. (let* ((num-points (* artist-spray-radius artist-spray-radius))
  2522. (spray-points (artist-spray-random-points num-points
  2523. artist-spray-radius)))
  2524. (while spray-points
  2525. ;; Replace one spray point
  2526. (let* ((point (car spray-points))
  2527. (x (+ x1 (car point)))
  2528. (y (+ y1 (cdr point)))
  2529. (buf-c (artist-get-char-at-xy-conv x y))
  2530. (this-c (memq buf-c artist-spray-chars))
  2531. (next-c (cond ((null this-c) artist-spray-new-char)
  2532. ((null (cdr this-c)) (car this-c))
  2533. (t (car (cdr this-c))))))
  2534. (artist-move-to-xy x y)
  2535. (artist-replace-char next-c))
  2536. ;; Step to next spray point
  2537. (setq spray-points (cdr spray-points)))))
  2538. (defun artist-spray-clear-circle (circle x1 y1 x2 y2)
  2539. "Clear circle CIRCLE at X1, Y1 through X2, Y2."
  2540. (artist-undraw-circle circle))
  2541. (defun artist-spray-set-radius (circle x1 y1 x2 y2)
  2542. "Set spray radius from CIRCLE at X1, Y1 through X2, Y2."
  2543. (let ((dx (- x2 x1))
  2544. (dy (- y2 y1)))
  2545. (setq artist-spray-radius (round (sqrt (+ (* dx dx) (* dy dy)))))
  2546. (if (= 0 artist-spray-radius)
  2547. (setq artist-spray-radius 1))))
  2548. ;;
  2549. ;; Erasing
  2550. ;;
  2551. (defun artist-erase-char (x1 y1)
  2552. "Erase a character at X1, Y1.
  2553. The character is replaced with the character in `artist-erase-char'."
  2554. (artist-move-to-xy x1 y1)
  2555. (artist-replace-char artist-erase-char))
  2556. (defun artist-erase-rect (rect x1 y1 x2 y2)
  2557. "Erase rectangle RECT from X1, Y1, X2, Y2."
  2558. (let ((artist-line-char-set t)
  2559. (artist-fill-char-set t)
  2560. (artist-line-char artist-erase-char)
  2561. (artist-fill-char artist-erase-char))
  2562. (artist-draw-rect x1 y1 x2 y2)
  2563. (artist-fill-rect rect x1 y1 x2 y2)))
  2564. ;;
  2565. ;; Vaporizing (erasing) line and lines
  2566. ;;
  2567. (defun artist-vap-find-endpoint (x1 y1 step-x step-y accept-set reject-set)
  2568. "Find one endpoint for line through X1, Y1.
  2569. The endpoint is searched for in the direction defined by STEP-X, STEP-Y,
  2570. accepting characters in the list ACCEPT-SET, stopping immediately
  2571. when finding characters in the list REJECT-SET. Fuzziness, that is
  2572. the number of consecutive characters not in ACCEPT-SET to allow as
  2573. part of the line, is determined by the variable `artist-vaporize-fuzziness'.
  2574. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  2575. (let ((x x1)
  2576. (y y1)
  2577. (x-last x1)
  2578. (y-last y1)
  2579. (done nil))
  2580. (while (not done)
  2581. (let ((c (artist-get-char-at-xy-conv x y)))
  2582. (cond ((memq c reject-set)
  2583. (setq done t))
  2584. ;; We found a character we are accepting as part of the line.
  2585. ;; Update position
  2586. ((memq c accept-set)
  2587. (setq x-last x
  2588. y-last y
  2589. x (+ x step-x)
  2590. y (+ y step-y))
  2591. (if (or (< x 0) (< y 0)) ;stop at the edge
  2592. (setq done t)))
  2593. ;; We found a character we are not accepting as part of
  2594. ;; the line Search `artist-vaporize-fuzziness'
  2595. ;; characters away from this position in the same
  2596. ;; direction to see if there are any characters in the
  2597. ;; accept-set. If not, we have found the endpoint.
  2598. (t
  2599. (let ((fuzziness artist-vaporize-fuzziness)
  2600. (x-tmp x)
  2601. (y-tmp y))
  2602. ;; while we have more fuzziness left and we have not
  2603. ;; found a character accepted as a line, move
  2604. ;; forward!
  2605. (while (and (> fuzziness 0) (not (memq c accept-set)))
  2606. (setq x-tmp (+ x-tmp step-x))
  2607. (setq y-tmp (+ y-tmp step-y))
  2608. (setq c (artist-get-char-at-xy-conv x-tmp y-tmp))
  2609. (setq fuzziness (- fuzziness 1)))
  2610. (if (memq c accept-set)
  2611. ;; The line continues on the other side of the
  2612. ;; not-accepted character.
  2613. (setq x x-tmp
  2614. y y-tmp)
  2615. ;; Else: We couldn't find any line on the other side.
  2616. ;; That means we are done searching for the endpoint.
  2617. (setq done t)))))))
  2618. (cons x-last y-last)))
  2619. (defun artist-vap-find-endpoints-horiz (x y)
  2620. "Find endpoints for a horizontal line through X, Y.
  2621. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  2622. (list (artist-vap-find-endpoint x y 1 0 '(?- ?+) '(?\s))
  2623. (artist-vap-find-endpoint x y -1 0 '(?- ?+) '(?\s))))
  2624. (defun artist-vap-find-endpoints-vert (x y)
  2625. "Find endpoints for a vertical line through X, Y.
  2626. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  2627. (list (artist-vap-find-endpoint x y 0 1 '(?| ?+) '(?\s))
  2628. (artist-vap-find-endpoint x y 0 -1 '(?| ?+) '(?\s))))
  2629. (defun artist-vap-find-endpoints-swne (x y)
  2630. "Find endpoints for a diagonal line (made by /'s) through X, Y.
  2631. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  2632. (list (artist-vap-find-endpoint x y 1 -1 '(?/ ?X) '(?\s))
  2633. (artist-vap-find-endpoint x y -1 1 '(?/ ?X) '(?\s))))
  2634. (defun artist-vap-find-endpoints-nwse (x y)
  2635. "Find endpoints for a diagonal line (made by \\'s) through X, Y.
  2636. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  2637. (list (artist-vap-find-endpoint x y 1 1 '(?\\ ?X) '(?\s))
  2638. (artist-vap-find-endpoint x y -1 -1 '(?\\ ?X) '(?\s))))
  2639. (defun artist-vap-find-endpoints (x y)
  2640. "Given a point X1, Y1, return a list of endpoints of lines through X, Y.
  2641. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  2642. (if artist-line-char-set
  2643. nil
  2644. (let ((c (artist-get-char-at-xy-conv x y)))
  2645. (cond ((eq c ?-) (artist-vap-find-endpoints-horiz x y))
  2646. ((eq c ?|) (artist-vap-find-endpoints-vert x y))
  2647. ((eq c ?/) (artist-vap-find-endpoints-swne x y))
  2648. ((eq c ?\\) (artist-vap-find-endpoints-nwse x y))
  2649. ((eq c ?+) (append (artist-vap-find-endpoints-horiz x y)
  2650. (artist-vap-find-endpoints-vert x y)))
  2651. ((eq c ?X) (append (artist-vap-find-endpoints-swne x y)
  2652. (artist-vap-find-endpoints-nwse x y)))
  2653. ;; We don't know how to find directions when we are on
  2654. ;; another character
  2655. (t nil)))))
  2656. (defun artist-vap-group-in-pairs (l)
  2657. "Group elements in list L in pairs."
  2658. (cond ((null l) nil)
  2659. ((null (cdr l)) l) ; unevent number of elements in list
  2660. (t (append (list (list (car l) (car (cdr l))))
  2661. (artist-vap-group-in-pairs (cdr (cdr l)))))))
  2662. (defun artist-vaporize-by-endpoints (endpoint1 endpoint2)
  2663. "Given ENDPOINT1 and ENDPOINT2, vaporize the line between them.
  2664. An endpoint is a pair (X . Y)."
  2665. (let* ((x1 (car endpoint1))
  2666. (y1 (cdr endpoint1))
  2667. (x2 (car endpoint2))
  2668. (y2 (cdr endpoint2))
  2669. (dir (artist-find-direction x1 y1 x2 y2))
  2670. (x-step (aref [1 1 0 -1 -1 -1 0 1] dir))
  2671. (y-step (aref [0 1 1 1 0 -1 -1 -1] dir))
  2672. (line-c (aref [?- ?\\ ?| ?/ ?- ?\\ ?| ?/] dir))
  2673. (line-len (elt (list (abs (- x2 x1))
  2674. (abs (- x2 x1))
  2675. (abs (- y2 y1))
  2676. (abs (- y2 y1))
  2677. (abs (- x1 x2))
  2678. (abs (- x1 x2))
  2679. (abs (- y1 y2))
  2680. (abs (- y1 y2)))
  2681. dir))
  2682. (x x1)
  2683. (y y1))
  2684. (while (>= line-len 0)
  2685. (let* ((buffer-c (artist-get-char-at-xy-conv x y))
  2686. (new-c (artist-unintersection-char line-c buffer-c)))
  2687. (artist-move-to-xy x y)
  2688. (artist-replace-char new-c))
  2689. (setq x (+ x x-step)
  2690. y (+ y y-step)
  2691. line-len (- line-len 1)))))
  2692. (defun artist-vaporize-line (x1 y1)
  2693. "Vaporize (erase) the straight line through X1, Y1.
  2694. Do this by replacing the characters that forms the line with
  2695. `artist-erase-char'. Output is a list of endpoints for lines through
  2696. X1, Y1. An endpoint is a cons pair, (ENDPOINT-X . ENDPOINT-Y)."
  2697. (let ((endpoints (artist-vap-find-endpoints x1 y1)))
  2698. (mapc
  2699. (lambda (endpoints)
  2700. (let ((ep1 (car endpoints))
  2701. (ep2 (car (cdr endpoints))))
  2702. (artist-vaporize-by-endpoints ep1 ep2)))
  2703. (artist-vap-group-in-pairs endpoints))
  2704. endpoints))
  2705. ;; Implementation note: This depends on artist-vaporize-line doing
  2706. ;; unintersections of intersecting lines.
  2707. ;;
  2708. ;; Example:
  2709. ;; Suppose the buffer looks like this and that we start vaporizing
  2710. ;; lines at (3,0) (at the ``*'').
  2711. ;;
  2712. ;; 0123456
  2713. ;; 0+--*--+
  2714. ;; 1| |
  2715. ;; 2| |
  2716. ;; 3+-----+
  2717. ;;
  2718. ;; We will then push (0,0) and (6,0) on the stack, and vaporize the
  2719. ;; topmost horizontal line:
  2720. ;;
  2721. ;; 0123456
  2722. ;; 0| |
  2723. ;; 1| |
  2724. ;; 2| |
  2725. ;; 3+-----+
  2726. ;;
  2727. ;; We will then pop (0,0) and remove the left-most vertical line while
  2728. ;; pushing the lower left corner (0,3) on the stack, and so on until
  2729. ;; the entire rectangle is vaporized.
  2730. ;;
  2731. ;; Now, What if the `+' in the upper left and upper right corners,
  2732. ;; had not been changed to `|' but to spaces instead? We would
  2733. ;; have failed when popping (0,0) and vaporizing that line because
  2734. ;; we wouldn't find any line at (0,0):
  2735. ;;
  2736. ;; 0123456
  2737. ;; 0
  2738. ;; 1| |
  2739. ;; 2| |
  2740. ;; 3+-----+
  2741. ;;
  2742. ;; That's why we depend on artist-vaporize-line doing unintersecting
  2743. ;; of crossing lines. There are alternative ways to handle this
  2744. ;; if it becomes too much a trouble.
  2745. ;;
  2746. (defun artist-vaporize-lines (x1 y1)
  2747. "Vaporize lines reachable from point X1, Y1."
  2748. (let ((ep-stack nil))
  2749. (mapc
  2750. (lambda (ep) (push ep ep-stack))
  2751. (artist-vap-find-endpoints x1 y1))
  2752. (while (not (null ep-stack))
  2753. (let* ((vaporize-point (pop ep-stack))
  2754. (new-endpoints (artist-vaporize-line (car vaporize-point)
  2755. (cdr vaporize-point))))
  2756. (mapc
  2757. (lambda (endpoint) (push endpoint ep-stack))
  2758. new-endpoints)))))
  2759. ;;
  2760. ;; Circles and ellipses
  2761. ;;
  2762. (defun artist-ellipse-generate-quadrant (x-radius y-radius)
  2763. "Create a point-list for first quadrant.
  2764. Points go from (X-RADIUS, 0) to (0, Y-RADIUS).
  2765. Quadrant is generated around origin."
  2766. (let* ((rx2 (* x-radius x-radius))
  2767. (ry2 (* y-radius y-radius))
  2768. (2rx2 (* 2 rx2))
  2769. (2ry2 (* 2 ry2))
  2770. (p)
  2771. (x 0)
  2772. (y y-radius)
  2773. (px 0)
  2774. (py (* 2rx2 y))
  2775. (point-list nil))
  2776. (artist-put-pixel point-list x y)
  2777. (setq p (round (+ ry2 (- (* rx2 y-radius)) (* 0.25 rx2))))
  2778. (while (< px py)
  2779. (setq x (1+ x)
  2780. px (+ px 2ry2))
  2781. (if (< p 0)
  2782. (setq p (+ p ry2 px))
  2783. (setq y (- y 1)
  2784. py (- py 2rx2)
  2785. p (+ p ry2 px (- py))))
  2786. (artist-put-pixel point-list x y))
  2787. (setq p (round (+ (* ry2 (+ x 0.5) (+ x 0.5))
  2788. (* rx2 (- y 1) (- y 1))
  2789. (- (* rx2 ry2)))))
  2790. (while (> y 0)
  2791. (setq y (- y 1)
  2792. py (- py 2rx2))
  2793. (if (> p 0)
  2794. (setq p (+ p rx2 (- py)))
  2795. (setq x (1+ x)
  2796. px (+ px 2ry2)
  2797. p (+ p rx2 (- py) px)))
  2798. (artist-put-pixel point-list x y))
  2799. point-list))
  2800. (defsubst artist-new-fill-item (x y width)
  2801. "Create a new item at X, Y, with WIDTH.
  2802. This is for use in fill-info in ellipses and circles."
  2803. (let ((new-item (make-vector 3 x)))
  2804. (aset new-item 1 y)
  2805. (aset new-item 2 width)
  2806. new-item))
  2807. (defsubst artist-fill-item-get-x (fill-item)
  2808. "Retrieve the x component of a FILL-ITEM."
  2809. (aref fill-item 0))
  2810. (defsubst artist-fill-item-set-x (fill-item new-x)
  2811. "Set the x component of a FILL-ITEM to NEW-X."
  2812. (aset fill-item 0 new-x)
  2813. fill-item)
  2814. (defsubst artist-fill-item-get-y (fill-item)
  2815. "Retrieve the y component of a FILL-ITEM."
  2816. (aref fill-item 1))
  2817. (defsubst artist-fill-item-set-y (fill-item new-y)
  2818. "Set the y component of a FILL-ITEM to NEW-Y."
  2819. (aset fill-item 1 new-y)
  2820. fill-item)
  2821. (defsubst artist-fill-item-get-width (fill-item)
  2822. "Retrieve the width component of a FILL-ITEM."
  2823. (aref fill-item 2))
  2824. (defsubst artist-fill-item-set-width (fill-item new-width)
  2825. "Set the width component of a FILL-ITEM to NEW-WIDTH."
  2826. (aset fill-item 2 new-width)
  2827. fill-item)
  2828. (defun artist-ellipse-point-list-add-center (x-center y-center point-list)
  2829. "Add offsets X-CENTER and Y-CENTER to coordinates in POINT-LIST."
  2830. (mapcar
  2831. (lambda (p)
  2832. (artist-coord-set-x p (+ x-center (artist-coord-get-x p)))
  2833. (artist-coord-set-y p (+ y-center (artist-coord-get-y p))))
  2834. point-list))
  2835. (defun artist-ellipse-fill-info-add-center (x-center y-center fill-info)
  2836. "Add offsets X-CENTER and Y-CENTER to fill-items in FILL-INFO."
  2837. (mapcar
  2838. (lambda (p)
  2839. (artist-fill-item-set-x p (+ x-center (artist-fill-item-get-x p)))
  2840. (artist-fill-item-set-y p (+ y-center (artist-fill-item-get-y p))))
  2841. fill-info))
  2842. (defun artist-ellipse-remove-0-fills (fill-info)
  2843. "Remove fill-infos from FILL-INFO that fills a zero-width field."
  2844. (cond ((null fill-info)
  2845. nil)
  2846. ((= 0 (artist-fill-item-get-width (car fill-info)))
  2847. (artist-ellipse-remove-0-fills (cdr fill-info)))
  2848. (t
  2849. (append (list (car fill-info))
  2850. (artist-ellipse-remove-0-fills (cdr fill-info))))))
  2851. (defun artist-ellipse-compute-fill-info (point-list)
  2852. "Compute fill info for ellipse around 0,0 from POINT-LIST.
  2853. The POINT-LIST is expected to cover the first quadrant."
  2854. (let ((first-half nil)
  2855. (both-halves nil)
  2856. (last-y nil))
  2857. ;; Create first half (the lower one (since y grows downwards)) from
  2858. ;; the first quadrant.
  2859. (mapc
  2860. (lambda (coord)
  2861. (let* ((x (artist-coord-get-x coord))
  2862. (y (artist-coord-get-y coord))
  2863. (width (max (- (* 2 x) 1) 0))
  2864. (left-edge (- x width)))
  2865. (if (or (null last-y) (not (= y last-y)))
  2866. ;; This was either the first time,
  2867. ;; or it was the first time on a new line
  2868. (setq first-half
  2869. (append first-half
  2870. ;; Fill info item starts at left-edge on line y
  2871. (list (artist-new-fill-item left-edge y width)))))
  2872. (setq last-y y)))
  2873. point-list)
  2874. ;; Create the other half by mirroring the first half.
  2875. (setq both-halves
  2876. (append first-half
  2877. (mapc
  2878. (lambda (i)
  2879. (artist-new-fill-item (artist-fill-item-get-x i)
  2880. (- (artist-fill-item-get-y i))
  2881. (artist-fill-item-get-width i)))
  2882. ;; The cdr below is so we don't include fill-info for
  2883. ;;; the middle line twice
  2884. (cdr (reverse first-half)))))
  2885. (artist-ellipse-remove-0-fills both-halves)))
  2886. (defun artist-ellipse-mirror-quadrant (point-list)
  2887. "Mirror a POINT-LIST describing first quadrant to create a complete ellipse."
  2888. (let ((right-half nil)
  2889. (left-half nil))
  2890. ;; First, if last char in that quadrant is `/', then replace it with `)'
  2891. ;; This way we avoids things
  2892. ;; --------- ---------
  2893. ;; / \ / \
  2894. ;; that look like: \ / instead we get: ( )
  2895. ;; \ / \ /
  2896. ;; --------- ---------
  2897. (let ((last-coord (car (last point-list))))
  2898. (if (= (artist-coord-get-new-char last-coord) ?/)
  2899. (artist-coord-set-new-char last-coord artist-ellipse-right-char)))
  2900. ;; Create the other part of the right half by mirroring the first part
  2901. (setq right-half
  2902. (append
  2903. point-list
  2904. (mapcar
  2905. (lambda (coord)
  2906. (let ((c (artist-coord-get-new-char coord)))
  2907. (artist-new-coord (artist-coord-get-x coord)
  2908. (- (artist-coord-get-y coord))
  2909. (cond ((= c ?/) ?\\)
  2910. ((= c ?\\) ?/)
  2911. (t c)))))
  2912. ;; The cdr below is so we don't draw the middle right char twice
  2913. (cdr (reverse point-list)))))
  2914. ;; Create the left half by mirroring the right half.
  2915. (setq left-half
  2916. (mapcar
  2917. (lambda (coord)
  2918. (let ((c (artist-coord-get-new-char coord)))
  2919. (artist-new-coord (- (artist-coord-get-x coord))
  2920. (artist-coord-get-y coord)
  2921. (cond ((= c ?/) ?\\)
  2922. ((= c ?\\) ?/)
  2923. ((= c artist-ellipse-right-char)
  2924. artist-ellipse-left-char)
  2925. (t c)))))
  2926. ;; The cdr and butlast below is so we don't draw the middle top
  2927. ;; and middle bottom char twice.
  2928. (butlast (cdr (reverse right-half)))))
  2929. (append right-half left-half)))
  2930. (defun artist-draw-ellipse-general (x1 y1 x-radius y-radius)
  2931. "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS.
  2932. Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
  2933. END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
  2934. SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
  2935. POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
  2936. FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE].
  2937. Ellipses with zero Y-RADIUS are not drawn correctly."
  2938. (let* ((point-list (artist-ellipse-generate-quadrant x-radius y-radius))
  2939. (fill-info (artist-ellipse-compute-fill-info point-list))
  2940. (shape-info (make-vector 2 0)))
  2941. (setq point-list (artist-calculate-new-chars point-list))
  2942. (setq point-list (artist-ellipse-mirror-quadrant point-list))
  2943. (setq point-list (artist-ellipse-point-list-add-center x1 y1 point-list))
  2944. (setq fill-info (artist-ellipse-fill-info-add-center x1 y1 fill-info))
  2945. ;; Draw the ellipse
  2946. (setq point-list
  2947. (mapcar
  2948. (lambda (coord)
  2949. (artist-move-to-xy (artist-coord-get-x coord)
  2950. (artist-coord-get-y coord))
  2951. (if artist-line-char-set
  2952. (artist-replace-char artist-line-char)
  2953. (artist-replace-char (artist-coord-get-new-char coord)))
  2954. coord)
  2955. (artist-modify-new-chars
  2956. (artist-save-chars-under-point-list point-list))))
  2957. (aset shape-info 0 point-list)
  2958. (aset shape-info 1 fill-info)
  2959. (artist-make-2point-object (artist-make-endpoint x1 y1)
  2960. (artist-make-endpoint x-radius y-radius)
  2961. shape-info)))
  2962. (defun artist-draw-ellipse-with-0-height (x1 y1 x-radius y-radius)
  2963. "Draw an ellipse with center at X1, Y1 and X-RADIUS and Y-RADIUS.
  2964. Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
  2965. END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
  2966. SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
  2967. POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
  2968. FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE].
  2969. The Y-RADIUS must be 0, but the X-RADIUS must not be 0."
  2970. (let ((point-list nil)
  2971. (width (max (- (abs (* 2 x-radius)) 1)))
  2972. (left-edge (1+ (- x1 (abs x-radius))))
  2973. (line-char (if artist-line-char-set artist-line-char ?-))
  2974. (i 0)
  2975. (point-list nil)
  2976. (fill-info nil)
  2977. (shape-info (make-vector 2 0)))
  2978. (while (< i width)
  2979. (let* ((line-x (+ left-edge i))
  2980. (line-y y1)
  2981. (new-coord (artist-new-coord line-x line-y)))
  2982. (artist-coord-add-saved-char new-coord
  2983. (artist-get-char-at-xy line-x line-y))
  2984. (artist-move-to-xy line-x line-y)
  2985. (artist-replace-char line-char)
  2986. (setq point-list (append point-list (list new-coord)))
  2987. (setq i (1+ i))))
  2988. (aset shape-info 0 point-list)
  2989. (aset shape-info 1 fill-info)
  2990. (artist-make-2point-object (artist-make-endpoint x1 y1)
  2991. (artist-make-endpoint x-radius y-radius)
  2992. shape-info)))
  2993. (defun artist-draw-ellipse (x1 y1 x2 y2)
  2994. "Draw an ellipse with center at X1, Y1 and point X2,Y2.
  2995. Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
  2996. END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
  2997. SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
  2998. POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
  2999. FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]."
  3000. (let* ((artist-line-char (artist-compute-line-char))
  3001. (artist-line-char-set artist-line-char)
  3002. (width (abs (- x2 x1)))
  3003. (height (abs (- y2 y1)))
  3004. ;;
  3005. ;; When we draw our ellipse, we want it to go through the cursor
  3006. ;; position, but since x1,y1, x2,y2 marks the corners of one
  3007. ;; of the quadrants, we have to enlarge the ellipse a bit.
  3008. ;; Ok, so then why by sqrt(2)?
  3009. ;; It comes from the equation for the ellipse (where a is the
  3010. ;; x-radius and b is the y-radius):
  3011. ;; f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0
  3012. ;; and the fact that we want the enlarged ellipse to have the
  3013. ;; same proportions as the smaller square, therefore we have:
  3014. ;; a/b = x/y
  3015. ;; Solving this yields a-in-larger-ellipse = a-in-smaller * sqrt(2)
  3016. (x-radius (round (* width (sqrt 2))))
  3017. (y-radius (round (* height (sqrt 2))))
  3018. (x x1)
  3019. (y y1))
  3020. (if (and (= y1 y2) (not (= x1 x2)))
  3021. (artist-draw-ellipse-with-0-height x y x-radius y-radius)
  3022. (artist-draw-ellipse-general x y x-radius y-radius))))
  3023. (defun artist-undraw-ellipse (ellipse)
  3024. "Undraw ELLIPSE."
  3025. (if ellipse
  3026. (let ((point-list (aref (artist-2point-get-shapeinfo ellipse) 0)))
  3027. (mapcar
  3028. (lambda (coord)
  3029. (artist-move-to-xy (artist-coord-get-x coord)
  3030. (artist-coord-get-y coord))
  3031. (artist-replace-char (artist-coord-get-saved-char coord))
  3032. coord)
  3033. point-list))))
  3034. (defun artist-draw-circle (x1 y1 x2 y2)
  3035. "Draw a circle with center at X1, Y1 and point X2,Y2.
  3036. Output is an ellipse, which is a list (END-POINT-1 END-POINT-2 SHAPE-INFO).
  3037. END-POINT-1 and END-POINT-2 are two-element vectors on the form [X Y].
  3038. SHAPE-INFO is a two-element vector on the form [POINT-LIST FILL-INFO].
  3039. POINT-LIST is a list of vectors on the form [X Y SAVED-CHAR NEW-CHAR].
  3040. FILL-INFO is a list of vectors on the form [X Y ELLIPSE-WIDTH-ON-THIS-LINE]."
  3041. (let* ((artist-line-char (artist-compute-line-char))
  3042. (artist-line-char-set artist-line-char)
  3043. (width (abs (- x2 x1)))
  3044. (height (abs (- y2 y1)))
  3045. ;; When drawing our circle, we want it to through the cursor
  3046. ;; just as when drawing the ellipse, but we have to take
  3047. ;; care for the aspect-ratio.
  3048. ;; The equation for the ellipse (where a is the x-radius and
  3049. ;; b is the y-radius):
  3050. ;; f(x,y) = x^2 / a^2 + y^2 / b^2 - 1 = 0
  3051. ;; together with the relationship
  3052. ;; a = aspect-ratio * b
  3053. ;; gives
  3054. ;; a = sqrt( x^2 + (aspect-ratio * y)^2 ) and
  3055. ;; b = a / aspect-ratio
  3056. (x-radius (round (sqrt (+ (* width width)
  3057. (* (* artist-aspect-ratio height)
  3058. (* artist-aspect-ratio height))))))
  3059. (y-radius (round (/ x-radius artist-aspect-ratio))))
  3060. (artist-draw-ellipse-general x1 y1 x-radius y-radius)))
  3061. (defalias 'artist-undraw-circle 'artist-undraw-ellipse)
  3062. ;
  3063. ; Filling ellipses
  3064. ;
  3065. (defun artist-fill-ellipse (ellipse x y x-radius y-radius)
  3066. "Fill an ELLIPSE centered at X,Y with radius X-RADIUS and Y-RADIUS."
  3067. (let ((fill-info (aref (artist-2point-get-shapeinfo ellipse) 1)))
  3068. (mapcar
  3069. (lambda (fill-item)
  3070. (artist-move-to-xy (artist-fill-item-get-x fill-item)
  3071. (artist-fill-item-get-y fill-item))
  3072. (artist-replace-chars artist-fill-char
  3073. (artist-fill-item-get-width fill-item))
  3074. fill-item)
  3075. fill-info)))
  3076. (defalias 'artist-fill-circle 'artist-fill-ellipse)
  3077. ;;
  3078. ;; Cutting, copying and pasting rectangles and squares
  3079. ;; (filling functions)
  3080. ;;
  3081. (defun artist-cut-rect (rect x1 y1 x2 y2)
  3082. "Copy rectangle RECT drawn from X1, Y1 to X2, Y2, then clear it."
  3083. (artist-undraw-rect rect)
  3084. (artist-copy-generic x1 y1 x2 y2)
  3085. (artist-erase-rect rect x1 y1 x2 y2))
  3086. (defun artist-cut-square (square x1 y1 x2 y2)
  3087. "Copy a SQUARE drawn from X1, Y1 to X2, Y2 (made square), then clears it."
  3088. (artist-undraw-square square)
  3089. (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
  3090. (new-x1 (elt square-corners 0))
  3091. (new-y1 (elt square-corners 1))
  3092. (new-x2 (elt square-corners 2))
  3093. (new-y2 (elt square-corners 3)))
  3094. (artist-copy-generic new-x1 new-y1 new-x2 new-y2)
  3095. (artist-erase-rect square new-x1 new-y1 new-x2 new-y2)))
  3096. (defun artist-get-buffer-contents-at-xy (x y width)
  3097. "Retrieve contents from the buffer at X, Y. WIDTH characters are returned."
  3098. (artist-move-to-xy x y)
  3099. (let ((here (point))
  3100. (there (save-excursion (artist-move-to-xy (+ x width) y) (point))))
  3101. (untabify here there)
  3102. (setq there (save-excursion (artist-move-to-xy (+ x width) y) (point)))
  3103. (buffer-substring here there)))
  3104. (defun artist-copy-generic (x1 y1 x2 y2)
  3105. "Copy a rectangular area with corners at X1, Y1 and X2, Y2.
  3106. Output is a copy buffer, a list of strings, representing the
  3107. original contents of that area in the buffer."
  3108. (let* ((x (min x1 x2))
  3109. (y (min y1 y2))
  3110. (x-max (max x1 x2))
  3111. (y-max (max y1 y2))
  3112. (w (+ (- x-max x) 1))
  3113. (l nil))
  3114. (while (<= y y-max)
  3115. (setq l (cons (artist-get-buffer-contents-at-xy x y w) l))
  3116. (setq y (1+ y)))
  3117. (if artist-interface-with-rect
  3118. (setq killed-rectangle (reverse l))
  3119. (setq artist-copy-buffer (reverse l)))))
  3120. (defun artist-copy-rect (rect x1 y1 x2 y2)
  3121. "Copy rectangle RECT drawn from X1, Y1 to X2, Y2."
  3122. (artist-undraw-rect rect)
  3123. (artist-copy-generic x1 y1 x2 y2))
  3124. (defun artist-copy-square (square x1 y1 x2 y2)
  3125. "Copy a SQUARE drawn from X1, Y1 to X2, Y2 (but made square)."
  3126. (artist-undraw-square square)
  3127. (let* ((square-corners (artist-rect-corners-squarify x1 y1 x2 y2))
  3128. (new-x1 (elt square-corners 0))
  3129. (new-y1 (elt square-corners 1))
  3130. (new-x2 (elt square-corners 2))
  3131. (new-y2 (elt square-corners 3)))
  3132. (artist-copy-generic new-x1 new-y1 new-x2 new-y2)))
  3133. (defun artist-paste (x y)
  3134. "Paste the contents of the copy-buffer at X,Y."
  3135. (let ((copy-buf (if artist-interface-with-rect
  3136. killed-rectangle
  3137. artist-copy-buffer)))
  3138. (if (not (null copy-buf))
  3139. (while (not (null copy-buf))
  3140. (artist-move-to-xy x y)
  3141. (artist-replace-string (car copy-buf))
  3142. (setq copy-buf (cdr copy-buf))
  3143. (setq y (1+ y)))
  3144. (message "Nothing to paste"))))
  3145. ;;
  3146. ;; Flood filling
  3147. ;;
  3148. (defun artist-ff-too-far-right (x)
  3149. "Determine if the position X is too far to the right."
  3150. (cond ((numberp artist-flood-fill-right-border)
  3151. (> x artist-flood-fill-right-border))
  3152. ((eq artist-flood-fill-right-border 'window-width)
  3153. (> x (- (window-width) 2)))
  3154. ((eq artist-flood-fill-right-border 'fill-column)
  3155. (> x fill-column))
  3156. (t (error "Invalid value for `artist-flood-fill-right-border'"))))
  3157. (defun artist-ff-get-rightmost-from-xy (x y)
  3158. "Find the rightmost position in this run, starting at X, Y."
  3159. (save-excursion
  3160. (let ((char-at-xy (artist-get-char-at-xy-conv x y))
  3161. (last-x x))
  3162. (setq x (1+ x))
  3163. (while (and (not (artist-ff-too-far-right x))
  3164. (= char-at-xy (artist-get-char-at-xy-conv x y)))
  3165. (setq last-x x)
  3166. (setq x (1+ x)))
  3167. last-x)))
  3168. (defun artist-ff-is-topmost-line (x y)
  3169. "Determine whether the position X,Y is on the topmost line or not."
  3170. (= y 0))
  3171. (defun artist-ff-is-bottommost-line (x y)
  3172. "Determine whether the position X,Y is on the bottommost line or not."
  3173. (save-excursion
  3174. (goto-char (point-max))
  3175. (beginning-of-line)
  3176. (let ((last-line (artist-current-line)))
  3177. (if (= (point) (point-max))
  3178. ;; Last line is empty, don't paint on it, report previous line
  3179. ;; as last line
  3180. (>= y (- last-line 1))
  3181. (>= y last-line)))))
  3182. (defun artist-flood-fill (x1 y1)
  3183. "Flood-fill starting at X1, Y1. Fill with the char in `artist-fill-char'."
  3184. (let ((stack nil)
  3185. (input-queue nil)
  3186. ;; We are flood-filling the area that has this character.
  3187. (c (artist-get-char-at-xy-conv x1 y1))
  3188. (artist-fill-char (if artist-fill-char-set
  3189. artist-fill-char
  3190. artist-default-fill-char)))
  3191. ;; Fill only if the fill-char is not the same as the character whose
  3192. ;; area we are about to fill, or, in other words, don't fill if we
  3193. ;; needn't.
  3194. (if (not (= c artist-fill-char))
  3195. (push (artist-new-coord x1 y1) stack))
  3196. (while (not (null stack))
  3197. (let* ((coord (pop stack))
  3198. (x (artist-coord-get-x coord))
  3199. (y (artist-coord-get-y coord))
  3200. ;; Here we keep track of the leftmost and rightmost position
  3201. ;; for this run
  3202. (x-leftmost 0)
  3203. (x-rightmost 0)
  3204. (last-x 0)
  3205. ;; Remember if line above and below are accessible
  3206. ;; Lines below the last one, and prior to the first-one
  3207. ;; are not accessible.
  3208. (lines-above nil)
  3209. (lines-below nil)
  3210. ;; Remember char for position on line above and below, so we
  3211. ;; can find the rightmost positions on the runs.
  3212. (last-c-above -1)
  3213. (last-c-below -1))
  3214. (setq x-rightmost (artist-ff-get-rightmost-from-xy x y))
  3215. (setq lines-above (not (artist-ff-is-topmost-line x y)))
  3216. (setq lines-below (not (artist-ff-is-bottommost-line x y)))
  3217. (setq last-x x-rightmost)
  3218. (setq x x-rightmost)
  3219. ;; Search line above, push rightmost positions of runs for that line
  3220. (while (and (>= x 0) (= c (artist-get-char-at-xy-conv x y)))
  3221. (if lines-above
  3222. (let ((c-above (artist-get-char-at-xy-conv x (- y 1))))
  3223. (if (and (= c-above c) (/= c-above last-c-above))
  3224. (push (artist-new-coord x (- y 1)) stack))
  3225. (setq last-c-above c-above)))
  3226. (setq last-x x)
  3227. (setq x (- x 1)))
  3228. ;; Remember the left-most position on this run
  3229. (setq x-leftmost last-x)
  3230. ;; Search line below, push rightmost positions of runs for that line
  3231. (setq x x-rightmost)
  3232. (while (>= x x-leftmost)
  3233. (if lines-below
  3234. (let ((c-below (artist-get-char-at-xy-conv x (1+ y))))
  3235. (if (and (= c-below c) (/= c-below last-c-below))
  3236. (push (artist-new-coord x (1+ y)) stack))
  3237. (setq last-c-below c-below)))
  3238. (setq x (- x 1)))
  3239. (artist-move-to-xy x-leftmost y)
  3240. (artist-replace-chars artist-fill-char (1+ (- x-rightmost x-leftmost)))
  3241. ;; If we are to show incrementally, we have to remove any pending
  3242. ;; input from the input queue, because processing of pending input
  3243. ;; always has priority over display updates (although this input
  3244. ;; won't be processed until we are done). Later on we will queue
  3245. ;; the input on the input queue again.
  3246. (if artist-flood-fill-show-incrementally
  3247. (progn
  3248. (if (input-pending-p)
  3249. (discard-input))
  3250. (artist-update-display)))))))
  3251. ;;
  3252. ;; Accessors to arrow-points
  3253. ;;
  3254. (defun artist-make-arrow-point (x y direction &optional state)
  3255. "Create an arrow point at X, Y for a line in direction DIRECTION.
  3256. Optional argument STATE can be used to set state (default is nil)."
  3257. (save-excursion
  3258. (let* ((arrow-point (make-vector 4 0))
  3259. (arrow-marker (make-marker)))
  3260. (artist-move-to-xy x y)
  3261. (set-marker arrow-marker (point))
  3262. (aset arrow-point 0 arrow-marker)
  3263. (aset arrow-point 1 (artist-get-char-at-xy x y))
  3264. (aset arrow-point 2 direction)
  3265. (aset arrow-point 3 state)
  3266. arrow-point)))
  3267. (defsubst artist-arrow-point-get-marker (arrow-point)
  3268. "Retrieve the marker component of an ARROW-POINT."
  3269. (aref arrow-point 0))
  3270. (defsubst artist-arrow-point-get-orig-char (arrow-point)
  3271. "Retrieve the orig char component of an ARROW-POINT."
  3272. (aref arrow-point 1))
  3273. (defsubst artist-arrow-point-get-direction (arrow-point)
  3274. "Retrieve the direction component of an ARROW-POINT."
  3275. (aref arrow-point 2))
  3276. (defsubst artist-arrow-point-get-state (arrow-point)
  3277. "Retrieve the state component of an ARROW-POINT."
  3278. (aref arrow-point 3))
  3279. (defsubst artist-arrow-point-set-state (arrow-point new-state)
  3280. "Set the state component of an ARROW-POINT to NEW-STATE."
  3281. (aset arrow-point 3 new-state))
  3282. (defun artist-clear-arrow-points ()
  3283. "Clear current endpoints."
  3284. (setq artist-arrow-point-1 nil)
  3285. (setq artist-arrow-point-2 nil))
  3286. (defun artist-set-arrow-points-for-poly (point-list)
  3287. "Generic function for setting arrow-points for poly-shapes from POINT-LIST."
  3288. (let* ((ep1 (elt point-list 0))
  3289. (ep2 (elt point-list 1))
  3290. (x1 (artist-endpoint-get-x ep1))
  3291. (y1 (artist-endpoint-get-y ep1))
  3292. (x2 (artist-endpoint-get-x ep2))
  3293. (y2 (artist-endpoint-get-y ep2))
  3294. (dir1 (artist-find-direction x2 y2 x1 y1))
  3295. (epn (car (last point-list)))
  3296. (epn-1 (car (last point-list 2)))
  3297. (xn (artist-endpoint-get-x epn))
  3298. (yn (artist-endpoint-get-y epn))
  3299. (xn-1 (artist-endpoint-get-x epn-1))
  3300. (yn-1 (artist-endpoint-get-y epn-1))
  3301. (dirn (artist-find-direction xn-1 yn-1 xn yn)))
  3302. (setq artist-arrow-point-1 (artist-make-arrow-point x1 y1 dir1))
  3303. (setq artist-arrow-point-2 (artist-make-arrow-point xn yn dirn))))
  3304. (defun artist-set-arrow-points-for-2points (shape x1 y1 x2 y2)
  3305. "Generic function for setting arrow-points for 2-point shapes.
  3306. The 2-point shape SHAPE is drawn from X1, Y1 to X2, Y2."
  3307. (let* ((endpoint1 (artist-2point-get-endpoint1 shape))
  3308. (endpoint2 (artist-2point-get-endpoint2 shape))
  3309. (x1 (artist-endpoint-get-x endpoint1))
  3310. (y1 (artist-endpoint-get-y endpoint1))
  3311. (x2 (artist-endpoint-get-x endpoint2))
  3312. (y2 (artist-endpoint-get-y endpoint2)))
  3313. (setq artist-arrow-point-1
  3314. (artist-make-arrow-point x1 y1
  3315. (artist-find-direction x2 y2 x1 y1)))
  3316. (setq artist-arrow-point-2
  3317. (artist-make-arrow-point x2 y2
  3318. (artist-find-direction x1 y1 x2 y2)))))
  3319. ;;
  3320. ;; Common routine for drawing/undrawing shapes based
  3321. ;; on the draw-how
  3322. ;;
  3323. (defun artist-key-undraw-continously (x y)
  3324. "Undraw current continuous shape with point at X, Y."
  3325. ;; No undraw-info for continuous shapes
  3326. nil)
  3327. (defun artist-key-undraw-poly (x y)
  3328. "Undraw current poly shape with point at X, Y."
  3329. (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
  3330. (x1 (artist-endpoint-get-x artist-key-endpoint1))
  3331. (y1 (artist-endpoint-get-y artist-key-endpoint1)))
  3332. (artist-funcall undraw-fn artist-key-shape)))
  3333. (defun artist-key-undraw-1point (x y)
  3334. "Undraw current 1-point shape at X, Y."
  3335. ;; No undraw-info for 1-point shapes
  3336. nil)
  3337. (defun artist-key-undraw-2points (x y)
  3338. "Undraw current 2-point shape at X, Y."
  3339. (let ((undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
  3340. (x1 (artist-endpoint-get-x artist-key-endpoint1))
  3341. (y1 (artist-endpoint-get-y artist-key-endpoint1)))
  3342. (artist-funcall undraw-fn artist-key-shape)))
  3343. (defun artist-key-undraw-common ()
  3344. "Common routine undrawing current shape."
  3345. (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
  3346. (col (artist-current-column))
  3347. (row (artist-current-line)))
  3348. ;; Depending on what we are currently drawing, call other routines
  3349. ;; that knows how to do the job
  3350. ;;
  3351. (cond ((eq draw-how 'artist-do-continously)
  3352. (artist-key-undraw-continously col row))
  3353. ((eq draw-how 'artist-do-poly)
  3354. (artist-key-undraw-poly col row))
  3355. ((and (numberp draw-how) (= draw-how 1))
  3356. (artist-key-undraw-1point col row))
  3357. ((and (numberp draw-how) (= draw-how 2))
  3358. (artist-key-undraw-2points col row))
  3359. (t (message "Undrawing \"%s\"s is not yet implemented" draw-how)))
  3360. ;; Now restore the old position
  3361. ;;
  3362. (artist-move-to-xy col row)))
  3363. ;; Implementation note: This really should honor the interval-fn entry
  3364. ;; in the master table, `artist-mt', which would mean leaving a timer
  3365. ;; that calls `draw-fn' every now and then. That timer would then have
  3366. ;; to be canceled and reinstalled whenever the user moves the cursor.
  3367. ;; This could be done, but what if the user suddenly switches to another
  3368. ;; drawing mode, or even kills the buffer! In the mouse case, it is much
  3369. ;; simpler: when at the end of `artist-mouse-draw-continously', the
  3370. ;; user has released the button, so the timer will always be canceled
  3371. ;; at that point.
  3372. (defun artist-key-draw-continously (x y)
  3373. "Draw current continuous shape at X,Y."
  3374. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)))
  3375. (setq artist-key-shape (artist-funcall draw-fn x y))))
  3376. (defun artist-key-draw-poly (x y)
  3377. "Draw current poly-point shape with nth point at X,Y."
  3378. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
  3379. (x1 (artist-endpoint-get-x artist-key-endpoint1))
  3380. (y1 (artist-endpoint-get-y artist-key-endpoint1)))
  3381. (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y))))
  3382. (defun artist-key-draw-1point (x y)
  3383. "Draw current 1-point shape at X,Y."
  3384. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)))
  3385. (setq artist-key-shape (artist-funcall draw-fn x y))))
  3386. (defun artist-key-draw-2points (x y)
  3387. "Draw current 2-point shape at X,Y."
  3388. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
  3389. (x1 (artist-endpoint-get-x artist-key-endpoint1))
  3390. (y1 (artist-endpoint-get-y artist-key-endpoint1)))
  3391. (setq artist-key-shape (artist-funcall draw-fn x1 y1 x y))))
  3392. (defun artist-key-draw-common ()
  3393. "Common routine for drawing current shape."
  3394. (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
  3395. (col (artist-current-column))
  3396. (row (artist-current-line)))
  3397. ;; Depending on what we are currently drawing, call other routines
  3398. ;; that knows how to do the job
  3399. ;;
  3400. (cond ((eq draw-how 'artist-do-continously)
  3401. (artist-key-draw-continously col row))
  3402. ((eq draw-how 'artist-do-poly)
  3403. (artist-key-draw-poly col row))
  3404. ((and (numberp draw-how) (= draw-how 1))
  3405. (artist-key-draw-1point col row))
  3406. ((and (numberp draw-how) (= draw-how 2))
  3407. (artist-key-draw-2points col row))
  3408. (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))
  3409. ;; Now restore the old position
  3410. ;;
  3411. (artist-move-to-xy col row)))
  3412. ;;
  3413. ;; Functions related to trimming line-endings
  3414. ;; The region between the topmost and bottommost visited line is
  3415. ;; called a draw-region.
  3416. ;;
  3417. (defun artist-draw-region-reset ()
  3418. "Reset the current draw-region."
  3419. (setq artist-draw-region-max-y 0)
  3420. (setq artist-draw-region-min-y 1000000))
  3421. (defun artist-draw-region-trim-line-endings (min-y max-y)
  3422. "Trim lines in current draw-region from MIN-Y to MAX-Y.
  3423. Trimming here means removing white space at end of a line."
  3424. ;; Safety check: switch min-y and max-y if max-y is smaller
  3425. (if (< max-y min-y)
  3426. (let ((tmp min-y))
  3427. (setq min-y max-y)
  3428. (setq max-y tmp)))
  3429. (save-excursion
  3430. (let ((curr-y min-y))
  3431. (while (<= curr-y max-y)
  3432. (artist-move-to-xy 0 curr-y)
  3433. (end-of-line)
  3434. (delete-horizontal-space)
  3435. (setq curr-y (1+ curr-y))))))
  3436. ;;
  3437. ;; Drawing shapes by using keys
  3438. ;;
  3439. (defun artist-key-do-continously-continously (x y)
  3440. "Update current continuous shape at X,Y."
  3441. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go)))
  3442. (artist-funcall draw-fn x y)))
  3443. (defun artist-key-do-continously-poly (x y)
  3444. "Update current poly-point shape with nth point at X,Y."
  3445. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
  3446. (undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
  3447. (x1 (artist-endpoint-get-x artist-key-endpoint1))
  3448. (y1 (artist-endpoint-get-y artist-key-endpoint1))
  3449. (x2 x)
  3450. (y2 y))
  3451. ;; If not rubber-banding, then move the 2
  3452. ;; Otherwise re-draw the shape to the new position
  3453. ;;
  3454. (if (not artist-rubber-banding)
  3455. (progn
  3456. (artist-no-rb-unset-point2)
  3457. (artist-no-rb-set-point2 x y))
  3458. (progn
  3459. (artist-funcall undraw-fn artist-key-shape)
  3460. (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))))
  3461. (defun artist-key-do-continously-1point (x y)
  3462. "Update current 1-point shape at X,Y."
  3463. ;; Nothing to do continuously for operations
  3464. ;; where we have only one input point
  3465. nil)
  3466. (defun artist-key-do-continously-2points (x y)
  3467. "Update current 2-point shape with 2nd point at X,Y."
  3468. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
  3469. (undraw-fn (artist-go-get-undraw-fn-from-symbol artist-curr-go))
  3470. (x1 (artist-endpoint-get-x artist-key-endpoint1))
  3471. (y1 (artist-endpoint-get-y artist-key-endpoint1))
  3472. (x2 x)
  3473. (y2 y))
  3474. ;; If not rubber-banding, then move the 2
  3475. ;; Otherwise re-draw the shape to the new position
  3476. ;;
  3477. (if (not artist-rubber-banding)
  3478. (progn
  3479. (artist-no-rb-unset-point2)
  3480. (artist-no-rb-set-point2 x y))
  3481. (progn
  3482. (artist-funcall undraw-fn artist-key-shape)
  3483. (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))))
  3484. (defun artist-key-do-continously-common ()
  3485. "Common routine for updating current shape."
  3486. (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
  3487. (col (artist-current-column))
  3488. (row (artist-current-line)))
  3489. ;; Depending on what we are currently drawing, call other routines
  3490. ;; that knows how to do the job
  3491. ;;
  3492. (cond ((eq draw-how 'artist-do-continously)
  3493. (artist-key-do-continously-continously col row))
  3494. ((eq draw-how 'artist-do-poly)
  3495. (artist-key-do-continously-poly col row))
  3496. ((and (numberp draw-how) (= draw-how 1))
  3497. (artist-key-do-continously-1point col row))
  3498. ((and (numberp draw-how) (= draw-how 2))
  3499. (artist-key-do-continously-2points col row))
  3500. (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))
  3501. ;; Now restore the old position
  3502. ;;
  3503. (artist-move-to-xy col row)))
  3504. (defun artist-key-set-point-continously (x y)
  3505. "Set point for current continuous shape at X,Y."
  3506. ;; Maybe set arrow-points for continuous shapes
  3507. (let ((arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
  3508. (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go))
  3509. (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
  3510. (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
  3511. (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go)))
  3512. (if (not artist-key-is-drawing)
  3513. ;; *** We are about to begin drawing
  3514. (progn
  3515. (artist-funcall init-fn x y))
  3516. ;; *** We are about to stop drawing
  3517. (progn
  3518. (artist-funcall prep-fill-fn x y)
  3519. (if (artist-funcall arrow-pred)
  3520. (artist-funcall arrow-set-fn x y)
  3521. (artist-clear-arrow-points))
  3522. (artist-funcall exit-fn x y))))
  3523. ;; Toggle the is-drawing flag
  3524. (setq artist-key-is-drawing (not artist-key-is-drawing)))
  3525. (defun artist-key-set-point-poly (x y &optional this-is-last-point)
  3526. "Set point for current poly-point shape at X,Y.
  3527. If optional argument THIS-IS-LAST-POINT is non-nil, this point is the last."
  3528. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
  3529. (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
  3530. (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
  3531. (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))
  3532. (fill-pred (artist-go-get-fill-pred-from-symbol artist-curr-go))
  3533. (fill-fn (artist-go-get-fill-fn-from-symbol artist-curr-go))
  3534. (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
  3535. (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
  3536. (if (not artist-key-is-drawing)
  3537. ;; *** We were not drawing ==> set first point
  3538. (progn
  3539. (artist-funcall init-fn x y)
  3540. ;; If not rubber-banding, set first point.
  3541. ;; Otherwise, draw the shape from x,y to x,y
  3542. (if (not artist-rubber-banding)
  3543. (artist-no-rb-set-point1 x y)
  3544. (setq artist-key-shape (artist-funcall draw-fn x y x y)))
  3545. ;; Set first endpoint
  3546. (setq artist-key-endpoint1 (artist-make-endpoint x y))
  3547. ;; Set point-list to contain start point
  3548. (setq artist-key-poly-point-list (list (artist-make-endpoint x y)))
  3549. ;; Since we are not ready, set the arrow-points to nil
  3550. (artist-clear-arrow-points)
  3551. ;; Change state to drawing
  3552. (setq artist-key-is-drawing t)
  3553. ;; Feedback
  3554. (message "%s" (substitute-command-keys
  3555. (concat "First point set. "
  3556. "Set next with \\[artist-key-set-point], "
  3557. "set last with C-u \\[artist-key-set-point]"))))
  3558. ;; *** We were drawing ==> we are about to set nth point
  3559. ;; (last point if the argument this-is-last-point is non-nil)
  3560. ;;
  3561. (let ((x1 (artist-endpoint-get-x artist-key-endpoint1))
  3562. (y1 (artist-endpoint-get-y artist-key-endpoint1))
  3563. (x2 x)
  3564. (y2 y))
  3565. ;; If not rubber-banding, undraw the 1's and 2's, then
  3566. ;; draw the shape (if we were rubber-banding, then the
  3567. ;; shape is already drawn in artist-key-do-continously-2points.)
  3568. ;;
  3569. (if (not artist-rubber-banding)
  3570. (progn
  3571. (artist-no-rb-unset-points)
  3572. (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))
  3573. ;; Set x2 and y2 from shape's second point
  3574. ;; (which might be different from the mouse's second point,
  3575. ;; if, for example, we are drawing a straight line)
  3576. ;;
  3577. (if (not (null artist-key-shape))
  3578. (let ((endpoint2 (artist-2point-get-endpoint2 artist-key-shape)))
  3579. (setq x2 (artist-endpoint-get-x endpoint2))
  3580. (setq y2 (artist-endpoint-get-y endpoint2))))
  3581. ;; Add the endpoint to the list of poly-points
  3582. (setq artist-key-poly-point-list
  3583. (append artist-key-poly-point-list
  3584. (list (artist-make-endpoint x2 y2))))
  3585. ;; Now do handle the case when this is the last point,
  3586. ;; and the case when this point isn't the last
  3587. ;;
  3588. (if (not this-is-last-point)
  3589. ;; ** This is not the last point
  3590. (progn
  3591. ;; Start drawing a new 2-point-shape from last endpoint.
  3592. ;; First set the start-point
  3593. (setq x1 x2)
  3594. (setq y1 y2)
  3595. (setq artist-key-endpoint1 (artist-make-endpoint x1 y1))
  3596. ;; If we are not rubber-banding, then place the '1
  3597. ;; Otherwise, draw the shape from x1,y1 to x1,y1
  3598. (if (not artist-rubber-banding)
  3599. (artist-no-rb-set-point1 x1 y1)
  3600. (setq artist-key-shape (artist-funcall draw-fn x1 y1 x1 y1)))
  3601. ;; Feedback
  3602. (message "Point set"))
  3603. ;; ** This is the last point
  3604. (progn
  3605. (artist-funcall prep-fill-fn artist-key-poly-point-list)
  3606. ;; Maybe fill
  3607. (if (artist-funcall fill-pred)
  3608. (artist-funcall fill-fn artist-key-shape
  3609. artist-key-poly-point-list))
  3610. ;; Set the arrow-points
  3611. (if (artist-funcall arrow-pred)
  3612. (artist-funcall arrow-set-fn artist-key-poly-point-list)
  3613. (artist-clear-arrow-points))
  3614. (artist-funcall exit-fn artist-key-poly-point-list)
  3615. ;; Change state to not drawing
  3616. (setq artist-key-shape nil)
  3617. (setq artist-key-endpoint1 nil)
  3618. (setq artist-key-is-drawing nil)))))))
  3619. (defun artist-key-set-point-1point (x y)
  3620. "Set point for current 1-point shape at X,Y."
  3621. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
  3622. (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
  3623. (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
  3624. (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))
  3625. (draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
  3626. (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
  3627. (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
  3628. (artist-funcall init-fn x y)
  3629. (artist-funcall draw-fn x y)
  3630. (artist-funcall prep-fill-fn x y)
  3631. (if (artist-funcall arrow-pred)
  3632. (artist-funcall arrow-set-fn x y)
  3633. (artist-clear-arrow-points))
  3634. (artist-funcall exit-fn x y))
  3635. (setq artist-key-shape nil)
  3636. (setq artist-key-is-drawing nil))
  3637. (defun artist-key-set-point-2points (x y)
  3638. "Set first or second point in current 2-point shape at X,Y."
  3639. (let ((draw-fn (artist-go-get-draw-fn-from-symbol artist-curr-go))
  3640. (init-fn (artist-go-get-init-fn-from-symbol artist-curr-go))
  3641. (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol artist-curr-go))
  3642. (exit-fn (artist-go-get-exit-fn-from-symbol artist-curr-go))
  3643. (fill-pred (artist-go-get-fill-pred-from-symbol artist-curr-go))
  3644. (fill-fn (artist-go-get-fill-fn-from-symbol artist-curr-go))
  3645. (arrow-pred (artist-go-get-arrow-pred-from-symbol artist-curr-go))
  3646. (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol artist-curr-go)))
  3647. (if (not artist-key-is-drawing)
  3648. ;; *** We were not drawing ==> set first point
  3649. (progn
  3650. (artist-funcall init-fn x y)
  3651. ;; If not rubber-banding, set first point.
  3652. ;; Otherwise, draw the shape from x,y to x,y
  3653. (if (not artist-rubber-banding)
  3654. (artist-no-rb-set-point1 x y)
  3655. (setq artist-key-shape (artist-funcall draw-fn x y x y)))
  3656. ;; Set first endpoint
  3657. (setq artist-key-endpoint1 (artist-make-endpoint x y))
  3658. ;; Since we are not ready, clear the arrow-points
  3659. (artist-clear-arrow-points)
  3660. ;; Change state to drawing
  3661. (setq artist-key-is-drawing t))
  3662. ;; *** We were drawing ==> we are about to set 2nd point
  3663. ;; and end the drawing operation
  3664. (let ((x1 (artist-endpoint-get-x artist-key-endpoint1))
  3665. (y1 (artist-endpoint-get-y artist-key-endpoint1))
  3666. (x2 x)
  3667. (y2 y))
  3668. ;; If not rubber-banding, undraw the 1's and 2's, then
  3669. ;; draw the shape (if we were rubber-banding, then the
  3670. ;; shape is already drawn in artist-key-do-continously-2points.)
  3671. ;;
  3672. (if (not artist-rubber-banding)
  3673. (progn
  3674. (artist-no-rb-unset-points)
  3675. (setq artist-key-shape (artist-funcall draw-fn x1 y1 x2 y2))))
  3676. (artist-funcall prep-fill-fn artist-key-shape x1 y1 x2 y2)
  3677. ;; Maybe fill
  3678. ;;
  3679. (if (artist-funcall fill-pred)
  3680. (artist-funcall fill-fn artist-key-shape x1 y1 x2 y2))
  3681. ;; Maybe set the arrow-points
  3682. ;;
  3683. (if (artist-funcall arrow-pred)
  3684. (artist-funcall arrow-set-fn artist-key-shape x1 y1 x2 y2)
  3685. (artist-clear-arrow-points))
  3686. (artist-funcall exit-fn artist-key-shape x1 y1 x2 y2)
  3687. ;; Change state to not drawing
  3688. (setq artist-key-is-drawing nil)))))
  3689. (defun artist-key-set-point-common (arg)
  3690. "Common routine for setting point in current shape.
  3691. With non-nil ARG, set the last point."
  3692. (let ((draw-how (artist-go-get-draw-how-from-symbol artist-curr-go))
  3693. (col (artist-current-column))
  3694. (row (artist-current-line))
  3695. (was-drawing artist-key-is-drawing))
  3696. ;; First, if we are about to draw, then reset the draw-region
  3697. (if (not artist-key-is-drawing)
  3698. (artist-draw-region-reset))
  3699. ;; Depending on what we are currently drawing, call other routines
  3700. ;; that knows how to do the job
  3701. ;;
  3702. (cond ((eq draw-how 'artist-do-continously)
  3703. (artist-key-set-point-continously col row)
  3704. ;; Do this now, otherwise nothing will happen until we move.
  3705. (artist-key-do-continously-continously col row))
  3706. ((eq draw-how 'artist-do-poly)
  3707. (artist-key-set-point-poly col row arg))
  3708. ((and (numberp draw-how) (= draw-how 1))
  3709. (artist-key-set-point-1point col row))
  3710. ((and (numberp draw-how) (= draw-how 2))
  3711. (artist-key-set-point-2points col row))
  3712. (t (message "Drawing \"%s\"s is not yet implemented" draw-how)))
  3713. ;; Maybe trim line endings
  3714. (if (and artist-trim-line-endings
  3715. was-drawing
  3716. (not artist-key-is-drawing))
  3717. (artist-draw-region-trim-line-endings artist-draw-region-min-y
  3718. artist-draw-region-max-y))
  3719. ;; Now restore the old position
  3720. ;;
  3721. (artist-move-to-xy col row)
  3722. (artist-mode-line-show-curr-operation artist-key-is-drawing)))
  3723. ;;
  3724. ;; Key navigation
  3725. ;;
  3726. (defun artist-previous-line (&optional n)
  3727. "Move cursor up N lines (default is 1), updating current shape.
  3728. If N is negative, move cursor down."
  3729. (interactive "p")
  3730. (let ((col (artist-current-column)))
  3731. (forward-line (- n))
  3732. (move-to-column col t))
  3733. (when artist-key-is-drawing
  3734. (artist-key-do-continously-common)))
  3735. (defun artist-next-line (&optional n)
  3736. "Move cursor down N lines (default is 1), updating current shape.
  3737. If N is negative, move cursor up."
  3738. (interactive "p")
  3739. (let ((col (artist-current-column)))
  3740. (forward-line n)
  3741. (move-to-column col t))
  3742. (when artist-key-is-drawing
  3743. (artist-key-do-continously-common)))
  3744. (defun artist-backward-char (&optional n)
  3745. "Move cursor backward N chars (default is 1), updating current shape.
  3746. If N is negative, move forward."
  3747. (interactive "p")
  3748. (if (> n 0)
  3749. (artist-forward-char (- n))
  3750. (artist-forward-char n)))
  3751. (defun artist-forward-char (&optional n)
  3752. "Move cursor forward N chars (default is 1), updating current shape.
  3753. If N is negative, move backward."
  3754. (interactive "p")
  3755. (let* ((step-x (if (>= n 0) 1 -1))
  3756. (distance (abs n))
  3757. (curr-col (artist-current-column))
  3758. (new-col (max 0 (+ curr-col (* distance step-x)))))
  3759. (if (not artist-key-is-drawing)
  3760. (move-to-column new-col t)
  3761. (move-to-column new-col t)
  3762. (artist-key-do-continously-common))))
  3763. (defun artist-key-set-point (&optional arg)
  3764. "Set a point for the current shape. With optional ARG, set the last point."
  3765. (interactive "P")
  3766. (artist-key-set-point-common arg))
  3767. (defun artist-select-fill-char (c)
  3768. "Set current fill character to be C."
  3769. (interactive "cType fill char (type RET to turn off): ")
  3770. (cond ((eq c ?\r) (setq artist-fill-char-set nil)
  3771. (message "Fill canceled"))
  3772. (t (setq artist-fill-char-set t)
  3773. (setq artist-fill-char c)
  3774. (message "Fill set to \"%c\"" c))))
  3775. (defun artist-select-line-char (c)
  3776. "Set current line character to be C."
  3777. (interactive "cType line char (type RET to turn off): ")
  3778. (cond ((eq c ?\r) (setq artist-line-char-set nil)
  3779. (message "Normal lines"))
  3780. (t (setq artist-line-char-set t)
  3781. (setq artist-line-char c)
  3782. (message "Line drawn with \"%c\"" c)))
  3783. (if artist-key-is-drawing
  3784. (artist-key-do-continously-common)))
  3785. (defun artist-select-erase-char (c)
  3786. "Set current erase character to be C."
  3787. (interactive "cType char to use when erasing (type RET for normal): ")
  3788. (cond ((eq c ?\r) (setq artist-erase-char ?\s)
  3789. (message "Normal erasing"))
  3790. (t (setq artist-erase-char c)
  3791. (message "Erasing with \"%c\"" c)))
  3792. (if artist-key-is-drawing
  3793. (artist-key-do-continously-common)))
  3794. (defun artist-charlist-to-string (char-list)
  3795. "Convert a list of characters, CHAR-LIST, to a string."
  3796. (concat char-list))
  3797. (defun artist-string-to-charlist (str)
  3798. "Convert a string, STR, to list of characters."
  3799. (append str nil))
  3800. (defun artist-select-spray-chars (chars initial-char)
  3801. "Set current spray characters to be CHARS, starting with INITIAL-CHAR."
  3802. ;; This huge unreadable `interactive'-clause does the following
  3803. ;; 1. Asks for a string of spray-characters
  3804. ;; 2. Asks for the initial character (default is the first),
  3805. ;; and loops if the answer is not a char within the string in 1.
  3806. (interactive
  3807. (let* ((str (read-string "Select spray-can characters, lightest first: "
  3808. (artist-charlist-to-string artist-spray-chars)))
  3809. (char-list (artist-string-to-charlist str))
  3810. (initial (let* ((err-msg "")
  3811. (ok nil)
  3812. (first-char-as-str (char-to-string (car char-list)))
  3813. (first-s) (first-c))
  3814. (while (not ok)
  3815. (setq first-s
  3816. (read-string
  3817. (format (concat "%sSelect initial-character, "
  3818. "one of \"%s\" (%s): ")
  3819. err-msg str first-char-as-str)))
  3820. (if (equal first-s "")
  3821. (setq first-s first-char-as-str))
  3822. (setq first-c (car (artist-string-to-charlist first-s)))
  3823. (setq ok (not (null (member first-c char-list))))
  3824. (if (not ok)
  3825. (setq err-msg (format
  3826. "Not in spray-chars: \"%s\". "
  3827. (char-to-string first-c)))))
  3828. first-c)))
  3829. (list char-list initial)))
  3830. (setq artist-spray-chars chars)
  3831. (setq artist-spray-new-char initial-char)
  3832. (message "Spray-chars set to \"%s\", initial: \"%s\""
  3833. (artist-charlist-to-string chars) (char-to-string initial-char)))
  3834. (defun artist-select-operation (op-str)
  3835. "Select drawing operation OP-STR."
  3836. (interactive (list (completing-read "Select operation: "
  3837. artist-key-compl-table)))
  3838. (let* ((op-symbol (artist-mt-get-symbol-from-keyword op-str))
  3839. (draw-how (if op-symbol
  3840. (artist-go-get-draw-how-from-symbol op-symbol)
  3841. nil)))
  3842. ;; First check that the string was valid
  3843. (if (null op-symbol)
  3844. (error "Unknown drawing method: %s" op-str))
  3845. ;; Second, check that we are not about to switch to a different
  3846. ;; kind of shape (do that only if we are drawing with keys;
  3847. ;; otherwise this function cannot get called).
  3848. (if (and artist-key-is-drawing
  3849. (not (equal artist-key-draw-how draw-how)))
  3850. (error "Cannot switch to a different kind of shape while drawing"))
  3851. ;; If we were drawing, undraw the shape
  3852. (if (and artist-key-is-drawing
  3853. artist-rubber-banding)
  3854. (artist-key-undraw-common))
  3855. ;; Set the current operation and draw-how
  3856. (setq artist-curr-go op-symbol)
  3857. (setq artist-key-draw-how draw-how)
  3858. ;; If we were drawing, redraw the shape (but don't if shape
  3859. ;; is drawn by setting only one point)
  3860. (if (and artist-key-is-drawing
  3861. artist-rubber-banding
  3862. (not (eq artist-key-draw-how 1)))
  3863. (artist-key-draw-common)))
  3864. ;; Feedback
  3865. (artist-mode-line-show-curr-operation artist-key-is-drawing))
  3866. (defun artist-toggle-rubber-banding (&optional state)
  3867. "Toggle rubber-banding.
  3868. If optional argument STATE is positive, turn rubber-banding on."
  3869. (interactive)
  3870. (if artist-key-is-drawing
  3871. (error "Cannot toggle rubber-banding while drawing"))
  3872. (if (setq artist-rubber-banding
  3873. (if (null state) (not artist-rubber-banding)
  3874. (> (prefix-numeric-value state) 0)))
  3875. (message "Rubber-banding is now on")
  3876. (message "Rubber-banding is now off")))
  3877. (defun artist-toggle-trim-line-endings (&optional state)
  3878. "Toggle trimming of line-endings.
  3879. If optional argument STATE is positive, turn trimming on."
  3880. (interactive)
  3881. (if (setq artist-trim-line-endings
  3882. (if (null state) (not artist-trim-line-endings)
  3883. (> (prefix-numeric-value state) 0)))
  3884. (message "Trimming is now on")
  3885. (message "Trimming is now off")))
  3886. (defun artist-toggle-borderless-shapes (&optional state)
  3887. "Toggle borders of shapes.
  3888. If optional argument STATE is positive, turn borders on."
  3889. (interactive)
  3890. (if (setq artist-borderless-shapes
  3891. (if (null state) (not artist-borderless-shapes)
  3892. (> (prefix-numeric-value state) 0)))
  3893. (message "Borders are now off")
  3894. (message "Borders are now on")))
  3895. (defun artist-toggle-first-arrow ()
  3896. "Toggle first arrow for shape, if possible."
  3897. (interactive)
  3898. (save-excursion
  3899. (if (not (null artist-arrow-point-1))
  3900. (let* ((arrow-point artist-arrow-point-1)
  3901. (arrow-state (artist-arrow-point-get-state arrow-point))
  3902. (arrow-marker (artist-arrow-point-get-marker arrow-point))
  3903. (direction (artist-arrow-point-get-direction arrow-point))
  3904. (orig-char (artist-arrow-point-get-orig-char arrow-point))
  3905. (arrow-char (aref artist-arrows direction))
  3906. (new-state (not arrow-state)))
  3907. (goto-char (marker-position arrow-marker))
  3908. (if new-state
  3909. (if arrow-char
  3910. (artist-replace-char arrow-char))
  3911. (artist-replace-char orig-char))
  3912. (artist-arrow-point-set-state artist-arrow-point-1 new-state)))))
  3913. (defun artist-toggle-second-arrow ()
  3914. "Toggle second arrow for shape, if possible."
  3915. (interactive)
  3916. (save-excursion
  3917. (if (not (null artist-arrow-point-2))
  3918. (let* ((arrow-point artist-arrow-point-2)
  3919. (arrow-state (artist-arrow-point-get-state arrow-point))
  3920. (arrow-marker (artist-arrow-point-get-marker arrow-point))
  3921. (direction (artist-arrow-point-get-direction arrow-point))
  3922. (orig-char (artist-arrow-point-get-orig-char arrow-point))
  3923. (arrow-char (aref artist-arrows direction))
  3924. (new-state (not arrow-state)))
  3925. (goto-char (marker-position arrow-marker))
  3926. (if new-state
  3927. (if arrow-char
  3928. (artist-replace-char arrow-char))
  3929. (artist-replace-char orig-char))
  3930. (artist-arrow-point-set-state artist-arrow-point-2 new-state)))))
  3931. (defun artist-select-op-pen-line ()
  3932. "Select drawing pen lines."
  3933. (interactive)
  3934. (artist-select-operation "Pen Line"))
  3935. (defun artist-select-op-line ()
  3936. "Select drawing lines."
  3937. (interactive)
  3938. (artist-select-operation "line"))
  3939. (defun artist-select-op-straight-line ()
  3940. "Select drawing straight lines."
  3941. (interactive)
  3942. (artist-select-operation "straight line"))
  3943. (defun artist-select-op-rectangle ()
  3944. "Select drawing rectangles."
  3945. (interactive)
  3946. (artist-select-operation "rectangle"))
  3947. (defun artist-select-op-square ()
  3948. "Select drawing squares."
  3949. (interactive)
  3950. (artist-select-operation "square"))
  3951. (defun artist-select-op-poly-line ()
  3952. "Select drawing poly-lines."
  3953. (interactive)
  3954. (artist-select-operation "poly-line"))
  3955. (defun artist-select-op-straight-poly-line ()
  3956. "Select drawing straight poly-lines."
  3957. (interactive)
  3958. (artist-select-operation "straight poly-line"))
  3959. (defun artist-select-op-ellipse ()
  3960. "Select drawing ellipses."
  3961. (interactive)
  3962. (artist-select-operation "ellipse"))
  3963. (defun artist-select-op-circle ()
  3964. "Select drawing circles."
  3965. (interactive)
  3966. (artist-select-operation "circle"))
  3967. (defun artist-select-op-text-see-thru ()
  3968. "Select rendering text (see thru)."
  3969. (interactive)
  3970. (artist-select-operation "text see-thru"))
  3971. (defun artist-select-op-text-overwrite ()
  3972. "Select rendering text (overwrite)."
  3973. (interactive)
  3974. (artist-select-operation "text overwrite"))
  3975. (defun artist-select-op-spray-can ()
  3976. "Select spraying."
  3977. (interactive)
  3978. (artist-select-operation "spray-can"))
  3979. (defun artist-select-op-spray-set-size ()
  3980. "Select setting size for spraying."
  3981. (interactive)
  3982. (artist-select-operation "spray set size"))
  3983. (defun artist-select-op-erase-char ()
  3984. "Select erasing characters."
  3985. (interactive)
  3986. (artist-select-operation "erase char"))
  3987. (defun artist-select-op-erase-rectangle ()
  3988. "Select erasing rectangles."
  3989. (interactive)
  3990. (artist-select-operation "erase rectangle"))
  3991. (defun artist-select-op-vaporize-line ()
  3992. "Select vaporizing single lines."
  3993. (interactive)
  3994. (artist-select-operation "vaporize line"))
  3995. (defun artist-select-op-vaporize-lines ()
  3996. "Select vaporizing connected lines."
  3997. (interactive)
  3998. (artist-select-operation "vaporize lines"))
  3999. (defun artist-select-op-cut-rectangle ()
  4000. "Select cutting rectangles."
  4001. (interactive)
  4002. (artist-select-operation "cut rectangle"))
  4003. (defun artist-select-op-cut-square ()
  4004. "Select cutting squares."
  4005. (interactive)
  4006. (artist-select-operation "cut square"))
  4007. (defun artist-select-op-copy-rectangle ()
  4008. "Select copying rectangles."
  4009. (interactive)
  4010. (artist-select-operation "copy rectangle"))
  4011. (defun artist-select-op-copy-square ()
  4012. "Select copying squares."
  4013. (interactive)
  4014. (artist-select-operation "cut square"))
  4015. (defun artist-select-op-paste ()
  4016. "Select pasting."
  4017. (interactive)
  4018. (artist-select-operation "paste"))
  4019. (defun artist-select-op-flood-fill ()
  4020. "Select flood-filling."
  4021. (interactive)
  4022. (artist-select-operation "flood-fill"))
  4023. ;; Drawing lines by using mouse
  4024. ;; Mouse button actions
  4025. ;;
  4026. (defun artist-update-pointer-shape ()
  4027. "Perform the update of the X Windows pointer shape."
  4028. (set-mouse-color nil))
  4029. (defvar x-pointer-shape)
  4030. (defun artist-set-pointer-shape (new-pointer-shape)
  4031. "Set the shape of the X Windows pointer to NEW-POINTER-SHAPE."
  4032. (setq x-pointer-shape new-pointer-shape)
  4033. (artist-update-pointer-shape))
  4034. (defsubst artist-event-is-shifted (ev)
  4035. "Check whether the shift-key is pressed in event EV."
  4036. (memq 'shift (event-modifiers ev)))
  4037. (defun artist-do-nothing ()
  4038. "Function that does nothing."
  4039. (interactive))
  4040. (defun artist-compute-up-event-key (ev)
  4041. "Compute the corresponding up key sequence for event EV."
  4042. (let* ((basic (event-basic-type ev))
  4043. (unshifted basic)
  4044. (shifted (make-symbol (concat "S-" (symbol-name basic)))))
  4045. (if (artist-event-is-shifted ev)
  4046. (make-vector 1 shifted)
  4047. (make-vector 1 unshifted))))
  4048. (defun artist-down-mouse-1 (ev)
  4049. "Perform drawing action for event EV."
  4050. (interactive "@e")
  4051. (let* ((real (artist-go-get-symbol-shift
  4052. artist-curr-go (artist-event-is-shifted ev)))
  4053. (draw-how (artist-go-get-draw-how-from-symbol real))
  4054. ;; Remember original values for draw-region-min-y and max-y
  4055. ;; in case we are interrupting a key-draw operation.
  4056. (orig-draw-region-min-y artist-draw-region-min-y)
  4057. (orig-draw-region-max-y artist-draw-region-max-y)
  4058. (orig-pointer-shape (if (eq window-system 'x) x-pointer-shape nil))
  4059. (echoq-keystrokes 10000) ; a lot of seconds
  4060. ;; Remember original binding for the button-up event to this
  4061. ;; button-down event.
  4062. (key (artist-compute-up-event-key ev))
  4063. (orig-button-up-binding (lookup-key (current-global-map) key)))
  4064. (unwind-protect
  4065. (progn
  4066. (if (eq window-system 'x)
  4067. (artist-set-pointer-shape artist-pointer-shape))
  4068. ;; Redefine the button-up binding temporarily (the original
  4069. ;; binding is restored in the unwind-forms below). This is to
  4070. ;; avoid the phenomenon outlined in this scenario:
  4071. ;;
  4072. ;; 1. A routine which reads something from the mini-buffer (such
  4073. ;; as the text renderer) is called from below.
  4074. ;; 2. Meanwhile, the users releases the mouse button.
  4075. ;; 3. As a (funny :-) coincidence, the binding for the
  4076. ;; button-up event is often mouse-set-point, so Emacs
  4077. ;; sets the point to where the button was released, which is
  4078. ;; in the buffer where the user wants to place the text.
  4079. ;; 4. The user types C-x o (or uses the mouse once again)
  4080. ;; until he reaches the mini-buffer which is still prompting
  4081. ;; for some text to render.
  4082. ;;
  4083. ;; To do this foolproof, all local and minor-mode maps should
  4084. ;; be searched and temporarily changed as well, since they
  4085. ;; too might have some binding for the button-up event,
  4086. ;; but I hope dealing with the global map will suffice.
  4087. (define-key (current-global-map) key 'artist-do-nothing)
  4088. (artist-draw-region-reset)
  4089. (artist-mode-line-show-curr-operation t)
  4090. (cond ((eq draw-how 'artist-do-continously)
  4091. (artist-mouse-draw-continously ev))
  4092. ((eq draw-how 'artist-do-poly)
  4093. (artist-mouse-draw-poly ev))
  4094. ((and (numberp draw-how) (= draw-how 1))
  4095. (artist-mouse-draw-1point ev))
  4096. ((and (numberp draw-how) (= draw-how 2))
  4097. (artist-mouse-draw-2points ev))
  4098. (t (message "Drawing \"%s\"s is not yet implemented"
  4099. draw-how)))
  4100. (if artist-trim-line-endings
  4101. (artist-draw-region-trim-line-endings artist-draw-region-min-y
  4102. artist-draw-region-max-y))
  4103. (setq artist-draw-region-min-y orig-draw-region-min-y)
  4104. (setq artist-draw-region-max-y orig-draw-region-max-y))
  4105. ; This is protected
  4106. (if (eq window-system 'x)
  4107. (artist-set-pointer-shape orig-pointer-shape))
  4108. (if orig-button-up-binding
  4109. (define-key (current-global-map) key orig-button-up-binding))
  4110. (artist-mode-line-show-curr-operation artist-key-is-drawing))))
  4111. (defun artist-mouse-choose-operation (ev op)
  4112. "Choose operation for event EV and operation OP."
  4113. (interactive
  4114. (progn
  4115. (select-window (posn-window (event-start last-input-event)))
  4116. (list last-input-event
  4117. (if (display-popup-menus-p)
  4118. (x-popup-menu last-nonmenu-event artist-popup-menu-table)
  4119. 'no-popup-menus))))
  4120. (if (eq op 'no-popup-menus)
  4121. ;; No popup menus. Call `tmm-prompt' instead, but with the
  4122. ;; up-mouse-button, if any, temporarily disabled, otherwise
  4123. ;; it'll interfere.
  4124. (let* ((key (artist-compute-up-event-key ev))
  4125. (orig-button-up-binding (lookup-key (current-global-map) key)))
  4126. (unwind-protect
  4127. (define-key (current-global-map) key 'artist-do-nothing)
  4128. (setq op (tmm-prompt artist-popup-menu-table))
  4129. (if orig-button-up-binding
  4130. (define-key (current-global-map) key orig-button-up-binding)))))
  4131. (let ((draw-fn (artist-go-get-draw-fn-from-symbol (car op)))
  4132. (set-fn (artist-fc-get-fn-from-symbol (car op))))
  4133. (cond
  4134. ;; *** It was a draw-function
  4135. ((not (listp draw-fn))
  4136. (let* ((unshifted (artist-go-get-symbol-shift (car op) nil))
  4137. (shifted (artist-go-get-symbol-shift (car op) t))
  4138. (shift-state (artist-event-is-shifted ev))
  4139. (selected-op (if shift-state shifted unshifted))
  4140. (keyword (artist-go-get-keyword-from-symbol selected-op)))
  4141. (artist-select-operation keyword)))
  4142. ;; *** It was a set/unset function
  4143. ((not (listp set-fn))
  4144. (call-interactively set-fn)))))
  4145. (defun artist-down-mouse-3 (ev)
  4146. "Erase character or rectangle, depending on event EV."
  4147. (interactive "@e")
  4148. (let ((artist-curr-go 'erase-char))
  4149. (artist-down-mouse-1 ev))
  4150. ;; Restore mode-line
  4151. (artist-mode-line-show-curr-operation artist-key-is-drawing))
  4152. ;;
  4153. ;; Mouse routines
  4154. ;;
  4155. (defsubst artist-shift-has-changed (shift-state ev)
  4156. "From the last SHIFT-STATE and EV, determine if the shift-state has changed."
  4157. ;; This one simply doesn't work.
  4158. ;;
  4159. ;; There seems to be no way to tell whether the user has pressed shift
  4160. ;; while dragging the cursor around when we are in a track-mouse
  4161. ;; form. Calling (event-modifiers ev) yields nil :-( Neither is the
  4162. ;; (event-basic-type ev) of any help (it is simply `mouse-movement').
  4163. ;;
  4164. ;; So this doesn't work:
  4165. ;; (cond ((and shift-state (not (artist-event-is-shifted ev))) t)
  4166. ;; ((and (not shift-state) (artist-event-is-shifted ev)) t)
  4167. ;; (t nil))
  4168. nil)
  4169. (defun artist-coord-win-to-buf (coord)
  4170. "Convert a window-relative coordinate COORD to a buffer-relative coordinate."
  4171. (let ((window-x (car coord))
  4172. (window-y (cdr coord))
  4173. (window-start-x (window-hscroll))
  4174. (window-start-y (save-excursion (goto-char (window-start))
  4175. (artist-current-line))))
  4176. (cons (+ window-x window-start-x)
  4177. (+ window-y window-start-y))))
  4178. (defun artist-mouse-draw-continously (ev)
  4179. "Generic function for shapes that require 1 point as input.
  4180. Operation is done continuously while the mouse button is hold down.
  4181. The event, EV, is the mouse event."
  4182. (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
  4183. (shifted (artist-go-get-symbol-shift artist-curr-go t))
  4184. (shift-state (artist-event-is-shifted ev))
  4185. (op (if shift-state shifted unshifted))
  4186. (draw-how (artist-go-get-draw-how-from-symbol op))
  4187. (init-fn (artist-go-get-init-fn-from-symbol op))
  4188. (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
  4189. (exit-fn (artist-go-get-exit-fn-from-symbol op))
  4190. (draw-fn (artist-go-get-draw-fn-from-symbol op))
  4191. (interval-fn (artist-go-get-interval-fn-from-symbol op))
  4192. (interval (artist-funcall interval-fn))
  4193. (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
  4194. (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
  4195. (ev-start (event-start ev))
  4196. (initial-win (posn-window ev-start))
  4197. (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
  4198. (x1 (car ev-start-pos))
  4199. (y1 (cdr ev-start-pos))
  4200. (shape)
  4201. (timer))
  4202. (select-window (posn-window ev-start))
  4203. (artist-funcall init-fn x1 y1)
  4204. (if (not artist-rubber-banding)
  4205. (artist-no-rb-set-point1 x1 y1))
  4206. (track-mouse
  4207. (while (or (mouse-movement-p ev)
  4208. (member 'down (event-modifiers ev)))
  4209. (setq ev-start-pos (artist-coord-win-to-buf
  4210. (posn-col-row (event-start ev))))
  4211. (setq x1 (car ev-start-pos))
  4212. (setq y1 (cdr ev-start-pos))
  4213. ;; Cancel previous timer
  4214. (if timer
  4215. (cancel-timer timer))
  4216. (if (not (eq initial-win (posn-window (event-start ev))))
  4217. ;; If we moved outside the window, do nothing
  4218. nil
  4219. ;; Still in same window:
  4220. ;;
  4221. ;; Check if user presses or releases shift key
  4222. (if (artist-shift-has-changed shift-state ev)
  4223. ;; First check that the draw-how is the same as we
  4224. ;; already have. Otherwise, ignore the changed shift-state.
  4225. (if (not (eq draw-how
  4226. (artist-go-get-draw-how-from-symbol
  4227. (if (not shift-state) shifted unshifted))))
  4228. (message "Cannot switch to shifted operation")
  4229. ;; progn is "implicit" since this is the else-part
  4230. (setq shift-state (not shift-state))
  4231. (setq op (if shift-state shifted unshifted))
  4232. (setq draw-how (artist-go-get-draw-how-from-symbol op))
  4233. (setq draw-fn (artist-go-get-draw-fn-from-symbol op))))
  4234. ;; Draw the new shape
  4235. (setq shape (artist-funcall draw-fn x1 y1))
  4236. (artist-move-to-xy x1 y1)
  4237. ;; Start the timer to call `draw-fn' repeatedly every
  4238. ;; `interval' second
  4239. (if (and interval draw-fn)
  4240. (setq timer (run-at-time interval interval draw-fn x1 y1))))
  4241. ;; Read next event
  4242. (setq ev (read-event))))
  4243. ;; Cancel any timers
  4244. (if timer
  4245. (cancel-timer timer))
  4246. (artist-funcall prep-fill-fn x1 y1)
  4247. (if (artist-funcall arrow-pred)
  4248. (artist-funcall arrow-set-fn x1 y1)
  4249. (artist-clear-arrow-points))
  4250. (artist-funcall exit-fn x1 y1)
  4251. (artist-move-to-xy x1 y1)))
  4252. (defun artist-mouse-draw-poly (ev)
  4253. "Generic function for shapes requiring several points as input.
  4254. The event, EV, is the mouse event."
  4255. (interactive "@e")
  4256. (message "Mouse-1: set new point, mouse-2: set last point")
  4257. (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
  4258. (shifted (artist-go-get-symbol-shift artist-curr-go t))
  4259. (shift-state (artist-event-is-shifted ev))
  4260. (op (if shift-state shifted unshifted))
  4261. (draw-how (artist-go-get-draw-how-from-symbol op))
  4262. (init-fn (artist-go-get-init-fn-from-symbol op))
  4263. (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
  4264. (exit-fn (artist-go-get-exit-fn-from-symbol op))
  4265. (draw-fn (artist-go-get-draw-fn-from-symbol op))
  4266. (undraw-fn (artist-go-get-undraw-fn-from-symbol op))
  4267. (fill-pred (artist-go-get-fill-pred-from-symbol op))
  4268. (fill-fn (artist-go-get-fill-fn-from-symbol op))
  4269. (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
  4270. (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
  4271. (ev-start (event-start ev))
  4272. (initial-win (posn-window ev-start))
  4273. (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
  4274. (x1-last (car ev-start-pos))
  4275. (y1-last (cdr ev-start-pos))
  4276. (x2 x1-last)
  4277. (y2 y1-last)
  4278. (is-down t)
  4279. (shape nil)
  4280. (point-list nil)
  4281. (done nil))
  4282. (select-window (posn-window ev-start))
  4283. (artist-funcall init-fn x1-last y1-last)
  4284. (if (not artist-rubber-banding)
  4285. (artist-no-rb-set-point1 x1-last y1-last))
  4286. (track-mouse
  4287. (while (not done)
  4288. ;; decide what to do
  4289. (cond
  4290. ;; *** Mouse button is released.
  4291. ((and is-down
  4292. (or (member 'click (event-modifiers ev))
  4293. (member 'drag (event-modifiers ev))))
  4294. ;; First, if not rubber-banding, draw the line.
  4295. ;;
  4296. (if (not artist-rubber-banding)
  4297. (progn
  4298. (artist-no-rb-unset-points)
  4299. (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))))
  4300. ;; Set the second point to the shape's second point
  4301. ;; (which might be different from the mouse's second point,
  4302. ;; if, for example, we are drawing a straight line)
  4303. ;;
  4304. (if (not (null shape))
  4305. (let ((endpoint2 (artist-2point-get-endpoint2 shape)))
  4306. (setq x1-last (artist-endpoint-get-x endpoint2))
  4307. (setq y1-last (artist-endpoint-get-y endpoint2))))
  4308. (setq point-list (cons (artist-make-endpoint x1-last y1-last)
  4309. point-list))
  4310. (setq shape nil)
  4311. (setq is-down nil))
  4312. ;; *** Mouse button 2 or 3 down
  4313. ((and (member 'down (event-modifiers ev))
  4314. (or (equal (event-basic-type ev) 'mouse-2)
  4315. (equal (event-basic-type ev) 'mouse-3)))
  4316. ;; Ignore
  4317. nil)
  4318. ;; *** Mouse button 2 or 3 released
  4319. ((and (or (member 'click (event-modifiers ev))
  4320. (member 'drag (event-modifiers ev)))
  4321. (or (equal (event-basic-type ev) 'mouse-2)
  4322. (equal (event-basic-type ev) 'mouse-3)))
  4323. ;; This means the end of our poly-line drawing-session.
  4324. ;;
  4325. (setq done t))
  4326. ;; *** Mouse button 1 went down
  4327. ((and (not is-down)
  4328. (member 'down (event-modifiers ev))
  4329. (equal (event-basic-type ev) 'mouse-1))
  4330. ;; Check whether the (possibly new, that depends on if shift
  4331. ;; has been pressed or released) symbol has the same draw-how
  4332. ;; information as the previous had. If it hasn't, we can't
  4333. ;; proceed.
  4334. ;;
  4335. (if (not (eq draw-how
  4336. (artist-go-get-draw-how-from-symbol
  4337. (if (not shift-state) shifted unshifted))))
  4338. (message "Cannot switch operation")
  4339. (progn
  4340. ;; Decide operation
  4341. ;;
  4342. (setq unshifted
  4343. (artist-go-get-symbol-shift artist-curr-go nil)
  4344. shifted
  4345. (artist-go-get-symbol-shift artist-curr-go t)
  4346. shift-state (artist-event-is-shifted ev)
  4347. op (if shift-state shifted unshifted)
  4348. draw-how (artist-go-get-draw-how-from-symbol op)
  4349. draw-fn (artist-go-get-draw-fn-from-symbol op)
  4350. undraw-fn (artist-go-get-undraw-fn-from-symbol op)
  4351. fill-pred (artist-go-get-fill-pred-from-symbol op)
  4352. fill-fn (artist-go-get-fill-fn-from-symbol op))
  4353. ;; Draw shape from last place to this place
  4354. ;; set x2 and y2
  4355. ;;
  4356. (setq ev-start-pos (artist-coord-win-to-buf
  4357. (posn-col-row (event-start ev))))
  4358. (setq x2 (car ev-start-pos))
  4359. (setq y2 (cdr ev-start-pos))
  4360. ;; Draw the new shape (if not rubber-banding, place both marks)
  4361. ;;
  4362. (if artist-rubber-banding
  4363. (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))
  4364. (progn
  4365. (artist-no-rb-set-point1 x1-last y1-last)
  4366. (artist-no-rb-set-point2 x2 y2)))
  4367. ;; Show new operation in mode-line
  4368. (let ((artist-curr-go op))
  4369. (artist-mode-line-show-curr-operation t))))
  4370. (setq is-down t))
  4371. ;; *** Mouse moved, button is down and we are still in orig window
  4372. ((and (mouse-movement-p ev)
  4373. is-down
  4374. (eq initial-win (posn-window (event-start ev))))
  4375. ;; Draw shape from last place to this place
  4376. ;;
  4377. ;; set x2 and y2
  4378. (setq ev-start-pos (artist-coord-win-to-buf
  4379. (posn-col-row (event-start ev))))
  4380. (setq x2 (car ev-start-pos))
  4381. (setq y2 (cdr ev-start-pos))
  4382. ;; First undraw last shape
  4383. ;; (unset last point if not rubberbanding)
  4384. ;;
  4385. (artist-funcall undraw-fn shape)
  4386. ;; Draw the new shape (if not rubberbanding, set 2nd mark)
  4387. ;;
  4388. (if artist-rubber-banding
  4389. (setq shape (artist-funcall draw-fn x1-last y1-last x2 y2))
  4390. (progn
  4391. (artist-no-rb-unset-point2)
  4392. (artist-no-rb-set-point2 x2 y2)))
  4393. ;; Move cursor
  4394. (artist-move-to-xy x2 y2))
  4395. ;; *** Mouse moved, button is down but we are NOT in orig window
  4396. ((and (mouse-movement-p ev)
  4397. is-down
  4398. (not (eq initial-win (posn-window (event-start ev)))))
  4399. ;; Ignore
  4400. nil)
  4401. ;; *** Moving mouse while mouse button is not down
  4402. ((and (mouse-movement-p ev) (not is-down))
  4403. ;; don't do anything.
  4404. nil)
  4405. ;; *** Mouse button 1 went down, first time
  4406. ((and is-down
  4407. (member 'down (event-modifiers ev))
  4408. (equal (event-basic-type ev) 'mouse-1))
  4409. ;; don't do anything
  4410. nil)
  4411. ;; *** Another event
  4412. (t
  4413. ;; End drawing
  4414. ;;
  4415. (setq done t)))
  4416. ;; Read next event (only if we should not stop)
  4417. (if (not done)
  4418. (setq ev (read-event)))))
  4419. ;; Reverse point-list (last points are cond'ed first)
  4420. (setq point-list (reverse point-list))
  4421. (artist-funcall prep-fill-fn point-list)
  4422. ;; Maybe fill
  4423. (if (artist-funcall fill-pred)
  4424. (artist-funcall fill-fn point-list))
  4425. ;; Maybe set arrow points
  4426. (if (and point-list (artist-funcall arrow-pred))
  4427. (artist-funcall arrow-set-fn point-list)
  4428. (artist-clear-arrow-points))
  4429. (artist-funcall exit-fn point-list)
  4430. (artist-move-to-xy x2 y2)))
  4431. (defun artist-mouse-draw-1point (ev)
  4432. "Generic function for shapes requiring only 1 point as input.
  4433. Operation is done once. The event, EV, is the mouse event."
  4434. (interactive "@e")
  4435. (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
  4436. (shifted (artist-go-get-symbol-shift artist-curr-go t))
  4437. (shift-state (artist-event-is-shifted ev))
  4438. (op (if shift-state shifted unshifted))
  4439. (draw-how (artist-go-get-draw-how-from-symbol op))
  4440. (init-fn (artist-go-get-init-fn-from-symbol op))
  4441. (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
  4442. (exit-fn (artist-go-get-exit-fn-from-symbol op))
  4443. (draw-fn (artist-go-get-draw-fn-from-symbol op))
  4444. (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
  4445. (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
  4446. (ev-start (event-start ev))
  4447. (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
  4448. (x1 (car ev-start-pos))
  4449. (y1 (cdr ev-start-pos)))
  4450. (select-window (posn-window ev-start))
  4451. (artist-funcall init-fn x1 y1)
  4452. (artist-funcall draw-fn x1 y1)
  4453. (artist-funcall prep-fill-fn x1 y1)
  4454. (if (artist-funcall arrow-pred)
  4455. (artist-funcall arrow-set-fn x1 y1)
  4456. (artist-clear-arrow-points))
  4457. (artist-funcall exit-fn x1 y1)
  4458. (artist-move-to-xy x1 y1)))
  4459. (defun artist-mouse-draw-2points (ev)
  4460. "Generic function for shapes requiring 2 points as input.
  4461. The event, EV, is the mouse event."
  4462. (interactive "@e")
  4463. (let* ((unshifted (artist-go-get-symbol-shift artist-curr-go nil))
  4464. (shifted (artist-go-get-symbol-shift artist-curr-go t))
  4465. (shift-state (artist-event-is-shifted ev))
  4466. (op (if shift-state shifted unshifted))
  4467. (draw-how (artist-go-get-draw-how-from-symbol op))
  4468. (init-fn (artist-go-get-init-fn-from-symbol op))
  4469. (prep-fill-fn (artist-go-get-prep-fill-fn-from-symbol op))
  4470. (exit-fn (artist-go-get-exit-fn-from-symbol op))
  4471. (draw-fn (artist-go-get-draw-fn-from-symbol op))
  4472. (undraw-fn (artist-go-get-undraw-fn-from-symbol op))
  4473. (fill-pred (artist-go-get-fill-pred-from-symbol op))
  4474. (fill-fn (artist-go-get-fill-fn-from-symbol op))
  4475. (arrow-pred (artist-go-get-arrow-pred-from-symbol op))
  4476. (arrow-set-fn (artist-go-get-arrow-set-fn-from-symbol op))
  4477. (ev-start (event-start ev))
  4478. (initial-win (posn-window ev-start))
  4479. (ev-start-pos (artist-coord-win-to-buf (posn-col-row ev-start)))
  4480. (x1 (car ev-start-pos))
  4481. (y1 (cdr ev-start-pos))
  4482. (x2)
  4483. (y2)
  4484. (shape))
  4485. (select-window (posn-window ev-start))
  4486. (artist-funcall init-fn x1 y1)
  4487. (if (not artist-rubber-banding)
  4488. (artist-no-rb-set-point1 x1 y1))
  4489. (track-mouse
  4490. (while (or (mouse-movement-p ev)
  4491. (member 'down (event-modifiers ev)))
  4492. (setq ev-start-pos (artist-coord-win-to-buf
  4493. (posn-col-row (event-start ev))))
  4494. (setq x2 (car ev-start-pos))
  4495. (setq y2 (cdr ev-start-pos))
  4496. (if (not (eq initial-win (posn-window (event-start ev))))
  4497. ;; If we moved outside the window, do nothing
  4498. nil
  4499. ;; Still in same window:
  4500. ;;
  4501. ;; First undraw last shape (unset last point if not rubberbanding)
  4502. (if artist-rubber-banding
  4503. (artist-funcall undraw-fn shape)
  4504. (artist-no-rb-unset-point2))
  4505. ;; Check if user presses or releases shift key
  4506. (if (artist-shift-has-changed shift-state ev)
  4507. ;; First check that the draw-how is the same as we
  4508. ;; already have. Otherwise, ignore the changed shift-state.
  4509. (if (not (eq draw-how
  4510. (artist-go-get-draw-how-from-symbol
  4511. (if (not shift-state) shifted unshifted))))
  4512. (message "Cannot switch to shifted operation")
  4513. (message "Switching")
  4514. ;; progn is "implicit" since this is the else-part
  4515. (setq shift-state (not shift-state))
  4516. (setq op (if shift-state shifted unshifted))
  4517. (setq draw-how (artist-go-get-draw-how-from-symbol op))
  4518. (setq draw-fn (artist-go-get-draw-fn-from-symbol op))
  4519. (setq undraw-fn (artist-go-get-undraw-fn-from-symbol op))
  4520. (setq fill-pred (artist-go-get-fill-pred-from-symbol op))
  4521. (setq fill-fn (artist-go-get-fill-fn-from-symbol op))))
  4522. ;; Draw the new shape
  4523. (if artist-rubber-banding
  4524. (setq shape (artist-funcall draw-fn x1 y1 x2 y2))
  4525. (artist-no-rb-set-point2 x2 y2))
  4526. ;; Move cursor
  4527. (artist-move-to-xy x2 y2))
  4528. ;; Read next event
  4529. (setq ev (read-event))))
  4530. ;; If we are not rubber-banding (that is, we were moving around the `2')
  4531. ;; draw the shape
  4532. (if (not artist-rubber-banding)
  4533. (progn
  4534. (artist-no-rb-unset-points)
  4535. (setq shape (artist-funcall draw-fn x1 y1 x2 y2))))
  4536. (artist-funcall prep-fill-fn shape x1 y1 x2 y2)
  4537. ;; Maybe fill
  4538. (if (artist-funcall fill-pred)
  4539. (artist-funcall fill-fn shape x1 y1 x2 y2))
  4540. ;; Maybe set arrow-points
  4541. (if (artist-funcall arrow-pred)
  4542. (artist-funcall arrow-set-fn shape x1 y1 x2 y2)
  4543. (artist-clear-arrow-points))
  4544. (artist-funcall exit-fn shape x1 y1 x2 y2)
  4545. (artist-move-to-xy x2 y2)))
  4546. ;;
  4547. ;; Bug-report-submitting
  4548. ;;
  4549. (defun artist-submit-bug-report ()
  4550. "Submit via mail a bug report on Artist."
  4551. (interactive)
  4552. (require 'reporter)
  4553. (if (y-or-n-p "Do you want to submit a bug report on Artist? ")
  4554. (let ((to artist-maintainer-address)
  4555. (vars '(window-system
  4556. window-system-version
  4557. ;;
  4558. artist-rubber-banding
  4559. artist-interface-with-rect
  4560. artist-aspect-ratio
  4561. ;; Now the internal ones
  4562. artist-curr-go
  4563. artist-key-poly-point-list
  4564. artist-key-shape
  4565. artist-key-draw-how
  4566. artist-arrow-point-1
  4567. artist-arrow-point-2)))
  4568. ;; Remove those variables from vars that are not bound
  4569. (mapc
  4570. (function
  4571. (lambda (x)
  4572. (if (not (and (boundp x) (symbol-value x)))
  4573. (setq vars (delq x vars))))) vars)
  4574. (reporter-submit-bug-report
  4575. artist-maintainer-address
  4576. (concat "artist.el " artist-version)
  4577. vars
  4578. nil nil
  4579. (concat "Hello Tomas,\n\n"
  4580. "I have a nice bug report on Artist for you! Here it is:")))))
  4581. ;;
  4582. ;; Now provide this minor mode
  4583. ;;
  4584. (provide 'artist)
  4585. ;;; About adding drawing modes
  4586. ;;; --------------------------
  4587. ;; If you are going to add a new drawing mode, read the following
  4588. ;; sketchy outlines to get started a bit easier.
  4589. ;;
  4590. ;; 1. If your new drawing mode falls into one of the following
  4591. ;; categories, goto point 2, otherwise goto point 3.
  4592. ;;
  4593. ;; - Modes where the shapes are drawn continuously, as long as
  4594. ;; the mouse button is held down (continuous modes).
  4595. ;; Example: the erase-char mode, the pen and pen-line modes.
  4596. ;;
  4597. ;; - Modes where the shape is made up of from 2 points to an
  4598. ;; arbitrary number of points (poly-point modes).
  4599. ;; Example: the poly-line mode
  4600. ;;
  4601. ;; - Modes where the shape is made up of 2 points (2-point
  4602. ;; modes).
  4603. ;; Example: lines, rectangles
  4604. ;;
  4605. ;; - Modes where the shape is made up of 1 point (1-point
  4606. ;; modes). This mode differs from the continuous modes in
  4607. ;; that the shape is drawn only once when the mouse button
  4608. ;; is pressed.
  4609. ;; Examples: paste, a flood-fill, vaporize modes
  4610. ;;
  4611. ;;
  4612. ;; 2. To make it easier and more flexible to program new drawing
  4613. ;; modes, you might choose to specify
  4614. ;; init-fn: a function to be called at the very beginning
  4615. ;; of the drawing phase,
  4616. ;; prep-fill-fn: a function to be called before filling,
  4617. ;; arrow-set-fn: a function for setting arrows, to be called
  4618. ;; after filling, and
  4619. ;; exit-fn: a function to be called at the very end of
  4620. ;; the drawing phase.
  4621. ;; For each of the cases below, the arguments given to the init-fn,
  4622. ;; prep-fill-fn, arrow-set-fn and exit-fn are stated.
  4623. ;;
  4624. ;; If your mode matches the continuous mode or the 1-point mode:
  4625. ;;
  4626. ;; a. Create a draw-function that draws your shape. Your function
  4627. ;; must take x and y as arguments. The return value is not
  4628. ;; used.
  4629. ;;
  4630. ;; b. Add your mode to the master table, `artist-mt'.
  4631. ;;
  4632. ;; init-fn: x y
  4633. ;; prep-fill-fn: x y
  4634. ;; arrow-set-fn: x y
  4635. ;; exit-fn: x y
  4636. ;;
  4637. ;; If your mode matches the 2-point mode:
  4638. ;;
  4639. ;; a. Create one draw-function that draws your shape and one
  4640. ;; undraw-function that undraws it.
  4641. ;;
  4642. ;; The draw-function must take x1, y1, x2 and y2 as
  4643. ;; arguments. It must return a list with three elements:
  4644. ;; Endpoint1: a vector [x1 y1]
  4645. ;; Endpoint2: a vector [x2 y2]
  4646. ;; Shapeinfo: all info necessary for your undraw-function to
  4647. ;; be able to undraw the shape
  4648. ;; Use the artist-endpoint-* accessors to create and inspect
  4649. ;; the endpoints.
  4650. ;;
  4651. ;; If applicable, you must be able to draw your shape without
  4652. ;; borders if the `artist-borderless-shapes' is non-nil.
  4653. ;; See `artist-draw-rect' for an example.
  4654. ;;
  4655. ;; The undraw-function must take one argument: the list created
  4656. ;; by your draw-function. The return value is not used.
  4657. ;;
  4658. ;; b. If you want to provide a fill-function, then create a
  4659. ;; function that takes 5 arguments: the list created by your
  4660. ;; draw-function, x1, y1, x2 and y2. The return value is not
  4661. ;; used.
  4662. ;;
  4663. ;; c. Add your mode to the master table, `artist-mt'.
  4664. ;;
  4665. ;; init-fn: x1 y1
  4666. ;; prep-fill-fn: shape x1 y1 x2 y2
  4667. ;; arrow-set-fn: shape x1 y1 x2 y2
  4668. ;; exit-fn: shape x1 y1 x2 y2
  4669. ;;
  4670. ;; If your mode matches the poly-point mode:
  4671. ;;
  4672. ;; a. Create one draw-function that draws your shape and one
  4673. ;; undraw-function that undraws it. The draw- and
  4674. ;; undraw-functions are used to draw/undraw a segment of
  4675. ;; your poly-point mode between 2 points. The draw- and
  4676. ;; undraw-functions are then really 2-point mode functions.
  4677. ;; They must take the same arguments and return the same
  4678. ;; values as those of the 2-point mode.
  4679. ;;
  4680. ;; If applicable, you must be able to draw your shape without
  4681. ;; borders if the `artist-borderless-shapes' is non-nil.
  4682. ;; See `artist-draw-rect' for an example.
  4683. ;;
  4684. ;; b. If you want to provide a fill-function, then create a
  4685. ;; function that takes 1 argument: a list of points where each
  4686. ;; point is a vector, [x, y].
  4687. ;;
  4688. ;; c. Add your mode to the master table, `artist-mt'.
  4689. ;;
  4690. ;; init-fn: x1 y1
  4691. ;; prep-fill-fn: point-list
  4692. ;; arrow-set-fn: point-list
  4693. ;; exit-fn: point-list
  4694. ;;
  4695. ;; The arrow-set-fn must set the variables `artist-arrow-point-1'
  4696. ;; and `artist-arrow-point-2'. If your mode does not take arrows,
  4697. ;; you must set the variables to nil. Use the accessors
  4698. ;; artist-arrow-point-* to create and inspect arrow-points.
  4699. ;;
  4700. ;;
  4701. ;; 3. If your mode doesn't match any of the categories, you are facing
  4702. ;; a bit more work, and I cannot be as detailed as above. Here is a
  4703. ;; brief outline of what you have to do:
  4704. ;;
  4705. ;; a. Decide on a name for your type of mode. Let's assume that
  4706. ;; you decided on `xxx'. Then you should use the draw-how
  4707. ;; symbol artist-do-xxx.
  4708. ;;
  4709. ;; b. Create a function artist-mouse-draw-xxx for drawing with
  4710. ;; mouse. It should be called from `artist-down-mouse-1'.
  4711. ;;
  4712. ;; The all coordinates must be converted from window-relative
  4713. ;; to buffer relative before saved or handed over to
  4714. ;; any other function. Converting is done with
  4715. ;; the function `artist-coord-win-to-buf'.
  4716. ;;
  4717. ;; It must take care to the `artist-rubber-banding' variable
  4718. ;; and perform rubber-banding accordingly. Use the
  4719. ;; artist-no-rb-* functions if not rubber-banding.
  4720. ;;
  4721. ;; If applicable, you must be able to draw your shape without
  4722. ;; borders if the `artist-borderless-shapes' is non-nil.
  4723. ;; See `artist-draw-rect' for an example.
  4724. ;;
  4725. ;; You must call the init-fn, the prep-fill-fn, arrow-set-fn
  4726. ;; and the exit-fn at the appropriate points.
  4727. ;;
  4728. ;; When artist-mouse-draw-xxx ends, the shape for your mode
  4729. ;; must be completely drawn.
  4730. ;;
  4731. ;; c. Create functions for drawing with keys:
  4732. ;;
  4733. ;; - artist-key-set-point-xxx for setting a point in the
  4734. ;; mode, to be called from `artist-key-set-point-common'.
  4735. ;;
  4736. ;; - artist-key-do-continuously-xxx to be called from
  4737. ;; `artist-key-do-continuously-common' whenever the user
  4738. ;; moves around.
  4739. ;;
  4740. ;; As for the artist-mouse-draw-xxx, these two functions must
  4741. ;; take care to do rubber-banding, borderless shapes and to
  4742. ;; set arrows.
  4743. ;;
  4744. ;; These functions should set the variable `artist-key-shape'
  4745. ;; to the shape drawn.
  4746. ;;
  4747. ;; d. Create artist-key-draw-xxx and artist-key-undraw-xxx for
  4748. ;; drawing and undrawing. These are needed when the user
  4749. ;; switches operation to draw another shape of the same type
  4750. ;; of drawing mode.
  4751. ;;
  4752. ;; You should provide these functions. You might think that
  4753. ;; only you is using your type of mode, so no one will be able
  4754. ;; to switch to another operation of the same type of mode,
  4755. ;; but someone else might base a new drawing mode upon your
  4756. ;; work.
  4757. ;;
  4758. ;; You must call the init-fn, the prep-fill-fn, arrow-set-fn
  4759. ;; and the exit-fn at the appropriate points.
  4760. ;;
  4761. ;; e. Add your new mode to the master table, `artist-mt'.
  4762. ;;
  4763. ;;
  4764. ;; Happy hacking! Please let me hear if you add any drawing modes!
  4765. ;; Don't hesitate to ask me any questions.
  4766. ;;; artist.el ends here