org.el 550 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755127561275712758127591276012761127621276312764127651276612767127681276912770127711277212773127741277512776127771277812779127801278112782127831278412785127861278712788127891279012791127921279312794127951279612797127981279912800128011280212803128041280512806128071280812809128101281112812128131281412815128161281712818128191282012821128221282312824128251282612827128281282912830128311283212833128341283512836128371283812839128401284112842128431284412845128461284712848128491285012851128521285312854128551285612857128581285912860128611286212863128641286512866128671286812869128701287112872128731287412875128761287712878128791288012881128821288312884128851288612887128881288912890128911289212893128941289512896128971289812899129001290112902129031290412905129061290712908129091291012911129121291312914129151291612917129181291912920129211292212923129241292512926129271292812929129301293112932129331293412935129361293712938129391294012941129421294312944129451294612947129481294912950129511295212953129541295512956129571295812959129601296112962129631296412965129661296712968129691297012971129721297312974129751297612977129781297912980129811298212983129841298512986129871298812989129901299112992129931299412995129961299712998129991300013001130021300313004130051300613007130081300913010130111301213013130141301513016130171301813019130201302113022130231302413025130261302713028130291303013031130321303313034130351303613037130381303913040130411304213043130441304513046130471304813049130501305113052130531305413055130561305713058130591306013061130621306313064130651306613067130681306913070130711307213073130741307513076130771307813079130801308113082130831308413085130861308713088130891309013091130921309313094130951309613097130981309913100131011310213103131041310513106131071310813109131101311113112131131311413115131161311713118131191312013121131221312313124131251312613127131281312913130131311313213133131341313513136131371313813139131401314113142131431314413145131461314713148131491315013151131521315313154131551315613157131581315913160131611316213163131641316513166131671316813169131701317113172131731317413175131761317713178131791318013181131821318313184131851318613187131881318913190131911319213193131941319513196131971319813199132001320113202132031320413205132061320713208132091321013211132121321313214132151321613217132181321913220132211322213223132241322513226132271322813229132301323113232132331323413235132361323713238132391324013241132421324313244132451324613247132481324913250132511325213253132541325513256132571325813259132601326113262132631326413265132661326713268132691327013271132721327313274132751327613277132781327913280132811328213283132841328513286132871328813289132901329113292132931329413295132961329713298132991330013301133021330313304133051330613307133081330913310133111331213313133141331513316133171331813319133201332113322133231332413325133261332713328133291333013331133321333313334133351333613337133381333913340133411334213343133441334513346133471334813349133501335113352133531335413355133561335713358133591336013361133621336313364133651336613367133681336913370133711337213373133741337513376133771337813379133801338113382133831338413385133861338713388133891339013391133921339313394133951339613397133981339913400134011340213403134041340513406134071340813409134101341113412134131341413415134161341713418134191342013421134221342313424134251342613427134281342913430134311343213433134341343513436134371343813439134401344113442134431344413445134461344713448134491345013451134521345313454134551345613457134581345913460134611346213463134641346513466134671346813469134701347113472134731347413475134761347713478134791348013481134821348313484134851348613487134881348913490134911349213493134941349513496134971349813499135001350113502135031350413505135061350713508135091351013511135121351313514135151351613517135181351913520135211352213523135241352513526135271352813529135301353113532135331353413535135361353713538135391354013541135421354313544135451354613547135481354913550135511355213553135541355513556135571355813559135601356113562135631356413565135661356713568135691357013571135721357313574135751357613577135781357913580135811358213583135841358513586135871358813589135901359113592135931359413595135961359713598135991360013601136021360313604136051360613607136081360913610136111361213613136141361513616136171361813619136201362113622136231362413625136261362713628136291363013631136321363313634136351363613637136381363913640136411364213643136441364513646136471364813649136501365113652136531365413655136561365713658136591366013661136621366313664136651366613667136681366913670136711367213673136741367513676136771367813679136801368113682136831368413685136861368713688136891369013691136921369313694136951369613697136981369913700137011370213703137041370513706137071370813709137101371113712137131371413715137161371713718137191372013721137221372313724137251372613727137281372913730137311373213733137341373513736137371373813739137401374113742137431374413745137461374713748137491375013751137521375313754137551375613757137581375913760137611376213763137641376513766137671376813769137701377113772137731377413775137761377713778137791378013781137821378313784137851378613787137881378913790137911379213793137941379513796137971379813799138001380113802138031380413805138061380713808138091381013811138121381313814138151381613817138181381913820138211382213823138241382513826138271382813829138301383113832138331383413835138361383713838138391384013841138421384313844138451384613847138481384913850138511385213853138541385513856138571385813859138601386113862138631386413865138661386713868138691387013871138721387313874138751387613877138781387913880138811388213883138841388513886138871388813889138901389113892138931389413895138961389713898138991390013901139021390313904139051390613907139081390913910139111391213913139141391513916139171391813919139201392113922139231392413925139261392713928139291393013931139321393313934139351393613937139381393913940139411394213943139441394513946139471394813949139501395113952139531395413955139561395713958139591396013961139621396313964139651396613967139681396913970139711397213973139741397513976139771397813979139801398113982139831398413985139861398713988139891399013991139921399313994139951399613997139981399914000140011400214003140041400514006140071400814009140101401114012140131401414015140161401714018140191402014021140221402314024140251402614027140281402914030140311403214033140341403514036140371403814039140401404114042140431404414045140461404714048140491405014051140521405314054140551405614057140581405914060140611406214063140641406514066140671406814069140701407114072140731407414075140761407714078140791408014081140821408314084140851408614087140881408914090140911409214093140941409514096140971409814099141001410114102141031410414105141061410714108141091411014111141121411314114141151411614117141181411914120141211412214123141241412514126141271412814129141301413114132141331413414135141361413714138141391414014141141421414314144141451414614147141481414914150141511415214153141541415514156141571415814159141601416114162141631416414165141661416714168141691417014171141721417314174141751417614177141781417914180141811418214183141841418514186141871418814189141901419114192141931419414195141961419714198141991420014201142021420314204142051420614207142081420914210142111421214213142141421514216142171421814219142201422114222142231422414225142261422714228142291423014231142321423314234142351423614237142381423914240142411424214243142441424514246142471424814249142501425114252142531425414255142561425714258142591426014261142621426314264142651426614267142681426914270142711427214273142741427514276142771427814279142801428114282142831428414285142861428714288142891429014291142921429314294142951429614297142981429914300143011430214303143041430514306143071430814309143101431114312143131431414315143161431714318143191432014321143221432314324143251432614327143281432914330143311433214333143341433514336143371433814339143401434114342143431434414345143461434714348143491435014351143521435314354143551435614357143581435914360143611436214363143641436514366143671436814369143701437114372143731437414375143761437714378143791438014381143821438314384143851438614387143881438914390143911439214393143941439514396143971439814399144001440114402144031440414405144061440714408144091441014411144121441314414144151441614417144181441914420144211442214423144241442514426144271442814429144301443114432144331443414435144361443714438144391444014441144421444314444144451444614447144481444914450144511445214453144541445514456144571445814459144601446114462144631446414465144661446714468144691447014471144721447314474144751447614477144781447914480144811448214483144841448514486144871448814489144901449114492144931449414495144961449714498144991450014501145021450314504145051450614507145081450914510145111451214513145141451514516145171451814519145201452114522145231452414525145261452714528145291453014531145321453314534145351453614537145381453914540145411454214543145441454514546145471454814549145501455114552145531455414555145561455714558145591456014561145621456314564145651456614567145681456914570145711457214573145741457514576145771457814579145801458114582145831458414585145861458714588145891459014591145921459314594145951459614597145981459914600146011460214603146041460514606146071460814609146101461114612146131461414615146161461714618146191462014621146221462314624146251462614627146281462914630146311463214633146341463514636146371463814639146401464114642146431464414645146461464714648146491465014651146521465314654146551465614657146581465914660146611466214663146641466514666146671466814669146701467114672146731467414675146761467714678146791468014681146821468314684146851468614687146881468914690146911469214693146941469514696146971469814699147001470114702147031470414705147061470714708147091471014711147121471314714147151471614717147181471914720147211472214723147241472514726147271472814729147301473114732147331473414735147361473714738147391474014741147421474314744147451474614747147481474914750147511475214753147541475514756147571475814759147601476114762147631476414765147661476714768147691477014771147721477314774147751477614777147781477914780147811478214783147841478514786147871478814789147901479114792147931479414795147961479714798147991480014801148021480314804148051480614807148081480914810148111481214813148141481514816148171481814819148201482114822148231482414825148261482714828148291483014831
  1. ;;; org.el --- Outline-based notes management and organizer
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <carsten at orgmode dot org>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://orgmode.org
  8. ;; Version: 6.16trans
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.16trans"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-modules'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (when (org-bound-and-true-p org-modules)
  125. (let ((a (member 'org-infojs org-modules)))
  126. (and a (setcar a 'org-jsinfo))))
  127. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  128. "Modules that should always be loaded together with org.el.
  129. If a description starts with <C>, the file is not part of Emacs
  130. and loading it will require that you have downloaded and properly installed
  131. the org-mode distribution.
  132. You can also use this system to load external packages (i.e. neither Org
  133. core modules, not modules from the CONTRIB directory). Just add symbols
  134. to the end of the list. If the package is called org-xyz.el, then you need
  135. to add the symbol `xyz', and the package must have a call to
  136. (provide 'org-xyz)"
  137. :group 'org
  138. :set 'org-set-modules
  139. :type
  140. '(set :greedy t
  141. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  142. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  143. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  144. (const :tag " id: Global IDs for identifying entries" org-id)
  145. (const :tag " info: Links to Info nodes" org-info)
  146. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  147. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  148. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  149. (const :tag " mew Links to Mew folders/messages" org-mew)
  150. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  151. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  152. (const :tag " vm: Links to VM folders/messages" org-vm)
  153. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  154. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  155. (const :tag " mouse: Additional mouse support" org-mouse)
  156. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  157. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  158. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  159. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  160. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  161. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  162. (const :tag "C eval: Include command output as text" org-eval)
  163. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  164. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  165. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  166. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  167. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  168. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  169. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  170. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  171. (const :tag "C registry: A registry for Org links" org-registry)
  172. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  173. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  174. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  175. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  176. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  177. (defgroup org-startup nil
  178. "Options concerning startup of Org-mode."
  179. :tag "Org Startup"
  180. :group 'org)
  181. (defcustom org-startup-folded t
  182. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  183. This can also be configured on a per-file basis by adding one of
  184. the following lines anywhere in the buffer:
  185. #+STARTUP: fold
  186. #+STARTUP: nofold
  187. #+STARTUP: content"
  188. :group 'org-startup
  189. :type '(choice
  190. (const :tag "nofold: show all" nil)
  191. (const :tag "fold: overview" t)
  192. (const :tag "content: all headlines" content)))
  193. (defcustom org-startup-truncated t
  194. "Non-nil means, entering Org-mode will set `truncate-lines'.
  195. This is useful since some lines containing links can be very long and
  196. uninteresting. Also tables look terrible when wrapped."
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-startup-align-all-tables nil
  200. "Non-nil means, align all tables when visiting a file.
  201. This is useful when the column width in tables is forced with <N> cookies
  202. in table fields. Such tables will look correct only after the first re-align.
  203. This can also be configured on a per-file basis by adding one of
  204. the following lines anywhere in the buffer:
  205. #+STARTUP: align
  206. #+STARTUP: noalign"
  207. :group 'org-startup
  208. :type 'boolean)
  209. (defcustom org-insert-mode-line-in-empty-file nil
  210. "Non-nil means insert the first line setting Org-mode in empty files.
  211. When the function `org-mode' is called interactively in an empty file, this
  212. normally means that the file name does not automatically trigger Org-mode.
  213. To ensure that the file will always be in Org-mode in the future, a
  214. line enforcing Org-mode will be inserted into the buffer, if this option
  215. has been set."
  216. :group 'org-startup
  217. :type 'boolean)
  218. (defcustom org-replace-disputed-keys nil
  219. "Non-nil means use alternative key bindings for some keys.
  220. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  221. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  222. If you want to use Org-mode together with one of these other modes,
  223. or more generally if you would like to move some Org-mode commands to
  224. other keys, set this variable and configure the keys with the variable
  225. `org-disputed-keys'.
  226. This option is only relevant at load-time of Org-mode, and must be set
  227. *before* org.el is loaded. Changing it requires a restart of Emacs to
  228. become effective."
  229. :group 'org-startup
  230. :type 'boolean)
  231. (defcustom org-use-extra-keys nil
  232. "Non-nil means use extra key sequence definitions for certain
  233. commands. This happens automatically if you run XEmacs or if
  234. window-system is nil. This variable lets you do the same
  235. manually. You must set it before loading org.
  236. Example: on Carbon Emacs 22 running graphically, with an external
  237. keyboard on a Powerbook, the default way of setting M-left might
  238. not work for either Alt or ESC. Setting this variable will make
  239. it work for ESC."
  240. :group 'org-startup
  241. :type 'boolean)
  242. (if (fboundp 'defvaralias)
  243. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  244. (defcustom org-disputed-keys
  245. '(([(shift up)] . [(meta p)])
  246. ([(shift down)] . [(meta n)])
  247. ([(shift left)] . [(meta -)])
  248. ([(shift right)] . [(meta +)])
  249. ([(control shift right)] . [(meta shift +)])
  250. ([(control shift left)] . [(meta shift -)]))
  251. "Keys for which Org-mode and other modes compete.
  252. This is an alist, cars are the default keys, second element specifies
  253. the alternative to use when `org-replace-disputed-keys' is t.
  254. Keys can be specified in any syntax supported by `define-key'.
  255. The value of this option takes effect only at Org-mode's startup,
  256. therefore you'll have to restart Emacs to apply it after changing."
  257. :group 'org-startup
  258. :type 'alist)
  259. (defun org-key (key)
  260. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  261. Or return the original if not disputed."
  262. (if org-replace-disputed-keys
  263. (let* ((nkey (key-description key))
  264. (x (org-find-if (lambda (x)
  265. (equal (key-description (car x)) nkey))
  266. org-disputed-keys)))
  267. (if x (cdr x) key))
  268. key))
  269. (defun org-find-if (predicate seq)
  270. (catch 'exit
  271. (while seq
  272. (if (funcall predicate (car seq))
  273. (throw 'exit (car seq))
  274. (pop seq)))))
  275. (defun org-defkey (keymap key def)
  276. "Define a key, possibly translated, as returned by `org-key'."
  277. (define-key keymap (org-key key) def))
  278. (defcustom org-ellipsis nil
  279. "The ellipsis to use in the Org-mode outline.
  280. When nil, just use the standard three dots. When a string, use that instead,
  281. When a face, use the standard 3 dots, but with the specified face.
  282. The change affects only Org-mode (which will then use its own display table).
  283. Changing this requires executing `M-x org-mode' in a buffer to become
  284. effective."
  285. :group 'org-startup
  286. :type '(choice (const :tag "Default" nil)
  287. (face :tag "Face" :value org-warning)
  288. (string :tag "String" :value "...#")))
  289. (defvar org-display-table nil
  290. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  291. (defgroup org-keywords nil
  292. "Keywords in Org-mode."
  293. :tag "Org Keywords"
  294. :group 'org)
  295. (defcustom org-deadline-string "DEADLINE:"
  296. "String to mark deadline entries.
  297. A deadline is this string, followed by a time stamp. Should be a word,
  298. terminated by a colon. You can insert a schedule keyword and
  299. a timestamp with \\[org-deadline].
  300. Changes become only effective after restarting Emacs."
  301. :group 'org-keywords
  302. :type 'string)
  303. (defcustom org-scheduled-string "SCHEDULED:"
  304. "String to mark scheduled TODO entries.
  305. A schedule is this string, followed by a time stamp. Should be a word,
  306. terminated by a colon. You can insert a schedule keyword and
  307. a timestamp with \\[org-schedule].
  308. Changes become only effective after restarting Emacs."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defcustom org-closed-string "CLOSED:"
  312. "String used as the prefix for timestamps logging closing a TODO entry."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defcustom org-clock-string "CLOCK:"
  316. "String used as prefix for timestamps clocking work hours on an item."
  317. :group 'org-keywords
  318. :type 'string)
  319. (defcustom org-comment-string "COMMENT"
  320. "Entries starting with this keyword will never be exported.
  321. An entry can be toggled between COMMENT and normal with
  322. \\[org-toggle-comment].
  323. Changes become only effective after restarting Emacs."
  324. :group 'org-keywords
  325. :type 'string)
  326. (defcustom org-quote-string "QUOTE"
  327. "Entries starting with this keyword will be exported in fixed-width font.
  328. Quoting applies only to the text in the entry following the headline, and does
  329. not extend beyond the next headline, even if that is lower level.
  330. An entry can be toggled between QUOTE and normal with
  331. \\[org-toggle-fixed-width-section]."
  332. :group 'org-keywords
  333. :type 'string)
  334. (defconst org-repeat-re
  335. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  336. "Regular expression for specifying repeated events.
  337. After a match, group 1 contains the repeat expression.")
  338. (defgroup org-structure nil
  339. "Options concerning the general structure of Org-mode files."
  340. :tag "Org Structure"
  341. :group 'org)
  342. (defgroup org-reveal-location nil
  343. "Options about how to make context of a location visible."
  344. :tag "Org Reveal Location"
  345. :group 'org-structure)
  346. (defconst org-context-choice
  347. '(choice
  348. (const :tag "Always" t)
  349. (const :tag "Never" nil)
  350. (repeat :greedy t :tag "Individual contexts"
  351. (cons
  352. (choice :tag "Context"
  353. (const agenda)
  354. (const org-goto)
  355. (const occur-tree)
  356. (const tags-tree)
  357. (const link-search)
  358. (const mark-goto)
  359. (const bookmark-jump)
  360. (const isearch)
  361. (const default))
  362. (boolean))))
  363. "Contexts for the reveal options.")
  364. (defcustom org-show-hierarchy-above '((default . t))
  365. "Non-nil means, show full hierarchy when revealing a location.
  366. Org-mode often shows locations in an org-mode file which might have
  367. been invisible before. When this is set, the hierarchy of headings
  368. above the exposed location is shown.
  369. Turning this off for example for sparse trees makes them very compact.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. Valid contexts are
  372. agenda when exposing an entry from the agenda
  373. org-goto when using the command `org-goto' on key C-c C-j
  374. occur-tree when using the command `org-occur' on key C-c /
  375. tags-tree when constructing a sparse tree based on tags matches
  376. link-search when exposing search matches associated with a link
  377. mark-goto when exposing the jump goal of a mark
  378. bookmark-jump when exposing a bookmark location
  379. isearch when exiting from an incremental search
  380. default default for all contexts not set explicitly"
  381. :group 'org-reveal-location
  382. :type org-context-choice)
  383. (defcustom org-show-following-heading '((default . nil))
  384. "Non-nil means, show following heading when revealing a location.
  385. Org-mode often shows locations in an org-mode file which might have
  386. been invisible before. When this is set, the heading following the
  387. match is shown.
  388. Turning this off for example for sparse trees makes them very compact,
  389. but makes it harder to edit the location of the match. In such a case,
  390. use the command \\[org-reveal] to show more context.
  391. Instead of t, this can also be an alist specifying this option for different
  392. contexts. See `org-show-hierarchy-above' for valid contexts."
  393. :group 'org-reveal-location
  394. :type org-context-choice)
  395. (defcustom org-show-siblings '((default . nil) (isearch t))
  396. "Non-nil means, show all sibling heading when revealing a location.
  397. Org-mode often shows locations in an org-mode file which might have
  398. been invisible before. When this is set, the sibling of the current entry
  399. heading are all made visible. If `org-show-hierarchy-above' is t,
  400. the same happens on each level of the hierarchy above the current entry.
  401. By default this is on for the isearch context, off for all other contexts.
  402. Turning this off for example for sparse trees makes them very compact,
  403. but makes it harder to edit the location of the match. In such a case,
  404. use the command \\[org-reveal] to show more context.
  405. Instead of t, this can also be an alist specifying this option for different
  406. contexts. See `org-show-hierarchy-above' for valid contexts."
  407. :group 'org-reveal-location
  408. :type org-context-choice)
  409. (defcustom org-show-entry-below '((default . nil))
  410. "Non-nil means, show the entry below a headline when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the text below the headline that is
  413. exposed is also shown.
  414. By default this is off for all contexts.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. See `org-show-hierarchy-above' for valid contexts."
  417. :group 'org-reveal-location
  418. :type org-context-choice)
  419. (defcustom org-indirect-buffer-display 'other-window
  420. "How should indirect tree buffers be displayed?
  421. This applies to indirect buffers created with the commands
  422. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  423. Valid values are:
  424. current-window Display in the current window
  425. other-window Just display in another window.
  426. dedicated-frame Create one new frame, and re-use it each time.
  427. new-frame Make a new frame each time. Note that in this case
  428. previously-made indirect buffers are kept, and you need to
  429. kill these buffers yourself."
  430. :group 'org-structure
  431. :group 'org-agenda-windows
  432. :type '(choice
  433. (const :tag "In current window" current-window)
  434. (const :tag "In current frame, other window" other-window)
  435. (const :tag "Each time a new frame" new-frame)
  436. (const :tag "One dedicated frame" dedicated-frame)))
  437. (defgroup org-cycle nil
  438. "Options concerning visibility cycling in Org-mode."
  439. :tag "Org Cycle"
  440. :group 'org-structure)
  441. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  442. "Names of drawers. Drawers are not opened by cycling on the headline above.
  443. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  444. this:
  445. :DRAWERNAME:
  446. .....
  447. :END:
  448. The drawer \"PROPERTIES\" is special for capturing properties through
  449. the property API.
  450. Drawers can be defined on the per-file basis with a line like:
  451. #+DRAWERS: HIDDEN STATE PROPERTIES"
  452. :group 'org-structure
  453. :type '(repeat (string :tag "Drawer Name")))
  454. (defcustom org-cycle-global-at-bob nil
  455. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  456. This makes it possible to do global cycling without having to use S-TAB or
  457. C-u TAB. For this special case to work, the first line of the buffer
  458. must not be a headline - it may be empty ot some other text. When used in
  459. this way, `org-cycle-hook' is disables temporarily, to make sure the
  460. cursor stays at the beginning of the buffer.
  461. When this option is nil, don't do anything special at the beginning
  462. of the buffer."
  463. :group 'org-cycle
  464. :type 'boolean)
  465. (defcustom org-cycle-emulate-tab t
  466. "Where should `org-cycle' emulate TAB.
  467. nil Never
  468. white Only in completely white lines
  469. whitestart Only at the beginning of lines, before the first non-white char
  470. t Everywhere except in headlines
  471. exc-hl-bol Everywhere except at the start of a headline
  472. If TAB is used in a place where it does not emulate TAB, the current subtree
  473. visibility is cycled."
  474. :group 'org-cycle
  475. :type '(choice (const :tag "Never" nil)
  476. (const :tag "Only in completely white lines" white)
  477. (const :tag "Before first char in a line" whitestart)
  478. (const :tag "Everywhere except in headlines" t)
  479. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  480. ))
  481. (defcustom org-cycle-separator-lines 2
  482. "Number of empty lines needed to keep an empty line between collapsed trees.
  483. If you leave an empty line between the end of a subtree and the following
  484. headline, this empty line is hidden when the subtree is folded.
  485. Org-mode will leave (exactly) one empty line visible if the number of
  486. empty lines is equal or larger to the number given in this variable.
  487. So the default 2 means, at least 2 empty lines after the end of a subtree
  488. are needed to produce free space between a collapsed subtree and the
  489. following headline.
  490. Special case: when 0, never leave empty lines in collapsed view."
  491. :group 'org-cycle
  492. :type 'integer)
  493. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  494. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  495. org-cycle-hide-drawers
  496. org-cycle-show-empty-lines
  497. org-optimize-window-after-visibility-change)
  498. "Hook that is run after `org-cycle' has changed the buffer visibility.
  499. The function(s) in this hook must accept a single argument which indicates
  500. the new state that was set by the most recent `org-cycle' command. The
  501. argument is a symbol. After a global state change, it can have the values
  502. `overview', `content', or `all'. After a local state change, it can have
  503. the values `folded', `children', or `subtree'."
  504. :group 'org-cycle
  505. :type 'hook)
  506. (defgroup org-edit-structure nil
  507. "Options concerning structure editing in Org-mode."
  508. :tag "Org Edit Structure"
  509. :group 'org-structure)
  510. (defcustom org-odd-levels-only nil
  511. "Non-nil means, skip even levels and only use odd levels for the outline.
  512. This has the effect that two stars are being added/taken away in
  513. promotion/demotion commands. It also influences how levels are
  514. handled by the exporters.
  515. Changing it requires restart of `font-lock-mode' to become effective
  516. for fontification also in regions already fontified.
  517. You may also set this on a per-file basis by adding one of the following
  518. lines to the buffer:
  519. #+STARTUP: odd
  520. #+STARTUP: oddeven"
  521. :group 'org-edit-structure
  522. :group 'org-font-lock
  523. :type 'boolean)
  524. (defcustom org-adapt-indentation t
  525. "Non-nil means, adapt indentation when promoting and demoting.
  526. When this is set and the *entire* text in an entry is indented, the
  527. indentation is increased by one space in a demotion command, and
  528. decreased by one in a promotion command. If any line in the entry
  529. body starts at column 0, indentation is not changed at all."
  530. :group 'org-edit-structure
  531. :type 'boolean)
  532. (defcustom org-special-ctrl-a/e nil
  533. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  534. When t, `C-a' will bring back the cursor to the beginning of the
  535. headline text, i.e. after the stars and after a possible TODO keyword.
  536. In an item, this will be the position after the bullet.
  537. When the cursor is already at that position, another `C-a' will bring
  538. it to the beginning of the line.
  539. `C-e' will jump to the end of the headline, ignoring the presence of tags
  540. in the headline. A second `C-e' will then jump to the true end of the
  541. line, after any tags.
  542. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  543. and only a directly following, identical keypress will bring the cursor
  544. to the special positions."
  545. :group 'org-edit-structure
  546. :type '(choice
  547. (const :tag "off" nil)
  548. (const :tag "after bullet first" t)
  549. (const :tag "border first" reversed)))
  550. (if (fboundp 'defvaralias)
  551. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  552. (defcustom org-special-ctrl-k nil
  553. "Non-nil means `C-k' will behave specially in headlines.
  554. When nil, `C-k' will call the default `kill-line' command.
  555. When t, the following will happen while the cursor is in the headline:
  556. - When the cursor is at the beginning of a headline, kill the entire
  557. line and possible the folded subtree below the line.
  558. - When in the middle of the headline text, kill the headline up to the tags.
  559. - When after the headline text, kill the tags."
  560. :group 'org-edit-structure
  561. :type 'boolean)
  562. (defcustom org-yank-folded-subtrees t
  563. "Non-nil means, when yanking subtrees, fold them.
  564. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  565. it starts with a heading and all other headings in it are either children
  566. or siblings, then fold all the subtrees. However, do this only if no
  567. text after the yank would be swallowed into a folded tree by this action."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defcustom org-yank-adjusted-subtrees t
  571. "Non-nil means, when yanking subtrees, adjust the level.
  572. With this setting, `org-paste-subtree' is used to insert the subtree, see
  573. this function for details."
  574. :group 'org-edit-structure
  575. :type 'boolean)
  576. (defcustom org-M-RET-may-split-line '((default . t))
  577. "Non-nil means, M-RET will split the line at the cursor position.
  578. When nil, it will go to the end of the line before making a
  579. new line.
  580. You may also set this option in a different way for different
  581. contexts. Valid contexts are:
  582. headline when creating a new headline
  583. item when creating a new item
  584. table in a table field
  585. default the value to be used for all contexts not explicitly
  586. customized"
  587. :group 'org-structure
  588. :group 'org-table
  589. :type '(choice
  590. (const :tag "Always" t)
  591. (const :tag "Never" nil)
  592. (repeat :greedy t :tag "Individual contexts"
  593. (cons
  594. (choice :tag "Context"
  595. (const headline)
  596. (const item)
  597. (const table)
  598. (const default))
  599. (boolean)))))
  600. (defcustom org-insert-heading-respect-content nil
  601. "Non-nil means, insert new headings after the current subtree.
  602. When nil, the new heading is created directly after the current line.
  603. The commands \\[org-insert-heading-respect-content] and
  604. \\[org-insert-todo-heading-respect-content] turn this variable on
  605. for the duration of the command."
  606. :group 'org-structure
  607. :type 'boolean)
  608. (defcustom org-blank-before-new-entry '((heading . nil)
  609. (plain-list-item . nil))
  610. "Should `org-insert-heading' leave a blank line before new heading/item?
  611. The value is an alist, with `heading' and `plain-list-item' as car,
  612. and a boolean flag as cdr."
  613. :group 'org-edit-structure
  614. :type '(list
  615. (cons (const heading) (boolean))
  616. (cons (const plain-list-item) (boolean))))
  617. (defcustom org-insert-heading-hook nil
  618. "Hook being run after inserting a new heading."
  619. :group 'org-edit-structure
  620. :type 'hook)
  621. (defcustom org-enable-fixed-width-editor t
  622. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  623. This currently only means, they are never auto-wrapped.
  624. When nil, such lines will be treated like ordinary lines.
  625. See also the QUOTE keyword."
  626. :group 'org-edit-structure
  627. :type 'boolean)
  628. (defcustom org-edit-src-region-extra nil
  629. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  630. For examples see the function `org-edit-src-find-region-and-lang'.
  631. The regular expression identifying the begin marker should end with a newline,
  632. and the regexp marking the end line should start with a newline, to make sure
  633. there are kept outside the narrowed region."
  634. :group 'org-edit-structure
  635. :type '(repeat
  636. (list
  637. (regexp :tag "begin regexp")
  638. (regexp :tag "end regexp")
  639. (choice :tag "language"
  640. (string :tag "specify")
  641. (integer :tag "from match group")
  642. (const :tag "from `lang' element")
  643. (const :tag "from `style' element")))))
  644. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  645. "The mode that should be used to edit fixed-width regions.
  646. These are the regions where each line starts with a colon."
  647. :group 'org-edit-structure
  648. :type '(choice
  649. (const artist-mode)
  650. (const picture-mode)
  651. (const fundamental-mode)
  652. (function :tag "Other (specify)")))
  653. (defcustom org-goto-auto-isearch t
  654. "Non-nil means, typing characters in org-goto starts incremental search."
  655. :group 'org-edit-structure
  656. :type 'boolean)
  657. (defgroup org-sparse-trees nil
  658. "Options concerning sparse trees in Org-mode."
  659. :tag "Org Sparse Trees"
  660. :group 'org-structure)
  661. (defcustom org-highlight-sparse-tree-matches t
  662. "Non-nil means, highlight all matches that define a sparse tree.
  663. The highlights will automatically disappear the next time the buffer is
  664. changed by an edit command."
  665. :group 'org-sparse-trees
  666. :type 'boolean)
  667. (defcustom org-remove-highlights-with-change t
  668. "Non-nil means, any change to the buffer will remove temporary highlights.
  669. Such highlights are created by `org-occur' and `org-clock-display'.
  670. When nil, `C-c C-c needs to be used to get rid of the highlights.
  671. The highlights created by `org-preview-latex-fragment' always need
  672. `C-c C-c' to be removed."
  673. :group 'org-sparse-trees
  674. :group 'org-time
  675. :type 'boolean)
  676. (defcustom org-occur-hook '(org-first-headline-recenter)
  677. "Hook that is run after `org-occur' has constructed a sparse tree.
  678. This can be used to recenter the window to show as much of the structure
  679. as possible."
  680. :group 'org-sparse-trees
  681. :type 'hook)
  682. (defgroup org-imenu-and-speedbar nil
  683. "Options concerning imenu and speedbar in Org-mode."
  684. :tag "Org Imenu and Speedbar"
  685. :group 'org-structure)
  686. (defcustom org-imenu-depth 2
  687. "The maximum level for Imenu access to Org-mode headlines.
  688. This also applied for speedbar access."
  689. :group 'org-imenu-and-speedbar
  690. :type 'number)
  691. (defgroup org-table nil
  692. "Options concerning tables in Org-mode."
  693. :tag "Org Table"
  694. :group 'org)
  695. (defcustom org-enable-table-editor 'optimized
  696. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  697. When nil, such lines will be treated like ordinary lines.
  698. When equal to the symbol `optimized', the table editor will be optimized to
  699. do the following:
  700. - Automatic overwrite mode in front of whitespace in table fields.
  701. This makes the structure of the table stay in tact as long as the edited
  702. field does not exceed the column width.
  703. - Minimize the number of realigns. Normally, the table is aligned each time
  704. TAB or RET are pressed to move to another field. With optimization this
  705. happens only if changes to a field might have changed the column width.
  706. Optimization requires replacing the functions `self-insert-command',
  707. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  708. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  709. very good at guessing when a re-align will be necessary, but you can always
  710. force one with \\[org-ctrl-c-ctrl-c].
  711. If you would like to use the optimized version in Org-mode, but the
  712. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  713. This variable can be used to turn on and off the table editor during a session,
  714. but in order to toggle optimization, a restart is required.
  715. See also the variable `org-table-auto-blank-field'."
  716. :group 'org-table
  717. :type '(choice
  718. (const :tag "off" nil)
  719. (const :tag "on" t)
  720. (const :tag "on, optimized" optimized)))
  721. (defcustom org-table-tab-recognizes-table.el t
  722. "Non-nil means, TAB will automatically notice a table.el table.
  723. When it sees such a table, it moves point into it and - if necessary -
  724. calls `table-recognize-table'."
  725. :group 'org-table-editing
  726. :type 'boolean)
  727. (defgroup org-link nil
  728. "Options concerning links in Org-mode."
  729. :tag "Org Link"
  730. :group 'org)
  731. (defvar org-link-abbrev-alist-local nil
  732. "Buffer-local version of `org-link-abbrev-alist', which see.
  733. The value of this is taken from the #+LINK lines.")
  734. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  735. (defcustom org-link-abbrev-alist nil
  736. "Alist of link abbreviations.
  737. The car of each element is a string, to be replaced at the start of a link.
  738. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  739. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  740. [[linkkey:tag][description]]
  741. If REPLACE is a string, the tag will simply be appended to create the link.
  742. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  743. the placeholder \"%h\" will cause a url-encoded version of the tag to
  744. be inserted at that point (see the function `url-hexify-string').
  745. REPLACE may also be a function that will be called with the tag as the
  746. only argument to create the link, which should be returned as a string.
  747. See the manual for examples."
  748. :group 'org-link
  749. :type '(repeat
  750. (cons
  751. (string :tag "Protocol")
  752. (choice
  753. (string :tag "Format")
  754. (function)))))
  755. (defcustom org-descriptive-links t
  756. "Non-nil means, hide link part and only show description of bracket links.
  757. Bracket links are like [[link][description]]. This variable sets the initial
  758. state in new org-mode buffers. The setting can then be toggled on a
  759. per-buffer basis from the Org->Hyperlinks menu."
  760. :group 'org-link
  761. :type 'boolean)
  762. (defcustom org-link-file-path-type 'adaptive
  763. "How the path name in file links should be stored.
  764. Valid values are:
  765. relative Relative to the current directory, i.e. the directory of the file
  766. into which the link is being inserted.
  767. absolute Absolute path, if possible with ~ for home directory.
  768. noabbrev Absolute path, no abbreviation of home directory.
  769. adaptive Use relative path for files in the current directory and sub-
  770. directories of it. For other files, use an absolute path."
  771. :group 'org-link
  772. :type '(choice
  773. (const relative)
  774. (const absolute)
  775. (const noabbrev)
  776. (const adaptive)))
  777. (defcustom org-activate-links '(bracket angle plain radio tag date)
  778. "Types of links that should be activated in Org-mode files.
  779. This is a list of symbols, each leading to the activation of a certain link
  780. type. In principle, it does not hurt to turn on most link types - there may
  781. be a small gain when turning off unused link types. The types are:
  782. bracket The recommended [[link][description]] or [[link]] links with hiding.
  783. angular Links in angular brackets that may contain whitespace like
  784. <bbdb:Carsten Dominik>.
  785. plain Plain links in normal text, no whitespace, like http://google.com.
  786. radio Text that is matched by a radio target, see manual for details.
  787. tag Tag settings in a headline (link to tag search).
  788. date Time stamps (link to calendar).
  789. Changing this variable requires a restart of Emacs to become effective."
  790. :group 'org-link
  791. :type '(set (const :tag "Double bracket links (new style)" bracket)
  792. (const :tag "Angular bracket links (old style)" angular)
  793. (const :tag "Plain text links" plain)
  794. (const :tag "Radio target matches" radio)
  795. (const :tag "Tags" tag)
  796. (const :tag "Timestamps" date)))
  797. (defcustom org-make-link-description-function nil
  798. "Function to use to generate link descriptions from links. If
  799. nil the link location will be used. This function must take two
  800. parameters; the first is the link and the second the description
  801. org-insert-link has generated, and should return the description
  802. to use."
  803. :group 'org-link
  804. :type 'function)
  805. (defgroup org-link-store nil
  806. "Options concerning storing links in Org-mode."
  807. :tag "Org Store Link"
  808. :group 'org-link)
  809. (defcustom org-email-link-description-format "Email %c: %.30s"
  810. "Format of the description part of a link to an email or usenet message.
  811. The following %-escapes will be replaced by corresponding information:
  812. %F full \"From\" field
  813. %f name, taken from \"From\" field, address if no name
  814. %T full \"To\" field
  815. %t first name in \"To\" field, address if no name
  816. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  817. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  818. %s subject
  819. %m message-id.
  820. You may use normal field width specification between the % and the letter.
  821. This is for example useful to limit the length of the subject.
  822. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  823. :group 'org-link-store
  824. :type 'string)
  825. (defcustom org-from-is-user-regexp
  826. (let (r1 r2)
  827. (when (and user-mail-address (not (string= user-mail-address "")))
  828. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  829. (when (and user-full-name (not (string= user-full-name "")))
  830. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  831. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  832. "Regexp matched against the \"From:\" header of an email or usenet message.
  833. It should match if the message is from the user him/herself."
  834. :group 'org-link-store
  835. :type 'regexp)
  836. (defcustom org-link-to-org-use-id 'create-if-interactive
  837. "Non-nil means, storing a link to an Org file will use entry IDs.
  838. Note that before this variable is even considered, org-id must be loaded,
  839. to please customize `org-modules' and turn it on.
  840. The variable can have the following values:
  841. t Create an ID if needed to make a link to the current entry.
  842. create-if-interactive
  843. If `org-store-link' is called directly (interactively, as a user
  844. command), do create an ID to support the link. But when doing the
  845. job for remember, only use the ID if it already exists. The
  846. purpose of this setting is to avoid proliferation of unwanted
  847. IDs, just because you happen to be in an Org file when you
  848. call `org-remember' that automatically and preemptively
  849. creates a link. If you do want to get an ID link in a remember
  850. template to an entry not having an ID, create it first by
  851. explicitly creating a link to it, using `C-c C-l' first.
  852. use-existing
  853. Use existing ID, do not create one.
  854. nil Never use an ID to make a link, instead link using a text search for
  855. the headline text."
  856. :group 'org-link-store
  857. :type '(choice
  858. (const :tag "Create ID to make link" t)
  859. (const :tag "Create if string link interactively"
  860. 'create-if-interactive)
  861. (const :tag "Only use existing" 'use-existing)
  862. (const :tag "Do not use ID to create link" nil)))
  863. (defcustom org-context-in-file-links t
  864. "Non-nil means, file links from `org-store-link' contain context.
  865. A search string will be added to the file name with :: as separator and
  866. used to find the context when the link is activated by the command
  867. `org-open-at-point'.
  868. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  869. negates this setting for the duration of the command."
  870. :group 'org-link-store
  871. :type 'boolean)
  872. (defcustom org-keep-stored-link-after-insertion nil
  873. "Non-nil means, keep link in list for entire session.
  874. The command `org-store-link' adds a link pointing to the current
  875. location to an internal list. These links accumulate during a session.
  876. The command `org-insert-link' can be used to insert links into any
  877. Org-mode file (offering completion for all stored links). When this
  878. option is nil, every link which has been inserted once using \\[org-insert-link]
  879. will be removed from the list, to make completing the unused links
  880. more efficient."
  881. :group 'org-link-store
  882. :type 'boolean)
  883. (defgroup org-link-follow nil
  884. "Options concerning following links in Org-mode."
  885. :tag "Org Follow Link"
  886. :group 'org-link)
  887. (defcustom org-link-translation-function nil
  888. "Function to translate links with different syntax to Org syntax.
  889. This can be used to translate links created for example by the Planner
  890. or emacs-wiki packages to Org syntax.
  891. The function must accept two parameters, a TYPE containing the link
  892. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  893. which is everything after the link protocol. It should return a cons
  894. with possibly modified values of type and path.
  895. Org contains a function for this, so if you set this variable to
  896. `org-translate-link-from-planner', you should be able follow many
  897. links created by planner."
  898. :group 'org-link-follow
  899. :type 'function)
  900. (defcustom org-follow-link-hook nil
  901. "Hook that is run after a link has been followed."
  902. :group 'org-link-follow
  903. :type 'hook)
  904. (defcustom org-tab-follows-link nil
  905. "Non-nil means, on links TAB will follow the link.
  906. Needs to be set before org.el is loaded."
  907. :group 'org-link-follow
  908. :type 'boolean)
  909. (defcustom org-return-follows-link nil
  910. "Non-nil means, on links RET will follow the link.
  911. Needs to be set before org.el is loaded."
  912. :group 'org-link-follow
  913. :type 'boolean)
  914. (defcustom org-mouse-1-follows-link
  915. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  916. "Non-nil means, mouse-1 on a link will follow the link.
  917. A longer mouse click will still set point. Does not work on XEmacs.
  918. Needs to be set before org.el is loaded."
  919. :group 'org-link-follow
  920. :type 'boolean)
  921. (defcustom org-mark-ring-length 4
  922. "Number of different positions to be recorded in the ring
  923. Changing this requires a restart of Emacs to work correctly."
  924. :group 'org-link-follow
  925. :type 'integer)
  926. (defcustom org-link-frame-setup
  927. '((vm . vm-visit-folder-other-frame)
  928. (gnus . gnus-other-frame)
  929. (file . find-file-other-window))
  930. "Setup the frame configuration for following links.
  931. When following a link with Emacs, it may often be useful to display
  932. this link in another window or frame. This variable can be used to
  933. set this up for the different types of links.
  934. For VM, use any of
  935. `vm-visit-folder'
  936. `vm-visit-folder-other-frame'
  937. For Gnus, use any of
  938. `gnus'
  939. `gnus-other-frame'
  940. `org-gnus-no-new-news'
  941. For FILE, use any of
  942. `find-file'
  943. `find-file-other-window'
  944. `find-file-other-frame'
  945. For the calendar, use the variable `calendar-setup'.
  946. For BBDB, it is currently only possible to display the matches in
  947. another window."
  948. :group 'org-link-follow
  949. :type '(list
  950. (cons (const vm)
  951. (choice
  952. (const vm-visit-folder)
  953. (const vm-visit-folder-other-window)
  954. (const vm-visit-folder-other-frame)))
  955. (cons (const gnus)
  956. (choice
  957. (const gnus)
  958. (const gnus-other-frame)
  959. (const org-gnus-no-new-news)))
  960. (cons (const file)
  961. (choice
  962. (const find-file)
  963. (const find-file-other-window)
  964. (const find-file-other-frame)))))
  965. (defcustom org-display-internal-link-with-indirect-buffer nil
  966. "Non-nil means, use indirect buffer to display infile links.
  967. Activating internal links (from one location in a file to another location
  968. in the same file) normally just jumps to the location. When the link is
  969. activated with a C-u prefix (or with mouse-3), the link is displayed in
  970. another window. When this option is set, the other window actually displays
  971. an indirect buffer clone of the current buffer, to avoid any visibility
  972. changes to the current buffer."
  973. :group 'org-link-follow
  974. :type 'boolean)
  975. (defcustom org-open-non-existing-files nil
  976. "Non-nil means, `org-open-file' will open non-existing files.
  977. When nil, an error will be generated."
  978. :group 'org-link-follow
  979. :type 'boolean)
  980. (defcustom org-open-directory-means-index-dot-org nil
  981. "Non-nil means, a link to a directory really means to index.org.
  982. When nil, following a directory link will run dired or open a finder/explorer
  983. window on that directory."
  984. :group 'org-link-follow
  985. :type 'boolean)
  986. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  987. "Function and arguments to call for following mailto links.
  988. This is a list with the first element being a lisp function, and the
  989. remaining elements being arguments to the function. In string arguments,
  990. %a will be replaced by the address, and %s will be replaced by the subject
  991. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  992. :group 'org-link-follow
  993. :type '(choice
  994. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  995. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  996. (const :tag "message-mail" (message-mail "%a" "%s"))
  997. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  998. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  999. "Non-nil means, ask for confirmation before executing shell links.
  1000. Shell links can be dangerous: just think about a link
  1001. [[shell:rm -rf ~/*][Google Search]]
  1002. This link would show up in your Org-mode document as \"Google Search\",
  1003. but really it would remove your entire home directory.
  1004. Therefore we advise against setting this variable to nil.
  1005. Just change it to `y-or-n-p' of you want to confirm with a
  1006. single keystroke rather than having to type \"yes\"."
  1007. :group 'org-link-follow
  1008. :type '(choice
  1009. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1010. (const :tag "with y-or-n (faster)" y-or-n-p)
  1011. (const :tag "no confirmation (dangerous)" nil)))
  1012. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1013. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1014. Elisp links can be dangerous: just think about a link
  1015. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1016. This link would show up in your Org-mode document as \"Google Search\",
  1017. but really it would remove your entire home directory.
  1018. Therefore we advise against setting this variable to nil.
  1019. Just change it to `y-or-n-p' of you want to confirm with a
  1020. single keystroke rather than having to type \"yes\"."
  1021. :group 'org-link-follow
  1022. :type '(choice
  1023. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1024. (const :tag "with y-or-n (faster)" y-or-n-p)
  1025. (const :tag "no confirmation (dangerous)" nil)))
  1026. (defconst org-file-apps-defaults-gnu
  1027. '((remote . emacs)
  1028. (system . mailcap)
  1029. (t . mailcap))
  1030. "Default file applications on a UNIX or GNU/Linux system.
  1031. See `org-file-apps'.")
  1032. (defconst org-file-apps-defaults-macosx
  1033. '((remote . emacs)
  1034. (t . "open %s")
  1035. (system . "open %s")
  1036. ("ps.gz" . "gv %s")
  1037. ("eps.gz" . "gv %s")
  1038. ("dvi" . "xdvi %s")
  1039. ("fig" . "xfig %s"))
  1040. "Default file applications on a MacOS X system.
  1041. The system \"open\" is known as a default, but we use X11 applications
  1042. for some files for which the OS does not have a good default.
  1043. See `org-file-apps'.")
  1044. (defconst org-file-apps-defaults-windowsnt
  1045. (list
  1046. '(remote . emacs)
  1047. (cons t
  1048. (list (if (featurep 'xemacs)
  1049. 'mswindows-shell-execute
  1050. 'w32-shell-execute)
  1051. "open" 'file))
  1052. (cons 'system
  1053. (list (if (featurep 'xemacs)
  1054. 'mswindows-shell-execute
  1055. 'w32-shell-execute)
  1056. "open" 'file)))
  1057. "Default file applications on a Windows NT system.
  1058. The system \"open\" is used for most files.
  1059. See `org-file-apps'.")
  1060. (defcustom org-file-apps
  1061. '(
  1062. (auto-mode . emacs)
  1063. ("\\.x?html?\\'" . default)
  1064. ("\\.pdf\\'" . default)
  1065. )
  1066. "External applications for opening `file:path' items in a document.
  1067. Org-mode uses system defaults for different file types, but
  1068. you can use this variable to set the application for a given file
  1069. extension. The entries in this list are cons cells where the car identifies
  1070. files and the cdr the corresponding command. Possible values for the
  1071. file identifier are
  1072. \"regex\" Regular expression matched against the file name. For backward
  1073. compatibility, this can also be a string with only alphanumeric
  1074. characters, which is then interpreted as an extension.
  1075. `directory' Matches a directory
  1076. `remote' Matches a remote file, accessible through tramp or efs.
  1077. Remote files most likely should be visited through Emacs
  1078. because external applications cannot handle such paths.
  1079. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1080. so all files Emacs knows how to handle. Using this with
  1081. command `emacs' will open most files in Emacs. Beware that this
  1082. will also open html files inside Emacs, unless you add
  1083. (\"html\" . default) to the list as well.
  1084. t Default for files not matched by any of the other options.
  1085. `system' The system command to open files, like `open' on Windows
  1086. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1087. that will be selected if you call `C-c C-o' with a double
  1088. `C-u C-u' prefix.
  1089. Possible values for the command are:
  1090. `emacs' The file will be visited by the current Emacs process.
  1091. `default' Use the default application for this file type, which is the
  1092. association for t in the list, most likely in the system-specific
  1093. part.
  1094. This can be used to overrule an unwanted setting in the
  1095. system-specific variable.
  1096. `system' Use the system command for opening files, like \"open\".
  1097. This command is specified by the entry whose car is `system'.
  1098. Most likely, the system-specific version of this variable
  1099. does define this command, but you can overrule/replace it
  1100. here.
  1101. string A command to be executed by a shell; %s will be replaced
  1102. by the path to the file.
  1103. sexp A Lisp form which will be evaluated. The file path will
  1104. be available in the Lisp variable `file'.
  1105. For more examples, see the system specific constants
  1106. `org-file-apps-defaults-macosx'
  1107. `org-file-apps-defaults-windowsnt'
  1108. `org-file-apps-defaults-gnu'."
  1109. :group 'org-link-follow
  1110. :type '(repeat
  1111. (cons (choice :value ""
  1112. (string :tag "Extension")
  1113. (const :tag "System command to open files" system)
  1114. (const :tag "Default for unrecognized files" t)
  1115. (const :tag "Remote file" remote)
  1116. (const :tag "Links to a directory" directory)
  1117. (const :tag "Any files that have Emacs modes"
  1118. auto-mode))
  1119. (choice :value ""
  1120. (const :tag "Visit with Emacs" emacs)
  1121. (const :tag "Use default" default)
  1122. (const :tag "Use the system command" system)
  1123. (string :tag "Command")
  1124. (sexp :tag "Lisp form")))))
  1125. (defgroup org-refile nil
  1126. "Options concerning refiling entries in Org-mode."
  1127. :tag "Org Refile"
  1128. :group 'org)
  1129. (defcustom org-directory "~/org"
  1130. "Directory with org files.
  1131. This directory will be used as default to prompt for org files.
  1132. Used by the hooks for remember.el."
  1133. :group 'org-refile
  1134. :group 'org-remember
  1135. :type 'directory)
  1136. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1137. "Default target for storing notes.
  1138. Used by the hooks for remember.el. This can be a string, or nil to mean
  1139. the value of `remember-data-file'.
  1140. You can set this on a per-template basis with the variable
  1141. `org-remember-templates'."
  1142. :group 'org-refile
  1143. :group 'org-remember
  1144. :type '(choice
  1145. (const :tag "Default from remember-data-file" nil)
  1146. file))
  1147. (defcustom org-goto-interface 'outline
  1148. "The default interface to be used for `org-goto'.
  1149. Allowed values are:
  1150. outline The interface shows an outline of the relevant file
  1151. and the correct heading is found by moving through
  1152. the outline or by searching with incremental search.
  1153. outline-path-completion Headlines in the current buffer are offered via
  1154. completion. This is the interface also used by
  1155. the refile command."
  1156. :group 'org-refile
  1157. :type '(choice
  1158. (const :tag "Outline" outline)
  1159. (const :tag "Outline-path-completion" outline-path-completion)))
  1160. (defcustom org-goto-max-level 5
  1161. "Maximum level to be considered when running org-goto with refile interface."
  1162. :group 'org-refile
  1163. :type 'number)
  1164. (defcustom org-reverse-note-order nil
  1165. "Non-nil means, store new notes at the beginning of a file or entry.
  1166. When nil, new notes will be filed to the end of a file or entry.
  1167. This can also be a list with cons cells of regular expressions that
  1168. are matched against file names, and values."
  1169. :group 'org-remember
  1170. :group 'org-refile
  1171. :type '(choice
  1172. (const :tag "Reverse always" t)
  1173. (const :tag "Reverse never" nil)
  1174. (repeat :tag "By file name regexp"
  1175. (cons regexp boolean))))
  1176. (defcustom org-refile-targets nil
  1177. "Targets for refiling entries with \\[org-refile].
  1178. This is list of cons cells. Each cell contains:
  1179. - a specification of the files to be considered, either a list of files,
  1180. or a symbol whose function or variable value will be used to retrieve
  1181. a file name or a list of file names. If you use `org-agenda-files' for
  1182. that, all agenda files will be scanned for targets. Nil means, consider
  1183. headings in the current buffer.
  1184. - A specification of how to select find candidate refile targets. This
  1185. may be any of
  1186. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1187. This tag has to be present in all target headlines, inheritance will
  1188. not be considered.
  1189. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1190. todo keyword.
  1191. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1192. headlines that are refiling targets.
  1193. - a cons cell (:level . N). Any headline of level N is considered a target.
  1194. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1195. When this variable is nil, all top-level headlines in the current buffer
  1196. are used, equivalent to the value `((nil . (:level . 1))'."
  1197. :group 'org-refile
  1198. :type '(repeat
  1199. (cons
  1200. (choice :value org-agenda-files
  1201. (const :tag "All agenda files" org-agenda-files)
  1202. (const :tag "Current buffer" nil)
  1203. (function) (variable) (file))
  1204. (choice :tag "Identify target headline by"
  1205. (cons :tag "Specific tag" (const :value :tag) (string))
  1206. (cons :tag "TODO keyword" (const :value :todo) (string))
  1207. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1208. (cons :tag "Level number" (const :value :level) (integer))
  1209. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1210. (defcustom org-refile-use-outline-path nil
  1211. "Non-nil means, provide refile targets as paths.
  1212. So a level 3 headline will be available as level1/level2/level3.
  1213. When the value is `file', also include the file name (without directory)
  1214. into the path. When `full-file-path', include the full file path."
  1215. :group 'org-refile
  1216. :type '(choice
  1217. (const :tag "Not" nil)
  1218. (const :tag "Yes" t)
  1219. (const :tag "Start with file name" file)
  1220. (const :tag "Start with full file path" full-file-path)))
  1221. (defcustom org-outline-path-complete-in-steps t
  1222. "Non-nil means, complete the outline path in hierarchical steps.
  1223. When Org-mode uses the refile interface to select an outline path
  1224. \(see variable `org-refile-use-outline-path'), the completion of
  1225. the path can be done is a single go, or if can be done in steps down
  1226. the headline hierarchy. Going in steps is probably the best if you
  1227. do not use a special completion package like `ido' or `icicles'.
  1228. However, when using these packages, going in one step can be very
  1229. fast, while still showing the whole path to the entry."
  1230. :group 'org-refile
  1231. :type 'boolean)
  1232. (defgroup org-todo nil
  1233. "Options concerning TODO items in Org-mode."
  1234. :tag "Org TODO"
  1235. :group 'org)
  1236. (defgroup org-progress nil
  1237. "Options concerning Progress logging in Org-mode."
  1238. :tag "Org Progress"
  1239. :group 'org-time)
  1240. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1241. "List of TODO entry keyword sequences and their interpretation.
  1242. \\<org-mode-map>This is a list of sequences.
  1243. Each sequence starts with a symbol, either `sequence' or `type',
  1244. indicating if the keywords should be interpreted as a sequence of
  1245. action steps, or as different types of TODO items. The first
  1246. keywords are states requiring action - these states will select a headline
  1247. for inclusion into the global TODO list Org-mode produces. If one of
  1248. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1249. signify that no further action is necessary. If \"|\" is not found,
  1250. the last keyword is treated as the only DONE state of the sequence.
  1251. The command \\[org-todo] cycles an entry through these states, and one
  1252. additional state where no keyword is present. For details about this
  1253. cycling, see the manual.
  1254. TODO keywords and interpretation can also be set on a per-file basis with
  1255. the special #+SEQ_TODO and #+TYP_TODO lines.
  1256. Each keyword can optionally specify a character for fast state selection
  1257. \(in combination with the variable `org-use-fast-todo-selection')
  1258. and specifiers for state change logging, using the same syntax
  1259. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1260. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1261. indicates to record a time stamp each time this state is selected.
  1262. Each keyword may also specify if a timestamp or a note should be
  1263. recorded when entering or leaving the state, by adding additional
  1264. characters in the parenthesis after the keyword. This looks like this:
  1265. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1266. record only the time of the state change. With X and Y being either
  1267. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1268. Y when leaving the state if and only if the *target* state does not
  1269. define X. You may omit any of the fast-selection key or X or /Y,
  1270. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1271. For backward compatibility, this variable may also be just a list
  1272. of keywords - in this case the interpretation (sequence or type) will be
  1273. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1274. :group 'org-todo
  1275. :group 'org-keywords
  1276. :type '(choice
  1277. (repeat :tag "Old syntax, just keywords"
  1278. (string :tag "Keyword"))
  1279. (repeat :tag "New syntax"
  1280. (cons
  1281. (choice
  1282. :tag "Interpretation"
  1283. (const :tag "Sequence (cycling hits every state)" sequence)
  1284. (const :tag "Type (cycling directly to DONE)" type))
  1285. (repeat
  1286. (string :tag "Keyword"))))))
  1287. (defvar org-todo-keywords-1 nil
  1288. "All TODO and DONE keywords active in a buffer.")
  1289. (make-variable-buffer-local 'org-todo-keywords-1)
  1290. (defvar org-todo-keywords-for-agenda nil)
  1291. (defvar org-done-keywords-for-agenda nil)
  1292. (defvar org-todo-keyword-alist-for-agenda nil)
  1293. (defvar org-tag-alist-for-agenda nil)
  1294. (defvar org-agenda-contributing-files nil)
  1295. (defvar org-not-done-keywords nil)
  1296. (make-variable-buffer-local 'org-not-done-keywords)
  1297. (defvar org-done-keywords nil)
  1298. (make-variable-buffer-local 'org-done-keywords)
  1299. (defvar org-todo-heads nil)
  1300. (make-variable-buffer-local 'org-todo-heads)
  1301. (defvar org-todo-sets nil)
  1302. (make-variable-buffer-local 'org-todo-sets)
  1303. (defvar org-todo-log-states nil)
  1304. (make-variable-buffer-local 'org-todo-log-states)
  1305. (defvar org-todo-kwd-alist nil)
  1306. (make-variable-buffer-local 'org-todo-kwd-alist)
  1307. (defvar org-todo-key-alist nil)
  1308. (make-variable-buffer-local 'org-todo-key-alist)
  1309. (defvar org-todo-key-trigger nil)
  1310. (make-variable-buffer-local 'org-todo-key-trigger)
  1311. (defcustom org-todo-interpretation 'sequence
  1312. "Controls how TODO keywords are interpreted.
  1313. This variable is in principle obsolete and is only used for
  1314. backward compatibility, if the interpretation of todo keywords is
  1315. not given already in `org-todo-keywords'. See that variable for
  1316. more information."
  1317. :group 'org-todo
  1318. :group 'org-keywords
  1319. :type '(choice (const sequence)
  1320. (const type)))
  1321. (defcustom org-use-fast-todo-selection 'prefix
  1322. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1323. This variable describes if and under what circumstances the cycling
  1324. mechanism for TODO keywords will be replaced by a single-key, direct
  1325. selection scheme.
  1326. When nil, fast selection is never used.
  1327. When the symbol `prefix', it will be used when `org-todo' is called with
  1328. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1329. in an agenda buffer.
  1330. When t, fast selection is used by default. In this case, the prefix
  1331. argument forces cycling instead.
  1332. In all cases, the special interface is only used if access keys have actually
  1333. been assigned by the user, i.e. if keywords in the configuration are followed
  1334. by a letter in parenthesis, like TODO(t)."
  1335. :group 'org-todo
  1336. :type '(choice
  1337. (const :tag "Never" nil)
  1338. (const :tag "By default" t)
  1339. (const :tag "Only with C-u C-c C-t" prefix)))
  1340. (defcustom org-provide-todo-statistics t
  1341. "Non-nil means, update todo statistics after insert and toggle.
  1342. When this is set, todo statistics is updated in the parent of the current
  1343. entry each time a todo state is changed."
  1344. :group 'org-todo
  1345. :type 'boolean)
  1346. (defcustom org-after-todo-state-change-hook nil
  1347. "Hook which is run after the state of a TODO item was changed.
  1348. The new state (a string with a TODO keyword, or nil) is available in the
  1349. Lisp variable `state'."
  1350. :group 'org-todo
  1351. :type 'hook)
  1352. (defcustom org-todo-state-tags-triggers nil
  1353. "Tag changes that should be triggered by TODO state changes.
  1354. This is a list. Each entry is
  1355. (state-change (tag . flag) .......)
  1356. State-change can be a string with a state, and empty string to indicate the
  1357. state that has no TODO keyword, or it can be one of the symbols `todo'
  1358. or `done', meaning any not-done or done state, respectively."
  1359. :group 'org-todo
  1360. :group 'org-tags
  1361. :type '(repeat
  1362. (cons (choice :tag "When changing to"
  1363. (const :tag "Not-done state" todo)
  1364. (const :tag "Done state" done)
  1365. (string :tag "State"))
  1366. (repeat
  1367. (cons :tag "Tag action"
  1368. (string :tag "Tag")
  1369. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1370. (defcustom org-log-done nil
  1371. "Information to record when a task moves to the DONE state.
  1372. Possible values are:
  1373. nil Don't add anything, just change the keyword
  1374. time Add a time stamp to the task
  1375. note Prompt a closing note and add it with template `org-log-note-headings'
  1376. This option can also be set with on a per-file-basis with
  1377. #+STARTUP: nologdone
  1378. #+STARTUP: logdone
  1379. #+STARTUP: lognotedone
  1380. You can have local logging settings for a subtree by setting the LOGGING
  1381. property to one or more of these keywords."
  1382. :group 'org-todo
  1383. :group 'org-progress
  1384. :type '(choice
  1385. (const :tag "No logging" nil)
  1386. (const :tag "Record CLOSED timestamp" time)
  1387. (const :tag "Record CLOSED timestamp with closing note." note)))
  1388. ;; Normalize old uses of org-log-done.
  1389. (cond
  1390. ((eq org-log-done t) (setq org-log-done 'time))
  1391. ((and (listp org-log-done) (memq 'done org-log-done))
  1392. (setq org-log-done 'note)))
  1393. (defcustom org-log-note-clock-out nil
  1394. "Non-nil means, record a note when clocking out of an item.
  1395. This can also be configured on a per-file basis by adding one of
  1396. the following lines anywhere in the buffer:
  1397. #+STARTUP: lognoteclock-out
  1398. #+STARTUP: nolognoteclock-out"
  1399. :group 'org-todo
  1400. :group 'org-progress
  1401. :type 'boolean)
  1402. (defcustom org-log-done-with-time t
  1403. "Non-nil means, the CLOSED time stamp will contain date and time.
  1404. When nil, only the date will be recorded."
  1405. :group 'org-progress
  1406. :type 'boolean)
  1407. (defcustom org-log-note-headings
  1408. '((done . "CLOSING NOTE %t")
  1409. (state . "State %-12s %t")
  1410. (note . "Note taken on %t")
  1411. (clock-out . ""))
  1412. "Headings for notes added to entries.
  1413. The value is an alist, with the car being a symbol indicating the note
  1414. context, and the cdr is the heading to be used. The heading may also be the
  1415. empty string.
  1416. %t in the heading will be replaced by a time stamp.
  1417. %s will be replaced by the new TODO state, in double quotes.
  1418. %u will be replaced by the user name.
  1419. %U will be replaced by the full user name."
  1420. :group 'org-todo
  1421. :group 'org-progress
  1422. :type '(list :greedy t
  1423. (cons (const :tag "Heading when closing an item" done) string)
  1424. (cons (const :tag
  1425. "Heading when changing todo state (todo sequence only)"
  1426. state) string)
  1427. (cons (const :tag "Heading when just taking a note" note) string)
  1428. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1429. (unless (assq 'note org-log-note-headings)
  1430. (push '(note . "%t") org-log-note-headings))
  1431. (defcustom org-log-state-notes-insert-after-drawers nil
  1432. "Non-nil means, insert state change notes after any drawers in entry.
  1433. Only the drawers that *immediately* follow the headline and the
  1434. deadline/scheduled line are skipped.
  1435. When nil, insert notes right after the heading and perhaps the line
  1436. with deadline/scheduling if present."
  1437. :group 'org-todo
  1438. :group 'org-progress
  1439. :type 'boolean)
  1440. (defcustom org-log-states-order-reversed t
  1441. "Non-nil means, the latest state change note will be directly after heading.
  1442. When nil, the notes will be orderer according to time."
  1443. :group 'org-todo
  1444. :group 'org-progress
  1445. :type 'boolean)
  1446. (defcustom org-log-repeat 'time
  1447. "Non-nil means, record moving through the DONE state when triggering repeat.
  1448. An auto-repeating tasks is immediately switched back to TODO when marked
  1449. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1450. the TODO keyword definition, or recording a closing note by setting
  1451. `org-log-done', there will be no record of the task moving through DONE.
  1452. This variable forces taking a note anyway. Possible values are:
  1453. nil Don't force a record
  1454. time Record a time stamp
  1455. note Record a note
  1456. This option can also be set with on a per-file-basis with
  1457. #+STARTUP: logrepeat
  1458. #+STARTUP: lognoterepeat
  1459. #+STARTUP: nologrepeat
  1460. You can have local logging settings for a subtree by setting the LOGGING
  1461. property to one or more of these keywords."
  1462. :group 'org-todo
  1463. :group 'org-progress
  1464. :type '(choice
  1465. (const :tag "Don't force a record" nil)
  1466. (const :tag "Force recording the DONE state" time)
  1467. (const :tag "Force recording a note with the DONE state" note)))
  1468. (defgroup org-priorities nil
  1469. "Priorities in Org-mode."
  1470. :tag "Org Priorities"
  1471. :group 'org-todo)
  1472. (defcustom org-highest-priority ?A
  1473. "The highest priority of TODO items. A character like ?A, ?B etc.
  1474. Must have a smaller ASCII number than `org-lowest-priority'."
  1475. :group 'org-priorities
  1476. :type 'character)
  1477. (defcustom org-lowest-priority ?C
  1478. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1479. Must have a larger ASCII number than `org-highest-priority'."
  1480. :group 'org-priorities
  1481. :type 'character)
  1482. (defcustom org-default-priority ?B
  1483. "The default priority of TODO items.
  1484. This is the priority an item get if no explicit priority is given."
  1485. :group 'org-priorities
  1486. :type 'character)
  1487. (defcustom org-priority-start-cycle-with-default t
  1488. "Non-nil means, start with default priority when starting to cycle.
  1489. When this is nil, the first step in the cycle will be (depending on the
  1490. command used) one higher or lower that the default priority."
  1491. :group 'org-priorities
  1492. :type 'boolean)
  1493. (defgroup org-time nil
  1494. "Options concerning time stamps and deadlines in Org-mode."
  1495. :tag "Org Time"
  1496. :group 'org)
  1497. (defcustom org-insert-labeled-timestamps-at-point nil
  1498. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1499. When nil, these labeled time stamps are forces into the second line of an
  1500. entry, just after the headline. When scheduling from the global TODO list,
  1501. the time stamp will always be forced into the second line."
  1502. :group 'org-time
  1503. :type 'boolean)
  1504. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1505. "Formats for `format-time-string' which are used for time stamps.
  1506. It is not recommended to change this constant.")
  1507. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1508. "Number of minutes to round time stamps to.
  1509. These are two values, the first applies when first creating a time stamp.
  1510. The second applies when changing it with the commands `S-up' and `S-down'.
  1511. When changing the time stamp, this means that it will change in steps
  1512. of N minutes, as given by the second value.
  1513. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1514. numbers should be factors of 60, so for example 5, 10, 15.
  1515. When this is larger than 1, you can still force an exact time-stamp by using
  1516. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1517. and by using a prefix arg to `S-up/down' to specify the exact number
  1518. of minutes to shift."
  1519. :group 'org-time
  1520. :get '(lambda (var) ; Make sure all entries have 5 elements
  1521. (if (integerp (default-value var))
  1522. (list (default-value var) 5)
  1523. (default-value var)))
  1524. :type '(list
  1525. (integer :tag "when inserting times")
  1526. (integer :tag "when modifying times")))
  1527. ;; Normalize old customizations of this variable.
  1528. (when (integerp org-time-stamp-rounding-minutes)
  1529. (setq org-time-stamp-rounding-minutes
  1530. (list org-time-stamp-rounding-minutes
  1531. org-time-stamp-rounding-minutes)))
  1532. (defcustom org-display-custom-times nil
  1533. "Non-nil means, overlay custom formats over all time stamps.
  1534. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1535. To turn this on on a per-file basis, insert anywhere in the file:
  1536. #+STARTUP: customtime"
  1537. :group 'org-time
  1538. :set 'set-default
  1539. :type 'sexp)
  1540. (make-variable-buffer-local 'org-display-custom-times)
  1541. (defcustom org-time-stamp-custom-formats
  1542. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1543. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1544. These are overlayed over the default ISO format if the variable
  1545. `org-display-custom-times' is set. Time like %H:%M should be at the
  1546. end of the second format."
  1547. :group 'org-time
  1548. :type 'sexp)
  1549. (defun org-time-stamp-format (&optional long inactive)
  1550. "Get the right format for a time string."
  1551. (let ((f (if long (cdr org-time-stamp-formats)
  1552. (car org-time-stamp-formats))))
  1553. (if inactive
  1554. (concat "[" (substring f 1 -1) "]")
  1555. f)))
  1556. (defcustom org-time-clocksum-format "%d:%02d"
  1557. "The format string used when creating CLOCKSUM lines, or when
  1558. org-mode generates a time duration."
  1559. :group 'org-time
  1560. :type 'string)
  1561. (defcustom org-deadline-warning-days 14
  1562. "No. of days before expiration during which a deadline becomes active.
  1563. This variable governs the display in sparse trees and in the agenda.
  1564. When 0 or negative, it means use this number (the absolute value of it)
  1565. even if a deadline has a different individual lead time specified."
  1566. :group 'org-time
  1567. :group 'org-agenda-daily/weekly
  1568. :type 'number)
  1569. (defcustom org-read-date-prefer-future t
  1570. "Non-nil means, assume future for incomplete date input from user.
  1571. This affects the following situations:
  1572. 1. The user gives a day, but no month.
  1573. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1574. read this as the third of *next* month. However, if you enter \"17\",
  1575. it will be considered as *this* month.
  1576. 2. The user gives a month but not a year.
  1577. For example, if it is april and you enter \"feb 2\", this will be read
  1578. as feb 2, *next* year. \"May 5\", however, will be this year.
  1579. Currently this does not work for ISO week specifications.
  1580. When this option is nil, the current month and year will always be used
  1581. as defaults."
  1582. :group 'org-time
  1583. :type 'boolean)
  1584. (defcustom org-read-date-display-live t
  1585. "Non-nil means, display current interpretation of date prompt live.
  1586. This display will be in an overlay, in the minibuffer."
  1587. :group 'org-time
  1588. :type 'boolean)
  1589. (defcustom org-read-date-popup-calendar t
  1590. "Non-nil means, pop up a calendar when prompting for a date.
  1591. In the calendar, the date can be selected with mouse-1. However, the
  1592. minibuffer will also be active, and you can simply enter the date as well.
  1593. When nil, only the minibuffer will be available."
  1594. :group 'org-time
  1595. :type 'boolean)
  1596. (if (fboundp 'defvaralias)
  1597. (defvaralias 'org-popup-calendar-for-date-prompt
  1598. 'org-read-date-popup-calendar))
  1599. (defcustom org-extend-today-until 0
  1600. "The hour when your day really ends. Must be an integer.
  1601. This has influence for the following applications:
  1602. - When switching the agenda to \"today\". It it is still earlier than
  1603. the time given here, the day recognized as TODAY is actually yesterday.
  1604. - When a date is read from the user and it is still before the time given
  1605. here, the current date and time will be assumed to be yesterday, 23:59.
  1606. Also, timestamps inserted in remember templates follow this rule.
  1607. IMPORTANT: This is a feature whose implementation is and likely will
  1608. remain incomplete. Really, it is only here because past midnight seems to
  1609. be the favorite working time of John Wiegley :-)"
  1610. :group 'org-time
  1611. :type 'number)
  1612. (defcustom org-edit-timestamp-down-means-later nil
  1613. "Non-nil means, S-down will increase the time in a time stamp.
  1614. When nil, S-up will increase."
  1615. :group 'org-time
  1616. :type 'boolean)
  1617. (defcustom org-calendar-follow-timestamp-change t
  1618. "Non-nil means, make the calendar window follow timestamp changes.
  1619. When a timestamp is modified and the calendar window is visible, it will be
  1620. moved to the new date."
  1621. :group 'org-time
  1622. :type 'boolean)
  1623. (defgroup org-tags nil
  1624. "Options concerning tags in Org-mode."
  1625. :tag "Org Tags"
  1626. :group 'org)
  1627. (defcustom org-tag-alist nil
  1628. "List of tags allowed in Org-mode files.
  1629. When this list is nil, Org-mode will base TAG input on what is already in the
  1630. buffer.
  1631. The value of this variable is an alist, the car of each entry must be a
  1632. keyword as a string, the cdr may be a character that is used to select
  1633. that tag through the fast-tag-selection interface.
  1634. See the manual for details."
  1635. :group 'org-tags
  1636. :type '(repeat
  1637. (choice
  1638. (cons (string :tag "Tag name")
  1639. (character :tag "Access char"))
  1640. (const :tag "Start radio group" (:startgroup))
  1641. (const :tag "End radio group" (:endgroup)))))
  1642. (defvar org-file-tags nil
  1643. "List of tags that can be inherited by all entries in the file.
  1644. The tags will be inherited if the variable `org-use-tag-inheritance'
  1645. says they should be.
  1646. This variable is populated from #+TAG lines.")
  1647. (defcustom org-use-fast-tag-selection 'auto
  1648. "Non-nil means, use fast tag selection scheme.
  1649. This is a special interface to select and deselect tags with single keys.
  1650. When nil, fast selection is never used.
  1651. When the symbol `auto', fast selection is used if and only if selection
  1652. characters for tags have been configured, either through the variable
  1653. `org-tag-alist' or through a #+TAGS line in the buffer.
  1654. When t, fast selection is always used and selection keys are assigned
  1655. automatically if necessary."
  1656. :group 'org-tags
  1657. :type '(choice
  1658. (const :tag "Always" t)
  1659. (const :tag "Never" nil)
  1660. (const :tag "When selection characters are configured" 'auto)))
  1661. (defcustom org-fast-tag-selection-single-key nil
  1662. "Non-nil means, fast tag selection exits after first change.
  1663. When nil, you have to press RET to exit it.
  1664. During fast tag selection, you can toggle this flag with `C-c'.
  1665. This variable can also have the value `expert'. In this case, the window
  1666. displaying the tags menu is not even shown, until you press C-c again."
  1667. :group 'org-tags
  1668. :type '(choice
  1669. (const :tag "No" nil)
  1670. (const :tag "Yes" t)
  1671. (const :tag "Expert" expert)))
  1672. (defvar org-fast-tag-selection-include-todo nil
  1673. "Non-nil means, fast tags selection interface will also offer TODO states.
  1674. This is an undocumented feature, you should not rely on it.")
  1675. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1676. "The column to which tags should be indented in a headline.
  1677. If this number is positive, it specifies the column. If it is negative,
  1678. it means that the tags should be flushright to that column. For example,
  1679. -80 works well for a normal 80 character screen."
  1680. :group 'org-tags
  1681. :type 'integer)
  1682. (defcustom org-auto-align-tags t
  1683. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1684. These operations change the length of a headline and therefore shift
  1685. the tags around. With this options turned on, after each such operation
  1686. the tags are again aligned to `org-tags-column'."
  1687. :group 'org-tags
  1688. :type 'boolean)
  1689. (defcustom org-use-tag-inheritance t
  1690. "Non-nil means, tags in levels apply also for sublevels.
  1691. When nil, only the tags directly given in a specific line apply there.
  1692. This may also be a list of tags that should be inherited, or a regexp that
  1693. matches tags that should be inherited. Additional control is possible
  1694. with the variable `org-tags-exclude-from-inheritance' which gives an
  1695. explicit list of tags to be excluded from inheritance., even if the value of
  1696. `org-use-tag-inheritance' would select it for inheritance.
  1697. If this option is t, a match early-on in a tree can lead to a large
  1698. number of matches in the subtree when constructing the agenda or creating
  1699. a sparse tree. If you only want to see the first match in a tree during
  1700. a search, check out the variable `org-tags-match-list-sublevels'."
  1701. :group 'org-tags
  1702. :type '(choice
  1703. (const :tag "Not" nil)
  1704. (const :tag "Always" t)
  1705. (repeat :tag "Specific tags" (string :tag "Tag"))
  1706. (regexp :tag "Tags matched by regexp")))
  1707. (defcustom org-tags-exclude-from-inheritance nil
  1708. "List of tags that should never be inherited.
  1709. This is a way to exclude a few tags from inheritance. For way to do
  1710. the opposite, to actively allow inheritance for selected tags,
  1711. see the variable `org-use-tag-inheritance'."
  1712. :group 'org-tags
  1713. :type '(repeat (string :tag "Tag")))
  1714. (defun org-tag-inherit-p (tag)
  1715. "Check if TAG is one that should be inherited."
  1716. (cond
  1717. ((member tag org-tags-exclude-from-inheritance) nil)
  1718. ((eq org-use-tag-inheritance t) t)
  1719. ((not org-use-tag-inheritance) nil)
  1720. ((stringp org-use-tag-inheritance)
  1721. (string-match org-use-tag-inheritance tag))
  1722. ((listp org-use-tag-inheritance)
  1723. (member tag org-use-tag-inheritance))
  1724. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1725. (defcustom org-tags-match-list-sublevels t
  1726. "Non-nil means list also sublevels of headlines matching tag search.
  1727. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1728. the sublevels of a headline matching a tag search often also match
  1729. the same search. Listing all of them can create very long lists.
  1730. Setting this variable to nil causes subtrees of a match to be skipped.
  1731. This option is off by default, because inheritance in on. If you turn
  1732. inheritance off, you very likely want to turn this option on.
  1733. As a special case, if the tag search is restricted to TODO items, the
  1734. value of this variable is ignored and sublevels are always checked, to
  1735. make sure all corresponding TODO items find their way into the list.
  1736. This variable is semi-obsolete and probably should always be true. It
  1737. is better to limit inheritance to certain tags using the variables
  1738. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1739. :group 'org-tags
  1740. :type 'boolean)
  1741. (defvar org-tags-history nil
  1742. "History of minibuffer reads for tags.")
  1743. (defvar org-last-tags-completion-table nil
  1744. "The last used completion table for tags.")
  1745. (defvar org-after-tags-change-hook nil
  1746. "Hook that is run after the tags in a line have changed.")
  1747. (defgroup org-properties nil
  1748. "Options concerning properties in Org-mode."
  1749. :tag "Org Properties"
  1750. :group 'org)
  1751. (defcustom org-property-format "%-10s %s"
  1752. "How property key/value pairs should be formatted by `indent-line'.
  1753. When `indent-line' hits a property definition, it will format the line
  1754. according to this format, mainly to make sure that the values are
  1755. lined-up with respect to each other."
  1756. :group 'org-properties
  1757. :type 'string)
  1758. (defcustom org-use-property-inheritance nil
  1759. "Non-nil means, properties apply also for sublevels.
  1760. This setting is chiefly used during property searches. Turning it on can
  1761. cause significant overhead when doing a search, which is why it is not
  1762. on by default.
  1763. When nil, only the properties directly given in the current entry count.
  1764. When t, every property is inherited. The value may also be a list of
  1765. properties that should have inheritance, or a regular expression matching
  1766. properties that should be inherited.
  1767. However, note that some special properties use inheritance under special
  1768. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1769. and the properties ending in \"_ALL\" when they are used as descriptor
  1770. for valid values of a property.
  1771. Note for programmers:
  1772. When querying an entry with `org-entry-get', you can control if inheritance
  1773. should be used. By default, `org-entry-get' looks only at the local
  1774. properties. You can request inheritance by setting the inherit argument
  1775. to t (to force inheritance) or to `selective' (to respect the setting
  1776. in this variable)."
  1777. :group 'org-properties
  1778. :type '(choice
  1779. (const :tag "Not" nil)
  1780. (const :tag "Always" t)
  1781. (repeat :tag "Specific properties" (string :tag "Property"))
  1782. (regexp :tag "Properties matched by regexp")))
  1783. (defun org-property-inherit-p (property)
  1784. "Check if PROPERTY is one that should be inherited."
  1785. (cond
  1786. ((eq org-use-property-inheritance t) t)
  1787. ((not org-use-property-inheritance) nil)
  1788. ((stringp org-use-property-inheritance)
  1789. (string-match org-use-property-inheritance property))
  1790. ((listp org-use-property-inheritance)
  1791. (member property org-use-property-inheritance))
  1792. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1793. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1794. "The default column format, if no other format has been defined.
  1795. This variable can be set on the per-file basis by inserting a line
  1796. #+COLUMNS: %25ITEM ....."
  1797. :group 'org-properties
  1798. :type 'string)
  1799. (defcustom org-columns-ellipses ".."
  1800. "The ellipses to be used when a field in column view is truncated.
  1801. When this is the empty string, as many characters as possible are shown,
  1802. but then there will be no visual indication that the field has been truncated.
  1803. When this is a string of length N, the last N characters of a truncated
  1804. field are replaced by this string. If the column is narrower than the
  1805. ellipses string, only part of the ellipses string will be shown."
  1806. :group 'org-properties
  1807. :type 'string)
  1808. (defcustom org-columns-modify-value-for-display-function nil
  1809. "Function that modifies values for display in column view.
  1810. For example, it can be used to cut out a certain part from a time stamp.
  1811. The function must take 2 arguments:
  1812. column-title The title of the column (*not* the property name)
  1813. value The value that should be modified.
  1814. The function should return the value that should be displayed,
  1815. or nil if the normal value should be used."
  1816. :group 'org-properties
  1817. :type 'function)
  1818. (defcustom org-effort-property "Effort"
  1819. "The property that is being used to keep track of effort estimates.
  1820. Effort estimates given in this property need to have the format H:MM."
  1821. :group 'org-properties
  1822. :group 'org-progress
  1823. :type '(string :tag "Property"))
  1824. (defconst org-global-properties-fixed
  1825. '(("VISIBILITY_ALL" . "folded children content all"))
  1826. "List of property/value pairs that can be inherited by any entry.
  1827. These are fixed values, for the preset properties.")
  1828. (defcustom org-global-properties nil
  1829. "List of property/value pairs that can be inherited by any entry.
  1830. You can set buffer-local values for the same purpose in the variable
  1831. `org-file-properties' this by adding lines like
  1832. #+PROPERTY: NAME VALUE"
  1833. :group 'org-properties
  1834. :type '(repeat
  1835. (cons (string :tag "Property")
  1836. (string :tag "Value"))))
  1837. (defvar org-file-properties nil
  1838. "List of property/value pairs that can be inherited by any entry.
  1839. Valid for the current buffer.
  1840. This variable is populated from #+PROPERTY lines.")
  1841. (make-variable-buffer-local 'org-file-properties)
  1842. (defgroup org-agenda nil
  1843. "Options concerning agenda views in Org-mode."
  1844. :tag "Org Agenda"
  1845. :group 'org)
  1846. (defvar org-category nil
  1847. "Variable used by org files to set a category for agenda display.
  1848. Such files should use a file variable to set it, for example
  1849. # -*- mode: org; org-category: \"ELisp\"
  1850. or contain a special line
  1851. #+CATEGORY: ELisp
  1852. If the file does not specify a category, then file's base name
  1853. is used instead.")
  1854. (make-variable-buffer-local 'org-category)
  1855. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1856. (defcustom org-agenda-files nil
  1857. "The files to be used for agenda display.
  1858. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1859. \\[org-remove-file]. You can also use customize to edit the list.
  1860. If an entry is a directory, all files in that directory that are matched by
  1861. `org-agenda-file-regexp' will be part of the file list.
  1862. If the value of the variable is not a list but a single file name, then
  1863. the list of agenda files is actually stored and maintained in that file, one
  1864. agenda file per line."
  1865. :group 'org-agenda
  1866. :type '(choice
  1867. (repeat :tag "List of files and directories" file)
  1868. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1869. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1870. "Regular expression to match files for `org-agenda-files'.
  1871. If any element in the list in that variable contains a directory instead
  1872. of a normal file, all files in that directory that are matched by this
  1873. regular expression will be included."
  1874. :group 'org-agenda
  1875. :type 'regexp)
  1876. (defcustom org-agenda-text-search-extra-files nil
  1877. "List of extra files to be searched by text search commands.
  1878. These files will be search in addition to the agenda files by the
  1879. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1880. Note that these files will only be searched for text search commands,
  1881. not for the other agenda views like todo lists, tag searches or the weekly
  1882. agenda. This variable is intended to list notes and possibly archive files
  1883. that should also be searched by these two commands.
  1884. In fact, if the first element in the list is the symbol `agenda-archives',
  1885. than all archive files of all agenda files will be added to the search
  1886. scope."
  1887. :group 'org-agenda
  1888. :type '(set :greedy t
  1889. (const :tag "Agenda Archives" agenda-archives)
  1890. (repeat :inline t (file))))
  1891. (if (fboundp 'defvaralias)
  1892. (defvaralias 'org-agenda-multi-occur-extra-files
  1893. 'org-agenda-text-search-extra-files))
  1894. (defcustom org-agenda-skip-unavailable-files nil
  1895. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1896. A nil value means to remove them, after a query, from the list."
  1897. :group 'org-agenda
  1898. :type 'boolean)
  1899. (defcustom org-calendar-to-agenda-key [?c]
  1900. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1901. The command `org-calendar-goto-agenda' will be bound to this key. The
  1902. default is the character `c' because then `c' can be used to switch back and
  1903. forth between agenda and calendar."
  1904. :group 'org-agenda
  1905. :type 'sexp)
  1906. (defcustom org-calendar-agenda-action-key [?k]
  1907. "The key to be installed in `calendar-mode-map' for agenda-action.
  1908. The command `org-agenda-action' will be bound to this key. The
  1909. default is the character `k' because we use the same key in the agenda."
  1910. :group 'org-agenda
  1911. :type 'sexp)
  1912. (eval-after-load "calendar"
  1913. '(progn
  1914. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1915. 'org-calendar-goto-agenda)
  1916. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1917. 'org-agenda-action)))
  1918. (defgroup org-latex nil
  1919. "Options for embedding LaTeX code into Org-mode."
  1920. :tag "Org LaTeX"
  1921. :group 'org)
  1922. (defcustom org-format-latex-options
  1923. '(:foreground default :background default :scale 1.0
  1924. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1925. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1926. "Options for creating images from LaTeX fragments.
  1927. This is a property list with the following properties:
  1928. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1929. `default' means use the foreground of the default face.
  1930. :background the background color, or \"Transparent\".
  1931. `default' means use the background of the default face.
  1932. :scale a scaling factor for the size of the images.
  1933. :html-foreground, :html-background, :html-scale
  1934. the same numbers for HTML export.
  1935. :matchers a list indicating which matchers should be used to
  1936. find LaTeX fragments. Valid members of this list are:
  1937. \"begin\" find environments
  1938. \"$\" find math expressions surrounded by $...$
  1939. \"$$\" find math expressions surrounded by $$....$$
  1940. \"\\(\" find math expressions surrounded by \\(...\\)
  1941. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1942. :group 'org-latex
  1943. :type 'plist)
  1944. (defcustom org-format-latex-header "\\documentclass{article}
  1945. \\usepackage{fullpage} % do not remove
  1946. \\usepackage{amssymb}
  1947. \\usepackage[usenames]{color}
  1948. \\usepackage{amsmath}
  1949. \\usepackage{latexsym}
  1950. \\usepackage[mathscr]{eucal}
  1951. \\pagestyle{empty} % do not remove"
  1952. "The document header used for processing LaTeX fragments."
  1953. :group 'org-latex
  1954. :type 'string)
  1955. (defgroup org-font-lock nil
  1956. "Font-lock settings for highlighting in Org-mode."
  1957. :tag "Org Font Lock"
  1958. :group 'org)
  1959. (defcustom org-level-color-stars-only nil
  1960. "Non-nil means fontify only the stars in each headline.
  1961. When nil, the entire headline is fontified.
  1962. Changing it requires restart of `font-lock-mode' to become effective
  1963. also in regions already fontified."
  1964. :group 'org-font-lock
  1965. :type 'boolean)
  1966. (defcustom org-hide-leading-stars nil
  1967. "Non-nil means, hide the first N-1 stars in a headline.
  1968. This works by using the face `org-hide' for these stars. This
  1969. face is white for a light background, and black for a dark
  1970. background. You may have to customize the face `org-hide' to
  1971. make this work.
  1972. Changing it requires restart of `font-lock-mode' to become effective
  1973. also in regions already fontified.
  1974. You may also set this on a per-file basis by adding one of the following
  1975. lines to the buffer:
  1976. #+STARTUP: hidestars
  1977. #+STARTUP: showstars"
  1978. :group 'org-font-lock
  1979. :type 'boolean)
  1980. (defcustom org-fontify-done-headline nil
  1981. "Non-nil means, change the face of a headline if it is marked DONE.
  1982. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1983. When this is non-nil, the headline after the keyword is set to the
  1984. `org-headline-done' as an additional indication."
  1985. :group 'org-font-lock
  1986. :type 'boolean)
  1987. (defcustom org-fontify-emphasized-text t
  1988. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1989. Changing this variable requires a restart of Emacs to take effect."
  1990. :group 'org-font-lock
  1991. :type 'boolean)
  1992. (defcustom org-highlight-latex-fragments-and-specials nil
  1993. "Non-nil means, fontify what is treated specially by the exporters."
  1994. :group 'org-font-lock
  1995. :type 'boolean)
  1996. (defcustom org-hide-emphasis-markers nil
  1997. "Non-nil mean font-lock should hide the emphasis marker characters."
  1998. :group 'org-font-lock
  1999. :type 'boolean)
  2000. (defvar org-emph-re nil
  2001. "Regular expression for matching emphasis.")
  2002. (defvar org-verbatim-re nil
  2003. "Regular expression for matching verbatim text.")
  2004. (defvar org-emphasis-regexp-components) ; defined just below
  2005. (defvar org-emphasis-alist) ; defined just below
  2006. (defun org-set-emph-re (var val)
  2007. "Set variable and compute the emphasis regular expression."
  2008. (set var val)
  2009. (when (and (boundp 'org-emphasis-alist)
  2010. (boundp 'org-emphasis-regexp-components)
  2011. org-emphasis-alist org-emphasis-regexp-components)
  2012. (let* ((e org-emphasis-regexp-components)
  2013. (pre (car e))
  2014. (post (nth 1 e))
  2015. (border (nth 2 e))
  2016. (body (nth 3 e))
  2017. (nl (nth 4 e))
  2018. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  2019. (body1 (concat body "*?"))
  2020. (markers (mapconcat 'car org-emphasis-alist ""))
  2021. (vmarkers (mapconcat
  2022. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2023. org-emphasis-alist "")))
  2024. ;; make sure special characters appear at the right position in the class
  2025. (if (string-match "\\^" markers)
  2026. (setq markers (concat (replace-match "" t t markers) "^")))
  2027. (if (string-match "-" markers)
  2028. (setq markers (concat (replace-match "" t t markers) "-")))
  2029. (if (string-match "\\^" vmarkers)
  2030. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2031. (if (string-match "-" vmarkers)
  2032. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2033. (if (> nl 0)
  2034. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2035. (int-to-string nl) "\\}")))
  2036. ;; Make the regexp
  2037. (setq org-emph-re
  2038. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  2039. "\\("
  2040. "\\([" markers "]\\)"
  2041. "\\("
  2042. "[^" border "]\\|"
  2043. "[^" border (if (and nil stacked) markers) "]"
  2044. body1
  2045. "[^" border (if (and nil stacked) markers) "]"
  2046. "\\)"
  2047. "\\3\\)"
  2048. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  2049. (setq org-verbatim-re
  2050. (concat "\\([" pre "]\\|^\\)"
  2051. "\\("
  2052. "\\([" vmarkers "]\\)"
  2053. "\\("
  2054. "[^" border "]\\|"
  2055. "[^" border "]"
  2056. body1
  2057. "[^" border "]"
  2058. "\\)"
  2059. "\\3\\)"
  2060. "\\([" post "]\\|$\\)")))))
  2061. (defcustom org-emphasis-regexp-components
  2062. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2063. "Components used to build the regular expression for emphasis.
  2064. This is a list with 6 entries. Terminology: In an emphasis string
  2065. like \" *strong word* \", we call the initial space PREMATCH, the final
  2066. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2067. and \"trong wor\" is the body. The different components in this variable
  2068. specify what is allowed/forbidden in each part:
  2069. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2070. post Chars allowed as postmatch. End of line will be allowed too.
  2071. border The chars *forbidden* as border characters.
  2072. body-regexp A regexp like \".\" to match a body character. Don't use
  2073. non-shy groups here, and don't allow newline here.
  2074. newline The maximum number of newlines allowed in an emphasis exp.
  2075. Use customize to modify this, or restart Emacs after changing it."
  2076. :group 'org-font-lock
  2077. :set 'org-set-emph-re
  2078. :type '(list
  2079. (sexp :tag "Allowed chars in pre ")
  2080. (sexp :tag "Allowed chars in post ")
  2081. (sexp :tag "Forbidden chars in border ")
  2082. (sexp :tag "Regexp for body ")
  2083. (integer :tag "number of newlines allowed")
  2084. (option (boolean :tag "Please ignore this button"))))
  2085. (defcustom org-emphasis-alist
  2086. `(("*" bold "<b>" "</b>")
  2087. ("/" italic "<i>" "</i>")
  2088. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2089. ("=" org-code "<code>" "</code>" verbatim)
  2090. ("~" org-verbatim "<code>" "</code>" verbatim)
  2091. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2092. "<del>" "</del>")
  2093. )
  2094. "Special syntax for emphasized text.
  2095. Text starting and ending with a special character will be emphasized, for
  2096. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2097. characters, the face to be used by font-lock for highlighting in Org-mode
  2098. Emacs buffers, and the HTML tags to be used for this.
  2099. Use customize to modify this, or restart Emacs after changing it."
  2100. :group 'org-font-lock
  2101. :set 'org-set-emph-re
  2102. :type '(repeat
  2103. (list
  2104. (string :tag "Marker character")
  2105. (choice
  2106. (face :tag "Font-lock-face")
  2107. (plist :tag "Face property list"))
  2108. (string :tag "HTML start tag")
  2109. (string :tag "HTML end tag")
  2110. (option (const verbatim)))))
  2111. ;;; Miscellaneous options
  2112. (defgroup org-completion nil
  2113. "Completion in Org-mode."
  2114. :tag "Org Completion"
  2115. :group 'org)
  2116. (defcustom org-completion-use-ido nil
  2117. "Non-nil means, use ido completion wherever possible.
  2118. Note that `ido-mode' must be active for this variable to be relevant.
  2119. If you decide to turn this variable on, you might well want to turn off
  2120. `org-outline-path-complete-in-steps'."
  2121. :group 'org-completion
  2122. :type 'boolean)
  2123. (defcustom org-completion-fallback-command 'hippie-expand
  2124. "The expansion command called by \\[org-complete] in normal context.
  2125. Normal means, no org-mode-specific context."
  2126. :group 'org-completion
  2127. :type 'function)
  2128. ;;; Functions and variables from ther packages
  2129. ;; Declared here to avoid compiler warnings
  2130. ;; XEmacs only
  2131. (defvar outline-mode-menu-heading)
  2132. (defvar outline-mode-menu-show)
  2133. (defvar outline-mode-menu-hide)
  2134. (defvar zmacs-regions) ; XEmacs regions
  2135. ;; Emacs only
  2136. (defvar mark-active)
  2137. ;; Various packages
  2138. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2139. (declare-function calendar-forward-day "cal-move" (arg))
  2140. (declare-function calendar-goto-date "cal-move" (date))
  2141. (declare-function calendar-goto-today "cal-move" ())
  2142. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2143. (defvar calc-embedded-close-formula)
  2144. (defvar calc-embedded-open-formula)
  2145. (declare-function cdlatex-tab "ext:cdlatex" ())
  2146. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2147. (defvar font-lock-unfontify-region-function)
  2148. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2149. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2150. (defvar iswitchb-temp-buflist)
  2151. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2152. (declare-function org-agenda-skip "org-agenda" ())
  2153. (declare-function org-format-agenda-item "org-agenda"
  2154. (extra txt &optional category tags dotime noprefix remove-re))
  2155. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2156. (declare-function org-agenda-change-all-lines "org-agenda"
  2157. (newhead hdmarker &optional fixface just-this))
  2158. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2159. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2160. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2161. (beg end))
  2162. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2163. (declare-function parse-time-string "parse-time" (string))
  2164. (declare-function remember "remember" (&optional initial))
  2165. (declare-function remember-buffer-desc "remember" ())
  2166. (declare-function remember-finalize "remember" ())
  2167. (defvar remember-save-after-remembering)
  2168. (defvar remember-data-file)
  2169. (defvar remember-register)
  2170. (defvar remember-buffer)
  2171. (defvar remember-handler-functions)
  2172. (defvar remember-annotation-functions)
  2173. (defvar texmathp-why)
  2174. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2175. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2176. (defvar w3m-current-url)
  2177. (defvar w3m-current-title)
  2178. (defvar org-latex-regexps)
  2179. ;;; Autoload and prepare some org modules
  2180. ;; Some table stuff that needs to be defined here, because it is used
  2181. ;; by the functions setting up org-mode or checking for table context.
  2182. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2183. "Detects an org-type or table-type table.")
  2184. (defconst org-table-line-regexp "^[ \t]*|"
  2185. "Detects an org-type table line.")
  2186. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2187. "Detects an org-type table line.")
  2188. (defconst org-table-hline-regexp "^[ \t]*|-"
  2189. "Detects an org-type table hline.")
  2190. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2191. "Detects a table-type table hline.")
  2192. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2193. "Searching from within a table (any type) this finds the first line
  2194. outside the table.")
  2195. ;; Autoload the functions in org-table.el that are needed by functions here.
  2196. (eval-and-compile
  2197. (org-autoload "org-table"
  2198. '(org-table-align org-table-begin org-table-blank-field
  2199. org-table-convert org-table-convert-region org-table-copy-down
  2200. org-table-copy-region org-table-create
  2201. org-table-create-or-convert-from-region
  2202. org-table-create-with-table.el org-table-current-dline
  2203. org-table-cut-region org-table-delete-column org-table-edit-field
  2204. org-table-edit-formulas org-table-end org-table-eval-formula
  2205. org-table-export org-table-field-info
  2206. org-table-get-stored-formulas org-table-goto-column
  2207. org-table-hline-and-move org-table-import org-table-insert-column
  2208. org-table-insert-hline org-table-insert-row org-table-iterate
  2209. org-table-justify-field-maybe org-table-kill-row
  2210. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2211. org-table-move-column org-table-move-column-left
  2212. org-table-move-column-right org-table-move-row
  2213. org-table-move-row-down org-table-move-row-up
  2214. org-table-next-field org-table-next-row org-table-paste-rectangle
  2215. org-table-previous-field org-table-recalculate
  2216. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2217. org-table-toggle-coordinate-overlays
  2218. org-table-toggle-formula-debugger org-table-wrap-region
  2219. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2220. (defun org-at-table-p (&optional table-type)
  2221. "Return t if the cursor is inside an org-type table.
  2222. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2223. (if org-enable-table-editor
  2224. (save-excursion
  2225. (beginning-of-line 1)
  2226. (looking-at (if table-type org-table-any-line-regexp
  2227. org-table-line-regexp)))
  2228. nil))
  2229. (defsubst org-table-p () (org-at-table-p))
  2230. (defun org-at-table.el-p ()
  2231. "Return t if and only if we are at a table.el table."
  2232. (and (org-at-table-p 'any)
  2233. (save-excursion
  2234. (goto-char (org-table-begin 'any))
  2235. (looking-at org-table1-hline-regexp))))
  2236. (defun org-table-recognize-table.el ()
  2237. "If there is a table.el table nearby, recognize it and move into it."
  2238. (if org-table-tab-recognizes-table.el
  2239. (if (org-at-table.el-p)
  2240. (progn
  2241. (beginning-of-line 1)
  2242. (if (looking-at org-table-dataline-regexp)
  2243. nil
  2244. (if (looking-at org-table1-hline-regexp)
  2245. (progn
  2246. (beginning-of-line 2)
  2247. (if (looking-at org-table-any-border-regexp)
  2248. (beginning-of-line -1)))))
  2249. (if (re-search-forward "|" (org-table-end t) t)
  2250. (progn
  2251. (require 'table)
  2252. (if (table--at-cell-p (point))
  2253. t
  2254. (message "recognizing table.el table...")
  2255. (table-recognize-table)
  2256. (message "recognizing table.el table...done")))
  2257. (error "This should not happen..."))
  2258. t)
  2259. nil)
  2260. nil))
  2261. (defun org-at-table-hline-p ()
  2262. "Return t if the cursor is inside a hline in a table."
  2263. (if org-enable-table-editor
  2264. (save-excursion
  2265. (beginning-of-line 1)
  2266. (looking-at org-table-hline-regexp))
  2267. nil))
  2268. (defvar org-table-clean-did-remove-column nil)
  2269. (defun org-table-map-tables (function)
  2270. "Apply FUNCTION to the start of all tables in the buffer."
  2271. (save-excursion
  2272. (save-restriction
  2273. (widen)
  2274. (goto-char (point-min))
  2275. (while (re-search-forward org-table-any-line-regexp nil t)
  2276. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2277. (beginning-of-line 1)
  2278. (if (looking-at org-table-line-regexp)
  2279. (save-excursion (funcall function)))
  2280. (re-search-forward org-table-any-border-regexp nil 1))))
  2281. (message "Mapping tables: done"))
  2282. ;; Declare and autoload functions from org-exp.el
  2283. (declare-function org-default-export-plist "org-exp")
  2284. (declare-function org-infile-export-plist "org-exp")
  2285. (declare-function org-get-current-options "org-exp")
  2286. (eval-and-compile
  2287. (org-autoload "org-exp"
  2288. '(org-export org-export-as-ascii org-export-visible
  2289. org-insert-export-options-template org-export-as-html-and-open
  2290. org-export-as-html-batch org-export-as-html-to-buffer
  2291. org-replace-region-by-html org-export-region-as-html
  2292. org-export-as-html org-export-icalendar-this-file
  2293. org-export-icalendar-all-agenda-files
  2294. org-table-clean-before-export
  2295. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2296. ;; Declare and autoload functions from org-agenda.el
  2297. (eval-and-compile
  2298. (org-autoload "org-agenda"
  2299. '(org-agenda org-agenda-list org-search-view
  2300. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2301. org-diary org-agenda-to-appt)))
  2302. ;; Autoload org-remember
  2303. (eval-and-compile
  2304. (org-autoload "org-remember"
  2305. '(org-remember-insinuate org-remember-annotation
  2306. org-remember-apply-template org-remember org-remember-handler)))
  2307. ;; Autoload org-clock.el
  2308. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2309. (beg end))
  2310. (declare-function org-update-mode-line "org-clock" ())
  2311. (defvar org-clock-start-time)
  2312. (defvar org-clock-marker (make-marker)
  2313. "Marker recording the last clock-in.")
  2314. (eval-and-compile
  2315. (org-autoload
  2316. "org-clock"
  2317. '(org-clock-in org-clock-out org-clock-cancel
  2318. org-clock-goto org-clock-sum org-clock-display
  2319. org-remove-clock-overlays org-clock-report
  2320. org-clocktable-shift org-dblock-write:clocktable
  2321. org-get-clocktable)))
  2322. (defun org-clock-update-time-maybe ()
  2323. "If this is a CLOCK line, update it and return t.
  2324. Otherwise, return nil."
  2325. (interactive)
  2326. (save-excursion
  2327. (beginning-of-line 1)
  2328. (skip-chars-forward " \t")
  2329. (when (looking-at org-clock-string)
  2330. (let ((re (concat "[ \t]*" org-clock-string
  2331. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2332. "\\([ \t]*=>.*\\)?\\)?"))
  2333. ts te h m s neg)
  2334. (cond
  2335. ((not (looking-at re))
  2336. nil)
  2337. ((not (match-end 2))
  2338. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2339. (> org-clock-marker (point))
  2340. (<= org-clock-marker (point-at-eol)))
  2341. ;; The clock is running here
  2342. (setq org-clock-start-time
  2343. (apply 'encode-time
  2344. (org-parse-time-string (match-string 1))))
  2345. (org-update-mode-line)))
  2346. (t
  2347. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2348. (end-of-line 1)
  2349. (setq ts (match-string 1)
  2350. te (match-string 3))
  2351. (setq s (- (time-to-seconds
  2352. (apply 'encode-time (org-parse-time-string te)))
  2353. (time-to-seconds
  2354. (apply 'encode-time (org-parse-time-string ts))))
  2355. neg (< s 0)
  2356. s (abs s)
  2357. h (floor (/ s 3600))
  2358. s (- s (* 3600 h))
  2359. m (floor (/ s 60))
  2360. s (- s (* 60 s)))
  2361. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2362. t))))))
  2363. (defun org-check-running-clock ()
  2364. "Check if the current buffer contains the running clock.
  2365. If yes, offer to stop it and to save the buffer with the changes."
  2366. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2367. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2368. (buffer-name))))
  2369. (org-clock-out)
  2370. (when (y-or-n-p "Save changed buffer?")
  2371. (save-buffer))))
  2372. (defun org-clocktable-try-shift (dir n)
  2373. "Check if this line starts a clock table, if yes, shift the time block."
  2374. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2375. (org-clocktable-shift dir n)))
  2376. ;; Autoload org-timer.el
  2377. ;(declare-function org-timer "org-timer")
  2378. (eval-and-compile
  2379. (org-autoload
  2380. "org-timer"
  2381. '(org-timer-start org-timer org-timer-item
  2382. org-timer-change-times-in-region)))
  2383. ;; Autoload archiving code
  2384. ;; The stuff that is needed for cycling and tags has to be defined here.
  2385. (defgroup org-archive nil
  2386. "Options concerning archiving in Org-mode."
  2387. :tag "Org Archive"
  2388. :group 'org-structure)
  2389. (defcustom org-archive-location "%s_archive::"
  2390. "The location where subtrees should be archived.
  2391. The value of this variable is a string, consisting of two parts,
  2392. separated by a double-colon. The first part is a filename and
  2393. the second part is a headline.
  2394. When the filename is omitted, archiving happens in the same file.
  2395. %s in the filename will be replaced by the current file
  2396. name (without the directory part). Archiving to a different file
  2397. is useful to keep archived entries from contributing to the
  2398. Org-mode Agenda.
  2399. The archived entries will be filed as subtrees of the specified
  2400. headline. When the headline is omitted, the subtrees are simply
  2401. filed away at the end of the file, as top-level entries.
  2402. Here are a few examples:
  2403. \"%s_archive::\"
  2404. If the current file is Projects.org, archive in file
  2405. Projects.org_archive, as top-level trees. This is the default.
  2406. \"::* Archived Tasks\"
  2407. Archive in the current file, under the top-level headline
  2408. \"* Archived Tasks\".
  2409. \"~/org/archive.org::\"
  2410. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2411. \"basement::** Finished Tasks\"
  2412. Archive in file ./basement (relative path), as level 3 trees
  2413. below the level 2 heading \"** Finished Tasks\".
  2414. You may set this option on a per-file basis by adding to the buffer a
  2415. line like
  2416. #+ARCHIVE: basement::** Finished Tasks
  2417. You may also define it locally for a subtree by setting an ARCHIVE property
  2418. in the entry. If such a property is found in an entry, or anywhere up
  2419. the hierarchy, it will be used."
  2420. :group 'org-archive
  2421. :type 'string)
  2422. (defcustom org-archive-tag "ARCHIVE"
  2423. "The tag that marks a subtree as archived.
  2424. An archived subtree does not open during visibility cycling, and does
  2425. not contribute to the agenda listings.
  2426. After changing this, font-lock must be restarted in the relevant buffers to
  2427. get the proper fontification."
  2428. :group 'org-archive
  2429. :group 'org-keywords
  2430. :type 'string)
  2431. (defcustom org-agenda-skip-archived-trees t
  2432. "Non-nil means, the agenda will skip any items located in archived trees.
  2433. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2434. variable is no longer recommended, you should leave it at the value t.
  2435. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2436. :group 'org-archive
  2437. :group 'org-agenda-skip
  2438. :type 'boolean)
  2439. (defcustom org-cycle-open-archived-trees nil
  2440. "Non-nil means, `org-cycle' will open archived trees.
  2441. An archived tree is a tree marked with the tag ARCHIVE.
  2442. When nil, archived trees will stay folded. You can still open them with
  2443. normal outline commands like `show-all', but not with the cycling commands."
  2444. :group 'org-archive
  2445. :group 'org-cycle
  2446. :type 'boolean)
  2447. (defcustom org-sparse-tree-open-archived-trees nil
  2448. "Non-nil means sparse tree construction shows matches in archived trees.
  2449. When nil, matches in these trees are highlighted, but the trees are kept in
  2450. collapsed state."
  2451. :group 'org-archive
  2452. :group 'org-sparse-trees
  2453. :type 'boolean)
  2454. (defun org-cycle-hide-archived-subtrees (state)
  2455. "Re-hide all archived subtrees after a visibility state change."
  2456. (when (and (not org-cycle-open-archived-trees)
  2457. (not (memq state '(overview folded))))
  2458. (save-excursion
  2459. (let* ((globalp (memq state '(contents all)))
  2460. (beg (if globalp (point-min) (point)))
  2461. (end (if globalp (point-max) (org-end-of-subtree t))))
  2462. (org-hide-archived-subtrees beg end)
  2463. (goto-char beg)
  2464. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2465. (message "%s" (substitute-command-keys
  2466. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2467. (defun org-force-cycle-archived ()
  2468. "Cycle subtree even if it is archived."
  2469. (interactive)
  2470. (setq this-command 'org-cycle)
  2471. (let ((org-cycle-open-archived-trees t))
  2472. (call-interactively 'org-cycle)))
  2473. (defun org-hide-archived-subtrees (beg end)
  2474. "Re-hide all archived subtrees after a visibility state change."
  2475. (save-excursion
  2476. (let* ((re (concat ":" org-archive-tag ":")))
  2477. (goto-char beg)
  2478. (while (re-search-forward re end t)
  2479. (and (org-on-heading-p) (hide-subtree))
  2480. (org-end-of-subtree t)))))
  2481. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2482. (eval-and-compile
  2483. (org-autoload "org-archive"
  2484. '(org-add-archive-files org-archive-subtree
  2485. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2486. ;; Autoload Column View Code
  2487. (declare-function org-columns-number-to-string "org-colview")
  2488. (declare-function org-columns-get-format-and-top-level "org-colview")
  2489. (declare-function org-columns-compute "org-colview")
  2490. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2491. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2492. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2493. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2494. ;; Autoload ID code
  2495. (declare-function org-id-store-link "org-id")
  2496. (org-autoload "org-id"
  2497. '(org-id-get-create org-id-new org-id-copy org-id-get
  2498. org-id-get-with-outline-path-completion
  2499. org-id-get-with-outline-drilling
  2500. org-id-goto org-id-find org-id-store-link))
  2501. ;;; Variables for pre-computed regular expressions, all buffer local
  2502. (defvar org-drawer-regexp nil
  2503. "Matches first line of a hidden block.")
  2504. (make-variable-buffer-local 'org-drawer-regexp)
  2505. (defvar org-todo-regexp nil
  2506. "Matches any of the TODO state keywords.")
  2507. (make-variable-buffer-local 'org-todo-regexp)
  2508. (defvar org-not-done-regexp nil
  2509. "Matches any of the TODO state keywords except the last one.")
  2510. (make-variable-buffer-local 'org-not-done-regexp)
  2511. (defvar org-todo-line-regexp nil
  2512. "Matches a headline and puts TODO state into group 2 if present.")
  2513. (make-variable-buffer-local 'org-todo-line-regexp)
  2514. (defvar org-complex-heading-regexp nil
  2515. "Matches a headline and puts everything into groups:
  2516. group 1: the stars
  2517. group 2: The todo keyword, maybe
  2518. group 3: Priority cookie
  2519. group 4: True headline
  2520. group 5: Tags")
  2521. (make-variable-buffer-local 'org-complex-heading-regexp)
  2522. (defvar org-todo-line-tags-regexp nil
  2523. "Matches a headline and puts TODO state into group 2 if present.
  2524. Also put tags into group 4 if tags are present.")
  2525. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2526. (defvar org-nl-done-regexp nil
  2527. "Matches newline followed by a headline with the DONE keyword.")
  2528. (make-variable-buffer-local 'org-nl-done-regexp)
  2529. (defvar org-looking-at-done-regexp nil
  2530. "Matches the DONE keyword a point.")
  2531. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2532. (defvar org-ds-keyword-length 12
  2533. "Maximum length of the Deadline and SCHEDULED keywords.")
  2534. (make-variable-buffer-local 'org-ds-keyword-length)
  2535. (defvar org-deadline-regexp nil
  2536. "Matches the DEADLINE keyword.")
  2537. (make-variable-buffer-local 'org-deadline-regexp)
  2538. (defvar org-deadline-time-regexp nil
  2539. "Matches the DEADLINE keyword together with a time stamp.")
  2540. (make-variable-buffer-local 'org-deadline-time-regexp)
  2541. (defvar org-deadline-line-regexp nil
  2542. "Matches the DEADLINE keyword and the rest of the line.")
  2543. (make-variable-buffer-local 'org-deadline-line-regexp)
  2544. (defvar org-scheduled-regexp nil
  2545. "Matches the SCHEDULED keyword.")
  2546. (make-variable-buffer-local 'org-scheduled-regexp)
  2547. (defvar org-scheduled-time-regexp nil
  2548. "Matches the SCHEDULED keyword together with a time stamp.")
  2549. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2550. (defvar org-closed-time-regexp nil
  2551. "Matches the CLOSED keyword together with a time stamp.")
  2552. (make-variable-buffer-local 'org-closed-time-regexp)
  2553. (defvar org-keyword-time-regexp nil
  2554. "Matches any of the 4 keywords, together with the time stamp.")
  2555. (make-variable-buffer-local 'org-keyword-time-regexp)
  2556. (defvar org-keyword-time-not-clock-regexp nil
  2557. "Matches any of the 3 keywords, together with the time stamp.")
  2558. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2559. (defvar org-maybe-keyword-time-regexp nil
  2560. "Matches a timestamp, possibly preceeded by a keyword.")
  2561. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2562. (defvar org-planning-or-clock-line-re nil
  2563. "Matches a line with planning or clock info.")
  2564. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2565. (defconst org-plain-time-of-day-regexp
  2566. (concat
  2567. "\\(\\<[012]?[0-9]"
  2568. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2569. "\\(--?"
  2570. "\\(\\<[012]?[0-9]"
  2571. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2572. "\\)?")
  2573. "Regular expression to match a plain time or time range.
  2574. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2575. groups carry important information:
  2576. 0 the full match
  2577. 1 the first time, range or not
  2578. 8 the second time, if it is a range.")
  2579. (defconst org-plain-time-extension-regexp
  2580. (concat
  2581. "\\(\\<[012]?[0-9]"
  2582. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2583. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2584. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2585. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2586. groups carry important information:
  2587. 0 the full match
  2588. 7 hours of duration
  2589. 9 minutes of duration")
  2590. (defconst org-stamp-time-of-day-regexp
  2591. (concat
  2592. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2593. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2594. "\\(--?"
  2595. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2596. "Regular expression to match a timestamp time or time range.
  2597. After a match, the following groups carry important information:
  2598. 0 the full match
  2599. 1 date plus weekday, for backreferencing to make sure both times on same day
  2600. 2 the first time, range or not
  2601. 4 the second time, if it is a range.")
  2602. (defconst org-startup-options
  2603. '(("fold" org-startup-folded t)
  2604. ("overview" org-startup-folded t)
  2605. ("nofold" org-startup-folded nil)
  2606. ("showall" org-startup-folded nil)
  2607. ("content" org-startup-folded content)
  2608. ("hidestars" org-hide-leading-stars t)
  2609. ("showstars" org-hide-leading-stars nil)
  2610. ("odd" org-odd-levels-only t)
  2611. ("oddeven" org-odd-levels-only nil)
  2612. ("align" org-startup-align-all-tables t)
  2613. ("noalign" org-startup-align-all-tables nil)
  2614. ("customtime" org-display-custom-times t)
  2615. ("logdone" org-log-done time)
  2616. ("lognotedone" org-log-done note)
  2617. ("nologdone" org-log-done nil)
  2618. ("lognoteclock-out" org-log-note-clock-out t)
  2619. ("nolognoteclock-out" org-log-note-clock-out nil)
  2620. ("logrepeat" org-log-repeat state)
  2621. ("lognoterepeat" org-log-repeat note)
  2622. ("nologrepeat" org-log-repeat nil)
  2623. ("constcgs" constants-unit-system cgs)
  2624. ("constSI" constants-unit-system SI))
  2625. "Variable associated with STARTUP options for org-mode.
  2626. Each element is a list of three items: The startup options as written
  2627. in the #+STARTUP line, the corresponding variable, and the value to
  2628. set this variable to if the option is found. An optional forth element PUSH
  2629. means to push this value onto the list in the variable.")
  2630. (defun org-set-regexps-and-options ()
  2631. "Precompute regular expressions for current buffer."
  2632. (when (org-mode-p)
  2633. (org-set-local 'org-todo-kwd-alist nil)
  2634. (org-set-local 'org-todo-key-alist nil)
  2635. (org-set-local 'org-todo-key-trigger nil)
  2636. (org-set-local 'org-todo-keywords-1 nil)
  2637. (org-set-local 'org-done-keywords nil)
  2638. (org-set-local 'org-todo-heads nil)
  2639. (org-set-local 'org-todo-sets nil)
  2640. (org-set-local 'org-todo-log-states nil)
  2641. (org-set-local 'org-file-properties nil)
  2642. (org-set-local 'org-file-tags nil)
  2643. (let ((re (org-make-options-regexp
  2644. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2645. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2646. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2647. (splitre "[ \t]+")
  2648. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2649. tail sep kws1 prio props ftags drawers
  2650. ext-setup-or-nil setup-contents (start 0))
  2651. (save-excursion
  2652. (save-restriction
  2653. (widen)
  2654. (goto-char (point-min))
  2655. (while (or (and ext-setup-or-nil
  2656. (string-match re ext-setup-or-nil start)
  2657. (setq start (match-end 0)))
  2658. (and (setq ext-setup-or-nil nil start 0)
  2659. (re-search-forward re nil t)))
  2660. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2661. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2662. (cond
  2663. ((equal key "CATEGORY")
  2664. (if (string-match "[ \t]+$" value)
  2665. (setq value (replace-match "" t t value)))
  2666. (setq cat value))
  2667. ((member key '("SEQ_TODO" "TODO"))
  2668. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2669. ((equal key "TYP_TODO")
  2670. (push (cons 'type (org-split-string value splitre)) kwds))
  2671. ((equal key "TAGS")
  2672. (setq tags (append tags (org-split-string value splitre))))
  2673. ((equal key "COLUMNS")
  2674. (org-set-local 'org-columns-default-format value))
  2675. ((equal key "LINK")
  2676. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2677. (push (cons (match-string 1 value)
  2678. (org-trim (match-string 2 value)))
  2679. links)))
  2680. ((equal key "PRIORITIES")
  2681. (setq prio (org-split-string value " +")))
  2682. ((equal key "PROPERTY")
  2683. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2684. (push (cons (match-string 1 value) (match-string 2 value))
  2685. props)))
  2686. ((equal key "FILETAGS")
  2687. (when (string-match "\\S-" value)
  2688. (setq ftags
  2689. (append
  2690. ftags
  2691. (apply 'append
  2692. (mapcar (lambda (x) (org-split-string x ":"))
  2693. (org-split-string value)))))))
  2694. ((equal key "DRAWERS")
  2695. (setq drawers (org-split-string value splitre)))
  2696. ((equal key "CONSTANTS")
  2697. (setq const (append const (org-split-string value splitre))))
  2698. ((equal key "STARTUP")
  2699. (let ((opts (org-split-string value splitre))
  2700. l var val)
  2701. (while (setq l (pop opts))
  2702. (when (setq l (assoc l org-startup-options))
  2703. (setq var (nth 1 l) val (nth 2 l))
  2704. (if (not (nth 3 l))
  2705. (set (make-local-variable var) val)
  2706. (if (not (listp (symbol-value var)))
  2707. (set (make-local-variable var) nil))
  2708. (set (make-local-variable var) (symbol-value var))
  2709. (add-to-list var val))))))
  2710. ((equal key "ARCHIVE")
  2711. (string-match " *$" value)
  2712. (setq arch (replace-match "" t t value))
  2713. (remove-text-properties 0 (length arch)
  2714. '(face t fontified t) arch))
  2715. ((equal key "SETUPFILE")
  2716. (setq setup-contents (org-file-contents
  2717. (expand-file-name
  2718. (org-remove-double-quotes value))
  2719. 'noerror))
  2720. (if (not ext-setup-or-nil)
  2721. (setq ext-setup-or-nil setup-contents start 0)
  2722. (setq ext-setup-or-nil
  2723. (concat (substring ext-setup-or-nil 0 start)
  2724. "\n" setup-contents "\n"
  2725. (substring ext-setup-or-nil start)))))
  2726. ))))
  2727. (when cat
  2728. (org-set-local 'org-category (intern cat))
  2729. (push (cons "CATEGORY" cat) props))
  2730. (when prio
  2731. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2732. (setq prio (mapcar 'string-to-char prio))
  2733. (org-set-local 'org-highest-priority (nth 0 prio))
  2734. (org-set-local 'org-lowest-priority (nth 1 prio))
  2735. (org-set-local 'org-default-priority (nth 2 prio)))
  2736. (and props (org-set-local 'org-file-properties (nreverse props)))
  2737. (and ftags (org-set-local 'org-file-tags ftags))
  2738. (and drawers (org-set-local 'org-drawers drawers))
  2739. (and arch (org-set-local 'org-archive-location arch))
  2740. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2741. ;; Process the TODO keywords
  2742. (unless kwds
  2743. ;; Use the global values as if they had been given locally.
  2744. (setq kwds (default-value 'org-todo-keywords))
  2745. (if (stringp (car kwds))
  2746. (setq kwds (list (cons org-todo-interpretation
  2747. (default-value 'org-todo-keywords)))))
  2748. (setq kwds (reverse kwds)))
  2749. (setq kwds (nreverse kwds))
  2750. (let (inter kws kw)
  2751. (while (setq kws (pop kwds))
  2752. (setq inter (pop kws) sep (member "|" kws)
  2753. kws0 (delete "|" (copy-sequence kws))
  2754. kwsa nil
  2755. kws1 (mapcar
  2756. (lambda (x)
  2757. ;; 1 2
  2758. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2759. (progn
  2760. (setq kw (match-string 1 x)
  2761. key (and (match-end 2) (match-string 2 x))
  2762. log (org-extract-log-state-settings x))
  2763. (push (cons kw (and key (string-to-char key))) kwsa)
  2764. (and log (push log org-todo-log-states))
  2765. kw)
  2766. (error "Invalid TODO keyword %s" x)))
  2767. kws0)
  2768. kwsa (if kwsa (append '((:startgroup))
  2769. (nreverse kwsa)
  2770. '((:endgroup))))
  2771. hw (car kws1)
  2772. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2773. tail (list inter hw (car dws) (org-last dws)))
  2774. (add-to-list 'org-todo-heads hw 'append)
  2775. (push kws1 org-todo-sets)
  2776. (setq org-done-keywords (append org-done-keywords dws nil))
  2777. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2778. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2779. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2780. (setq org-todo-sets (nreverse org-todo-sets)
  2781. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2782. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2783. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2784. ;; Process the constants
  2785. (when const
  2786. (let (e cst)
  2787. (while (setq e (pop const))
  2788. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2789. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2790. (setq org-table-formula-constants-local cst)))
  2791. ;; Process the tags.
  2792. (when tags
  2793. (let (e tgs)
  2794. (while (setq e (pop tags))
  2795. (cond
  2796. ((equal e "{") (push '(:startgroup) tgs))
  2797. ((equal e "}") (push '(:endgroup) tgs))
  2798. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2799. (push (cons (match-string 1 e)
  2800. (string-to-char (match-string 2 e)))
  2801. tgs))
  2802. (t (push (list e) tgs))))
  2803. (org-set-local 'org-tag-alist nil)
  2804. (while (setq e (pop tgs))
  2805. (or (and (stringp (car e))
  2806. (assoc (car e) org-tag-alist))
  2807. (push e org-tag-alist)))))
  2808. ;; Compute the regular expressions and other local variables
  2809. (if (not org-done-keywords)
  2810. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2811. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2812. (length org-scheduled-string)
  2813. (length org-clock-string)
  2814. (length org-closed-string)))
  2815. org-drawer-regexp
  2816. (concat "^[ \t]*:\\("
  2817. (mapconcat 'regexp-quote org-drawers "\\|")
  2818. "\\):[ \t]*$")
  2819. org-not-done-keywords
  2820. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2821. org-todo-regexp
  2822. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2823. "\\|") "\\)\\>")
  2824. org-not-done-regexp
  2825. (concat "\\<\\("
  2826. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2827. "\\)\\>")
  2828. org-todo-line-regexp
  2829. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2830. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2831. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2832. org-complex-heading-regexp
  2833. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2834. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2835. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2836. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2837. org-nl-done-regexp
  2838. (concat "\n\\*+[ \t]+"
  2839. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2840. "\\)" "\\>")
  2841. org-todo-line-tags-regexp
  2842. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2843. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2844. (org-re
  2845. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2846. org-looking-at-done-regexp
  2847. (concat "^" "\\(?:"
  2848. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2849. "\\>")
  2850. org-deadline-regexp (concat "\\<" org-deadline-string)
  2851. org-deadline-time-regexp
  2852. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2853. org-deadline-line-regexp
  2854. (concat "\\<\\(" org-deadline-string "\\).*")
  2855. org-scheduled-regexp
  2856. (concat "\\<" org-scheduled-string)
  2857. org-scheduled-time-regexp
  2858. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2859. org-closed-time-regexp
  2860. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2861. org-keyword-time-regexp
  2862. (concat "\\<\\(" org-scheduled-string
  2863. "\\|" org-deadline-string
  2864. "\\|" org-closed-string
  2865. "\\|" org-clock-string "\\)"
  2866. " *[[<]\\([^]>]+\\)[]>]")
  2867. org-keyword-time-not-clock-regexp
  2868. (concat "\\<\\(" org-scheduled-string
  2869. "\\|" org-deadline-string
  2870. "\\|" org-closed-string
  2871. "\\)"
  2872. " *[[<]\\([^]>]+\\)[]>]")
  2873. org-maybe-keyword-time-regexp
  2874. (concat "\\(\\<\\(" org-scheduled-string
  2875. "\\|" org-deadline-string
  2876. "\\|" org-closed-string
  2877. "\\|" org-clock-string "\\)\\)?"
  2878. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2879. org-planning-or-clock-line-re
  2880. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2881. "\\|" org-deadline-string
  2882. "\\|" org-closed-string "\\|" org-clock-string
  2883. "\\)\\>\\)")
  2884. )
  2885. (org-compute-latex-and-specials-regexp)
  2886. (org-set-font-lock-defaults))))
  2887. (defun org-file-contents (file &optional noerror)
  2888. "Return the contents of FILE, as a string."
  2889. (if (or (not file)
  2890. (not (file-readable-p file)))
  2891. (if noerror
  2892. (progn
  2893. (message "Cannot read file %s" file)
  2894. (ding) (sit-for 2)
  2895. "")
  2896. (error "Cannot read file %s" file))
  2897. (with-temp-buffer
  2898. (insert-file-contents file)
  2899. (buffer-string))))
  2900. (defun org-extract-log-state-settings (x)
  2901. "Extract the log state setting from a TODO keyword string.
  2902. This will extract info from a string like \"WAIT(w@/!)\"."
  2903. (let (kw key log1 log2)
  2904. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2905. (setq kw (match-string 1 x)
  2906. key (and (match-end 2) (match-string 2 x))
  2907. log1 (and (match-end 3) (match-string 3 x))
  2908. log2 (and (match-end 4) (match-string 4 x)))
  2909. (and (or log1 log2)
  2910. (list kw
  2911. (and log1 (if (equal log1 "!") 'time 'note))
  2912. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2913. (defun org-remove-keyword-keys (list)
  2914. "Remove a pair of parenthesis at the end of each string in LIST."
  2915. (mapcar (lambda (x)
  2916. (if (string-match "(.*)$" x)
  2917. (substring x 0 (match-beginning 0))
  2918. x))
  2919. list))
  2920. ;; FIXME: this could be done much better, using second characters etc.
  2921. (defun org-assign-fast-keys (alist)
  2922. "Assign fast keys to a keyword-key alist.
  2923. Respect keys that are already there."
  2924. (let (new e k c c1 c2 (char ?a))
  2925. (while (setq e (pop alist))
  2926. (cond
  2927. ((equal e '(:startgroup)) (push e new))
  2928. ((equal e '(:endgroup)) (push e new))
  2929. (t
  2930. (setq k (car e) c2 nil)
  2931. (if (cdr e)
  2932. (setq c (cdr e))
  2933. ;; automatically assign a character.
  2934. (setq c1 (string-to-char
  2935. (downcase (substring
  2936. k (if (= (string-to-char k) ?@) 1 0)))))
  2937. (if (or (rassoc c1 new) (rassoc c1 alist))
  2938. (while (or (rassoc char new) (rassoc char alist))
  2939. (setq char (1+ char)))
  2940. (setq c2 c1))
  2941. (setq c (or c2 char)))
  2942. (push (cons k c) new))))
  2943. (nreverse new)))
  2944. ;;; Some variables used in various places
  2945. (defvar org-window-configuration nil
  2946. "Used in various places to store a window configuration.")
  2947. (defvar org-finish-function nil
  2948. "Function to be called when `C-c C-c' is used.
  2949. This is for getting out of special buffers like remember.")
  2950. ;; FIXME: Occasionally check by commenting these, to make sure
  2951. ;; no other functions uses these, forgetting to let-bind them.
  2952. (defvar entry)
  2953. (defvar state)
  2954. (defvar last-state)
  2955. (defvar date)
  2956. (defvar description)
  2957. ;; Defined somewhere in this file, but used before definition.
  2958. (defvar org-html-entities)
  2959. (defvar org-struct-menu)
  2960. (defvar org-org-menu)
  2961. (defvar org-tbl-menu)
  2962. (defvar org-agenda-keymap)
  2963. ;;;; Define the Org-mode
  2964. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2965. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
  2966. ;; We use a before-change function to check if a table might need
  2967. ;; an update.
  2968. (defvar org-table-may-need-update t
  2969. "Indicates that a table might need an update.
  2970. This variable is set by `org-before-change-function'.
  2971. `org-table-align' sets it back to nil.")
  2972. (defun org-before-change-function (beg end)
  2973. "Every change indicates that a table might need an update."
  2974. (setq org-table-may-need-update t))
  2975. (defvar org-mode-map)
  2976. (defvar org-mode-hook nil)
  2977. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2978. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2979. (defvar org-table-buffer-is-an nil)
  2980. (defconst org-outline-regexp "\\*+ ")
  2981. ;;;###autoload
  2982. (define-derived-mode org-mode outline-mode "Org"
  2983. "Outline-based notes management and organizer, alias
  2984. \"Carsten's outline-mode for keeping track of everything.\"
  2985. Org-mode develops organizational tasks around a NOTES file which
  2986. contains information about projects as plain text. Org-mode is
  2987. implemented on top of outline-mode, which is ideal to keep the content
  2988. of large files well structured. It supports ToDo items, deadlines and
  2989. time stamps, which magically appear in the diary listing of the Emacs
  2990. calendar. Tables are easily created with a built-in table editor.
  2991. Plain text URL-like links connect to websites, emails (VM), Usenet
  2992. messages (Gnus), BBDB entries, and any files related to the project.
  2993. For printing and sharing of notes, an Org-mode file (or a part of it)
  2994. can be exported as a structured ASCII or HTML file.
  2995. The following commands are available:
  2996. \\{org-mode-map}"
  2997. ;; Get rid of Outline menus, they are not needed
  2998. ;; Need to do this here because define-derived-mode sets up
  2999. ;; the keymap so late. Still, it is a waste to call this each time
  3000. ;; we switch another buffer into org-mode.
  3001. (if (featurep 'xemacs)
  3002. (when (boundp 'outline-mode-menu-heading)
  3003. ;; Assume this is Greg's port, it used easymenu
  3004. (easy-menu-remove outline-mode-menu-heading)
  3005. (easy-menu-remove outline-mode-menu-show)
  3006. (easy-menu-remove outline-mode-menu-hide))
  3007. (define-key org-mode-map [menu-bar headings] 'undefined)
  3008. (define-key org-mode-map [menu-bar hide] 'undefined)
  3009. (define-key org-mode-map [menu-bar show] 'undefined))
  3010. (org-load-modules-maybe)
  3011. (easy-menu-add org-org-menu)
  3012. (easy-menu-add org-tbl-menu)
  3013. (org-install-agenda-files-menu)
  3014. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3015. (org-add-to-invisibility-spec '(org-cwidth))
  3016. (when (featurep 'xemacs)
  3017. (org-set-local 'line-move-ignore-invisible t))
  3018. (org-set-local 'outline-regexp org-outline-regexp)
  3019. (org-set-local 'outline-level 'org-outline-level)
  3020. (when (and org-ellipsis
  3021. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3022. (fboundp 'make-glyph-code))
  3023. (unless org-display-table
  3024. (setq org-display-table (make-display-table)))
  3025. (set-display-table-slot
  3026. org-display-table 4
  3027. (vconcat (mapcar
  3028. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3029. org-ellipsis)))
  3030. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3031. (setq buffer-display-table org-display-table))
  3032. (org-set-regexps-and-options)
  3033. (when (and org-tag-faces (not org-tags-special-faces-re))
  3034. ;; tag faces set outside customize.... force initialization.
  3035. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3036. ;; Calc embedded
  3037. (org-set-local 'calc-embedded-open-mode "# ")
  3038. (modify-syntax-entry ?# "<")
  3039. (modify-syntax-entry ?@ "w")
  3040. (if org-startup-truncated (setq truncate-lines t))
  3041. (org-set-local 'font-lock-unfontify-region-function
  3042. 'org-unfontify-region)
  3043. ;; Activate before-change-function
  3044. (org-set-local 'org-table-may-need-update t)
  3045. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3046. 'local)
  3047. ;; Check for running clock before killing a buffer
  3048. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3049. ;; Paragraphs and auto-filling
  3050. (org-set-autofill-regexps)
  3051. (setq indent-line-function 'org-indent-line-function)
  3052. (org-update-radio-target-regexp)
  3053. ;; Comment characters
  3054. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3055. (org-set-local 'comment-padding " ")
  3056. ;; Align options lines
  3057. (org-set-local
  3058. 'align-mode-rules-list
  3059. '((org-in-buffer-settings
  3060. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3061. (modes . '(org-mode)))))
  3062. ;; Imenu
  3063. (org-set-local 'imenu-create-index-function
  3064. 'org-imenu-get-tree)
  3065. ;; Make isearch reveal context
  3066. (if (or (featurep 'xemacs)
  3067. (not (boundp 'outline-isearch-open-invisible-function)))
  3068. ;; Emacs 21 and XEmacs make use of the hook
  3069. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3070. ;; Emacs 22 deals with this through a special variable
  3071. (org-set-local 'outline-isearch-open-invisible-function
  3072. (lambda (&rest ignore) (org-show-context 'isearch))))
  3073. ;; If empty file that did not turn on org-mode automatically, make it to.
  3074. (if (and org-insert-mode-line-in-empty-file
  3075. (interactive-p)
  3076. (= (point-min) (point-max)))
  3077. (insert "# -*- mode: org -*-\n\n"))
  3078. (unless org-inhibit-startup
  3079. (when org-startup-align-all-tables
  3080. (let ((bmp (buffer-modified-p)))
  3081. (org-table-map-tables 'org-table-align)
  3082. (set-buffer-modified-p bmp)))
  3083. (org-set-startup-visibility)))
  3084. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3085. (defun org-current-time ()
  3086. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3087. (if (> (car org-time-stamp-rounding-minutes) 1)
  3088. (let ((r (car org-time-stamp-rounding-minutes))
  3089. (time (decode-time)))
  3090. (apply 'encode-time
  3091. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3092. (nthcdr 2 time))))
  3093. (current-time)))
  3094. ;;;; Font-Lock stuff, including the activators
  3095. (defvar org-mouse-map (make-sparse-keymap))
  3096. (org-defkey org-mouse-map
  3097. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3098. (org-defkey org-mouse-map
  3099. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3100. (when org-mouse-1-follows-link
  3101. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3102. (when org-tab-follows-link
  3103. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3104. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3105. (when org-return-follows-link
  3106. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3107. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3108. (require 'font-lock)
  3109. (defconst org-non-link-chars "]\t\n\r<>")
  3110. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3111. "shell" "elisp"))
  3112. (defvar org-link-types-re nil
  3113. "Matches a link that has a url-like prefix like \"http:\"")
  3114. (defvar org-link-re-with-space nil
  3115. "Matches a link with spaces, optional angular brackets around it.")
  3116. (defvar org-link-re-with-space2 nil
  3117. "Matches a link with spaces, optional angular brackets around it.")
  3118. (defvar org-link-re-with-space3 nil
  3119. "Matches a link with spaces, only for internal part in bracket links.")
  3120. (defvar org-angle-link-re nil
  3121. "Matches link with angular brackets, spaces are allowed.")
  3122. (defvar org-plain-link-re nil
  3123. "Matches plain link, without spaces.")
  3124. (defvar org-bracket-link-regexp nil
  3125. "Matches a link in double brackets.")
  3126. (defvar org-bracket-link-analytic-regexp nil
  3127. "Regular expression used to analyze links.
  3128. Here is what the match groups contain after a match:
  3129. 1: http:
  3130. 2: http
  3131. 3: path
  3132. 4: [desc]
  3133. 5: desc")
  3134. (defvar org-any-link-re nil
  3135. "Regular expression matching any link.")
  3136. (defun org-make-link-regexps ()
  3137. "Update the link regular expressions.
  3138. This should be called after the variable `org-link-types' has changed."
  3139. (setq org-link-types-re
  3140. (concat
  3141. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3142. org-link-re-with-space
  3143. (concat
  3144. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3145. "\\([^" org-non-link-chars " ]"
  3146. "[^" org-non-link-chars "]*"
  3147. "[^" org-non-link-chars " ]\\)>?")
  3148. org-link-re-with-space2
  3149. (concat
  3150. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3151. "\\([^" org-non-link-chars " ]"
  3152. "[^\t\n\r]*"
  3153. "[^" org-non-link-chars " ]\\)>?")
  3154. org-link-re-with-space3
  3155. (concat
  3156. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3157. "\\([^" org-non-link-chars " ]"
  3158. "[^\t\n\r]*\\)")
  3159. org-angle-link-re
  3160. (concat
  3161. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3162. "\\([^" org-non-link-chars " ]"
  3163. "[^" org-non-link-chars "]*"
  3164. "\\)>")
  3165. org-plain-link-re
  3166. (concat
  3167. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3168. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3169. org-bracket-link-regexp
  3170. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3171. org-bracket-link-analytic-regexp
  3172. (concat
  3173. "\\[\\["
  3174. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3175. "\\([^]]+\\)"
  3176. "\\]"
  3177. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3178. "\\]")
  3179. org-any-link-re
  3180. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3181. org-angle-link-re "\\)\\|\\("
  3182. org-plain-link-re "\\)")))
  3183. (org-make-link-regexps)
  3184. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3185. "Regular expression for fast time stamp matching.")
  3186. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3187. "Regular expression for fast time stamp matching.")
  3188. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3189. "Regular expression matching time strings for analysis.
  3190. This one does not require the space after the date, so it can be used
  3191. on a string that terminates immediately after the date.")
  3192. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3193. "Regular expression matching time strings for analysis.")
  3194. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3195. "Regular expression matching time stamps, with groups.")
  3196. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3197. "Regular expression matching time stamps (also [..]), with groups.")
  3198. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3199. "Regular expression matching a time stamp range.")
  3200. (defconst org-tr-regexp-both
  3201. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3202. "Regular expression matching a time stamp range.")
  3203. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3204. org-ts-regexp "\\)?")
  3205. "Regular expression matching a time stamp or time stamp range.")
  3206. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3207. org-ts-regexp-both "\\)?")
  3208. "Regular expression matching a time stamp or time stamp range.
  3209. The time stamps may be either active or inactive.")
  3210. (defvar org-emph-face nil)
  3211. (defun org-do-emphasis-faces (limit)
  3212. "Run through the buffer and add overlays to links."
  3213. (let (rtn)
  3214. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3215. (if (not (= (char-after (match-beginning 3))
  3216. (char-after (match-beginning 4))))
  3217. (progn
  3218. (setq rtn t)
  3219. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3220. 'face
  3221. (nth 1 (assoc (match-string 3)
  3222. org-emphasis-alist)))
  3223. (add-text-properties (match-beginning 2) (match-end 2)
  3224. '(font-lock-multiline t))
  3225. (when org-hide-emphasis-markers
  3226. (add-text-properties (match-end 4) (match-beginning 5)
  3227. '(invisible org-link))
  3228. (add-text-properties (match-beginning 3) (match-end 3)
  3229. '(invisible org-link)))))
  3230. (backward-char 1))
  3231. rtn))
  3232. (defun org-emphasize (&optional char)
  3233. "Insert or change an emphasis, i.e. a font like bold or italic.
  3234. If there is an active region, change that region to a new emphasis.
  3235. If there is no region, just insert the marker characters and position
  3236. the cursor between them.
  3237. CHAR should be either the marker character, or the first character of the
  3238. HTML tag associated with that emphasis. If CHAR is a space, the means
  3239. to remove the emphasis of the selected region.
  3240. If char is not given (for example in an interactive call) it
  3241. will be prompted for."
  3242. (interactive)
  3243. (let ((eal org-emphasis-alist) e det
  3244. (erc org-emphasis-regexp-components)
  3245. (prompt "")
  3246. (string "") beg end move tag c s)
  3247. (if (org-region-active-p)
  3248. (setq beg (region-beginning) end (region-end)
  3249. string (buffer-substring beg end))
  3250. (setq move t))
  3251. (while (setq e (pop eal))
  3252. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3253. c (aref tag 0))
  3254. (push (cons c (string-to-char (car e))) det)
  3255. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3256. (substring tag 1)))))
  3257. (setq det (nreverse det))
  3258. (unless char
  3259. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3260. (setq char (read-char-exclusive)))
  3261. (setq char (or (cdr (assoc char det)) char))
  3262. (if (equal char ?\ )
  3263. (setq s "" move nil)
  3264. (unless (assoc (char-to-string char) org-emphasis-alist)
  3265. (error "No such emphasis marker: \"%c\"" char))
  3266. (setq s (char-to-string char)))
  3267. (while (and (> (length string) 1)
  3268. (equal (substring string 0 1) (substring string -1))
  3269. (assoc (substring string 0 1) org-emphasis-alist))
  3270. (setq string (substring string 1 -1)))
  3271. (setq string (concat s string s))
  3272. (if beg (delete-region beg end))
  3273. (unless (or (bolp)
  3274. (string-match (concat "[" (nth 0 erc) "\n]")
  3275. (char-to-string (char-before (point)))))
  3276. (insert " "))
  3277. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3278. (char-to-string (char-after (point))))
  3279. (insert " ") (backward-char 1))
  3280. (insert string)
  3281. (and move (backward-char 1))))
  3282. (defconst org-nonsticky-props
  3283. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3284. (defun org-activate-plain-links (limit)
  3285. "Run through the buffer and add overlays to links."
  3286. (catch 'exit
  3287. (let (f)
  3288. (while (re-search-forward org-plain-link-re limit t)
  3289. (setq f (get-text-property (match-beginning 0) 'face))
  3290. (if (or (eq f 'org-tag)
  3291. (and (listp f) (memq 'org-tag f)))
  3292. nil
  3293. (add-text-properties (match-beginning 0) (match-end 0)
  3294. (list 'mouse-face 'highlight
  3295. 'rear-nonsticky org-nonsticky-props
  3296. 'keymap org-mouse-map
  3297. ))
  3298. (throw 'exit t))))))
  3299. (defun org-activate-code (limit)
  3300. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3301. (progn
  3302. (remove-text-properties (match-beginning 0) (match-end 0)
  3303. '(display t invisible t intangible t))
  3304. t)))
  3305. (defun org-activate-angle-links (limit)
  3306. "Run through the buffer and add overlays to links."
  3307. (if (re-search-forward org-angle-link-re limit t)
  3308. (progn
  3309. (add-text-properties (match-beginning 0) (match-end 0)
  3310. (list 'mouse-face 'highlight
  3311. 'rear-nonsticky org-nonsticky-props
  3312. 'keymap org-mouse-map
  3313. ))
  3314. t)))
  3315. (defun org-activate-bracket-links (limit)
  3316. "Run through the buffer and add overlays to bracketed links."
  3317. (if (re-search-forward org-bracket-link-regexp limit t)
  3318. (let* ((help (concat "LINK: "
  3319. (org-match-string-no-properties 1)))
  3320. ;; FIXME: above we should remove the escapes.
  3321. ;; but that requires another match, protecting match data,
  3322. ;; a lot of overhead for font-lock.
  3323. (ip (org-maybe-intangible
  3324. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3325. 'keymap org-mouse-map 'mouse-face 'highlight
  3326. 'font-lock-multiline t 'help-echo help)))
  3327. (vp (list 'rear-nonsticky org-nonsticky-props
  3328. 'keymap org-mouse-map 'mouse-face 'highlight
  3329. ' font-lock-multiline t 'help-echo help)))
  3330. ;; We need to remove the invisible property here. Table narrowing
  3331. ;; may have made some of this invisible.
  3332. (remove-text-properties (match-beginning 0) (match-end 0)
  3333. '(invisible nil))
  3334. (if (match-end 3)
  3335. (progn
  3336. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3337. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3338. (add-text-properties (match-end 3) (match-end 0) ip))
  3339. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3340. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3341. (add-text-properties (match-end 1) (match-end 0) ip))
  3342. t)))
  3343. (defun org-activate-dates (limit)
  3344. "Run through the buffer and add overlays to dates."
  3345. (if (re-search-forward org-tsr-regexp-both limit t)
  3346. (progn
  3347. (add-text-properties (match-beginning 0) (match-end 0)
  3348. (list 'mouse-face 'highlight
  3349. 'rear-nonsticky org-nonsticky-props
  3350. 'keymap org-mouse-map))
  3351. (when org-display-custom-times
  3352. (if (match-end 3)
  3353. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3354. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3355. t)))
  3356. (defvar org-target-link-regexp nil
  3357. "Regular expression matching radio targets in plain text.")
  3358. (make-variable-buffer-local 'org-target-link-regexp)
  3359. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3360. "Regular expression matching a link target.")
  3361. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3362. "Regular expression matching a radio target.")
  3363. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3364. "Regular expression matching any target.")
  3365. (defun org-activate-target-links (limit)
  3366. "Run through the buffer and add overlays to target matches."
  3367. (when org-target-link-regexp
  3368. (let ((case-fold-search t))
  3369. (if (re-search-forward org-target-link-regexp limit t)
  3370. (progn
  3371. (add-text-properties (match-beginning 0) (match-end 0)
  3372. (list 'mouse-face 'highlight
  3373. 'rear-nonsticky org-nonsticky-props
  3374. 'keymap org-mouse-map
  3375. 'help-echo "Radio target link"
  3376. 'org-linked-text t))
  3377. t)))))
  3378. (defun org-update-radio-target-regexp ()
  3379. "Find all radio targets in this file and update the regular expression."
  3380. (interactive)
  3381. (when (memq 'radio org-activate-links)
  3382. (setq org-target-link-regexp
  3383. (org-make-target-link-regexp (org-all-targets 'radio)))
  3384. (org-restart-font-lock)))
  3385. (defun org-hide-wide-columns (limit)
  3386. (let (s e)
  3387. (setq s (text-property-any (point) (or limit (point-max))
  3388. 'org-cwidth t))
  3389. (when s
  3390. (setq e (next-single-property-change s 'org-cwidth))
  3391. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3392. (goto-char e)
  3393. t)))
  3394. (defvar org-latex-and-specials-regexp nil
  3395. "Regular expression for highlighting export special stuff.")
  3396. (defvar org-match-substring-regexp)
  3397. (defvar org-match-substring-with-braces-regexp)
  3398. (defvar org-export-html-special-string-regexps)
  3399. (defun org-compute-latex-and-specials-regexp ()
  3400. "Compute regular expression for stuff treated specially by exporters."
  3401. (if (not org-highlight-latex-fragments-and-specials)
  3402. (org-set-local 'org-latex-and-specials-regexp nil)
  3403. (require 'org-exp)
  3404. (let*
  3405. ((matchers (plist-get org-format-latex-options :matchers))
  3406. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3407. org-latex-regexps)))
  3408. (options (org-combine-plists (org-default-export-plist)
  3409. (org-infile-export-plist)))
  3410. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3411. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3412. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3413. (org-export-html-expand (plist-get options :expand-quoted-html))
  3414. (org-export-with-special-strings (plist-get options :special-strings))
  3415. (re-sub
  3416. (cond
  3417. ((equal org-export-with-sub-superscripts '{})
  3418. (list org-match-substring-with-braces-regexp))
  3419. (org-export-with-sub-superscripts
  3420. (list org-match-substring-regexp))
  3421. (t nil)))
  3422. (re-latex
  3423. (if org-export-with-LaTeX-fragments
  3424. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3425. (re-macros
  3426. (if org-export-with-TeX-macros
  3427. (list (concat "\\\\"
  3428. (regexp-opt
  3429. (append (mapcar 'car org-html-entities)
  3430. (if (boundp 'org-latex-entities)
  3431. org-latex-entities nil))
  3432. 'words))) ; FIXME
  3433. ))
  3434. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3435. (re-special (if org-export-with-special-strings
  3436. (mapcar (lambda (x) (car x))
  3437. org-export-html-special-string-regexps)))
  3438. (re-rest
  3439. (delq nil
  3440. (list
  3441. (if org-export-html-expand "@<[^>\n]+>")
  3442. ))))
  3443. (org-set-local
  3444. 'org-latex-and-specials-regexp
  3445. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3446. re-rest) "\\|")))))
  3447. (defun org-do-latex-and-special-faces (limit)
  3448. "Run through the buffer and add overlays to links."
  3449. (when org-latex-and-specials-regexp
  3450. (let (rtn d)
  3451. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3452. limit t))
  3453. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3454. 'face))
  3455. '(org-code org-verbatim underline)))
  3456. (progn
  3457. (setq rtn t
  3458. d (cond ((member (char-after (1+ (match-beginning 0)))
  3459. '(?_ ?^)) 1)
  3460. (t 0)))
  3461. (font-lock-prepend-text-property
  3462. (+ d (match-beginning 0)) (match-end 0)
  3463. 'face 'org-latex-and-export-specials)
  3464. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3465. '(font-lock-multiline t)))))
  3466. rtn)))
  3467. (defun org-restart-font-lock ()
  3468. "Restart font-lock-mode, to force refontification."
  3469. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3470. (font-lock-mode -1)
  3471. (font-lock-mode 1)))
  3472. (defun org-all-targets (&optional radio)
  3473. "Return a list of all targets in this file.
  3474. With optional argument RADIO, only find radio targets."
  3475. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3476. rtn)
  3477. (save-excursion
  3478. (goto-char (point-min))
  3479. (while (re-search-forward re nil t)
  3480. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3481. rtn)))
  3482. (defun org-make-target-link-regexp (targets)
  3483. "Make regular expression matching all strings in TARGETS.
  3484. The regular expression finds the targets also if there is a line break
  3485. between words."
  3486. (and targets
  3487. (concat
  3488. "\\<\\("
  3489. (mapconcat
  3490. (lambda (x)
  3491. (while (string-match " +" x)
  3492. (setq x (replace-match "\\s-+" t t x)))
  3493. x)
  3494. targets
  3495. "\\|")
  3496. "\\)\\>")))
  3497. (defun org-activate-tags (limit)
  3498. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3499. (progn
  3500. (add-text-properties (match-beginning 1) (match-end 1)
  3501. (list 'mouse-face 'highlight
  3502. 'rear-nonsticky org-nonsticky-props
  3503. 'keymap org-mouse-map))
  3504. t)))
  3505. (defun org-outline-level ()
  3506. (save-excursion
  3507. (looking-at outline-regexp)
  3508. (if (match-beginning 1)
  3509. (+ (org-get-string-indentation (match-string 1)) 1000)
  3510. (1- (- (match-end 0) (match-beginning 0))))))
  3511. (defvar org-font-lock-keywords nil)
  3512. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3513. "Regular expression matching a property line.")
  3514. (defvar org-font-lock-hook nil
  3515. "Functions to be called for special font lock stuff.")
  3516. (defun org-font-lock-hook (limit)
  3517. (run-hook-with-args 'org-font-lock-hook limit))
  3518. (defun org-set-font-lock-defaults ()
  3519. (let* ((em org-fontify-emphasized-text)
  3520. (lk org-activate-links)
  3521. (org-font-lock-extra-keywords
  3522. (list
  3523. ;; Call the hook
  3524. '(org-font-lock-hook)
  3525. ;; Headlines
  3526. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3527. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3528. ;; Table lines
  3529. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3530. (1 'org-table t))
  3531. ;; Table internals
  3532. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3533. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3534. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3535. ;; Drawers
  3536. (list org-drawer-regexp '(0 'org-special-keyword t))
  3537. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3538. ;; Properties
  3539. (list org-property-re
  3540. '(1 'org-special-keyword t)
  3541. '(3 'org-property-value t))
  3542. (if org-format-transports-properties-p
  3543. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3544. ;; Links
  3545. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3546. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3547. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3548. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3549. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3550. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3551. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3552. '(org-hide-wide-columns (0 nil append))
  3553. ;; TODO lines
  3554. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3555. '(1 (org-get-todo-face 1) t))
  3556. ;; DONE
  3557. (if org-fontify-done-headline
  3558. (list (concat "^[*]+ +\\<\\("
  3559. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3560. "\\)\\(.*\\)")
  3561. '(2 'org-headline-done t))
  3562. nil)
  3563. ;; Priorities
  3564. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3565. ;; Tags
  3566. '(org-font-lock-add-tag-faces)
  3567. ;; Special keywords
  3568. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3569. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3570. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3571. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3572. ;; Emphasis
  3573. (if em
  3574. (if (featurep 'xemacs)
  3575. '(org-do-emphasis-faces (0 nil append))
  3576. '(org-do-emphasis-faces)))
  3577. ;; Checkboxes
  3578. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3579. 2 'bold prepend)
  3580. (if org-provide-checkbox-statistics
  3581. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3582. (0 (org-get-checkbox-statistics-face) t)))
  3583. ;; Description list items
  3584. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3585. 2 'bold prepend)
  3586. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3587. '(1 'org-archived prepend))
  3588. ;; Specials
  3589. '(org-do-latex-and-special-faces)
  3590. ;; Code
  3591. '(org-activate-code (1 'org-code t))
  3592. ;; COMMENT
  3593. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3594. "\\|" org-quote-string "\\)\\>")
  3595. '(1 'org-special-keyword t))
  3596. '("^#.*" (0 'font-lock-comment-face t))
  3597. )))
  3598. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3599. ;; Now set the full font-lock-keywords
  3600. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3601. (org-set-local 'font-lock-defaults
  3602. '(org-font-lock-keywords t nil nil backward-paragraph))
  3603. (kill-local-variable 'font-lock-keywords) nil))
  3604. (defvar org-m nil)
  3605. (defvar org-l nil)
  3606. (defvar org-f nil)
  3607. (defun org-get-level-face (n)
  3608. "Get the right face for match N in font-lock matching of headlines."
  3609. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3610. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3611. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3612. (cond
  3613. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3614. ((eq n 2) org-f)
  3615. (t (if org-level-color-stars-only nil org-f))))
  3616. (defun org-get-todo-face (kwd)
  3617. "Get the right face for a TODO keyword KWD.
  3618. If KWD is a number, get the corresponding match group."
  3619. (if (numberp kwd) (setq kwd (match-string kwd)))
  3620. (or (cdr (assoc kwd org-todo-keyword-faces))
  3621. (and (member kwd org-done-keywords) 'org-done)
  3622. 'org-todo))
  3623. (defun org-font-lock-add-tag-faces (limit)
  3624. "Add the special tag faces."
  3625. (when (and org-tag-faces org-tags-special-faces-re)
  3626. (while (re-search-forward org-tags-special-faces-re limit t)
  3627. (add-text-properties (match-beginning 1) (match-end 1)
  3628. (list 'face (org-get-tag-face 1)
  3629. 'font-lock-fontified t))
  3630. (backward-char 1))))
  3631. (defun org-get-tag-face (kwd)
  3632. "Get the right face for a TODO keyword KWD.
  3633. If KWD is a number, get the corresponding match group."
  3634. (if (numberp kwd) (setq kwd (match-string kwd)))
  3635. (or (cdr (assoc kwd org-tag-faces))
  3636. 'org-tag))
  3637. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3638. "Remove fontification and activation overlays from links."
  3639. (font-lock-default-unfontify-region beg end)
  3640. (let* ((buffer-undo-list t)
  3641. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3642. (inhibit-modification-hooks t)
  3643. deactivate-mark buffer-file-name buffer-file-truename)
  3644. (remove-text-properties beg end
  3645. '(mouse-face t keymap t org-linked-text t
  3646. invisible t intangible t))))
  3647. ;;;; Visibility cycling, including org-goto and indirect buffer
  3648. ;;; Cycling
  3649. (defvar org-cycle-global-status nil)
  3650. (make-variable-buffer-local 'org-cycle-global-status)
  3651. (defvar org-cycle-subtree-status nil)
  3652. (make-variable-buffer-local 'org-cycle-subtree-status)
  3653. ;;;###autoload
  3654. (defun org-cycle (&optional arg)
  3655. "Visibility cycling for Org-mode.
  3656. - When this function is called with a prefix argument, rotate the entire
  3657. buffer through 3 states (global cycling)
  3658. 1. OVERVIEW: Show only top-level headlines.
  3659. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3660. 3. SHOW ALL: Show everything.
  3661. When called with two C-u C-u prefixes, switch to the startup visibility,
  3662. determined by the variable `org-startup-folded', and by any VISIBILITY
  3663. properties in the buffer.
  3664. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3665. including drawers.
  3666. - When point is at the beginning of a headline, rotate the subtree started
  3667. by this line through 3 different states (local cycling)
  3668. 1. FOLDED: Only the main headline is shown.
  3669. 2. CHILDREN: The main headline and the direct children are shown.
  3670. From this state, you can move to one of the children
  3671. and zoom in further.
  3672. 3. SUBTREE: Show the entire subtree, including body text.
  3673. - When there is a numeric prefix, go up to a heading with level ARG, do
  3674. a `show-subtree' and return to the previous cursor position. If ARG
  3675. is negative, go up that many levels.
  3676. - When point is not at the beginning of a headline, execute the global
  3677. binding for TAB, which is re-indenting the line. See the option
  3678. `org-cycle-emulate-tab' for details.
  3679. - Special case: if point is at the beginning of the buffer and there is
  3680. no headline in line 1, this function will act as if called with prefix arg.
  3681. But only if also the variable `org-cycle-global-at-bob' is t."
  3682. (interactive "P")
  3683. (org-load-modules-maybe)
  3684. (let* ((outline-regexp
  3685. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3686. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3687. outline-regexp))
  3688. (bob-special (and org-cycle-global-at-bob (bobp)
  3689. (not (looking-at outline-regexp))))
  3690. (org-cycle-hook
  3691. (if bob-special
  3692. (delq 'org-optimize-window-after-visibility-change
  3693. (copy-sequence org-cycle-hook))
  3694. org-cycle-hook))
  3695. (pos (point)))
  3696. (if (or bob-special (equal arg '(4)))
  3697. ;; special case: use global cycling
  3698. (setq arg t))
  3699. (cond
  3700. ((equal arg '(16))
  3701. (org-set-startup-visibility)
  3702. (message "Startup visibility, plus VISIBILITY properties"))
  3703. ((equal arg '(64))
  3704. (show-all)
  3705. (message "Entire buffer visible, including drawers"))
  3706. ((org-at-table-p 'any)
  3707. ;; Enter the table or move to the next field in the table
  3708. (or (org-table-recognize-table.el)
  3709. (progn
  3710. (if arg (org-table-edit-field t)
  3711. (org-table-justify-field-maybe)
  3712. (call-interactively 'org-table-next-field)))))
  3713. ((eq arg t) ;; Global cycling
  3714. (cond
  3715. ((and (eq last-command this-command)
  3716. (eq org-cycle-global-status 'overview))
  3717. ;; We just created the overview - now do table of contents
  3718. ;; This can be slow in very large buffers, so indicate action
  3719. (message "CONTENTS...")
  3720. (org-content)
  3721. (message "CONTENTS...done")
  3722. (setq org-cycle-global-status 'contents)
  3723. (run-hook-with-args 'org-cycle-hook 'contents))
  3724. ((and (eq last-command this-command)
  3725. (eq org-cycle-global-status 'contents))
  3726. ;; We just showed the table of contents - now show everything
  3727. (show-all)
  3728. (message "SHOW ALL")
  3729. (setq org-cycle-global-status 'all)
  3730. (run-hook-with-args 'org-cycle-hook 'all))
  3731. (t
  3732. ;; Default action: go to overview
  3733. (org-overview)
  3734. (message "OVERVIEW")
  3735. (setq org-cycle-global-status 'overview)
  3736. (run-hook-with-args 'org-cycle-hook 'overview))))
  3737. ((and org-drawers org-drawer-regexp
  3738. (save-excursion
  3739. (beginning-of-line 1)
  3740. (looking-at org-drawer-regexp)))
  3741. ;; Toggle block visibility
  3742. (org-flag-drawer
  3743. (not (get-char-property (match-end 0) 'invisible))))
  3744. ((integerp arg)
  3745. ;; Show-subtree, ARG levels up from here.
  3746. (save-excursion
  3747. (org-back-to-heading)
  3748. (outline-up-heading (if (< arg 0) (- arg)
  3749. (- (funcall outline-level) arg)))
  3750. (org-show-subtree)))
  3751. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3752. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3753. ;; At a heading: rotate between three different views
  3754. (org-back-to-heading)
  3755. (let ((goal-column 0) eoh eol eos)
  3756. ;; First, some boundaries
  3757. (save-excursion
  3758. (org-back-to-heading)
  3759. (save-excursion
  3760. (beginning-of-line 2)
  3761. (while (and (not (eobp)) ;; this is like `next-line'
  3762. (get-char-property (1- (point)) 'invisible))
  3763. (beginning-of-line 2)) (setq eol (point)))
  3764. (outline-end-of-heading) (setq eoh (point))
  3765. (org-end-of-subtree t)
  3766. (unless (eobp)
  3767. (skip-chars-forward " \t\n")
  3768. (beginning-of-line 1) ; in case this is an item
  3769. )
  3770. (setq eos (1- (point))))
  3771. ;; Find out what to do next and set `this-command'
  3772. (cond
  3773. ((= eos eoh)
  3774. ;; Nothing is hidden behind this heading
  3775. (message "EMPTY ENTRY")
  3776. (setq org-cycle-subtree-status nil)
  3777. (save-excursion
  3778. (goto-char eos)
  3779. (outline-next-heading)
  3780. (if (org-invisible-p) (org-flag-heading nil))))
  3781. ((or (>= eol eos)
  3782. (not (string-match "\\S-" (buffer-substring eol eos))))
  3783. ;; Entire subtree is hidden in one line: open it
  3784. (org-show-entry)
  3785. (show-children)
  3786. (message "CHILDREN")
  3787. (save-excursion
  3788. (goto-char eos)
  3789. (outline-next-heading)
  3790. (if (org-invisible-p) (org-flag-heading nil)))
  3791. (setq org-cycle-subtree-status 'children)
  3792. (run-hook-with-args 'org-cycle-hook 'children))
  3793. ((and (eq last-command this-command)
  3794. (eq org-cycle-subtree-status 'children))
  3795. ;; We just showed the children, now show everything.
  3796. (org-show-subtree)
  3797. (message "SUBTREE")
  3798. (setq org-cycle-subtree-status 'subtree)
  3799. (run-hook-with-args 'org-cycle-hook 'subtree))
  3800. (t
  3801. ;; Default action: hide the subtree.
  3802. (hide-subtree)
  3803. (message "FOLDED")
  3804. (setq org-cycle-subtree-status 'folded)
  3805. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3806. ;; TAB emulation and template completion
  3807. (buffer-read-only (org-back-to-heading))
  3808. ((org-try-structure-completion))
  3809. ((org-try-cdlatex-tab))
  3810. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3811. (or (not (bolp))
  3812. (not (looking-at outline-regexp))))
  3813. (call-interactively (global-key-binding "\t")))
  3814. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3815. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3816. (or (and (eq org-cycle-emulate-tab 'white)
  3817. (= (match-end 0) (point-at-eol)))
  3818. (and (eq org-cycle-emulate-tab 'whitestart)
  3819. (>= (match-end 0) pos))))
  3820. t
  3821. (eq org-cycle-emulate-tab t))
  3822. (call-interactively (global-key-binding "\t")))
  3823. (t (save-excursion
  3824. (org-back-to-heading)
  3825. (org-cycle))))))
  3826. ;;;###autoload
  3827. (defun org-global-cycle (&optional arg)
  3828. "Cycle the global visibility. For details see `org-cycle'.
  3829. With C-u prefix arg, switch to startup visibility.
  3830. With a numeric prefix, show all headlines up to that level."
  3831. (interactive "P")
  3832. (let ((org-cycle-include-plain-lists
  3833. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3834. (cond
  3835. ((integerp arg)
  3836. (show-all)
  3837. (hide-sublevels arg)
  3838. (setq org-cycle-global-status 'contents))
  3839. ((equal arg '(4))
  3840. (org-set-startup-visibility)
  3841. (message "Startup visibility, plus VISIBILITY properties."))
  3842. (t
  3843. (org-cycle '(4))))))
  3844. (defun org-set-startup-visibility ()
  3845. "Set the visibility required by startup options and properties."
  3846. (cond
  3847. ((eq org-startup-folded t)
  3848. (org-cycle '(4)))
  3849. ((eq org-startup-folded 'content)
  3850. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3851. (org-cycle '(4)) (org-cycle '(4)))))
  3852. (org-set-visibility-according-to-property 'no-cleanup)
  3853. (org-cycle-hide-archived-subtrees 'all)
  3854. (org-cycle-hide-drawers 'all)
  3855. (org-cycle-show-empty-lines 'all))
  3856. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3857. "Switch subtree visibilities according to :VISIBILITY: property."
  3858. (interactive)
  3859. (let (state)
  3860. (save-excursion
  3861. (goto-char (point-min))
  3862. (while (re-search-forward
  3863. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3864. nil t)
  3865. (setq state (match-string 1))
  3866. (save-excursion
  3867. (org-back-to-heading t)
  3868. (hide-subtree)
  3869. (org-reveal)
  3870. (cond
  3871. ((equal state '("fold" "folded"))
  3872. (hide-subtree))
  3873. ((equal state "children")
  3874. (org-show-hidden-entry)
  3875. (show-children))
  3876. ((equal state "content")
  3877. (save-excursion
  3878. (save-restriction
  3879. (org-narrow-to-subtree)
  3880. (org-content))))
  3881. ((member state '("all" "showall"))
  3882. (show-subtree)))))
  3883. (unless no-cleanup
  3884. (org-cycle-hide-archived-subtrees 'all)
  3885. (org-cycle-hide-drawers 'all)
  3886. (org-cycle-show-empty-lines 'all)))))
  3887. (defun org-overview ()
  3888. "Switch to overview mode, showing only top-level headlines.
  3889. Really, this shows all headlines with level equal or greater than the level
  3890. of the first headline in the buffer. This is important, because if the
  3891. first headline is not level one, then (hide-sublevels 1) gives confusing
  3892. results."
  3893. (interactive)
  3894. (let ((level (save-excursion
  3895. (goto-char (point-min))
  3896. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3897. (progn
  3898. (goto-char (match-beginning 0))
  3899. (funcall outline-level))))))
  3900. (and level (hide-sublevels level))))
  3901. (defun org-content (&optional arg)
  3902. "Show all headlines in the buffer, like a table of contents.
  3903. With numerical argument N, show content up to level N."
  3904. (interactive "P")
  3905. (save-excursion
  3906. ;; Visit all headings and show their offspring
  3907. (and (integerp arg) (org-overview))
  3908. (goto-char (point-max))
  3909. (catch 'exit
  3910. (while (and (progn (condition-case nil
  3911. (outline-previous-visible-heading 1)
  3912. (error (goto-char (point-min))))
  3913. t)
  3914. (looking-at outline-regexp))
  3915. (if (integerp arg)
  3916. (show-children (1- arg))
  3917. (show-branches))
  3918. (if (bobp) (throw 'exit nil))))))
  3919. (defun org-optimize-window-after-visibility-change (state)
  3920. "Adjust the window after a change in outline visibility.
  3921. This function is the default value of the hook `org-cycle-hook'."
  3922. (when (get-buffer-window (current-buffer))
  3923. (cond
  3924. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3925. ; ((eq state 'overview) (org-beginning-of-line))
  3926. ((eq state 'content) nil)
  3927. ((eq state 'all) nil)
  3928. ((eq state 'folded) nil)
  3929. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3930. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3931. (defun org-compact-display-after-subtree-move ()
  3932. (let (beg end)
  3933. (save-excursion
  3934. (if (org-up-heading-safe)
  3935. (progn
  3936. (hide-subtree)
  3937. (show-entry)
  3938. (show-children)
  3939. (org-cycle-show-empty-lines 'children)
  3940. (org-cycle-hide-drawers 'children))
  3941. (org-overview)))))
  3942. (defun org-cycle-show-empty-lines (state)
  3943. "Show empty lines above all visible headlines.
  3944. The region to be covered depends on STATE when called through
  3945. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3946. entire buffer covered. Note that an empty line is only shown if there
  3947. are at least `org-cycle-separator-lines' empty lines before the headline."
  3948. (when (> org-cycle-separator-lines 0)
  3949. (save-excursion
  3950. (let* ((n org-cycle-separator-lines)
  3951. (re (cond
  3952. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3953. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3954. (t (let ((ns (number-to-string (- n 2))))
  3955. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3956. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3957. beg end)
  3958. (cond
  3959. ((memq state '(overview contents t))
  3960. (setq beg (point-min) end (point-max)))
  3961. ((memq state '(children folded))
  3962. (setq beg (point) end (progn (org-end-of-subtree t t)
  3963. (beginning-of-line 2)
  3964. (point)))))
  3965. (when beg
  3966. (goto-char beg)
  3967. (while (re-search-forward re end t)
  3968. (if (not (get-char-property (match-end 1) 'invisible))
  3969. (outline-flag-region
  3970. (match-beginning 1) (match-end 1) nil)))))))
  3971. ;; Never hide empty lines at the end of the file.
  3972. (save-excursion
  3973. (goto-char (point-max))
  3974. (outline-previous-heading)
  3975. (outline-end-of-heading)
  3976. (if (and (looking-at "[ \t\n]+")
  3977. (= (match-end 0) (point-max)))
  3978. (outline-flag-region (point) (match-end 0) nil))))
  3979. (defun org-show-empty-lines-in-parent ()
  3980. "Move to the parent and re-show empty lines before visible headlines."
  3981. (save-excursion
  3982. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3983. (org-cycle-show-empty-lines context))))
  3984. (defun org-cycle-hide-drawers (state)
  3985. "Re-hide all drawers after a visibility state change."
  3986. (when (and (org-mode-p)
  3987. (not (memq state '(overview folded))))
  3988. (save-excursion
  3989. (let* ((globalp (memq state '(contents all)))
  3990. (beg (if globalp (point-min) (point)))
  3991. (end (if globalp (point-max) (org-end-of-subtree t))))
  3992. (goto-char beg)
  3993. (while (re-search-forward org-drawer-regexp end t)
  3994. (org-flag-drawer t))))))
  3995. (defun org-flag-drawer (flag)
  3996. (save-excursion
  3997. (beginning-of-line 1)
  3998. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3999. (let ((b (match-end 0))
  4000. (outline-regexp org-outline-regexp))
  4001. (if (re-search-forward
  4002. "^[ \t]*:END:"
  4003. (save-excursion (outline-next-heading) (point)) t)
  4004. (outline-flag-region b (point-at-eol) flag)
  4005. (error ":END: line missing"))))))
  4006. (defun org-subtree-end-visible-p ()
  4007. "Is the end of the current subtree visible?"
  4008. (pos-visible-in-window-p
  4009. (save-excursion (org-end-of-subtree t) (point))))
  4010. (defun org-first-headline-recenter (&optional N)
  4011. "Move cursor to the first headline and recenter the headline.
  4012. Optional argument N means, put the headline into the Nth line of the window."
  4013. (goto-char (point-min))
  4014. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4015. (beginning-of-line)
  4016. (recenter (prefix-numeric-value N))))
  4017. ;;; Org-goto
  4018. (defvar org-goto-window-configuration nil)
  4019. (defvar org-goto-marker nil)
  4020. (defvar org-goto-map
  4021. (let ((map (make-sparse-keymap)))
  4022. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4023. (while (setq cmd (pop cmds))
  4024. (substitute-key-definition cmd cmd map global-map)))
  4025. (suppress-keymap map)
  4026. (org-defkey map "\C-m" 'org-goto-ret)
  4027. (org-defkey map [(return)] 'org-goto-ret)
  4028. (org-defkey map [(left)] 'org-goto-left)
  4029. (org-defkey map [(right)] 'org-goto-right)
  4030. (org-defkey map [(control ?g)] 'org-goto-quit)
  4031. (org-defkey map "\C-i" 'org-cycle)
  4032. (org-defkey map [(tab)] 'org-cycle)
  4033. (org-defkey map [(down)] 'outline-next-visible-heading)
  4034. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4035. (if org-goto-auto-isearch
  4036. (if (fboundp 'define-key-after)
  4037. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4038. nil)
  4039. (org-defkey map "q" 'org-goto-quit)
  4040. (org-defkey map "n" 'outline-next-visible-heading)
  4041. (org-defkey map "p" 'outline-previous-visible-heading)
  4042. (org-defkey map "f" 'outline-forward-same-level)
  4043. (org-defkey map "b" 'outline-backward-same-level)
  4044. (org-defkey map "u" 'outline-up-heading))
  4045. (org-defkey map "/" 'org-occur)
  4046. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4047. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4048. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4049. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4050. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4051. map))
  4052. (defconst org-goto-help
  4053. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4054. RET=jump to location [Q]uit and return to previous location
  4055. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4056. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4057. ;; FIXME: Docstring doe not mention both interfaces
  4058. (defun org-goto (&optional alternative-interface)
  4059. "Look up a different location in the current file, keeping current visibility.
  4060. When you want look-up or go to a different location in a document, the
  4061. fastest way is often to fold the entire buffer and then dive into the tree.
  4062. This method has the disadvantage, that the previous location will be folded,
  4063. which may not be what you want.
  4064. This command works around this by showing a copy of the current buffer
  4065. in an indirect buffer, in overview mode. You can dive into the tree in
  4066. that copy, use org-occur and incremental search to find a location.
  4067. When pressing RET or `Q', the command returns to the original buffer in
  4068. which the visibility is still unchanged. After RET is will also jump to
  4069. the location selected in the indirect buffer and expose the
  4070. the headline hierarchy above."
  4071. (interactive "P")
  4072. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4073. (org-refile-use-outline-path t)
  4074. (interface
  4075. (if (not alternative-interface)
  4076. org-goto-interface
  4077. (if (eq org-goto-interface 'outline)
  4078. 'outline-path-completion
  4079. 'outline)))
  4080. (org-goto-start-pos (point))
  4081. (selected-point
  4082. (if (eq interface 'outline)
  4083. (car (org-get-location (current-buffer) org-goto-help))
  4084. (nth 3 (org-refile-get-location "Goto: ")))))
  4085. (if selected-point
  4086. (progn
  4087. (org-mark-ring-push org-goto-start-pos)
  4088. (goto-char selected-point)
  4089. (if (or (org-invisible-p) (org-invisible-p2))
  4090. (org-show-context 'org-goto)))
  4091. (message "Quit"))))
  4092. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4093. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4094. (defvar org-goto-local-auto-isearch-map) ; defined below
  4095. (defun org-get-location (buf help)
  4096. "Let the user select a location in the Org-mode buffer BUF.
  4097. This function uses a recursive edit. It returns the selected position
  4098. or nil."
  4099. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4100. (isearch-hide-immediately nil)
  4101. (isearch-search-fun-function
  4102. (lambda () 'org-goto-local-search-headings))
  4103. (org-goto-selected-point org-goto-exit-command))
  4104. (save-excursion
  4105. (save-window-excursion
  4106. (delete-other-windows)
  4107. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4108. (switch-to-buffer
  4109. (condition-case nil
  4110. (make-indirect-buffer (current-buffer) "*org-goto*")
  4111. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4112. (with-output-to-temp-buffer "*Help*"
  4113. (princ help))
  4114. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4115. (setq buffer-read-only nil)
  4116. (let ((org-startup-truncated t)
  4117. (org-startup-folded nil)
  4118. (org-startup-align-all-tables nil))
  4119. (org-mode)
  4120. (org-overview))
  4121. (setq buffer-read-only t)
  4122. (if (and (boundp 'org-goto-start-pos)
  4123. (integer-or-marker-p org-goto-start-pos))
  4124. (let ((org-show-hierarchy-above t)
  4125. (org-show-siblings t)
  4126. (org-show-following-heading t))
  4127. (goto-char org-goto-start-pos)
  4128. (and (org-invisible-p) (org-show-context)))
  4129. (goto-char (point-min)))
  4130. (org-beginning-of-line)
  4131. (message "Select location and press RET")
  4132. (use-local-map org-goto-map)
  4133. (recursive-edit)
  4134. ))
  4135. (kill-buffer "*org-goto*")
  4136. (cons org-goto-selected-point org-goto-exit-command)))
  4137. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4138. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4139. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4140. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4141. (defun org-goto-local-search-headings (string bound noerror)
  4142. "Search and make sure that any matches are in headlines."
  4143. (catch 'return
  4144. (while (if isearch-forward
  4145. (search-forward string bound noerror)
  4146. (search-backward string bound noerror))
  4147. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4148. (and (member :headline context)
  4149. (not (member :tags context))))
  4150. (throw 'return (point))))))
  4151. (defun org-goto-local-auto-isearch ()
  4152. "Start isearch."
  4153. (interactive)
  4154. (goto-char (point-min))
  4155. (let ((keys (this-command-keys)))
  4156. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4157. (isearch-mode t)
  4158. (isearch-process-search-char (string-to-char keys)))))
  4159. (defun org-goto-ret (&optional arg)
  4160. "Finish `org-goto' by going to the new location."
  4161. (interactive "P")
  4162. (setq org-goto-selected-point (point)
  4163. org-goto-exit-command 'return)
  4164. (throw 'exit nil))
  4165. (defun org-goto-left ()
  4166. "Finish `org-goto' by going to the new location."
  4167. (interactive)
  4168. (if (org-on-heading-p)
  4169. (progn
  4170. (beginning-of-line 1)
  4171. (setq org-goto-selected-point (point)
  4172. org-goto-exit-command 'left)
  4173. (throw 'exit nil))
  4174. (error "Not on a heading")))
  4175. (defun org-goto-right ()
  4176. "Finish `org-goto' by going to the new location."
  4177. (interactive)
  4178. (if (org-on-heading-p)
  4179. (progn
  4180. (setq org-goto-selected-point (point)
  4181. org-goto-exit-command 'right)
  4182. (throw 'exit nil))
  4183. (error "Not on a heading")))
  4184. (defun org-goto-quit ()
  4185. "Finish `org-goto' without cursor motion."
  4186. (interactive)
  4187. (setq org-goto-selected-point nil)
  4188. (setq org-goto-exit-command 'quit)
  4189. (throw 'exit nil))
  4190. ;;; Indirect buffer display of subtrees
  4191. (defvar org-indirect-dedicated-frame nil
  4192. "This is the frame being used for indirect tree display.")
  4193. (defvar org-last-indirect-buffer nil)
  4194. (defun org-tree-to-indirect-buffer (&optional arg)
  4195. "Create indirect buffer and narrow it to current subtree.
  4196. With numerical prefix ARG, go up to this level and then take that tree.
  4197. If ARG is negative, go up that many levels.
  4198. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4199. indirect buffer previously made with this command, to avoid proliferation of
  4200. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4201. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4202. is kept so that you can work with several indirect buffers at the same time.
  4203. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4204. requests that a new frame be made for the new buffer, so that the dedicated
  4205. frame is not changed."
  4206. (interactive "P")
  4207. (let ((cbuf (current-buffer))
  4208. (cwin (selected-window))
  4209. (pos (point))
  4210. beg end level heading ibuf)
  4211. (save-excursion
  4212. (org-back-to-heading t)
  4213. (when (numberp arg)
  4214. (setq level (org-outline-level))
  4215. (if (< arg 0) (setq arg (+ level arg)))
  4216. (while (> (setq level (org-outline-level)) arg)
  4217. (outline-up-heading 1 t)))
  4218. (setq beg (point)
  4219. heading (org-get-heading))
  4220. (org-end-of-subtree t) (setq end (point)))
  4221. (if (and (buffer-live-p org-last-indirect-buffer)
  4222. (not (eq org-indirect-buffer-display 'new-frame))
  4223. (not arg))
  4224. (kill-buffer org-last-indirect-buffer))
  4225. (setq ibuf (org-get-indirect-buffer cbuf)
  4226. org-last-indirect-buffer ibuf)
  4227. (cond
  4228. ((or (eq org-indirect-buffer-display 'new-frame)
  4229. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4230. (select-frame (make-frame))
  4231. (delete-other-windows)
  4232. (switch-to-buffer ibuf)
  4233. (org-set-frame-title heading))
  4234. ((eq org-indirect-buffer-display 'dedicated-frame)
  4235. (raise-frame
  4236. (select-frame (or (and org-indirect-dedicated-frame
  4237. (frame-live-p org-indirect-dedicated-frame)
  4238. org-indirect-dedicated-frame)
  4239. (setq org-indirect-dedicated-frame (make-frame)))))
  4240. (delete-other-windows)
  4241. (switch-to-buffer ibuf)
  4242. (org-set-frame-title (concat "Indirect: " heading)))
  4243. ((eq org-indirect-buffer-display 'current-window)
  4244. (switch-to-buffer ibuf))
  4245. ((eq org-indirect-buffer-display 'other-window)
  4246. (pop-to-buffer ibuf))
  4247. (t (error "Invalid value.")))
  4248. (if (featurep 'xemacs)
  4249. (save-excursion (org-mode) (turn-on-font-lock)))
  4250. (narrow-to-region beg end)
  4251. (show-all)
  4252. (goto-char pos)
  4253. (and (window-live-p cwin) (select-window cwin))))
  4254. (defun org-get-indirect-buffer (&optional buffer)
  4255. (setq buffer (or buffer (current-buffer)))
  4256. (let ((n 1) (base (buffer-name buffer)) bname)
  4257. (while (buffer-live-p
  4258. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4259. (setq n (1+ n)))
  4260. (condition-case nil
  4261. (make-indirect-buffer buffer bname 'clone)
  4262. (error (make-indirect-buffer buffer bname)))))
  4263. (defun org-set-frame-title (title)
  4264. "Set the title of the current frame to the string TITLE."
  4265. ;; FIXME: how to name a single frame in XEmacs???
  4266. (unless (featurep 'xemacs)
  4267. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4268. ;;;; Structure editing
  4269. ;;; Inserting headlines
  4270. (defun org-insert-heading (&optional force-heading)
  4271. "Insert a new heading or item with same depth at point.
  4272. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4273. If point is at the beginning of a headline, insert a sibling before the
  4274. current headline. If point is not at the beginning, do not split the line,
  4275. but create the new headline after the current line."
  4276. (interactive "P")
  4277. (if (= (buffer-size) 0)
  4278. (insert "\n* ")
  4279. (when (or force-heading (not (org-insert-item)))
  4280. (let* ((head (save-excursion
  4281. (condition-case nil
  4282. (progn
  4283. (org-back-to-heading)
  4284. (match-string 0))
  4285. (error "*"))))
  4286. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4287. pos hide-previous previous-pos)
  4288. (cond
  4289. ((and (org-on-heading-p) (bolp)
  4290. (or (bobp)
  4291. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4292. ;; insert before the current line
  4293. (open-line (if blank 2 1)))
  4294. ((and (bolp)
  4295. (or (bobp)
  4296. (save-excursion
  4297. (backward-char 1) (not (org-invisible-p)))))
  4298. ;; insert right here
  4299. nil)
  4300. (t
  4301. ;; somewhere in the line
  4302. (save-excursion
  4303. (setq previous-pos (point-at-bol))
  4304. (end-of-line)
  4305. (setq hide-previous (org-invisible-p)))
  4306. (and org-insert-heading-respect-content (org-show-subtree))
  4307. (let ((split
  4308. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4309. (save-excursion
  4310. (let ((p (point)))
  4311. (goto-char (point-at-bol))
  4312. (and (looking-at org-complex-heading-regexp)
  4313. (> p (match-beginning 4)))))))
  4314. tags pos)
  4315. (cond
  4316. (org-insert-heading-respect-content
  4317. (org-end-of-subtree nil t)
  4318. (or (bolp) (newline))
  4319. (open-line 1))
  4320. ((org-on-heading-p)
  4321. (when hide-previous
  4322. (show-children)
  4323. (org-show-entry))
  4324. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4325. (setq tags (and (match-end 2) (match-string 2)))
  4326. (and (match-end 1)
  4327. (delete-region (match-beginning 1) (match-end 1)))
  4328. (setq pos (point-at-bol))
  4329. (or split (end-of-line 1))
  4330. (delete-horizontal-space)
  4331. (newline (if blank 2 1))
  4332. (when tags
  4333. (save-excursion
  4334. (goto-char pos)
  4335. (end-of-line 1)
  4336. (insert " " tags)
  4337. (org-set-tags nil 'align))))
  4338. (t
  4339. (or split (end-of-line 1))
  4340. (newline (if blank 2 1)))))))
  4341. (insert head) (just-one-space)
  4342. (setq pos (point))
  4343. (end-of-line 1)
  4344. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4345. (when (and org-insert-heading-respect-content hide-previous)
  4346. (save-excursion
  4347. (goto-char previous-pos)
  4348. (hide-subtree)))
  4349. (run-hooks 'org-insert-heading-hook)))))
  4350. (defun org-get-heading (&optional no-tags)
  4351. "Return the heading of the current entry, without the stars."
  4352. (save-excursion
  4353. (org-back-to-heading t)
  4354. (if (looking-at
  4355. (if no-tags
  4356. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4357. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4358. (match-string 1) "")))
  4359. (defun org-insert-heading-after-current ()
  4360. "Insert a new heading with same level as current, after current subtree."
  4361. (interactive)
  4362. (org-back-to-heading)
  4363. (org-insert-heading)
  4364. (org-move-subtree-down)
  4365. (end-of-line 1))
  4366. (defun org-insert-heading-respect-content ()
  4367. (interactive)
  4368. (let ((org-insert-heading-respect-content t))
  4369. (org-insert-heading t)))
  4370. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4371. (interactive "P")
  4372. (let ((org-insert-heading-respect-content t))
  4373. (org-insert-todo-heading force-state t)))
  4374. (defun org-insert-todo-heading (arg &optional force-heading)
  4375. "Insert a new heading with the same level and TODO state as current heading.
  4376. If the heading has no TODO state, or if the state is DONE, use the first
  4377. state (TODO by default). Also with prefix arg, force first state."
  4378. (interactive "P")
  4379. (when (or force-heading (not (org-insert-item 'checkbox)))
  4380. (org-insert-heading force-heading)
  4381. (save-excursion
  4382. (org-back-to-heading)
  4383. (outline-previous-heading)
  4384. (looking-at org-todo-line-regexp))
  4385. (if (or arg
  4386. (not (match-beginning 2))
  4387. (member (match-string 2) org-done-keywords))
  4388. (insert (car org-todo-keywords-1) " ")
  4389. (insert (match-string 2) " "))
  4390. (when org-provide-todo-statistics
  4391. (org-update-parent-todo-statistics))))
  4392. (defun org-insert-subheading (arg)
  4393. "Insert a new subheading and demote it.
  4394. Works for outline headings and for plain lists alike."
  4395. (interactive "P")
  4396. (org-insert-heading arg)
  4397. (cond
  4398. ((org-on-heading-p) (org-do-demote))
  4399. ((org-at-item-p) (org-indent-item 1))))
  4400. (defun org-insert-todo-subheading (arg)
  4401. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4402. Works for outline headings and for plain lists alike."
  4403. (interactive "P")
  4404. (org-insert-todo-heading arg)
  4405. (cond
  4406. ((org-on-heading-p) (org-do-demote))
  4407. ((org-at-item-p) (org-indent-item 1))))
  4408. ;;; Promotion and Demotion
  4409. (defun org-promote-subtree ()
  4410. "Promote the entire subtree.
  4411. See also `org-promote'."
  4412. (interactive)
  4413. (save-excursion
  4414. (org-map-tree 'org-promote))
  4415. (org-fix-position-after-promote))
  4416. (defun org-demote-subtree ()
  4417. "Demote the entire subtree. See `org-demote'.
  4418. See also `org-promote'."
  4419. (interactive)
  4420. (save-excursion
  4421. (org-map-tree 'org-demote))
  4422. (org-fix-position-after-promote))
  4423. (defun org-do-promote ()
  4424. "Promote the current heading higher up the tree.
  4425. If the region is active in `transient-mark-mode', promote all headings
  4426. in the region."
  4427. (interactive)
  4428. (save-excursion
  4429. (if (org-region-active-p)
  4430. (org-map-region 'org-promote (region-beginning) (region-end))
  4431. (org-promote)))
  4432. (org-fix-position-after-promote))
  4433. (defun org-do-demote ()
  4434. "Demote the current heading lower down the tree.
  4435. If the region is active in `transient-mark-mode', demote all headings
  4436. in the region."
  4437. (interactive)
  4438. (save-excursion
  4439. (if (org-region-active-p)
  4440. (org-map-region 'org-demote (region-beginning) (region-end))
  4441. (org-demote)))
  4442. (org-fix-position-after-promote))
  4443. (defun org-fix-position-after-promote ()
  4444. "Make sure that after pro/demotion cursor position is right."
  4445. (let ((pos (point)))
  4446. (when (save-excursion
  4447. (beginning-of-line 1)
  4448. (looking-at org-todo-line-regexp)
  4449. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4450. (cond ((eobp) (insert " "))
  4451. ((eolp) (insert " "))
  4452. ((equal (char-after) ?\ ) (forward-char 1))))))
  4453. (defun org-reduced-level (l)
  4454. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4455. (defun org-get-valid-level (level &optional change)
  4456. "Rectify a level change under the influence of `org-odd-levels-only'
  4457. LEVEL is a current level, CHANGE is by how much the level should be
  4458. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4459. even level numbers will become the next higher odd number."
  4460. (if org-odd-levels-only
  4461. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4462. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4463. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4464. (max 1 (+ level change))))
  4465. (if (boundp 'define-obsolete-function-alias)
  4466. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4467. (define-obsolete-function-alias 'org-get-legal-level
  4468. 'org-get-valid-level)
  4469. (define-obsolete-function-alias 'org-get-legal-level
  4470. 'org-get-valid-level "23.1")))
  4471. (defun org-promote ()
  4472. "Promote the current heading higher up the tree.
  4473. If the region is active in `transient-mark-mode', promote all headings
  4474. in the region."
  4475. (org-back-to-heading t)
  4476. (let* ((level (save-match-data (funcall outline-level)))
  4477. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4478. (diff (abs (- level (length up-head) -1))))
  4479. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4480. (replace-match up-head nil t)
  4481. ;; Fixup tag positioning
  4482. (and org-auto-align-tags (org-set-tags nil t))
  4483. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4484. (defun org-demote ()
  4485. "Demote the current heading lower down the tree.
  4486. If the region is active in `transient-mark-mode', demote all headings
  4487. in the region."
  4488. (org-back-to-heading t)
  4489. (let* ((level (save-match-data (funcall outline-level)))
  4490. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4491. (diff (abs (- level (length down-head) -1))))
  4492. (replace-match down-head nil t)
  4493. ;; Fixup tag positioning
  4494. (and org-auto-align-tags (org-set-tags nil t))
  4495. (if org-adapt-indentation (org-fixup-indentation diff))))
  4496. (defun org-map-tree (fun)
  4497. "Call FUN for every heading underneath the current one."
  4498. (org-back-to-heading)
  4499. (let ((level (funcall outline-level)))
  4500. (save-excursion
  4501. (funcall fun)
  4502. (while (and (progn
  4503. (outline-next-heading)
  4504. (> (funcall outline-level) level))
  4505. (not (eobp)))
  4506. (funcall fun)))))
  4507. (defun org-map-region (fun beg end)
  4508. "Call FUN for every heading between BEG and END."
  4509. (let ((org-ignore-region t))
  4510. (save-excursion
  4511. (setq end (copy-marker end))
  4512. (goto-char beg)
  4513. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4514. (< (point) end))
  4515. (funcall fun))
  4516. (while (and (progn
  4517. (outline-next-heading)
  4518. (< (point) end))
  4519. (not (eobp)))
  4520. (funcall fun)))))
  4521. (defun org-fixup-indentation (diff)
  4522. "Change the indentation in the current entry by DIFF
  4523. However, if any line in the current entry has no indentation, or if it
  4524. would end up with no indentation after the change, nothing at all is done."
  4525. (save-excursion
  4526. (let ((end (save-excursion (outline-next-heading)
  4527. (point-marker)))
  4528. (prohibit (if (> diff 0)
  4529. "^\\S-"
  4530. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4531. col)
  4532. (unless (save-excursion (end-of-line 1)
  4533. (re-search-forward prohibit end t))
  4534. (while (and (< (point) end)
  4535. (re-search-forward "^[ \t]+" end t))
  4536. (goto-char (match-end 0))
  4537. (setq col (current-column))
  4538. (if (< diff 0) (replace-match ""))
  4539. (org-indent-to-column (+ diff col))))
  4540. (move-marker end nil))))
  4541. (defun org-convert-to-odd-levels ()
  4542. "Convert an org-mode file with all levels allowed to one with odd levels.
  4543. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4544. level 5 etc."
  4545. (interactive)
  4546. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4547. (let ((org-odd-levels-only nil) n)
  4548. (save-excursion
  4549. (goto-char (point-min))
  4550. (while (re-search-forward "^\\*\\*+ " nil t)
  4551. (setq n (- (length (match-string 0)) 2))
  4552. (while (>= (setq n (1- n)) 0)
  4553. (org-demote))
  4554. (end-of-line 1))))))
  4555. (defun org-convert-to-oddeven-levels ()
  4556. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4557. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4558. section with an even level, conversion would destroy the structure of the file. An error
  4559. is signaled in this case."
  4560. (interactive)
  4561. (goto-char (point-min))
  4562. ;; First check if there are no even levels
  4563. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4564. (org-show-context t)
  4565. (error "Not all levels are odd in this file. Conversion not possible."))
  4566. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4567. (let ((org-odd-levels-only nil) n)
  4568. (save-excursion
  4569. (goto-char (point-min))
  4570. (while (re-search-forward "^\\*\\*+ " nil t)
  4571. (setq n (/ (1- (length (match-string 0))) 2))
  4572. (while (>= (setq n (1- n)) 0)
  4573. (org-promote))
  4574. (end-of-line 1))))))
  4575. (defun org-tr-level (n)
  4576. "Make N odd if required."
  4577. (if org-odd-levels-only (1+ (/ n 2)) n))
  4578. ;;; Vertical tree motion, cutting and pasting of subtrees
  4579. (defun org-move-subtree-up (&optional arg)
  4580. "Move the current subtree up past ARG headlines of the same level."
  4581. (interactive "p")
  4582. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4583. (defun org-move-subtree-down (&optional arg)
  4584. "Move the current subtree down past ARG headlines of the same level."
  4585. (interactive "p")
  4586. (setq arg (prefix-numeric-value arg))
  4587. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4588. 'outline-get-last-sibling))
  4589. (ins-point (make-marker))
  4590. (cnt (abs arg))
  4591. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4592. ;; Select the tree
  4593. (org-back-to-heading)
  4594. (setq beg0 (point))
  4595. (save-excursion
  4596. (setq ne-beg (org-back-over-empty-lines))
  4597. (setq beg (point)))
  4598. (save-match-data
  4599. (save-excursion (outline-end-of-heading)
  4600. (setq folded (org-invisible-p)))
  4601. (outline-end-of-subtree))
  4602. (outline-next-heading)
  4603. (setq ne-end (org-back-over-empty-lines))
  4604. (setq end (point))
  4605. (goto-char beg0)
  4606. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4607. ;; include less whitespace
  4608. (save-excursion
  4609. (goto-char beg)
  4610. (forward-line (- ne-beg ne-end))
  4611. (setq beg (point))))
  4612. ;; Find insertion point, with error handling
  4613. (while (> cnt 0)
  4614. (or (and (funcall movfunc) (looking-at outline-regexp))
  4615. (progn (goto-char beg0)
  4616. (error "Cannot move past superior level or buffer limit")))
  4617. (setq cnt (1- cnt)))
  4618. (if (> arg 0)
  4619. ;; Moving forward - still need to move over subtree
  4620. (progn (org-end-of-subtree t t)
  4621. (save-excursion
  4622. (org-back-over-empty-lines)
  4623. (or (bolp) (newline)))))
  4624. (setq ne-ins (org-back-over-empty-lines))
  4625. (move-marker ins-point (point))
  4626. (setq txt (buffer-substring beg end))
  4627. (org-save-markers-in-region beg end)
  4628. (delete-region beg end)
  4629. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4630. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4631. (let ((bbb (point)))
  4632. (insert-before-markers txt)
  4633. (org-reinstall-markers-in-region bbb)
  4634. (move-marker ins-point bbb))
  4635. (or (bolp) (insert "\n"))
  4636. (setq ins-end (point))
  4637. (goto-char ins-point)
  4638. (org-skip-whitespace)
  4639. (when (and (< arg 0)
  4640. (org-first-sibling-p)
  4641. (> ne-ins ne-beg))
  4642. ;; Move whitespace back to beginning
  4643. (save-excursion
  4644. (goto-char ins-end)
  4645. (let ((kill-whole-line t))
  4646. (kill-line (- ne-ins ne-beg)) (point)))
  4647. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4648. (move-marker ins-point nil)
  4649. (org-compact-display-after-subtree-move)
  4650. (org-show-empty-lines-in-parent)
  4651. (unless folded
  4652. (org-show-entry)
  4653. (show-children)
  4654. (org-cycle-hide-drawers 'children))))
  4655. (defvar org-subtree-clip ""
  4656. "Clipboard for cut and paste of subtrees.
  4657. This is actually only a copy of the kill, because we use the normal kill
  4658. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4659. (defvar org-subtree-clip-folded nil
  4660. "Was the last copied subtree folded?
  4661. This is used to fold the tree back after pasting.")
  4662. (defun org-cut-subtree (&optional n)
  4663. "Cut the current subtree into the clipboard.
  4664. With prefix arg N, cut this many sequential subtrees.
  4665. This is a short-hand for marking the subtree and then cutting it."
  4666. (interactive "p")
  4667. (org-copy-subtree n 'cut))
  4668. (defun org-copy-subtree (&optional n cut force-store-markers)
  4669. "Cut the current subtree into the clipboard.
  4670. With prefix arg N, cut this many sequential subtrees.
  4671. This is a short-hand for marking the subtree and then copying it.
  4672. If CUT is non-nil, actually cut the subtree.
  4673. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4674. of some markers in the region, even if CUT is non-nil. This is
  4675. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4676. (interactive "p")
  4677. (let (beg end folded (beg0 (point)))
  4678. (if (interactive-p)
  4679. (org-back-to-heading nil) ; take what looks like a subtree
  4680. (org-back-to-heading t)) ; take what is really there
  4681. (org-back-over-empty-lines)
  4682. (setq beg (point))
  4683. (skip-chars-forward " \t\r\n")
  4684. (save-match-data
  4685. (save-excursion (outline-end-of-heading)
  4686. (setq folded (org-invisible-p)))
  4687. (condition-case nil
  4688. (outline-forward-same-level (1- n))
  4689. (error nil))
  4690. (org-end-of-subtree t t))
  4691. (org-back-over-empty-lines)
  4692. (setq end (point))
  4693. (goto-char beg0)
  4694. (when (> end beg)
  4695. (setq org-subtree-clip-folded folded)
  4696. (when (or cut force-store-markers)
  4697. (org-save-markers-in-region beg end))
  4698. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4699. (setq org-subtree-clip (current-kill 0))
  4700. (message "%s: Subtree(s) with %d characters"
  4701. (if cut "Cut" "Copied")
  4702. (length org-subtree-clip)))))
  4703. (defun org-paste-subtree (&optional level tree for-yank)
  4704. "Paste the clipboard as a subtree, with modification of headline level.
  4705. The entire subtree is promoted or demoted in order to match a new headline
  4706. level.
  4707. If the cursor is at the beginning of a headline, the same level as
  4708. that headline is used to paste the tree
  4709. If not, the new level is derived from the *visible* headings
  4710. before and after the insertion point, and taken to be the inferior headline
  4711. level of the two. So if the previous visible heading is level 3 and the
  4712. next is level 4 (or vice versa), level 4 will be used for insertion.
  4713. This makes sure that the subtree remains an independent subtree and does
  4714. not swallow low level entries.
  4715. You can also force a different level, either by using a numeric prefix
  4716. argument, or by inserting the heading marker by hand. For example, if the
  4717. cursor is after \"*****\", then the tree will be shifted to level 5.
  4718. If optional TREE is given, use this text instead of the kill ring.
  4719. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4720. move back over whitespace before inserting, and move point to the end of
  4721. the inserted text when done."
  4722. (interactive "P")
  4723. (unless (org-kill-is-subtree-p tree)
  4724. (error "%s"
  4725. (substitute-command-keys
  4726. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4727. (let* ((visp (not (org-invisible-p)))
  4728. (txt (or tree (and kill-ring (current-kill 0))))
  4729. (^re (concat "^\\(" outline-regexp "\\)"))
  4730. (re (concat "\\(" outline-regexp "\\)"))
  4731. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4732. (old-level (if (string-match ^re txt)
  4733. (- (match-end 0) (match-beginning 0) 1)
  4734. -1))
  4735. (force-level (cond (level (prefix-numeric-value level))
  4736. ((and (looking-at "[ \t]*$")
  4737. (string-match
  4738. ^re_ (buffer-substring
  4739. (point-at-bol) (point))))
  4740. (- (match-end 1) (match-beginning 1)))
  4741. ((and (bolp)
  4742. (looking-at org-outline-regexp))
  4743. (- (match-end 0) (point) 1))
  4744. (t nil)))
  4745. (previous-level (save-excursion
  4746. (condition-case nil
  4747. (progn
  4748. (outline-previous-visible-heading 1)
  4749. (if (looking-at re)
  4750. (- (match-end 0) (match-beginning 0) 1)
  4751. 1))
  4752. (error 1))))
  4753. (next-level (save-excursion
  4754. (condition-case nil
  4755. (progn
  4756. (or (looking-at outline-regexp)
  4757. (outline-next-visible-heading 1))
  4758. (if (looking-at re)
  4759. (- (match-end 0) (match-beginning 0) 1)
  4760. 1))
  4761. (error 1))))
  4762. (new-level (or force-level (max previous-level next-level)))
  4763. (shift (if (or (= old-level -1)
  4764. (= new-level -1)
  4765. (= old-level new-level))
  4766. 0
  4767. (- new-level old-level)))
  4768. (delta (if (> shift 0) -1 1))
  4769. (func (if (> shift 0) 'org-demote 'org-promote))
  4770. (org-odd-levels-only nil)
  4771. beg end newend)
  4772. ;; Remove the forced level indicator
  4773. (if force-level
  4774. (delete-region (point-at-bol) (point)))
  4775. ;; Paste
  4776. (beginning-of-line 1)
  4777. (unless for-yank (org-back-over-empty-lines))
  4778. (setq beg (point))
  4779. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4780. (insert-before-markers txt)
  4781. (unless (string-match "\n\\'" txt) (insert "\n"))
  4782. (setq newend (point))
  4783. (org-reinstall-markers-in-region beg)
  4784. (setq end (point))
  4785. (goto-char beg)
  4786. (skip-chars-forward " \t\n\r")
  4787. (setq beg (point))
  4788. (if (and (org-invisible-p) visp)
  4789. (save-excursion (outline-show-heading)))
  4790. ;; Shift if necessary
  4791. (unless (= shift 0)
  4792. (save-restriction
  4793. (narrow-to-region beg end)
  4794. (while (not (= shift 0))
  4795. (org-map-region func (point-min) (point-max))
  4796. (setq shift (+ delta shift)))
  4797. (goto-char (point-min))
  4798. (setq newend (point-max))))
  4799. (when (or (interactive-p) for-yank)
  4800. (message "Clipboard pasted as level %d subtree" new-level))
  4801. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4802. kill-ring
  4803. (eq org-subtree-clip (current-kill 0))
  4804. org-subtree-clip-folded)
  4805. ;; The tree was folded before it was killed/copied
  4806. (hide-subtree))
  4807. (and for-yank (goto-char newend))))
  4808. (defun org-kill-is-subtree-p (&optional txt)
  4809. "Check if the current kill is an outline subtree, or a set of trees.
  4810. Returns nil if kill does not start with a headline, or if the first
  4811. headline level is not the largest headline level in the tree.
  4812. So this will actually accept several entries of equal levels as well,
  4813. which is OK for `org-paste-subtree'.
  4814. If optional TXT is given, check this string instead of the current kill."
  4815. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4816. (start-level (and kill
  4817. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4818. org-outline-regexp "\\)")
  4819. kill)
  4820. (- (match-end 2) (match-beginning 2) 1)))
  4821. (re (concat "^" org-outline-regexp))
  4822. (start (1+ (or (match-beginning 2) -1))))
  4823. (if (not start-level)
  4824. (progn
  4825. nil) ;; does not even start with a heading
  4826. (catch 'exit
  4827. (while (setq start (string-match re kill (1+ start)))
  4828. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4829. (throw 'exit nil)))
  4830. t))))
  4831. (defvar org-markers-to-move nil
  4832. "Markers that should be moved with a cut-and-paste operation.
  4833. Those markers are stored together with their positions relative to
  4834. the start of the region.")
  4835. (defun org-save-markers-in-region (beg end)
  4836. "Check markers in region.
  4837. If these markers are between BEG and END, record their position relative
  4838. to BEG, so that after moving the block of text, we can put the markers back
  4839. into place.
  4840. This function gets called just before an entry or tree gets cut from the
  4841. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4842. called immediately, to move the markers with the entries."
  4843. (setq org-markers-to-move nil)
  4844. (when (featurep 'org-clock)
  4845. (org-clock-save-markers-for-cut-and-paste beg end))
  4846. (when (featurep 'org-agenda)
  4847. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4848. (defun org-check-and-save-marker (marker beg end)
  4849. "Check if MARKER is between BEG and END.
  4850. If yes, remember the marker and the distance to BEG."
  4851. (when (and (marker-buffer marker)
  4852. (equal (marker-buffer marker) (current-buffer)))
  4853. (if (and (>= marker beg) (< marker end))
  4854. (push (cons marker (- marker beg)) org-markers-to-move))))
  4855. (defun org-reinstall-markers-in-region (beg)
  4856. "Move all remembered markers to their position relative to BEG."
  4857. (mapc (lambda (x)
  4858. (move-marker (car x) (+ beg (cdr x))))
  4859. org-markers-to-move)
  4860. (setq org-markers-to-move nil))
  4861. (defun org-narrow-to-subtree ()
  4862. "Narrow buffer to the current subtree."
  4863. (interactive)
  4864. (save-excursion
  4865. (save-match-data
  4866. (narrow-to-region
  4867. (progn (org-back-to-heading) (point))
  4868. (progn (org-end-of-subtree t) (point))))))
  4869. ;;; Outline Sorting
  4870. (defun org-sort (with-case)
  4871. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4872. Optional argument WITH-CASE means sort case-sensitively."
  4873. (interactive "P")
  4874. (if (org-at-table-p)
  4875. (org-call-with-arg 'org-table-sort-lines with-case)
  4876. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4877. (defun org-sort-remove-invisible (s)
  4878. (remove-text-properties 0 (length s) org-rm-props s)
  4879. (while (string-match org-bracket-link-regexp s)
  4880. (setq s (replace-match (if (match-end 2)
  4881. (match-string 3 s)
  4882. (match-string 1 s)) t t s)))
  4883. s)
  4884. (defvar org-priority-regexp) ; defined later in the file
  4885. (defun org-sort-entries-or-items
  4886. (&optional with-case sorting-type getkey-func compare-func property)
  4887. "Sort entries on a certain level of an outline tree.
  4888. If there is an active region, the entries in the region are sorted.
  4889. Else, if the cursor is before the first entry, sort the top-level items.
  4890. Else, the children of the entry at point are sorted.
  4891. Sorting can be alphabetically, numerically, and by date/time as given by
  4892. the first time stamp in the entry. The command prompts for the sorting
  4893. type unless it has been given to the function through the SORTING-TYPE
  4894. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4895. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4896. called with point at the beginning of the record. It must return either
  4897. a string or a number that should serve as the sorting key for that record.
  4898. Comparing entries ignores case by default. However, with an optional argument
  4899. WITH-CASE, the sorting considers case as well."
  4900. (interactive "P")
  4901. (let ((case-func (if with-case 'identity 'downcase))
  4902. start beg end stars re re2
  4903. txt what tmp plain-list-p)
  4904. ;; Find beginning and end of region to sort
  4905. (cond
  4906. ((org-region-active-p)
  4907. ;; we will sort the region
  4908. (setq end (region-end)
  4909. what "region")
  4910. (goto-char (region-beginning))
  4911. (if (not (org-on-heading-p)) (outline-next-heading))
  4912. (setq start (point)))
  4913. ((org-at-item-p)
  4914. ;; we will sort this plain list
  4915. (org-beginning-of-item-list) (setq start (point))
  4916. (org-end-of-item-list) (setq end (point))
  4917. (goto-char start)
  4918. (setq plain-list-p t
  4919. what "plain list"))
  4920. ((or (org-on-heading-p)
  4921. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4922. ;; we will sort the children of the current headline
  4923. (org-back-to-heading)
  4924. (setq start (point)
  4925. end (progn (org-end-of-subtree t t)
  4926. (org-back-over-empty-lines)
  4927. (point))
  4928. what "children")
  4929. (goto-char start)
  4930. (show-subtree)
  4931. (outline-next-heading))
  4932. (t
  4933. ;; we will sort the top-level entries in this file
  4934. (goto-char (point-min))
  4935. (or (org-on-heading-p) (outline-next-heading))
  4936. (setq start (point) end (point-max) what "top-level")
  4937. (goto-char start)
  4938. (show-all)))
  4939. (setq beg (point))
  4940. (if (>= beg end) (error "Nothing to sort"))
  4941. (unless plain-list-p
  4942. (looking-at "\\(\\*+\\)")
  4943. (setq stars (match-string 1)
  4944. re (concat "^" (regexp-quote stars) " +")
  4945. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4946. txt (buffer-substring beg end))
  4947. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4948. (if (and (not (equal stars "*")) (string-match re2 txt))
  4949. (error "Region to sort contains a level above the first entry")))
  4950. (unless sorting-type
  4951. (message
  4952. (if plain-list-p
  4953. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4954. "Sort %s: [a]lpha [n]umeric [t]ime [p]riority p[r]operty todo[o]rder [f]unc A/N/T/P/O/F means reversed:")
  4955. what)
  4956. (setq sorting-type (read-char-exclusive))
  4957. (and (= (downcase sorting-type) ?f)
  4958. (setq getkey-func
  4959. (org-ido-completing-read "Sort using function: "
  4960. obarray 'fboundp t nil nil))
  4961. (setq getkey-func (intern getkey-func)))
  4962. (and (= (downcase sorting-type) ?r)
  4963. (setq property
  4964. (org-ido-completing-read "Property: "
  4965. (mapcar 'list (org-buffer-property-keys t))
  4966. nil t))))
  4967. (message "Sorting entries...")
  4968. (save-restriction
  4969. (narrow-to-region start end)
  4970. (let ((dcst (downcase sorting-type))
  4971. (now (current-time)))
  4972. (sort-subr
  4973. (/= dcst sorting-type)
  4974. ;; This function moves to the beginning character of the "record" to
  4975. ;; be sorted.
  4976. (if plain-list-p
  4977. (lambda nil
  4978. (if (org-at-item-p) t (goto-char (point-max))))
  4979. (lambda nil
  4980. (if (re-search-forward re nil t)
  4981. (goto-char (match-beginning 0))
  4982. (goto-char (point-max)))))
  4983. ;; This function moves to the last character of the "record" being
  4984. ;; sorted.
  4985. (if plain-list-p
  4986. 'org-end-of-item
  4987. (lambda nil
  4988. (save-match-data
  4989. (condition-case nil
  4990. (outline-forward-same-level 1)
  4991. (error
  4992. (goto-char (point-max)))))))
  4993. ;; This function returns the value that gets sorted against.
  4994. (if plain-list-p
  4995. (lambda nil
  4996. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4997. (cond
  4998. ((= dcst ?n)
  4999. (string-to-number (buffer-substring (match-end 0)
  5000. (point-at-eol))))
  5001. ((= dcst ?a)
  5002. (buffer-substring (match-end 0) (point-at-eol)))
  5003. ((= dcst ?t)
  5004. (if (re-search-forward org-ts-regexp
  5005. (point-at-eol) t)
  5006. (org-time-string-to-time (match-string 0))
  5007. now))
  5008. ((= dcst ?f)
  5009. (if getkey-func
  5010. (progn
  5011. (setq tmp (funcall getkey-func))
  5012. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5013. tmp)
  5014. (error "Invalid key function `%s'" getkey-func)))
  5015. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5016. (lambda nil
  5017. (cond
  5018. ((= dcst ?n)
  5019. (if (looking-at org-complex-heading-regexp)
  5020. (string-to-number (match-string 4))
  5021. nil))
  5022. ((= dcst ?a)
  5023. (if (looking-at org-complex-heading-regexp)
  5024. (funcall case-func (match-string 4))
  5025. nil))
  5026. ((= dcst ?t)
  5027. (if (re-search-forward org-ts-regexp
  5028. (save-excursion
  5029. (forward-line 2)
  5030. (point)) t)
  5031. (org-time-string-to-time (match-string 0))
  5032. now))
  5033. ((= dcst ?p)
  5034. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5035. (string-to-char (match-string 2))
  5036. org-default-priority))
  5037. ((= dcst ?r)
  5038. (or (org-entry-get nil property) ""))
  5039. ((= dcst ?o)
  5040. (if (looking-at org-complex-heading-regexp)
  5041. (- 9999 (length (member (match-string 2)
  5042. org-todo-keywords-1)))))
  5043. ((= dcst ?f)
  5044. (if getkey-func
  5045. (progn
  5046. (setq tmp (funcall getkey-func))
  5047. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5048. tmp)
  5049. (error "Invalid key function `%s'" getkey-func)))
  5050. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5051. nil
  5052. (cond
  5053. ((= dcst ?a) 'string<)
  5054. ((= dcst ?t) 'time-less-p)
  5055. ((= dcst ?f) compare-func)
  5056. (t nil)))))
  5057. (message "Sorting entries...done")))
  5058. (defun org-do-sort (table what &optional with-case sorting-type)
  5059. "Sort TABLE of WHAT according to SORTING-TYPE.
  5060. The user will be prompted for the SORTING-TYPE if the call to this
  5061. function does not specify it. WHAT is only for the prompt, to indicate
  5062. what is being sorted. The sorting key will be extracted from
  5063. the car of the elements of the table.
  5064. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5065. (unless sorting-type
  5066. (message
  5067. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5068. what)
  5069. (setq sorting-type (read-char-exclusive)))
  5070. (let ((dcst (downcase sorting-type))
  5071. extractfun comparefun)
  5072. ;; Define the appropriate functions
  5073. (cond
  5074. ((= dcst ?n)
  5075. (setq extractfun 'string-to-number
  5076. comparefun (if (= dcst sorting-type) '< '>)))
  5077. ((= dcst ?a)
  5078. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5079. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5080. comparefun (if (= dcst sorting-type)
  5081. 'string<
  5082. (lambda (a b) (and (not (string< a b))
  5083. (not (string= a b)))))))
  5084. ((= dcst ?t)
  5085. (setq extractfun
  5086. (lambda (x)
  5087. (if (string-match org-ts-regexp x)
  5088. (time-to-seconds
  5089. (org-time-string-to-time (match-string 0 x)))
  5090. 0))
  5091. comparefun (if (= dcst sorting-type) '< '>)))
  5092. (t (error "Invalid sorting type `%c'" sorting-type)))
  5093. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5094. table)
  5095. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5096. ;;; Editing source examples
  5097. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5098. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5099. (defvar org-edit-src-force-single-line nil)
  5100. (defvar org-edit-src-from-org-mode nil)
  5101. (defvar org-edit-src-picture nil)
  5102. (define-minor-mode org-exit-edit-mode
  5103. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5104. (defun org-edit-src-code ()
  5105. "Edit the source code example at point.
  5106. An indirect buffer is created, and that buffer is then narrowed to the
  5107. example at point and switched to the correct language mode. When done,
  5108. exit by killing the buffer with \\[org-edit-src-exit]."
  5109. (interactive)
  5110. (let ((line (org-current-line))
  5111. (case-fold-search t)
  5112. (msg (substitute-command-keys
  5113. "Edit, then exit with C-c ' (C-c and single quote)"))
  5114. (info (org-edit-src-find-region-and-lang))
  5115. (org-mode-p (eq major-mode 'org-mode))
  5116. beg end lang lang-f single)
  5117. (if (not info)
  5118. nil
  5119. (setq beg (nth 0 info)
  5120. end (nth 1 info)
  5121. lang (nth 2 info)
  5122. single (nth 3 info)
  5123. lang-f (intern (concat lang "-mode")))
  5124. (unless (functionp lang-f)
  5125. (error "No such language mode: %s" lang-f))
  5126. (goto-line line)
  5127. (if (get-buffer "*Org Edit Src Example*")
  5128. (kill-buffer "*Org Edit Src Example*"))
  5129. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5130. "*Org Edit Src Example*"))
  5131. (narrow-to-region beg end)
  5132. (remove-text-properties beg end '(display nil invisible nil
  5133. intangible nil))
  5134. (let ((org-inhibit-startup t))
  5135. (funcall lang-f))
  5136. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5137. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5138. (when org-mode-p
  5139. (goto-char (point-min))
  5140. (while (re-search-forward "^," nil t)
  5141. (replace-match "")))
  5142. (goto-line line)
  5143. (org-exit-edit-mode)
  5144. (org-set-local 'header-line-format msg)
  5145. (message "%s" msg)
  5146. t)))
  5147. (defun org-edit-fixed-width-region ()
  5148. "Edit the fixed-width ascii drawing at point.
  5149. This must be a region where each line starts with a colon followed by
  5150. a space character.
  5151. An indirect buffer is created, and that buffer is then narrowed to the
  5152. example at point and switched to artist-mode. When done,
  5153. exit by killing the buffer with \\[org-edit-src-exit]."
  5154. (interactive)
  5155. (let ((line (org-current-line))
  5156. (case-fold-search t)
  5157. (msg (substitute-command-keys
  5158. "Edit, then exit with C-c ' (C-c and single quote)"))
  5159. (org-mode-p (eq major-mode 'org-mode))
  5160. beg end lang lang-f)
  5161. (beginning-of-line 1)
  5162. (if (looking-at "[ \t]*[^:\n \t]")
  5163. nil
  5164. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5165. (setq beg (point) end beg)
  5166. (save-excursion
  5167. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5168. (setq beg (point-at-bol 2))
  5169. (setq beg (point))))
  5170. (save-excursion
  5171. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5172. (setq end (1- (match-beginning 0)))
  5173. (setq end (point))))
  5174. (goto-line line))
  5175. (if (get-buffer "*Org Edit Picture*")
  5176. (kill-buffer "*Org Edit Picture*"))
  5177. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5178. "*Org Edit Picture*"))
  5179. (narrow-to-region beg end)
  5180. (remove-text-properties beg end '(display nil invisible nil
  5181. intangible nil))
  5182. (when (fboundp 'font-lock-unfontify-region)
  5183. (font-lock-unfontify-region (point-min) (point-max)))
  5184. (cond
  5185. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5186. (fundamental-mode)
  5187. (artist-mode 1))
  5188. (t (funcall org-edit-fixed-width-region-mode)))
  5189. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5190. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5191. (set (make-local-variable 'org-edit-src-picture) t)
  5192. (goto-char (point-min))
  5193. (while (re-search-forward "^[ \t]*: ?" nil t)
  5194. (replace-match ""))
  5195. (goto-line line)
  5196. (org-exit-edit-mode)
  5197. (org-set-local 'header-line-format msg)
  5198. (message "%s" msg)
  5199. t)))
  5200. (defun org-edit-src-find-region-and-lang ()
  5201. "Find the region and language for a local edit.
  5202. Return a list with beginning and end of the region, a string representing
  5203. the language, a switch telling of the content should be in a single line."
  5204. (let ((re-list
  5205. (append
  5206. org-edit-src-region-extra
  5207. '(
  5208. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5209. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5210. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5211. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5212. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5213. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5214. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5215. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5216. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5217. ("^#\\+html:" "\n" "html" single-line)
  5218. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5219. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5220. ("^#\\+latex:" "\n" "latex" single-line)
  5221. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5222. ("^#\\+ascii:" "\n" "ascii" single-line)
  5223. )))
  5224. (pos (point))
  5225. re re1 re2 single beg end lang)
  5226. (catch 'exit
  5227. (while (setq entry (pop re-list))
  5228. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5229. single (nth 3 entry))
  5230. (save-excursion
  5231. (if (or (looking-at re1)
  5232. (re-search-backward re1 nil t))
  5233. (progn
  5234. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5235. (if (and (re-search-forward re2 nil t)
  5236. (>= (match-end 0) pos))
  5237. (throw 'exit (list beg (match-beginning 0) lang single))))
  5238. (if (or (looking-at re2)
  5239. (re-search-forward re2 nil t))
  5240. (progn
  5241. (setq end (match-beginning 0))
  5242. (if (and (re-search-backward re1 nil t)
  5243. (<= (match-beginning 0) pos))
  5244. (throw 'exit
  5245. (list (match-end 0) end
  5246. (org-edit-src-get-lang lang) single)))))))))))
  5247. (defun org-edit-src-get-lang (lang)
  5248. "Extract the src language."
  5249. (let ((m (match-string 0)))
  5250. (cond
  5251. ((stringp lang) lang)
  5252. ((integerp lang) (match-string lang))
  5253. ((and (eq lang 'lang)
  5254. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5255. (match-string 1 m))
  5256. ((and (eq lang 'style)
  5257. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5258. (match-string 1 m))
  5259. (t "fundamental"))))
  5260. (defun org-edit-src-exit ()
  5261. "Exit special edit and protect problematic lines."
  5262. (interactive)
  5263. (unless (buffer-base-buffer (current-buffer))
  5264. (error "This is not an indirect buffer, something is wrong..."))
  5265. (unless (> (point-min) 1)
  5266. (error "This buffer is not narrowed, something is wrong..."))
  5267. (goto-char (point-min))
  5268. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5269. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5270. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5271. (goto-char (point-min))
  5272. (while (re-search-forward "\n" nil t)
  5273. (replace-match " "))
  5274. (goto-char (point-min))
  5275. (if (looking-at "\\s-*") (replace-match " "))
  5276. (if (re-search-forward "\\s-+\\'" nil t)
  5277. (replace-match "")))
  5278. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5279. (goto-char (point-min))
  5280. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5281. (replace-match ",\\1"))
  5282. (when font-lock-mode
  5283. (font-lock-unfontify-region (point-min) (point-max)))
  5284. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5285. (when (org-bound-and-true-p org-edit-src-picture)
  5286. (untabify (point-min) (point-max))
  5287. (goto-char (point-min))
  5288. (while (re-search-forward "^" nil t)
  5289. (replace-match ": "))
  5290. (when font-lock-mode
  5291. (font-lock-unfontify-region (point-min) (point-max)))
  5292. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5293. (kill-buffer (current-buffer))
  5294. (and (org-mode-p) (org-restart-font-lock)))
  5295. ;;; The orgstruct minor mode
  5296. ;; Define a minor mode which can be used in other modes in order to
  5297. ;; integrate the org-mode structure editing commands.
  5298. ;; This is really a hack, because the org-mode structure commands use
  5299. ;; keys which normally belong to the major mode. Here is how it
  5300. ;; works: The minor mode defines all the keys necessary to operate the
  5301. ;; structure commands, but wraps the commands into a function which
  5302. ;; tests if the cursor is currently at a headline or a plain list
  5303. ;; item. If that is the case, the structure command is used,
  5304. ;; temporarily setting many Org-mode variables like regular
  5305. ;; expressions for filling etc. However, when any of those keys is
  5306. ;; used at a different location, function uses `key-binding' to look
  5307. ;; up if the key has an associated command in another currently active
  5308. ;; keymap (minor modes, major mode, global), and executes that
  5309. ;; command. There might be problems if any of the keys is otherwise
  5310. ;; used as a prefix key.
  5311. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5312. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5313. ;; addresses this by checking explicitly for both bindings.
  5314. (defvar orgstruct-mode-map (make-sparse-keymap)
  5315. "Keymap for the minor `orgstruct-mode'.")
  5316. (defvar org-local-vars nil
  5317. "List of local variables, for use by `orgstruct-mode'")
  5318. ;;;###autoload
  5319. (define-minor-mode orgstruct-mode
  5320. "Toggle the minor more `orgstruct-mode'.
  5321. This mode is for using Org-mode structure commands in other modes.
  5322. The following key behave as if Org-mode was active, if the cursor
  5323. is on a headline, or on a plain list item (both in the definition
  5324. of Org-mode).
  5325. M-up Move entry/item up
  5326. M-down Move entry/item down
  5327. M-left Promote
  5328. M-right Demote
  5329. M-S-up Move entry/item up
  5330. M-S-down Move entry/item down
  5331. M-S-left Promote subtree
  5332. M-S-right Demote subtree
  5333. M-q Fill paragraph and items like in Org-mode
  5334. C-c ^ Sort entries
  5335. C-c - Cycle list bullet
  5336. TAB Cycle item visibility
  5337. M-RET Insert new heading/item
  5338. S-M-RET Insert new TODO heading / Checkbox item
  5339. C-c C-c Set tags / toggle checkbox"
  5340. nil " OrgStruct" nil
  5341. (org-load-modules-maybe)
  5342. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5343. ;;;###autoload
  5344. (defun turn-on-orgstruct ()
  5345. "Unconditionally turn on `orgstruct-mode'."
  5346. (orgstruct-mode 1))
  5347. ;;;###autoload
  5348. (defun turn-on-orgstruct++ ()
  5349. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5350. In addition to setting orgstruct-mode, this also exports all indentation and
  5351. autofilling variables from org-mode into the buffer. Note that turning
  5352. off orgstruct-mode will *not* remove these additional settings."
  5353. (orgstruct-mode 1)
  5354. (let (var val)
  5355. (mapc
  5356. (lambda (x)
  5357. (when (string-match
  5358. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5359. (symbol-name (car x)))
  5360. (setq var (car x) val (nth 1 x))
  5361. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5362. org-local-vars)))
  5363. (defun orgstruct-error ()
  5364. "Error when there is no default binding for a structure key."
  5365. (interactive)
  5366. (error "This key has no function outside structure elements"))
  5367. (defun orgstruct-setup ()
  5368. "Setup orgstruct keymaps."
  5369. (let ((nfunc 0)
  5370. (bindings
  5371. (list
  5372. '([(meta up)] org-metaup)
  5373. '([(meta down)] org-metadown)
  5374. '([(meta left)] org-metaleft)
  5375. '([(meta right)] org-metaright)
  5376. '([(meta shift up)] org-shiftmetaup)
  5377. '([(meta shift down)] org-shiftmetadown)
  5378. '([(meta shift left)] org-shiftmetaleft)
  5379. '([(meta shift right)] org-shiftmetaright)
  5380. '([(shift up)] org-shiftup)
  5381. '([(shift down)] org-shiftdown)
  5382. '([(shift left)] org-shiftleft)
  5383. '([(shift right)] org-shiftright)
  5384. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5385. '("\M-q" fill-paragraph)
  5386. '("\C-c^" org-sort)
  5387. '("\C-c-" org-cycle-list-bullet)))
  5388. elt key fun cmd)
  5389. (while (setq elt (pop bindings))
  5390. (setq nfunc (1+ nfunc))
  5391. (setq key (org-key (car elt))
  5392. fun (nth 1 elt)
  5393. cmd (orgstruct-make-binding fun nfunc key))
  5394. (org-defkey orgstruct-mode-map key cmd))
  5395. ;; Special treatment needed for TAB and RET
  5396. (org-defkey orgstruct-mode-map [(tab)]
  5397. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5398. (org-defkey orgstruct-mode-map "\C-i"
  5399. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5400. (org-defkey orgstruct-mode-map "\M-\C-m"
  5401. (orgstruct-make-binding 'org-insert-heading 105
  5402. "\M-\C-m" [(meta return)]))
  5403. (org-defkey orgstruct-mode-map [(meta return)]
  5404. (orgstruct-make-binding 'org-insert-heading 106
  5405. [(meta return)] "\M-\C-m"))
  5406. (org-defkey orgstruct-mode-map [(shift meta return)]
  5407. (orgstruct-make-binding 'org-insert-todo-heading 107
  5408. [(meta return)] "\M-\C-m"))
  5409. (unless org-local-vars
  5410. (setq org-local-vars (org-get-local-variables)))
  5411. t))
  5412. (defun orgstruct-make-binding (fun n &rest keys)
  5413. "Create a function for binding in the structure minor mode.
  5414. FUN is the command to call inside a table. N is used to create a unique
  5415. command name. KEYS are keys that should be checked in for a command
  5416. to execute outside of tables."
  5417. (eval
  5418. (list 'defun
  5419. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5420. '(arg)
  5421. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5422. "Outside of structure, run the binding of `"
  5423. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5424. "'.")
  5425. '(interactive "p")
  5426. (list 'if
  5427. '(org-context-p 'headline 'item)
  5428. (list 'org-run-like-in-org-mode (list 'quote fun))
  5429. (list 'let '(orgstruct-mode)
  5430. (list 'call-interactively
  5431. (append '(or)
  5432. (mapcar (lambda (k)
  5433. (list 'key-binding k))
  5434. keys)
  5435. '('orgstruct-error))))))))
  5436. (defun org-context-p (&rest contexts)
  5437. "Check if local context is any of CONTEXTS.
  5438. Possible values in the list of contexts are `table', `headline', and `item'."
  5439. (let ((pos (point)))
  5440. (goto-char (point-at-bol))
  5441. (prog1 (or (and (memq 'table contexts)
  5442. (looking-at "[ \t]*|"))
  5443. (and (memq 'headline contexts)
  5444. ;;????????? (looking-at "\\*+"))
  5445. (looking-at outline-regexp))
  5446. (and (memq 'item contexts)
  5447. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5448. (goto-char pos))))
  5449. (defun org-get-local-variables ()
  5450. "Return a list of all local variables in an org-mode buffer."
  5451. (let (varlist)
  5452. (with-current-buffer (get-buffer-create "*Org tmp*")
  5453. (erase-buffer)
  5454. (org-mode)
  5455. (setq varlist (buffer-local-variables)))
  5456. (kill-buffer "*Org tmp*")
  5457. (delq nil
  5458. (mapcar
  5459. (lambda (x)
  5460. (setq x
  5461. (if (symbolp x)
  5462. (list x)
  5463. (list (car x) (list 'quote (cdr x)))))
  5464. (if (string-match
  5465. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5466. (symbol-name (car x)))
  5467. x nil))
  5468. varlist))))
  5469. ;;;###autoload
  5470. (defun org-run-like-in-org-mode (cmd)
  5471. (org-load-modules-maybe)
  5472. (unless org-local-vars
  5473. (setq org-local-vars (org-get-local-variables)))
  5474. (eval (list 'let org-local-vars
  5475. (list 'call-interactively (list 'quote cmd)))))
  5476. ;;;; Archiving
  5477. (defun org-get-category (&optional pos)
  5478. "Get the category applying to position POS."
  5479. (get-text-property (or pos (point)) 'org-category))
  5480. (defun org-refresh-category-properties ()
  5481. "Refresh category text properties in the buffer."
  5482. (let ((def-cat (cond
  5483. ((null org-category)
  5484. (if buffer-file-name
  5485. (file-name-sans-extension
  5486. (file-name-nondirectory buffer-file-name))
  5487. "???"))
  5488. ((symbolp org-category) (symbol-name org-category))
  5489. (t org-category)))
  5490. beg end cat pos optionp)
  5491. (org-unmodified
  5492. (save-excursion
  5493. (save-restriction
  5494. (widen)
  5495. (goto-char (point-min))
  5496. (put-text-property (point) (point-max) 'org-category def-cat)
  5497. (while (re-search-forward
  5498. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5499. (setq pos (match-end 0)
  5500. optionp (equal (char-after (match-beginning 0)) ?#)
  5501. cat (org-trim (match-string 2)))
  5502. (if optionp
  5503. (setq beg (point-at-bol) end (point-max))
  5504. (org-back-to-heading t)
  5505. (setq beg (point) end (org-end-of-subtree t t)))
  5506. (put-text-property beg end 'org-category cat)
  5507. (goto-char pos)))))))
  5508. ;;;; Link Stuff
  5509. ;;; Link abbreviations
  5510. (defun org-link-expand-abbrev (link)
  5511. "Apply replacements as defined in `org-link-abbrev-alist."
  5512. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5513. (let* ((key (match-string 1 link))
  5514. (as (or (assoc key org-link-abbrev-alist-local)
  5515. (assoc key org-link-abbrev-alist)))
  5516. (tag (and (match-end 2) (match-string 3 link)))
  5517. rpl)
  5518. (if (not as)
  5519. link
  5520. (setq rpl (cdr as))
  5521. (cond
  5522. ((symbolp rpl) (funcall rpl tag))
  5523. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5524. ((string-match "%h" rpl)
  5525. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5526. (t (concat rpl tag)))))
  5527. link))
  5528. ;;; Storing and inserting links
  5529. (defvar org-insert-link-history nil
  5530. "Minibuffer history for links inserted with `org-insert-link'.")
  5531. (defvar org-stored-links nil
  5532. "Contains the links stored with `org-store-link'.")
  5533. (defvar org-store-link-plist nil
  5534. "Plist with info about the most recently link created with `org-store-link'.")
  5535. (defvar org-link-protocols nil
  5536. "Link protocols added to Org-mode using `org-add-link-type'.")
  5537. (defvar org-store-link-functions nil
  5538. "List of functions that are called to create and store a link.
  5539. Each function will be called in turn until one returns a non-nil
  5540. value. Each function should check if it is responsible for creating
  5541. this link (for example by looking at the major mode).
  5542. If not, it must exit and return nil.
  5543. If yes, it should return a non-nil value after a calling
  5544. `org-store-link-props' with a list of properties and values.
  5545. Special properties are:
  5546. :type The link prefix. like \"http\". This must be given.
  5547. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5548. This is obligatory as well.
  5549. :description Optional default description for the second pair
  5550. of brackets in an Org-mode link. The user can still change
  5551. this when inserting this link into an Org-mode buffer.
  5552. In addition to these, any additional properties can be specified
  5553. and then used in remember templates.")
  5554. (defun org-add-link-type (type &optional follow export)
  5555. "Add TYPE to the list of `org-link-types'.
  5556. Re-compute all regular expressions depending on `org-link-types'
  5557. FOLLOW and EXPORT are two functions.
  5558. FOLLOW should take the link path as the single argument and do whatever
  5559. is necessary to follow the link, for example find a file or display
  5560. a mail message.
  5561. EXPORT should format the link path for export to one of the export formats.
  5562. It should be a function accepting three arguments:
  5563. path the path of the link, the text after the prefix (like \"http:\")
  5564. desc the description of the link, if any, nil if there was no description
  5565. format the export format, a symbol like `html' or `latex'.
  5566. The function may use the FORMAT information to return different values
  5567. depending on the format. The return value will be put literally into
  5568. the exported file.
  5569. Org-mode has a built-in default for exporting links. If you are happy with
  5570. this default, there is no need to define an export function for the link
  5571. type. For a simple example of an export function, see `org-bbdb.el'."
  5572. (add-to-list 'org-link-types type t)
  5573. (org-make-link-regexps)
  5574. (if (assoc type org-link-protocols)
  5575. (setcdr (assoc type org-link-protocols) (list follow export))
  5576. (push (list type follow export) org-link-protocols)))
  5577. ;;;###autoload
  5578. (defun org-store-link (arg)
  5579. "\\<org-mode-map>Store an org-link to the current location.
  5580. This link is added to `org-stored-links' and can later be inserted
  5581. into an org-buffer with \\[org-insert-link].
  5582. For some link types, a prefix arg is interpreted:
  5583. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5584. For file links, arg negates `org-context-in-file-links'."
  5585. (interactive "P")
  5586. (org-load-modules-maybe)
  5587. (setq org-store-link-plist nil) ; reset
  5588. (let (link cpltxt desc description search txt)
  5589. (cond
  5590. ((run-hook-with-args-until-success 'org-store-link-functions)
  5591. (setq link (plist-get org-store-link-plist :link)
  5592. desc (or (plist-get org-store-link-plist :description) link)))
  5593. ((eq major-mode 'calendar-mode)
  5594. (let ((cd (calendar-cursor-to-date)))
  5595. (setq link
  5596. (format-time-string
  5597. (car org-time-stamp-formats)
  5598. (apply 'encode-time
  5599. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5600. nil nil nil))))
  5601. (org-store-link-props :type "calendar" :date cd)))
  5602. ((eq major-mode 'w3-mode)
  5603. (setq cpltxt (url-view-url t)
  5604. link (org-make-link cpltxt))
  5605. (org-store-link-props :type "w3" :url (url-view-url t)))
  5606. ((eq major-mode 'w3m-mode)
  5607. (setq cpltxt (or w3m-current-title w3m-current-url)
  5608. link (org-make-link w3m-current-url))
  5609. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5610. ((setq search (run-hook-with-args-until-success
  5611. 'org-create-file-search-functions))
  5612. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5613. "::" search))
  5614. (setq cpltxt (or description link)))
  5615. ((eq major-mode 'image-mode)
  5616. (setq cpltxt (concat "file:"
  5617. (abbreviate-file-name buffer-file-name))
  5618. link (org-make-link cpltxt))
  5619. (org-store-link-props :type "image" :file buffer-file-name))
  5620. ((eq major-mode 'dired-mode)
  5621. ;; link to the file in the current line
  5622. (setq cpltxt (concat "file:"
  5623. (abbreviate-file-name
  5624. (expand-file-name
  5625. (dired-get-filename nil t))))
  5626. link (org-make-link cpltxt)))
  5627. ((and buffer-file-name (org-mode-p))
  5628. (cond
  5629. ((org-in-regexp "<<\\(.*?\\)>>")
  5630. (setq cpltxt
  5631. (concat "file:"
  5632. (abbreviate-file-name buffer-file-name)
  5633. "::" (match-string 1))
  5634. link (org-make-link cpltxt)))
  5635. ((and (featurep 'org-id)
  5636. (or (eq org-link-to-org-use-id t)
  5637. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5638. (interactive-p))
  5639. (and org-link-to-org-use-id
  5640. (condition-case nil
  5641. (org-entry-get nil "ID")
  5642. (error nil)))))
  5643. ;; We can make a link using the ID.
  5644. (setq link (condition-case nil
  5645. (prog1 (org-id-store-link)
  5646. (setq desc (plist-get org-store-link-plist
  5647. :description)))
  5648. (error
  5649. ;; probably before first headline, link to file only
  5650. (concat "file:"
  5651. (abbreviate-file-name buffer-file-name))))))
  5652. (t
  5653. ;; Just link to current headline
  5654. (setq cpltxt (concat "file:"
  5655. (abbreviate-file-name buffer-file-name)))
  5656. ;; Add a context search string
  5657. (when (org-xor org-context-in-file-links arg)
  5658. (setq txt (cond
  5659. ((org-on-heading-p) nil)
  5660. ((org-region-active-p)
  5661. (buffer-substring (region-beginning) (region-end)))
  5662. (t nil)))
  5663. (when (or (null txt) (string-match "\\S-" txt))
  5664. (setq cpltxt
  5665. (concat cpltxt "::"
  5666. (condition-case nil
  5667. (org-make-org-heading-search-string txt)
  5668. (error "")))
  5669. desc "NONE")))
  5670. (if (string-match "::\\'" cpltxt)
  5671. (setq cpltxt (substring cpltxt 0 -2)))
  5672. (setq link (org-make-link cpltxt)))))
  5673. ((buffer-file-name (buffer-base-buffer))
  5674. ;; Just link to this file here.
  5675. (setq cpltxt (concat "file:"
  5676. (abbreviate-file-name
  5677. (buffer-file-name (buffer-base-buffer)))))
  5678. ;; Add a context string
  5679. (when (org-xor org-context-in-file-links arg)
  5680. (setq txt (if (org-region-active-p)
  5681. (buffer-substring (region-beginning) (region-end))
  5682. (buffer-substring (point-at-bol) (point-at-eol))))
  5683. ;; Only use search option if there is some text.
  5684. (when (string-match "\\S-" txt)
  5685. (setq cpltxt
  5686. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5687. desc "NONE")))
  5688. (setq link (org-make-link cpltxt)))
  5689. ((interactive-p)
  5690. (error "Cannot link to a buffer which is not visiting a file"))
  5691. (t (setq link nil)))
  5692. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5693. (setq link (or link cpltxt)
  5694. desc (or desc cpltxt))
  5695. (if (equal desc "NONE") (setq desc nil))
  5696. (if (and (interactive-p) link)
  5697. (progn
  5698. (setq org-stored-links
  5699. (cons (list link desc) org-stored-links))
  5700. (message "Stored: %s" (or desc link)))
  5701. (and link (org-make-link-string link desc)))))
  5702. (defun org-store-link-props (&rest plist)
  5703. "Store link properties, extract names and addresses."
  5704. (let (x adr)
  5705. (when (setq x (plist-get plist :from))
  5706. (setq adr (mail-extract-address-components x))
  5707. (setq plist (plist-put plist :fromname (car adr)))
  5708. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5709. (when (setq x (plist-get plist :to))
  5710. (setq adr (mail-extract-address-components x))
  5711. (setq plist (plist-put plist :toname (car adr)))
  5712. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5713. (let ((from (plist-get plist :from))
  5714. (to (plist-get plist :to)))
  5715. (when (and from to org-from-is-user-regexp)
  5716. (setq plist
  5717. (plist-put plist :fromto
  5718. (if (string-match org-from-is-user-regexp from)
  5719. (concat "to %t")
  5720. (concat "from %f"))))))
  5721. (setq org-store-link-plist plist))
  5722. (defun org-add-link-props (&rest plist)
  5723. "Add these properties to the link property list."
  5724. (let (key value)
  5725. (while plist
  5726. (setq key (pop plist) value (pop plist))
  5727. (setq org-store-link-plist
  5728. (plist-put org-store-link-plist key value)))))
  5729. (defun org-email-link-description (&optional fmt)
  5730. "Return the description part of an email link.
  5731. This takes information from `org-store-link-plist' and formats it
  5732. according to FMT (default from `org-email-link-description-format')."
  5733. (setq fmt (or fmt org-email-link-description-format))
  5734. (let* ((p org-store-link-plist)
  5735. (to (plist-get p :toaddress))
  5736. (from (plist-get p :fromaddress))
  5737. (table
  5738. (list
  5739. (cons "%c" (plist-get p :fromto))
  5740. (cons "%F" (plist-get p :from))
  5741. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5742. (cons "%T" (plist-get p :to))
  5743. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5744. (cons "%s" (plist-get p :subject))
  5745. (cons "%m" (plist-get p :message-id)))))
  5746. (when (string-match "%c" fmt)
  5747. ;; Check if the user wrote this message
  5748. (if (and org-from-is-user-regexp from to
  5749. (save-match-data (string-match org-from-is-user-regexp from)))
  5750. (setq fmt (replace-match "to %t" t t fmt))
  5751. (setq fmt (replace-match "from %f" t t fmt))))
  5752. (org-replace-escapes fmt table)))
  5753. (defun org-make-org-heading-search-string (&optional string heading)
  5754. "Make search string for STRING or current headline."
  5755. (interactive)
  5756. (let ((s (or string (org-get-heading))))
  5757. (unless (and string (not heading))
  5758. ;; We are using a headline, clean up garbage in there.
  5759. (if (string-match org-todo-regexp s)
  5760. (setq s (replace-match "" t t s)))
  5761. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5762. (setq s (replace-match "" t t s)))
  5763. (setq s (org-trim s))
  5764. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5765. org-comment-string "\\)") s)
  5766. (setq s (replace-match "" t t s)))
  5767. (while (string-match org-ts-regexp s)
  5768. (setq s (replace-match "" t t s))))
  5769. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5770. (setq s (replace-match " " t t s)))
  5771. (or string (setq s (concat "*" s))) ; Add * for headlines
  5772. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5773. (defun org-make-link (&rest strings)
  5774. "Concatenate STRINGS."
  5775. (apply 'concat strings))
  5776. (defun org-make-link-string (link &optional description)
  5777. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5778. (unless (string-match "\\S-" link)
  5779. (error "Empty link"))
  5780. (when (stringp description)
  5781. ;; Remove brackets from the description, they are fatal.
  5782. (while (string-match "\\[" description)
  5783. (setq description (replace-match "{" t t description)))
  5784. (while (string-match "\\]" description)
  5785. (setq description (replace-match "}" t t description))))
  5786. (when (equal (org-link-escape link) description)
  5787. ;; No description needed, it is identical
  5788. (setq description nil))
  5789. (when (and (not description)
  5790. (not (equal link (org-link-escape link))))
  5791. (setq description (org-extract-attributes link)))
  5792. (concat "[[" (org-link-escape link) "]"
  5793. (if description (concat "[" description "]") "")
  5794. "]"))
  5795. (defconst org-link-escape-chars
  5796. '((?\ . "%20")
  5797. (?\[ . "%5B")
  5798. (?\] . "%5D")
  5799. (?\340 . "%E0") ; `a
  5800. (?\342 . "%E2") ; ^a
  5801. (?\347 . "%E7") ; ,c
  5802. (?\350 . "%E8") ; `e
  5803. (?\351 . "%E9") ; 'e
  5804. (?\352 . "%EA") ; ^e
  5805. (?\356 . "%EE") ; ^i
  5806. (?\364 . "%F4") ; ^o
  5807. (?\371 . "%F9") ; `u
  5808. (?\373 . "%FB") ; ^u
  5809. (?\; . "%3B")
  5810. (?? . "%3F")
  5811. (?= . "%3D")
  5812. (?+ . "%2B")
  5813. )
  5814. "Association list of escapes for some characters problematic in links.
  5815. This is the list that is used for internal purposes.")
  5816. (defconst org-link-escape-chars-browser
  5817. '((?\ . "%20")) ; 32 for the SPC char
  5818. "Association list of escapes for some characters problematic in links.
  5819. This is the list that is used before handing over to the browser.")
  5820. (defun org-link-escape (text &optional table)
  5821. "Escape characters in TEXT that are problematic for links."
  5822. (setq table (or table org-link-escape-chars))
  5823. (when text
  5824. (let ((re (mapconcat (lambda (x) (regexp-quote
  5825. (char-to-string (car x))))
  5826. table "\\|")))
  5827. (while (string-match re text)
  5828. (setq text
  5829. (replace-match
  5830. (cdr (assoc (string-to-char (match-string 0 text))
  5831. table))
  5832. t t text)))
  5833. text)))
  5834. (defun org-link-unescape (text &optional table)
  5835. "Reverse the action of `org-link-escape'."
  5836. (setq table (or table org-link-escape-chars))
  5837. (when text
  5838. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5839. table "\\|")))
  5840. (while (string-match re text)
  5841. (setq text
  5842. (replace-match
  5843. (char-to-string (car (rassoc (match-string 0 text) table)))
  5844. t t text)))
  5845. text)))
  5846. (defun org-xor (a b)
  5847. "Exclusive or."
  5848. (if a (not b) b))
  5849. (defun org-get-header (header)
  5850. "Find a header field in the current buffer."
  5851. (save-excursion
  5852. (goto-char (point-min))
  5853. (let ((case-fold-search t) s)
  5854. (cond
  5855. ((eq header 'from)
  5856. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5857. (setq s (match-string 1)))
  5858. (while (string-match "\"" s)
  5859. (setq s (replace-match "" t t s)))
  5860. (if (string-match "[<(].*" s)
  5861. (setq s (replace-match "" t t s))))
  5862. ((eq header 'message-id)
  5863. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5864. (setq s (match-string 1))))
  5865. ((eq header 'subject)
  5866. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5867. (setq s (match-string 1)))))
  5868. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5869. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5870. s)))
  5871. (defun org-fixup-message-id-for-http (s)
  5872. "Replace special characters in a message id, so it can be used in an http query."
  5873. (while (string-match "<" s)
  5874. (setq s (replace-match "%3C" t t s)))
  5875. (while (string-match ">" s)
  5876. (setq s (replace-match "%3E" t t s)))
  5877. (while (string-match "@" s)
  5878. (setq s (replace-match "%40" t t s)))
  5879. s)
  5880. ;;;###autoload
  5881. (defun org-insert-link-global ()
  5882. "Insert a link like Org-mode does.
  5883. This command can be called in any mode to insert a link in Org-mode syntax."
  5884. (interactive)
  5885. (org-load-modules-maybe)
  5886. (org-run-like-in-org-mode 'org-insert-link))
  5887. (defun org-insert-link (&optional complete-file link-location)
  5888. "Insert a link. At the prompt, enter the link.
  5889. Completion can be used to insert any of the link protocol prefixes like
  5890. http or ftp in use.
  5891. The history can be used to select a link previously stored with
  5892. `org-store-link'. When the empty string is entered (i.e. if you just
  5893. press RET at the prompt), the link defaults to the most recently
  5894. stored link. As SPC triggers completion in the minibuffer, you need to
  5895. use M-SPC or C-q SPC to force the insertion of a space character.
  5896. You will also be prompted for a description, and if one is given, it will
  5897. be displayed in the buffer instead of the link.
  5898. If there is already a link at point, this command will allow you to edit link
  5899. and description parts.
  5900. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5901. be selected using completion. The path to the file will be relative to the
  5902. current directory if the file is in the current directory or a subdirectory.
  5903. Otherwise, the link will be the absolute path as completed in the minibuffer
  5904. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5905. option `org-link-file-path-type'.
  5906. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5907. the current directory or below.
  5908. With three \\[universal-argument] prefixes, negate the meaning of
  5909. `org-keep-stored-link-after-insertion'.
  5910. If `org-make-link-description-function' is non-nil, this function will be
  5911. called with the link target, and the result will be the default
  5912. link description.
  5913. If the LINK-LOCATION parameter is non-nil, this value will be
  5914. used as the link location instead of reading one interactively."
  5915. (interactive "P")
  5916. (let* ((wcf (current-window-configuration))
  5917. (region (if (org-region-active-p)
  5918. (buffer-substring (region-beginning) (region-end))))
  5919. (remove (and region (list (region-beginning) (region-end))))
  5920. (desc region)
  5921. tmphist ; byte-compile incorrectly complains about this
  5922. (link link-location)
  5923. entry file)
  5924. (cond
  5925. (link-location) ; specified by arg, just use it.
  5926. ((org-in-regexp org-bracket-link-regexp 1)
  5927. ;; We do have a link at point, and we are going to edit it.
  5928. (setq remove (list (match-beginning 0) (match-end 0)))
  5929. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5930. (setq link (read-string "Link: "
  5931. (org-link-unescape
  5932. (org-match-string-no-properties 1)))))
  5933. ((or (org-in-regexp org-angle-link-re)
  5934. (org-in-regexp org-plain-link-re))
  5935. ;; Convert to bracket link
  5936. (setq remove (list (match-beginning 0) (match-end 0))
  5937. link (read-string "Link: "
  5938. (org-remove-angle-brackets (match-string 0)))))
  5939. ((member complete-file '((4) (16)))
  5940. ;; Completing read for file names.
  5941. (setq file (read-file-name "File: "))
  5942. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5943. (pwd1 (file-name-as-directory (abbreviate-file-name
  5944. (expand-file-name ".")))))
  5945. (cond
  5946. ((equal complete-file '(16))
  5947. (setq link (org-make-link
  5948. "file:"
  5949. (abbreviate-file-name (expand-file-name file)))))
  5950. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5951. (setq link (org-make-link "file:" (match-string 1 file))))
  5952. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5953. (expand-file-name file))
  5954. (setq link (org-make-link
  5955. "file:" (match-string 1 (expand-file-name file)))))
  5956. (t (setq link (org-make-link "file:" file))))))
  5957. (t
  5958. ;; Read link, with completion for stored links.
  5959. (with-output-to-temp-buffer "*Org Links*"
  5960. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5961. (when org-stored-links
  5962. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5963. (princ (mapconcat
  5964. (lambda (x)
  5965. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5966. (reverse org-stored-links) "\n"))))
  5967. (let ((cw (selected-window)))
  5968. (select-window (get-buffer-window "*Org Links*"))
  5969. (org-fit-window-to-buffer)
  5970. (setq truncate-lines t)
  5971. (select-window cw))
  5972. ;; Fake a link history, containing the stored links.
  5973. (setq tmphist (append (mapcar 'car org-stored-links)
  5974. org-insert-link-history))
  5975. (unwind-protect
  5976. (setq link (org-completing-read
  5977. "Link: "
  5978. (append
  5979. (mapcar (lambda (x) (list (concat (car x) ":")))
  5980. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5981. (mapcar (lambda (x) (list (concat x ":")))
  5982. org-link-types))
  5983. nil nil nil
  5984. 'tmphist
  5985. (or (car (car org-stored-links)))))
  5986. (set-window-configuration wcf)
  5987. (kill-buffer "*Org Links*"))
  5988. (setq entry (assoc link org-stored-links))
  5989. (or entry (push link org-insert-link-history))
  5990. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5991. (not org-keep-stored-link-after-insertion))
  5992. (setq org-stored-links (delq (assoc link org-stored-links)
  5993. org-stored-links)))
  5994. (setq desc (or desc (nth 1 entry)))))
  5995. (if (string-match org-plain-link-re link)
  5996. ;; URL-like link, normalize the use of angular brackets.
  5997. (setq link (org-make-link (org-remove-angle-brackets link))))
  5998. ;; Check if we are linking to the current file with a search option
  5999. ;; If yes, simplify the link by using only the search option.
  6000. (when (and buffer-file-name
  6001. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6002. (let* ((path (match-string 1 link))
  6003. (case-fold-search nil)
  6004. (search (match-string 2 link)))
  6005. (save-match-data
  6006. (if (equal (file-truename buffer-file-name) (file-truename path))
  6007. ;; We are linking to this same file, with a search option
  6008. (setq link search)))))
  6009. ;; Check if we can/should use a relative path. If yes, simplify the link
  6010. (when (string-match "^file:\\(.*\\)" link)
  6011. (let* ((path (match-string 1 link))
  6012. (origpath path)
  6013. (case-fold-search nil))
  6014. (cond
  6015. ((or (eq org-link-file-path-type 'absolute)
  6016. (equal complete-file '(16)))
  6017. (setq path (abbreviate-file-name (expand-file-name path))))
  6018. ((eq org-link-file-path-type 'noabbrev)
  6019. (setq path (expand-file-name path)))
  6020. ((eq org-link-file-path-type 'relative)
  6021. (setq path (file-relative-name path)))
  6022. (t
  6023. (save-match-data
  6024. (if (string-match (concat "^" (regexp-quote
  6025. (file-name-as-directory
  6026. (expand-file-name "."))))
  6027. (expand-file-name path))
  6028. ;; We are linking a file with relative path name.
  6029. (setq path (substring (expand-file-name path)
  6030. (match-end 0)))
  6031. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6032. (setq link (concat "file:" path))
  6033. (if (equal desc origpath)
  6034. (setq desc path))))
  6035. (if org-make-link-description-function
  6036. (setq desc (funcall org-make-link-description-function link desc)))
  6037. (setq desc (read-string "Description: " desc))
  6038. (unless (string-match "\\S-" desc) (setq desc nil))
  6039. (if remove (apply 'delete-region remove))
  6040. (insert (org-make-link-string link desc))))
  6041. (defun org-completing-read (&rest args)
  6042. "Completing-read with SPACE being a normal character."
  6043. (let ((minibuffer-local-completion-map
  6044. (copy-keymap minibuffer-local-completion-map)))
  6045. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6046. (apply 'org-ido-completing-read args)))
  6047. (defun org-ido-completing-read (&rest args)
  6048. "Completing-read using `ido-mode' speedups if available"
  6049. (if (and org-completion-use-ido
  6050. (fboundp 'ido-completing-read)
  6051. (boundp 'ido-mode) ido-mode
  6052. (listp (second args)))
  6053. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6054. (apply 'completing-read args)))
  6055. (defun org-extract-attributes (s)
  6056. "Extract the attributes cookie from a string and set as text property."
  6057. (let (a attr (start 0) key value)
  6058. (save-match-data
  6059. (when (string-match "{{\\([^}]+\\)}}$" s)
  6060. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6061. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6062. (setq key (match-string 1 a) value (match-string 2 a)
  6063. start (match-end 0)
  6064. attr (plist-put attr (intern key) value))))
  6065. (org-add-props s nil 'org-attr attr))
  6066. s))
  6067. (defun org-attributes-to-string (plist)
  6068. "Format a property list into an HTML attribute list."
  6069. (let ((s "") key value)
  6070. (while plist
  6071. (setq key (pop plist) value (pop plist))
  6072. (and value
  6073. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6074. s))
  6075. ;;; Opening/following a link
  6076. (defvar org-link-search-failed nil)
  6077. (defun org-next-link ()
  6078. "Move forward to the next link.
  6079. If the link is in hidden text, expose it."
  6080. (interactive)
  6081. (when (and org-link-search-failed (eq this-command last-command))
  6082. (goto-char (point-min))
  6083. (message "Link search wrapped back to beginning of buffer"))
  6084. (setq org-link-search-failed nil)
  6085. (let* ((pos (point))
  6086. (ct (org-context))
  6087. (a (assoc :link ct)))
  6088. (if a (goto-char (nth 2 a)))
  6089. (if (re-search-forward org-any-link-re nil t)
  6090. (progn
  6091. (goto-char (match-beginning 0))
  6092. (if (org-invisible-p) (org-show-context)))
  6093. (goto-char pos)
  6094. (setq org-link-search-failed t)
  6095. (error "No further link found"))))
  6096. (defun org-previous-link ()
  6097. "Move backward to the previous link.
  6098. If the link is in hidden text, expose it."
  6099. (interactive)
  6100. (when (and org-link-search-failed (eq this-command last-command))
  6101. (goto-char (point-max))
  6102. (message "Link search wrapped back to end of buffer"))
  6103. (setq org-link-search-failed nil)
  6104. (let* ((pos (point))
  6105. (ct (org-context))
  6106. (a (assoc :link ct)))
  6107. (if a (goto-char (nth 1 a)))
  6108. (if (re-search-backward org-any-link-re nil t)
  6109. (progn
  6110. (goto-char (match-beginning 0))
  6111. (if (org-invisible-p) (org-show-context)))
  6112. (goto-char pos)
  6113. (setq org-link-search-failed t)
  6114. (error "No further link found"))))
  6115. (defun org-translate-link (s)
  6116. "Translate a link string if a translation function has been defined."
  6117. (if (and org-link-translation-function
  6118. (fboundp org-link-translation-function)
  6119. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6120. (progn
  6121. (setq s (funcall org-link-translation-function
  6122. (match-string 1) (match-string 2)))
  6123. (concat (car s) ":" (cdr s)))
  6124. s))
  6125. (defun org-translate-link-from-planner (type path)
  6126. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6127. This is still an experimental function, your mileage may vary."
  6128. (cond
  6129. ((member type '("http" "https" "news" "ftp"))
  6130. ;; standard Internet links are the same.
  6131. nil)
  6132. ((and (equal type "irc") (string-match "^//" path))
  6133. ;; Planner has two / at the beginning of an irc link, we have 1.
  6134. ;; We should have zero, actually....
  6135. (setq path (substring path 1)))
  6136. ((and (equal type "lisp") (string-match "^/" path))
  6137. ;; Planner has a slash, we do not.
  6138. (setq type "elisp" path (substring path 1)))
  6139. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6140. ;; A typical message link. Planner has the id after the fina slash,
  6141. ;; we separate it with a hash mark
  6142. (setq path (concat (match-string 1 path) "#"
  6143. (org-remove-angle-brackets (match-string 2 path)))))
  6144. )
  6145. (cons type path))
  6146. (defun org-find-file-at-mouse (ev)
  6147. "Open file link or URL at mouse."
  6148. (interactive "e")
  6149. (mouse-set-point ev)
  6150. (org-open-at-point 'in-emacs))
  6151. (defun org-open-at-mouse (ev)
  6152. "Open file link or URL at mouse."
  6153. (interactive "e")
  6154. (mouse-set-point ev)
  6155. (if (eq major-mode 'org-agenda-mode)
  6156. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6157. (org-open-at-point))
  6158. (defvar org-window-config-before-follow-link nil
  6159. "The window configuration before following a link.
  6160. This is saved in case the need arises to restore it.")
  6161. (defvar org-open-link-marker (make-marker)
  6162. "Marker pointing to the location where `org-open-at-point; was called.")
  6163. ;;;###autoload
  6164. (defun org-open-at-point-global ()
  6165. "Follow a link like Org-mode does.
  6166. This command can be called in any mode to follow a link that has
  6167. Org-mode syntax."
  6168. (interactive)
  6169. (org-run-like-in-org-mode 'org-open-at-point))
  6170. ;;;###autoload
  6171. (defun org-open-link-from-string (s &optional arg)
  6172. "Open a link in the string S, as if it was in Org-mode."
  6173. (interactive "sLink: \nP")
  6174. (with-temp-buffer
  6175. (let ((org-inhibit-startup t))
  6176. (org-mode)
  6177. (insert s)
  6178. (goto-char (point-min))
  6179. (org-open-at-point arg))))
  6180. (defun org-open-at-point (&optional in-emacs)
  6181. "Open link at or after point.
  6182. If there is no link at point, this function will search forward up to
  6183. the end of the current subtree.
  6184. Normally, files will be opened by an appropriate application. If the
  6185. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6186. With a double prefix argument, try to open outside of Emacs, in the
  6187. application the system uses for this file type."
  6188. (interactive "P")
  6189. (org-load-modules-maybe)
  6190. (move-marker org-open-link-marker (point))
  6191. (setq org-window-config-before-follow-link (current-window-configuration))
  6192. (org-remove-occur-highlights nil nil t)
  6193. (if (org-at-timestamp-p t)
  6194. (org-follow-timestamp-link)
  6195. (let (type path link line search (pos (point)))
  6196. (catch 'match
  6197. (save-excursion
  6198. (skip-chars-forward "^]\n\r")
  6199. (when (org-in-regexp org-bracket-link-regexp)
  6200. (setq link (org-extract-attributes
  6201. (org-link-unescape (org-match-string-no-properties 1))))
  6202. (while (string-match " *\n *" link)
  6203. (setq link (replace-match " " t t link)))
  6204. (setq link (org-link-expand-abbrev link))
  6205. (cond
  6206. ((or (file-name-absolute-p link)
  6207. (string-match "^\\.\\.?/" link))
  6208. (setq type "file" path link))
  6209. ((string-match org-link-re-with-space3 link)
  6210. (setq type (match-string 1 link) path (match-string 2 link)))
  6211. (t (setq type "thisfile" path link)))
  6212. (throw 'match t)))
  6213. (when (get-text-property (point) 'org-linked-text)
  6214. (setq type "thisfile"
  6215. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6216. (1+ (point)) (point))
  6217. path (buffer-substring
  6218. (previous-single-property-change pos 'org-linked-text)
  6219. (next-single-property-change pos 'org-linked-text)))
  6220. (throw 'match t))
  6221. (save-excursion
  6222. (when (or (org-in-regexp org-angle-link-re)
  6223. (org-in-regexp org-plain-link-re))
  6224. (setq type (match-string 1) path (match-string 2))
  6225. (throw 'match t)))
  6226. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6227. (setq type "tree-match"
  6228. path (match-string 1))
  6229. (throw 'match t))
  6230. (save-excursion
  6231. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6232. (setq type "tags"
  6233. path (match-string 1))
  6234. (while (string-match ":" path)
  6235. (setq path (replace-match "+" t t path)))
  6236. (throw 'match t))))
  6237. (unless path
  6238. (error "No link found"))
  6239. ;; Remove any trailing spaces in path
  6240. (if (string-match " +\\'" path)
  6241. (setq path (replace-match "" t t path)))
  6242. (if (and org-link-translation-function
  6243. (fboundp org-link-translation-function))
  6244. ;; Check if we need to translate the link
  6245. (let ((tmp (funcall org-link-translation-function type path)))
  6246. (setq type (car tmp) path (cdr tmp))))
  6247. (cond
  6248. ((assoc type org-link-protocols)
  6249. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6250. ((equal type "mailto")
  6251. (let ((cmd (car org-link-mailto-program))
  6252. (args (cdr org-link-mailto-program)) args1
  6253. (address path) (subject "") a)
  6254. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6255. (setq address (match-string 1 path)
  6256. subject (org-link-escape (match-string 2 path))))
  6257. (while args
  6258. (cond
  6259. ((not (stringp (car args))) (push (pop args) args1))
  6260. (t (setq a (pop args))
  6261. (if (string-match "%a" a)
  6262. (setq a (replace-match address t t a)))
  6263. (if (string-match "%s" a)
  6264. (setq a (replace-match subject t t a)))
  6265. (push a args1))))
  6266. (apply cmd (nreverse args1))))
  6267. ((member type '("http" "https" "ftp" "news"))
  6268. (browse-url (concat type ":" (org-link-escape
  6269. path org-link-escape-chars-browser))))
  6270. ((member type '("message"))
  6271. (browse-url (concat type ":" path)))
  6272. ((string= type "tags")
  6273. (org-tags-view in-emacs path))
  6274. ((string= type "thisfile")
  6275. (if in-emacs
  6276. (switch-to-buffer-other-window
  6277. (org-get-buffer-for-internal-link (current-buffer)))
  6278. (org-mark-ring-push))
  6279. (let ((cmd `(org-link-search
  6280. ,path
  6281. ,(cond ((equal in-emacs '(4)) 'occur)
  6282. ((equal in-emacs '(16)) 'org-occur)
  6283. (t nil))
  6284. ,pos)))
  6285. (condition-case nil (eval cmd)
  6286. (error (progn (widen) (eval cmd))))))
  6287. ((string= type "tree-match")
  6288. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6289. ((string= type "file")
  6290. (if (string-match "::\\([0-9]+\\)\\'" path)
  6291. (setq line (string-to-number (match-string 1 path))
  6292. path (substring path 0 (match-beginning 0)))
  6293. (if (string-match "::\\(.+\\)\\'" path)
  6294. (setq search (match-string 1 path)
  6295. path (substring path 0 (match-beginning 0)))))
  6296. (if (string-match "[*?{]" (file-name-nondirectory path))
  6297. (dired path)
  6298. (org-open-file path in-emacs line search)))
  6299. ((string= type "news")
  6300. (require 'org-gnus)
  6301. (org-gnus-follow-link path))
  6302. ((string= type "shell")
  6303. (let ((cmd path))
  6304. (if (or (not org-confirm-shell-link-function)
  6305. (funcall org-confirm-shell-link-function
  6306. (format "Execute \"%s\" in shell? "
  6307. (org-add-props cmd nil
  6308. 'face 'org-warning))))
  6309. (progn
  6310. (message "Executing %s" cmd)
  6311. (shell-command cmd))
  6312. (error "Abort"))))
  6313. ((string= type "elisp")
  6314. (let ((cmd path))
  6315. (if (or (not org-confirm-elisp-link-function)
  6316. (funcall org-confirm-elisp-link-function
  6317. (format "Execute \"%s\" as elisp? "
  6318. (org-add-props cmd nil
  6319. 'face 'org-warning))))
  6320. (message "%s => %s" cmd
  6321. (if (equal (string-to-char cmd) ?\()
  6322. (eval (read cmd))
  6323. (call-interactively (read cmd))))
  6324. (error "Abort"))))
  6325. (t
  6326. (browse-url-at-point)))))
  6327. (move-marker org-open-link-marker nil)
  6328. (run-hook-with-args 'org-follow-link-hook))
  6329. ;;;; Time estimates
  6330. (defun org-get-effort (&optional pom)
  6331. "Get the effort estimate for the current entry."
  6332. (org-entry-get pom org-effort-property))
  6333. ;;; File search
  6334. (defvar org-create-file-search-functions nil
  6335. "List of functions to construct the right search string for a file link.
  6336. These functions are called in turn with point at the location to
  6337. which the link should point.
  6338. A function in the hook should first test if it would like to
  6339. handle this file type, for example by checking the major-mode or
  6340. the file extension. If it decides not to handle this file, it
  6341. should just return nil to give other functions a chance. If it
  6342. does handle the file, it must return the search string to be used
  6343. when following the link. The search string will be part of the
  6344. file link, given after a double colon, and `org-open-at-point'
  6345. will automatically search for it. If special measures must be
  6346. taken to make the search successful, another function should be
  6347. added to the companion hook `org-execute-file-search-functions',
  6348. which see.
  6349. A function in this hook may also use `setq' to set the variable
  6350. `description' to provide a suggestion for the descriptive text to
  6351. be used for this link when it gets inserted into an Org-mode
  6352. buffer with \\[org-insert-link].")
  6353. (defvar org-execute-file-search-functions nil
  6354. "List of functions to execute a file search triggered by a link.
  6355. Functions added to this hook must accept a single argument, the
  6356. search string that was part of the file link, the part after the
  6357. double colon. The function must first check if it would like to
  6358. handle this search, for example by checking the major-mode or the
  6359. file extension. If it decides not to handle this search, it
  6360. should just return nil to give other functions a chance. If it
  6361. does handle the search, it must return a non-nil value to keep
  6362. other functions from trying.
  6363. Each function can access the current prefix argument through the
  6364. variable `current-prefix-argument'. Note that a single prefix is
  6365. used to force opening a link in Emacs, so it may be good to only
  6366. use a numeric or double prefix to guide the search function.
  6367. In case this is needed, a function in this hook can also restore
  6368. the window configuration before `org-open-at-point' was called using:
  6369. (set-window-configuration org-window-config-before-follow-link)")
  6370. (defun org-link-search (s &optional type avoid-pos)
  6371. "Search for a link search option.
  6372. If S is surrounded by forward slashes, it is interpreted as a
  6373. regular expression. In org-mode files, this will create an `org-occur'
  6374. sparse tree. In ordinary files, `occur' will be used to list matches.
  6375. If the current buffer is in `dired-mode', grep will be used to search
  6376. in all files. If AVOID-POS is given, ignore matches near that position."
  6377. (let ((case-fold-search t)
  6378. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6379. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6380. (append '(("") (" ") ("\t") ("\n"))
  6381. org-emphasis-alist)
  6382. "\\|") "\\)"))
  6383. (pos (point))
  6384. (pre nil) (post nil)
  6385. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6386. (cond
  6387. ;; First check if there are any special
  6388. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6389. ;; Now try the builtin stuff
  6390. ((save-excursion
  6391. (goto-char (point-min))
  6392. (and
  6393. (re-search-forward
  6394. (concat "<<" (regexp-quote s0) ">>") nil t)
  6395. (setq type 'dedicated
  6396. pos (match-beginning 0))))
  6397. ;; There is an exact target for this
  6398. (goto-char pos))
  6399. ((string-match "^/\\(.*\\)/$" s)
  6400. ;; A regular expression
  6401. (cond
  6402. ((org-mode-p)
  6403. (org-occur (match-string 1 s)))
  6404. ;;((eq major-mode 'dired-mode)
  6405. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6406. (t (org-do-occur (match-string 1 s)))))
  6407. (t
  6408. ;; A normal search strings
  6409. (when (equal (string-to-char s) ?*)
  6410. ;; Anchor on headlines, post may include tags.
  6411. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6412. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6413. s (substring s 1)))
  6414. (remove-text-properties
  6415. 0 (length s)
  6416. '(face nil mouse-face nil keymap nil fontified nil) s)
  6417. ;; Make a series of regular expressions to find a match
  6418. (setq words (org-split-string s "[ \n\r\t]+")
  6419. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6420. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6421. "\\)" markers)
  6422. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6423. re2a (concat "[ \t\r\n]" re2a_)
  6424. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6425. re4 (concat "[^a-zA-Z_]" re4_)
  6426. re1 (concat pre re2 post)
  6427. re3 (concat pre (if pre re4_ re4) post)
  6428. re5 (concat pre ".*" re4)
  6429. re2 (concat pre re2)
  6430. re2a (concat pre (if pre re2a_ re2a))
  6431. re4 (concat pre (if pre re4_ re4))
  6432. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6433. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6434. re5 "\\)"
  6435. ))
  6436. (cond
  6437. ((eq type 'org-occur) (org-occur reall))
  6438. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6439. (t (goto-char (point-min))
  6440. (setq type 'fuzzy)
  6441. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6442. (org-search-not-self 1 re1 nil t)
  6443. (org-search-not-self 1 re2 nil t)
  6444. (org-search-not-self 1 re2a nil t)
  6445. (org-search-not-self 1 re3 nil t)
  6446. (org-search-not-self 1 re4 nil t)
  6447. (org-search-not-self 1 re5 nil t)
  6448. )
  6449. (goto-char (match-beginning 1))
  6450. (goto-char pos)
  6451. (error "No match")))))
  6452. (t
  6453. ;; Normal string-search
  6454. (goto-char (point-min))
  6455. (if (search-forward s nil t)
  6456. (goto-char (match-beginning 0))
  6457. (error "No match"))))
  6458. (and (org-mode-p) (org-show-context 'link-search))
  6459. type))
  6460. (defun org-search-not-self (group &rest args)
  6461. "Execute `re-search-forward', but only accept matches that do not
  6462. enclose the position of `org-open-link-marker'."
  6463. (let ((m org-open-link-marker))
  6464. (catch 'exit
  6465. (while (apply 're-search-forward args)
  6466. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6467. (goto-char (match-end group))
  6468. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6469. (> (match-beginning 0) (marker-position m))
  6470. (< (match-end 0) (marker-position m)))
  6471. (save-match-data
  6472. (or (not (org-in-regexp
  6473. org-bracket-link-analytic-regexp 1))
  6474. (not (match-end 4)) ; no description
  6475. (and (<= (match-beginning 4) (point))
  6476. (>= (match-end 4) (point))))))
  6477. (throw 'exit (point))))))))
  6478. (defun org-get-buffer-for-internal-link (buffer)
  6479. "Return a buffer to be used for displaying the link target of internal links."
  6480. (cond
  6481. ((not org-display-internal-link-with-indirect-buffer)
  6482. buffer)
  6483. ((string-match "(Clone)$" (buffer-name buffer))
  6484. (message "Buffer is already a clone, not making another one")
  6485. ;; we also do not modify visibility in this case
  6486. buffer)
  6487. (t ; make a new indirect buffer for displaying the link
  6488. (let* ((bn (buffer-name buffer))
  6489. (ibn (concat bn "(Clone)"))
  6490. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6491. (with-current-buffer ib (org-overview))
  6492. ib))))
  6493. (defun org-do-occur (regexp &optional cleanup)
  6494. "Call the Emacs command `occur'.
  6495. If CLEANUP is non-nil, remove the printout of the regular expression
  6496. in the *Occur* buffer. This is useful if the regex is long and not useful
  6497. to read."
  6498. (occur regexp)
  6499. (when cleanup
  6500. (let ((cwin (selected-window)) win beg end)
  6501. (when (setq win (get-buffer-window "*Occur*"))
  6502. (select-window win))
  6503. (goto-char (point-min))
  6504. (when (re-search-forward "match[a-z]+" nil t)
  6505. (setq beg (match-end 0))
  6506. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6507. (setq end (1- (match-beginning 0)))))
  6508. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6509. (goto-char (point-min))
  6510. (select-window cwin))))
  6511. ;;; The mark ring for links jumps
  6512. (defvar org-mark-ring nil
  6513. "Mark ring for positions before jumps in Org-mode.")
  6514. (defvar org-mark-ring-last-goto nil
  6515. "Last position in the mark ring used to go back.")
  6516. ;; Fill and close the ring
  6517. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6518. (loop for i from 1 to org-mark-ring-length do
  6519. (push (make-marker) org-mark-ring))
  6520. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6521. org-mark-ring)
  6522. (defun org-mark-ring-push (&optional pos buffer)
  6523. "Put the current position or POS into the mark ring and rotate it."
  6524. (interactive)
  6525. (setq pos (or pos (point)))
  6526. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6527. (move-marker (car org-mark-ring)
  6528. (or pos (point))
  6529. (or buffer (current-buffer)))
  6530. (message "%s"
  6531. (substitute-command-keys
  6532. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6533. (defun org-mark-ring-goto (&optional n)
  6534. "Jump to the previous position in the mark ring.
  6535. With prefix arg N, jump back that many stored positions. When
  6536. called several times in succession, walk through the entire ring.
  6537. Org-mode commands jumping to a different position in the current file,
  6538. or to another Org-mode file, automatically push the old position
  6539. onto the ring."
  6540. (interactive "p")
  6541. (let (p m)
  6542. (if (eq last-command this-command)
  6543. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6544. (setq p org-mark-ring))
  6545. (setq org-mark-ring-last-goto p)
  6546. (setq m (car p))
  6547. (switch-to-buffer (marker-buffer m))
  6548. (goto-char m)
  6549. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6550. (defun org-remove-angle-brackets (s)
  6551. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6552. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6553. s)
  6554. (defun org-add-angle-brackets (s)
  6555. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6556. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6557. s)
  6558. (defun org-remove-double-quotes (s)
  6559. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6560. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6561. s)
  6562. ;;; Following specific links
  6563. (defun org-follow-timestamp-link ()
  6564. (cond
  6565. ((org-at-date-range-p t)
  6566. (let ((org-agenda-start-on-weekday)
  6567. (t1 (match-string 1))
  6568. (t2 (match-string 2)))
  6569. (setq t1 (time-to-days (org-time-string-to-time t1))
  6570. t2 (time-to-days (org-time-string-to-time t2)))
  6571. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6572. ((org-at-timestamp-p t)
  6573. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6574. (substring (match-string 1) 0 10)))
  6575. 1))
  6576. (t (error "This should not happen"))))
  6577. ;;; Following file links
  6578. (defvar org-wait nil)
  6579. (defun org-open-file (path &optional in-emacs line search)
  6580. "Open the file at PATH.
  6581. First, this expands any special file name abbreviations. Then the
  6582. configuration variable `org-file-apps' is checked if it contains an
  6583. entry for this file type, and if yes, the corresponding command is launched.
  6584. If no application is found, Emacs simply visits the file.
  6585. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6586. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6587. and o use an external application to visit the file.
  6588. Optional LINE specifies a line to go to, optional SEARCH a string to
  6589. search for. If LINE or SEARCH is given, the file will always be
  6590. opened in Emacs.
  6591. If the file does not exist, an error is thrown."
  6592. (setq in-emacs (or in-emacs line search))
  6593. (let* ((file (if (equal path "")
  6594. buffer-file-name
  6595. (substitute-in-file-name (expand-file-name path))))
  6596. (apps (append org-file-apps (org-default-apps)))
  6597. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6598. (dirp (if remp nil (file-directory-p file)))
  6599. (file (if (and dirp org-open-directory-means-index-dot-org)
  6600. (concat (file-name-as-directory file) "index.org")
  6601. file))
  6602. (a-m-a-p (assq 'auto-mode apps))
  6603. (dfile (downcase file))
  6604. (old-buffer (current-buffer))
  6605. (old-pos (point))
  6606. (old-mode major-mode)
  6607. ext cmd)
  6608. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6609. (setq ext (match-string 1 dfile))
  6610. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6611. (setq ext (match-string 1 dfile))))
  6612. (cond
  6613. ((equal in-emacs '(16))
  6614. (setq cmd (cdr (assoc 'system apps))))
  6615. (in-emacs (setq cmd 'emacs))
  6616. (t
  6617. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6618. (and dirp (cdr (assoc 'directory apps)))
  6619. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6620. 'string-match)
  6621. (cdr (assoc ext apps))
  6622. (cdr (assoc t apps))))))
  6623. (when (eq cmd 'system)
  6624. (setq cmd (cdr (assoc 'system apps))))
  6625. (when (eq cmd 'default)
  6626. (setq cmd (cdr (assoc t apps))))
  6627. (when (eq cmd 'mailcap)
  6628. (require 'mailcap)
  6629. (mailcap-parse-mailcaps)
  6630. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6631. (command (mailcap-mime-info mime-type)))
  6632. (if (stringp command)
  6633. (setq cmd command)
  6634. (setq cmd 'emacs))))
  6635. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6636. (not (file-exists-p file))
  6637. (not org-open-non-existing-files))
  6638. (error "No such file: %s" file))
  6639. (cond
  6640. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6641. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6642. (while (string-match "['\"]%s['\"]" cmd)
  6643. (setq cmd (replace-match "%s" t t cmd)))
  6644. (while (string-match "%s" cmd)
  6645. (setq cmd (replace-match
  6646. (save-match-data
  6647. (shell-quote-argument
  6648. (convert-standard-filename file)))
  6649. t t cmd)))
  6650. (save-window-excursion
  6651. (start-process-shell-command cmd nil cmd)
  6652. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6653. ))
  6654. ((or (stringp cmd)
  6655. (eq cmd 'emacs))
  6656. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6657. (widen)
  6658. (if line (goto-line line)
  6659. (if search (org-link-search search))))
  6660. ((consp cmd)
  6661. (let ((file (convert-standard-filename file)))
  6662. (eval cmd)))
  6663. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6664. (and (org-mode-p) (eq old-mode 'org-mode)
  6665. (or (not (equal old-buffer (current-buffer)))
  6666. (not (equal old-pos (point))))
  6667. (org-mark-ring-push old-pos old-buffer))))
  6668. (defun org-default-apps ()
  6669. "Return the default applications for this operating system."
  6670. (cond
  6671. ((eq system-type 'darwin)
  6672. org-file-apps-defaults-macosx)
  6673. ((eq system-type 'windows-nt)
  6674. org-file-apps-defaults-windowsnt)
  6675. (t org-file-apps-defaults-gnu)))
  6676. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6677. "Convert extensions to regular expressions in the cars of LIST.
  6678. Also, weed out any non-string entries, because the return value is used
  6679. only for regexp matching.
  6680. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6681. point to the symbol `emacs', indicating that the file should
  6682. be opened in Emacs."
  6683. (append
  6684. (delq nil
  6685. (mapcar (lambda (x)
  6686. (if (not (stringp (car x)))
  6687. nil
  6688. (if (string-match "\\W" (car x))
  6689. x
  6690. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6691. list))
  6692. (if add-auto-mode
  6693. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6694. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6695. (defun org-file-remote-p (file)
  6696. "Test whether FILE specifies a location on a remote system.
  6697. Return non-nil if the location is indeed remote.
  6698. For example, the filename \"/user@host:/foo\" specifies a location
  6699. on the system \"/user@host:\"."
  6700. (cond ((fboundp 'file-remote-p)
  6701. (file-remote-p file))
  6702. ((fboundp 'tramp-handle-file-remote-p)
  6703. (tramp-handle-file-remote-p file))
  6704. ((and (boundp 'ange-ftp-name-format)
  6705. (string-match (car ange-ftp-name-format) file))
  6706. t)
  6707. (t nil)))
  6708. ;;;; Refiling
  6709. (defun org-get-org-file ()
  6710. "Read a filename, with default directory `org-directory'."
  6711. (let ((default (or org-default-notes-file remember-data-file)))
  6712. (read-file-name (format "File name [%s]: " default)
  6713. (file-name-as-directory org-directory)
  6714. default)))
  6715. (defun org-notes-order-reversed-p ()
  6716. "Check if the current file should receive notes in reversed order."
  6717. (cond
  6718. ((not org-reverse-note-order) nil)
  6719. ((eq t org-reverse-note-order) t)
  6720. ((not (listp org-reverse-note-order)) nil)
  6721. (t (catch 'exit
  6722. (let ((all org-reverse-note-order)
  6723. entry)
  6724. (while (setq entry (pop all))
  6725. (if (string-match (car entry) buffer-file-name)
  6726. (throw 'exit (cdr entry))))
  6727. nil)))))
  6728. (defvar org-refile-target-table nil
  6729. "The list of refile targets, created by `org-refile'.")
  6730. (defvar org-agenda-new-buffers nil
  6731. "Buffers created to visit agenda files.")
  6732. (defun org-get-refile-targets (&optional default-buffer)
  6733. "Produce a table with refile targets."
  6734. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6735. targets txt re files f desc descre fast-path-p level)
  6736. (message "Getting targets...")
  6737. (with-current-buffer (or default-buffer (current-buffer))
  6738. (while (setq entry (pop entries))
  6739. (setq files (car entry) desc (cdr entry))
  6740. (setq fast-path-p nil)
  6741. (cond
  6742. ((null files) (setq files (list (current-buffer))))
  6743. ((eq files 'org-agenda-files)
  6744. (setq files (org-agenda-files 'unrestricted)))
  6745. ((and (symbolp files) (fboundp files))
  6746. (setq files (funcall files)))
  6747. ((and (symbolp files) (boundp files))
  6748. (setq files (symbol-value files))))
  6749. (if (stringp files) (setq files (list files)))
  6750. (cond
  6751. ((eq (car desc) :tag)
  6752. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6753. ((eq (car desc) :todo)
  6754. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6755. ((eq (car desc) :regexp)
  6756. (setq descre (cdr desc)))
  6757. ((eq (car desc) :level)
  6758. (setq descre (concat "^\\*\\{" (number-to-string
  6759. (if org-odd-levels-only
  6760. (1- (* 2 (cdr desc)))
  6761. (cdr desc)))
  6762. "\\}[ \t]")))
  6763. ((eq (car desc) :maxlevel)
  6764. (setq fast-path-p t)
  6765. (setq descre (concat "^\\*\\{1," (number-to-string
  6766. (if org-odd-levels-only
  6767. (1- (* 2 (cdr desc)))
  6768. (cdr desc)))
  6769. "\\}[ \t]")))
  6770. (t (error "Bad refiling target description %s" desc)))
  6771. (while (setq f (pop files))
  6772. (save-excursion
  6773. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6774. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6775. (setq f (expand-file-name f))
  6776. (save-excursion
  6777. (save-restriction
  6778. (widen)
  6779. (goto-char (point-min))
  6780. (while (re-search-forward descre nil t)
  6781. (goto-char (point-at-bol))
  6782. (when (looking-at org-complex-heading-regexp)
  6783. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6784. txt (org-link-display-format (match-string 4))
  6785. re (concat "^" (regexp-quote
  6786. (buffer-substring (match-beginning 1)
  6787. (match-end 4)))))
  6788. (if (match-end 5) (setq re (concat re "[ \t]+"
  6789. (regexp-quote
  6790. (match-string 5)))))
  6791. (setq re (concat re "[ \t]*$"))
  6792. (when org-refile-use-outline-path
  6793. (setq txt (mapconcat 'org-protect-slash
  6794. (append
  6795. (if (eq org-refile-use-outline-path 'file)
  6796. (list (file-name-nondirectory
  6797. (buffer-file-name (buffer-base-buffer))))
  6798. (if (eq org-refile-use-outline-path 'full-file-path)
  6799. (list (buffer-file-name (buffer-base-buffer)))))
  6800. (org-get-outline-path fast-path-p level txt)
  6801. (list txt))
  6802. "/")))
  6803. (push (list txt f re (point)) targets))
  6804. (goto-char (point-at-eol))))))))
  6805. (message "Getting targets...done")
  6806. (nreverse targets))))
  6807. (defun org-protect-slash (s)
  6808. (while (string-match "/" s)
  6809. (setq s (replace-match "\\" t t s)))
  6810. s)
  6811. (defvar org-olpa (make-vector 20 nil))
  6812. (defun org-get-outline-path (&optional fastp level heading)
  6813. "Return the outline path to the current entry, as a list."
  6814. (if fastp
  6815. (progn
  6816. (if (> level 19)
  6817. (error "Outline path failure, more than 19 levels."))
  6818. (loop for i from level upto 19 do
  6819. (aset org-olpa i nil))
  6820. (prog1
  6821. (delq nil (append org-olpa nil))
  6822. (aset org-olpa level heading)))
  6823. (let (rtn)
  6824. (save-excursion
  6825. (while (org-up-heading-safe)
  6826. (when (looking-at org-complex-heading-regexp)
  6827. (push (org-match-string-no-properties 4) rtn)))
  6828. rtn))))
  6829. (defvar org-refile-history nil
  6830. "History for refiling operations.")
  6831. (defun org-refile (&optional goto default-buffer)
  6832. "Move the entry at point to another heading.
  6833. The list of target headings is compiled using the information in
  6834. `org-refile-targets', which see. This list is created before each use
  6835. and will therefore always be up-to-date.
  6836. At the target location, the entry is filed as a subitem of the target heading.
  6837. Depending on `org-reverse-note-order', the new subitem will either be the
  6838. first or the last subitem.
  6839. If there is an active region, all entries in that region will be moved.
  6840. However, the region must fulfil the requirement that the first heading
  6841. is the first one sets the top-level of the moved text - at most siblings
  6842. below it are allowed.
  6843. With prefix arg GOTO, the command will only visit the target location,
  6844. not actually move anything.
  6845. With a double prefix `C-u C-u', go to the location where the last refiling
  6846. operation has put the subtree."
  6847. (interactive "P")
  6848. (let* ((cbuf (current-buffer))
  6849. (regionp (org-region-active-p))
  6850. (region-start (and regionp (region-beginning)))
  6851. (region-end (and regionp (region-end)))
  6852. (region-length (and regionp (- region-end region-start)))
  6853. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6854. pos it nbuf file re level reversed)
  6855. (when regionp (goto-char region-start)
  6856. (unless (org-kill-is-subtree-p
  6857. (buffer-substring region-start region-end))
  6858. (error "The region is not a (sequence of) subtree(s)")))
  6859. (if (equal goto '(16))
  6860. (org-refile-goto-last-stored)
  6861. (when (setq it (org-refile-get-location
  6862. (if goto "Goto: " "Refile to: ") default-buffer))
  6863. (setq file (nth 1 it)
  6864. re (nth 2 it)
  6865. pos (nth 3 it))
  6866. (if (and (equal (buffer-file-name) file)
  6867. (if regionp
  6868. (and (>= pos region-start)
  6869. (<= pos region-end))
  6870. (and (>= pos (point))
  6871. (< pos (save-excursion
  6872. (org-end-of-subtree t t))))))
  6873. (error "Cannot refile to position inside the tree or region"))
  6874. (setq nbuf (or (find-buffer-visiting file)
  6875. (find-file-noselect file)))
  6876. (if goto
  6877. (progn
  6878. (switch-to-buffer nbuf)
  6879. (goto-char pos)
  6880. (org-show-context 'org-goto))
  6881. (if regionp
  6882. (progn
  6883. (kill-new (buffer-substring region-start region-end))
  6884. (org-save-markers-in-region region-start region-end))
  6885. (org-copy-subtree 1 nil t))
  6886. (save-excursion
  6887. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6888. (find-file-noselect file))))
  6889. (setq reversed (org-notes-order-reversed-p))
  6890. (save-excursion
  6891. (save-restriction
  6892. (widen)
  6893. (goto-char pos)
  6894. (looking-at outline-regexp)
  6895. (setq level (org-get-valid-level (funcall outline-level) 1))
  6896. (goto-char
  6897. (if reversed
  6898. (or (outline-next-heading) (point-max))
  6899. (or (save-excursion (outline-get-next-sibling))
  6900. (org-end-of-subtree t t)
  6901. (point-max))))
  6902. (if (not (bolp)) (newline))
  6903. (bookmark-set "org-refile-last-stored")
  6904. (org-paste-subtree level))))
  6905. (if regionp
  6906. (delete-region (point) (+ (point) region-length))
  6907. (org-cut-subtree))
  6908. (setq org-markers-to-move nil)
  6909. (message "Refiled to \"%s\"" (car it)))))))
  6910. (defun org-refile-goto-last-stored ()
  6911. "Go to the location where the last refile was stored."
  6912. (interactive)
  6913. (bookmark-jump "org-refile-last-stored")
  6914. (message "This is the location of the last refile"))
  6915. (defun org-refile-get-location (&optional prompt default-buffer)
  6916. "Prompt the user for a refile location, using PROMPT."
  6917. (let ((org-refile-targets org-refile-targets)
  6918. (org-refile-use-outline-path org-refile-use-outline-path))
  6919. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6920. (unless org-refile-target-table
  6921. (error "No refile targets"))
  6922. (let* ((cbuf (current-buffer))
  6923. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  6924. (cfunc (if (and org-refile-use-outline-path
  6925. org-outline-path-complete-in-steps)
  6926. 'org-olpath-completing-read
  6927. 'org-ido-completing-read))
  6928. (extra (if org-refile-use-outline-path "/" ""))
  6929. (filename (and cfn (expand-file-name cfn)))
  6930. (tbl (mapcar
  6931. (lambda (x)
  6932. (if (not (equal filename (nth 1 x)))
  6933. (cons (concat (car x) extra " ("
  6934. (file-name-nondirectory (nth 1 x)) ")")
  6935. (cdr x))
  6936. (cons (concat (car x) extra) (cdr x))))
  6937. org-refile-target-table))
  6938. (completion-ignore-case t))
  6939. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6940. tbl)))
  6941. (defun org-olpath-completing-read (prompt collection &rest args)
  6942. "Read an outline path like a file name."
  6943. (let ((thetable collection))
  6944. (apply
  6945. 'org-ido-completing-read prompt
  6946. (lambda (string predicate &optional flag)
  6947. (let (rtn r s f (l (length string)))
  6948. (cond
  6949. ((eq flag nil)
  6950. ;; try completion
  6951. (try-completion string thetable))
  6952. ((eq flag t)
  6953. ;; all-completions
  6954. (setq rtn (all-completions string thetable predicate))
  6955. (mapcar
  6956. (lambda (x)
  6957. (setq r (substring x l))
  6958. (if (string-match " ([^)]*)$" x)
  6959. (setq f (match-string 0 x))
  6960. (setq f ""))
  6961. (if (string-match "/" r)
  6962. (concat string (substring r 0 (match-end 0)) f)
  6963. x))
  6964. rtn))
  6965. ((eq flag 'lambda)
  6966. ;; exact match?
  6967. (assoc string thetable)))
  6968. ))
  6969. args)))
  6970. ;;;; Dynamic blocks
  6971. (defun org-find-dblock (name)
  6972. "Find the first dynamic block with name NAME in the buffer.
  6973. If not found, stay at current position and return nil."
  6974. (let (pos)
  6975. (save-excursion
  6976. (goto-char (point-min))
  6977. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6978. nil t)
  6979. (match-beginning 0))))
  6980. (if pos (goto-char pos))
  6981. pos))
  6982. (defconst org-dblock-start-re
  6983. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6984. "Matches the startline of a dynamic block, with parameters.")
  6985. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6986. "Matches the end of a dynamic block.")
  6987. (defun org-create-dblock (plist)
  6988. "Create a dynamic block section, with parameters taken from PLIST.
  6989. PLIST must contain a :name entry which is used as name of the block."
  6990. (unless (bolp) (newline))
  6991. (let ((name (plist-get plist :name)))
  6992. (insert "#+BEGIN: " name)
  6993. (while plist
  6994. (if (eq (car plist) :name)
  6995. (setq plist (cddr plist))
  6996. (insert " " (prin1-to-string (pop plist)))))
  6997. (insert "\n\n#+END:\n")
  6998. (beginning-of-line -2)))
  6999. (defun org-prepare-dblock ()
  7000. "Prepare dynamic block for refresh.
  7001. This empties the block, puts the cursor at the insert position and returns
  7002. the property list including an extra property :name with the block name."
  7003. (unless (looking-at org-dblock-start-re)
  7004. (error "Not at a dynamic block"))
  7005. (let* ((begdel (1+ (match-end 0)))
  7006. (name (org-no-properties (match-string 1)))
  7007. (params (append (list :name name)
  7008. (read (concat "(" (match-string 3) ")")))))
  7009. (unless (re-search-forward org-dblock-end-re nil t)
  7010. (error "Dynamic block not terminated"))
  7011. (setq params
  7012. (append params
  7013. (list :content (buffer-substring
  7014. begdel (match-beginning 0)))))
  7015. (delete-region begdel (match-beginning 0))
  7016. (goto-char begdel)
  7017. (open-line 1)
  7018. params))
  7019. (defun org-map-dblocks (&optional command)
  7020. "Apply COMMAND to all dynamic blocks in the current buffer.
  7021. If COMMAND is not given, use `org-update-dblock'."
  7022. (let ((cmd (or command 'org-update-dblock))
  7023. pos)
  7024. (save-excursion
  7025. (goto-char (point-min))
  7026. (while (re-search-forward org-dblock-start-re nil t)
  7027. (goto-char (setq pos (match-beginning 0)))
  7028. (condition-case nil
  7029. (funcall cmd)
  7030. (error (message "Error during update of dynamic block")))
  7031. (goto-char pos)
  7032. (unless (re-search-forward org-dblock-end-re nil t)
  7033. (error "Dynamic block not terminated"))))))
  7034. (defun org-dblock-update (&optional arg)
  7035. "User command for updating dynamic blocks.
  7036. Update the dynamic block at point. With prefix ARG, update all dynamic
  7037. blocks in the buffer."
  7038. (interactive "P")
  7039. (if arg
  7040. (org-update-all-dblocks)
  7041. (or (looking-at org-dblock-start-re)
  7042. (org-beginning-of-dblock))
  7043. (org-update-dblock)))
  7044. (defun org-update-dblock ()
  7045. "Update the dynamic block at point
  7046. This means to empty the block, parse for parameters and then call
  7047. the correct writing function."
  7048. (save-window-excursion
  7049. (let* ((pos (point))
  7050. (line (org-current-line))
  7051. (params (org-prepare-dblock))
  7052. (name (plist-get params :name))
  7053. (cmd (intern (concat "org-dblock-write:" name))))
  7054. (message "Updating dynamic block `%s' at line %d..." name line)
  7055. (funcall cmd params)
  7056. (message "Updating dynamic block `%s' at line %d...done" name line)
  7057. (goto-char pos))))
  7058. (defun org-beginning-of-dblock ()
  7059. "Find the beginning of the dynamic block at point.
  7060. Error if there is no such block at point."
  7061. (let ((pos (point))
  7062. beg)
  7063. (end-of-line 1)
  7064. (if (and (re-search-backward org-dblock-start-re nil t)
  7065. (setq beg (match-beginning 0))
  7066. (re-search-forward org-dblock-end-re nil t)
  7067. (> (match-end 0) pos))
  7068. (goto-char beg)
  7069. (goto-char pos)
  7070. (error "Not in a dynamic block"))))
  7071. (defun org-update-all-dblocks ()
  7072. "Update all dynamic blocks in the buffer.
  7073. This function can be used in a hook."
  7074. (when (org-mode-p)
  7075. (org-map-dblocks 'org-update-dblock)))
  7076. ;;;; Completion
  7077. (defconst org-additional-option-like-keywords
  7078. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7079. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7080. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7081. "BEGIN_QUOTE" "END_QUOTE"
  7082. "BEGIN_VERSE" "END_VERSE"
  7083. "BEGIN_SRC" "END_SRC"
  7084. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7085. (defcustom org-structure-template-alist
  7086. '(
  7087. ("s" "#+begin_src ?\n\n#+end_src"
  7088. "<src lang=\"?\">\n\n</src>")
  7089. ("e" "#+begin_example\n?\n#+end_example"
  7090. "<example>\n?\n</example>")
  7091. ("q" "#+begin_quote\n?\n#+end_quote"
  7092. "<quote>\n?\n</quote>")
  7093. ("v" "#+begin_verse\n?\n#+end_verse"
  7094. "<verse>\n?\n/verse>")
  7095. ("l" "#+begin_latex\n?\n#+end_latex"
  7096. "<literal style=\"latex\">\n?\n</literal>")
  7097. ("L" "#+latex: "
  7098. "<literal style=\"latex\">?</literal>")
  7099. ("h" "#+begin_html\n?\n#+end_html"
  7100. "<literal style=\"html\">\n?\n</literal>")
  7101. ("H" "#+html: "
  7102. "<literal style=\"html\">?</literal>")
  7103. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7104. ("A" "#+ascii: ")
  7105. ("i" "#+include %file ?"
  7106. "<include file=%file markup=\"?\">")
  7107. )
  7108. "Structure completion elements.
  7109. This is a list of abbreviation keys and values. The value gets inserted
  7110. it you type @samp{.} followed by the key and then the completion key,
  7111. usually `M-TAB'. %file will be replaced by a file name after prompting
  7112. for the file using completion.
  7113. There are two templates for each key, the first uses the original Org syntax,
  7114. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7115. the default when the /org-mtags.el/ module has been loaded. See also the
  7116. variable `org-mtags-prefer-muse-templates'.
  7117. This is an experimental feature, it is undecided if it is going to stay in."
  7118. :group 'org-completion
  7119. :type '(repeat
  7120. (string :tag "Key")
  7121. (string :tag "Template")
  7122. (string :tag "Muse Template")))
  7123. (defun org-try-structure-completion ()
  7124. "Try to complete a structure template before point.
  7125. This looks for strings like \"<e\" on an otherwise empty line and
  7126. expands them."
  7127. (let ((l (buffer-substring (point-at-bol) (point)))
  7128. a)
  7129. (when (and (looking-at "[ \t]*$")
  7130. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7131. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7132. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7133. (match-beginning 1)) a)
  7134. t)))
  7135. (defun org-complete-expand-structure-template (start cell)
  7136. "Expand a structure template."
  7137. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7138. (rpl (nth (if musep 2 1) cell)))
  7139. (delete-region start (point))
  7140. (when (string-match "\\`#\\+" rpl)
  7141. (cond
  7142. ((bolp))
  7143. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7144. (delete-region (point-at-bol) (point)))
  7145. (t (newline))))
  7146. (setq start (point))
  7147. (if (string-match "%file" rpl)
  7148. (setq rpl (replace-match
  7149. (concat
  7150. "\""
  7151. (save-match-data
  7152. (abbreviate-file-name (read-file-name "Include file: ")))
  7153. "\"")
  7154. t t rpl)))
  7155. (insert rpl)
  7156. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7157. (defun org-complete (&optional arg)
  7158. "Perform completion on word at point.
  7159. At the beginning of a headline, this completes TODO keywords as given in
  7160. `org-todo-keywords'.
  7161. If the current word is preceded by a backslash, completes the TeX symbols
  7162. that are supported for HTML support.
  7163. If the current word is preceded by \"#+\", completes special words for
  7164. setting file options.
  7165. In the line after \"#+STARTUP:, complete valid keywords.\"
  7166. At all other locations, this simply calls the value of
  7167. `org-completion-fallback-command'."
  7168. (interactive "P")
  7169. (org-without-partial-completion
  7170. (catch 'exit
  7171. (let* ((a nil)
  7172. (end (point))
  7173. (beg1 (save-excursion
  7174. (skip-chars-backward (org-re "[:alnum:]_@"))
  7175. (point)))
  7176. (beg (save-excursion
  7177. (skip-chars-backward "a-zA-Z0-9_:$")
  7178. (point)))
  7179. (confirm (lambda (x) (stringp (car x))))
  7180. (searchhead (equal (char-before beg) ?*))
  7181. (struct
  7182. (when (and (member (char-before beg1) '(?. ?<))
  7183. (setq a (assoc (buffer-substring beg1 (point))
  7184. org-structure-template-alist)))
  7185. (org-complete-expand-structure-template (1- beg1) a)
  7186. (throw 'exit t)))
  7187. (tag (and (equal (char-before beg1) ?:)
  7188. (equal (char-after (point-at-bol)) ?*)))
  7189. (prop (and (equal (char-before beg1) ?:)
  7190. (not (equal (char-after (point-at-bol)) ?*))))
  7191. (texp (equal (char-before beg) ?\\))
  7192. (link (equal (char-before beg) ?\[))
  7193. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7194. beg)
  7195. "#+"))
  7196. (startup (string-match "^#\\+STARTUP:.*"
  7197. (buffer-substring (point-at-bol) (point))))
  7198. (completion-ignore-case opt)
  7199. (type nil)
  7200. (tbl nil)
  7201. (table (cond
  7202. (opt
  7203. (setq type :opt)
  7204. (require 'org-exp)
  7205. (append
  7206. (mapcar
  7207. (lambda (x)
  7208. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7209. (cons (match-string 2 x) (match-string 1 x)))
  7210. (org-split-string (org-get-current-options) "\n"))
  7211. (mapcar 'list org-additional-option-like-keywords)))
  7212. (startup
  7213. (setq type :startup)
  7214. org-startup-options)
  7215. (link (append org-link-abbrev-alist-local
  7216. org-link-abbrev-alist))
  7217. (texp
  7218. (setq type :tex)
  7219. org-html-entities)
  7220. ((string-match "\\`\\*+[ \t]+\\'"
  7221. (buffer-substring (point-at-bol) beg))
  7222. (setq type :todo)
  7223. (mapcar 'list org-todo-keywords-1))
  7224. (searchhead
  7225. (setq type :searchhead)
  7226. (save-excursion
  7227. (goto-char (point-min))
  7228. (while (re-search-forward org-todo-line-regexp nil t)
  7229. (push (list
  7230. (org-make-org-heading-search-string
  7231. (match-string 3) t))
  7232. tbl)))
  7233. tbl)
  7234. (tag (setq type :tag beg beg1)
  7235. (or org-tag-alist (org-get-buffer-tags)))
  7236. (prop (setq type :prop beg beg1)
  7237. (mapcar 'list (org-buffer-property-keys nil t t)))
  7238. (t (progn
  7239. (call-interactively org-completion-fallback-command)
  7240. (throw 'exit nil)))))
  7241. (pattern (buffer-substring-no-properties beg end))
  7242. (completion (try-completion pattern table confirm)))
  7243. (cond ((eq completion t)
  7244. (if (not (assoc (upcase pattern) table))
  7245. (message "Already complete")
  7246. (if (and (equal type :opt)
  7247. (not (member (car (assoc (upcase pattern) table))
  7248. org-additional-option-like-keywords)))
  7249. (insert (substring (cdr (assoc (upcase pattern) table))
  7250. (length pattern)))
  7251. (if (memq type '(:tag :prop)) (insert ":")))))
  7252. ((null completion)
  7253. (message "Can't find completion for \"%s\"" pattern)
  7254. (ding))
  7255. ((not (string= pattern completion))
  7256. (delete-region beg end)
  7257. (if (string-match " +$" completion)
  7258. (setq completion (replace-match "" t t completion)))
  7259. (insert completion)
  7260. (if (get-buffer-window "*Completions*")
  7261. (delete-window (get-buffer-window "*Completions*")))
  7262. (if (assoc completion table)
  7263. (if (eq type :todo) (insert " ")
  7264. (if (memq type '(:tag :prop)) (insert ":"))))
  7265. (if (and (equal type :opt) (assoc completion table))
  7266. (message "%s" (substitute-command-keys
  7267. "Press \\[org-complete] again to insert example settings"))))
  7268. (t
  7269. (message "Making completion list...")
  7270. (let ((list (sort (all-completions pattern table confirm)
  7271. 'string<)))
  7272. (with-output-to-temp-buffer "*Completions*"
  7273. (condition-case nil
  7274. ;; Protection needed for XEmacs and emacs 21
  7275. (display-completion-list list pattern)
  7276. (error (display-completion-list list)))))
  7277. (message "Making completion list...%s" "done")))))))
  7278. ;;;; TODO, DEADLINE, Comments
  7279. (defun org-toggle-comment ()
  7280. "Change the COMMENT state of an entry."
  7281. (interactive)
  7282. (save-excursion
  7283. (org-back-to-heading)
  7284. (let (case-fold-search)
  7285. (if (looking-at (concat outline-regexp
  7286. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7287. (replace-match "" t t nil 1)
  7288. (if (looking-at outline-regexp)
  7289. (progn
  7290. (goto-char (match-end 0))
  7291. (insert org-comment-string " ")))))))
  7292. (defvar org-last-todo-state-is-todo nil
  7293. "This is non-nil when the last TODO state change led to a TODO state.
  7294. If the last change removed the TODO tag or switched to DONE, then
  7295. this is nil.")
  7296. (defvar org-setting-tags nil) ; dynamically skipped
  7297. (defun org-parse-local-options (string var)
  7298. "Parse STRING for startup setting relevant for variable VAR."
  7299. (let ((rtn (symbol-value var))
  7300. e opts)
  7301. (save-match-data
  7302. (if (or (not string) (not (string-match "\\S-" string)))
  7303. rtn
  7304. (setq opts (delq nil (mapcar (lambda (x)
  7305. (setq e (assoc x org-startup-options))
  7306. (if (eq (nth 1 e) var) e nil))
  7307. (org-split-string string "[ \t]+"))))
  7308. (if (not opts)
  7309. rtn
  7310. (setq rtn nil)
  7311. (while (setq e (pop opts))
  7312. (if (not (nth 3 e))
  7313. (setq rtn (nth 2 e))
  7314. (if (not (listp rtn)) (setq rtn nil))
  7315. (push (nth 2 e) rtn)))
  7316. rtn)))))
  7317. (defvar org-blocker-hook nil
  7318. "Hook for functions that are allowed to block a state change.
  7319. Each function gets as its single argument a property list, see
  7320. `org-trigger-hook' for more information about this list.
  7321. If any of the functions in this hook returns nil, the state change
  7322. is blocked.")
  7323. (defvar org-trigger-hook nil
  7324. "Hook for functions that are triggered by a state change.
  7325. Each function gets as its single argument a property list with at least
  7326. the following elements:
  7327. (:type type-of-change :position pos-at-entry-start
  7328. :from old-state :to new-state)
  7329. Depending on the type, more properties may be present.
  7330. This mechanism is currently implemented for:
  7331. TODO state changes
  7332. ------------------
  7333. :type todo-state-change
  7334. :from previous state (keyword as a string), or nil
  7335. :to new state (keyword as a string), or nil")
  7336. (defvar org-agenda-headline-snapshot-before-repeat)
  7337. (defun org-todo (&optional arg)
  7338. "Change the TODO state of an item.
  7339. The state of an item is given by a keyword at the start of the heading,
  7340. like
  7341. *** TODO Write paper
  7342. *** DONE Call mom
  7343. The different keywords are specified in the variable `org-todo-keywords'.
  7344. By default the available states are \"TODO\" and \"DONE\".
  7345. So for this example: when the item starts with TODO, it is changed to DONE.
  7346. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7347. DONE are present, add TODO at the beginning of the heading.
  7348. With C-u prefix arg, use completion to determine the new state.
  7349. With numeric prefix arg, switch to that state.
  7350. For calling through lisp, arg is also interpreted in the following way:
  7351. 'none -> empty state
  7352. \"\"(empty string) -> switch to empty state
  7353. 'done -> switch to DONE
  7354. 'nextset -> switch to the next set of keywords
  7355. 'previousset -> switch to the previous set of keywords
  7356. \"WAITING\" -> switch to the specified keyword, but only if it
  7357. really is a member of `org-todo-keywords'."
  7358. (interactive "P")
  7359. (save-excursion
  7360. (catch 'exit
  7361. (org-back-to-heading)
  7362. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7363. (or (looking-at (concat " +" org-todo-regexp " *"))
  7364. (looking-at " *"))
  7365. (let* ((match-data (match-data))
  7366. (startpos (point-at-bol))
  7367. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7368. (org-log-done org-log-done)
  7369. (org-log-repeat org-log-repeat)
  7370. (org-todo-log-states org-todo-log-states)
  7371. (this (match-string 1))
  7372. (hl-pos (match-beginning 0))
  7373. (head (org-get-todo-sequence-head this))
  7374. (ass (assoc head org-todo-kwd-alist))
  7375. (interpret (nth 1 ass))
  7376. (done-word (nth 3 ass))
  7377. (final-done-word (nth 4 ass))
  7378. (last-state (or this ""))
  7379. (completion-ignore-case t)
  7380. (member (member this org-todo-keywords-1))
  7381. (tail (cdr member))
  7382. (state (cond
  7383. ((and org-todo-key-trigger
  7384. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7385. (and (not arg) org-use-fast-todo-selection
  7386. (not (eq org-use-fast-todo-selection 'prefix)))))
  7387. ;; Use fast selection
  7388. (org-fast-todo-selection))
  7389. ((and (equal arg '(4))
  7390. (or (not org-use-fast-todo-selection)
  7391. (not org-todo-key-trigger)))
  7392. ;; Read a state with completion
  7393. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7394. org-todo-keywords-1)
  7395. nil t))
  7396. ((eq arg 'right)
  7397. (if this
  7398. (if tail (car tail) nil)
  7399. (car org-todo-keywords-1)))
  7400. ((eq arg 'left)
  7401. (if (equal member org-todo-keywords-1)
  7402. nil
  7403. (if this
  7404. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7405. org-todo-keywords-1)
  7406. (org-last org-todo-keywords-1))))
  7407. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7408. (setq arg nil))) ; hack to fall back to cycling
  7409. (arg
  7410. ;; user or caller requests a specific state
  7411. (cond
  7412. ((equal arg "") nil)
  7413. ((eq arg 'none) nil)
  7414. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7415. ((eq arg 'nextset)
  7416. (or (car (cdr (member head org-todo-heads)))
  7417. (car org-todo-heads)))
  7418. ((eq arg 'previousset)
  7419. (let ((org-todo-heads (reverse org-todo-heads)))
  7420. (or (car (cdr (member head org-todo-heads)))
  7421. (car org-todo-heads))))
  7422. ((car (member arg org-todo-keywords-1)))
  7423. ((nth (1- (prefix-numeric-value arg))
  7424. org-todo-keywords-1))))
  7425. ((null member) (or head (car org-todo-keywords-1)))
  7426. ((equal this final-done-word) nil) ;; -> make empty
  7427. ((null tail) nil) ;; -> first entry
  7428. ((eq interpret 'sequence)
  7429. (car tail))
  7430. ((memq interpret '(type priority))
  7431. (if (eq this-command last-command)
  7432. (car tail)
  7433. (if (> (length tail) 0)
  7434. (or done-word (car org-done-keywords))
  7435. nil)))
  7436. (t nil)))
  7437. (next (if state (concat " " state " ") " "))
  7438. (change-plist (list :type 'todo-state-change :from this :to state
  7439. :position startpos))
  7440. dolog now-done-p)
  7441. (when org-blocker-hook
  7442. (unless (save-excursion
  7443. (save-match-data
  7444. (run-hook-with-args-until-failure
  7445. 'org-blocker-hook change-plist)))
  7446. (if (interactive-p)
  7447. (error "TODO state change from %s to %s blocked" this state)
  7448. ;; fail silently
  7449. (message "TODO state change from %s to %s blocked" this state)
  7450. (throw 'exit nil))))
  7451. (store-match-data match-data)
  7452. (replace-match next t t)
  7453. (unless (pos-visible-in-window-p hl-pos)
  7454. (message "TODO state changed to %s" (org-trim next)))
  7455. (unless head
  7456. (setq head (org-get-todo-sequence-head state)
  7457. ass (assoc head org-todo-kwd-alist)
  7458. interpret (nth 1 ass)
  7459. done-word (nth 3 ass)
  7460. final-done-word (nth 4 ass)))
  7461. (when (memq arg '(nextset previousset))
  7462. (message "Keyword-Set %d/%d: %s"
  7463. (- (length org-todo-sets) -1
  7464. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7465. (length org-todo-sets)
  7466. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7467. (setq org-last-todo-state-is-todo
  7468. (not (member state org-done-keywords)))
  7469. (setq now-done-p (and (member state org-done-keywords)
  7470. (not (member this org-done-keywords))))
  7471. (and logging (org-local-logging logging))
  7472. (when (and (or org-todo-log-states org-log-done)
  7473. (not (memq arg '(nextset previousset))))
  7474. ;; we need to look at recording a time and note
  7475. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7476. (nth 2 (assoc this org-todo-log-states))))
  7477. (when (and state
  7478. (member state org-not-done-keywords)
  7479. (not (member this org-not-done-keywords)))
  7480. ;; This is now a todo state and was not one before
  7481. ;; If there was a CLOSED time stamp, get rid of it.
  7482. (org-add-planning-info nil nil 'closed))
  7483. (when (and now-done-p org-log-done)
  7484. ;; It is now done, and it was not done before
  7485. (org-add-planning-info 'closed (org-current-time))
  7486. (if (and (not dolog) (eq 'note org-log-done))
  7487. (org-add-log-setup 'done state 'findpos 'note)))
  7488. (when (and state dolog)
  7489. ;; This is a non-nil state, and we need to log it
  7490. (org-add-log-setup 'state state 'findpos dolog)))
  7491. ;; Fixup tag positioning
  7492. (org-todo-trigger-tag-changes state)
  7493. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7494. (when org-provide-todo-statistics
  7495. (org-update-parent-todo-statistics))
  7496. (run-hooks 'org-after-todo-state-change-hook)
  7497. (if (and arg (not (member state org-done-keywords)))
  7498. (setq head (org-get-todo-sequence-head state)))
  7499. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7500. ;; Do we need to trigger a repeat?
  7501. (when now-done-p
  7502. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7503. ;; This is for the agenda, take a snapshot of the headline.
  7504. (save-match-data
  7505. (setq org-agenda-headline-snapshot-before-repeat
  7506. (org-get-heading))))
  7507. (org-auto-repeat-maybe state))
  7508. ;; Fixup cursor location if close to the keyword
  7509. (if (and (outline-on-heading-p)
  7510. (not (bolp))
  7511. (save-excursion (beginning-of-line 1)
  7512. (looking-at org-todo-line-regexp))
  7513. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7514. (progn
  7515. (goto-char (or (match-end 2) (match-end 1)))
  7516. (just-one-space)))
  7517. (when org-trigger-hook
  7518. (save-excursion
  7519. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7520. (defun org-update-parent-todo-statistics ()
  7521. "Update any statistics cookie in the parent of the current headline."
  7522. (interactive)
  7523. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7524. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7525. (catch 'exit
  7526. (save-excursion
  7527. (setq level (org-up-heading-safe))
  7528. (unless (and level
  7529. (re-search-forward box-re (point-at-eol) t))
  7530. (throw 'exit nil))
  7531. (setq is-percent (match-end 2))
  7532. (save-match-data
  7533. (unless (outline-next-heading) (throw 'exit nil))
  7534. (while (looking-at org-todo-line-regexp)
  7535. (setq kwd (match-string 2))
  7536. (and kwd (setq cnt-all (1+ cnt-all)))
  7537. (and (member kwd org-done-keywords)
  7538. (setq cnt-done (1+ cnt-done)))
  7539. (condition-case nil
  7540. (org-forward-same-level 1)
  7541. (error (end-of-line 1)))))
  7542. (replace-match
  7543. (if is-percent
  7544. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7545. (format "[%d/%d]" cnt-done cnt-all)))
  7546. (run-hook-with-args 'org-after-todo-statistics-hook
  7547. cnt-done (- cnt-all cnt-done))))))
  7548. (defvar org-after-todo-statistics-hook nil
  7549. "Hook that is called after a TODO statistics cookie has been updated.
  7550. Each function is called with two arguments: the number of not-done entries
  7551. and the number of done entries.
  7552. For example, the following function, when added to this hook, will switch
  7553. an entry to DONE when all children are done, and back to TODO when new
  7554. entries are set to a TODO status. Note that this hook is only called
  7555. when there is a statistics cookie in the headline!
  7556. (defun org-summary-todo (n-done n-not-done)
  7557. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7558. (let (org-log-done org-log-states) ; turn off logging
  7559. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7560. ")
  7561. (defun org-todo-trigger-tag-changes (state)
  7562. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7563. (let ((l org-todo-state-tags-triggers)
  7564. changes)
  7565. (when (or (not state) (equal state ""))
  7566. (setq changes (append changes (cdr (assoc "" l)))))
  7567. (when (and (stringp state) (> (length state) 0))
  7568. (setq changes (append changes (cdr (assoc state l)))))
  7569. (when (member state org-not-done-keywords)
  7570. (setq changes (append changes (cdr (assoc 'todo l)))))
  7571. (when (member state org-done-keywords)
  7572. (setq changes (append changes (cdr (assoc 'done l)))))
  7573. (dolist (c changes)
  7574. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7575. (defun org-local-logging (value)
  7576. "Get logging settings from a property VALUE."
  7577. (let* (words w a)
  7578. ;; directly set the variables, they are already local.
  7579. (setq org-log-done nil
  7580. org-log-repeat nil
  7581. org-todo-log-states nil)
  7582. (setq words (org-split-string value))
  7583. (while (setq w (pop words))
  7584. (cond
  7585. ((setq a (assoc w org-startup-options))
  7586. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7587. (set (nth 1 a) (nth 2 a))))
  7588. ((setq a (org-extract-log-state-settings w))
  7589. (and (member (car a) org-todo-keywords-1)
  7590. (push a org-todo-log-states)))))))
  7591. (defun org-get-todo-sequence-head (kwd)
  7592. "Return the head of the TODO sequence to which KWD belongs.
  7593. If KWD is not set, check if there is a text property remembering the
  7594. right sequence."
  7595. (let (p)
  7596. (cond
  7597. ((not kwd)
  7598. (or (get-text-property (point-at-bol) 'org-todo-head)
  7599. (progn
  7600. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7601. nil (point-at-eol)))
  7602. (get-text-property p 'org-todo-head))))
  7603. ((not (member kwd org-todo-keywords-1))
  7604. (car org-todo-keywords-1))
  7605. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7606. (defun org-fast-todo-selection ()
  7607. "Fast TODO keyword selection with single keys.
  7608. Returns the new TODO keyword, or nil if no state change should occur."
  7609. (let* ((fulltable org-todo-key-alist)
  7610. (done-keywords org-done-keywords) ;; needed for the faces.
  7611. (maxlen (apply 'max (mapcar
  7612. (lambda (x)
  7613. (if (stringp (car x)) (string-width (car x)) 0))
  7614. fulltable)))
  7615. (expert nil)
  7616. (fwidth (+ maxlen 3 1 3))
  7617. (ncol (/ (- (window-width) 4) fwidth))
  7618. tg cnt e c tbl
  7619. groups ingroup)
  7620. (save-window-excursion
  7621. (if expert
  7622. (set-buffer (get-buffer-create " *Org todo*"))
  7623. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7624. (erase-buffer)
  7625. (org-set-local 'org-done-keywords done-keywords)
  7626. (setq tbl fulltable cnt 0)
  7627. (while (setq e (pop tbl))
  7628. (cond
  7629. ((equal e '(:startgroup))
  7630. (push '() groups) (setq ingroup t)
  7631. (when (not (= cnt 0))
  7632. (setq cnt 0)
  7633. (insert "\n"))
  7634. (insert "{ "))
  7635. ((equal e '(:endgroup))
  7636. (setq ingroup nil cnt 0)
  7637. (insert "}\n"))
  7638. (t
  7639. (setq tg (car e) c (cdr e))
  7640. (if ingroup (push tg (car groups)))
  7641. (setq tg (org-add-props tg nil 'face
  7642. (org-get-todo-face tg)))
  7643. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7644. (insert "[" c "] " tg (make-string
  7645. (- fwidth 4 (length tg)) ?\ ))
  7646. (when (= (setq cnt (1+ cnt)) ncol)
  7647. (insert "\n")
  7648. (if ingroup (insert " "))
  7649. (setq cnt 0)))))
  7650. (insert "\n")
  7651. (goto-char (point-min))
  7652. (if (not expert) (org-fit-window-to-buffer))
  7653. (message "[a-z..]:Set [SPC]:clear")
  7654. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7655. (cond
  7656. ((or (= c ?\C-g)
  7657. (and (= c ?q) (not (rassoc c fulltable))))
  7658. (setq quit-flag t))
  7659. ((= c ?\ ) nil)
  7660. ((setq e (rassoc c fulltable) tg (car e))
  7661. tg)
  7662. (t (setq quit-flag t))))))
  7663. (defun org-entry-is-todo-p ()
  7664. (member (org-get-todo-state) org-not-done-keywords))
  7665. (defun org-entry-is-done-p ()
  7666. (member (org-get-todo-state) org-done-keywords))
  7667. (defun org-get-todo-state ()
  7668. (save-excursion
  7669. (org-back-to-heading t)
  7670. (and (looking-at org-todo-line-regexp)
  7671. (match-end 2)
  7672. (match-string 2))))
  7673. (defun org-at-date-range-p (&optional inactive-ok)
  7674. "Is the cursor inside a date range?"
  7675. (interactive)
  7676. (save-excursion
  7677. (catch 'exit
  7678. (let ((pos (point)))
  7679. (skip-chars-backward "^[<\r\n")
  7680. (skip-chars-backward "<[")
  7681. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7682. (>= (match-end 0) pos)
  7683. (throw 'exit t))
  7684. (skip-chars-backward "^<[\r\n")
  7685. (skip-chars-backward "<[")
  7686. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7687. (>= (match-end 0) pos)
  7688. (throw 'exit t)))
  7689. nil)))
  7690. (defun org-get-repeat ()
  7691. "Check if there is a deadline/schedule with repeater in this entry."
  7692. (save-match-data
  7693. (save-excursion
  7694. (org-back-to-heading t)
  7695. (if (re-search-forward
  7696. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7697. (match-string 1)))))
  7698. (defvar org-last-changed-timestamp)
  7699. (defvar org-last-inserted-timestamp)
  7700. (defvar org-log-post-message)
  7701. (defvar org-log-note-purpose)
  7702. (defvar org-log-note-how)
  7703. (defvar org-log-note-extra)
  7704. (defun org-auto-repeat-maybe (done-word)
  7705. "Check if the current headline contains a repeated deadline/schedule.
  7706. If yes, set TODO state back to what it was and change the base date
  7707. of repeating deadline/scheduled time stamps to new date.
  7708. This function is run automatically after each state change to a DONE state."
  7709. ;; last-state is dynamically scoped into this function
  7710. (let* ((repeat (org-get-repeat))
  7711. (aa (assoc last-state org-todo-kwd-alist))
  7712. (interpret (nth 1 aa))
  7713. (head (nth 2 aa))
  7714. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7715. (msg "Entry repeats: ")
  7716. (org-log-done nil)
  7717. (org-todo-log-states nil)
  7718. (nshiftmax 10) (nshift 0)
  7719. re type n what ts mb0 time)
  7720. (when repeat
  7721. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7722. (org-todo (if (eq interpret 'type) last-state head))
  7723. (when org-log-repeat
  7724. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7725. (memq 'org-add-log-note post-command-hook))
  7726. ;; OK, we are already setup for some record
  7727. (if (eq org-log-repeat 'note)
  7728. ;; make sure we take a note, not only a time stamp
  7729. (setq org-log-note-how 'note))
  7730. ;; Set up for taking a record
  7731. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7732. 'findpos org-log-repeat)))
  7733. (org-back-to-heading t)
  7734. (org-add-planning-info nil nil 'closed)
  7735. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7736. org-deadline-time-regexp "\\)\\|\\("
  7737. org-ts-regexp "\\)"))
  7738. (while (re-search-forward
  7739. re (save-excursion (outline-next-heading) (point)) t)
  7740. (setq type (if (match-end 1) org-scheduled-string
  7741. (if (match-end 3) org-deadline-string "Plain:"))
  7742. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7743. mb0 (match-beginning 0))
  7744. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7745. (setq n (string-to-number (match-string 2 ts))
  7746. what (match-string 3 ts))
  7747. (if (equal what "w") (setq n (* n 7) what "d"))
  7748. ;; Preparation, see if we need to modify the start date for the change
  7749. (when (match-end 1)
  7750. (setq time (save-match-data (org-time-string-to-time ts)))
  7751. (cond
  7752. ((equal (match-string 1 ts) ".")
  7753. ;; Shift starting date to today
  7754. (org-timestamp-change
  7755. (- (time-to-days (current-time)) (time-to-days time))
  7756. 'day))
  7757. ((equal (match-string 1 ts) "+")
  7758. (while (or (= nshift 0)
  7759. (<= (time-to-days time) (time-to-days (current-time))))
  7760. (when (= (incf nshift) nshiftmax)
  7761. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7762. (error "Abort")))
  7763. (org-timestamp-change n (cdr (assoc what whata)))
  7764. (org-at-timestamp-p t)
  7765. (setq ts (match-string 1))
  7766. (setq time (save-match-data (org-time-string-to-time ts))))
  7767. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7768. ;; rematch, so that we have everything in place for the real shift
  7769. (org-at-timestamp-p t)
  7770. (setq ts (match-string 1))
  7771. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7772. (org-timestamp-change n (cdr (assoc what whata)))
  7773. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7774. (setq org-log-post-message msg)
  7775. (message "%s" msg))))
  7776. (defun org-show-todo-tree (arg)
  7777. "Make a compact tree which shows all headlines marked with TODO.
  7778. The tree will show the lines where the regexp matches, and all higher
  7779. headlines above the match.
  7780. With a \\[universal-argument] prefix, also show the DONE entries.
  7781. With a numeric prefix N, construct a sparse tree for the Nth element
  7782. of `org-todo-keywords-1'."
  7783. (interactive "P")
  7784. (let ((case-fold-search nil)
  7785. (kwd-re
  7786. (cond ((null arg) org-not-done-regexp)
  7787. ((equal arg '(4))
  7788. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7789. (mapcar 'list org-todo-keywords-1))))
  7790. (concat "\\("
  7791. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7792. "\\)\\>")))
  7793. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7794. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7795. org-todo-keywords-1)))
  7796. (t (error "Invalid prefix argument: %s" arg)))))
  7797. (message "%d TODO entries found"
  7798. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7799. (defun org-deadline (&optional remove time)
  7800. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7801. With argument REMOVE, remove any deadline from the item.
  7802. When TIME is set, it should be an internal time specification, and the
  7803. scheduling will use the corresponding date."
  7804. (interactive "P")
  7805. (if remove
  7806. (progn
  7807. (org-remove-timestamp-with-keyword org-deadline-string)
  7808. (message "Item no longer has a deadline."))
  7809. (if (org-get-repeat)
  7810. (error "Cannot change deadline on task with repeater, please do that by hand")
  7811. (org-add-planning-info 'deadline time 'closed)
  7812. (message "Deadline on %s" org-last-inserted-timestamp))))
  7813. (defun org-schedule (&optional remove time)
  7814. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7815. With argument REMOVE, remove any scheduling date from the item.
  7816. When TIME is set, it should be an internal time specification, and the
  7817. scheduling will use the corresponding date."
  7818. (interactive "P")
  7819. (if remove
  7820. (progn
  7821. (org-remove-timestamp-with-keyword org-scheduled-string)
  7822. (message "Item is no longer scheduled."))
  7823. (if (org-get-repeat)
  7824. (error "Cannot reschedule task with repeater, please do that by hand")
  7825. (org-add-planning-info 'scheduled time 'closed)
  7826. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7827. (defun org-remove-timestamp-with-keyword (keyword)
  7828. "Remove all time stamps with KEYWORD in the current entry."
  7829. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7830. beg)
  7831. (save-excursion
  7832. (org-back-to-heading t)
  7833. (setq beg (point))
  7834. (org-end-of-subtree t t)
  7835. (while (re-search-backward re beg t)
  7836. (replace-match "")
  7837. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7838. (equal (char-before) ?\ ))
  7839. (backward-delete-char 1)
  7840. (if (string-match "^[ \t]*$" (buffer-substring
  7841. (point-at-bol) (point-at-eol)))
  7842. (delete-region (point-at-bol)
  7843. (min (point-max) (1+ (point-at-eol))))))))))
  7844. (defun org-add-planning-info (what &optional time &rest remove)
  7845. "Insert new timestamp with keyword in the line directly after the headline.
  7846. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7847. If non is given, the user is prompted for a date.
  7848. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7849. be removed."
  7850. (interactive)
  7851. (let (org-time-was-given org-end-time-was-given ts
  7852. end default-time default-input)
  7853. (when (and (not time) (memq what '(scheduled deadline)))
  7854. ;; Try to get a default date/time from existing timestamp
  7855. (save-excursion
  7856. (org-back-to-heading t)
  7857. (setq end (save-excursion (outline-next-heading) (point)))
  7858. (when (re-search-forward (if (eq what 'scheduled)
  7859. org-scheduled-time-regexp
  7860. org-deadline-time-regexp)
  7861. end t)
  7862. (setq ts (match-string 1)
  7863. default-time
  7864. (apply 'encode-time (org-parse-time-string ts))
  7865. default-input (and ts (org-get-compact-tod ts))))))
  7866. (when what
  7867. ;; If necessary, get the time from the user
  7868. (setq time (or time (org-read-date nil 'to-time nil nil
  7869. default-time default-input))))
  7870. (when (and org-insert-labeled-timestamps-at-point
  7871. (member what '(scheduled deadline)))
  7872. (insert
  7873. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7874. (org-insert-time-stamp time org-time-was-given
  7875. nil nil nil (list org-end-time-was-given))
  7876. (setq what nil))
  7877. (save-excursion
  7878. (save-restriction
  7879. (let (col list elt ts buffer-invisibility-spec)
  7880. (org-back-to-heading t)
  7881. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7882. (goto-char (match-end 1))
  7883. (setq col (current-column))
  7884. (goto-char (match-end 0))
  7885. (if (eobp) (insert "\n") (forward-char 1))
  7886. (if (and (not (looking-at outline-regexp))
  7887. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7888. "[^\r\n]*"))
  7889. (not (equal (match-string 1) org-clock-string)))
  7890. (narrow-to-region (match-beginning 0) (match-end 0))
  7891. (insert-before-markers "\n")
  7892. (backward-char 1)
  7893. (narrow-to-region (point) (point))
  7894. (and org-adapt-indentation (org-indent-to-column col)))
  7895. ;; Check if we have to remove something.
  7896. (setq list (cons what remove))
  7897. (while list
  7898. (setq elt (pop list))
  7899. (goto-char (point-min))
  7900. (when (or (and (eq elt 'scheduled)
  7901. (re-search-forward org-scheduled-time-regexp nil t))
  7902. (and (eq elt 'deadline)
  7903. (re-search-forward org-deadline-time-regexp nil t))
  7904. (and (eq elt 'closed)
  7905. (re-search-forward org-closed-time-regexp nil t)))
  7906. (replace-match "")
  7907. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7908. (if (looking-at " +") (replace-match ""))))
  7909. (goto-char (point-max))
  7910. (when what
  7911. (insert
  7912. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7913. (cond ((eq what 'scheduled) org-scheduled-string)
  7914. ((eq what 'deadline) org-deadline-string)
  7915. ((eq what 'closed) org-closed-string))
  7916. " ")
  7917. (setq ts (org-insert-time-stamp
  7918. time
  7919. (or org-time-was-given
  7920. (and (eq what 'closed) org-log-done-with-time))
  7921. (eq what 'closed)
  7922. nil nil (list org-end-time-was-given)))
  7923. (end-of-line 1))
  7924. (goto-char (point-min))
  7925. (widen)
  7926. (if (and (looking-at "[ \t]+\n")
  7927. (equal (char-before) ?\n))
  7928. (delete-region (1- (point)) (point-at-eol)))
  7929. ts)))))
  7930. (defvar org-log-note-marker (make-marker))
  7931. (defvar org-log-note-purpose nil)
  7932. (defvar org-log-note-state nil)
  7933. (defvar org-log-note-how nil)
  7934. (defvar org-log-note-extra nil)
  7935. (defvar org-log-note-window-configuration nil)
  7936. (defvar org-log-note-return-to (make-marker))
  7937. (defvar org-log-post-message nil
  7938. "Message to be displayed after a log note has been stored.
  7939. The auto-repeater uses this.")
  7940. (defun org-add-note ()
  7941. "Add a note to the current entry.
  7942. This is done in the same way as adding a state change note."
  7943. (interactive)
  7944. (org-add-log-setup 'note nil 'findpos nil))
  7945. (defvar org-property-end-re)
  7946. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7947. "Set up the post command hook to take a note.
  7948. If this is about to TODO state change, the new state is expected in STATE.
  7949. When FINDPOS is non-nil, find the correct position for the note in
  7950. the current entry. If not, assume that it can be inserted at point.
  7951. HOW is an indicator what kind of note should be created.
  7952. EXTRA is additional text that will be inserted into the notes buffer."
  7953. (save-restriction
  7954. (save-excursion
  7955. (when findpos
  7956. (org-back-to-heading t)
  7957. (narrow-to-region (point) (save-excursion
  7958. (outline-next-heading) (point)))
  7959. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7960. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7961. "[^\r\n]*\\)?"))
  7962. (goto-char (match-end 0))
  7963. (when (and org-log-state-notes-insert-after-drawers
  7964. (save-excursion
  7965. (forward-line) (looking-at org-drawer-regexp)))
  7966. (progn (forward-line)
  7967. (while (looking-at org-drawer-regexp)
  7968. (goto-char (match-end 0))
  7969. (re-search-forward org-property-end-re (point-max) t)
  7970. (forward-line))
  7971. (forward-line -1)))
  7972. (unless org-log-states-order-reversed
  7973. (and (= (char-after) ?\n) (forward-char 1))
  7974. (org-skip-over-state-notes)
  7975. (skip-chars-backward " \t\n\r")))
  7976. (move-marker org-log-note-marker (point))
  7977. (setq org-log-note-purpose purpose
  7978. org-log-note-state state
  7979. org-log-note-how how
  7980. org-log-note-extra extra)
  7981. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7982. (defun org-skip-over-state-notes ()
  7983. "Skip past the list of State notes in an entry."
  7984. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7985. (while (looking-at "[ \t]*- State")
  7986. (condition-case nil
  7987. (org-next-item)
  7988. (error (org-end-of-item)))))
  7989. (defun org-add-log-note (&optional purpose)
  7990. "Pop up a window for taking a note, and add this note later at point."
  7991. (remove-hook 'post-command-hook 'org-add-log-note)
  7992. (setq org-log-note-window-configuration (current-window-configuration))
  7993. (delete-other-windows)
  7994. (move-marker org-log-note-return-to (point))
  7995. (switch-to-buffer (marker-buffer org-log-note-marker))
  7996. (goto-char org-log-note-marker)
  7997. (org-switch-to-buffer-other-window "*Org Note*")
  7998. (erase-buffer)
  7999. (if (memq org-log-note-how '(time state))
  8000. (let (current-prefix-arg) (org-store-log-note))
  8001. (let ((org-inhibit-startup t)) (org-mode))
  8002. (insert (format "# Insert note for %s.
  8003. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8004. (cond
  8005. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8006. ((eq org-log-note-purpose 'done) "closed todo item")
  8007. ((eq org-log-note-purpose 'state)
  8008. (format "state change to \"%s\"" org-log-note-state))
  8009. ((eq org-log-note-purpose 'note)
  8010. "this entry")
  8011. (t (error "This should not happen")))))
  8012. (if org-log-note-extra (insert org-log-note-extra))
  8013. (org-set-local 'org-finish-function 'org-store-log-note)))
  8014. (defvar org-note-abort nil) ; dynamically scoped
  8015. (defun org-store-log-note ()
  8016. "Finish taking a log note, and insert it to where it belongs."
  8017. (let ((txt (buffer-string))
  8018. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8019. lines ind)
  8020. (kill-buffer (current-buffer))
  8021. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8022. (setq txt (replace-match "" t t txt)))
  8023. (if (string-match "\\s-+\\'" txt)
  8024. (setq txt (replace-match "" t t txt)))
  8025. (setq lines (org-split-string txt "\n"))
  8026. (when (and note (string-match "\\S-" note))
  8027. (setq note
  8028. (org-replace-escapes
  8029. note
  8030. (list (cons "%u" (user-login-name))
  8031. (cons "%U" user-full-name)
  8032. (cons "%t" (format-time-string
  8033. (org-time-stamp-format 'long 'inactive)
  8034. (current-time)))
  8035. (cons "%s" (if org-log-note-state
  8036. (concat "\"" org-log-note-state "\"")
  8037. "")))))
  8038. (if lines (setq note (concat note " \\\\")))
  8039. (push note lines))
  8040. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8041. (when lines
  8042. (save-excursion
  8043. (set-buffer (marker-buffer org-log-note-marker))
  8044. (save-excursion
  8045. (goto-char org-log-note-marker)
  8046. (move-marker org-log-note-marker nil)
  8047. (end-of-line 1)
  8048. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8049. (indent-relative nil)
  8050. (insert "- " (pop lines))
  8051. (org-indent-line-function)
  8052. (beginning-of-line 1)
  8053. (looking-at "[ \t]*")
  8054. (setq ind (concat (match-string 0) " "))
  8055. (end-of-line 1)
  8056. (while lines (insert "\n" ind (pop lines)))))))
  8057. (set-window-configuration org-log-note-window-configuration)
  8058. (with-current-buffer (marker-buffer org-log-note-return-to)
  8059. (goto-char org-log-note-return-to))
  8060. (move-marker org-log-note-return-to nil)
  8061. (and org-log-post-message (message "%s" org-log-post-message)))
  8062. (defun org-sparse-tree (&optional arg)
  8063. "Create a sparse tree, prompt for the details.
  8064. This command can create sparse trees. You first need to select the type
  8065. of match used to create the tree:
  8066. t Show entries with a specific TODO keyword.
  8067. T Show entries selected by a tags match.
  8068. p Enter a property name and its value (both with completion on existing
  8069. names/values) and show entries with that property.
  8070. r Show entries matching a regular expression
  8071. d Show deadlines due within `org-deadline-warning-days'."
  8072. (interactive "P")
  8073. (let (ans kwd value)
  8074. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8075. (setq ans (read-char-exclusive))
  8076. (cond
  8077. ((equal ans ?d)
  8078. (call-interactively 'org-check-deadlines))
  8079. ((equal ans ?b)
  8080. (call-interactively 'org-check-before-date))
  8081. ((equal ans ?t)
  8082. (org-show-todo-tree '(4)))
  8083. ((equal ans ?T)
  8084. (call-interactively 'org-tags-sparse-tree))
  8085. ((member ans '(?p ?P))
  8086. (setq kwd (org-ido-completing-read "Property: "
  8087. (mapcar 'list (org-buffer-property-keys))))
  8088. (setq value (org-ido-completing-read "Value: "
  8089. (mapcar 'list (org-property-values kwd))))
  8090. (unless (string-match "\\`{.*}\\'" value)
  8091. (setq value (concat "\"" value "\"")))
  8092. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8093. ((member ans '(?r ?R ?/))
  8094. (call-interactively 'org-occur))
  8095. (t (error "No such sparse tree command \"%c\"" ans)))))
  8096. (defvar org-occur-highlights nil
  8097. "List of overlays used for occur matches.")
  8098. (make-variable-buffer-local 'org-occur-highlights)
  8099. (defvar org-occur-parameters nil
  8100. "Parameters of the active org-occur calls.
  8101. This is a list, each call to org-occur pushes as cons cell,
  8102. containing the regular expression and the callback, onto the list.
  8103. The list can contain several entries if `org-occur' has been called
  8104. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8105. will only contain one set of parameters. When the highlights are
  8106. removed (for example with `C-c C-c', or with the next edit (depending
  8107. on `org-remove-highlights-with-change'), this variable is emptied
  8108. as well.")
  8109. (make-variable-buffer-local 'org-occur-parameters)
  8110. (defun org-occur (regexp &optional keep-previous callback)
  8111. "Make a compact tree which shows all matches of REGEXP.
  8112. The tree will show the lines where the regexp matches, and all higher
  8113. headlines above the match. It will also show the heading after the match,
  8114. to make sure editing the matching entry is easy.
  8115. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8116. call to `org-occur' will be kept, to allow stacking of calls to this
  8117. command.
  8118. If CALLBACK is non-nil, it is a function which is called to confirm
  8119. that the match should indeed be shown."
  8120. (interactive "sRegexp: \nP")
  8121. (unless keep-previous
  8122. (org-remove-occur-highlights nil nil t))
  8123. (push (cons regexp callback) org-occur-parameters)
  8124. (let ((cnt 0))
  8125. (save-excursion
  8126. (goto-char (point-min))
  8127. (if (or (not keep-previous) ; do not want to keep
  8128. (not org-occur-highlights)) ; no previous matches
  8129. ;; hide everything
  8130. (org-overview))
  8131. (while (re-search-forward regexp nil t)
  8132. (when (or (not callback)
  8133. (save-match-data (funcall callback)))
  8134. (setq cnt (1+ cnt))
  8135. (when org-highlight-sparse-tree-matches
  8136. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8137. (org-show-context 'occur-tree))))
  8138. (when org-remove-highlights-with-change
  8139. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8140. nil 'local))
  8141. (unless org-sparse-tree-open-archived-trees
  8142. (org-hide-archived-subtrees (point-min) (point-max)))
  8143. (run-hooks 'org-occur-hook)
  8144. (if (interactive-p)
  8145. (message "%d match(es) for regexp %s" cnt regexp))
  8146. cnt))
  8147. (defun org-show-context (&optional key)
  8148. "Make sure point and context and visible.
  8149. How much context is shown depends upon the variables
  8150. `org-show-hierarchy-above', `org-show-following-heading'. and
  8151. `org-show-siblings'."
  8152. (let ((heading-p (org-on-heading-p t))
  8153. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8154. (following-p (org-get-alist-option org-show-following-heading key))
  8155. (entry-p (org-get-alist-option org-show-entry-below key))
  8156. (siblings-p (org-get-alist-option org-show-siblings key)))
  8157. (catch 'exit
  8158. ;; Show heading or entry text
  8159. (if (and heading-p (not entry-p))
  8160. (org-flag-heading nil) ; only show the heading
  8161. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8162. (org-show-hidden-entry))) ; show entire entry
  8163. (when following-p
  8164. ;; Show next sibling, or heading below text
  8165. (save-excursion
  8166. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8167. (org-flag-heading nil))))
  8168. (when siblings-p (org-show-siblings))
  8169. (when hierarchy-p
  8170. ;; show all higher headings, possibly with siblings
  8171. (save-excursion
  8172. (while (and (condition-case nil
  8173. (progn (org-up-heading-all 1) t)
  8174. (error nil))
  8175. (not (bobp)))
  8176. (org-flag-heading nil)
  8177. (when siblings-p (org-show-siblings))))))))
  8178. (defun org-reveal (&optional siblings)
  8179. "Show current entry, hierarchy above it, and the following headline.
  8180. This can be used to show a consistent set of context around locations
  8181. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8182. not t for the search context.
  8183. With optional argument SIBLINGS, on each level of the hierarchy all
  8184. siblings are shown. This repairs the tree structure to what it would
  8185. look like when opened with hierarchical calls to `org-cycle'."
  8186. (interactive "P")
  8187. (let ((org-show-hierarchy-above t)
  8188. (org-show-following-heading t)
  8189. (org-show-siblings (if siblings t org-show-siblings)))
  8190. (org-show-context nil)))
  8191. (defun org-highlight-new-match (beg end)
  8192. "Highlight from BEG to END and mark the highlight is an occur headline."
  8193. (let ((ov (org-make-overlay beg end)))
  8194. (org-overlay-put ov 'face 'secondary-selection)
  8195. (push ov org-occur-highlights)))
  8196. (defun org-remove-occur-highlights (&optional beg end noremove)
  8197. "Remove the occur highlights from the buffer.
  8198. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8199. from the `before-change-functions' in the current buffer."
  8200. (interactive)
  8201. (unless org-inhibit-highlight-removal
  8202. (mapc 'org-delete-overlay org-occur-highlights)
  8203. (setq org-occur-highlights nil)
  8204. (setq org-occur-parameters nil)
  8205. (unless noremove
  8206. (remove-hook 'before-change-functions
  8207. 'org-remove-occur-highlights 'local))))
  8208. ;;;; Priorities
  8209. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8210. "Regular expression matching the priority indicator.")
  8211. (defvar org-remove-priority-next-time nil)
  8212. (defun org-priority-up ()
  8213. "Increase the priority of the current item."
  8214. (interactive)
  8215. (org-priority 'up))
  8216. (defun org-priority-down ()
  8217. "Decrease the priority of the current item."
  8218. (interactive)
  8219. (org-priority 'down))
  8220. (defun org-priority (&optional action)
  8221. "Change the priority of an item by ARG.
  8222. ACTION can be `set', `up', `down', or a character."
  8223. (interactive)
  8224. (setq action (or action 'set))
  8225. (let (current new news have remove)
  8226. (save-excursion
  8227. (org-back-to-heading)
  8228. (if (looking-at org-priority-regexp)
  8229. (setq current (string-to-char (match-string 2))
  8230. have t)
  8231. (setq current org-default-priority))
  8232. (cond
  8233. ((or (eq action 'set)
  8234. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8235. (if (not (eq action 'set))
  8236. (setq new action)
  8237. (message "Priority %c-%c, SPC to remove: "
  8238. org-highest-priority org-lowest-priority)
  8239. (setq new (read-char-exclusive)))
  8240. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8241. (= (upcase org-lowest-priority) org-lowest-priority))
  8242. (setq new (upcase new)))
  8243. (cond ((equal new ?\ ) (setq remove t))
  8244. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8245. (error "Priority must be between `%c' and `%c'"
  8246. org-highest-priority org-lowest-priority))))
  8247. ((eq action 'up)
  8248. (if (and (not have) (eq last-command this-command))
  8249. (setq new org-lowest-priority)
  8250. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8251. org-default-priority (1- current)))))
  8252. ((eq action 'down)
  8253. (if (and (not have) (eq last-command this-command))
  8254. (setq new org-highest-priority)
  8255. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8256. org-default-priority (1+ current)))))
  8257. (t (error "Invalid action")))
  8258. (if (or (< (upcase new) org-highest-priority)
  8259. (> (upcase new) org-lowest-priority))
  8260. (setq remove t))
  8261. (setq news (format "%c" new))
  8262. (if have
  8263. (if remove
  8264. (replace-match "" t t nil 1)
  8265. (replace-match news t t nil 2))
  8266. (if remove
  8267. (error "No priority cookie found in line")
  8268. (looking-at org-todo-line-regexp)
  8269. (if (match-end 2)
  8270. (progn
  8271. (goto-char (match-end 2))
  8272. (insert " [#" news "]"))
  8273. (goto-char (match-beginning 3))
  8274. (insert "[#" news "] ")))))
  8275. (org-preserve-lc (org-set-tags nil 'align))
  8276. (if remove
  8277. (message "Priority removed")
  8278. (message "Priority of current item set to %s" news))))
  8279. (defun org-get-priority (s)
  8280. "Find priority cookie and return priority."
  8281. (save-match-data
  8282. (if (not (string-match org-priority-regexp s))
  8283. (* 1000 (- org-lowest-priority org-default-priority))
  8284. (* 1000 (- org-lowest-priority
  8285. (string-to-char (match-string 2 s)))))))
  8286. ;;;; Tags
  8287. (defvar org-agenda-archives-mode)
  8288. (defun org-scan-tags (action matcher &optional todo-only)
  8289. "Scan headline tags with inheritance and produce output ACTION.
  8290. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8291. or `agenda' to produce an entry list for an agenda view. It can also be
  8292. a Lisp form or a function that should be called at each matched headline, in
  8293. this case the return value is a list of all return values from these calls.
  8294. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8295. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8296. only lines with a TODO keyword are included in the output."
  8297. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8298. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8299. (org-re
  8300. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8301. (props (list 'face 'default
  8302. 'done-face 'org-done
  8303. 'undone-face 'default
  8304. 'mouse-face 'highlight
  8305. 'org-not-done-regexp org-not-done-regexp
  8306. 'org-todo-regexp org-todo-regexp
  8307. 'keymap org-agenda-keymap
  8308. 'help-echo
  8309. (format "mouse-2 or RET jump to org file %s"
  8310. (abbreviate-file-name
  8311. (or (buffer-file-name (buffer-base-buffer))
  8312. (buffer-name (buffer-base-buffer)))))))
  8313. (case-fold-search nil)
  8314. lspos tags tags-list
  8315. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8316. (llast 0) rtn rtn1 level category i txt
  8317. todo marker entry priority)
  8318. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8319. (setq action (list 'lambda nil action)))
  8320. (save-excursion
  8321. (goto-char (point-min))
  8322. (when (eq action 'sparse-tree)
  8323. (org-overview)
  8324. (org-remove-occur-highlights))
  8325. (while (re-search-forward re nil t)
  8326. (catch :skip
  8327. (setq todo (if (match-end 1) (match-string 2))
  8328. tags (if (match-end 4) (match-string 4)))
  8329. (goto-char (setq lspos (1+ (match-beginning 0))))
  8330. (setq level (org-reduced-level (funcall outline-level))
  8331. category (org-get-category))
  8332. (setq i llast llast level)
  8333. ;; remove tag lists from same and sublevels
  8334. (while (>= i level)
  8335. (when (setq entry (assoc i tags-alist))
  8336. (setq tags-alist (delete entry tags-alist)))
  8337. (setq i (1- i)))
  8338. ;; add the next tags
  8339. (when tags
  8340. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8341. tags-alist
  8342. (cons (cons level tags) tags-alist)))
  8343. ;; compile tags for current headline
  8344. (setq tags-list
  8345. (if org-use-tag-inheritance
  8346. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8347. tags))
  8348. (when org-use-tag-inheritance
  8349. (setcdr (car tags-alist)
  8350. (mapcar (lambda (x)
  8351. (setq x (copy-sequence x))
  8352. (org-add-prop-inherited x))
  8353. (cdar tags-alist))))
  8354. (when (and tags org-use-tag-inheritance
  8355. (not (eq t org-use-tag-inheritance)))
  8356. ;; selective inheritance, remove uninherited ones
  8357. (setcdr (car tags-alist)
  8358. (org-remove-uniherited-tags (cdar tags-alist))))
  8359. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8360. (let ((case-fold-search t)) (eval matcher))
  8361. (or
  8362. (not (member org-archive-tag tags-list))
  8363. ;; we have an archive tag, should we use this anyway?
  8364. (or (not org-agenda-skip-archived-trees)
  8365. (and (eq action 'agenda) org-agenda-archives-mode))))
  8366. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8367. ;; select this headline
  8368. (cond
  8369. ((eq action 'sparse-tree)
  8370. (and org-highlight-sparse-tree-matches
  8371. (org-get-heading) (match-end 0)
  8372. (org-highlight-new-match
  8373. (match-beginning 0) (match-beginning 1)))
  8374. (org-show-context 'tags-tree))
  8375. ((eq action 'agenda)
  8376. (setq txt (org-format-agenda-item
  8377. ""
  8378. (concat
  8379. (if org-tags-match-list-sublevels
  8380. (make-string (1- level) ?.) "")
  8381. (org-get-heading))
  8382. category (org-get-tags-at))
  8383. priority (org-get-priority txt))
  8384. (goto-char lspos)
  8385. (setq marker (org-agenda-new-marker))
  8386. (org-add-props txt props
  8387. 'org-marker marker 'org-hd-marker marker 'org-category category
  8388. 'priority priority 'type "tagsmatch")
  8389. (push txt rtn))
  8390. ((functionp action)
  8391. (save-excursion
  8392. (setq rtn1 (funcall action))
  8393. (push rtn1 rtn))
  8394. (goto-char (point-at-eol)))
  8395. (t (error "Invalid action")))
  8396. ;; if we are to skip sublevels, jump to end of subtree
  8397. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8398. (when (and (eq action 'sparse-tree)
  8399. (not org-sparse-tree-open-archived-trees))
  8400. (org-hide-archived-subtrees (point-min) (point-max)))
  8401. (nreverse rtn)))
  8402. (defun org-remove-uniherited-tags (tags)
  8403. "Remove all tags that are not inherited from the list TAGS."
  8404. (cond
  8405. ((eq org-use-tag-inheritance t)
  8406. (if org-tags-exclude-from-inheritance
  8407. (org-delete-all org-tags-exclude-from-inheritance tags)
  8408. tags))
  8409. ((not org-use-tag-inheritance) nil)
  8410. ((stringp org-use-tag-inheritance)
  8411. (delq nil (mapcar
  8412. (lambda (x)
  8413. (if (and (string-match org-use-tag-inheritance x)
  8414. (not (member x org-tags-exclude-from-inheritance)))
  8415. x nil))
  8416. tags)))
  8417. ((listp org-use-tag-inheritance)
  8418. (delq nil (mapcar
  8419. (lambda (x)
  8420. (if (member x org-use-tag-inheritance) x nil))
  8421. tags)))))
  8422. (defvar todo-only) ;; dynamically scoped
  8423. (defun org-tags-sparse-tree (&optional todo-only match)
  8424. "Create a sparse tree according to tags string MATCH.
  8425. MATCH can contain positive and negative selection of tags, like
  8426. \"+WORK+URGENT-WITHBOSS\".
  8427. If optional argument TODO-ONLY is non-nil, only select lines that are
  8428. also TODO lines."
  8429. (interactive "P")
  8430. (org-prepare-agenda-buffers (list (current-buffer)))
  8431. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8432. (defvar org-cached-props nil)
  8433. (defun org-cached-entry-get (pom property)
  8434. (if (or (eq t org-use-property-inheritance)
  8435. (and (stringp org-use-property-inheritance)
  8436. (string-match org-use-property-inheritance property))
  8437. (and (listp org-use-property-inheritance)
  8438. (member property org-use-property-inheritance)))
  8439. ;; Caching is not possible, check it directly
  8440. (org-entry-get pom property 'inherit)
  8441. ;; Get all properties, so that we can do complicated checks easily
  8442. (cdr (assoc property (or org-cached-props
  8443. (setq org-cached-props
  8444. (org-entry-properties pom)))))))
  8445. (defun org-global-tags-completion-table (&optional files)
  8446. "Return the list of all tags in all agenda buffer/files."
  8447. (save-excursion
  8448. (org-uniquify
  8449. (delq nil
  8450. (apply 'append
  8451. (mapcar
  8452. (lambda (file)
  8453. (set-buffer (find-file-noselect file))
  8454. (append (org-get-buffer-tags)
  8455. (mapcar (lambda (x) (if (stringp (car-safe x))
  8456. (list (car-safe x)) nil))
  8457. org-tag-alist)))
  8458. (if (and files (car files))
  8459. files
  8460. (org-agenda-files))))))))
  8461. (defun org-make-tags-matcher (match)
  8462. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8463. ;; todo-only is scoped dynamically into this function, and the function
  8464. ;; may change it if the matcher asks for it.
  8465. (unless match
  8466. ;; Get a new match request, with completion
  8467. (let ((org-last-tags-completion-table
  8468. (org-global-tags-completion-table)))
  8469. (setq match (org-ido-completing-read
  8470. "Match: " 'org-tags-completion-function nil nil nil
  8471. 'org-tags-history))))
  8472. ;; Parse the string and create a lisp form
  8473. (let ((match0 match)
  8474. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8475. minus tag mm
  8476. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8477. orterms term orlist re-p str-p level-p level-op time-p
  8478. prop-p pn pv po cat-p gv rest)
  8479. (if (string-match "/+" match)
  8480. ;; match contains also a todo-matching request
  8481. (progn
  8482. (setq tagsmatch (substring match 0 (match-beginning 0))
  8483. todomatch (substring match (match-end 0)))
  8484. (if (string-match "^!" todomatch)
  8485. (setq todo-only t todomatch (substring todomatch 1)))
  8486. (if (string-match "^\\s-*$" todomatch)
  8487. (setq todomatch nil)))
  8488. ;; only matching tags
  8489. (setq tagsmatch match todomatch nil))
  8490. ;; Make the tags matcher
  8491. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8492. (setq tagsmatcher t)
  8493. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8494. (while (setq term (pop orterms))
  8495. (while (and (equal (substring term -1) "\\") orterms)
  8496. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8497. (while (string-match re term)
  8498. (setq rest (substring term (match-end 0))
  8499. minus (and (match-end 1)
  8500. (equal (match-string 1 term) "-"))
  8501. tag (match-string 2 term)
  8502. re-p (equal (string-to-char tag) ?{)
  8503. level-p (match-end 4)
  8504. prop-p (match-end 5)
  8505. mm (cond
  8506. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8507. (level-p
  8508. (setq level-op (org-op-to-function (match-string 3 term)))
  8509. `(,level-op level ,(string-to-number
  8510. (match-string 4 term))))
  8511. (prop-p
  8512. (setq pn (match-string 5 term)
  8513. po (match-string 6 term)
  8514. pv (match-string 7 term)
  8515. cat-p (equal pn "CATEGORY")
  8516. re-p (equal (string-to-char pv) ?{)
  8517. str-p (equal (string-to-char pv) ?\")
  8518. time-p (save-match-data
  8519. (string-match "^\"[[<].*[]>]\"$" pv))
  8520. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8521. (if time-p (setq pv (org-matcher-time pv)))
  8522. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8523. (cond
  8524. ((equal pn "CATEGORY")
  8525. (setq gv '(get-text-property (point) 'org-category)))
  8526. ((equal pn "TODO")
  8527. (setq gv 'todo))
  8528. (t
  8529. (setq gv `(org-cached-entry-get nil ,pn))))
  8530. (if re-p
  8531. (if (eq po 'org<>)
  8532. `(not (string-match ,pv (or ,gv "")))
  8533. `(string-match ,pv (or ,gv "")))
  8534. (if str-p
  8535. `(,po (or ,gv "") ,pv)
  8536. `(,po (string-to-number (or ,gv ""))
  8537. ,(string-to-number pv) ))))
  8538. (t `(member ,(downcase tag) tags-list)))
  8539. mm (if minus (list 'not mm) mm)
  8540. term rest)
  8541. (push mm tagsmatcher))
  8542. (push (if (> (length tagsmatcher) 1)
  8543. (cons 'and tagsmatcher)
  8544. (car tagsmatcher))
  8545. orlist)
  8546. (setq tagsmatcher nil))
  8547. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8548. (setq tagsmatcher
  8549. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8550. ;; Make the todo matcher
  8551. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8552. (setq todomatcher t)
  8553. (setq orterms (org-split-string todomatch "|") orlist nil)
  8554. (while (setq term (pop orterms))
  8555. (while (string-match re term)
  8556. (setq minus (and (match-end 1)
  8557. (equal (match-string 1 term) "-"))
  8558. kwd (match-string 2 term)
  8559. re-p (equal (string-to-char kwd) ?{)
  8560. term (substring term (match-end 0))
  8561. mm (if re-p
  8562. `(string-match ,(substring kwd 1 -1) todo)
  8563. (list 'equal 'todo kwd))
  8564. mm (if minus (list 'not mm) mm))
  8565. (push mm todomatcher))
  8566. (push (if (> (length todomatcher) 1)
  8567. (cons 'and todomatcher)
  8568. (car todomatcher))
  8569. orlist)
  8570. (setq todomatcher nil))
  8571. (setq todomatcher (if (> (length orlist) 1)
  8572. (cons 'or orlist) (car orlist))))
  8573. ;; Return the string and lisp forms of the matcher
  8574. (setq matcher (if todomatcher
  8575. (list 'and tagsmatcher todomatcher)
  8576. tagsmatcher))
  8577. (cons match0 matcher)))
  8578. (defun org-op-to-function (op &optional stringp)
  8579. "Turn an operator into the appropriate function."
  8580. (setq op
  8581. (cond
  8582. ((equal op "<" ) '(< string< org-time<))
  8583. ((equal op ">" ) '(> org-string> org-time>))
  8584. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8585. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8586. ((member op '("=" "==")) '(= string= org-time=))
  8587. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8588. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8589. (defun org<> (a b) (not (= a b)))
  8590. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8591. (defun org-string>= (a b) (not (string< a b)))
  8592. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8593. (defun org-string<> (a b) (not (string= a b)))
  8594. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8595. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8596. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8597. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8598. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8599. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8600. (defun org-2ft (s)
  8601. "Convert S to a floating point time.
  8602. If S is already a number, just return it. If it is a string, parse
  8603. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8604. (cond
  8605. ((numberp s) s)
  8606. ((stringp s)
  8607. (condition-case nil
  8608. (float-time (apply 'encode-time (org-parse-time-string s)))
  8609. (error 0.)))
  8610. (t 0.)))
  8611. (defun org-time-today ()
  8612. "Time in seconds today at 0:00.
  8613. Returns the float number of seconds since the beginning of the
  8614. epoch to the beginning of today (00:00)."
  8615. (float-time (apply 'encode-time
  8616. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8617. (defun org-matcher-time (s)
  8618. "Interpret a time comparison value."
  8619. (save-match-data
  8620. (cond
  8621. ((string= s "<now>") (float-time))
  8622. ((string= s "<today>") (org-time-today))
  8623. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8624. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8625. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8626. (+ (org-time-today)
  8627. (* (string-to-number (match-string 1 s))
  8628. (cdr (assoc (match-string 2 s)
  8629. '(("d" . 86400.0) ("w" . 604800.0)
  8630. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8631. (t (org-2ft s)))))
  8632. (defun org-match-any-p (re list)
  8633. "Does re match any element of list?"
  8634. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8635. (delq nil list))
  8636. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8637. (defvar org-tags-overlay (org-make-overlay 1 1))
  8638. (org-detach-overlay org-tags-overlay)
  8639. (defun org-get-local-tags-at (&optional pos)
  8640. "Get a list of tags defined in the current headline."
  8641. (org-get-tags-at pos 'local))
  8642. (defun org-get-local-tags ()
  8643. "Get a list of tags defined in the current headline."
  8644. (org-get-tags-at nil 'local))
  8645. (defun org-get-tags-at (&optional pos local)
  8646. "Get a list of all headline tags applicable at POS.
  8647. POS defaults to point. If tags are inherited, the list contains
  8648. the targets in the same sequence as the headlines appear, i.e.
  8649. the tags of the current headline come last.
  8650. When LOCAL is non-nil, only return tags from the current headline,
  8651. ignore inherited ones."
  8652. (interactive)
  8653. (let (tags ltags lastpos parent)
  8654. (save-excursion
  8655. (save-restriction
  8656. (widen)
  8657. (goto-char (or pos (point)))
  8658. (save-match-data
  8659. (catch 'done
  8660. (condition-case nil
  8661. (progn
  8662. (org-back-to-heading t)
  8663. (while (not (equal lastpos (point)))
  8664. (setq lastpos (point))
  8665. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8666. (setq ltags (org-split-string
  8667. (org-match-string-no-properties 1) ":"))
  8668. (when parent
  8669. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8670. (setq tags (append
  8671. (if parent
  8672. (org-remove-uniherited-tags ltags)
  8673. ltags)
  8674. tags)))
  8675. (or org-use-tag-inheritance (throw 'done t))
  8676. (if local (throw 'done t))
  8677. (org-up-heading-all 1)
  8678. (setq parent t)))
  8679. (error nil)))))
  8680. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8681. (defun org-add-prop-inherited (s)
  8682. (add-text-properties 0 (length s) '(inherited t) s)
  8683. s)
  8684. (defun org-toggle-tag (tag &optional onoff)
  8685. "Toggle the tag TAG for the current line.
  8686. If ONOFF is `on' or `off', don't toggle but set to this state."
  8687. (let (res current)
  8688. (save-excursion
  8689. (org-back-to-heading t)
  8690. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8691. (point-at-eol) t)
  8692. (progn
  8693. (setq current (match-string 1))
  8694. (replace-match ""))
  8695. (setq current ""))
  8696. (setq current (nreverse (org-split-string current ":")))
  8697. (cond
  8698. ((eq onoff 'on)
  8699. (setq res t)
  8700. (or (member tag current) (push tag current)))
  8701. ((eq onoff 'off)
  8702. (or (not (member tag current)) (setq current (delete tag current))))
  8703. (t (if (member tag current)
  8704. (setq current (delete tag current))
  8705. (setq res t)
  8706. (push tag current))))
  8707. (end-of-line 1)
  8708. (if current
  8709. (progn
  8710. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8711. (org-set-tags nil t))
  8712. (delete-horizontal-space))
  8713. (run-hooks 'org-after-tags-change-hook))
  8714. res))
  8715. (defun org-align-tags-here (to-col)
  8716. ;; Assumes that this is a headline
  8717. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8718. (beginning-of-line 1)
  8719. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8720. (< pos (match-beginning 2)))
  8721. (progn
  8722. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8723. (goto-char (match-beginning 1))
  8724. (insert " ")
  8725. (delete-region (point) (1+ (match-beginning 2)))
  8726. (setq ncol (max (1+ (current-column))
  8727. (1+ col)
  8728. (if (> to-col 0)
  8729. to-col
  8730. (- (abs to-col) tags-l))))
  8731. (setq p (point))
  8732. (insert (make-string (- ncol (current-column)) ?\ ))
  8733. (setq ncol (current-column))
  8734. (when indent-tabs-mode (tabify p (point-at-eol)))
  8735. (org-move-to-column (min ncol col) t))
  8736. (goto-char pos))))
  8737. (defun org-set-tags-command (&optional arg just-align)
  8738. "Call the set-tags command for the current entry."
  8739. (interactive "P")
  8740. (if (org-on-heading-p)
  8741. (org-set-tags arg just-align)
  8742. (save-excursion
  8743. (org-back-to-heading t)
  8744. (org-set-tags arg just-align))))
  8745. (defun org-set-tags (&optional arg just-align)
  8746. "Set the tags for the current headline.
  8747. With prefix ARG, realign all tags in headings in the current buffer."
  8748. (interactive "P")
  8749. (let* ((re (concat "^" outline-regexp))
  8750. (current (org-get-tags-string))
  8751. (col (current-column))
  8752. (org-setting-tags t)
  8753. table current-tags inherited-tags ; computed below when needed
  8754. tags p0 c0 c1 rpl)
  8755. (if arg
  8756. (save-excursion
  8757. (goto-char (point-min))
  8758. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8759. (while (re-search-forward re nil t)
  8760. (org-set-tags nil t)
  8761. (end-of-line 1)))
  8762. (message "All tags realigned to column %d" org-tags-column))
  8763. (if just-align
  8764. (setq tags current)
  8765. ;; Get a new set of tags from the user
  8766. (save-excursion
  8767. (setq table (or org-tag-alist (org-get-buffer-tags))
  8768. org-last-tags-completion-table table
  8769. current-tags (org-split-string current ":")
  8770. inherited-tags (nreverse
  8771. (nthcdr (length current-tags)
  8772. (nreverse (org-get-tags-at))))
  8773. tags
  8774. (if (or (eq t org-use-fast-tag-selection)
  8775. (and org-use-fast-tag-selection
  8776. (delq nil (mapcar 'cdr table))))
  8777. (org-fast-tag-selection
  8778. current-tags inherited-tags table
  8779. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8780. (let ((org-add-colon-after-tag-completion t))
  8781. (org-trim
  8782. (org-without-partial-completion
  8783. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8784. nil nil current 'org-tags-history)))))))
  8785. (while (string-match "[-+&]+" tags)
  8786. ;; No boolean logic, just a list
  8787. (setq tags (replace-match ":" t t tags))))
  8788. (if (string-match "\\`[\t ]*\\'" tags)
  8789. (setq tags "")
  8790. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8791. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8792. ;; Insert new tags at the correct column
  8793. (beginning-of-line 1)
  8794. (cond
  8795. ((and (equal current "") (equal tags "")))
  8796. ((re-search-forward
  8797. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8798. (point-at-eol) t)
  8799. (if (equal tags "")
  8800. (setq rpl "")
  8801. (goto-char (match-beginning 0))
  8802. (setq c0 (current-column) p0 (point)
  8803. c1 (max (1+ c0) (if (> org-tags-column 0)
  8804. org-tags-column
  8805. (- (- org-tags-column) (length tags))))
  8806. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8807. (replace-match rpl t t)
  8808. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8809. tags)
  8810. (t (error "Tags alignment failed")))
  8811. (org-move-to-column col)
  8812. (unless just-align
  8813. (run-hooks 'org-after-tags-change-hook)))))
  8814. (defun org-change-tag-in-region (beg end tag off)
  8815. "Add or remove TAG for each entry in the region.
  8816. This works in the agenda, and also in an org-mode buffer."
  8817. (interactive
  8818. (list (region-beginning) (region-end)
  8819. (let ((org-last-tags-completion-table
  8820. (if (org-mode-p)
  8821. (org-get-buffer-tags)
  8822. (org-global-tags-completion-table))))
  8823. (org-ido-completing-read
  8824. "Tag: " 'org-tags-completion-function nil nil nil
  8825. 'org-tags-history))
  8826. (progn
  8827. (message "[s]et or [r]emove? ")
  8828. (equal (read-char-exclusive) ?r))))
  8829. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8830. (let ((agendap (equal major-mode 'org-agenda-mode))
  8831. l1 l2 m buf pos newhead (cnt 0))
  8832. (goto-char end)
  8833. (setq l2 (1- (org-current-line)))
  8834. (goto-char beg)
  8835. (setq l1 (org-current-line))
  8836. (loop for l from l1 to l2 do
  8837. (goto-line l)
  8838. (setq m (get-text-property (point) 'org-hd-marker))
  8839. (when (or (and (org-mode-p) (org-on-heading-p))
  8840. (and agendap m))
  8841. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8842. pos (if agendap m (point)))
  8843. (with-current-buffer buf
  8844. (save-excursion
  8845. (save-restriction
  8846. (goto-char pos)
  8847. (setq cnt (1+ cnt))
  8848. (org-toggle-tag tag (if off 'off 'on))
  8849. (setq newhead (org-get-heading)))))
  8850. (and agendap (org-agenda-change-all-lines newhead m))))
  8851. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8852. (defun org-tags-completion-function (string predicate &optional flag)
  8853. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8854. (confirm (lambda (x) (stringp (car x)))))
  8855. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8856. (setq s1 (match-string 1 string)
  8857. s2 (match-string 2 string))
  8858. (setq s1 "" s2 string))
  8859. (cond
  8860. ((eq flag nil)
  8861. ;; try completion
  8862. (setq rtn (try-completion s2 ctable confirm))
  8863. (if (stringp rtn)
  8864. (setq rtn
  8865. (concat s1 s2 (substring rtn (length s2))
  8866. (if (and org-add-colon-after-tag-completion
  8867. (assoc rtn ctable))
  8868. ":" ""))))
  8869. rtn)
  8870. ((eq flag t)
  8871. ;; all-completions
  8872. (all-completions s2 ctable confirm)
  8873. )
  8874. ((eq flag 'lambda)
  8875. ;; exact match?
  8876. (assoc s2 ctable)))
  8877. ))
  8878. (defun org-fast-tag-insert (kwd tags face &optional end)
  8879. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  8880. (insert (format "%-12s" (concat kwd ":"))
  8881. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8882. (or end "")))
  8883. (defun org-fast-tag-show-exit (flag)
  8884. (save-excursion
  8885. (goto-line 3)
  8886. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8887. (replace-match ""))
  8888. (when flag
  8889. (end-of-line 1)
  8890. (org-move-to-column (- (window-width) 19) t)
  8891. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8892. (defun org-set-current-tags-overlay (current prefix)
  8893. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8894. (if (featurep 'xemacs)
  8895. (org-overlay-display org-tags-overlay (concat prefix s)
  8896. 'secondary-selection)
  8897. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8898. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8899. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8900. "Fast tag selection with single keys.
  8901. CURRENT is the current list of tags in the headline, INHERITED is the
  8902. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8903. possibly with grouping information. TODO-TABLE is a similar table with
  8904. TODO keywords, should these have keys assigned to them.
  8905. If the keys are nil, a-z are automatically assigned.
  8906. Returns the new tags string, or nil to not change the current settings."
  8907. (let* ((fulltable (append table todo-table))
  8908. (maxlen (apply 'max (mapcar
  8909. (lambda (x)
  8910. (if (stringp (car x)) (string-width (car x)) 0))
  8911. fulltable)))
  8912. (buf (current-buffer))
  8913. (expert (eq org-fast-tag-selection-single-key 'expert))
  8914. (buffer-tags nil)
  8915. (fwidth (+ maxlen 3 1 3))
  8916. (ncol (/ (- (window-width) 4) fwidth))
  8917. (i-face 'org-done)
  8918. (c-face 'org-todo)
  8919. tg cnt e c char c1 c2 ntable tbl rtn
  8920. ov-start ov-end ov-prefix
  8921. (exit-after-next org-fast-tag-selection-single-key)
  8922. (done-keywords org-done-keywords)
  8923. groups ingroup)
  8924. (save-excursion
  8925. (beginning-of-line 1)
  8926. (if (looking-at
  8927. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8928. (setq ov-start (match-beginning 1)
  8929. ov-end (match-end 1)
  8930. ov-prefix "")
  8931. (setq ov-start (1- (point-at-eol))
  8932. ov-end (1+ ov-start))
  8933. (skip-chars-forward "^\n\r")
  8934. (setq ov-prefix
  8935. (concat
  8936. (buffer-substring (1- (point)) (point))
  8937. (if (> (current-column) org-tags-column)
  8938. " "
  8939. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8940. (org-move-overlay org-tags-overlay ov-start ov-end)
  8941. (save-window-excursion
  8942. (if expert
  8943. (set-buffer (get-buffer-create " *Org tags*"))
  8944. (delete-other-windows)
  8945. (split-window-vertically)
  8946. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8947. (erase-buffer)
  8948. (org-set-local 'org-done-keywords done-keywords)
  8949. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8950. (org-fast-tag-insert "Current" current c-face "\n\n")
  8951. (org-fast-tag-show-exit exit-after-next)
  8952. (org-set-current-tags-overlay current ov-prefix)
  8953. (setq tbl fulltable char ?a cnt 0)
  8954. (while (setq e (pop tbl))
  8955. (cond
  8956. ((equal e '(:startgroup))
  8957. (push '() groups) (setq ingroup t)
  8958. (when (not (= cnt 0))
  8959. (setq cnt 0)
  8960. (insert "\n"))
  8961. (insert "{ "))
  8962. ((equal e '(:endgroup))
  8963. (setq ingroup nil cnt 0)
  8964. (insert "}\n"))
  8965. (t
  8966. (setq tg (car e) c2 nil)
  8967. (if (cdr e)
  8968. (setq c (cdr e))
  8969. ;; automatically assign a character.
  8970. (setq c1 (string-to-char
  8971. (downcase (substring
  8972. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8973. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8974. (while (or (rassoc char ntable) (rassoc char table))
  8975. (setq char (1+ char)))
  8976. (setq c2 c1))
  8977. (setq c (or c2 char)))
  8978. (if ingroup (push tg (car groups)))
  8979. (setq tg (org-add-props tg nil 'face
  8980. (cond
  8981. ((not (assoc tg table))
  8982. (org-get-todo-face tg))
  8983. ((member tg current) c-face)
  8984. ((member tg inherited) i-face)
  8985. (t nil))))
  8986. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8987. (insert "[" c "] " tg (make-string
  8988. (- fwidth 4 (length tg)) ?\ ))
  8989. (push (cons tg c) ntable)
  8990. (when (= (setq cnt (1+ cnt)) ncol)
  8991. (insert "\n")
  8992. (if ingroup (insert " "))
  8993. (setq cnt 0)))))
  8994. (setq ntable (nreverse ntable))
  8995. (insert "\n")
  8996. (goto-char (point-min))
  8997. (if (not expert) (org-fit-window-to-buffer))
  8998. (setq rtn
  8999. (catch 'exit
  9000. (while t
  9001. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9002. (if groups " [!] no groups" " [!]groups")
  9003. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9004. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9005. (cond
  9006. ((= c ?\r) (throw 'exit t))
  9007. ((= c ?!)
  9008. (setq groups (not groups))
  9009. (goto-char (point-min))
  9010. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9011. ((= c ?\C-c)
  9012. (if (not expert)
  9013. (org-fast-tag-show-exit
  9014. (setq exit-after-next (not exit-after-next)))
  9015. (setq expert nil)
  9016. (delete-other-windows)
  9017. (split-window-vertically)
  9018. (org-switch-to-buffer-other-window " *Org tags*")
  9019. (org-fit-window-to-buffer)))
  9020. ((or (= c ?\C-g)
  9021. (and (= c ?q) (not (rassoc c ntable))))
  9022. (org-detach-overlay org-tags-overlay)
  9023. (setq quit-flag t))
  9024. ((= c ?\ )
  9025. (setq current nil)
  9026. (if exit-after-next (setq exit-after-next 'now)))
  9027. ((= c ?\t)
  9028. (condition-case nil
  9029. (setq tg (org-ido-completing-read
  9030. "Tag: "
  9031. (or buffer-tags
  9032. (with-current-buffer buf
  9033. (org-get-buffer-tags)))))
  9034. (quit (setq tg "")))
  9035. (when (string-match "\\S-" tg)
  9036. (add-to-list 'buffer-tags (list tg))
  9037. (if (member tg current)
  9038. (setq current (delete tg current))
  9039. (push tg current)))
  9040. (if exit-after-next (setq exit-after-next 'now)))
  9041. ((setq e (rassoc c todo-table) tg (car e))
  9042. (with-current-buffer buf
  9043. (save-excursion (org-todo tg)))
  9044. (if exit-after-next (setq exit-after-next 'now)))
  9045. ((setq e (rassoc c ntable) tg (car e))
  9046. (if (member tg current)
  9047. (setq current (delete tg current))
  9048. (loop for g in groups do
  9049. (if (member tg g)
  9050. (mapc (lambda (x)
  9051. (setq current (delete x current)))
  9052. g)))
  9053. (push tg current))
  9054. (if exit-after-next (setq exit-after-next 'now))))
  9055. ;; Create a sorted list
  9056. (setq current
  9057. (sort current
  9058. (lambda (a b)
  9059. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9060. (if (eq exit-after-next 'now) (throw 'exit t))
  9061. (goto-char (point-min))
  9062. (beginning-of-line 2)
  9063. (delete-region (point) (point-at-eol))
  9064. (org-fast-tag-insert "Current" current c-face)
  9065. (org-set-current-tags-overlay current ov-prefix)
  9066. (while (re-search-forward
  9067. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9068. (setq tg (match-string 1))
  9069. (add-text-properties
  9070. (match-beginning 1) (match-end 1)
  9071. (list 'face
  9072. (cond
  9073. ((member tg current) c-face)
  9074. ((member tg inherited) i-face)
  9075. (t (get-text-property (match-beginning 1) 'face))))))
  9076. (goto-char (point-min)))))
  9077. (org-detach-overlay org-tags-overlay)
  9078. (if rtn
  9079. (mapconcat 'identity current ":")
  9080. nil))))
  9081. (defun org-get-tags-string ()
  9082. "Get the TAGS string in the current headline."
  9083. (unless (org-on-heading-p t)
  9084. (error "Not on a heading"))
  9085. (save-excursion
  9086. (beginning-of-line 1)
  9087. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9088. (org-match-string-no-properties 1)
  9089. "")))
  9090. (defun org-get-tags ()
  9091. "Get the list of tags specified in the current headline."
  9092. (org-split-string (org-get-tags-string) ":"))
  9093. (defun org-get-buffer-tags ()
  9094. "Get a table of all tags used in the buffer, for completion."
  9095. (let (tags)
  9096. (save-excursion
  9097. (goto-char (point-min))
  9098. (while (re-search-forward
  9099. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9100. (when (equal (char-after (point-at-bol 0)) ?*)
  9101. (mapc (lambda (x) (add-to-list 'tags x))
  9102. (org-split-string (org-match-string-no-properties 1) ":")))))
  9103. (mapcar 'list tags)))
  9104. ;;;; The mapping API
  9105. ;;;###autoload
  9106. (defun org-map-entries (func &optional match scope &rest skip)
  9107. "Call FUNC at each headline selected by MATCH in SCOPE.
  9108. FUNC is a function or a lisp form. The function will be called without
  9109. arguments, with the cursor positioned at the beginning of the headline.
  9110. The return values of all calls to the function will be collected and
  9111. returned as a list.
  9112. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9113. Only headlines that are matched by this query will be considered during
  9114. the iteration. When MATCH is nil or t, all headlines will be
  9115. visited by the iteration.
  9116. SCOPE determines the scope of this command. It can be any of:
  9117. nil The current buffer, respecting the restriction if any
  9118. tree The subtree started with the entry at point
  9119. file The current buffer, without restriction
  9120. file-with-archives
  9121. The current buffer, and any archives associated with it
  9122. agenda All agenda files
  9123. agenda-with-archives
  9124. All agenda files with any archive files associated with them
  9125. \(file1 file2 ...)
  9126. If this is a list, all files in the list will be scanned
  9127. The remaining args are treated as settings for the skipping facilities of
  9128. the scanner. The following items can be given here:
  9129. archive skip trees with the archive tag.
  9130. comment skip trees with the COMMENT keyword
  9131. function or Emacs Lisp form:
  9132. will be used as value for `org-agenda-skip-function', so whenever
  9133. the the function returns t, FUNC will not be called for that
  9134. entry and search will continue from the point where the
  9135. function leaves it."
  9136. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9137. (org-agenda-skip-archived-trees (memq 'archive skip))
  9138. (org-agenda-skip-comment-trees (memq 'comment skip))
  9139. (org-agenda-skip-function
  9140. (car (org-delete-all '(comment archive) skip)))
  9141. (org-tags-match-list-sublevels t)
  9142. matcher pos file res
  9143. org-todo-keywords-for-agenda
  9144. org-done-keywords-for-agenda
  9145. org-todo-keyword-alist-for-agenda
  9146. org-tag-alist-for-agenda)
  9147. (cond
  9148. ((eq match t) (setq matcher t))
  9149. ((eq match nil) (setq matcher t))
  9150. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9151. (when (eq scope 'tree)
  9152. (org-back-to-heading t)
  9153. (org-narrow-to-subtree)
  9154. (setq scope nil))
  9155. (if (not scope)
  9156. (progn
  9157. (org-prepare-agenda-buffers
  9158. (list (buffer-file-name (current-buffer))))
  9159. (org-scan-tags func matcher))
  9160. ;; Get the right scope
  9161. (setq pos (point))
  9162. (cond
  9163. ((and scope (listp scope) (symbolp (car scope)))
  9164. (setq scope (eval scope)))
  9165. ((eq scope 'agenda)
  9166. (setq scope (org-agenda-files t)))
  9167. ((eq scope 'agenda-with-archives)
  9168. (setq scope (org-agenda-files t))
  9169. (setq scope (org-add-archive-files scope)))
  9170. ((eq scope 'file)
  9171. (setq scope (list (buffer-file-name))))
  9172. ((eq scope 'file-with-archives)
  9173. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9174. (org-prepare-agenda-buffers scope)
  9175. (while (setq file (pop scope))
  9176. (with-current-buffer (org-find-base-buffer-visiting file)
  9177. (save-excursion
  9178. (save-restriction
  9179. (widen)
  9180. (goto-char (point-min))
  9181. (setq res (append res (org-scan-tags func matcher)))))))
  9182. res)))
  9183. ;;;; Properties
  9184. ;;; Setting and retrieving properties
  9185. (defconst org-special-properties
  9186. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9187. "TIMESTAMP" "TIMESTAMP_IA")
  9188. "The special properties valid in Org-mode.
  9189. These are properties that are not defined in the property drawer,
  9190. but in some other way.")
  9191. (defconst org-default-properties
  9192. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9193. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9194. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9195. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9196. "Some properties that are used by Org-mode for various purposes.
  9197. Being in this list makes sure that they are offered for completion.")
  9198. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9199. "Regular expression matching the first line of a property drawer.")
  9200. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9201. "Regular expression matching the first line of a property drawer.")
  9202. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9203. "Regular expression matching the first line of a property drawer.")
  9204. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9205. "Regular expression matching the first line of a property drawer.")
  9206. (defconst org-property-drawer-re
  9207. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9208. org-property-end-re "\\)\n?")
  9209. "Matches an entire property drawer.")
  9210. (defconst org-clock-drawer-re
  9211. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9212. org-property-end-re "\\)\n?")
  9213. "Matches an entire clock drawer.")
  9214. (defun org-property-action ()
  9215. "Do an action on properties."
  9216. (interactive)
  9217. (let (c)
  9218. (org-at-property-p)
  9219. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9220. (setq c (read-char-exclusive))
  9221. (cond
  9222. ((equal c ?s)
  9223. (call-interactively 'org-set-property))
  9224. ((equal c ?d)
  9225. (call-interactively 'org-delete-property))
  9226. ((equal c ?D)
  9227. (call-interactively 'org-delete-property-globally))
  9228. ((equal c ?c)
  9229. (call-interactively 'org-compute-property-at-point))
  9230. (t (error "No such property action %c" c)))))
  9231. (defun org-at-property-p ()
  9232. "Is the cursor in a property line?"
  9233. ;; FIXME: Does not check if we are actually in the drawer.
  9234. ;; FIXME: also returns true on any drawers.....
  9235. ;; This is used by C-c C-c for property action.
  9236. (save-excursion
  9237. (beginning-of-line 1)
  9238. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9239. (defun org-get-property-block (&optional beg end force)
  9240. "Return the (beg . end) range of the body of the property drawer.
  9241. BEG and END can be beginning and end of subtree, if not given
  9242. they will be found.
  9243. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9244. (catch 'exit
  9245. (save-excursion
  9246. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9247. (end (or end (progn (outline-next-heading) (point)))))
  9248. (goto-char beg)
  9249. (if (re-search-forward org-property-start-re end t)
  9250. (setq beg (1+ (match-end 0)))
  9251. (if force
  9252. (save-excursion
  9253. (org-insert-property-drawer)
  9254. (setq end (progn (outline-next-heading) (point))))
  9255. (throw 'exit nil))
  9256. (goto-char beg)
  9257. (if (re-search-forward org-property-start-re end t)
  9258. (setq beg (1+ (match-end 0)))))
  9259. (if (re-search-forward org-property-end-re end t)
  9260. (setq end (match-beginning 0))
  9261. (or force (throw 'exit nil))
  9262. (goto-char beg)
  9263. (setq end beg)
  9264. (org-indent-line-function)
  9265. (insert ":END:\n"))
  9266. (cons beg end)))))
  9267. (defun org-entry-properties (&optional pom which)
  9268. "Get all properties of the entry at point-or-marker POM.
  9269. This includes the TODO keyword, the tags, time strings for deadline,
  9270. scheduled, and clocking, and any additional properties defined in the
  9271. entry. The return value is an alist, keys may occur multiple times
  9272. if the property key was used several times.
  9273. POM may also be nil, in which case the current entry is used.
  9274. If WHICH is nil or `all', get all properties. If WHICH is
  9275. `special' or `standard', only get that subclass."
  9276. (setq which (or which 'all))
  9277. (org-with-point-at pom
  9278. (let ((clockstr (substring org-clock-string 0 -1))
  9279. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9280. beg end range props sum-props key value string clocksum)
  9281. (save-excursion
  9282. (when (condition-case nil (org-back-to-heading t) (error nil))
  9283. (setq beg (point))
  9284. (setq sum-props (get-text-property (point) 'org-summaries))
  9285. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9286. (outline-next-heading)
  9287. (setq end (point))
  9288. (when (memq which '(all special))
  9289. ;; Get the special properties, like TODO and tags
  9290. (goto-char beg)
  9291. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9292. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9293. (when (looking-at org-priority-regexp)
  9294. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9295. (when (and (setq value (org-get-tags-string))
  9296. (string-match "\\S-" value))
  9297. (push (cons "TAGS" value) props))
  9298. (when (setq value (org-get-tags-at))
  9299. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9300. props))
  9301. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9302. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9303. string (if (equal key clockstr)
  9304. (org-no-properties
  9305. (org-trim
  9306. (buffer-substring
  9307. (match-beginning 3) (goto-char (point-at-eol)))))
  9308. (substring (org-match-string-no-properties 3) 1 -1)))
  9309. (unless key
  9310. (if (= (char-after (match-beginning 3)) ?\[)
  9311. (setq key "TIMESTAMP_IA")
  9312. (setq key "TIMESTAMP")))
  9313. (when (or (equal key clockstr) (not (assoc key props)))
  9314. (push (cons key string) props)))
  9315. )
  9316. (when (memq which '(all standard))
  9317. ;; Get the standard properties, like :PORP: ...
  9318. (setq range (org-get-property-block beg end))
  9319. (when range
  9320. (goto-char (car range))
  9321. (while (re-search-forward
  9322. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9323. (cdr range) t)
  9324. (setq key (org-match-string-no-properties 1)
  9325. value (org-trim (or (org-match-string-no-properties 2) "")))
  9326. (unless (member key excluded)
  9327. (push (cons key (or value "")) props)))))
  9328. (if clocksum
  9329. (push (cons "CLOCKSUM"
  9330. (org-columns-number-to-string (/ (float clocksum) 60.)
  9331. 'add_times))
  9332. props))
  9333. (unless (assoc "CATEGORY" props)
  9334. (setq value (or (org-get-category)
  9335. (progn (org-refresh-category-properties)
  9336. (org-get-category))))
  9337. (push (cons "CATEGORY" value) props))
  9338. (append sum-props (nreverse props)))))))
  9339. (defun org-entry-get (pom property &optional inherit)
  9340. "Get value of PROPERTY for entry at point-or-marker POM.
  9341. If INHERIT is non-nil and the entry does not have the property,
  9342. then also check higher levels of the hierarchy.
  9343. If INHERIT is the symbol `selective', use inheritance only if the setting
  9344. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9345. If the property is present but empty, the return value is the empty string.
  9346. If the property is not present at all, nil is returned."
  9347. (org-with-point-at pom
  9348. (if (and inherit (if (eq inherit 'selective)
  9349. (org-property-inherit-p property)
  9350. t))
  9351. (org-entry-get-with-inheritance property)
  9352. (if (member property org-special-properties)
  9353. ;; We need a special property. Use brute force, get all properties.
  9354. (cdr (assoc property (org-entry-properties nil 'special)))
  9355. (let ((range (org-get-property-block)))
  9356. (if (and range
  9357. (goto-char (car range))
  9358. (re-search-forward
  9359. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9360. (cdr range) t))
  9361. ;; Found the property, return it.
  9362. (if (match-end 1)
  9363. (org-match-string-no-properties 1)
  9364. "")))))))
  9365. (defun org-property-or-variable-value (var &optional inherit)
  9366. "Check if there is a property fixing the value of VAR.
  9367. If yes, return this value. If not, return the current value of the variable."
  9368. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9369. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9370. (read prop)
  9371. (symbol-value var))))
  9372. (defun org-entry-delete (pom property)
  9373. "Delete the property PROPERTY from entry at point-or-marker POM."
  9374. (org-with-point-at pom
  9375. (if (member property org-special-properties)
  9376. nil ; cannot delete these properties.
  9377. (let ((range (org-get-property-block)))
  9378. (if (and range
  9379. (goto-char (car range))
  9380. (re-search-forward
  9381. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9382. (cdr range) t))
  9383. (progn
  9384. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9385. t)
  9386. nil)))))
  9387. ;; Multi-values properties are properties that contain multiple values
  9388. ;; These values are assumed to be single words, separated by whitespace.
  9389. (defun org-entry-add-to-multivalued-property (pom property value)
  9390. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9391. (let* ((old (org-entry-get pom property))
  9392. (values (and old (org-split-string old "[ \t]"))))
  9393. (setq value (org-entry-protect-space value))
  9394. (unless (member value values)
  9395. (setq values (cons value values))
  9396. (org-entry-put pom property
  9397. (mapconcat 'identity values " ")))))
  9398. (defun org-entry-remove-from-multivalued-property (pom property value)
  9399. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9400. (let* ((old (org-entry-get pom property))
  9401. (values (and old (org-split-string old "[ \t]"))))
  9402. (setq value (org-entry-protect-space value))
  9403. (when (member value values)
  9404. (setq values (delete value values))
  9405. (org-entry-put pom property
  9406. (mapconcat 'identity values " ")))))
  9407. (defun org-entry-member-in-multivalued-property (pom property value)
  9408. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9409. (let* ((old (org-entry-get pom property))
  9410. (values (and old (org-split-string old "[ \t]"))))
  9411. (setq value (org-entry-protect-space value))
  9412. (member value values)))
  9413. (defun org-entry-get-multivalued-property (pom property)
  9414. "Return a list of values in a multivalued property."
  9415. (let* ((value (org-entry-get pom property))
  9416. (values (and value (org-split-string value "[ \t]"))))
  9417. (mapcar 'org-entry-restore-space values)))
  9418. (defun org-entry-put-multivalued-property (pom property &rest values)
  9419. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9420. VALUES should be a list of strings. Spaces will be protected."
  9421. (org-entry-put pom property
  9422. (mapconcat 'org-entry-protect-space values " "))
  9423. (let* ((value (org-entry-get pom property))
  9424. (values (and value (org-split-string value "[ \t]"))))
  9425. (mapcar 'org-entry-restore-space values)))
  9426. (defun org-entry-protect-space (s)
  9427. "Protect spaces and newline in string S."
  9428. (while (string-match " " s)
  9429. (setq s (replace-match "%20" t t s)))
  9430. (while (string-match "\n" s)
  9431. (setq s (replace-match "%0A" t t s)))
  9432. s)
  9433. (defun org-entry-restore-space (s)
  9434. "Restore spaces and newline in string S."
  9435. (while (string-match "%20" s)
  9436. (setq s (replace-match " " t t s)))
  9437. (while (string-match "%0A" s)
  9438. (setq s (replace-match "\n" t t s)))
  9439. s)
  9440. (defvar org-entry-property-inherited-from (make-marker)
  9441. "Marker pointing to the entry from where a property was inherited.
  9442. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9443. location of the entry where the inheritance search matched. If there was
  9444. no match, the marker will point nowhere.
  9445. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9446. is set.")
  9447. (defun org-entry-get-with-inheritance (property)
  9448. "Get entry property, and search higher levels if not present."
  9449. (move-marker org-entry-property-inherited-from nil)
  9450. (let (tmp)
  9451. (save-excursion
  9452. (save-restriction
  9453. (widen)
  9454. (catch 'ex
  9455. (while t
  9456. (when (setq tmp (org-entry-get nil property))
  9457. (org-back-to-heading t)
  9458. (move-marker org-entry-property-inherited-from (point))
  9459. (throw 'ex tmp))
  9460. (or (org-up-heading-safe) (throw 'ex nil)))))
  9461. (or tmp
  9462. (cdr (assoc property org-file-properties))
  9463. (cdr (assoc property org-global-properties))
  9464. (cdr (assoc property org-global-properties-fixed))))))
  9465. (defun org-entry-put (pom property value)
  9466. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9467. (org-with-point-at pom
  9468. (org-back-to-heading t)
  9469. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9470. range)
  9471. (cond
  9472. ((equal property "TODO")
  9473. (when (and (stringp value) (string-match "\\S-" value)
  9474. (not (member value org-todo-keywords-1)))
  9475. (error "\"%s\" is not a valid TODO state" value))
  9476. (if (or (not value)
  9477. (not (string-match "\\S-" value)))
  9478. (setq value 'none))
  9479. (org-todo value)
  9480. (org-set-tags nil 'align))
  9481. ((equal property "PRIORITY")
  9482. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9483. (string-to-char value) ?\ ))
  9484. (org-set-tags nil 'align))
  9485. ((equal property "SCHEDULED")
  9486. (if (re-search-forward org-scheduled-time-regexp end t)
  9487. (cond
  9488. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9489. ((eq value 'later) (org-timestamp-change 1 'day))
  9490. (t (call-interactively 'org-schedule)))
  9491. (call-interactively 'org-schedule)))
  9492. ((equal property "DEADLINE")
  9493. (if (re-search-forward org-deadline-time-regexp end t)
  9494. (cond
  9495. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9496. ((eq value 'later) (org-timestamp-change 1 'day))
  9497. (t (call-interactively 'org-deadline)))
  9498. (call-interactively 'org-deadline)))
  9499. ((member property org-special-properties)
  9500. (error "The %s property can not yet be set with `org-entry-put'"
  9501. property))
  9502. (t ; a non-special property
  9503. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9504. (setq range (org-get-property-block beg end 'force))
  9505. (goto-char (car range))
  9506. (if (re-search-forward
  9507. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9508. (progn
  9509. (delete-region (match-beginning 1) (match-end 1))
  9510. (goto-char (match-beginning 1)))
  9511. (goto-char (cdr range))
  9512. (insert "\n")
  9513. (backward-char 1)
  9514. (org-indent-line-function)
  9515. (insert ":" property ":"))
  9516. (and value (insert " " value))
  9517. (org-indent-line-function)))))))
  9518. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9519. "Get all property keys in the current buffer.
  9520. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9521. like tags and TODO state.
  9522. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9523. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9524. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9525. formats in the current buffer."
  9526. (let (rtn range cfmt cols s p)
  9527. (save-excursion
  9528. (save-restriction
  9529. (widen)
  9530. (goto-char (point-min))
  9531. (while (re-search-forward org-property-start-re nil t)
  9532. (setq range (org-get-property-block))
  9533. (goto-char (car range))
  9534. (while (re-search-forward
  9535. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9536. (cdr range) t)
  9537. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9538. (outline-next-heading))))
  9539. (when include-specials
  9540. (setq rtn (append org-special-properties rtn)))
  9541. (when include-defaults
  9542. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9543. (when include-columns
  9544. (save-excursion
  9545. (save-restriction
  9546. (widen)
  9547. (goto-char (point-min))
  9548. (while (re-search-forward
  9549. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9550. nil t)
  9551. (setq cfmt (match-string 2) s 0)
  9552. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9553. cfmt s)
  9554. (setq s (match-end 0)
  9555. p (match-string 1 cfmt))
  9556. (unless (or (equal p "ITEM")
  9557. (member p org-special-properties))
  9558. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9559. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9560. (defun org-property-values (key)
  9561. "Return a list of all values of property KEY."
  9562. (save-excursion
  9563. (save-restriction
  9564. (widen)
  9565. (goto-char (point-min))
  9566. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9567. values)
  9568. (while (re-search-forward re nil t)
  9569. (add-to-list 'values (org-trim (match-string 1))))
  9570. (delete "" values)))))
  9571. (defun org-insert-property-drawer ()
  9572. "Insert a property drawer into the current entry."
  9573. (interactive)
  9574. (org-back-to-heading t)
  9575. (looking-at outline-regexp)
  9576. (let ((indent (- (match-end 0)(match-beginning 0)))
  9577. (beg (point))
  9578. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9579. end hiddenp)
  9580. (outline-next-heading)
  9581. (setq end (point))
  9582. (goto-char beg)
  9583. (while (re-search-forward re end t))
  9584. (setq hiddenp (org-invisible-p))
  9585. (end-of-line 1)
  9586. (and (equal (char-after) ?\n) (forward-char 1))
  9587. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9588. (beginning-of-line 2))
  9589. (org-skip-over-state-notes)
  9590. (skip-chars-backward " \t\n\r")
  9591. (if (eq (char-before) ?*) (forward-char 1))
  9592. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9593. (beginning-of-line 0)
  9594. (org-indent-to-column indent)
  9595. (beginning-of-line 2)
  9596. (org-indent-to-column indent)
  9597. (beginning-of-line 0)
  9598. (if hiddenp
  9599. (save-excursion
  9600. (org-back-to-heading t)
  9601. (hide-entry))
  9602. (org-flag-drawer t))))
  9603. (defun org-set-property (property value)
  9604. "In the current entry, set PROPERTY to VALUE.
  9605. When called interactively, this will prompt for a property name, offering
  9606. completion on existing and default properties. And then it will prompt
  9607. for a value, offering completion either on allowed values (via an inherited
  9608. xxx_ALL property) or on existing values in other instances of this property
  9609. in the current file."
  9610. (interactive
  9611. (let* ((completion-ignore-case t)
  9612. (keys (org-buffer-property-keys nil t t))
  9613. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9614. (prop (if (member prop0 keys)
  9615. prop0
  9616. (or (cdr (assoc (downcase prop0)
  9617. (mapcar (lambda (x) (cons (downcase x) x))
  9618. keys)))
  9619. prop0)))
  9620. (cur (org-entry-get nil prop))
  9621. (allowed (org-property-get-allowed-values nil prop 'table))
  9622. (existing (mapcar 'list (org-property-values prop)))
  9623. (val (if allowed
  9624. (org-completing-read "Value: " allowed nil 'req-match)
  9625. (org-completing-read
  9626. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9627. (concat "[" cur "]") "")
  9628. ": ")
  9629. existing nil nil "" nil cur))))
  9630. (list prop (if (equal val "") cur val))))
  9631. (unless (equal (org-entry-get nil property) value)
  9632. (org-entry-put nil property value)))
  9633. (defun org-delete-property (property)
  9634. "In the current entry, delete PROPERTY."
  9635. (interactive
  9636. (let* ((completion-ignore-case t)
  9637. (prop (org-ido-completing-read
  9638. "Property: " (org-entry-properties nil 'standard))))
  9639. (list prop)))
  9640. (message "Property %s %s" property
  9641. (if (org-entry-delete nil property)
  9642. "deleted"
  9643. "was not present in the entry")))
  9644. (defun org-delete-property-globally (property)
  9645. "Remove PROPERTY globally, from all entries."
  9646. (interactive
  9647. (let* ((completion-ignore-case t)
  9648. (prop (org-ido-completing-read
  9649. "Globally remove property: "
  9650. (mapcar 'list (org-buffer-property-keys)))))
  9651. (list prop)))
  9652. (save-excursion
  9653. (save-restriction
  9654. (widen)
  9655. (goto-char (point-min))
  9656. (let ((cnt 0))
  9657. (while (re-search-forward
  9658. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9659. nil t)
  9660. (setq cnt (1+ cnt))
  9661. (replace-match ""))
  9662. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9663. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9664. (defun org-compute-property-at-point ()
  9665. "Compute the property at point.
  9666. This looks for an enclosing column format, extracts the operator and
  9667. then applies it to the property in the column format's scope."
  9668. (interactive)
  9669. (unless (org-at-property-p)
  9670. (error "Not at a property"))
  9671. (let ((prop (org-match-string-no-properties 2)))
  9672. (org-columns-get-format-and-top-level)
  9673. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9674. (error "No operator defined for property %s" prop))
  9675. (org-columns-compute prop)))
  9676. (defun org-property-get-allowed-values (pom property &optional table)
  9677. "Get allowed values for the property PROPERTY.
  9678. When TABLE is non-nil, return an alist that can directly be used for
  9679. completion."
  9680. (let (vals)
  9681. (cond
  9682. ((equal property "TODO")
  9683. (setq vals (org-with-point-at pom
  9684. (append org-todo-keywords-1 '("")))))
  9685. ((equal property "PRIORITY")
  9686. (let ((n org-lowest-priority))
  9687. (while (>= n org-highest-priority)
  9688. (push (char-to-string n) vals)
  9689. (setq n (1- n)))))
  9690. ((member property org-special-properties))
  9691. (t
  9692. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9693. (when (and vals (string-match "\\S-" vals))
  9694. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9695. (setq vals (mapcar (lambda (x)
  9696. (cond ((stringp x) x)
  9697. ((numberp x) (number-to-string x))
  9698. ((symbolp x) (symbol-name x))
  9699. (t "???")))
  9700. vals)))))
  9701. (if table (mapcar 'list vals) vals)))
  9702. (defun org-property-previous-allowed-value (&optional previous)
  9703. "Switch to the next allowed value for this property."
  9704. (interactive)
  9705. (org-property-next-allowed-value t))
  9706. (defun org-property-next-allowed-value (&optional previous)
  9707. "Switch to the next allowed value for this property."
  9708. (interactive)
  9709. (unless (org-at-property-p)
  9710. (error "Not at a property"))
  9711. (let* ((key (match-string 2))
  9712. (value (match-string 3))
  9713. (allowed (or (org-property-get-allowed-values (point) key)
  9714. (and (member value '("[ ]" "[-]" "[X]"))
  9715. '("[ ]" "[X]"))))
  9716. nval)
  9717. (unless allowed
  9718. (error "Allowed values for this property have not been defined"))
  9719. (if previous (setq allowed (reverse allowed)))
  9720. (if (member value allowed)
  9721. (setq nval (car (cdr (member value allowed)))))
  9722. (setq nval (or nval (car allowed)))
  9723. (if (equal nval value)
  9724. (error "Only one allowed value for this property"))
  9725. (org-at-property-p)
  9726. (replace-match (concat " :" key ": " nval) t t)
  9727. (org-indent-line-function)
  9728. (beginning-of-line 1)
  9729. (skip-chars-forward " \t")))
  9730. (defun org-find-entry-with-id (ident)
  9731. "Locate the entry that contains the ID property with exact value IDENT.
  9732. IDENT can be a string, a symbol or a number, this function will search for
  9733. the string representation of it.
  9734. Return the position where this entry starts, or nil if there is no such entry."
  9735. (interactive "sID: ")
  9736. (let ((id (cond
  9737. ((stringp ident) ident)
  9738. ((symbol-name ident) (symbol-name ident))
  9739. ((numberp ident) (number-to-string ident))
  9740. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9741. (case-fold-search nil))
  9742. (save-excursion
  9743. (save-restriction
  9744. (widen)
  9745. (goto-char (point-min))
  9746. (when (re-search-forward
  9747. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9748. nil t)
  9749. (org-back-to-heading)
  9750. (point))))))
  9751. ;;;; Timestamps
  9752. (defvar org-last-changed-timestamp nil)
  9753. (defvar org-last-inserted-timestamp nil
  9754. "The last time stamp inserted with `org-insert-time-stamp'.")
  9755. (defvar org-time-was-given) ; dynamically scoped parameter
  9756. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9757. (defvar org-ts-what) ; dynamically scoped parameter
  9758. (defun org-time-stamp (arg &optional inactive)
  9759. "Prompt for a date/time and insert a time stamp.
  9760. If the user specifies a time like HH:MM, or if this command is called
  9761. with a prefix argument, the time stamp will contain date and time.
  9762. Otherwise, only the date will be included. All parts of a date not
  9763. specified by the user will be filled in from the current date/time.
  9764. So if you press just return without typing anything, the time stamp
  9765. will represent the current date/time. If there is already a timestamp
  9766. at the cursor, it will be modified."
  9767. (interactive "P")
  9768. (let* ((ts nil)
  9769. (default-time
  9770. ;; Default time is either today, or, when entering a range,
  9771. ;; the range start.
  9772. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9773. (save-excursion
  9774. (re-search-backward
  9775. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9776. (- (point) 20) t)))
  9777. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9778. (current-time)))
  9779. (default-input (and ts (org-get-compact-tod ts)))
  9780. org-time-was-given org-end-time-was-given time)
  9781. (cond
  9782. ((and (org-at-timestamp-p t)
  9783. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9784. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9785. (insert "--")
  9786. (setq time (let ((this-command this-command))
  9787. (org-read-date arg 'totime nil nil
  9788. default-time default-input)))
  9789. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9790. ((org-at-timestamp-p t)
  9791. (setq time (let ((this-command this-command))
  9792. (org-read-date arg 'totime nil nil default-time default-input)))
  9793. (when (org-at-timestamp-p t) ; just to get the match data
  9794. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9795. (replace-match "")
  9796. (setq org-last-changed-timestamp
  9797. (org-insert-time-stamp
  9798. time (or org-time-was-given arg)
  9799. inactive nil nil (list org-end-time-was-given))))
  9800. (message "Timestamp updated"))
  9801. (t
  9802. (setq time (let ((this-command this-command))
  9803. (org-read-date arg 'totime nil nil default-time default-input)))
  9804. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9805. nil nil (list org-end-time-was-given))))))
  9806. ;; FIXME: can we use this for something else, like computing time differences?
  9807. (defun org-get-compact-tod (s)
  9808. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9809. (let* ((t1 (match-string 1 s))
  9810. (h1 (string-to-number (match-string 2 s)))
  9811. (m1 (string-to-number (match-string 3 s)))
  9812. (t2 (and (match-end 4) (match-string 5 s)))
  9813. (h2 (and t2 (string-to-number (match-string 6 s))))
  9814. (m2 (and t2 (string-to-number (match-string 7 s))))
  9815. dh dm)
  9816. (if (not t2)
  9817. t1
  9818. (setq dh (- h2 h1) dm (- m2 m1))
  9819. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9820. (concat t1 "+" (number-to-string dh)
  9821. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9822. (defun org-time-stamp-inactive (&optional arg)
  9823. "Insert an inactive time stamp.
  9824. An inactive time stamp is enclosed in square brackets instead of angle
  9825. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9826. does not link to the calendar and cannot be changed with the S-cursor keys.
  9827. So these are more for recording a certain time/date."
  9828. (interactive "P")
  9829. (org-time-stamp arg 'inactive))
  9830. (defvar org-date-ovl (org-make-overlay 1 1))
  9831. (org-overlay-put org-date-ovl 'face 'org-warning)
  9832. (org-detach-overlay org-date-ovl)
  9833. (defvar org-ans1) ; dynamically scoped parameter
  9834. (defvar org-ans2) ; dynamically scoped parameter
  9835. (defvar org-plain-time-of-day-regexp) ; defined below
  9836. (defvar org-overriding-default-time nil) ; dynamically scoped
  9837. (defvar org-read-date-overlay nil)
  9838. (defvar org-dcst nil) ; dynamically scoped
  9839. (defun org-read-date (&optional with-time to-time from-string prompt
  9840. default-time default-input)
  9841. "Read a date, possibly a time, and make things smooth for the user.
  9842. The prompt will suggest to enter an ISO date, but you can also enter anything
  9843. which will at least partially be understood by `parse-time-string'.
  9844. Unrecognized parts of the date will default to the current day, month, year,
  9845. hour and minute. If this command is called to replace a timestamp at point,
  9846. of to enter the second timestamp of a range, the default time is taken from the
  9847. existing stamp. For example,
  9848. 3-2-5 --> 2003-02-05
  9849. feb 15 --> currentyear-02-15
  9850. sep 12 9 --> 2009-09-12
  9851. 12:45 --> today 12:45
  9852. 22 sept 0:34 --> currentyear-09-22 0:34
  9853. 12 --> currentyear-currentmonth-12
  9854. Fri --> nearest Friday (today or later)
  9855. etc.
  9856. Furthermore you can specify a relative date by giving, as the *first* thing
  9857. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9858. change in days weeks, months, years.
  9859. With a single plus or minus, the date is relative to today. With a double
  9860. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9861. +4d --> four days from today
  9862. +4 --> same as above
  9863. +2w --> two weeks from today
  9864. ++5 --> five days from default date
  9865. The function understands only English month and weekday abbreviations,
  9866. but this can be configured with the variables `parse-time-months' and
  9867. `parse-time-weekdays'.
  9868. While prompting, a calendar is popped up - you can also select the
  9869. date with the mouse (button 1). The calendar shows a period of three
  9870. months. To scroll it to other months, use the keys `>' and `<'.
  9871. If you don't like the calendar, turn it off with
  9872. \(setq org-read-date-popup-calendar nil)
  9873. With optional argument TO-TIME, the date will immediately be converted
  9874. to an internal time.
  9875. With an optional argument WITH-TIME, the prompt will suggest to also
  9876. insert a time. Note that when WITH-TIME is not set, you can still
  9877. enter a time, and this function will inform the calling routine about
  9878. this change. The calling routine may then choose to change the format
  9879. used to insert the time stamp into the buffer to include the time.
  9880. With optional argument FROM-STRING, read from this string instead from
  9881. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9882. the time/date that is used for everything that is not specified by the
  9883. user."
  9884. (require 'parse-time)
  9885. (let* ((org-time-stamp-rounding-minutes
  9886. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9887. (org-dcst org-display-custom-times)
  9888. (ct (org-current-time))
  9889. (def (or org-overriding-default-time default-time ct))
  9890. (defdecode (decode-time def))
  9891. (dummy (progn
  9892. (when (< (nth 2 defdecode) org-extend-today-until)
  9893. (setcar (nthcdr 2 defdecode) -1)
  9894. (setcar (nthcdr 1 defdecode) 59)
  9895. (setq def (apply 'encode-time defdecode)
  9896. defdecode (decode-time def)))))
  9897. (calendar-move-hook nil)
  9898. (calendar-view-diary-initially-flag nil)
  9899. (view-diary-entries-initially nil)
  9900. (calendar-view-holidays-initially-flag nil)
  9901. (view-calendar-holidays-initially nil)
  9902. (timestr (format-time-string
  9903. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9904. (prompt (concat (if prompt (concat prompt " ") "")
  9905. (format "Date+time [%s]: " timestr)))
  9906. ans (org-ans0 "") org-ans1 org-ans2 final)
  9907. (cond
  9908. (from-string (setq ans from-string))
  9909. (org-read-date-popup-calendar
  9910. (save-excursion
  9911. (save-window-excursion
  9912. (calendar)
  9913. (calendar-forward-day (- (time-to-days def)
  9914. (calendar-absolute-from-gregorian
  9915. (calendar-current-date))))
  9916. (org-eval-in-calendar nil t)
  9917. (let* ((old-map (current-local-map))
  9918. (map (copy-keymap calendar-mode-map))
  9919. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9920. (org-defkey map (kbd "RET") 'org-calendar-select)
  9921. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9922. 'org-calendar-select-mouse)
  9923. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9924. 'org-calendar-select-mouse)
  9925. (org-defkey minibuffer-local-map [(meta shift left)]
  9926. (lambda () (interactive)
  9927. (org-eval-in-calendar '(calendar-backward-month 1))))
  9928. (org-defkey minibuffer-local-map [(meta shift right)]
  9929. (lambda () (interactive)
  9930. (org-eval-in-calendar '(calendar-forward-month 1))))
  9931. (org-defkey minibuffer-local-map [(meta shift up)]
  9932. (lambda () (interactive)
  9933. (org-eval-in-calendar '(calendar-backward-year 1))))
  9934. (org-defkey minibuffer-local-map [(meta shift down)]
  9935. (lambda () (interactive)
  9936. (org-eval-in-calendar '(calendar-forward-year 1))))
  9937. (org-defkey minibuffer-local-map [(shift up)]
  9938. (lambda () (interactive)
  9939. (org-eval-in-calendar '(calendar-backward-week 1))))
  9940. (org-defkey minibuffer-local-map [(shift down)]
  9941. (lambda () (interactive)
  9942. (org-eval-in-calendar '(calendar-forward-week 1))))
  9943. (org-defkey minibuffer-local-map [(shift left)]
  9944. (lambda () (interactive)
  9945. (org-eval-in-calendar '(calendar-backward-day 1))))
  9946. (org-defkey minibuffer-local-map [(shift right)]
  9947. (lambda () (interactive)
  9948. (org-eval-in-calendar '(calendar-forward-day 1))))
  9949. (org-defkey minibuffer-local-map ">"
  9950. (lambda () (interactive)
  9951. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9952. (org-defkey minibuffer-local-map "<"
  9953. (lambda () (interactive)
  9954. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9955. (unwind-protect
  9956. (progn
  9957. (use-local-map map)
  9958. (add-hook 'post-command-hook 'org-read-date-display)
  9959. (setq org-ans0 (read-string prompt default-input nil nil))
  9960. ;; org-ans0: from prompt
  9961. ;; org-ans1: from mouse click
  9962. ;; org-ans2: from calendar motion
  9963. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9964. (remove-hook 'post-command-hook 'org-read-date-display)
  9965. (use-local-map old-map)
  9966. (when org-read-date-overlay
  9967. (org-delete-overlay org-read-date-overlay)
  9968. (setq org-read-date-overlay nil)))))))
  9969. (t ; Naked prompt only
  9970. (unwind-protect
  9971. (setq ans (read-string prompt default-input nil timestr))
  9972. (when org-read-date-overlay
  9973. (org-delete-overlay org-read-date-overlay)
  9974. (setq org-read-date-overlay nil)))))
  9975. (setq final (org-read-date-analyze ans def defdecode))
  9976. (if to-time
  9977. (apply 'encode-time final)
  9978. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9979. (format "%04d-%02d-%02d %02d:%02d"
  9980. (nth 5 final) (nth 4 final) (nth 3 final)
  9981. (nth 2 final) (nth 1 final))
  9982. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9983. (defvar def)
  9984. (defvar defdecode)
  9985. (defvar with-time)
  9986. (defun org-read-date-display ()
  9987. "Display the current date prompt interpretation in the minibuffer."
  9988. (when org-read-date-display-live
  9989. (when org-read-date-overlay
  9990. (org-delete-overlay org-read-date-overlay))
  9991. (let ((p (point)))
  9992. (end-of-line 1)
  9993. (while (not (equal (buffer-substring
  9994. (max (point-min) (- (point) 4)) (point))
  9995. " "))
  9996. (insert " "))
  9997. (goto-char p))
  9998. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9999. " " (or org-ans1 org-ans2)))
  10000. (org-end-time-was-given nil)
  10001. (f (org-read-date-analyze ans def defdecode))
  10002. (fmts (if org-dcst
  10003. org-time-stamp-custom-formats
  10004. org-time-stamp-formats))
  10005. (fmt (if (or with-time
  10006. (and (boundp 'org-time-was-given) org-time-was-given))
  10007. (cdr fmts)
  10008. (car fmts)))
  10009. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10010. (when (and org-end-time-was-given
  10011. (string-match org-plain-time-of-day-regexp txt))
  10012. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10013. org-end-time-was-given
  10014. (substring txt (match-end 0)))))
  10015. (setq org-read-date-overlay
  10016. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10017. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10018. (defun org-read-date-analyze (ans def defdecode)
  10019. "Analyse the combined answer of the date prompt."
  10020. ;; FIXME: cleanup and comment
  10021. (let (delta deltan deltaw deltadef year month day
  10022. hour minute second wday pm h2 m2 tl wday1
  10023. iso-year iso-weekday iso-week iso-year iso-date)
  10024. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10025. (setq ans "+0"))
  10026. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10027. (setq ans (replace-match "" t t ans)
  10028. deltan (car delta)
  10029. deltaw (nth 1 delta)
  10030. deltadef (nth 2 delta)))
  10031. ;; Check if there is an iso week date in there
  10032. ;; If yes, sore the info and postpone interpreting it until the rest
  10033. ;; of the parsing is done
  10034. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10035. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10036. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10037. iso-week (string-to-number (match-string 2 ans)))
  10038. (setq ans (replace-match "" t t ans)))
  10039. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10040. (when (string-match
  10041. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10042. (setq year (if (match-end 2)
  10043. (string-to-number (match-string 2 ans))
  10044. (string-to-number (format-time-string "%Y")))
  10045. month (string-to-number (match-string 3 ans))
  10046. day (string-to-number (match-string 4 ans)))
  10047. (if (< year 100) (setq year (+ 2000 year)))
  10048. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10049. t nil ans)))
  10050. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10051. ;; If there is a time with am/pm, and *no* time without it, we convert
  10052. ;; so that matching will be successful.
  10053. (loop for i from 1 to 2 do ; twice, for end time as well
  10054. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10055. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10056. (setq hour (string-to-number (match-string 1 ans))
  10057. minute (if (match-end 3)
  10058. (string-to-number (match-string 3 ans))
  10059. 0)
  10060. pm (equal ?p
  10061. (string-to-char (downcase (match-string 4 ans)))))
  10062. (if (and (= hour 12) (not pm))
  10063. (setq hour 0)
  10064. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10065. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10066. t t ans))))
  10067. ;; Check if a time range is given as a duration
  10068. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10069. (setq hour (string-to-number (match-string 1 ans))
  10070. h2 (+ hour (string-to-number (match-string 3 ans)))
  10071. minute (string-to-number (match-string 2 ans))
  10072. m2 (+ minute (if (match-end 5) (string-to-number
  10073. (match-string 5 ans))0)))
  10074. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10075. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10076. t t ans)))
  10077. ;; Check if there is a time range
  10078. (when (boundp 'org-end-time-was-given)
  10079. (setq org-time-was-given nil)
  10080. (when (and (string-match org-plain-time-of-day-regexp ans)
  10081. (match-end 8))
  10082. (setq org-end-time-was-given (match-string 8 ans))
  10083. (setq ans (concat (substring ans 0 (match-beginning 7))
  10084. (substring ans (match-end 7))))))
  10085. (setq tl (parse-time-string ans)
  10086. day (or (nth 3 tl) (nth 3 defdecode))
  10087. month (or (nth 4 tl)
  10088. (if (and org-read-date-prefer-future
  10089. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10090. (1+ (nth 4 defdecode))
  10091. (nth 4 defdecode)))
  10092. year (or (nth 5 tl)
  10093. (if (and org-read-date-prefer-future
  10094. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10095. (1+ (nth 5 defdecode))
  10096. (nth 5 defdecode)))
  10097. hour (or (nth 2 tl) (nth 2 defdecode))
  10098. minute (or (nth 1 tl) (nth 1 defdecode))
  10099. second (or (nth 0 tl) 0)
  10100. wday (nth 6 tl))
  10101. ;; Special date definitions below
  10102. (cond
  10103. (iso-week
  10104. ;; There was an iso week
  10105. (setq year (or iso-year year)
  10106. day (or iso-weekday wday 1)
  10107. wday nil ; to make sure that the trigger below does not match
  10108. iso-date (calendar-gregorian-from-absolute
  10109. (calendar-absolute-from-iso
  10110. (list iso-week day year))))
  10111. ; FIXME: Should we also push ISO weeks into the future?
  10112. ; (when (and org-read-date-prefer-future
  10113. ; (not iso-year)
  10114. ; (< (calendar-absolute-from-gregorian iso-date)
  10115. ; (time-to-days (current-time))))
  10116. ; (setq year (1+ year)
  10117. ; iso-date (calendar-gregorian-from-absolute
  10118. ; (calendar-absolute-from-iso
  10119. ; (list iso-week day year)))))
  10120. (setq month (car iso-date)
  10121. year (nth 2 iso-date)
  10122. day (nth 1 iso-date)))
  10123. (deltan
  10124. (unless deltadef
  10125. (let ((now (decode-time (current-time))))
  10126. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10127. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10128. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10129. ((equal deltaw "m") (setq month (+ month deltan)))
  10130. ((equal deltaw "y") (setq year (+ year deltan)))))
  10131. ((and wday (not (nth 3 tl)))
  10132. ;; Weekday was given, but no day, so pick that day in the week
  10133. ;; on or after the derived date.
  10134. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10135. (unless (equal wday wday1)
  10136. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10137. (if (and (boundp 'org-time-was-given)
  10138. (nth 2 tl))
  10139. (setq org-time-was-given t))
  10140. (if (< year 100) (setq year (+ 2000 year)))
  10141. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10142. (list second minute hour day month year)))
  10143. (defvar parse-time-weekdays)
  10144. (defun org-read-date-get-relative (s today default)
  10145. "Check string S for special relative date string.
  10146. TODAY and DEFAULT are internal times, for today and for a default.
  10147. Return shift list (N what def-flag)
  10148. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10149. N is the number of WHATs to shift.
  10150. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10151. the DEFAULT date rather than TODAY."
  10152. (when (and
  10153. (string-match
  10154. (concat
  10155. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10156. "\\([0-9]+\\)?"
  10157. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10158. "\\([ \t]\\|$\\)") s)
  10159. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10160. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10161. (string-to-char (substring (match-string 1 s) -1))
  10162. ?+))
  10163. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10164. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10165. (what (if (match-end 3) (match-string 3 s) "d"))
  10166. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10167. (date (if rel default today))
  10168. (wday (nth 6 (decode-time date)))
  10169. delta)
  10170. (if wday1
  10171. (progn
  10172. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10173. (if (= dir ?-) (setq delta (- delta 7)))
  10174. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10175. (list delta "d" rel))
  10176. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10177. (defun org-eval-in-calendar (form &optional keepdate)
  10178. "Eval FORM in the calendar window and return to current window.
  10179. Also, store the cursor date in variable org-ans2."
  10180. (let ((sw (selected-window)))
  10181. (select-window (get-buffer-window "*Calendar*"))
  10182. (eval form)
  10183. (when (and (not keepdate) (calendar-cursor-to-date))
  10184. (let* ((date (calendar-cursor-to-date))
  10185. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10186. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10187. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10188. (select-window sw)))
  10189. (defun org-calendar-select ()
  10190. "Return to `org-read-date' with the date currently selected.
  10191. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10192. (interactive)
  10193. (when (calendar-cursor-to-date)
  10194. (let* ((date (calendar-cursor-to-date))
  10195. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10196. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10197. (if (active-minibuffer-window) (exit-minibuffer))))
  10198. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10199. "Insert a date stamp for the date given by the internal TIME.
  10200. WITH-HM means, use the stamp format that includes the time of the day.
  10201. INACTIVE means use square brackets instead of angular ones, so that the
  10202. stamp will not contribute to the agenda.
  10203. PRE and POST are optional strings to be inserted before and after the
  10204. stamp.
  10205. The command returns the inserted time stamp."
  10206. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10207. stamp)
  10208. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10209. (insert-before-markers (or pre ""))
  10210. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10211. (when (listp extra)
  10212. (setq extra (car extra))
  10213. (if (and (stringp extra)
  10214. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10215. (setq extra (format "-%02d:%02d"
  10216. (string-to-number (match-string 1 extra))
  10217. (string-to-number (match-string 2 extra))))
  10218. (setq extra nil)))
  10219. (when extra
  10220. (backward-char 1)
  10221. (insert-before-markers extra)
  10222. (forward-char 1))
  10223. (insert-before-markers (or post ""))
  10224. (setq org-last-inserted-timestamp stamp)))
  10225. (defun org-toggle-time-stamp-overlays ()
  10226. "Toggle the use of custom time stamp formats."
  10227. (interactive)
  10228. (setq org-display-custom-times (not org-display-custom-times))
  10229. (unless org-display-custom-times
  10230. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10231. (while (setq p (next-single-property-change p 'display))
  10232. (if (and (get-text-property p 'display)
  10233. (eq (get-text-property p 'face) 'org-date))
  10234. (remove-text-properties
  10235. p (setq p (next-single-property-change p 'display))
  10236. '(display t))))
  10237. (set-buffer-modified-p bmp)))
  10238. (if (featurep 'xemacs)
  10239. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10240. (org-restart-font-lock)
  10241. (setq org-table-may-need-update t)
  10242. (if org-display-custom-times
  10243. (message "Time stamps are overlayed with custom format")
  10244. (message "Time stamp overlays removed")))
  10245. (defun org-display-custom-time (beg end)
  10246. "Overlay modified time stamp format over timestamp between BEG and END."
  10247. (let* ((ts (buffer-substring beg end))
  10248. t1 w1 with-hm tf time str w2 (off 0))
  10249. (save-match-data
  10250. (setq t1 (org-parse-time-string ts t))
  10251. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10252. (setq off (- (match-end 0) (match-beginning 0)))))
  10253. (setq end (- end off))
  10254. (setq w1 (- end beg)
  10255. with-hm (and (nth 1 t1) (nth 2 t1))
  10256. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10257. time (org-fix-decoded-time t1)
  10258. str (org-add-props
  10259. (format-time-string
  10260. (substring tf 1 -1) (apply 'encode-time time))
  10261. nil 'mouse-face 'highlight)
  10262. w2 (length str))
  10263. (if (not (= w2 w1))
  10264. (add-text-properties (1+ beg) (+ 2 beg)
  10265. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10266. (if (featurep 'xemacs)
  10267. (progn
  10268. (put-text-property beg end 'invisible t)
  10269. (put-text-property beg end 'end-glyph (make-glyph str)))
  10270. (put-text-property beg end 'display str))))
  10271. (defun org-translate-time (string)
  10272. "Translate all timestamps in STRING to custom format.
  10273. But do this only if the variable `org-display-custom-times' is set."
  10274. (when org-display-custom-times
  10275. (save-match-data
  10276. (let* ((start 0)
  10277. (re org-ts-regexp-both)
  10278. t1 with-hm inactive tf time str beg end)
  10279. (while (setq start (string-match re string start))
  10280. (setq beg (match-beginning 0)
  10281. end (match-end 0)
  10282. t1 (save-match-data
  10283. (org-parse-time-string (substring string beg end) t))
  10284. with-hm (and (nth 1 t1) (nth 2 t1))
  10285. inactive (equal (substring string beg (1+ beg)) "[")
  10286. tf (funcall (if with-hm 'cdr 'car)
  10287. org-time-stamp-custom-formats)
  10288. time (org-fix-decoded-time t1)
  10289. str (format-time-string
  10290. (concat
  10291. (if inactive "[" "<") (substring tf 1 -1)
  10292. (if inactive "]" ">"))
  10293. (apply 'encode-time time))
  10294. string (replace-match str t t string)
  10295. start (+ start (length str)))))))
  10296. string)
  10297. (defun org-fix-decoded-time (time)
  10298. "Set 0 instead of nil for the first 6 elements of time.
  10299. Don't touch the rest."
  10300. (let ((n 0))
  10301. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10302. (defun org-days-to-time (timestamp-string)
  10303. "Difference between TIMESTAMP-STRING and now in days."
  10304. (- (time-to-days (org-time-string-to-time timestamp-string))
  10305. (time-to-days (current-time))))
  10306. (defun org-deadline-close (timestamp-string &optional ndays)
  10307. "Is the time in TIMESTAMP-STRING close to the current date?"
  10308. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10309. (and (< (org-days-to-time timestamp-string) ndays)
  10310. (not (org-entry-is-done-p))))
  10311. (defun org-get-wdays (ts)
  10312. "Get the deadline lead time appropriate for timestring TS."
  10313. (cond
  10314. ((<= org-deadline-warning-days 0)
  10315. ;; 0 or negative, enforce this value no matter what
  10316. (- org-deadline-warning-days))
  10317. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10318. ;; lead time is specified.
  10319. (floor (* (string-to-number (match-string 1 ts))
  10320. (cdr (assoc (match-string 2 ts)
  10321. '(("d" . 1) ("w" . 7)
  10322. ("m" . 30.4) ("y" . 365.25)))))))
  10323. ;; go for the default.
  10324. (t org-deadline-warning-days)))
  10325. (defun org-calendar-select-mouse (ev)
  10326. "Return to `org-read-date' with the date currently selected.
  10327. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10328. (interactive "e")
  10329. (mouse-set-point ev)
  10330. (when (calendar-cursor-to-date)
  10331. (let* ((date (calendar-cursor-to-date))
  10332. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10333. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10334. (if (active-minibuffer-window) (exit-minibuffer))))
  10335. (defun org-check-deadlines (ndays)
  10336. "Check if there are any deadlines due or past due.
  10337. A deadline is considered due if it happens within `org-deadline-warning-days'
  10338. days from today's date. If the deadline appears in an entry marked DONE,
  10339. it is not shown. The prefix arg NDAYS can be used to test that many
  10340. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10341. (interactive "P")
  10342. (let* ((org-warn-days
  10343. (cond
  10344. ((equal ndays '(4)) 100000)
  10345. (ndays (prefix-numeric-value ndays))
  10346. (t (abs org-deadline-warning-days))))
  10347. (case-fold-search nil)
  10348. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10349. (callback
  10350. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10351. (message "%d deadlines past-due or due within %d days"
  10352. (org-occur regexp nil callback)
  10353. org-warn-days)))
  10354. (defun org-check-before-date (date)
  10355. "Check if there are deadlines or scheduled entries before DATE."
  10356. (interactive (list (org-read-date)))
  10357. (let ((case-fold-search nil)
  10358. (regexp (concat "\\<\\(" org-deadline-string
  10359. "\\|" org-scheduled-string
  10360. "\\) *<\\([^>]+\\)>"))
  10361. (callback
  10362. (lambda () (time-less-p
  10363. (org-time-string-to-time (match-string 2))
  10364. (org-time-string-to-time date)))))
  10365. (message "%d entries before %s"
  10366. (org-occur regexp nil callback) date)))
  10367. (defun org-evaluate-time-range (&optional to-buffer)
  10368. "Evaluate a time range by computing the difference between start and end.
  10369. Normally the result is just printed in the echo area, but with prefix arg
  10370. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10371. If the time range is actually in a table, the result is inserted into the
  10372. next column.
  10373. For time difference computation, a year is assumed to be exactly 365
  10374. days in order to avoid rounding problems."
  10375. (interactive "P")
  10376. (or
  10377. (org-clock-update-time-maybe)
  10378. (save-excursion
  10379. (unless (org-at-date-range-p t)
  10380. (goto-char (point-at-bol))
  10381. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10382. (if (not (org-at-date-range-p t))
  10383. (error "Not at a time-stamp range, and none found in current line")))
  10384. (let* ((ts1 (match-string 1))
  10385. (ts2 (match-string 2))
  10386. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10387. (match-end (match-end 0))
  10388. (time1 (org-time-string-to-time ts1))
  10389. (time2 (org-time-string-to-time ts2))
  10390. (t1 (time-to-seconds time1))
  10391. (t2 (time-to-seconds time2))
  10392. (diff (abs (- t2 t1)))
  10393. (negative (< (- t2 t1) 0))
  10394. ;; (ys (floor (* 365 24 60 60)))
  10395. (ds (* 24 60 60))
  10396. (hs (* 60 60))
  10397. (fy "%dy %dd %02d:%02d")
  10398. (fy1 "%dy %dd")
  10399. (fd "%dd %02d:%02d")
  10400. (fd1 "%dd")
  10401. (fh "%02d:%02d")
  10402. y d h m align)
  10403. (if havetime
  10404. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10405. y 0
  10406. d (floor (/ diff ds)) diff (mod diff ds)
  10407. h (floor (/ diff hs)) diff (mod diff hs)
  10408. m (floor (/ diff 60)))
  10409. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10410. y 0
  10411. d (floor (+ (/ diff ds) 0.5))
  10412. h 0 m 0))
  10413. (if (not to-buffer)
  10414. (message "%s" (org-make-tdiff-string y d h m))
  10415. (if (org-at-table-p)
  10416. (progn
  10417. (goto-char match-end)
  10418. (setq align t)
  10419. (and (looking-at " *|") (goto-char (match-end 0))))
  10420. (goto-char match-end))
  10421. (if (looking-at
  10422. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10423. (replace-match ""))
  10424. (if negative (insert " -"))
  10425. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10426. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10427. (insert " " (format fh h m))))
  10428. (if align (org-table-align))
  10429. (message "Time difference inserted")))))
  10430. (defun org-make-tdiff-string (y d h m)
  10431. (let ((fmt "")
  10432. (l nil))
  10433. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10434. l (push y l)))
  10435. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10436. l (push d l)))
  10437. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10438. l (push h l)))
  10439. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10440. l (push m l)))
  10441. (apply 'format fmt (nreverse l))))
  10442. (defun org-time-string-to-time (s)
  10443. (apply 'encode-time (org-parse-time-string s)))
  10444. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10445. "Convert a time stamp to an absolute day number.
  10446. If there is a specifyer for a cyclic time stamp, get the closest date to
  10447. DAYNR.
  10448. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10449. (cond
  10450. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10451. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10452. daynr
  10453. (+ daynr 1000)))
  10454. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10455. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10456. (time-to-days (current-time))) (match-string 0 s)
  10457. prefer show-all))
  10458. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10459. (defun org-days-to-iso-week (days)
  10460. "Return the iso week number."
  10461. (require 'cal-iso)
  10462. (car (calendar-iso-from-absolute days)))
  10463. (defun org-small-year-to-year (year)
  10464. "Convert 2-digit years into 4-digit years.
  10465. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10466. The year 2000 cannot be abbreviated. Any year larger than 99
  10467. is returned unchanged."
  10468. (if (< year 38)
  10469. (setq year (+ 2000 year))
  10470. (if (< year 100)
  10471. (setq year (+ 1900 year))))
  10472. year)
  10473. (defun org-time-from-absolute (d)
  10474. "Return the time corresponding to date D.
  10475. D may be an absolute day number, or a calendar-type list (month day year)."
  10476. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10477. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10478. (defun org-calendar-holiday ()
  10479. "List of holidays, for Diary display in Org-mode."
  10480. (require 'holidays)
  10481. (let ((hl (funcall
  10482. (if (fboundp 'calendar-check-holidays)
  10483. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10484. (if hl (mapconcat 'identity hl "; "))))
  10485. (defun org-diary-sexp-entry (sexp entry date)
  10486. "Process a SEXP diary ENTRY for DATE."
  10487. (require 'diary-lib)
  10488. (let ((result (if calendar-debug-sexp
  10489. (let ((stack-trace-on-error t))
  10490. (eval (car (read-from-string sexp))))
  10491. (condition-case nil
  10492. (eval (car (read-from-string sexp)))
  10493. (error
  10494. (beep)
  10495. (message "Bad sexp at line %d in %s: %s"
  10496. (org-current-line)
  10497. (buffer-file-name) sexp)
  10498. (sleep-for 2))))))
  10499. (cond ((stringp result) result)
  10500. ((and (consp result)
  10501. (stringp (cdr result))) (cdr result))
  10502. (result entry)
  10503. (t nil))))
  10504. (defun org-diary-to-ical-string (frombuf)
  10505. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10506. This uses the icalendar.el library."
  10507. (let* ((tmpdir (if (featurep 'xemacs)
  10508. (temp-directory)
  10509. temporary-file-directory))
  10510. (tmpfile (make-temp-name
  10511. (expand-file-name "orgics" tmpdir)))
  10512. buf rtn b e)
  10513. (save-excursion
  10514. (set-buffer frombuf)
  10515. (icalendar-export-region (point-min) (point-max) tmpfile)
  10516. (setq buf (find-buffer-visiting tmpfile))
  10517. (set-buffer buf)
  10518. (goto-char (point-min))
  10519. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10520. (setq b (match-beginning 0)))
  10521. (goto-char (point-max))
  10522. (if (re-search-backward "^END:VEVENT" nil t)
  10523. (setq e (match-end 0)))
  10524. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10525. (kill-buffer buf)
  10526. (delete-file tmpfile)
  10527. rtn))
  10528. (defun org-closest-date (start current change prefer show-all)
  10529. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10530. When PREFER is `past' return a date that is either CURRENT or past.
  10531. When PREFER is `future', return a date that is either CURRENT or future.
  10532. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10533. ;; Make the proper lists from the dates
  10534. (catch 'exit
  10535. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10536. dn dw sday cday n1 n2
  10537. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10538. (setq start (org-date-to-gregorian start)
  10539. current (org-date-to-gregorian
  10540. (if show-all
  10541. current
  10542. (time-to-days (current-time))))
  10543. sday (calendar-absolute-from-gregorian start)
  10544. cday (calendar-absolute-from-gregorian current))
  10545. (if (<= cday sday) (throw 'exit sday))
  10546. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10547. (setq dn (string-to-number (match-string 1 change))
  10548. dw (cdr (assoc (match-string 2 change) a1)))
  10549. (error "Invalid change specifyer: %s" change))
  10550. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10551. (cond
  10552. ((eq dw 'day)
  10553. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10554. n2 (+ n1 dn)))
  10555. ((eq dw 'year)
  10556. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10557. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10558. (setq date1 (list m d y1)
  10559. n1 (calendar-absolute-from-gregorian date1)
  10560. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10561. n2 (calendar-absolute-from-gregorian date2)))
  10562. ((eq dw 'month)
  10563. ;; approx number of month between the two dates
  10564. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10565. ;; How often does dn fit in there?
  10566. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10567. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10568. m (+ m nm)
  10569. ny (floor (/ m 12))
  10570. y (+ y ny)
  10571. m (- m (* ny 12)))
  10572. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10573. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10574. (setq m2 (+ m dn) y2 y)
  10575. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10576. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10577. (while (<= n2 cday)
  10578. (setq n1 n2 m m2 y y2)
  10579. (setq m2 (+ m dn) y2 y)
  10580. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10581. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10582. (if show-all
  10583. (cond
  10584. ((eq prefer 'past) n1)
  10585. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10586. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10587. (cond
  10588. ((eq prefer 'past) n1)
  10589. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10590. (t (if (= cday n1) n1 n2)))))))
  10591. (defun org-date-to-gregorian (date)
  10592. "Turn any specification of DATE into a gregorian date for the calendar."
  10593. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10594. ((and (listp date) (= (length date) 3)) date)
  10595. ((stringp date)
  10596. (setq date (org-parse-time-string date))
  10597. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10598. ((listp date)
  10599. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10600. (defun org-parse-time-string (s &optional nodefault)
  10601. "Parse the standard Org-mode time string.
  10602. This should be a lot faster than the normal `parse-time-string'.
  10603. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10604. hour and minute fields will be nil if not given."
  10605. (if (string-match org-ts-regexp0 s)
  10606. (list 0
  10607. (if (or (match-beginning 8) (not nodefault))
  10608. (string-to-number (or (match-string 8 s) "0")))
  10609. (if (or (match-beginning 7) (not nodefault))
  10610. (string-to-number (or (match-string 7 s) "0")))
  10611. (string-to-number (match-string 4 s))
  10612. (string-to-number (match-string 3 s))
  10613. (string-to-number (match-string 2 s))
  10614. nil nil nil)
  10615. (make-list 9 0)))
  10616. (defun org-timestamp-up (&optional arg)
  10617. "Increase the date item at the cursor by one.
  10618. If the cursor is on the year, change the year. If it is on the month or
  10619. the day, change that.
  10620. With prefix ARG, change by that many units."
  10621. (interactive "p")
  10622. (org-timestamp-change (prefix-numeric-value arg)))
  10623. (defun org-timestamp-down (&optional arg)
  10624. "Decrease the date item at the cursor by one.
  10625. If the cursor is on the year, change the year. If it is on the month or
  10626. the day, change that.
  10627. With prefix ARG, change by that many units."
  10628. (interactive "p")
  10629. (org-timestamp-change (- (prefix-numeric-value arg))))
  10630. (defun org-timestamp-up-day (&optional arg)
  10631. "Increase the date in the time stamp by one day.
  10632. With prefix ARG, change that many days."
  10633. (interactive "p")
  10634. (if (and (not (org-at-timestamp-p t))
  10635. (org-on-heading-p))
  10636. (org-todo 'up)
  10637. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10638. (defun org-timestamp-down-day (&optional arg)
  10639. "Decrease the date in the time stamp by one day.
  10640. With prefix ARG, change that many days."
  10641. (interactive "p")
  10642. (if (and (not (org-at-timestamp-p t))
  10643. (org-on-heading-p))
  10644. (org-todo 'down)
  10645. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10646. (defun org-at-timestamp-p (&optional inactive-ok)
  10647. "Determine if the cursor is in or at a timestamp."
  10648. (interactive)
  10649. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10650. (pos (point))
  10651. (ans (or (looking-at tsr)
  10652. (save-excursion
  10653. (skip-chars-backward "^[<\n\r\t")
  10654. (if (> (point) (point-min)) (backward-char 1))
  10655. (and (looking-at tsr)
  10656. (> (- (match-end 0) pos) -1))))))
  10657. (and ans
  10658. (boundp 'org-ts-what)
  10659. (setq org-ts-what
  10660. (cond
  10661. ((= pos (match-beginning 0)) 'bracket)
  10662. ((= pos (1- (match-end 0))) 'bracket)
  10663. ((org-pos-in-match-range pos 2) 'year)
  10664. ((org-pos-in-match-range pos 3) 'month)
  10665. ((org-pos-in-match-range pos 7) 'hour)
  10666. ((org-pos-in-match-range pos 8) 'minute)
  10667. ((or (org-pos-in-match-range pos 4)
  10668. (org-pos-in-match-range pos 5)) 'day)
  10669. ((and (> pos (or (match-end 8) (match-end 5)))
  10670. (< pos (match-end 0)))
  10671. (- pos (or (match-end 8) (match-end 5))))
  10672. (t 'day))))
  10673. ans))
  10674. (defun org-toggle-timestamp-type ()
  10675. "Toggle the type (<active> or [inactive]) of a time stamp."
  10676. (interactive)
  10677. (when (org-at-timestamp-p t)
  10678. (let ((beg (match-beginning 0)) (end (match-end 0))
  10679. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10680. (save-excursion
  10681. (goto-char beg)
  10682. (while (re-search-forward "[][<>]" end t)
  10683. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10684. t t)))
  10685. (message "Timestamp is now %sactive"
  10686. (if (equal (char-after beg) ?<) "" "in")))))
  10687. (defun org-timestamp-change (n &optional what)
  10688. "Change the date in the time stamp at point.
  10689. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10690. `year', `minute', `second'. If WHAT is not given, the cursor position
  10691. in the timestamp determines what will be changed."
  10692. (let ((pos (point))
  10693. with-hm inactive
  10694. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10695. org-ts-what
  10696. extra rem
  10697. ts time time0)
  10698. (if (not (org-at-timestamp-p t))
  10699. (error "Not at a timestamp"))
  10700. (if (and (not what) (eq org-ts-what 'bracket))
  10701. (org-toggle-timestamp-type)
  10702. (if (and (not what) (not (eq org-ts-what 'day))
  10703. org-display-custom-times
  10704. (get-text-property (point) 'display)
  10705. (not (get-text-property (1- (point)) 'display)))
  10706. (setq org-ts-what 'day))
  10707. (setq org-ts-what (or what org-ts-what)
  10708. inactive (= (char-after (match-beginning 0)) ?\[)
  10709. ts (match-string 0))
  10710. (replace-match "")
  10711. (if (string-match
  10712. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10713. ts)
  10714. (setq extra (match-string 1 ts)))
  10715. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10716. (setq with-hm t))
  10717. (setq time0 (org-parse-time-string ts))
  10718. (when (and (eq org-ts-what 'minute)
  10719. (eq current-prefix-arg nil))
  10720. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10721. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10722. (setcar (cdr time0) (+ (nth 1 time0)
  10723. (if (> n 0) (- rem) (- dm rem))))))
  10724. (setq time
  10725. (encode-time (or (car time0) 0)
  10726. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10727. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10728. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10729. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10730. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10731. (nthcdr 6 time0)))
  10732. (when (integerp org-ts-what)
  10733. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10734. (if (eq what 'calendar)
  10735. (let ((cal-date (org-get-date-from-calendar)))
  10736. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10737. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10738. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10739. (setcar time0 (or (car time0) 0))
  10740. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10741. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10742. (setq time (apply 'encode-time time0))))
  10743. (setq org-last-changed-timestamp
  10744. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10745. (org-clock-update-time-maybe)
  10746. (goto-char pos)
  10747. ;; Try to recenter the calendar window, if any
  10748. (if (and org-calendar-follow-timestamp-change
  10749. (get-buffer-window "*Calendar*" t)
  10750. (memq org-ts-what '(day month year)))
  10751. (org-recenter-calendar (time-to-days time))))))
  10752. (defun org-modify-ts-extra (s pos n dm)
  10753. "Change the different parts of the lead-time and repeat fields in timestamp."
  10754. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10755. ng h m new rem)
  10756. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10757. (cond
  10758. ((or (org-pos-in-match-range pos 2)
  10759. (org-pos-in-match-range pos 3))
  10760. (setq m (string-to-number (match-string 3 s))
  10761. h (string-to-number (match-string 2 s)))
  10762. (if (org-pos-in-match-range pos 2)
  10763. (setq h (+ h n))
  10764. (setq n (* dm (org-no-warnings (signum n))))
  10765. (when (not (= 0 (setq rem (% m dm))))
  10766. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10767. (setq m (+ m n)))
  10768. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10769. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10770. (setq h (min 24 (max 0 h)))
  10771. (setq ng 1 new (format "-%02d:%02d" h m)))
  10772. ((org-pos-in-match-range pos 6)
  10773. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10774. ((org-pos-in-match-range pos 5)
  10775. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10776. ((org-pos-in-match-range pos 9)
  10777. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10778. ((org-pos-in-match-range pos 8)
  10779. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10780. (when ng
  10781. (setq s (concat
  10782. (substring s 0 (match-beginning ng))
  10783. new
  10784. (substring s (match-end ng))))))
  10785. s))
  10786. (defun org-recenter-calendar (date)
  10787. "If the calendar is visible, recenter it to DATE."
  10788. (let* ((win (selected-window))
  10789. (cwin (get-buffer-window "*Calendar*" t))
  10790. (calendar-move-hook nil))
  10791. (when cwin
  10792. (select-window cwin)
  10793. (calendar-goto-date (if (listp date) date
  10794. (calendar-gregorian-from-absolute date)))
  10795. (select-window win))))
  10796. (defun org-goto-calendar (&optional arg)
  10797. "Go to the Emacs calendar at the current date.
  10798. If there is a time stamp in the current line, go to that date.
  10799. A prefix ARG can be used to force the current date."
  10800. (interactive "P")
  10801. (let ((tsr org-ts-regexp) diff
  10802. (calendar-move-hook nil)
  10803. (calendar-view-holidays-initially-flag nil)
  10804. (view-calendar-holidays-initially nil)
  10805. (calendar-view-diary-initially-flag nil)
  10806. (view-diary-entries-initially nil))
  10807. (if (or (org-at-timestamp-p)
  10808. (save-excursion
  10809. (beginning-of-line 1)
  10810. (looking-at (concat ".*" tsr))))
  10811. (let ((d1 (time-to-days (current-time)))
  10812. (d2 (time-to-days
  10813. (org-time-string-to-time (match-string 1)))))
  10814. (setq diff (- d2 d1))))
  10815. (calendar)
  10816. (calendar-goto-today)
  10817. (if (and diff (not arg)) (calendar-forward-day diff))))
  10818. (defun org-get-date-from-calendar ()
  10819. "Return a list (month day year) of date at point in calendar."
  10820. (with-current-buffer "*Calendar*"
  10821. (save-match-data
  10822. (calendar-cursor-to-date))))
  10823. (defun org-date-from-calendar ()
  10824. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10825. If there is already a time stamp at the cursor position, update it."
  10826. (interactive)
  10827. (if (org-at-timestamp-p t)
  10828. (org-timestamp-change 0 'calendar)
  10829. (let ((cal-date (org-get-date-from-calendar)))
  10830. (org-insert-time-stamp
  10831. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10832. (defun org-minutes-to-hh:mm-string (m)
  10833. "Compute H:MM from a number of minutes."
  10834. (let ((h (/ m 60)))
  10835. (setq m (- m (* 60 h)))
  10836. (format org-time-clocksum-format h m)))
  10837. (defun org-hh:mm-string-to-minutes (s)
  10838. "Convert a string H:MM to a number of minutes."
  10839. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10840. (+ (* (string-to-number (match-string 1 s)) 60)
  10841. (string-to-number (match-string 2 s)))
  10842. 0))
  10843. ;;;; Agenda files
  10844. ;;;###autoload
  10845. (defun org-iswitchb (&optional arg)
  10846. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10847. With a prefix argument, restrict available to files.
  10848. With two prefix arguments, restrict available buffers to agenda files.
  10849. Due to some yet unresolved reason, the global function
  10850. `iswitchb-mode' needs to be active for this function to work."
  10851. (interactive "P")
  10852. (require 'iswitchb)
  10853. (let ((enabled iswitchb-mode) blist)
  10854. (or enabled (iswitchb-mode 1))
  10855. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10856. ((equal arg '(16)) (org-buffer-list 'agenda))
  10857. (t (org-buffer-list))))
  10858. (unwind-protect
  10859. (let ((iswitchb-make-buflist-hook
  10860. (lambda ()
  10861. (setq iswitchb-temp-buflist
  10862. (mapcar 'buffer-name blist)))))
  10863. (switch-to-buffer
  10864. (iswitchb-read-buffer
  10865. "Switch-to: " nil t))
  10866. (or enabled (iswitchb-mode -1))))))
  10867. ;;;###autoload
  10868. (defun org-ido-switchb (&optional arg)
  10869. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  10870. With a prefix argument, restrict available to files.
  10871. With two prefix arguments, restrict available buffers to agenda files."
  10872. (interactive "P")
  10873. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10874. ((equal arg '(16)) (org-buffer-list 'agenda))
  10875. (t (org-buffer-list)))))
  10876. (switch-to-buffer
  10877. (org-ido-completing-read "Org buffer: "
  10878. (mapcar 'buffer-name blist)
  10879. nil t))))
  10880. (defun org-buffer-list (&optional predicate exclude-tmp)
  10881. "Return a list of Org buffers.
  10882. PREDICATE can be `export', `files' or `agenda'.
  10883. export restrict the list to Export buffers.
  10884. files restrict the list to buffers visiting Org files.
  10885. agenda restrict the list to buffers visiting agenda files.
  10886. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10887. (let* ((bfn nil)
  10888. (agenda-files (and (eq predicate 'agenda)
  10889. (mapcar 'file-truename (org-agenda-files t))))
  10890. (filter
  10891. (cond
  10892. ((eq predicate 'files)
  10893. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10894. ((eq predicate 'export)
  10895. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10896. ((eq predicate 'agenda)
  10897. (lambda (b)
  10898. (with-current-buffer b
  10899. (and (eq major-mode 'org-mode)
  10900. (setq bfn (buffer-file-name b))
  10901. (member (file-truename bfn) agenda-files)))))
  10902. (t (lambda (b) (with-current-buffer b
  10903. (or (eq major-mode 'org-mode)
  10904. (string-match "\*Org .*Export"
  10905. (buffer-name b)))))))))
  10906. (delq nil
  10907. (mapcar
  10908. (lambda(b)
  10909. (if (and (funcall filter b)
  10910. (or (not exclude-tmp)
  10911. (not (string-match "tmp" (buffer-name b)))))
  10912. b
  10913. nil))
  10914. (buffer-list)))))
  10915. (defun org-agenda-files (&optional unrestricted archives)
  10916. "Get the list of agenda files.
  10917. Optional UNRESTRICTED means return the full list even if a restriction
  10918. is currently in place.
  10919. When ARCHIVES is t, include all archive files hat are really being
  10920. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10921. `org-agenda-archives-mode' is t."
  10922. (let ((files
  10923. (cond
  10924. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10925. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10926. ((listp org-agenda-files) org-agenda-files)
  10927. (t (error "Invalid value of `org-agenda-files'")))))
  10928. (setq files (apply 'append
  10929. (mapcar (lambda (f)
  10930. (if (file-directory-p f)
  10931. (directory-files
  10932. f t org-agenda-file-regexp)
  10933. (list f)))
  10934. files)))
  10935. (when org-agenda-skip-unavailable-files
  10936. (setq files (delq nil
  10937. (mapcar (function
  10938. (lambda (file)
  10939. (and (file-readable-p file) file)))
  10940. files))))
  10941. (when (or (eq archives t)
  10942. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10943. (setq files (org-add-archive-files files)))
  10944. files))
  10945. (defun org-edit-agenda-file-list ()
  10946. "Edit the list of agenda files.
  10947. Depending on setup, this either uses customize to edit the variable
  10948. `org-agenda-files', or it visits the file that is holding the list. In the
  10949. latter case, the buffer is set up in a way that saving it automatically kills
  10950. the buffer and restores the previous window configuration."
  10951. (interactive)
  10952. (if (stringp org-agenda-files)
  10953. (let ((cw (current-window-configuration)))
  10954. (find-file org-agenda-files)
  10955. (org-set-local 'org-window-configuration cw)
  10956. (org-add-hook 'after-save-hook
  10957. (lambda ()
  10958. (set-window-configuration
  10959. (prog1 org-window-configuration
  10960. (kill-buffer (current-buffer))))
  10961. (org-install-agenda-files-menu)
  10962. (message "New agenda file list installed"))
  10963. nil 'local)
  10964. (message "%s" (substitute-command-keys
  10965. "Edit list and finish with \\[save-buffer]")))
  10966. (customize-variable 'org-agenda-files)))
  10967. (defun org-store-new-agenda-file-list (list)
  10968. "Set new value for the agenda file list and save it correctly."
  10969. (if (stringp org-agenda-files)
  10970. (let ((f org-agenda-files) b)
  10971. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10972. (with-temp-file f
  10973. (insert (mapconcat 'identity list "\n") "\n")))
  10974. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10975. (setq org-agenda-files list)
  10976. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10977. (defun org-read-agenda-file-list ()
  10978. "Read the list of agenda files from a file."
  10979. (when (file-directory-p org-agenda-files)
  10980. (error "`org-agenda-files' cannot be a single directory"))
  10981. (when (stringp org-agenda-files)
  10982. (with-temp-buffer
  10983. (insert-file-contents org-agenda-files)
  10984. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10985. ;;;###autoload
  10986. (defun org-cycle-agenda-files ()
  10987. "Cycle through the files in `org-agenda-files'.
  10988. If the current buffer visits an agenda file, find the next one in the list.
  10989. If the current buffer does not, find the first agenda file."
  10990. (interactive)
  10991. (let* ((fs (org-agenda-files t))
  10992. (files (append fs (list (car fs))))
  10993. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10994. file)
  10995. (unless files (error "No agenda files"))
  10996. (catch 'exit
  10997. (while (setq file (pop files))
  10998. (if (equal (file-truename file) tcf)
  10999. (when (car files)
  11000. (find-file (car files))
  11001. (throw 'exit t))))
  11002. (find-file (car fs)))
  11003. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11004. (defun org-agenda-file-to-front (&optional to-end)
  11005. "Move/add the current file to the top of the agenda file list.
  11006. If the file is not present in the list, it is added to the front. If it is
  11007. present, it is moved there. With optional argument TO-END, add/move to the
  11008. end of the list."
  11009. (interactive "P")
  11010. (let ((org-agenda-skip-unavailable-files nil)
  11011. (file-alist (mapcar (lambda (x)
  11012. (cons (file-truename x) x))
  11013. (org-agenda-files t)))
  11014. (ctf (file-truename buffer-file-name))
  11015. x had)
  11016. (setq x (assoc ctf file-alist) had x)
  11017. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11018. (if to-end
  11019. (setq file-alist (append (delq x file-alist) (list x)))
  11020. (setq file-alist (cons x (delq x file-alist))))
  11021. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11022. (org-install-agenda-files-menu)
  11023. (message "File %s to %s of agenda file list"
  11024. (if had "moved" "added") (if to-end "end" "front"))))
  11025. (defun org-remove-file (&optional file)
  11026. "Remove current file from the list of files in variable `org-agenda-files'.
  11027. These are the files which are being checked for agenda entries.
  11028. Optional argument FILE means, use this file instead of the current."
  11029. (interactive)
  11030. (let* ((org-agenda-skip-unavailable-files nil)
  11031. (file (or file buffer-file-name))
  11032. (true-file (file-truename file))
  11033. (afile (abbreviate-file-name file))
  11034. (files (delq nil (mapcar
  11035. (lambda (x)
  11036. (if (equal true-file
  11037. (file-truename x))
  11038. nil x))
  11039. (org-agenda-files t)))))
  11040. (if (not (= (length files) (length (org-agenda-files t))))
  11041. (progn
  11042. (org-store-new-agenda-file-list files)
  11043. (org-install-agenda-files-menu)
  11044. (message "Removed file: %s" afile))
  11045. (message "File was not in list: %s (not removed)" afile))))
  11046. (defun org-file-menu-entry (file)
  11047. (vector file (list 'find-file file) t))
  11048. (defun org-check-agenda-file (file)
  11049. "Make sure FILE exists. If not, ask user what to do."
  11050. (when (not (file-exists-p file))
  11051. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11052. (abbreviate-file-name file))
  11053. (let ((r (downcase (read-char-exclusive))))
  11054. (cond
  11055. ((equal r ?r)
  11056. (org-remove-file file)
  11057. (throw 'nextfile t))
  11058. (t (error "Abort"))))))
  11059. (defun org-get-agenda-file-buffer (file)
  11060. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11061. it to the list of buffers which might be released later."
  11062. (let ((buf (org-find-base-buffer-visiting file)))
  11063. (if buf
  11064. buf ; just return it
  11065. ;; Make a new buffer and remember it
  11066. (setq buf (find-file-noselect file))
  11067. (if buf (push buf org-agenda-new-buffers))
  11068. buf)))
  11069. (defun org-release-buffers (blist)
  11070. "Release all buffers in list, asking the user for confirmation when needed.
  11071. When a buffer is unmodified, it is just killed. When modified, it is saved
  11072. \(if the user agrees) and then killed."
  11073. (let (buf file)
  11074. (while (setq buf (pop blist))
  11075. (setq file (buffer-file-name buf))
  11076. (when (and (buffer-modified-p buf)
  11077. file
  11078. (y-or-n-p (format "Save file %s? " file)))
  11079. (with-current-buffer buf (save-buffer)))
  11080. (kill-buffer buf))))
  11081. (defun org-prepare-agenda-buffers (files)
  11082. "Create buffers for all agenda files, protect archived trees and comments."
  11083. (interactive)
  11084. (let ((pa '(:org-archived t))
  11085. (pc '(:org-comment t))
  11086. (pall '(:org-archived t :org-comment t))
  11087. (inhibit-read-only t)
  11088. (rea (concat ":" org-archive-tag ":"))
  11089. bmp file re)
  11090. (save-excursion
  11091. (save-restriction
  11092. (while (setq file (pop files))
  11093. (if (bufferp file)
  11094. (set-buffer file)
  11095. (org-check-agenda-file file)
  11096. (set-buffer (org-get-agenda-file-buffer file)))
  11097. (widen)
  11098. (setq bmp (buffer-modified-p))
  11099. (org-refresh-category-properties)
  11100. (setq org-todo-keywords-for-agenda
  11101. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11102. (setq org-done-keywords-for-agenda
  11103. (append org-done-keywords-for-agenda org-done-keywords))
  11104. (setq org-todo-keyword-alist-for-agenda
  11105. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11106. (setq org-tag-alist-for-agenda
  11107. (append org-tag-alist-for-agenda org-tag-alist))
  11108. (save-excursion
  11109. (remove-text-properties (point-min) (point-max) pall)
  11110. (when org-agenda-skip-archived-trees
  11111. (goto-char (point-min))
  11112. (while (re-search-forward rea nil t)
  11113. (if (org-on-heading-p t)
  11114. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11115. (goto-char (point-min))
  11116. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11117. (while (re-search-forward re nil t)
  11118. (add-text-properties
  11119. (match-beginning 0) (org-end-of-subtree t) pc)))
  11120. (set-buffer-modified-p bmp))))
  11121. (setq org-todo-keyword-alist-for-agenda
  11122. (org-uniquify org-todo-keyword-alist-for-agenda)
  11123. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11124. ;;;; Embedded LaTeX
  11125. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11126. "Keymap for the minor `org-cdlatex-mode'.")
  11127. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11128. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11129. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11130. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11131. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11132. (defvar org-cdlatex-texmathp-advice-is-done nil
  11133. "Flag remembering if we have applied the advice to texmathp already.")
  11134. (define-minor-mode org-cdlatex-mode
  11135. "Toggle the minor `org-cdlatex-mode'.
  11136. This mode supports entering LaTeX environment and math in LaTeX fragments
  11137. in Org-mode.
  11138. \\{org-cdlatex-mode-map}"
  11139. nil " OCDL" nil
  11140. (when org-cdlatex-mode (require 'cdlatex))
  11141. (unless org-cdlatex-texmathp-advice-is-done
  11142. (setq org-cdlatex-texmathp-advice-is-done t)
  11143. (defadvice texmathp (around org-math-always-on activate)
  11144. "Always return t in org-mode buffers.
  11145. This is because we want to insert math symbols without dollars even outside
  11146. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11147. an embedded LaTeX fragment, let texmathp do its job.
  11148. \\[org-cdlatex-mode-map]"
  11149. (interactive)
  11150. (let (p)
  11151. (cond
  11152. ((not (org-mode-p)) ad-do-it)
  11153. ((eq this-command 'cdlatex-math-symbol)
  11154. (setq ad-return-value t
  11155. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11156. (t
  11157. (let ((p (org-inside-LaTeX-fragment-p)))
  11158. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11159. (setq ad-return-value t
  11160. texmathp-why '("Org-mode embedded math" . 0))
  11161. (if p ad-do-it)))))))))
  11162. (defun turn-on-org-cdlatex ()
  11163. "Unconditionally turn on `org-cdlatex-mode'."
  11164. (org-cdlatex-mode 1))
  11165. (defun org-inside-LaTeX-fragment-p ()
  11166. "Test if point is inside a LaTeX fragment.
  11167. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11168. sequence appearing also before point.
  11169. Even though the matchers for math are configurable, this function assumes
  11170. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11171. delimiters are skipped when they have been removed by customization.
  11172. The return value is nil, or a cons cell with the delimiter and
  11173. and the position of this delimiter.
  11174. This function does a reasonably good job, but can locally be fooled by
  11175. for example currency specifications. For example it will assume being in
  11176. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11177. fragments that are properly closed, but during editing, we have to live
  11178. with the uncertainty caused by missing closing delimiters. This function
  11179. looks only before point, not after."
  11180. (catch 'exit
  11181. (let ((pos (point))
  11182. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11183. (lim (progn
  11184. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11185. (point)))
  11186. dd-on str (start 0) m re)
  11187. (goto-char pos)
  11188. (when dodollar
  11189. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11190. re (nth 1 (assoc "$" org-latex-regexps)))
  11191. (while (string-match re str start)
  11192. (cond
  11193. ((= (match-end 0) (length str))
  11194. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11195. ((= (match-end 0) (- (length str) 5))
  11196. (throw 'exit nil))
  11197. (t (setq start (match-end 0))))))
  11198. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11199. (goto-char pos)
  11200. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11201. (and (match-beginning 2) (throw 'exit nil))
  11202. ;; count $$
  11203. (while (re-search-backward "\\$\\$" lim t)
  11204. (setq dd-on (not dd-on)))
  11205. (goto-char pos)
  11206. (if dd-on (cons "$$" m))))))
  11207. (defun org-try-cdlatex-tab ()
  11208. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11209. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11210. - inside a LaTeX fragment, or
  11211. - after the first word in a line, where an abbreviation expansion could
  11212. insert a LaTeX environment."
  11213. (when org-cdlatex-mode
  11214. (cond
  11215. ((save-excursion
  11216. (skip-chars-backward "a-zA-Z0-9*")
  11217. (skip-chars-backward " \t")
  11218. (bolp))
  11219. (cdlatex-tab) t)
  11220. ((org-inside-LaTeX-fragment-p)
  11221. (cdlatex-tab) t)
  11222. (t nil))))
  11223. (defun org-cdlatex-underscore-caret (&optional arg)
  11224. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11225. Revert to the normal definition outside of these fragments."
  11226. (interactive "P")
  11227. (if (org-inside-LaTeX-fragment-p)
  11228. (call-interactively 'cdlatex-sub-superscript)
  11229. (let (org-cdlatex-mode)
  11230. (call-interactively (key-binding (vector last-input-event))))))
  11231. (defun org-cdlatex-math-modify (&optional arg)
  11232. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11233. Revert to the normal definition outside of these fragments."
  11234. (interactive "P")
  11235. (if (org-inside-LaTeX-fragment-p)
  11236. (call-interactively 'cdlatex-math-modify)
  11237. (let (org-cdlatex-mode)
  11238. (call-interactively (key-binding (vector last-input-event))))))
  11239. (defvar org-latex-fragment-image-overlays nil
  11240. "List of overlays carrying the images of latex fragments.")
  11241. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11242. (defun org-remove-latex-fragment-image-overlays ()
  11243. "Remove all overlays with LaTeX fragment images in current buffer."
  11244. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11245. (setq org-latex-fragment-image-overlays nil))
  11246. (defun org-preview-latex-fragment (&optional subtree)
  11247. "Preview the LaTeX fragment at point, or all locally or globally.
  11248. If the cursor is in a LaTeX fragment, create the image and overlay
  11249. it over the source code. If there is no fragment at point, display
  11250. all fragments in the current text, from one headline to the next. With
  11251. prefix SUBTREE, display all fragments in the current subtree. With a
  11252. double prefix `C-u C-u', or when the cursor is before the first headline,
  11253. display all fragments in the buffer.
  11254. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11255. (interactive "P")
  11256. (org-remove-latex-fragment-image-overlays)
  11257. (save-excursion
  11258. (save-restriction
  11259. (let (beg end at msg)
  11260. (cond
  11261. ((or (equal subtree '(16))
  11262. (not (save-excursion
  11263. (re-search-backward (concat "^" outline-regexp) nil t))))
  11264. (setq beg (point-min) end (point-max)
  11265. msg "Creating images for buffer...%s"))
  11266. ((equal subtree '(4))
  11267. (org-back-to-heading)
  11268. (setq beg (point) end (org-end-of-subtree t)
  11269. msg "Creating images for subtree...%s"))
  11270. (t
  11271. (if (setq at (org-inside-LaTeX-fragment-p))
  11272. (goto-char (max (point-min) (- (cdr at) 2)))
  11273. (org-back-to-heading))
  11274. (setq beg (point) end (progn (outline-next-heading) (point))
  11275. msg (if at "Creating image...%s"
  11276. "Creating images for entry...%s"))))
  11277. (message msg "")
  11278. (narrow-to-region beg end)
  11279. (goto-char beg)
  11280. (org-format-latex
  11281. (concat "ltxpng/" (file-name-sans-extension
  11282. (file-name-nondirectory
  11283. buffer-file-name)))
  11284. default-directory 'overlays msg at 'forbuffer)
  11285. (message msg "done. Use `C-c C-c' to remove images.")))))
  11286. (defvar org-latex-regexps
  11287. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11288. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11289. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11290. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11291. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11292. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11293. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11294. "Regular expressions for matching embedded LaTeX.")
  11295. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11296. "Replace LaTeX fragments with links to an image, and produce images."
  11297. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11298. (let* ((prefixnodir (file-name-nondirectory prefix))
  11299. (absprefix (expand-file-name prefix dir))
  11300. (todir (file-name-directory absprefix))
  11301. (opt org-format-latex-options)
  11302. (matchers (plist-get opt :matchers))
  11303. (re-list org-latex-regexps)
  11304. (cnt 0) txt link beg end re e checkdir
  11305. m n block linkfile movefile ov)
  11306. ;; Check if there are old images files with this prefix, and remove them
  11307. (when (file-directory-p todir)
  11308. (mapc 'delete-file
  11309. (directory-files
  11310. todir 'full
  11311. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11312. ;; Check the different regular expressions
  11313. (while (setq e (pop re-list))
  11314. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11315. block (if (nth 3 e) "\n\n" ""))
  11316. (when (member m matchers)
  11317. (goto-char (point-min))
  11318. (while (re-search-forward re nil t)
  11319. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11320. (setq txt (match-string n)
  11321. beg (match-beginning n) end (match-end n)
  11322. cnt (1+ cnt)
  11323. linkfile (format "%s_%04d.png" prefix cnt)
  11324. movefile (format "%s_%04d.png" absprefix cnt)
  11325. link (concat block "[[file:" linkfile "]]" block))
  11326. (if msg (message msg cnt))
  11327. (goto-char beg)
  11328. (unless checkdir ; make sure the directory exists
  11329. (setq checkdir t)
  11330. (or (file-directory-p todir) (make-directory todir)))
  11331. (org-create-formula-image
  11332. txt movefile opt forbuffer)
  11333. (if overlays
  11334. (progn
  11335. (setq ov (org-make-overlay beg end))
  11336. (if (featurep 'xemacs)
  11337. (progn
  11338. (org-overlay-put ov 'invisible t)
  11339. (org-overlay-put
  11340. ov 'end-glyph
  11341. (make-glyph (vector 'png :file movefile))))
  11342. (org-overlay-put
  11343. ov 'display
  11344. (list 'image :type 'png :file movefile :ascent 'center)))
  11345. (push ov org-latex-fragment-image-overlays)
  11346. (goto-char end))
  11347. (delete-region beg end)
  11348. (insert link))))))))
  11349. ;; This function borrows from Ganesh Swami's latex2png.el
  11350. (defun org-create-formula-image (string tofile options buffer)
  11351. (let* ((tmpdir (if (featurep 'xemacs)
  11352. (temp-directory)
  11353. temporary-file-directory))
  11354. (texfilebase (make-temp-name
  11355. (expand-file-name "orgtex" tmpdir)))
  11356. (texfile (concat texfilebase ".tex"))
  11357. (dvifile (concat texfilebase ".dvi"))
  11358. (pngfile (concat texfilebase ".png"))
  11359. (fnh (if (featurep 'xemacs)
  11360. (font-height (get-face-font 'default))
  11361. (face-attribute 'default :height nil)))
  11362. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11363. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11364. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11365. "Black"))
  11366. (bg (or (plist-get options (if buffer :background :html-background))
  11367. "Transparent")))
  11368. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11369. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11370. (with-temp-file texfile
  11371. (insert org-format-latex-header
  11372. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11373. (let ((dir default-directory))
  11374. (condition-case nil
  11375. (progn
  11376. (cd tmpdir)
  11377. (call-process "latex" nil nil nil texfile))
  11378. (error nil))
  11379. (cd dir))
  11380. (if (not (file-exists-p dvifile))
  11381. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11382. (condition-case nil
  11383. (call-process "dvipng" nil nil nil
  11384. "-E" "-fg" fg "-bg" bg
  11385. "-D" dpi
  11386. ;;"-x" scale "-y" scale
  11387. "-T" "tight"
  11388. "-o" pngfile
  11389. dvifile)
  11390. (error nil))
  11391. (if (not (file-exists-p pngfile))
  11392. (progn (message "Failed to create png file from %s" texfile) nil)
  11393. ;; Use the requested file name and clean up
  11394. (copy-file pngfile tofile 'replace)
  11395. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11396. (delete-file (concat texfilebase e)))
  11397. pngfile))))
  11398. (defun org-dvipng-color (attr)
  11399. "Return an rgb color specification for dvipng."
  11400. (apply 'format "rgb %s %s %s"
  11401. (mapcar 'org-normalize-color
  11402. (color-values (face-attribute 'default attr nil)))))
  11403. (defun org-normalize-color (value)
  11404. "Return string to be used as color value for an RGB component."
  11405. (format "%g" (/ value 65535.0)))
  11406. ;;;; Key bindings
  11407. ;; Make `C-c C-x' a prefix key
  11408. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11409. ;; TAB key with modifiers
  11410. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11411. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11412. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11413. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11414. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11415. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11416. ;; The following line is necessary under Suse GNU/Linux
  11417. (unless (featurep 'xemacs)
  11418. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11419. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11420. (define-key org-mode-map [backtab] 'org-shifttab)
  11421. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11422. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11423. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11424. ;; Cursor keys with modifiers
  11425. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11426. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11427. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11428. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11429. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11430. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11431. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11432. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11433. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11434. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11435. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11436. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11437. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11438. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11439. ;;; Extra keys for tty access.
  11440. ;; We only set them when really needed because otherwise the
  11441. ;; menus don't show the simple keys
  11442. (when (or org-use-extra-keys
  11443. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11444. (not window-system))
  11445. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11446. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11447. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11448. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11449. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11450. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11451. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11452. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11453. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11454. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11455. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11456. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11457. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11458. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11459. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11460. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11461. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11462. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11463. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11464. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11465. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11466. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11467. ;; All the other keys
  11468. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11469. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11470. (if (boundp 'narrow-map)
  11471. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11472. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11473. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11474. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11475. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11476. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11477. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11478. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11479. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11480. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11481. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11482. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11483. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11484. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11485. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11486. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11487. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11488. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11489. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11490. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11491. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11492. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11493. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11494. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11495. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11496. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11497. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11498. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11499. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11500. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11501. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11502. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11503. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11504. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11505. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11506. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11507. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11508. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11509. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11510. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11511. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11512. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11513. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11514. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11515. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11516. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11517. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11518. (org-defkey org-mode-map "\C-m" 'org-return)
  11519. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11520. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11521. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11522. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11523. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11524. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11525. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11526. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11527. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11528. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11529. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11530. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11531. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11532. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11533. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11534. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11535. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11536. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11537. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11538. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11539. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11540. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11541. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11542. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11543. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11544. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11545. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11546. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11547. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11548. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11549. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11550. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11551. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11552. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11553. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11554. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11555. (when (featurep 'xemacs)
  11556. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11557. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11558. (defun org-self-insert-command (N)
  11559. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11560. If the cursor is in a table looking at whitespace, the whitespace is
  11561. overwritten, and the table is not marked as requiring realignment."
  11562. (interactive "p")
  11563. (if (and (org-table-p)
  11564. (progn
  11565. ;; check if we blank the field, and if that triggers align
  11566. (and (featurep 'org-table) org-table-auto-blank-field
  11567. (member last-command
  11568. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11569. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11570. ;; got extra space, this field does not determine column width
  11571. (let (org-table-may-need-update) (org-table-blank-field))
  11572. ;; no extra space, this field may determine column width
  11573. (org-table-blank-field)))
  11574. t)
  11575. (eq N 1)
  11576. (looking-at "[^|\n]* |"))
  11577. (let (org-table-may-need-update)
  11578. (goto-char (1- (match-end 0)))
  11579. (delete-backward-char 1)
  11580. (goto-char (match-beginning 0))
  11581. (self-insert-command N))
  11582. (setq org-table-may-need-update t)
  11583. (self-insert-command N)
  11584. (org-fix-tags-on-the-fly)))
  11585. (defun org-fix-tags-on-the-fly ()
  11586. (when (and (equal (char-after (point-at-bol)) ?*)
  11587. (org-on-heading-p))
  11588. (org-align-tags-here org-tags-column)))
  11589. (defun org-delete-backward-char (N)
  11590. "Like `delete-backward-char', insert whitespace at field end in tables.
  11591. When deleting backwards, in tables this function will insert whitespace in
  11592. front of the next \"|\" separator, to keep the table aligned. The table will
  11593. still be marked for re-alignment if the field did fill the entire column,
  11594. because, in this case the deletion might narrow the column."
  11595. (interactive "p")
  11596. (if (and (org-table-p)
  11597. (eq N 1)
  11598. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11599. (looking-at ".*?|"))
  11600. (let ((pos (point))
  11601. (noalign (looking-at "[^|\n\r]* |"))
  11602. (c org-table-may-need-update))
  11603. (backward-delete-char N)
  11604. (skip-chars-forward "^|")
  11605. (insert " ")
  11606. (goto-char (1- pos))
  11607. ;; noalign: if there were two spaces at the end, this field
  11608. ;; does not determine the width of the column.
  11609. (if noalign (setq org-table-may-need-update c)))
  11610. (backward-delete-char N)
  11611. (org-fix-tags-on-the-fly)))
  11612. (defun org-delete-char (N)
  11613. "Like `delete-char', but insert whitespace at field end in tables.
  11614. When deleting characters, in tables this function will insert whitespace in
  11615. front of the next \"|\" separator, to keep the table aligned. The table will
  11616. still be marked for re-alignment if the field did fill the entire column,
  11617. because, in this case the deletion might narrow the column."
  11618. (interactive "p")
  11619. (if (and (org-table-p)
  11620. (not (bolp))
  11621. (not (= (char-after) ?|))
  11622. (eq N 1))
  11623. (if (looking-at ".*?|")
  11624. (let ((pos (point))
  11625. (noalign (looking-at "[^|\n\r]* |"))
  11626. (c org-table-may-need-update))
  11627. (replace-match (concat
  11628. (substring (match-string 0) 1 -1)
  11629. " |"))
  11630. (goto-char pos)
  11631. ;; noalign: if there were two spaces at the end, this field
  11632. ;; does not determine the width of the column.
  11633. (if noalign (setq org-table-may-need-update c)))
  11634. (delete-char N))
  11635. (delete-char N)
  11636. (org-fix-tags-on-the-fly)))
  11637. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11638. (put 'org-self-insert-command 'delete-selection t)
  11639. (put 'orgtbl-self-insert-command 'delete-selection t)
  11640. (put 'org-delete-char 'delete-selection 'supersede)
  11641. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11642. ;; Make `flyspell-mode' delay after some commands
  11643. (put 'org-self-insert-command 'flyspell-delayed t)
  11644. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11645. (put 'org-delete-char 'flyspell-delayed t)
  11646. (put 'org-delete-backward-char 'flyspell-delayed t)
  11647. ;; Make pabbrev-mode expand after org-mode commands
  11648. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11649. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11650. ;; How to do this: Measure non-white length of current string
  11651. ;; If equal to column width, we should realign.
  11652. (defun org-remap (map &rest commands)
  11653. "In MAP, remap the functions given in COMMANDS.
  11654. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11655. (let (new old)
  11656. (while commands
  11657. (setq old (pop commands) new (pop commands))
  11658. (if (fboundp 'command-remapping)
  11659. (org-defkey map (vector 'remap old) new)
  11660. (substitute-key-definition old new map global-map)))))
  11661. (when (eq org-enable-table-editor 'optimized)
  11662. ;; If the user wants maximum table support, we need to hijack
  11663. ;; some standard editing functions
  11664. (org-remap org-mode-map
  11665. 'self-insert-command 'org-self-insert-command
  11666. 'delete-char 'org-delete-char
  11667. 'delete-backward-char 'org-delete-backward-char)
  11668. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11669. (defun org-shiftcursor-error ()
  11670. "Throw an error because Shift-Cursor command was applied in wrong context."
  11671. (error "This command is active in special context like tables, headlines or timestamps"))
  11672. (defun org-shifttab (&optional arg)
  11673. "Global visibility cycling or move to previous table field.
  11674. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11675. on context.
  11676. See the individual commands for more information."
  11677. (interactive "P")
  11678. (cond
  11679. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11680. ((integerp arg)
  11681. (message "Content view to level: %d" arg)
  11682. (org-content (prefix-numeric-value arg))
  11683. (setq org-cycle-global-status 'overview))
  11684. (t (call-interactively 'org-global-cycle))))
  11685. (defun org-shiftmetaleft ()
  11686. "Promote subtree or delete table column.
  11687. Calls `org-promote-subtree', `org-outdent-item',
  11688. or `org-table-delete-column', depending on context.
  11689. See the individual commands for more information."
  11690. (interactive)
  11691. (cond
  11692. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11693. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11694. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11695. (t (org-shiftcursor-error))))
  11696. (defun org-shiftmetaright ()
  11697. "Demote subtree or insert table column.
  11698. Calls `org-demote-subtree', `org-indent-item',
  11699. or `org-table-insert-column', depending on context.
  11700. See the individual commands for more information."
  11701. (interactive)
  11702. (cond
  11703. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11704. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11705. ((org-at-item-p) (call-interactively 'org-indent-item))
  11706. (t (org-shiftcursor-error))))
  11707. (defun org-shiftmetaup (&optional arg)
  11708. "Move subtree up or kill table row.
  11709. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11710. `org-move-item-up' depending on context. See the individual commands
  11711. for more information."
  11712. (interactive "P")
  11713. (cond
  11714. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11715. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11716. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11717. (t (org-shiftcursor-error))))
  11718. (defun org-shiftmetadown (&optional arg)
  11719. "Move subtree down or insert table row.
  11720. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11721. `org-move-item-down', depending on context. See the individual
  11722. commands for more information."
  11723. (interactive "P")
  11724. (cond
  11725. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11726. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11727. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11728. (t (org-shiftcursor-error))))
  11729. (defun org-metaleft (&optional arg)
  11730. "Promote heading or move table column to left.
  11731. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11732. With no specific context, calls the Emacs default `backward-word'.
  11733. See the individual commands for more information."
  11734. (interactive "P")
  11735. (cond
  11736. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11737. ((or (org-on-heading-p) (org-region-active-p))
  11738. (call-interactively 'org-do-promote))
  11739. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11740. (t (call-interactively 'backward-word))))
  11741. (defun org-metaright (&optional arg)
  11742. "Demote subtree or move table column to right.
  11743. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11744. With no specific context, calls the Emacs default `forward-word'.
  11745. See the individual commands for more information."
  11746. (interactive "P")
  11747. (cond
  11748. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11749. ((or (org-on-heading-p) (org-region-active-p))
  11750. (call-interactively 'org-do-demote))
  11751. ((org-at-item-p) (call-interactively 'org-indent-item))
  11752. (t (call-interactively 'forward-word))))
  11753. (defun org-metaup (&optional arg)
  11754. "Move subtree up or move table row up.
  11755. Calls `org-move-subtree-up' or `org-table-move-row' or
  11756. `org-move-item-up', depending on context. See the individual commands
  11757. for more information."
  11758. (interactive "P")
  11759. (cond
  11760. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11761. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11762. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11763. (t (transpose-lines 1) (beginning-of-line -1))))
  11764. (defun org-metadown (&optional arg)
  11765. "Move subtree down or move table row down.
  11766. Calls `org-move-subtree-down' or `org-table-move-row' or
  11767. `org-move-item-down', depending on context. See the individual
  11768. commands for more information."
  11769. (interactive "P")
  11770. (cond
  11771. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11772. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11773. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11774. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11775. (defun org-shiftup (&optional arg)
  11776. "Increase item in timestamp or increase priority of current headline.
  11777. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11778. depending on context. See the individual commands for more information."
  11779. (interactive "P")
  11780. (cond
  11781. ((org-at-timestamp-p t)
  11782. (call-interactively (if org-edit-timestamp-down-means-later
  11783. 'org-timestamp-down 'org-timestamp-up)))
  11784. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11785. ((org-at-item-p) (call-interactively 'org-previous-item))
  11786. ((org-clocktable-try-shift 'up arg))
  11787. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11788. (defun org-shiftdown (&optional arg)
  11789. "Decrease item in timestamp or decrease priority of current headline.
  11790. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11791. depending on context. See the individual commands for more information."
  11792. (interactive "P")
  11793. (cond
  11794. ((org-at-timestamp-p t)
  11795. (call-interactively (if org-edit-timestamp-down-means-later
  11796. 'org-timestamp-up 'org-timestamp-down)))
  11797. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11798. ((org-clocktable-try-shift 'down arg))
  11799. (t (call-interactively 'org-next-item))))
  11800. (defun org-shiftright (&optional arg)
  11801. "Cycle the thing at point or in the current line, depending on context.
  11802. Depending on context, this does one of the following:
  11803. - switch a timestamp at point one day into the future
  11804. - on a headline, switch to the next TODO keyword.
  11805. - on an item, switch entire list to the next bullet type
  11806. - on a property line, switch to the next allowed value
  11807. - on a clocktable definition line, move time block into the future"
  11808. (interactive "P")
  11809. (cond
  11810. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11811. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11812. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11813. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11814. ((org-clocktable-try-shift 'right arg))
  11815. (t (org-shiftcursor-error))))
  11816. (defun org-shiftleft (&optional arg)
  11817. "Cycle the thing at point or in the current line, depending on context.
  11818. Depending on context, this does one of the following:
  11819. - switch a timestamp at point one day into the past
  11820. - on a headline, switch to the previous TODO keyword.
  11821. - on an item, switch entire list to the previous bullet type
  11822. - on a property line, switch to the previous allowed value
  11823. - on a clocktable definition line, move time block into the past"
  11824. (interactive "P")
  11825. (cond
  11826. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11827. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11828. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11829. ((org-at-property-p)
  11830. (call-interactively 'org-property-previous-allowed-value))
  11831. ((org-clocktable-try-shift 'left arg))
  11832. (t (org-shiftcursor-error))))
  11833. (defun org-shiftcontrolright ()
  11834. "Switch to next TODO set."
  11835. (interactive)
  11836. (cond
  11837. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11838. (t (org-shiftcursor-error))))
  11839. (defun org-shiftcontrolleft ()
  11840. "Switch to previous TODO set."
  11841. (interactive)
  11842. (cond
  11843. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11844. (t (org-shiftcursor-error))))
  11845. (defun org-ctrl-c-ret ()
  11846. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11847. (interactive)
  11848. (cond
  11849. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11850. (t (call-interactively 'org-insert-heading))))
  11851. (defun org-copy-special ()
  11852. "Copy region in table or copy current subtree.
  11853. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11854. See the individual commands for more information."
  11855. (interactive)
  11856. (call-interactively
  11857. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11858. (defun org-cut-special ()
  11859. "Cut region in table or cut current subtree.
  11860. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11861. See the individual commands for more information."
  11862. (interactive)
  11863. (call-interactively
  11864. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11865. (defun org-paste-special (arg)
  11866. "Paste rectangular region into table, or past subtree relative to level.
  11867. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11868. See the individual commands for more information."
  11869. (interactive "P")
  11870. (if (org-at-table-p)
  11871. (org-table-paste-rectangle)
  11872. (org-paste-subtree arg)))
  11873. (defun org-edit-special ()
  11874. "Call a special editor for the stuff at point.
  11875. When at a table, call the formula editor with `org-table-edit-formulas'.
  11876. When at the first line of an src example, call `org-edit-src-code'.
  11877. When in an #+include line, visit the include file. Otherwise call
  11878. `ffap' to visit the file at point."
  11879. (interactive)
  11880. (cond
  11881. ((org-at-table-p)
  11882. (call-interactively 'org-table-edit-formulas))
  11883. ((save-excursion
  11884. (beginning-of-line 1)
  11885. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11886. (find-file (org-trim (match-string 1))))
  11887. ((org-edit-src-code))
  11888. ((org-edit-fixed-width-region))
  11889. (t (call-interactively 'ffap))))
  11890. (defun org-ctrl-c-ctrl-c (&optional arg)
  11891. "Set tags in headline, or update according to changed information at point.
  11892. This command does many different things, depending on context:
  11893. - If the cursor is in a headline, prompt for tags and insert them
  11894. into the current line, aligned to `org-tags-column'. When called
  11895. with prefix arg, realign all tags in the current buffer.
  11896. - If the cursor is in one of the special #+KEYWORD lines, this
  11897. triggers scanning the buffer for these lines and updating the
  11898. information.
  11899. - If the cursor is inside a table, realign the table. This command
  11900. works even if the automatic table editor has been turned off.
  11901. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11902. the entire table.
  11903. - If the cursor is a the beginning of a dynamic block, update it.
  11904. - If the cursor is inside a table created by the table.el package,
  11905. activate that table.
  11906. - If the current buffer is a remember buffer, close note and file
  11907. it. A prefix argument of 1 files to the default location
  11908. without further interaction. A prefix argument of 2 files to
  11909. the currently clocking task.
  11910. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11911. links in this buffer.
  11912. - If the cursor is on a numbered item in a plain list, renumber the
  11913. ordered list.
  11914. - If the cursor is on a checkbox, toggle it."
  11915. (interactive "P")
  11916. (let ((org-enable-table-editor t))
  11917. (cond
  11918. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11919. org-occur-highlights
  11920. org-latex-fragment-image-overlays)
  11921. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11922. (org-remove-occur-highlights)
  11923. (org-remove-latex-fragment-image-overlays)
  11924. (message "Temporary highlights/overlays removed from current buffer"))
  11925. ((and (local-variable-p 'org-finish-function (current-buffer))
  11926. (fboundp org-finish-function))
  11927. (funcall org-finish-function))
  11928. ((org-at-property-p)
  11929. (call-interactively 'org-property-action))
  11930. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11931. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11932. ((org-at-table.el-p)
  11933. (require 'table)
  11934. (beginning-of-line 1)
  11935. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11936. (call-interactively 'table-recognize-table))
  11937. ((org-at-table-p)
  11938. (org-table-maybe-eval-formula)
  11939. (if arg
  11940. (call-interactively 'org-table-recalculate)
  11941. (org-table-maybe-recalculate-line))
  11942. (call-interactively 'org-table-align))
  11943. ((org-at-item-checkbox-p)
  11944. (call-interactively 'org-toggle-checkbox))
  11945. ((org-at-item-p)
  11946. (call-interactively 'org-maybe-renumber-ordered-list))
  11947. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11948. ;; Dynamic block
  11949. (beginning-of-line 1)
  11950. (save-excursion (org-update-dblock)))
  11951. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11952. (cond
  11953. ((equal (match-string 1) "TBLFM")
  11954. ;; Recalculate the table before this line
  11955. (save-excursion
  11956. (beginning-of-line 1)
  11957. (skip-chars-backward " \r\n\t")
  11958. (if (org-at-table-p)
  11959. (org-call-with-arg 'org-table-recalculate t))))
  11960. (t
  11961. ; (org-set-regexps-and-options)
  11962. ; (org-restart-font-lock)
  11963. (let ((org-inhibit-startup t)) (org-mode-restart))
  11964. (message "Local setup has been refreshed"))))
  11965. (t (error "C-c C-c can do nothing useful at this location.")))))
  11966. (defun org-mode-restart ()
  11967. "Restart Org-mode, to scan again for special lines.
  11968. Also updates the keyword regular expressions."
  11969. (interactive)
  11970. (org-mode)
  11971. (message "Org-mode restarted"))
  11972. (defun org-kill-note-or-show-branches ()
  11973. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11974. (interactive)
  11975. (if (not org-finish-function)
  11976. (call-interactively 'show-branches)
  11977. (let ((org-note-abort t))
  11978. (funcall org-finish-function))))
  11979. (defun org-return (&optional indent)
  11980. "Goto next table row or insert a newline.
  11981. Calls `org-table-next-row' or `newline', depending on context.
  11982. See the individual commands for more information."
  11983. (interactive)
  11984. (cond
  11985. ((bobp) (if indent (newline-and-indent) (newline)))
  11986. ((and (org-at-heading-p)
  11987. (looking-at
  11988. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11989. (org-show-entry)
  11990. (end-of-line 1)
  11991. (newline))
  11992. ((org-at-table-p)
  11993. (org-table-justify-field-maybe)
  11994. (call-interactively 'org-table-next-row))
  11995. (t (if indent (newline-and-indent) (newline)))))
  11996. (defun org-return-indent ()
  11997. "Goto next table row or insert a newline and indent.
  11998. Calls `org-table-next-row' or `newline-and-indent', depending on
  11999. context. See the individual commands for more information."
  12000. (interactive)
  12001. (org-return t))
  12002. (defun org-ctrl-c-star ()
  12003. "Compute table, or change heading status of lines.
  12004. Calls `org-table-recalculate' or `org-toggle-region-headings',
  12005. depending on context. This will also turn a plain list item or a normal
  12006. line into a subheading."
  12007. (interactive)
  12008. (cond
  12009. ((org-at-table-p)
  12010. (call-interactively 'org-table-recalculate))
  12011. ((org-region-active-p)
  12012. ;; Convert all lines in region to list items
  12013. (call-interactively 'org-toggle-region-headings))
  12014. ((org-on-heading-p)
  12015. (org-toggle-region-headings (point-at-bol)
  12016. (min (1+ (point-at-eol)) (point-max))))
  12017. ((org-at-item-p)
  12018. ;; Convert to heading
  12019. (let ((level (save-match-data
  12020. (save-excursion
  12021. (condition-case nil
  12022. (progn
  12023. (org-back-to-heading t)
  12024. (funcall outline-level))
  12025. (error 0))))))
  12026. (replace-match
  12027. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12028. (t (org-toggle-region-headings (point-at-bol)
  12029. (min (1+ (point-at-eol)) (point-max))))))
  12030. (defun org-ctrl-c-minus ()
  12031. "Insert separator line in table or modify bullet status of line.
  12032. Also turns a plain line or a region of lines into list items.
  12033. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12034. `org-cycle-list-bullet', depending on context."
  12035. (interactive)
  12036. (cond
  12037. ((org-at-table-p)
  12038. (call-interactively 'org-table-insert-hline))
  12039. ((org-on-heading-p)
  12040. ;; Convert to item
  12041. (save-excursion
  12042. (beginning-of-line 1)
  12043. (if (looking-at "\\*+ ")
  12044. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  12045. ((org-region-active-p)
  12046. ;; Convert all lines in region to list items
  12047. (call-interactively 'org-toggle-region-items))
  12048. ((org-in-item-p)
  12049. (call-interactively 'org-cycle-list-bullet))
  12050. (t (org-toggle-region-items (point-at-bol)
  12051. (min (1+ (point-at-eol)) (point-max))))))
  12052. (defun org-toggle-region-items (beg end)
  12053. "Convert all lines in region to list items.
  12054. If the first line is already an item, convert all list items in the region
  12055. to normal lines."
  12056. (interactive "r")
  12057. (let (l2 l)
  12058. (save-excursion
  12059. (goto-char end)
  12060. (setq l2 (org-current-line))
  12061. (goto-char beg)
  12062. (beginning-of-line 1)
  12063. (setq l (1- (org-current-line)))
  12064. (if (org-at-item-p)
  12065. ;; We already have items, de-itemize
  12066. (while (< (setq l (1+ l)) l2)
  12067. (when (org-at-item-p)
  12068. (goto-char (match-beginning 2))
  12069. (delete-region (match-beginning 2) (match-end 2))
  12070. (and (looking-at "[ \t]+") (replace-match "")))
  12071. (beginning-of-line 2))
  12072. (while (< (setq l (1+ l)) l2)
  12073. (unless (org-at-item-p)
  12074. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12075. (replace-match "\\1- \\2")))
  12076. (beginning-of-line 2))))))
  12077. (defun org-toggle-region-headings (beg end)
  12078. "Convert all lines in region to list items.
  12079. If the first line is already an item, convert all list items in the region
  12080. to normal lines."
  12081. (interactive "r")
  12082. (let (l2 l)
  12083. (save-excursion
  12084. (goto-char end)
  12085. (setq l2 (org-current-line))
  12086. (goto-char beg)
  12087. (beginning-of-line 1)
  12088. (setq l (1- (org-current-line)))
  12089. (if (org-on-heading-p)
  12090. ;; We already have headlines, de-star them
  12091. (while (< (setq l (1+ l)) l2)
  12092. (when (org-on-heading-p t)
  12093. (and (looking-at outline-regexp) (replace-match "")))
  12094. (beginning-of-line 2))
  12095. (let* ((stars (save-excursion
  12096. (re-search-backward org-complex-heading-regexp nil t)
  12097. (or (match-string 1) "*")))
  12098. (add-stars (if org-odd-levels-only "**" "*"))
  12099. (rpl (concat stars add-stars " \\2")))
  12100. (while (< (setq l (1+ l)) l2)
  12101. (unless (org-on-heading-p)
  12102. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12103. (replace-match rpl)))
  12104. (beginning-of-line 2)))))))
  12105. (defun org-meta-return (&optional arg)
  12106. "Insert a new heading or wrap a region in a table.
  12107. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12108. See the individual commands for more information."
  12109. (interactive "P")
  12110. (cond
  12111. ((org-at-table-p)
  12112. (call-interactively 'org-table-wrap-region))
  12113. (t (call-interactively 'org-insert-heading))))
  12114. ;;; Menu entries
  12115. ;; Define the Org-mode menus
  12116. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12117. '("Tbl"
  12118. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12119. ["Next Field" org-cycle (org-at-table-p)]
  12120. ["Previous Field" org-shifttab (org-at-table-p)]
  12121. ["Next Row" org-return (org-at-table-p)]
  12122. "--"
  12123. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12124. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12125. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12126. "--"
  12127. ("Column"
  12128. ["Move Column Left" org-metaleft (org-at-table-p)]
  12129. ["Move Column Right" org-metaright (org-at-table-p)]
  12130. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12131. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12132. ("Row"
  12133. ["Move Row Up" org-metaup (org-at-table-p)]
  12134. ["Move Row Down" org-metadown (org-at-table-p)]
  12135. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12136. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12137. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12138. "--"
  12139. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12140. ("Rectangle"
  12141. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12142. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12143. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12144. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12145. "--"
  12146. ("Calculate"
  12147. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12148. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12149. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12150. "--"
  12151. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12152. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12153. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12154. "--"
  12155. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12156. "--"
  12157. ["Sum Column/Rectangle" org-table-sum
  12158. (or (org-at-table-p) (org-region-active-p))]
  12159. ["Which Column?" org-table-current-column (org-at-table-p)])
  12160. ["Debug Formulas"
  12161. org-table-toggle-formula-debugger
  12162. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12163. ["Show Col/Row Numbers"
  12164. org-table-toggle-coordinate-overlays
  12165. :style toggle
  12166. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12167. "--"
  12168. ["Create" org-table-create (and (not (org-at-table-p))
  12169. org-enable-table-editor)]
  12170. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12171. ["Import from File" org-table-import (not (org-at-table-p))]
  12172. ["Export to File" org-table-export (org-at-table-p)]
  12173. "--"
  12174. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12175. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12176. '("Org"
  12177. ("Show/Hide"
  12178. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12179. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12180. ["Sparse Tree..." org-sparse-tree t]
  12181. ["Reveal Context" org-reveal t]
  12182. ["Show All" show-all t]
  12183. "--"
  12184. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12185. "--"
  12186. ["New Heading" org-insert-heading t]
  12187. ("Navigate Headings"
  12188. ["Up" outline-up-heading t]
  12189. ["Next" outline-next-visible-heading t]
  12190. ["Previous" outline-previous-visible-heading t]
  12191. ["Next Same Level" outline-forward-same-level t]
  12192. ["Previous Same Level" outline-backward-same-level t]
  12193. "--"
  12194. ["Jump" org-goto t])
  12195. ("Edit Structure"
  12196. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12197. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12198. "--"
  12199. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12200. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12201. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12202. "--"
  12203. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12204. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12205. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12206. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12207. "--"
  12208. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12209. "--"
  12210. ["Convert to odd levels" org-convert-to-odd-levels t]
  12211. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12212. ("Editing"
  12213. ["Emphasis..." org-emphasize t]
  12214. ["Edit Source Example" org-edit-special t])
  12215. ("Archive"
  12216. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12217. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12218. ; :active t :keys "C-u C-c C-x C-a"]
  12219. ["Sparse trees open ARCHIVE trees"
  12220. (setq org-sparse-tree-open-archived-trees
  12221. (not org-sparse-tree-open-archived-trees))
  12222. :style toggle :selected org-sparse-tree-open-archived-trees]
  12223. ["Cycling opens ARCHIVE trees"
  12224. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12225. :style toggle :selected org-cycle-open-archived-trees]
  12226. "--"
  12227. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12228. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12229. ; ["Check and Move Children" (org-archive-subtree '(4))
  12230. ; :active t :keys "C-u C-c C-x C-s"]
  12231. )
  12232. "--"
  12233. ("TODO Lists"
  12234. ["TODO/DONE/-" org-todo t]
  12235. ("Select keyword"
  12236. ["Next keyword" org-shiftright (org-on-heading-p)]
  12237. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12238. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12239. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12240. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12241. ["Show TODO Tree" org-show-todo-tree t]
  12242. ["Global TODO list" org-todo-list t]
  12243. "--"
  12244. ["Set Priority" org-priority t]
  12245. ["Priority Up" org-shiftup t]
  12246. ["Priority Down" org-shiftdown t])
  12247. ("TAGS and Properties"
  12248. ["Set Tags" org-set-tags-command t]
  12249. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12250. "--"
  12251. ["Set property" org-set-property t]
  12252. ["Column view of properties" org-columns t]
  12253. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12254. ("Dates and Scheduling"
  12255. ["Timestamp" org-time-stamp t]
  12256. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12257. ("Change Date"
  12258. ["1 Day Later" org-shiftright t]
  12259. ["1 Day Earlier" org-shiftleft t]
  12260. ["1 ... Later" org-shiftup t]
  12261. ["1 ... Earlier" org-shiftdown t])
  12262. ["Compute Time Range" org-evaluate-time-range t]
  12263. ["Schedule Item" org-schedule t]
  12264. ["Deadline" org-deadline t]
  12265. "--"
  12266. ["Custom time format" org-toggle-time-stamp-overlays
  12267. :style radio :selected org-display-custom-times]
  12268. "--"
  12269. ["Goto Calendar" org-goto-calendar t]
  12270. ["Date from Calendar" org-date-from-calendar t]
  12271. "--"
  12272. ["Start/restart timer" org-timer-start t]
  12273. ["Insert timer string" org-timer t]
  12274. ["Insert timer item" org-timer-item t])
  12275. ("Logging work"
  12276. ["Clock in" org-clock-in t]
  12277. ["Clock out" org-clock-out t]
  12278. ["Clock cancel" org-clock-cancel t]
  12279. ["Goto running clock" org-clock-goto t]
  12280. ["Display times" org-clock-display t]
  12281. ["Create clock table" org-clock-report t]
  12282. "--"
  12283. ["Record DONE time"
  12284. (progn (setq org-log-done (not org-log-done))
  12285. (message "Switching to %s will %s record a timestamp"
  12286. (car org-done-keywords)
  12287. (if org-log-done "automatically" "not")))
  12288. :style toggle :selected org-log-done])
  12289. "--"
  12290. ["Agenda Command..." org-agenda t]
  12291. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12292. ("File List for Agenda")
  12293. ("Special views current file"
  12294. ["TODO Tree" org-show-todo-tree t]
  12295. ["Check Deadlines" org-check-deadlines t]
  12296. ["Timeline" org-timeline t]
  12297. ["Tags Tree" org-tags-sparse-tree t])
  12298. "--"
  12299. ("Hyperlinks"
  12300. ["Store Link (Global)" org-store-link t]
  12301. ["Insert Link" org-insert-link t]
  12302. ["Follow Link" org-open-at-point t]
  12303. "--"
  12304. ["Next link" org-next-link t]
  12305. ["Previous link" org-previous-link t]
  12306. "--"
  12307. ["Descriptive Links"
  12308. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12309. :style radio
  12310. :selected (member '(org-link) buffer-invisibility-spec)]
  12311. ["Literal Links"
  12312. (progn
  12313. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12314. :style radio
  12315. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12316. "--"
  12317. ["Export/Publish..." org-export t]
  12318. ("LaTeX"
  12319. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12320. :selected org-cdlatex-mode]
  12321. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12322. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12323. ["Modify math symbol" org-cdlatex-math-modify
  12324. (org-inside-LaTeX-fragment-p)]
  12325. ["Export LaTeX fragments as images"
  12326. (if (featurep 'org-exp)
  12327. (setq org-export-with-LaTeX-fragments
  12328. (not org-export-with-LaTeX-fragments))
  12329. (require 'org-exp))
  12330. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12331. org-export-with-LaTeX-fragments)])
  12332. "--"
  12333. ("Documentation"
  12334. ["Show Version" org-version t]
  12335. ["Info Documentation" org-info t])
  12336. ("Customize"
  12337. ["Browse Org Group" org-customize t]
  12338. "--"
  12339. ["Expand This Menu" org-create-customize-menu
  12340. (fboundp 'customize-menu-create)])
  12341. "--"
  12342. ["Refresh setup" org-mode-restart t]
  12343. ))
  12344. (defun org-info (&optional node)
  12345. "Read documentation for Org-mode in the info system.
  12346. With optional NODE, go directly to that node."
  12347. (interactive)
  12348. (info (format "(org)%s" (or node ""))))
  12349. (defun org-install-agenda-files-menu ()
  12350. (let ((bl (buffer-list)))
  12351. (save-excursion
  12352. (while bl
  12353. (set-buffer (pop bl))
  12354. (if (org-mode-p) (setq bl nil)))
  12355. (when (org-mode-p)
  12356. (easy-menu-change
  12357. '("Org") "File List for Agenda"
  12358. (append
  12359. (list
  12360. ["Edit File List" (org-edit-agenda-file-list) t]
  12361. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12362. ["Remove Current File from List" org-remove-file t]
  12363. ["Cycle through agenda files" org-cycle-agenda-files t]
  12364. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12365. "--")
  12366. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12367. ;;;; Documentation
  12368. ;;;###autoload
  12369. (defun org-require-autoloaded-modules ()
  12370. (interactive)
  12371. (mapc 'require
  12372. '(org-agenda org-archive org-clock org-colview
  12373. org-exp org-id org-export-latex org-publish
  12374. org-remember org-table)))
  12375. ;;;###autoload
  12376. (defun org-customize ()
  12377. "Call the customize function with org as argument."
  12378. (interactive)
  12379. (org-load-modules-maybe)
  12380. (org-require-autoloaded-modules)
  12381. (customize-browse 'org))
  12382. (defun org-create-customize-menu ()
  12383. "Create a full customization menu for Org-mode, insert it into the menu."
  12384. (interactive)
  12385. (org-load-modules-maybe)
  12386. (org-require-autoloaded-modules)
  12387. (if (fboundp 'customize-menu-create)
  12388. (progn
  12389. (easy-menu-change
  12390. '("Org") "Customize"
  12391. `(["Browse Org group" org-customize t]
  12392. "--"
  12393. ,(customize-menu-create 'org)
  12394. ["Set" Custom-set t]
  12395. ["Save" Custom-save t]
  12396. ["Reset to Current" Custom-reset-current t]
  12397. ["Reset to Saved" Custom-reset-saved t]
  12398. ["Reset to Standard Settings" Custom-reset-standard t]))
  12399. (message "\"Org\"-menu now contains full customization menu"))
  12400. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12401. ;;;; Miscellaneous stuff
  12402. ;;; Generally useful functions
  12403. (defun org-find-text-property-in-string (prop s)
  12404. "Return the first non-nil value of property PROP in string S."
  12405. (or (get-text-property 0 prop s)
  12406. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12407. prop s)))
  12408. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12409. "Display the given MESSAGE as a warning."
  12410. (if (fboundp 'display-warning)
  12411. (display-warning 'org message
  12412. (if (featurep 'xemacs)
  12413. 'warning
  12414. :warning))
  12415. (let ((buf (get-buffer-create "*Org warnings*")))
  12416. (with-current-buffer buf
  12417. (goto-char (point-max))
  12418. (insert "Warning (Org): " message)
  12419. (unless (bolp)
  12420. (newline)))
  12421. (display-buffer buf)
  12422. (sit-for 0))))
  12423. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12424. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12425. (if (and marker (marker-buffer marker)
  12426. (buffer-live-p (marker-buffer marker)))
  12427. (progn
  12428. (switch-to-buffer (marker-buffer marker))
  12429. (if (or (> marker (point-max)) (< marker (point-min)))
  12430. (widen))
  12431. (goto-char marker))
  12432. (if bookmark
  12433. (bookmark-jump bookmark)
  12434. (error "Cannot find location"))))
  12435. (defun org-quote-csv-field (s)
  12436. "Quote field for inclusion in CSV material."
  12437. (if (string-match "[\",]" s)
  12438. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12439. s))
  12440. (defun org-plist-delete (plist property)
  12441. "Delete PROPERTY from PLIST.
  12442. This is in contrast to merely setting it to 0."
  12443. (let (p)
  12444. (while plist
  12445. (if (not (eq property (car plist)))
  12446. (setq p (plist-put p (car plist) (nth 1 plist))))
  12447. (setq plist (cddr plist)))
  12448. p))
  12449. (defun org-force-self-insert (N)
  12450. "Needed to enforce self-insert under remapping."
  12451. (interactive "p")
  12452. (self-insert-command N))
  12453. (defun org-string-width (s)
  12454. "Compute width of string, ignoring invisible characters.
  12455. This ignores character with invisibility property `org-link', and also
  12456. characters with property `org-cwidth', because these will become invisible
  12457. upon the next fontification round."
  12458. (let (b l)
  12459. (when (or (eq t buffer-invisibility-spec)
  12460. (assq 'org-link buffer-invisibility-spec))
  12461. (while (setq b (text-property-any 0 (length s)
  12462. 'invisible 'org-link s))
  12463. (setq s (concat (substring s 0 b)
  12464. (substring s (or (next-single-property-change
  12465. b 'invisible s) (length s)))))))
  12466. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12467. (setq s (concat (substring s 0 b)
  12468. (substring s (or (next-single-property-change
  12469. b 'org-cwidth s) (length s))))))
  12470. (setq l (string-width s) b -1)
  12471. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12472. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12473. l))
  12474. (defun org-get-indentation (&optional line)
  12475. "Get the indentation of the current line, interpreting tabs.
  12476. When LINE is given, assume it represents a line and compute its indentation."
  12477. (if line
  12478. (if (string-match "^ *" (org-remove-tabs line))
  12479. (match-end 0))
  12480. (save-excursion
  12481. (beginning-of-line 1)
  12482. (skip-chars-forward " \t")
  12483. (current-column))))
  12484. (defun org-remove-tabs (s &optional width)
  12485. "Replace tabulators in S with spaces.
  12486. Assumes that s is a single line, starting in column 0."
  12487. (setq width (or width tab-width))
  12488. (while (string-match "\t" s)
  12489. (setq s (replace-match
  12490. (make-string
  12491. (- (* width (/ (+ (match-beginning 0) width) width))
  12492. (match-beginning 0)) ?\ )
  12493. t t s)))
  12494. s)
  12495. (defun org-fix-indentation (line ind)
  12496. "Fix indentation in LINE.
  12497. IND is a cons cell with target and minimum indentation.
  12498. If the current indentation in LINE is smaller than the minimum,
  12499. leave it alone. If it is larger than ind, set it to the target."
  12500. (let* ((l (org-remove-tabs line))
  12501. (i (org-get-indentation l))
  12502. (i1 (car ind)) (i2 (cdr ind)))
  12503. (if (>= i i2) (setq l (substring line i2)))
  12504. (if (> i1 0)
  12505. (concat (make-string i1 ?\ ) l)
  12506. l)))
  12507. (defun org-base-buffer (buffer)
  12508. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12509. (if (not buffer)
  12510. buffer
  12511. (or (buffer-base-buffer buffer)
  12512. buffer)))
  12513. (defun org-trim (s)
  12514. "Remove whitespace at beginning and end of string."
  12515. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12516. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12517. s)
  12518. (defun org-wrap (string &optional width lines)
  12519. "Wrap string to either a number of lines, or a width in characters.
  12520. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12521. that costs. If there is a word longer than WIDTH, the text is actually
  12522. wrapped to the length of that word.
  12523. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12524. many lines, whatever width that takes.
  12525. The return value is a list of lines, without newlines at the end."
  12526. (let* ((words (org-split-string string "[ \t\n]+"))
  12527. (maxword (apply 'max (mapcar 'org-string-width words)))
  12528. w ll)
  12529. (cond (width
  12530. (org-do-wrap words (max maxword width)))
  12531. (lines
  12532. (setq w maxword)
  12533. (setq ll (org-do-wrap words maxword))
  12534. (if (<= (length ll) lines)
  12535. ll
  12536. (setq ll words)
  12537. (while (> (length ll) lines)
  12538. (setq w (1+ w))
  12539. (setq ll (org-do-wrap words w)))
  12540. ll))
  12541. (t (error "Cannot wrap this")))))
  12542. (defun org-do-wrap (words width)
  12543. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12544. (let (lines line)
  12545. (while words
  12546. (setq line (pop words))
  12547. (while (and words (< (+ (length line) (length (car words))) width))
  12548. (setq line (concat line " " (pop words))))
  12549. (setq lines (push line lines)))
  12550. (nreverse lines)))
  12551. (defun org-split-string (string &optional separators)
  12552. "Splits STRING into substrings at SEPARATORS.
  12553. No empty strings are returned if there are matches at the beginning
  12554. and end of string."
  12555. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12556. (start 0)
  12557. notfirst
  12558. (list nil))
  12559. (while (and (string-match rexp string
  12560. (if (and notfirst
  12561. (= start (match-beginning 0))
  12562. (< start (length string)))
  12563. (1+ start) start))
  12564. (< (match-beginning 0) (length string)))
  12565. (setq notfirst t)
  12566. (or (eq (match-beginning 0) 0)
  12567. (and (eq (match-beginning 0) (match-end 0))
  12568. (eq (match-beginning 0) start))
  12569. (setq list
  12570. (cons (substring string start (match-beginning 0))
  12571. list)))
  12572. (setq start (match-end 0)))
  12573. (or (eq start (length string))
  12574. (setq list
  12575. (cons (substring string start)
  12576. list)))
  12577. (nreverse list)))
  12578. (defun org-context ()
  12579. "Return a list of contexts of the current cursor position.
  12580. If several contexts apply, all are returned.
  12581. Each context entry is a list with a symbol naming the context, and
  12582. two positions indicating start and end of the context. Possible
  12583. contexts are:
  12584. :headline anywhere in a headline
  12585. :headline-stars on the leading stars in a headline
  12586. :todo-keyword on a TODO keyword (including DONE) in a headline
  12587. :tags on the TAGS in a headline
  12588. :priority on the priority cookie in a headline
  12589. :item on the first line of a plain list item
  12590. :item-bullet on the bullet/number of a plain list item
  12591. :checkbox on the checkbox in a plain list item
  12592. :table in an org-mode table
  12593. :table-special on a special filed in a table
  12594. :table-table in a table.el table
  12595. :link on a hyperlink
  12596. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12597. :target on a <<target>>
  12598. :radio-target on a <<<radio-target>>>
  12599. :latex-fragment on a LaTeX fragment
  12600. :latex-preview on a LaTeX fragment with overlayed preview image
  12601. This function expects the position to be visible because it uses font-lock
  12602. faces as a help to recognize the following contexts: :table-special, :link,
  12603. and :keyword."
  12604. (let* ((f (get-text-property (point) 'face))
  12605. (faces (if (listp f) f (list f)))
  12606. (p (point)) clist o)
  12607. ;; First the large context
  12608. (cond
  12609. ((org-on-heading-p t)
  12610. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12611. (when (progn
  12612. (beginning-of-line 1)
  12613. (looking-at org-todo-line-tags-regexp))
  12614. (push (org-point-in-group p 1 :headline-stars) clist)
  12615. (push (org-point-in-group p 2 :todo-keyword) clist)
  12616. (push (org-point-in-group p 4 :tags) clist))
  12617. (goto-char p)
  12618. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12619. (if (looking-at "\\[#[A-Z0-9]\\]")
  12620. (push (org-point-in-group p 0 :priority) clist)))
  12621. ((org-at-item-p)
  12622. (push (org-point-in-group p 2 :item-bullet) clist)
  12623. (push (list :item (point-at-bol)
  12624. (save-excursion (org-end-of-item) (point)))
  12625. clist)
  12626. (and (org-at-item-checkbox-p)
  12627. (push (org-point-in-group p 0 :checkbox) clist)))
  12628. ((org-at-table-p)
  12629. (push (list :table (org-table-begin) (org-table-end)) clist)
  12630. (if (memq 'org-formula faces)
  12631. (push (list :table-special
  12632. (previous-single-property-change p 'face)
  12633. (next-single-property-change p 'face)) clist)))
  12634. ((org-at-table-p 'any)
  12635. (push (list :table-table) clist)))
  12636. (goto-char p)
  12637. ;; Now the small context
  12638. (cond
  12639. ((org-at-timestamp-p)
  12640. (push (org-point-in-group p 0 :timestamp) clist))
  12641. ((memq 'org-link faces)
  12642. (push (list :link
  12643. (previous-single-property-change p 'face)
  12644. (next-single-property-change p 'face)) clist))
  12645. ((memq 'org-special-keyword faces)
  12646. (push (list :keyword
  12647. (previous-single-property-change p 'face)
  12648. (next-single-property-change p 'face)) clist))
  12649. ((org-on-target-p)
  12650. (push (org-point-in-group p 0 :target) clist)
  12651. (goto-char (1- (match-beginning 0)))
  12652. (if (looking-at org-radio-target-regexp)
  12653. (push (org-point-in-group p 0 :radio-target) clist))
  12654. (goto-char p))
  12655. ((setq o (car (delq nil
  12656. (mapcar
  12657. (lambda (x)
  12658. (if (memq x org-latex-fragment-image-overlays) x))
  12659. (org-overlays-at (point))))))
  12660. (push (list :latex-fragment
  12661. (org-overlay-start o) (org-overlay-end o)) clist)
  12662. (push (list :latex-preview
  12663. (org-overlay-start o) (org-overlay-end o)) clist))
  12664. ((org-inside-LaTeX-fragment-p)
  12665. ;; FIXME: positions wrong.
  12666. (push (list :latex-fragment (point) (point)) clist)))
  12667. (setq clist (nreverse (delq nil clist)))
  12668. clist))
  12669. ;; FIXME: Compare with at-regexp-p Do we need both?
  12670. (defun org-in-regexp (re &optional nlines visually)
  12671. "Check if point is inside a match of regexp.
  12672. Normally only the current line is checked, but you can include NLINES extra
  12673. lines both before and after point into the search.
  12674. If VISUALLY is set, require that the cursor is not after the match but
  12675. really on, so that the block visually is on the match."
  12676. (catch 'exit
  12677. (let ((pos (point))
  12678. (eol (point-at-eol (+ 1 (or nlines 0))))
  12679. (inc (if visually 1 0)))
  12680. (save-excursion
  12681. (beginning-of-line (- 1 (or nlines 0)))
  12682. (while (re-search-forward re eol t)
  12683. (if (and (<= (match-beginning 0) pos)
  12684. (>= (+ inc (match-end 0)) pos))
  12685. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12686. (defun org-at-regexp-p (regexp)
  12687. "Is point inside a match of REGEXP in the current line?"
  12688. (catch 'exit
  12689. (save-excursion
  12690. (let ((pos (point)) (end (point-at-eol)))
  12691. (beginning-of-line 1)
  12692. (while (re-search-forward regexp end t)
  12693. (if (and (<= (match-beginning 0) pos)
  12694. (>= (match-end 0) pos))
  12695. (throw 'exit t)))
  12696. nil))))
  12697. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12698. "Call `multi-occur' with buffers for all agenda files."
  12699. (interactive "sOrg-files matching: \np")
  12700. (let* ((files (org-agenda-files))
  12701. (tnames (mapcar 'file-truename files))
  12702. (extra org-agenda-text-search-extra-files)
  12703. f)
  12704. (when (eq (car extra) 'agenda-archives)
  12705. (setq extra (cdr extra))
  12706. (setq files (org-add-archive-files files)))
  12707. (while (setq f (pop extra))
  12708. (unless (member (file-truename f) tnames)
  12709. (add-to-list 'files f 'append)
  12710. (add-to-list 'tnames (file-truename f) 'append)))
  12711. (multi-occur
  12712. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12713. regexp)))
  12714. (if (boundp 'occur-mode-find-occurrence-hook)
  12715. ;; Emacs 23
  12716. (add-hook 'occur-mode-find-occurrence-hook
  12717. (lambda ()
  12718. (when (org-mode-p)
  12719. (org-reveal))))
  12720. ;; Emacs 22
  12721. (defadvice occur-mode-goto-occurrence
  12722. (after org-occur-reveal activate)
  12723. (and (org-mode-p) (org-reveal)))
  12724. (defadvice occur-mode-goto-occurrence-other-window
  12725. (after org-occur-reveal activate)
  12726. (and (org-mode-p) (org-reveal)))
  12727. (defadvice occur-mode-display-occurrence
  12728. (after org-occur-reveal activate)
  12729. (when (org-mode-p)
  12730. (let ((pos (occur-mode-find-occurrence)))
  12731. (with-current-buffer (marker-buffer pos)
  12732. (save-excursion
  12733. (goto-char pos)
  12734. (org-reveal)))))))
  12735. (defun org-uniquify (list)
  12736. "Remove duplicate elements from LIST."
  12737. (let (res)
  12738. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12739. res))
  12740. (defun org-delete-all (elts list)
  12741. "Remove all elements in ELTS from LIST."
  12742. (while elts
  12743. (setq list (delete (pop elts) list)))
  12744. list)
  12745. (defun org-back-over-empty-lines ()
  12746. "Move backwards over whitespace, to the beginning of the first empty line.
  12747. Returns the number of empty lines passed."
  12748. (let ((pos (point)))
  12749. (skip-chars-backward " \t\n\r")
  12750. (beginning-of-line 2)
  12751. (goto-char (min (point) pos))
  12752. (count-lines (point) pos)))
  12753. (defun org-skip-whitespace ()
  12754. (skip-chars-forward " \t\n\r"))
  12755. (defun org-point-in-group (point group &optional context)
  12756. "Check if POINT is in match-group GROUP.
  12757. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12758. match. If the match group does ot exist or point is not inside it,
  12759. return nil."
  12760. (and (match-beginning group)
  12761. (>= point (match-beginning group))
  12762. (<= point (match-end group))
  12763. (if context
  12764. (list context (match-beginning group) (match-end group))
  12765. t)))
  12766. (defun org-switch-to-buffer-other-window (&rest args)
  12767. "Switch to buffer in a second window on the current frame.
  12768. In particular, do not allow pop-up frames."
  12769. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12770. special-display-function)
  12771. (apply 'switch-to-buffer-other-window args)))
  12772. (defun org-combine-plists (&rest plists)
  12773. "Create a single property list from all plists in PLISTS.
  12774. The process starts by copying the first list, and then setting properties
  12775. from the other lists. Settings in the last list are the most significant
  12776. ones and overrule settings in the other lists."
  12777. (let ((rtn (copy-sequence (pop plists)))
  12778. p v ls)
  12779. (while plists
  12780. (setq ls (pop plists))
  12781. (while ls
  12782. (setq p (pop ls) v (pop ls))
  12783. (setq rtn (plist-put rtn p v))))
  12784. rtn))
  12785. (defun org-move-line-down (arg)
  12786. "Move the current line down. With prefix argument, move it past ARG lines."
  12787. (interactive "p")
  12788. (let ((col (current-column))
  12789. beg end pos)
  12790. (beginning-of-line 1) (setq beg (point))
  12791. (beginning-of-line 2) (setq end (point))
  12792. (beginning-of-line (+ 1 arg))
  12793. (setq pos (move-marker (make-marker) (point)))
  12794. (insert (delete-and-extract-region beg end))
  12795. (goto-char pos)
  12796. (org-move-to-column col)))
  12797. (defun org-move-line-up (arg)
  12798. "Move the current line up. With prefix argument, move it past ARG lines."
  12799. (interactive "p")
  12800. (let ((col (current-column))
  12801. beg end pos)
  12802. (beginning-of-line 1) (setq beg (point))
  12803. (beginning-of-line 2) (setq end (point))
  12804. (beginning-of-line (- arg))
  12805. (setq pos (move-marker (make-marker) (point)))
  12806. (insert (delete-and-extract-region beg end))
  12807. (goto-char pos)
  12808. (org-move-to-column col)))
  12809. (defun org-replace-escapes (string table)
  12810. "Replace %-escapes in STRING with values in TABLE.
  12811. TABLE is an association list with keys like \"%a\" and string values.
  12812. The sequences in STRING may contain normal field width and padding information,
  12813. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12814. so values can contain further %-escapes if they are define later in TABLE."
  12815. (let ((case-fold-search nil)
  12816. e re rpl)
  12817. (while (setq e (pop table))
  12818. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12819. (while (string-match re string)
  12820. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12821. (cdr e)))
  12822. (setq string (replace-match rpl t t string))))
  12823. string))
  12824. (defun org-sublist (list start end)
  12825. "Return a section of LIST, from START to END.
  12826. Counting starts at 1."
  12827. (let (rtn (c start))
  12828. (setq list (nthcdr (1- start) list))
  12829. (while (and list (<= c end))
  12830. (push (pop list) rtn)
  12831. (setq c (1+ c)))
  12832. (nreverse rtn)))
  12833. (defun org-find-base-buffer-visiting (file)
  12834. "Like `find-buffer-visiting' but alway return the base buffer and
  12835. not an indirect buffer."
  12836. (let ((buf (find-buffer-visiting file)))
  12837. (if buf
  12838. (or (buffer-base-buffer buf) buf)
  12839. nil)))
  12840. (defun org-image-file-name-regexp ()
  12841. "Return regexp matching the file names of images."
  12842. (if (fboundp 'image-file-name-regexp)
  12843. (image-file-name-regexp)
  12844. (let ((image-file-name-extensions
  12845. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12846. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12847. (concat "\\."
  12848. (regexp-opt (nconc (mapcar 'upcase
  12849. image-file-name-extensions)
  12850. image-file-name-extensions)
  12851. t)
  12852. "\\'"))))
  12853. (defun org-file-image-p (file)
  12854. "Return non-nil if FILE is an image."
  12855. (save-match-data
  12856. (string-match (org-image-file-name-regexp) file)))
  12857. (defun org-get-cursor-date ()
  12858. "Return the date at cursor in as a time.
  12859. This works in the calendar and in the agenda, anywhere else it just
  12860. returns the current time."
  12861. (let (date day defd)
  12862. (cond
  12863. ((eq major-mode 'calendar-mode)
  12864. (setq date (calendar-cursor-to-date)
  12865. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12866. ((eq major-mode 'org-agenda-mode)
  12867. (setq day (get-text-property (point) 'day))
  12868. (if day
  12869. (setq date (calendar-gregorian-from-absolute day)
  12870. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12871. (nth 2 date))))))
  12872. (or defd (current-time))))
  12873. (defvar org-agenda-action-marker (make-marker)
  12874. "Marker pointing to the entry for the next agenda action.")
  12875. (defun org-mark-entry-for-agenda-action ()
  12876. "Mark the current entry as target of an agenda action.
  12877. Agenda actions are actions executed from the agenda with the key `k',
  12878. which make use of the date at the cursor."
  12879. (interactive)
  12880. (move-marker org-agenda-action-marker
  12881. (save-excursion (org-back-to-heading t) (point))
  12882. (current-buffer))
  12883. (message
  12884. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12885. ;;; Paragraph filling stuff.
  12886. ;; We want this to be just right, so use the full arsenal.
  12887. (defun org-indent-line-function ()
  12888. "Indent line like previous, but further if previous was headline or item."
  12889. (interactive)
  12890. (let* ((pos (point))
  12891. (itemp (org-at-item-p))
  12892. column bpos bcol tpos tcol bullet btype bullet-type)
  12893. ;; Find the previous relevant line
  12894. (beginning-of-line 1)
  12895. (cond
  12896. ((looking-at "#") (setq column 0))
  12897. ((looking-at "\\*+ ") (setq column 0))
  12898. (t
  12899. (beginning-of-line 0)
  12900. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12901. (beginning-of-line 0))
  12902. (cond
  12903. ((looking-at "\\*+[ \t]+")
  12904. (if (not org-adapt-indentation)
  12905. (setq column 0)
  12906. (goto-char (match-end 0))
  12907. (setq column (current-column))))
  12908. ((org-in-item-p)
  12909. (org-beginning-of-item)
  12910. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12911. (setq bpos (match-beginning 1) tpos (match-end 0)
  12912. bcol (progn (goto-char bpos) (current-column))
  12913. tcol (progn (goto-char tpos) (current-column))
  12914. bullet (match-string 1)
  12915. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12916. (if (> tcol (+ bcol org-description-max-indent))
  12917. (setq tcol (+ bcol 5)))
  12918. (if (not itemp)
  12919. (setq column tcol)
  12920. (goto-char pos)
  12921. (beginning-of-line 1)
  12922. (if (looking-at "\\S-")
  12923. (progn
  12924. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12925. (setq bullet (match-string 1)
  12926. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12927. (setq column (if (equal btype bullet-type) bcol tcol)))
  12928. (setq column (org-get-indentation)))))
  12929. (t (setq column (org-get-indentation))))))
  12930. (goto-char pos)
  12931. (if (<= (current-column) (current-indentation))
  12932. (org-indent-line-to column)
  12933. (save-excursion (org-indent-line-to column)))
  12934. (setq column (current-column))
  12935. (beginning-of-line 1)
  12936. (if (looking-at
  12937. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12938. (replace-match (concat "\\1" (format org-property-format
  12939. (match-string 2) (match-string 3)))
  12940. t nil))
  12941. (org-move-to-column column)))
  12942. (defun org-set-autofill-regexps ()
  12943. (interactive)
  12944. ;; In the paragraph separator we include headlines, because filling
  12945. ;; text in a line directly attached to a headline would otherwise
  12946. ;; fill the headline as well.
  12947. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12948. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12949. ;; The paragraph starter includes hand-formatted lists.
  12950. (org-set-local 'paragraph-start
  12951. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12952. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12953. ;; But only if the user has not turned off tables or fixed-width regions
  12954. (org-set-local
  12955. 'auto-fill-inhibit-regexp
  12956. (concat "\\*+ \\|#\\+"
  12957. "\\|[ \t]*" org-keyword-time-regexp
  12958. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12959. (concat
  12960. "\\|[ \t]*["
  12961. (if org-enable-table-editor "|" "")
  12962. (if org-enable-fixed-width-editor ":" "")
  12963. "]"))))
  12964. ;; We use our own fill-paragraph function, to make sure that tables
  12965. ;; and fixed-width regions are not wrapped. That function will pass
  12966. ;; through to `fill-paragraph' when appropriate.
  12967. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12968. ; Adaptive filling: To get full control, first make sure that
  12969. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12970. (org-set-local 'adaptive-fill-regexp "\000")
  12971. (org-set-local 'adaptive-fill-function
  12972. 'org-adaptive-fill-function)
  12973. (org-set-local
  12974. 'align-mode-rules-list
  12975. '((org-in-buffer-settings
  12976. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12977. (modes . '(org-mode))))))
  12978. (defun org-fill-paragraph (&optional justify)
  12979. "Re-align a table, pass through to fill-paragraph if no table."
  12980. (let ((table-p (org-at-table-p))
  12981. (table.el-p (org-at-table.el-p)))
  12982. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12983. (save-excursion (goto-char (point-at-bol))
  12984. (looking-at outline-regexp)))
  12985. t) ; skip headlines
  12986. (table.el-p t) ; skip table.el tables
  12987. (table-p (org-table-align) t) ; align org-mode tables
  12988. (t nil)))) ; call paragraph-fill
  12989. ;; For reference, this is the default value of adaptive-fill-regexp
  12990. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12991. (defun org-adaptive-fill-function ()
  12992. "Return a fill prefix for org-mode files.
  12993. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12994. work correctly."
  12995. (cond ((looking-at "#[ \t]+")
  12996. (match-string 0))
  12997. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12998. (save-excursion
  12999. (if (> (match-end 1) (+ (match-beginning 1)
  13000. org-description-max-indent))
  13001. (goto-char (+ (match-beginning 1) 5))
  13002. (goto-char (match-end 0)))
  13003. (make-string (current-column) ?\ )))
  13004. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13005. (save-excursion
  13006. (goto-char (match-end 0))
  13007. (make-string (current-column) ?\ )))
  13008. (t nil)))
  13009. ;;; Other stuff.
  13010. (defun org-toggle-fixed-width-section (arg)
  13011. "Toggle the fixed-width export.
  13012. If there is no active region, the QUOTE keyword at the current headline is
  13013. inserted or removed. When present, it causes the text between this headline
  13014. and the next to be exported as fixed-width text, and unmodified.
  13015. If there is an active region, this command adds or removes a colon as the
  13016. first character of this line. If the first character of a line is a colon,
  13017. this line is also exported in fixed-width font."
  13018. (interactive "P")
  13019. (let* ((cc 0)
  13020. (regionp (org-region-active-p))
  13021. (beg (if regionp (region-beginning) (point)))
  13022. (end (if regionp (region-end)))
  13023. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13024. (case-fold-search nil)
  13025. (re "[ \t]*\\(:\\)")
  13026. off)
  13027. (if regionp
  13028. (save-excursion
  13029. (goto-char beg)
  13030. (setq cc (current-column))
  13031. (beginning-of-line 1)
  13032. (setq off (looking-at re))
  13033. (while (> nlines 0)
  13034. (setq nlines (1- nlines))
  13035. (beginning-of-line 1)
  13036. (cond
  13037. (arg
  13038. (org-move-to-column cc t)
  13039. (insert ":\n")
  13040. (forward-line -1))
  13041. ((and off (looking-at re))
  13042. (replace-match "" t t nil 1))
  13043. ((not off) (org-move-to-column cc t) (insert ":")))
  13044. (forward-line 1)))
  13045. (save-excursion
  13046. (org-back-to-heading)
  13047. (if (looking-at (concat outline-regexp
  13048. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13049. (replace-match "" t t nil 1)
  13050. (if (looking-at outline-regexp)
  13051. (progn
  13052. (goto-char (match-end 0))
  13053. (insert org-quote-string " "))))))))
  13054. ;;;; Functions extending outline functionality
  13055. (defun org-beginning-of-line (&optional arg)
  13056. "Go to the beginning of the current line. If that is invisible, continue
  13057. to a visible line beginning. This makes the function of C-a more intuitive.
  13058. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13059. first attempt, and only move to after the tags when the cursor is already
  13060. beyond the end of the headline."
  13061. (interactive "P")
  13062. (let ((pos (point)) refpos)
  13063. (beginning-of-line 1)
  13064. (if (bobp)
  13065. nil
  13066. (backward-char 1)
  13067. (if (org-invisible-p)
  13068. (while (and (not (bobp)) (org-invisible-p))
  13069. (backward-char 1)
  13070. (beginning-of-line 1))
  13071. (forward-char 1)))
  13072. (when org-special-ctrl-a/e
  13073. (cond
  13074. ((and (looking-at org-complex-heading-regexp)
  13075. (= (char-after (match-end 1)) ?\ ))
  13076. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13077. (point-at-eol)))
  13078. (goto-char
  13079. (if (eq org-special-ctrl-a/e t)
  13080. (cond ((> pos refpos) refpos)
  13081. ((= pos (point)) refpos)
  13082. (t (point)))
  13083. (cond ((> pos (point)) (point))
  13084. ((not (eq last-command this-command)) (point))
  13085. (t refpos)))))
  13086. ((org-at-item-p)
  13087. (goto-char
  13088. (if (eq org-special-ctrl-a/e t)
  13089. (cond ((> pos (match-end 4)) (match-end 4))
  13090. ((= pos (point)) (match-end 4))
  13091. (t (point)))
  13092. (cond ((> pos (point)) (point))
  13093. ((not (eq last-command this-command)) (point))
  13094. (t (match-end 4))))))))
  13095. (org-no-warnings
  13096. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13097. (defun org-end-of-line (&optional arg)
  13098. "Go to the end of the line.
  13099. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13100. first attempt, and only move to after the tags when the cursor is already
  13101. beyond the end of the headline."
  13102. (interactive "P")
  13103. (if (or (not org-special-ctrl-a/e)
  13104. (not (org-on-heading-p)))
  13105. (end-of-line arg)
  13106. (let ((pos (point)))
  13107. (beginning-of-line 1)
  13108. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13109. (if (eq org-special-ctrl-a/e t)
  13110. (if (or (< pos (match-beginning 1))
  13111. (= pos (match-end 0)))
  13112. (goto-char (match-beginning 1))
  13113. (goto-char (match-end 0)))
  13114. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13115. (goto-char (match-end 0))
  13116. (goto-char (match-beginning 1))))
  13117. (end-of-line arg))))
  13118. (org-no-warnings
  13119. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13120. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13121. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13122. (defun org-kill-line (&optional arg)
  13123. "Kill line, to tags or end of line."
  13124. (interactive "P")
  13125. (cond
  13126. ((or (not org-special-ctrl-k)
  13127. (bolp)
  13128. (not (org-on-heading-p)))
  13129. (call-interactively 'kill-line))
  13130. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13131. (kill-region (point) (match-beginning 1))
  13132. (org-set-tags nil t))
  13133. (t (kill-region (point) (point-at-eol)))))
  13134. (define-key org-mode-map "\C-k" 'org-kill-line)
  13135. (defun org-yank (&optional arg)
  13136. "Yank. If the kill is a subtree, treat it specially.
  13137. This command will look at the current kill and check if is a single
  13138. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13139. cursor is at the beginning of a line or after the stars of a currently
  13140. empty headline, then the yank is handled specially. How exactly depends
  13141. on the value of the following variables, both set by default.
  13142. org-yank-folded-subtrees
  13143. When set, the subtree(s) will be folded after insertion, but only
  13144. if doing so would now swallow text after the yanked text.
  13145. org-yank-adjusted-subtrees
  13146. When set, the subtree will be promoted or demoted in order to
  13147. fit into the local outline tree structure, which means that the level
  13148. will be adjusted so that it becomes the smaller one of the two
  13149. *visible* surrounding headings.
  13150. Any prefix to this command will cause `yank' to be called directly with
  13151. no special treatment. In particular, a simple `C-u' prefix will just
  13152. plainly yank the text as it is.
  13153. \[1] Basically, the test checks if the first non-white line is a heading
  13154. and if there are no other headings with fewer stars."
  13155. (interactive "P")
  13156. (setq this-command 'yank)
  13157. (if arg
  13158. (call-interactively 'yank)
  13159. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13160. (and (org-kill-is-subtree-p)
  13161. (or (bolp)
  13162. (and (looking-at "[ \t]*$")
  13163. (string-match
  13164. "\\`\\*+\\'"
  13165. (buffer-substring (point-at-bol) (point)))))))
  13166. swallowp)
  13167. (cond
  13168. ((and subtreep org-yank-folded-subtrees)
  13169. (let ((beg (point))
  13170. end)
  13171. (if (and subtreep org-yank-adjusted-subtrees)
  13172. (org-paste-subtree nil nil 'for-yank)
  13173. (call-interactively 'yank))
  13174. (setq end (point))
  13175. (goto-char beg)
  13176. (when (and (bolp) subtreep
  13177. (not (setq swallowp
  13178. (org-yank-folding-would-swallow-text beg end))))
  13179. (or (looking-at outline-regexp)
  13180. (re-search-forward (concat "^" outline-regexp) end t))
  13181. (while (and (< (point) end) (looking-at outline-regexp))
  13182. (hide-subtree)
  13183. (org-cycle-show-empty-lines 'folded)
  13184. (condition-case nil
  13185. (outline-forward-same-level 1)
  13186. (error (goto-char end)))))
  13187. (when swallowp
  13188. (message
  13189. "Yanked text not folded because that would swallow text"))
  13190. (goto-char end)
  13191. (skip-chars-forward " \t\n\r")
  13192. (beginning-of-line 1)
  13193. (push-mark beg 'nomsg)))
  13194. ((and subtreep org-yank-adjusted-subtrees)
  13195. (let ((beg (point-at-bol)))
  13196. (org-paste-subtree nil nil 'for-yank)
  13197. (push-mark beg 'nomsg)))
  13198. (t
  13199. (call-interactively 'yank))))))
  13200. (defun org-yank-folding-would-swallow-text (beg end)
  13201. "Would hide-subtree at BEG swallow any text after END?"
  13202. (let (level)
  13203. (save-excursion
  13204. (goto-char beg)
  13205. (when (or (looking-at outline-regexp)
  13206. (re-search-forward (concat "^" outline-regexp) end t))
  13207. (setq level (org-outline-level)))
  13208. (goto-char end)
  13209. (skip-chars-forward " \t\r\n\v\f")
  13210. (if (or (eobp)
  13211. (and (bolp) (looking-at org-outline-regexp)
  13212. (<= (org-outline-level) level)))
  13213. nil ; Nothing would be swallowed
  13214. t)))) ; something would swallow
  13215. (define-key org-mode-map "\C-y" 'org-yank)
  13216. (defun org-invisible-p ()
  13217. "Check if point is at a character currently not visible."
  13218. ;; Early versions of noutline don't have `outline-invisible-p'.
  13219. (if (fboundp 'outline-invisible-p)
  13220. (outline-invisible-p)
  13221. (get-char-property (point) 'invisible)))
  13222. (defun org-invisible-p2 ()
  13223. "Check if point is at a character currently not visible."
  13224. (save-excursion
  13225. (if (and (eolp) (not (bobp))) (backward-char 1))
  13226. ;; Early versions of noutline don't have `outline-invisible-p'.
  13227. (if (fboundp 'outline-invisible-p)
  13228. (outline-invisible-p)
  13229. (get-char-property (point) 'invisible))))
  13230. (defun org-back-to-heading (&optional invisible-ok)
  13231. "Call `outline-back-to-heading', but provide a better error message."
  13232. (condition-case nil
  13233. (outline-back-to-heading invisible-ok)
  13234. (error (error "Before first headline at position %d in buffer %s"
  13235. (point) (current-buffer)))))
  13236. (defun org-before-first-heading-p ()
  13237. "Before first heading?"
  13238. (save-excursion
  13239. (null (re-search-backward "^\\*+ " nil t))))
  13240. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13241. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13242. (defun org-at-heading-or-item-p ()
  13243. (or (org-on-heading-p) (org-at-item-p)))
  13244. (defun org-on-target-p ()
  13245. (or (org-in-regexp org-radio-target-regexp)
  13246. (org-in-regexp org-target-regexp)))
  13247. (defun org-up-heading-all (arg)
  13248. "Move to the heading line of which the present line is a subheading.
  13249. This function considers both visible and invisible heading lines.
  13250. With argument, move up ARG levels."
  13251. (if (fboundp 'outline-up-heading-all)
  13252. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13253. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13254. (defun org-up-heading-safe ()
  13255. "Move to the heading line of which the present line is a subheading.
  13256. This version will not throw an error. It will return the level of the
  13257. headline found, or nil if no higher level is found."
  13258. (let (start-level re)
  13259. (org-back-to-heading t)
  13260. (setq start-level (funcall outline-level))
  13261. (if (equal start-level 1)
  13262. nil
  13263. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13264. (if (re-search-backward re nil t)
  13265. (funcall outline-level)))))
  13266. (defun org-first-sibling-p ()
  13267. "Is this heading the first child of its parents?"
  13268. (interactive)
  13269. (let ((re (concat "^" outline-regexp))
  13270. level l)
  13271. (unless (org-at-heading-p t)
  13272. (error "Not at a heading"))
  13273. (setq level (funcall outline-level))
  13274. (save-excursion
  13275. (if (not (re-search-backward re nil t))
  13276. t
  13277. (setq l (funcall outline-level))
  13278. (< l level)))))
  13279. (defun org-goto-sibling (&optional previous)
  13280. "Goto the next sibling, even if it is invisible.
  13281. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13282. when a sibling was found. When none is found, return nil and don't
  13283. move point."
  13284. (let ((fun (if previous 're-search-backward 're-search-forward))
  13285. (pos (point))
  13286. (re (concat "^" outline-regexp))
  13287. level l)
  13288. (when (condition-case nil (org-back-to-heading t) (error nil))
  13289. (setq level (funcall outline-level))
  13290. (catch 'exit
  13291. (or previous (forward-char 1))
  13292. (while (funcall fun re nil t)
  13293. (setq l (funcall outline-level))
  13294. (when (< l level) (goto-char pos) (throw 'exit nil))
  13295. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13296. (goto-char pos)
  13297. nil))))
  13298. (defun org-show-siblings ()
  13299. "Show all siblings of the current headline."
  13300. (save-excursion
  13301. (while (org-goto-sibling) (org-flag-heading nil)))
  13302. (save-excursion
  13303. (while (org-goto-sibling 'previous)
  13304. (org-flag-heading nil))))
  13305. (defun org-show-hidden-entry ()
  13306. "Show an entry where even the heading is hidden."
  13307. (save-excursion
  13308. (org-show-entry)))
  13309. (defun org-flag-heading (flag &optional entry)
  13310. "Flag the current heading. FLAG non-nil means make invisible.
  13311. When ENTRY is non-nil, show the entire entry."
  13312. (save-excursion
  13313. (org-back-to-heading t)
  13314. ;; Check if we should show the entire entry
  13315. (if entry
  13316. (progn
  13317. (org-show-entry)
  13318. (save-excursion
  13319. (and (outline-next-heading)
  13320. (org-flag-heading nil))))
  13321. (outline-flag-region (max (point-min) (1- (point)))
  13322. (save-excursion (outline-end-of-heading) (point))
  13323. flag))))
  13324. (defun org-forward-same-level (arg)
  13325. "Move forward to the ARG'th subheading at same level as this one.
  13326. Stop at the first and last subheadings of a superior heading.
  13327. This is like outline-forward-same-level, but invisible headings are ok."
  13328. (interactive "p")
  13329. (org-back-to-heading t)
  13330. (while (> arg 0)
  13331. (let ((point-to-move-to (save-excursion
  13332. (org-get-next-sibling))))
  13333. (if point-to-move-to
  13334. (progn
  13335. (goto-char point-to-move-to)
  13336. (setq arg (1- arg)))
  13337. (progn
  13338. (setq arg 0)
  13339. (error "No following same-level heading"))))))
  13340. (defun org-get-next-sibling ()
  13341. "Move to next heading of the same level, and return point.
  13342. If there is no such heading, return nil.
  13343. This is like outline-next-sibling, but invisible headings are ok."
  13344. (let ((level (funcall outline-level)))
  13345. (outline-next-heading)
  13346. (while (and (not (eobp)) (> (funcall outline-level) level))
  13347. (outline-next-heading))
  13348. (if (or (eobp) (< (funcall outline-level) level))
  13349. nil
  13350. (point))))
  13351. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13352. ;; This is an exact copy of the original function, but it uses
  13353. ;; `org-back-to-heading', to make it work also in invisible
  13354. ;; trees. And is uses an invisible-OK argument.
  13355. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13356. (org-back-to-heading invisible-OK)
  13357. (let ((first t)
  13358. (level (funcall outline-level)))
  13359. (while (and (not (eobp))
  13360. (or first (> (funcall outline-level) level)))
  13361. (setq first nil)
  13362. (outline-next-heading))
  13363. (unless to-heading
  13364. (if (memq (preceding-char) '(?\n ?\^M))
  13365. (progn
  13366. ;; Go to end of line before heading
  13367. (forward-char -1)
  13368. (if (memq (preceding-char) '(?\n ?\^M))
  13369. ;; leave blank line before heading
  13370. (forward-char -1))))))
  13371. (point))
  13372. (defun org-show-subtree ()
  13373. "Show everything after this heading at deeper levels."
  13374. (outline-flag-region
  13375. (point)
  13376. (save-excursion
  13377. (outline-end-of-subtree) (outline-next-heading) (point))
  13378. nil))
  13379. (defun org-show-entry ()
  13380. "Show the body directly following this heading.
  13381. Show the heading too, if it is currently invisible."
  13382. (interactive)
  13383. (save-excursion
  13384. (condition-case nil
  13385. (progn
  13386. (org-back-to-heading t)
  13387. (outline-flag-region
  13388. (max (point-min) (1- (point)))
  13389. (save-excursion
  13390. (re-search-forward
  13391. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13392. (or (match-beginning 1) (point-max)))
  13393. nil))
  13394. (error nil))))
  13395. (defun org-make-options-regexp (kwds)
  13396. "Make a regular expression for keyword lines."
  13397. (concat
  13398. "^"
  13399. "#?[ \t]*\\+\\("
  13400. (mapconcat 'regexp-quote kwds "\\|")
  13401. "\\):[ \t]*"
  13402. "\\(.+\\)"))
  13403. ;; Make isearch reveal the necessary context
  13404. (defun org-isearch-end ()
  13405. "Reveal context after isearch exits."
  13406. (when isearch-success ; only if search was successful
  13407. (if (featurep 'xemacs)
  13408. ;; Under XEmacs, the hook is run in the correct place,
  13409. ;; we directly show the context.
  13410. (org-show-context 'isearch)
  13411. ;; In Emacs the hook runs *before* restoring the overlays.
  13412. ;; So we have to use a one-time post-command-hook to do this.
  13413. ;; (Emacs 22 has a special variable, see function `org-mode')
  13414. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13415. isearch-mode-end-hook-quit)
  13416. ;; Only when the isearch was not quitted.
  13417. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13418. 'append 'local)))))
  13419. (defun org-isearch-post-command ()
  13420. "Remove self from hook, and show context."
  13421. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13422. (org-show-context 'isearch))
  13423. ;;;; Integration with and fixes for other packages
  13424. ;;; Imenu support
  13425. (defvar org-imenu-markers nil
  13426. "All markers currently used by Imenu.")
  13427. (make-variable-buffer-local 'org-imenu-markers)
  13428. (defun org-imenu-new-marker (&optional pos)
  13429. "Return a new marker for use by Imenu, and remember the marker."
  13430. (let ((m (make-marker)))
  13431. (move-marker m (or pos (point)))
  13432. (push m org-imenu-markers)
  13433. m))
  13434. (defun org-imenu-get-tree ()
  13435. "Produce the index for Imenu."
  13436. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13437. (setq org-imenu-markers nil)
  13438. (let* ((n org-imenu-depth)
  13439. (re (concat "^" outline-regexp))
  13440. (subs (make-vector (1+ n) nil))
  13441. (last-level 0)
  13442. m tree level head)
  13443. (save-excursion
  13444. (save-restriction
  13445. (widen)
  13446. (goto-char (point-max))
  13447. (while (re-search-backward re nil t)
  13448. (setq level (org-reduced-level (funcall outline-level)))
  13449. (when (<= level n)
  13450. (looking-at org-complex-heading-regexp)
  13451. (setq head (org-link-display-format
  13452. (org-match-string-no-properties 4))
  13453. m (org-imenu-new-marker))
  13454. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13455. (if (>= level last-level)
  13456. (push (cons head m) (aref subs level))
  13457. (push (cons head (aref subs (1+ level))) (aref subs level))
  13458. (loop for i from (1+ level) to n do (aset subs i nil)))
  13459. (setq last-level level)))))
  13460. (aref subs 1)))
  13461. (eval-after-load "imenu"
  13462. '(progn
  13463. (add-hook 'imenu-after-jump-hook
  13464. (lambda ()
  13465. (if (eq major-mode 'org-mode)
  13466. (org-show-context 'org-goto))))))
  13467. (defun org-link-display-format (link)
  13468. "Replace a link with either the description, or the link target
  13469. if no description is present"
  13470. (save-match-data
  13471. (if (string-match org-bracket-link-analytic-regexp link)
  13472. (replace-match (or (match-string 5 link)
  13473. (concat (match-string 1 link)
  13474. (match-string 3 link)))
  13475. nil nil link)
  13476. link)))
  13477. ;; Speedbar support
  13478. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13479. "Overlay marking the agenda restriction line in speedbar.")
  13480. (org-overlay-put org-speedbar-restriction-lock-overlay
  13481. 'face 'org-agenda-restriction-lock)
  13482. (org-overlay-put org-speedbar-restriction-lock-overlay
  13483. 'help-echo "Agendas are currently limited to this item.")
  13484. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13485. (defun org-speedbar-set-agenda-restriction ()
  13486. "Restrict future agenda commands to the location at point in speedbar.
  13487. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13488. (interactive)
  13489. (require 'org-agenda)
  13490. (let (p m tp np dir txt w)
  13491. (cond
  13492. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13493. 'org-imenu t))
  13494. (setq m (get-text-property p 'org-imenu-marker))
  13495. (save-excursion
  13496. (save-restriction
  13497. (set-buffer (marker-buffer m))
  13498. (goto-char m)
  13499. (org-agenda-set-restriction-lock 'subtree))))
  13500. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13501. 'speedbar-function 'speedbar-find-file))
  13502. (setq tp (previous-single-property-change
  13503. (1+ p) 'speedbar-function)
  13504. np (next-single-property-change
  13505. tp 'speedbar-function)
  13506. dir (speedbar-line-directory)
  13507. txt (buffer-substring-no-properties (or tp (point-min))
  13508. (or np (point-max))))
  13509. (save-excursion
  13510. (save-restriction
  13511. (set-buffer (find-file-noselect
  13512. (let ((default-directory dir))
  13513. (expand-file-name txt))))
  13514. (unless (org-mode-p)
  13515. (error "Cannot restrict to non-Org-mode file"))
  13516. (org-agenda-set-restriction-lock 'file))))
  13517. (t (error "Don't know how to restrict Org-mode's agenda")))
  13518. (org-move-overlay org-speedbar-restriction-lock-overlay
  13519. (point-at-bol) (point-at-eol))
  13520. (setq current-prefix-arg nil)
  13521. (org-agenda-maybe-redo)))
  13522. (eval-after-load "speedbar"
  13523. '(progn
  13524. (speedbar-add-supported-extension ".org")
  13525. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13526. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13527. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13528. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13529. (add-hook 'speedbar-visiting-tag-hook
  13530. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13531. ;;; Fixes and Hacks for problems with other packages
  13532. ;; Make flyspell not check words in links, to not mess up our keymap
  13533. (defun org-mode-flyspell-verify ()
  13534. "Don't let flyspell put overlays at active buttons."
  13535. (not (get-text-property (point) 'keymap)))
  13536. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13537. (eval-after-load "bookmark"
  13538. '(if (boundp 'bookmark-after-jump-hook)
  13539. ;; We can use the hook
  13540. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13541. ;; Hook not available, use advice
  13542. (defadvice bookmark-jump (after org-make-visible activate)
  13543. "Make the position visible."
  13544. (org-bookmark-jump-unhide))))
  13545. ;; Make sure saveplace show the location if it was hidden
  13546. (eval-after-load "saveplace"
  13547. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13548. "Make the position visible."
  13549. (org-bookmark-jump-unhide)))
  13550. (defun org-bookmark-jump-unhide ()
  13551. "Unhide the current position, to show the bookmark location."
  13552. (and (org-mode-p)
  13553. (or (org-invisible-p)
  13554. (save-excursion (goto-char (max (point-min) (1- (point))))
  13555. (org-invisible-p)))
  13556. (org-show-context 'bookmark-jump)))
  13557. ;; Make session.el ignore our circular variable
  13558. (eval-after-load "session"
  13559. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13560. ;;;; Experimental code
  13561. (defun org-closed-in-range ()
  13562. "Sparse tree of items closed in a certain time range.
  13563. Still experimental, may disappear in the future."
  13564. (interactive)
  13565. ;; Get the time interval from the user.
  13566. (let* ((time1 (time-to-seconds
  13567. (org-read-date nil 'to-time nil "Starting date: ")))
  13568. (time2 (time-to-seconds
  13569. (org-read-date nil 'to-time nil "End date:")))
  13570. ;; callback function
  13571. (callback (lambda ()
  13572. (let ((time
  13573. (time-to-seconds
  13574. (apply 'encode-time
  13575. (org-parse-time-string
  13576. (match-string 1))))))
  13577. ;; check if time in interval
  13578. (and (>= time time1) (<= time time2))))))
  13579. ;; make tree, check each match with the callback
  13580. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13581. ;;;; Finish up
  13582. (provide 'org)
  13583. (run-hooks 'org-load-hook)
  13584. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13585. ;;; org.el ends here